xref: /petsc/src/dm/impls/plex/plex.c (revision 0f7d6e4ad1c83352383c4dafad6e80ada3d62cac)
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 */
1141525646SFlorian Wechsung 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, DMPLEX_RebalanceSharedPoints;
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++) {
32344a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, i, NULL, &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) {
417a8ad634aSStefano Zampini     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
418a8ad634aSStefano Zampini     /* this need a better fix */
419a8ad634aSStefano Zampini     if (dm->useNatural) {
420a8ad634aSStefano Zampini       if (dm->sfNatural) {
421d930f514SMatthew G. Knepley         const char *vecname;
422d930f514SMatthew G. Knepley         PetscInt    n, nroots;
423d930f514SMatthew G. Knepley 
424ca43db0aSBarry Smith         ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
425d930f514SMatthew G. Knepley         ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
426d930f514SMatthew G. Knepley         if (n == nroots) {
427d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
429d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
430d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
431d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
432d930f514SMatthew G. Knepley         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
433d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
434a8ad634aSStefano Zampini     } else v = originalv;
435a8ad634aSStefano Zampini   } else v = originalv;
436a8ad634aSStefano Zampini 
437d930f514SMatthew G. Knepley   if (ishdf5) {
438d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
43939d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
440d930f514SMatthew G. Knepley #else
441d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
442d930f514SMatthew G. Knepley #endif
443d930f514SMatthew G. Knepley   } else if (isvtk) {
444d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
445d930f514SMatthew G. Knepley   } else {
446d930f514SMatthew G. Knepley     PetscBool isseq;
447d930f514SMatthew G. Knepley 
448d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
449d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
450d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
451d930f514SMatthew G. Knepley   }
452a8ad634aSStefano Zampini   if (v != originalv) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
453d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
454d930f514SMatthew G. Knepley }
455d930f514SMatthew G. Knepley 
4562c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4572c40f234SMatthew G. Knepley {
4582c40f234SMatthew G. Knepley   DM             dm;
4592c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4602c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4612c40f234SMatthew G. Knepley 
4622c40f234SMatthew G. Knepley   PetscFunctionBegin;
4632c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4652c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4662c40f234SMatthew G. Knepley   if (ishdf5) {
4672c40f234SMatthew G. Knepley     DM          dmBC;
4682c40f234SMatthew G. Knepley     Vec         gv;
4692c40f234SMatthew G. Knepley     const char *name;
4702c40f234SMatthew G. Knepley 
4712c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4722c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4732c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4742c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4752c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4762c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4772c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4782c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4792c40f234SMatthew G. Knepley   } else {
4802c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4812c40f234SMatthew G. Knepley   }
4822c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4832c40f234SMatthew G. Knepley }
4842c40f234SMatthew G. Knepley 
4852c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4862c40f234SMatthew G. Knepley {
4872c40f234SMatthew G. Knepley   DM             dm;
4882c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4892c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4902c40f234SMatthew G. Knepley 
4912c40f234SMatthew G. Knepley   PetscFunctionBegin;
4922c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4932c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4942c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4952c40f234SMatthew G. Knepley   if (ishdf5) {
496878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
49739d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
498b136c2c9SMatthew G. Knepley #else
499b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
500878b459fSMatthew G. Knepley #endif
5012c40f234SMatthew G. Knepley   } else {
5022c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
503552f7358SJed Brown   }
504552f7358SJed Brown   PetscFunctionReturn(0);
505552f7358SJed Brown }
506552f7358SJed Brown 
507d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
508d930f514SMatthew G. Knepley {
509d930f514SMatthew G. Knepley   DM                dm;
510d930f514SMatthew G. Knepley   PetscViewerFormat format;
511d930f514SMatthew G. Knepley   PetscBool         ishdf5;
512d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
513d930f514SMatthew G. Knepley 
514d930f514SMatthew G. Knepley   PetscFunctionBegin;
515d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
516d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
517d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
518d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
519d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
520a8ad634aSStefano Zampini     if (dm->useNatural) {
521d930f514SMatthew G. Knepley       if (dm->sfNatural) {
522d930f514SMatthew G. Knepley         if (ishdf5) {
523d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
524d930f514SMatthew G. Knepley           Vec         v;
525d930f514SMatthew G. Knepley           const char *vecname;
526d930f514SMatthew G. Knepley 
527d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
528d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
529d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
53039d25373SMatthew G. Knepley           ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
531d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
532d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
533d930f514SMatthew G. Knepley           ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
534d930f514SMatthew G. Knepley #else
535d930f514SMatthew G. Knepley           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
536d930f514SMatthew G. Knepley #endif
537d930f514SMatthew G. Knepley         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
538d930f514SMatthew G. Knepley       }
539a8ad634aSStefano Zampini     } else {
540a8ad634aSStefano Zampini       ierr = VecLoad_Default(originalv, viewer);CHKERRQ(ierr);
541a8ad634aSStefano Zampini     }
542d930f514SMatthew G. Knepley   }
543d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
544d930f514SMatthew G. Knepley }
545d930f514SMatthew G. Knepley 
5467cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
547731e8ddeSMatthew G. Knepley {
548731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
549731e8ddeSMatthew G. Knepley   Vec                coordinates;
550731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
551731e8ddeSMatthew G. Knepley   const char        *name[4];
552731e8ddeSMatthew G. Knepley   const PetscScalar *a;
553731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
554731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
555731e8ddeSMatthew G. Knepley 
556731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
557731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
559731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
560731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
561731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
562731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
563731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
564731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
565731e8ddeSMatthew G. Knepley   name[1]     = "edge";
566731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
567731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
568731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
569731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
570731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
571731e8ddeSMatthew G. Knepley 
572f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
573731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
575731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
576731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
577731e8ddeSMatthew G. Knepley 
578731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
579731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
580731e8ddeSMatthew G. Knepley       if (!dof) continue;
581731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
582731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
583f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
584731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
585731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
586731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
587731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
58841477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
589731e8ddeSMatthew G. Knepley         }
590731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
591731e8ddeSMatthew G. Knepley       }
592731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
593731e8ddeSMatthew G. Knepley     }
594731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
595731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
596731e8ddeSMatthew G. Knepley   }
597731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
598731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
599731e8ddeSMatthew G. Knepley }
600731e8ddeSMatthew G. Knepley 
6017cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
602552f7358SJed Brown {
603552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
604552f7358SJed Brown   DM                cdm;
605552f7358SJed Brown   DMLabel           markers;
606552f7358SJed Brown   PetscSection      coordSection;
607552f7358SJed Brown   Vec               coordinates;
608552f7358SJed Brown   PetscViewerFormat format;
609552f7358SJed Brown   PetscErrorCode    ierr;
610552f7358SJed Brown 
611552f7358SJed Brown   PetscFunctionBegin;
612552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
613e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
614552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
615552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
616552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
617552f7358SJed Brown     const char *name;
618f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
619552f7358SJed Brown     PetscInt    pStart, pEnd, p;
620552f7358SJed Brown     PetscMPIInt rank, size;
621552f7358SJed Brown 
62282f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
62382f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
624552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
625552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
626552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
627f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
628f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
629f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
630f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
631f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
632e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6334d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
634e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
635552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
636552f7358SJed Brown       PetscInt dof, off, s;
637552f7358SJed Brown 
638552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
639552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
640552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
641e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
642552f7358SJed Brown       }
643552f7358SJed Brown     }
644552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
645e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
646e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
647552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
648552f7358SJed Brown       PetscInt dof, off, c;
649552f7358SJed Brown 
650552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
651552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
652552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
653e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
654552f7358SJed Brown       }
655552f7358SJed Brown     }
656552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6574d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6583d2e540fSStefano Zampini     if (coordSection && coordinates) {
65980180ce3SStefano Zampini       ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
6603d2e540fSStefano Zampini     }
661c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
6627422c0d1SMatthew G. Knepley     if (markers) {ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);}
663552f7358SJed Brown     if (size > 1) {
664552f7358SJed Brown       PetscSF sf;
665552f7358SJed Brown 
666552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
667552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
668552f7358SJed Brown     }
669552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
670552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6710588280cSMatthew G. Knepley     const char  *name, *color;
6720588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6730588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
674552f7358SJed Brown     PetscReal    scale         = 2.0;
67578081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
6760588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6770588280cSMatthew G. Knepley     double       tcoords[3];
678552f7358SJed Brown     PetscScalar *coords;
6790588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
680552f7358SJed Brown     PetscMPIInt  rank, size;
6810588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
682202fd40aSStefano Zampini     PetscBool    plotEdges, flg;
683552f7358SJed Brown 
6840588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
685552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
686c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6870588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6880588280cSMatthew G. Knepley     numColors  = 10;
6890588280cSMatthew G. Knepley     numLColors = 10;
6900588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
691c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
69278081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
693c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
694c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6950588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
696c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6970588280cSMatthew G. Knepley     if (!useColors) {
6980588280cSMatthew G. Knepley       numColors = 3;
6990588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
7000588280cSMatthew G. Knepley     }
701c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
7020588280cSMatthew G. Knepley     if (!useColors) {
7030588280cSMatthew G. Knepley       numLColors = 4;
7040588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
7050588280cSMatthew G. Knepley     }
706202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
707202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
708202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
709202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
71082f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
71182f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
712552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
713770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7140588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
715552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
716552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
717552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7180588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7190588280cSMatthew G. Knepley     if (size > 1) {
720f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
721770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
722770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
723770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
724770b213bSMatthew G Knepley         } else if (p > 0) {
725770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
726770b213bSMatthew G Knepley         }
727770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
728770b213bSMatthew G Knepley       }
7290588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7300588280cSMatthew G. Knepley     }
73178081901SStefano Zampini     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", tikzscale);CHKERRQ(ierr);
732552f7358SJed Brown     /* Plot vertices */
733552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
734552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7354d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
736552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
737552f7358SJed Brown       PetscInt  off, dof, d;
7380588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
739552f7358SJed Brown 
740552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
741552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7420588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
743f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7440588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7450588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
746c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7470588280cSMatthew G. Knepley       }
7480588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7490588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
750552f7358SJed Brown       for (d = 0; d < dof; ++d) {
751552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7520588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
753552f7358SJed Brown       }
7540588280cSMatthew G. Knepley       color = colors[rank%numColors];
7550588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7560588280cSMatthew G. Knepley         PetscInt val;
757c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7580588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7590588280cSMatthew G. Knepley       }
7600588280cSMatthew G. Knepley       if (useNumbers) {
761e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7620588280cSMatthew G. Knepley       } else {
763e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7640588280cSMatthew G. Knepley       }
765552f7358SJed Brown     }
766552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
767552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
768846a3e8bSMatthew G. Knepley     /* Plot cells */
769846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
77078081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
771846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
772846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
773846a3e8bSMatthew G. Knepley         const PetscInt *cone;
774846a3e8bSMatthew G. Knepley 
775846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
776846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
777846a3e8bSMatthew G. Knepley           PetscInt val;
778846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
779846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
780846a3e8bSMatthew G. Knepley         }
781846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
782846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
783846a3e8bSMatthew G. Knepley       }
784846a3e8bSMatthew G. Knepley     } else {
785846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
786846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
787846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
788846a3e8bSMatthew G. Knepley 
789846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
790846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
791846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
792846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
793846a3e8bSMatthew G. Knepley 
794846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
795846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
796846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
797846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
798846a3e8bSMatthew G. Knepley         }
799846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
800846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
801846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
802846a3e8bSMatthew G. Knepley       }
803846a3e8bSMatthew G. Knepley     }
804846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
805846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
806846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
807846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
808846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
809846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
810846a3e8bSMatthew G. Knepley 
811846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
812846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
813846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
814846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
815846a3e8bSMatthew G. Knepley         PetscInt       off;
816846a3e8bSMatthew G. Knepley 
817846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
818846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
819846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
820846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
821846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
822846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
823846a3e8bSMatthew G. Knepley         }
824846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
825846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
826846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
827846a3e8bSMatthew G. Knepley         ++n;
828846a3e8bSMatthew G. Knepley       }
829846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
830846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
831846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
832846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
833846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", ccoords[d]);CHKERRQ(ierr);
834846a3e8bSMatthew G. Knepley       }
835846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
836846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
837846a3e8bSMatthew G. Knepley         PetscInt val;
838846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
839846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
840846a3e8bSMatthew G. Knepley       }
841846a3e8bSMatthew G. Knepley       if (useNumbers) {
842846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
843846a3e8bSMatthew G. Knepley       } else {
844846a3e8bSMatthew 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);
845846a3e8bSMatthew G. Knepley       }
846846a3e8bSMatthew G. Knepley     }
847846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
848552f7358SJed Brown     /* Plot edges */
849202fd40aSStefano Zampini     if (plotEdges) {
850552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
851552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
852552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
853552f7358SJed Brown         const PetscInt *cone;
854552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
855552f7358SJed Brown 
856552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
857f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
858552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
859552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
860552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
861552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
862552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
863552f7358SJed Brown         for (d = 0; d < dof; ++d) {
864202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
865c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
866552f7358SJed Brown         }
8670588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
8680588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
8690588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
8700588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
871f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
8720588280cSMatthew G. Knepley         }
8730588280cSMatthew G. Knepley         color = colors[rank%numColors];
8740588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
8750588280cSMatthew G. Knepley           PetscInt val;
876c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
8770750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
8780588280cSMatthew G. Knepley         }
879e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
880552f7358SJed Brown       }
881552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
882552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
883552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
8840588280cSMatthew G. Knepley     }
885552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8864d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8870588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
888770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
8890588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
8900588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
8910588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
8920588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
893*0f7d6e4aSStefano Zampini   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
894*0f7d6e4aSStefano Zampini     Vec                    cown,acown;
895*0f7d6e4aSStefano Zampini     VecScatter             sct;
896*0f7d6e4aSStefano Zampini     ISLocalToGlobalMapping g2l;
897*0f7d6e4aSStefano Zampini     IS                     gid,acis;
898*0f7d6e4aSStefano Zampini     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
899*0f7d6e4aSStefano Zampini     MPI_Group              ggroup,ngroup;
900*0f7d6e4aSStefano Zampini     PetscScalar            *array,nid;
901*0f7d6e4aSStefano Zampini     const PetscInt         *idxs;
902*0f7d6e4aSStefano Zampini     PetscInt               *idxs2,*start,*adjacency,*work;
903*0f7d6e4aSStefano Zampini     PetscInt64             lm[3],gm[3];
904*0f7d6e4aSStefano Zampini     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
905*0f7d6e4aSStefano Zampini     PetscMPIInt            d1,d2,rank;
906*0f7d6e4aSStefano Zampini 
907*0f7d6e4aSStefano Zampini     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
908*0f7d6e4aSStefano Zampini     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
909*0f7d6e4aSStefano Zampini #if defined(PETSC_HAVE_MPI_SHARED_COMM)
910*0f7d6e4aSStefano Zampini     ierr = MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);CHKERRQ(ierr);
911*0f7d6e4aSStefano Zampini #endif
912*0f7d6e4aSStefano Zampini     if (ncomm != MPI_COMM_NULL) {
913*0f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(comm,&ggroup);CHKERRQ(ierr);
914*0f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(ncomm,&ngroup);CHKERRQ(ierr);
915*0f7d6e4aSStefano Zampini       d1   = 0;
916*0f7d6e4aSStefano Zampini       ierr = MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);CHKERRQ(ierr);
917*0f7d6e4aSStefano Zampini       nid  = d2;
918*0f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ggroup);CHKERRQ(ierr);
919*0f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ngroup);CHKERRQ(ierr);
920*0f7d6e4aSStefano Zampini       ierr = MPI_Comm_free(&ncomm);CHKERRQ(ierr);
921*0f7d6e4aSStefano Zampini     } else nid = 0.0;
922*0f7d6e4aSStefano Zampini 
923*0f7d6e4aSStefano Zampini     /* Get connectivity */
924*0f7d6e4aSStefano Zampini     ierr = DMPlexGetVTKCellHeight(dm,&cellHeight);CHKERRQ(ierr);
925*0f7d6e4aSStefano Zampini     ierr = DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);CHKERRQ(ierr);
926*0f7d6e4aSStefano Zampini 
927*0f7d6e4aSStefano Zampini     /* filter overlapped local cells */
928*0f7d6e4aSStefano Zampini     ierr = DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);CHKERRQ(ierr);
929*0f7d6e4aSStefano Zampini     ierr = ISGetIndices(gid,&idxs);CHKERRQ(ierr);
930*0f7d6e4aSStefano Zampini     ierr = ISGetLocalSize(gid,&cum);CHKERRQ(ierr);
931*0f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&idxs2);CHKERRQ(ierr);
932*0f7d6e4aSStefano Zampini     for (c = cStart, cum = 0; c < cEnd; c++) {
933*0f7d6e4aSStefano Zampini       if (idxs[c-cStart] < 0) continue;
934*0f7d6e4aSStefano Zampini       idxs2[cum++] = idxs[c-cStart];
935*0f7d6e4aSStefano Zampini     }
936*0f7d6e4aSStefano Zampini     ierr = ISRestoreIndices(gid,&idxs);CHKERRQ(ierr);
937*0f7d6e4aSStefano Zampini     if (numVertices != cum) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
938*0f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
939*0f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);CHKERRQ(ierr);
940*0f7d6e4aSStefano Zampini 
941*0f7d6e4aSStefano Zampini     /* support for node-aware cell locality */
942*0f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);CHKERRQ(ierr);
943*0f7d6e4aSStefano Zampini     ierr = VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);CHKERRQ(ierr);
944*0f7d6e4aSStefano Zampini     ierr = VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);CHKERRQ(ierr);
945*0f7d6e4aSStefano Zampini     ierr = VecGetArray(cown,&array);CHKERRQ(ierr);
946*0f7d6e4aSStefano Zampini     for (c = 0; c < numVertices; c++) array[c] = nid;
947*0f7d6e4aSStefano Zampini     ierr = VecRestoreArray(cown,&array);CHKERRQ(ierr);
948*0f7d6e4aSStefano Zampini     ierr = VecScatterCreate(cown,acis,acown,NULL,&sct);CHKERRQ(ierr);
949*0f7d6e4aSStefano Zampini     ierr = VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
950*0f7d6e4aSStefano Zampini     ierr = VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
951*0f7d6e4aSStefano Zampini     ierr = ISDestroy(&acis);CHKERRQ(ierr);
952*0f7d6e4aSStefano Zampini     ierr = VecScatterDestroy(&sct);CHKERRQ(ierr);
953*0f7d6e4aSStefano Zampini     ierr = VecDestroy(&cown);CHKERRQ(ierr);
954*0f7d6e4aSStefano Zampini 
955*0f7d6e4aSStefano Zampini     /* compute edgeCut */
956*0f7d6e4aSStefano Zampini     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
957*0f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&work);CHKERRQ(ierr);
958*0f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingCreateIS(gid,&g2l);CHKERRQ(ierr);
959*0f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
960*0f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
961*0f7d6e4aSStefano Zampini     ierr = VecGetArray(acown,&array);CHKERRQ(ierr);
962*0f7d6e4aSStefano Zampini     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
963*0f7d6e4aSStefano Zampini       PetscInt totl;
964*0f7d6e4aSStefano Zampini 
965*0f7d6e4aSStefano Zampini       totl = start[c+1]-start[c];
966*0f7d6e4aSStefano Zampini       ierr = ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);CHKERRQ(ierr);
967*0f7d6e4aSStefano Zampini       for (i = 0; i < totl; i++) {
968*0f7d6e4aSStefano Zampini         if (work[i] < 0) {
969*0f7d6e4aSStefano Zampini           ect  += 1;
970*0f7d6e4aSStefano Zampini           ectn += (array[i + start[c]] != nid) ? 0 : 1;
971*0f7d6e4aSStefano Zampini         }
972*0f7d6e4aSStefano Zampini       }
973*0f7d6e4aSStefano Zampini     }
974*0f7d6e4aSStefano Zampini     ierr  = PetscFree(work);CHKERRQ(ierr);
975*0f7d6e4aSStefano Zampini     ierr  = VecRestoreArray(acown,&array);CHKERRQ(ierr);
976*0f7d6e4aSStefano Zampini     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
977*0f7d6e4aSStefano Zampini     lm[1] = -numVertices;
978*0f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);CHKERRQ(ierr);
979*0f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Cell balance: %.2f (max %D, min %D",-((double)gm[1])/((double)gm[0]),-(PetscInt)gm[1],(PetscInt)gm[0]);CHKERRQ(ierr);
980*0f7d6e4aSStefano Zampini     lm[0] = ect; /* edgeCut */
981*0f7d6e4aSStefano Zampini     lm[1] = ectn; /* node-aware edgeCut */
982*0f7d6e4aSStefano Zampini     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
983*0f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);CHKERRQ(ierr);
984*0f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);CHKERRQ(ierr);
985*0f7d6e4aSStefano Zampini #if defined(PETSC_HAVE_MPI_SHARED_COMM)
986*0f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),gm[0] ? ((double)(gm[1]))/((double)gm[0]) : 1.);CHKERRQ(ierr);
987*0f7d6e4aSStefano Zampini #else
988*0f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);CHKERRQ(ierr);
989*0f7d6e4aSStefano Zampini #endif
990*0f7d6e4aSStefano Zampini     ierr  = ISLocalToGlobalMappingDestroy(&g2l);CHKERRQ(ierr);
991*0f7d6e4aSStefano Zampini     ierr  = PetscFree(start);CHKERRQ(ierr);
992*0f7d6e4aSStefano Zampini     ierr  = PetscFree(adjacency);CHKERRQ(ierr);
993*0f7d6e4aSStefano Zampini     ierr  = VecDestroy(&acown);CHKERRQ(ierr);
994552f7358SJed Brown   } else {
99582f516ccSBarry Smith     MPI_Comm    comm;
996834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
997f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
998552f7358SJed Brown     PetscInt    pStart, pEnd, p;
999a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
10001143a3c0SMatthew G. Knepley     const char *name;
1001552f7358SJed Brown     PetscMPIInt size;
1002552f7358SJed Brown 
100382f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1004552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1005c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1006f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
10075f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
1008f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1009f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1010f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
1011552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
1012b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
1013a04427b4SStefano Zampini     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, depth > 1 ? &pMax[depth - 2] : NULL, &pMax[0]);CHKERRQ(ierr);
1014b0138d5dSStefano Zampini     ierr = PetscCalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
1015552f7358SJed Brown     if (depth == 1) {
1016552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
1017552f7358SJed Brown       pEnd = pEnd - pStart;
1018a04427b4SStefano Zampini       pMax[0] -= pStart;
1019552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1020a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[0], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1021f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
1022a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
1023a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1024a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
1025a04427b4SStefano Zampini       }
1026552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1027552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
1028552f7358SJed Brown       pEnd = pEnd - pStart;
1029a04427b4SStefano Zampini       pMax[depth] -= pStart;
1030552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1031a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[depth], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1032552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
1033a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
1034a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1035a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
1036a04427b4SStefano Zampini       }
1037552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1038552f7358SJed Brown     } else {
1039cbb7f117SMark Adams       PetscMPIInt rank;
1040cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1041552f7358SJed Brown       for (d = 0; d <= dim; d++) {
1042552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1043834065abSMatthew G. Knepley         pEnd    -= pStart;
1044834065abSMatthew G. Knepley         pMax[d] -= pStart;
1045552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1046834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1047552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
1048834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
1049cbb7f117SMark Adams           if (!rank) {
1050834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1051834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
1052834065abSMatthew G. Knepley           }
1053cbb7f117SMark Adams         }
1054552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1055552f7358SJed Brown       }
1056552f7358SJed Brown     }
1057834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
1058c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1059a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1060a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1061a57dd577SMatthew G Knepley       DMLabel         label;
1062a57dd577SMatthew G Knepley       const char     *name;
1063a57dd577SMatthew G Knepley       IS              valueIS;
1064a57dd577SMatthew G Knepley       const PetscInt *values;
1065a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1066a57dd577SMatthew G Knepley 
1067c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1068c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1069a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1070d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1071a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1072a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1073120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1074a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1075a57dd577SMatthew G Knepley         PetscInt size;
1076a57dd577SMatthew G Knepley 
1077a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1078a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1079d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1080a57dd577SMatthew G Knepley       }
1081a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1082120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1083a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
10844d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1085a57dd577SMatthew G Knepley     }
108634aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
108734aa8a36SMatthew G. Knepley     if (dm->Nf) {
108834aa8a36SMatthew G. Knepley       PetscInt f;
108934aa8a36SMatthew G. Knepley 
109034aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
109134aa8a36SMatthew G. Knepley         const char *name;
109234aa8a36SMatthew G. Knepley 
109334aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
109434aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
109534aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
109634aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
109734aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
109834aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1099ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
110034aa8a36SMatthew G. Knepley         } else {
110134aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
110234aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
110334aa8a36SMatthew G. Knepley         }
110434aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
110534aa8a36SMatthew G. Knepley       }
110634aa8a36SMatthew G. Knepley     }
1107a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
11088e7ff633SMatthew G. Knepley     if (cdm) {
11098e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
11108e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
11118e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
11128e7ff633SMatthew G. Knepley     }
1113552f7358SJed Brown   }
1114552f7358SJed Brown   PetscFunctionReturn(0);
1115552f7358SJed Brown }
1116552f7358SJed Brown 
11177cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1118e412dcbdSMatthew G. Knepley {
1119e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1120e412dcbdSMatthew G. Knepley   DM                 cdm;
1121e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1122e412dcbdSMatthew G. Knepley   Vec                coordinates;
1123e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
112429494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1125e412dcbdSMatthew G. Knepley   PetscBool          isnull;
1126e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
1127cf3064d3SMatthew G. Knepley   PetscMPIInt        rank;
1128e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1129e412dcbdSMatthew G. Knepley 
1130e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1131e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1132e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1133e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1134e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
1135e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1136e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1137e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1138e412dcbdSMatthew G. Knepley 
1139e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1140e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1141e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1142e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1143e412dcbdSMatthew G. Knepley 
1144e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1145e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1146e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
11470c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
11480c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1149e412dcbdSMatthew G. Knepley   }
1150e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
115129494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
115229494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
115329494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1154e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1155e412dcbdSMatthew G. Knepley 
1156cf3064d3SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1157cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1158cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1159cf3064d3SMatthew G. Knepley     PetscInt     numCoords,coneSize;
1160cf3064d3SMatthew G. Knepley 
1161cf3064d3SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1162cf3064d3SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1163cf3064d3SMatthew G. Knepley     switch (coneSize) {
1164cf3064d3SMatthew G. Knepley     case 3:
1165cf3064d3SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1166cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1167cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1168cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1169cf3064d3SMatthew G. Knepley       break;
1170cf3064d3SMatthew G. Knepley     case 4:
1171cf3064d3SMatthew G. Knepley       ierr = PetscDrawRectangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1172cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1173cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1174cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1175cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1176cf3064d3SMatthew G. Knepley       break;
1177cf3064d3SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1178cf3064d3SMatthew G. Knepley     }
1179cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1180cf3064d3SMatthew G. Knepley   }
1181e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1182e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
118329494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
1184e412dcbdSMatthew G. Knepley 
118529494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1186e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
118729494db1SLisandro Dalcin     switch (coneSize) {
118829494db1SLisandro Dalcin     case 3:
11890c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
11900c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
11910c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1192e412dcbdSMatthew G. Knepley       break;
119329494db1SLisandro Dalcin     case 4:
11940c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
11950c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
11960c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
11970c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1198e412dcbdSMatthew G. Knepley       break;
119929494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1200e412dcbdSMatthew G. Knepley     }
1201e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1202e412dcbdSMatthew G. Knepley   }
1203e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1204e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1205e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1206e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1207e412dcbdSMatthew G. Knepley }
1208e412dcbdSMatthew G. Knepley 
1209552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1210552f7358SJed Brown {
1211e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
1212002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1213552f7358SJed Brown   PetscErrorCode ierr;
1214552f7358SJed Brown 
1215552f7358SJed Brown   PetscFunctionBegin;
1216552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1217552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1218552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1219fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
1220c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
1221e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
12228135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
1223552f7358SJed Brown   if (iascii) {
12248135c375SStefano Zampini     PetscViewerFormat format;
12258135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
12268135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
12278135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
12288135c375SStefano Zampini     } else {
1229552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
12308135c375SStefano Zampini     }
1231c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1232c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
123339d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1234c6ccd67eSMatthew G. Knepley #else
1235c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1236552f7358SJed Brown #endif
1237e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1238fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1239e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1240e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
12418135c375SStefano Zampini   } else if (isglvis) {
12428135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
124362201deeSVaclav Hapla   } else {
1244a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1245fcf6c8fdSToby Isaac   }
1246cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1247cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1248cb3ba0daSMatthew G. Knepley   if (flg) {
1249cb3ba0daSMatthew G. Knepley     Vec ranks;
1250cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1251cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1252cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1253cb3ba0daSMatthew G. Knepley   }
1254002a2709SMatthew G. Knepley   /* Optionally view a label */
1255002a2709SMatthew G. Knepley   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
1256002a2709SMatthew G. Knepley   if (flg) {
1257002a2709SMatthew G. Knepley     DMLabel label;
1258002a2709SMatthew G. Knepley     Vec     val;
1259002a2709SMatthew G. Knepley 
1260002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1261002a2709SMatthew 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);
1262002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1263002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1264002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1265002a2709SMatthew G. Knepley   }
1266552f7358SJed Brown   PetscFunctionReturn(0);
1267552f7358SJed Brown }
1268552f7358SJed Brown 
12692c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
12702c40f234SMatthew G. Knepley {
1271d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
12722c40f234SMatthew G. Knepley   PetscErrorCode ierr;
12732c40f234SMatthew G. Knepley 
12742c40f234SMatthew G. Knepley   PetscFunctionBegin;
12752c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12762c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
12772c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1278d4f5a9a0SVaclav Hapla   if (ishdf5) {
12792c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
12809c48423bSVaclav Hapla     PetscViewerFormat format;
12819c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
12829c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
12839c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1284509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
128539d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1286509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
12872c40f234SMatthew G. Knepley #else
12882c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1289552f7358SJed Brown #endif
1290d4f5a9a0SVaclav Hapla   } else {
1291d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1292552f7358SJed Brown   }
1293552f7358SJed Brown   PetscFunctionReturn(0);
1294552f7358SJed Brown }
1295552f7358SJed Brown 
1296552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1297552f7358SJed Brown {
1298552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1299552f7358SJed Brown   PetscErrorCode ierr;
1300552f7358SJed Brown 
1301552f7358SJed Brown   PetscFunctionBegin;
13028135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
13038135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
13040d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1305552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1306552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1307552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1308552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1309be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1310552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1311552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1312d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1313d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
131477623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1315a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1316552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1317552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1318a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1319a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1320d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1321d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1322d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1323d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1324d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1325d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1326fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1327552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1328552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1329552f7358SJed Brown   PetscFunctionReturn(0);
1330552f7358SJed Brown }
1331552f7358SJed Brown 
1332b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1333552f7358SJed Brown {
13348d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1335acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1336552f7358SJed Brown   PetscInt               localSize;
1337837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1338552f7358SJed Brown   PetscErrorCode         ierr;
1339b412c318SBarry Smith   MatType                mtype;
13401428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1341552f7358SJed Brown 
1342552f7358SJed Brown   PetscFunctionBegin;
1343607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1344b412c318SBarry Smith   mtype = dm->mattype;
1345e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1346552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1347552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
134882f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1349552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1350552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1351552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1352acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1353acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1354552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1355552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1356552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1357552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1358552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1359552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1360552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1361837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1362552f7358SJed Brown   if (!isShell) {
1363be36d101SStefano Zampini     PetscSection subSection;
1364837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
13650be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1366fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1367552f7358SJed Brown 
136800140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1369be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1370be36d101SStefano Zampini       PetscSection section;
1371be36d101SStefano Zampini       PetscInt     size;
1372be36d101SStefano Zampini 
1373e87a4003SBarry Smith       ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1374be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1375be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1376be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1377be36d101SStefano Zampini     } else {
137800140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1379be36d101SStefano Zampini     }
1380552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1381be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1382a9d99c84SMatthew G. Knepley       PetscInt bdof;
1383a9d99c84SMatthew G. Knepley 
1384552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1385fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
13861d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
13871d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
13881d17a0a3SMatthew G. Knepley       if (dof) {
13891d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1390be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1391be36d101SStefano Zampini       }
1392be36d101SStefano Zampini       if (isMatIS) {
1393be36d101SStefano Zampini         PetscInt loff,c,off;
1394be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1395be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1396be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1397552f7358SJed Brown       }
13982a28c762SMatthew G Knepley     }
13992a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
14000be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
14010be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
14020be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
14030be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
14044fa26246SMatthew G. Knepley     bs = bs < 0 ? 1 : bs;
1405be36d101SStefano Zampini     if (isMatIS) {
14064fa26246SMatthew G. Knepley       PetscInt l;
14074fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
14084fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
14094fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1410be36d101SStefano Zampini     }
1411be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1412be36d101SStefano Zampini     if (isMatIS) {
1413be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1414be36d101SStefano Zampini     }
14151795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
14168d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1417552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1418552f7358SJed Brown   }
1419b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1420552f7358SJed Brown   PetscFunctionReturn(0);
1421552f7358SJed Brown }
1422552f7358SJed Brown 
14237cd05799SMatthew G. Knepley /*@
1424a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1425be36d101SStefano Zampini 
1426be36d101SStefano Zampini   Not collective
1427be36d101SStefano Zampini 
1428be36d101SStefano Zampini   Input Parameter:
1429be36d101SStefano Zampini . mesh - The DMPlex
1430be36d101SStefano Zampini 
1431be36d101SStefano Zampini   Output Parameters:
1432be36d101SStefano Zampini . subsection - The subdomain section
1433be36d101SStefano Zampini 
1434be36d101SStefano Zampini   Level: developer
1435be36d101SStefano Zampini 
1436be36d101SStefano Zampini .seealso:
14377cd05799SMatthew G. Knepley @*/
1438be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1439be36d101SStefano Zampini {
1440be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1441be36d101SStefano Zampini   PetscErrorCode ierr;
1442be36d101SStefano Zampini 
1443be36d101SStefano Zampini   PetscFunctionBegin;
1444be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1445be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1446be36d101SStefano Zampini     PetscSection section;
1447be36d101SStefano Zampini     PetscSF      sf;
1448be36d101SStefano Zampini 
1449be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1450e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
1451be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1452be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1453be36d101SStefano Zampini   }
1454be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1455be36d101SStefano Zampini   PetscFunctionReturn(0);
1456be36d101SStefano Zampini }
1457be36d101SStefano Zampini 
1458552f7358SJed Brown /*@
1459552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1460552f7358SJed Brown 
1461552f7358SJed Brown   Not collective
1462552f7358SJed Brown 
1463552f7358SJed Brown   Input Parameter:
1464552f7358SJed Brown . mesh - The DMPlex
1465552f7358SJed Brown 
1466552f7358SJed Brown   Output Parameters:
1467552f7358SJed Brown + pStart - The first mesh point
1468552f7358SJed Brown - pEnd   - The upper bound for mesh points
1469552f7358SJed Brown 
1470552f7358SJed Brown   Level: beginner
1471552f7358SJed Brown 
1472552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1473552f7358SJed Brown @*/
1474552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1475552f7358SJed Brown {
1476552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1477552f7358SJed Brown   PetscErrorCode ierr;
1478552f7358SJed Brown 
1479552f7358SJed Brown   PetscFunctionBegin;
1480552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1481552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1482552f7358SJed Brown   PetscFunctionReturn(0);
1483552f7358SJed Brown }
1484552f7358SJed Brown 
1485552f7358SJed Brown /*@
1486552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1487552f7358SJed Brown 
1488552f7358SJed Brown   Not collective
1489552f7358SJed Brown 
1490552f7358SJed Brown   Input Parameters:
1491552f7358SJed Brown + mesh - The DMPlex
1492552f7358SJed Brown . pStart - The first mesh point
1493552f7358SJed Brown - pEnd   - The upper bound for mesh points
1494552f7358SJed Brown 
1495552f7358SJed Brown   Output Parameters:
1496552f7358SJed Brown 
1497552f7358SJed Brown   Level: beginner
1498552f7358SJed Brown 
1499552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1500552f7358SJed Brown @*/
1501552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1502552f7358SJed Brown {
1503552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1504552f7358SJed Brown   PetscErrorCode ierr;
1505552f7358SJed Brown 
1506552f7358SJed Brown   PetscFunctionBegin;
1507552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1508552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1509552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1510552f7358SJed Brown   PetscFunctionReturn(0);
1511552f7358SJed Brown }
1512552f7358SJed Brown 
1513552f7358SJed Brown /*@
1514eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1515552f7358SJed Brown 
1516552f7358SJed Brown   Not collective
1517552f7358SJed Brown 
1518552f7358SJed Brown   Input Parameters:
1519552f7358SJed Brown + mesh - The DMPlex
1520eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1521552f7358SJed Brown 
1522552f7358SJed Brown   Output Parameter:
1523552f7358SJed Brown . size - The cone size for point p
1524552f7358SJed Brown 
1525552f7358SJed Brown   Level: beginner
1526552f7358SJed Brown 
1527552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1528552f7358SJed Brown @*/
1529552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1530552f7358SJed Brown {
1531552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1532552f7358SJed Brown   PetscErrorCode ierr;
1533552f7358SJed Brown 
1534552f7358SJed Brown   PetscFunctionBegin;
1535552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1536552f7358SJed Brown   PetscValidPointer(size, 3);
1537552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1538552f7358SJed Brown   PetscFunctionReturn(0);
1539552f7358SJed Brown }
1540552f7358SJed Brown 
1541552f7358SJed Brown /*@
1542eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1543552f7358SJed Brown 
1544552f7358SJed Brown   Not collective
1545552f7358SJed Brown 
1546552f7358SJed Brown   Input Parameters:
1547552f7358SJed Brown + mesh - The DMPlex
1548eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1549552f7358SJed Brown - size - The cone size for point p
1550552f7358SJed Brown 
1551552f7358SJed Brown   Output Parameter:
1552552f7358SJed Brown 
1553552f7358SJed Brown   Note:
1554552f7358SJed Brown   This should be called after DMPlexSetChart().
1555552f7358SJed Brown 
1556552f7358SJed Brown   Level: beginner
1557552f7358SJed Brown 
1558552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1559552f7358SJed Brown @*/
1560552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1561552f7358SJed Brown {
1562552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1563552f7358SJed Brown   PetscErrorCode ierr;
1564552f7358SJed Brown 
1565552f7358SJed Brown   PetscFunctionBegin;
1566552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1567552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
15680d644c17SKarl Rupp 
1569552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1570552f7358SJed Brown   PetscFunctionReturn(0);
1571552f7358SJed Brown }
1572552f7358SJed Brown 
1573f5a469b9SMatthew G. Knepley /*@
1574eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1575f5a469b9SMatthew G. Knepley 
1576f5a469b9SMatthew G. Knepley   Not collective
1577f5a469b9SMatthew G. Knepley 
1578f5a469b9SMatthew G. Knepley   Input Parameters:
1579f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1580eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1581f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1582f5a469b9SMatthew G. Knepley 
1583f5a469b9SMatthew G. Knepley   Output Parameter:
1584f5a469b9SMatthew G. Knepley 
1585f5a469b9SMatthew G. Knepley   Note:
1586f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1587f5a469b9SMatthew G. Knepley 
1588f5a469b9SMatthew G. Knepley   Level: beginner
1589f5a469b9SMatthew G. Knepley 
1590f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1591f5a469b9SMatthew G. Knepley @*/
1592f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1593f5a469b9SMatthew G. Knepley {
1594f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1595f5a469b9SMatthew G. Knepley   PetscInt       csize;
1596f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1597f5a469b9SMatthew G. Knepley 
1598f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1599f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1600f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1601f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1602f5a469b9SMatthew G. Knepley 
1603f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1604f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1605f5a469b9SMatthew G. Knepley }
1606f5a469b9SMatthew G. Knepley 
1607552f7358SJed Brown /*@C
1608eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1609552f7358SJed Brown 
1610552f7358SJed Brown   Not collective
1611552f7358SJed Brown 
1612552f7358SJed Brown   Input Parameters:
1613833c876bSVaclav Hapla + dm - The DMPlex
1614eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1615552f7358SJed Brown 
1616552f7358SJed Brown   Output Parameter:
1617552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1618552f7358SJed Brown 
1619552f7358SJed Brown   Level: beginner
1620552f7358SJed Brown 
16213813dfbdSMatthew G Knepley   Fortran Notes:
16223813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16233813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16243813dfbdSMatthew G Knepley 
16250ce7577fSVaclav Hapla .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1626552f7358SJed Brown @*/
1627552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1628552f7358SJed Brown {
1629552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1630552f7358SJed Brown   PetscInt       off;
1631552f7358SJed Brown   PetscErrorCode ierr;
1632552f7358SJed Brown 
1633552f7358SJed Brown   PetscFunctionBegin;
1634552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1635552f7358SJed Brown   PetscValidPointer(cone, 3);
1636552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1637552f7358SJed Brown   *cone = &mesh->cones[off];
1638552f7358SJed Brown   PetscFunctionReturn(0);
1639552f7358SJed Brown }
1640552f7358SJed Brown 
16410ce7577fSVaclav Hapla /*@C
16420ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
16430ce7577fSVaclav Hapla 
16440ce7577fSVaclav Hapla   Not collective
16450ce7577fSVaclav Hapla 
16460ce7577fSVaclav Hapla   Input Parameters:
16470ce7577fSVaclav Hapla + dm - The DMPlex
16480ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
16490ce7577fSVaclav Hapla 
16500ce7577fSVaclav Hapla   Output Parameter:
16510ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
16520ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
16530ce7577fSVaclav Hapla 
16540ce7577fSVaclav Hapla   Level: intermediate
16550ce7577fSVaclav Hapla 
1656d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
16570ce7577fSVaclav Hapla @*/
16580ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
16590ce7577fSVaclav Hapla {
16600ce7577fSVaclav Hapla   PetscSection        cs, newcs;
16610ce7577fSVaclav Hapla   PetscInt            *cones;
16620ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
16630ce7577fSVaclav Hapla   PetscInt            n;
16640ce7577fSVaclav Hapla   PetscErrorCode      ierr;
16650ce7577fSVaclav Hapla 
16660ce7577fSVaclav Hapla   PetscFunctionBegin;
16670ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
16680ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
16690ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
16700ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
16710ce7577fSVaclav Hapla   if (pCones) {
16720ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
16730ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
16740ce7577fSVaclav Hapla   }
16750ce7577fSVaclav Hapla   PetscFunctionReturn(0);
16760ce7577fSVaclav Hapla }
16770ce7577fSVaclav Hapla 
1678d4636a37SVaclav Hapla static PetscErrorCode DMPlexGetConeRecursive_Private(DM dm, PetscInt *n_inout, const PetscInt points[], PetscInt *offset_inout, PetscInt buf[])
1679d4636a37SVaclav Hapla {
1680d4636a37SVaclav Hapla   PetscInt p, n, cn, i;
1681d4636a37SVaclav Hapla   const PetscInt *cone;
1682d4636a37SVaclav Hapla   PetscErrorCode ierr;
1683d4636a37SVaclav Hapla 
1684d4636a37SVaclav Hapla   PetscFunctionBegin;
1685d4636a37SVaclav Hapla   n = *n_inout;
1686d4636a37SVaclav Hapla   *n_inout = 0;
1687d4636a37SVaclav Hapla   for (i=0; i<n; i++) {
1688d4636a37SVaclav Hapla     p = points[i];
1689d4636a37SVaclav Hapla     ierr = DMPlexGetConeSize(dm, p, &cn);CHKERRQ(ierr);
1690d4636a37SVaclav Hapla     if (!cn) {
1691d4636a37SVaclav Hapla       cn = 1;
1692d4636a37SVaclav Hapla       if (buf) {
1693d4636a37SVaclav Hapla         buf[*offset_inout] = p;
1694d4636a37SVaclav Hapla         ++(*offset_inout);
1695d4636a37SVaclav Hapla       }
1696d4636a37SVaclav Hapla     } else {
1697d4636a37SVaclav Hapla       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1698d4636a37SVaclav Hapla       ierr = DMPlexGetConeRecursive_Private(dm, &cn, cone, offset_inout, buf);CHKERRQ(ierr);
1699d4636a37SVaclav Hapla     }
1700d4636a37SVaclav Hapla     *n_inout += cn;
1701d4636a37SVaclav Hapla   }
1702d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1703d4636a37SVaclav Hapla }
1704d4636a37SVaclav Hapla 
1705d4636a37SVaclav Hapla /*@C
1706d4636a37SVaclav 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.
1707d4636a37SVaclav Hapla 
1708d4636a37SVaclav Hapla   Not collective
1709d4636a37SVaclav Hapla 
1710d4636a37SVaclav Hapla   Input Parameters:
1711d4636a37SVaclav Hapla + dm - The DMPlex
1712d4636a37SVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
1713d4636a37SVaclav Hapla 
1714d4636a37SVaclav Hapla   Output Parameter:
1715d4636a37SVaclav Hapla . pCones - An array of recursively expanded cones, i.e. containing only vertices, and each of them can be present multiple times
1716d4636a37SVaclav Hapla 
1717d4636a37SVaclav Hapla   Level: advanced
1718d4636a37SVaclav Hapla 
1719d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple()
1720d4636a37SVaclav Hapla @*/
1721d4636a37SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS p, IS *pCones)
1722d4636a37SVaclav Hapla {
1723d4636a37SVaclav Hapla   const PetscInt      *arr=NULL;
1724d4636a37SVaclav Hapla   PetscInt            *cpoints=NULL;
1725d4636a37SVaclav Hapla   PetscInt            n, cn;
1726d4636a37SVaclav Hapla   PetscInt            zero;
1727d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1728d4636a37SVaclav Hapla 
1729d4636a37SVaclav Hapla   PetscFunctionBegin;
1730d4636a37SVaclav Hapla   ierr = ISGetLocalSize(p, &n);CHKERRQ(ierr);
1731d4636a37SVaclav Hapla   ierr = ISGetIndices(p, &arr);CHKERRQ(ierr);
1732d4636a37SVaclav Hapla   zero = 0;
1733d4636a37SVaclav Hapla   /* first figure out the total number of returned points */
1734d4636a37SVaclav Hapla   cn = n;
1735d4636a37SVaclav Hapla   ierr = DMPlexGetConeRecursive_Private(dm, &cn, arr, &zero, NULL);CHKERRQ(ierr);
1736d4636a37SVaclav Hapla   ierr = PetscMalloc1(cn, &cpoints);CHKERRQ(ierr);
1737d4636a37SVaclav Hapla   /* now get recursive cones themselves */
1738d4636a37SVaclav Hapla   ierr = DMPlexGetConeRecursive_Private(dm, &n, arr, &zero, cpoints);CHKERRQ(ierr);
1739d4636a37SVaclav Hapla   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, cpoints, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
1740d4636a37SVaclav Hapla   ierr = ISRestoreIndices(p, &arr);CHKERRQ(ierr);
1741d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1742d4636a37SVaclav Hapla }
1743d4636a37SVaclav Hapla 
1744552f7358SJed Brown /*@
174592371b87SBarry 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
1746552f7358SJed Brown 
1747552f7358SJed Brown   Not collective
1748552f7358SJed Brown 
1749552f7358SJed Brown   Input Parameters:
1750552f7358SJed Brown + mesh - The DMPlex
1751eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1752552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1753552f7358SJed Brown 
1754552f7358SJed Brown   Output Parameter:
1755552f7358SJed Brown 
1756552f7358SJed Brown   Note:
1757552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1758552f7358SJed Brown 
175992371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
176092371b87SBarry Smith 
1761552f7358SJed Brown   Level: beginner
1762552f7358SJed Brown 
176392371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1764552f7358SJed Brown @*/
1765552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1766552f7358SJed Brown {
1767552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1768552f7358SJed Brown   PetscInt       pStart, pEnd;
1769552f7358SJed Brown   PetscInt       dof, off, c;
1770552f7358SJed Brown   PetscErrorCode ierr;
1771552f7358SJed Brown 
1772552f7358SJed Brown   PetscFunctionBegin;
1773552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1774552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1775552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1776552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1777552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
177882f516ccSBarry 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);
1779552f7358SJed Brown   for (c = 0; c < dof; ++c) {
178082f516ccSBarry 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);
1781552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1782552f7358SJed Brown   }
1783552f7358SJed Brown   PetscFunctionReturn(0);
1784552f7358SJed Brown }
1785552f7358SJed Brown 
1786552f7358SJed Brown /*@C
1787eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1788552f7358SJed Brown 
1789552f7358SJed Brown   Not collective
1790552f7358SJed Brown 
1791552f7358SJed Brown   Input Parameters:
1792552f7358SJed Brown + mesh - The DMPlex
1793eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1794552f7358SJed Brown 
1795552f7358SJed Brown   Output Parameter:
1796552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1797552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1798552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1799552f7358SJed Brown                     the index of the cone point on which to start.
1800552f7358SJed Brown 
1801552f7358SJed Brown   Level: beginner
1802552f7358SJed Brown 
18033813dfbdSMatthew G Knepley   Fortran Notes:
18043813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18053813dfbdSMatthew G Knepley   include petsc.h90 in your code.
18063813dfbdSMatthew G Knepley 
18073813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1808552f7358SJed Brown 
1809552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1810552f7358SJed Brown @*/
1811552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1812552f7358SJed Brown {
1813552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1814552f7358SJed Brown   PetscInt       off;
1815552f7358SJed Brown   PetscErrorCode ierr;
1816552f7358SJed Brown 
1817552f7358SJed Brown   PetscFunctionBegin;
1818552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1819552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1820552f7358SJed Brown   {
1821552f7358SJed Brown     PetscInt dof;
1822552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1823552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1824552f7358SJed Brown   }
1825552f7358SJed Brown #endif
1826552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
18270d644c17SKarl Rupp 
1828552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1829552f7358SJed Brown   PetscFunctionReturn(0);
1830552f7358SJed Brown }
1831552f7358SJed Brown 
1832552f7358SJed Brown /*@
1833eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1834552f7358SJed Brown 
1835552f7358SJed Brown   Not collective
1836552f7358SJed Brown 
1837552f7358SJed Brown   Input Parameters:
1838552f7358SJed Brown + mesh - The DMPlex
1839eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1840552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1841552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1842552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1843552f7358SJed Brown                     the index of the cone point on which to start.
1844552f7358SJed Brown 
1845552f7358SJed Brown   Output Parameter:
1846552f7358SJed Brown 
1847552f7358SJed Brown   Note:
1848552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1849552f7358SJed Brown 
1850552f7358SJed Brown   Level: beginner
1851552f7358SJed Brown 
1852552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1853552f7358SJed Brown @*/
1854552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1855552f7358SJed Brown {
1856552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1857552f7358SJed Brown   PetscInt       pStart, pEnd;
1858552f7358SJed Brown   PetscInt       dof, off, c;
1859552f7358SJed Brown   PetscErrorCode ierr;
1860552f7358SJed Brown 
1861552f7358SJed Brown   PetscFunctionBegin;
1862552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1863552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1864552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1865552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1866552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
186782f516ccSBarry 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);
1868552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1869552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1870552f7358SJed Brown 
1871552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
187282f516ccSBarry 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);
1873552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1874552f7358SJed Brown   }
1875552f7358SJed Brown   PetscFunctionReturn(0);
1876552f7358SJed Brown }
1877552f7358SJed Brown 
18787cd05799SMatthew G. Knepley /*@
1879eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
18807cd05799SMatthew G. Knepley 
18817cd05799SMatthew G. Knepley   Not collective
18827cd05799SMatthew G. Knepley 
18837cd05799SMatthew G. Knepley   Input Parameters:
18847cd05799SMatthew G. Knepley + mesh - The DMPlex
1885eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
18867cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
18877cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
18887cd05799SMatthew G. Knepley 
18897cd05799SMatthew G. Knepley   Level: beginner
18907cd05799SMatthew G. Knepley 
18917cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
18927cd05799SMatthew G. Knepley @*/
1893552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1894552f7358SJed Brown {
1895552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1896552f7358SJed Brown   PetscInt       pStart, pEnd;
1897552f7358SJed Brown   PetscInt       dof, off;
1898552f7358SJed Brown   PetscErrorCode ierr;
1899552f7358SJed Brown 
1900552f7358SJed Brown   PetscFunctionBegin;
1901552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1902552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
190382f516ccSBarry 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);
190482f516ccSBarry 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);
190577c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
190677c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
190777c88f5bSMatthew 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);
1908552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1909552f7358SJed Brown   PetscFunctionReturn(0);
1910552f7358SJed Brown }
1911552f7358SJed Brown 
19127cd05799SMatthew G. Knepley /*@
1913eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
19147cd05799SMatthew G. Knepley 
19157cd05799SMatthew G. Knepley   Not collective
19167cd05799SMatthew G. Knepley 
19177cd05799SMatthew G. Knepley   Input Parameters:
19187cd05799SMatthew G. Knepley + mesh - The DMPlex
1919eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
19207cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
19217cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
19227cd05799SMatthew G. Knepley 
19237cd05799SMatthew G. Knepley   Level: beginner
19247cd05799SMatthew G. Knepley 
19257cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
19267cd05799SMatthew G. Knepley @*/
192777c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
192877c88f5bSMatthew G Knepley {
192977c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
193077c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
193177c88f5bSMatthew G Knepley   PetscInt       dof, off;
193277c88f5bSMatthew G Knepley   PetscErrorCode ierr;
193377c88f5bSMatthew G Knepley 
193477c88f5bSMatthew G Knepley   PetscFunctionBegin;
193577c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
193677c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
193777c88f5bSMatthew 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);
193877c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
193977c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
194077c88f5bSMatthew 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);
194177c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
194277c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
194377c88f5bSMatthew G Knepley }
194477c88f5bSMatthew G Knepley 
1945552f7358SJed Brown /*@
1946eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
1947552f7358SJed Brown 
1948552f7358SJed Brown   Not collective
1949552f7358SJed Brown 
1950552f7358SJed Brown   Input Parameters:
1951552f7358SJed Brown + mesh - The DMPlex
1952eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1953552f7358SJed Brown 
1954552f7358SJed Brown   Output Parameter:
1955552f7358SJed Brown . size - The support size for point p
1956552f7358SJed Brown 
1957552f7358SJed Brown   Level: beginner
1958552f7358SJed Brown 
1959552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1960552f7358SJed Brown @*/
1961552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1962552f7358SJed Brown {
1963552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1964552f7358SJed Brown   PetscErrorCode ierr;
1965552f7358SJed Brown 
1966552f7358SJed Brown   PetscFunctionBegin;
1967552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1968552f7358SJed Brown   PetscValidPointer(size, 3);
1969552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1970552f7358SJed Brown   PetscFunctionReturn(0);
1971552f7358SJed Brown }
1972552f7358SJed Brown 
1973552f7358SJed Brown /*@
1974eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
1975552f7358SJed Brown 
1976552f7358SJed Brown   Not collective
1977552f7358SJed Brown 
1978552f7358SJed Brown   Input Parameters:
1979552f7358SJed Brown + mesh - The DMPlex
1980eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1981552f7358SJed Brown - size - The support size for point p
1982552f7358SJed Brown 
1983552f7358SJed Brown   Output Parameter:
1984552f7358SJed Brown 
1985552f7358SJed Brown   Note:
1986552f7358SJed Brown   This should be called after DMPlexSetChart().
1987552f7358SJed Brown 
1988552f7358SJed Brown   Level: beginner
1989552f7358SJed Brown 
1990552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1991552f7358SJed Brown @*/
1992552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1993552f7358SJed Brown {
1994552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1995552f7358SJed Brown   PetscErrorCode ierr;
1996552f7358SJed Brown 
1997552f7358SJed Brown   PetscFunctionBegin;
1998552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1999552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
20000d644c17SKarl Rupp 
2001552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2002552f7358SJed Brown   PetscFunctionReturn(0);
2003552f7358SJed Brown }
2004552f7358SJed Brown 
2005552f7358SJed Brown /*@C
2006eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2007552f7358SJed Brown 
2008552f7358SJed Brown   Not collective
2009552f7358SJed Brown 
2010552f7358SJed Brown   Input Parameters:
2011552f7358SJed Brown + mesh - The DMPlex
2012eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2013552f7358SJed Brown 
2014552f7358SJed Brown   Output Parameter:
2015552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2016552f7358SJed Brown 
2017552f7358SJed Brown   Level: beginner
2018552f7358SJed Brown 
20193813dfbdSMatthew G Knepley   Fortran Notes:
20203813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20213813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20223813dfbdSMatthew G Knepley 
20233813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
20243813dfbdSMatthew G Knepley 
2025552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2026552f7358SJed Brown @*/
2027552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2028552f7358SJed Brown {
2029552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2030552f7358SJed Brown   PetscInt       off;
2031552f7358SJed Brown   PetscErrorCode ierr;
2032552f7358SJed Brown 
2033552f7358SJed Brown   PetscFunctionBegin;
2034552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2035552f7358SJed Brown   PetscValidPointer(support, 3);
2036552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2037552f7358SJed Brown   *support = &mesh->supports[off];
2038552f7358SJed Brown   PetscFunctionReturn(0);
2039552f7358SJed Brown }
2040552f7358SJed Brown 
2041552f7358SJed Brown /*@
204292371b87SBarry 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
2043552f7358SJed Brown 
2044552f7358SJed Brown   Not collective
2045552f7358SJed Brown 
2046552f7358SJed Brown   Input Parameters:
2047552f7358SJed Brown + mesh - The DMPlex
2048eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
204992371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2050552f7358SJed Brown 
2051552f7358SJed Brown   Output Parameter:
2052552f7358SJed Brown 
2053552f7358SJed Brown   Note:
2054552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2055552f7358SJed Brown 
2056552f7358SJed Brown   Level: beginner
2057552f7358SJed Brown 
205892371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2059552f7358SJed Brown @*/
2060552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2061552f7358SJed Brown {
2062552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2063552f7358SJed Brown   PetscInt       pStart, pEnd;
2064552f7358SJed Brown   PetscInt       dof, off, c;
2065552f7358SJed Brown   PetscErrorCode ierr;
2066552f7358SJed Brown 
2067552f7358SJed Brown   PetscFunctionBegin;
2068552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2069552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2070552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2071552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2072552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
207382f516ccSBarry 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);
2074552f7358SJed Brown   for (c = 0; c < dof; ++c) {
207582f516ccSBarry 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);
2076552f7358SJed Brown     mesh->supports[off+c] = support[c];
2077552f7358SJed Brown   }
2078552f7358SJed Brown   PetscFunctionReturn(0);
2079552f7358SJed Brown }
2080552f7358SJed Brown 
20817cd05799SMatthew G. Knepley /*@
2082eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
20837cd05799SMatthew G. Knepley 
20847cd05799SMatthew G. Knepley   Not collective
20857cd05799SMatthew G. Knepley 
20867cd05799SMatthew G. Knepley   Input Parameters:
20877cd05799SMatthew G. Knepley + mesh - The DMPlex
2088eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20897cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
20907cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
20917cd05799SMatthew G. Knepley 
20927cd05799SMatthew G. Knepley   Level: beginner
20937cd05799SMatthew G. Knepley 
20947cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20957cd05799SMatthew G. Knepley @*/
2096552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2097552f7358SJed Brown {
2098552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2099552f7358SJed Brown   PetscInt       pStart, pEnd;
2100552f7358SJed Brown   PetscInt       dof, off;
2101552f7358SJed Brown   PetscErrorCode ierr;
2102552f7358SJed Brown 
2103552f7358SJed Brown   PetscFunctionBegin;
2104552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2105552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2106552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2107552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
210882f516ccSBarry 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);
210982f516ccSBarry 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);
211082f516ccSBarry 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);
2111552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2112552f7358SJed Brown   PetscFunctionReturn(0);
2113552f7358SJed Brown }
2114552f7358SJed Brown 
2115552f7358SJed Brown /*@C
2116eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2117552f7358SJed Brown 
2118552f7358SJed Brown   Not collective
2119552f7358SJed Brown 
2120552f7358SJed Brown   Input Parameters:
2121552f7358SJed Brown + mesh - The DMPlex
2122eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2123552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
21240298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2125552f7358SJed Brown 
2126552f7358SJed Brown   Output Parameters:
2127552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2128552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2129552f7358SJed Brown 
2130552f7358SJed Brown   Note:
21310298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2132552f7358SJed Brown 
21333813dfbdSMatthew G Knepley   Fortran Notes:
21343813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21353813dfbdSMatthew G Knepley   include petsc.h90 in your code.
21363813dfbdSMatthew G Knepley 
21373813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
21383813dfbdSMatthew G Knepley 
2139552f7358SJed Brown   Level: beginner
2140552f7358SJed Brown 
2141552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2142552f7358SJed Brown @*/
2143552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2144552f7358SJed Brown {
2145552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2146552f7358SJed Brown   PetscInt       *closure, *fifo;
21470298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2148552f7358SJed Brown   PetscInt        tmpSize, t;
2149552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2150552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2151552f7358SJed Brown   PetscErrorCode  ierr;
2152552f7358SJed Brown 
2153552f7358SJed Brown   PetscFunctionBegin;
2154552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2155552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2156552f7358SJed Brown   /* This is only 1-level */
2157552f7358SJed Brown   if (useCone) {
2158552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2159552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2160552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2161552f7358SJed Brown   } else {
2162552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2163552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2164552f7358SJed Brown   }
2165bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2166bfbcdd7aSMatthew G. Knepley     if (*points) {
2167bfbcdd7aSMatthew G. Knepley       closure = *points;
2168bfbcdd7aSMatthew G. Knepley     } else {
2169bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
217069291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2171bfbcdd7aSMatthew G. Knepley     }
2172bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2173bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2174bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2175bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2176bfbcdd7aSMatthew G. Knepley     }
2177bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2178bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2179bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2180bfbcdd7aSMatthew G. Knepley   }
218124c766afSToby Isaac   {
218224c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
218324c766afSToby Isaac 
218424c766afSToby Isaac     c = mesh->maxConeSize;
218524c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
218624c766afSToby Isaac     s = mesh->maxSupportSize;
218724c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
218824c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
218924c766afSToby Isaac   }
219069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2191bfbcdd7aSMatthew G. Knepley   if (*points) {
2192bfbcdd7aSMatthew G. Knepley     closure = *points;
2193bfbcdd7aSMatthew G. Knepley   } else {
219469291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2195bfbcdd7aSMatthew G. Knepley   }
2196bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2197552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2198552f7358SJed Brown     const PetscInt cp = tmp[t];
2199552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2200552f7358SJed Brown 
2201552f7358SJed Brown     closure[closureSize]   = cp;
2202552f7358SJed Brown     closure[closureSize+1] = co;
2203552f7358SJed Brown     fifo[fifoSize]         = cp;
2204552f7358SJed Brown     fifo[fifoSize+1]       = co;
2205552f7358SJed Brown   }
2206bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2207552f7358SJed Brown   while (fifoSize - fifoStart) {
2208552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2209552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2210552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2211552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2212552f7358SJed Brown 
2213552f7358SJed Brown     if (useCone) {
2214552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2215552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2216552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2217552f7358SJed Brown     } else {
2218552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2219552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
22200298fd71SBarry Smith       tmpO = NULL;
2221552f7358SJed Brown     }
2222552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2223552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2224552f7358SJed Brown       const PetscInt cp = tmp[i];
22252e1b13c2SMatthew 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. */
22262e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
22272e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
22282e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2229552f7358SJed Brown       PetscInt       c;
2230552f7358SJed Brown 
22312e1b13c2SMatthew G. Knepley       if (rev) {
22322e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
22332e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
22342e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
22352e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
22362e1b13c2SMatthew G. Knepley       }
2237552f7358SJed Brown       /* Check for duplicate */
2238552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2239552f7358SJed Brown         if (closure[c] == cp) break;
2240552f7358SJed Brown       }
2241552f7358SJed Brown       if (c == closureSize) {
2242552f7358SJed Brown         closure[closureSize]   = cp;
2243552f7358SJed Brown         closure[closureSize+1] = co;
2244552f7358SJed Brown         fifo[fifoSize]         = cp;
2245552f7358SJed Brown         fifo[fifoSize+1]       = co;
2246552f7358SJed Brown         closureSize           += 2;
2247552f7358SJed Brown         fifoSize              += 2;
2248552f7358SJed Brown       }
2249552f7358SJed Brown     }
2250552f7358SJed Brown     fifoStart += 2;
2251552f7358SJed Brown   }
2252552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2253552f7358SJed Brown   if (points)    *points    = closure;
225469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2255552f7358SJed Brown   PetscFunctionReturn(0);
2256552f7358SJed Brown }
2257552f7358SJed Brown 
22589bf0dad6SMatthew G. Knepley /*@C
2259eaf898f9SPatrick 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
22609bf0dad6SMatthew G. Knepley 
22619bf0dad6SMatthew G. Knepley   Not collective
22629bf0dad6SMatthew G. Knepley 
22639bf0dad6SMatthew G. Knepley   Input Parameters:
22649bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2265eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
22669bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
22679bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
22689bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
22699bf0dad6SMatthew G. Knepley 
22709bf0dad6SMatthew G. Knepley   Output Parameters:
22719bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
22729bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
22739bf0dad6SMatthew G. Knepley 
22749bf0dad6SMatthew G. Knepley   Note:
22759bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
22769bf0dad6SMatthew G. Knepley 
22779bf0dad6SMatthew G. Knepley   Fortran Notes:
22789bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22799bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
22809bf0dad6SMatthew G. Knepley 
22819bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22829bf0dad6SMatthew G. Knepley 
22839bf0dad6SMatthew G. Knepley   Level: beginner
22849bf0dad6SMatthew G. Knepley 
22859bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
22869bf0dad6SMatthew G. Knepley @*/
22879bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
22889bf0dad6SMatthew G. Knepley {
22899bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
22909bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
22919bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
22929bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
22939bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
22949bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
22959bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
22969bf0dad6SMatthew G. Knepley 
22979bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
22989bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22999bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
23009bf0dad6SMatthew G. Knepley   /* This is only 1-level */
23019bf0dad6SMatthew G. Knepley   if (useCone) {
23029bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
23039bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
23049bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
23059bf0dad6SMatthew G. Knepley   } else {
23069bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
23079bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
23089bf0dad6SMatthew G. Knepley   }
23099bf0dad6SMatthew G. Knepley   if (depth == 1) {
23109bf0dad6SMatthew G. Knepley     if (*points) {
23119bf0dad6SMatthew G. Knepley       closure = *points;
23129bf0dad6SMatthew G. Knepley     } else {
23139bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
231469291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
23159bf0dad6SMatthew G. Knepley     }
23169bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
23179bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
23189bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
23199bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
23209bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
23219bf0dad6SMatthew G. Knepley     }
23229bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
23239bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
23249bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
23259bf0dad6SMatthew G. Knepley   }
232624c766afSToby Isaac   {
232724c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
232824c766afSToby Isaac 
232924c766afSToby Isaac     c = mesh->maxConeSize;
233024c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
233124c766afSToby Isaac     s = mesh->maxSupportSize;
233224c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
233324c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
233424c766afSToby Isaac   }
233569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
23369bf0dad6SMatthew G. Knepley   if (*points) {
23379bf0dad6SMatthew G. Knepley     closure = *points;
23389bf0dad6SMatthew G. Knepley   } else {
233969291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
23409bf0dad6SMatthew G. Knepley   }
23419bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
23429bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
23439bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
23449bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
23459bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
23469bf0dad6SMatthew G. Knepley 
23479bf0dad6SMatthew G. Knepley     if (ornt < 0) {
23489bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
23499bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
235086b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
23519bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
23529bf0dad6SMatthew G. Knepley     }
23539bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
23549bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
23559bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
23569bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
23579bf0dad6SMatthew G. Knepley   }
23589bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
23599bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
23609bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
23619bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
23629bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
23639bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
23649bf0dad6SMatthew G. Knepley 
23659bf0dad6SMatthew G. Knepley     if (useCone) {
23669bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
23679bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
23689bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
23699bf0dad6SMatthew G. Knepley     } else {
23709bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
23719bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
23729bf0dad6SMatthew G. Knepley       tmpO = NULL;
23739bf0dad6SMatthew G. Knepley     }
23749bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
23759bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
23769bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
23779bf0dad6SMatthew 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. */
23789bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
23799bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
23809bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
23819bf0dad6SMatthew G. Knepley       PetscInt       c;
23829bf0dad6SMatthew G. Knepley 
23839bf0dad6SMatthew G. Knepley       if (rev) {
23849bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
23859bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
23869bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
23879bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
23889bf0dad6SMatthew G. Knepley       }
23899bf0dad6SMatthew G. Knepley       /* Check for duplicate */
23909bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
23919bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
23929bf0dad6SMatthew G. Knepley       }
23939bf0dad6SMatthew G. Knepley       if (c == closureSize) {
23949bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
23959bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
23969bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
23979bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
23989bf0dad6SMatthew G. Knepley         closureSize           += 2;
23999bf0dad6SMatthew G. Knepley         fifoSize              += 2;
24009bf0dad6SMatthew G. Knepley       }
24019bf0dad6SMatthew G. Knepley     }
24029bf0dad6SMatthew G. Knepley     fifoStart += 2;
24039bf0dad6SMatthew G. Knepley   }
24049bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
24059bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
240669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
24079bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
24089bf0dad6SMatthew G. Knepley }
24099bf0dad6SMatthew G. Knepley 
2410552f7358SJed Brown /*@C
2411eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2412552f7358SJed Brown 
2413552f7358SJed Brown   Not collective
2414552f7358SJed Brown 
2415552f7358SJed Brown   Input Parameters:
2416552f7358SJed Brown + mesh - The DMPlex
2417eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2418552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2419e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2420e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2421552f7358SJed Brown 
2422552f7358SJed Brown   Note:
24230298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2424552f7358SJed Brown 
24253813dfbdSMatthew G Knepley   Fortran Notes:
24263813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24273813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24283813dfbdSMatthew G Knepley 
24293813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24303813dfbdSMatthew G Knepley 
2431552f7358SJed Brown   Level: beginner
2432552f7358SJed Brown 
2433552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2434552f7358SJed Brown @*/
2435552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2436552f7358SJed Brown {
2437552f7358SJed Brown   PetscErrorCode ierr;
2438552f7358SJed Brown 
2439552f7358SJed Brown   PetscFunctionBegin;
2440552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2441e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2442e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
244369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
24444ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2445552f7358SJed Brown   PetscFunctionReturn(0);
2446552f7358SJed Brown }
2447552f7358SJed Brown 
2448552f7358SJed Brown /*@
2449eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2450552f7358SJed Brown 
2451552f7358SJed Brown   Not collective
2452552f7358SJed Brown 
2453552f7358SJed Brown   Input Parameter:
2454552f7358SJed Brown . mesh - The DMPlex
2455552f7358SJed Brown 
2456552f7358SJed Brown   Output Parameters:
2457552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2458552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2459552f7358SJed Brown 
2460552f7358SJed Brown   Level: beginner
2461552f7358SJed Brown 
2462552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2463552f7358SJed Brown @*/
2464552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2465552f7358SJed Brown {
2466552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2467552f7358SJed Brown 
2468552f7358SJed Brown   PetscFunctionBegin;
2469552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2470552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2471552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2472552f7358SJed Brown   PetscFunctionReturn(0);
2473552f7358SJed Brown }
2474552f7358SJed Brown 
2475552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2476552f7358SJed Brown {
2477552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2478552f7358SJed Brown   PetscInt       size;
2479552f7358SJed Brown   PetscErrorCode ierr;
2480552f7358SJed Brown 
2481552f7358SJed Brown   PetscFunctionBegin;
2482552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2483552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2484552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
24851795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
24861795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2487552f7358SJed Brown   if (mesh->maxSupportSize) {
2488552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2489552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
24901795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2491552f7358SJed Brown   }
2492552f7358SJed Brown   PetscFunctionReturn(0);
2493552f7358SJed Brown }
2494552f7358SJed Brown 
2495276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2496552f7358SJed Brown {
2497552f7358SJed Brown   PetscErrorCode ierr;
2498552f7358SJed Brown 
2499552f7358SJed Brown   PetscFunctionBegin;
25004d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2501792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2502c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2503736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2504f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2505f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2506f94b4a02SBlaise Bourdin 
25073dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
25083dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2509e87a4003SBarry Smith     ierr = DMGetSection((*subdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
2510f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2511f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2512f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2513f94b4a02SBlaise Bourdin 
2514f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2515c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2516f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2517f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2518f94b4a02SBlaise Bourdin   }
2519552f7358SJed Brown   PetscFunctionReturn(0);
2520552f7358SJed Brown }
2521552f7358SJed Brown 
25222adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
25232adcc780SMatthew G. Knepley {
25242adcc780SMatthew G. Knepley   PetscErrorCode ierr;
25253dcd263cSBlaise Bourdin   PetscInt       i = 0;
25262adcc780SMatthew G. Knepley 
25272adcc780SMatthew G. Knepley   PetscFunctionBegin;
2528435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2529792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
2530c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
25313dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
25323dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
25333dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
25343dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
25353dcd263cSBlaise Bourdin 
25363dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
25373dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2538c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
2539cf0b7c11SKarl Rupp       ierr = DMGetSection((*superdm), &section);CHKERRQ(ierr);
25403dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
25413dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
25423dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
25433dcd263cSBlaise Bourdin 
25443dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2545c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
25463dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
25473dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
25483dcd263cSBlaise Bourdin       break;
25493dcd263cSBlaise Bourdin     }
25503dcd263cSBlaise Bourdin   }
25512adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
25522adcc780SMatthew G. Knepley }
25532adcc780SMatthew G. Knepley 
2554552f7358SJed Brown /*@
2555eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2556552f7358SJed Brown 
2557552f7358SJed Brown   Not collective
2558552f7358SJed Brown 
2559552f7358SJed Brown   Input Parameter:
2560552f7358SJed Brown . mesh - The DMPlex
2561552f7358SJed Brown 
2562552f7358SJed Brown   Output Parameter:
2563552f7358SJed Brown 
2564552f7358SJed Brown   Note:
2565552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2566552f7358SJed Brown 
2567552f7358SJed Brown   Level: beginner
2568552f7358SJed Brown 
2569552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2570552f7358SJed Brown @*/
2571552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2572552f7358SJed Brown {
2573552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2574552f7358SJed Brown   PetscInt      *offsets;
2575552f7358SJed Brown   PetscInt       supportSize;
2576552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2577552f7358SJed Brown   PetscErrorCode ierr;
2578552f7358SJed Brown 
2579552f7358SJed Brown   PetscFunctionBegin;
2580552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
258182f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2582552f7358SJed Brown   /* Calculate support sizes */
2583552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2584552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2585552f7358SJed Brown     PetscInt dof, off, c;
2586552f7358SJed Brown 
2587552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2588552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2589552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2590552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2591552f7358SJed Brown     }
2592552f7358SJed Brown   }
2593552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2594552f7358SJed Brown     PetscInt dof;
2595552f7358SJed Brown 
2596552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
25970d644c17SKarl Rupp 
2598552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2599552f7358SJed Brown   }
2600552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2601552f7358SJed Brown   /* Calculate supports */
2602552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
26031795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
26041795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2605552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2606552f7358SJed Brown     PetscInt dof, off, c;
2607552f7358SJed Brown 
2608552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2609552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2610552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2611552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2612552f7358SJed Brown       PetscInt       offS;
2613552f7358SJed Brown 
2614552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
26150d644c17SKarl Rupp 
2616552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2617552f7358SJed Brown       ++offsets[q];
2618552f7358SJed Brown     }
2619552f7358SJed Brown   }
2620552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2621552f7358SJed Brown   PetscFunctionReturn(0);
2622552f7358SJed Brown }
2623552f7358SJed Brown 
2624277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2625277ea44aSLisandro Dalcin {
2626277ea44aSLisandro Dalcin   IS             stratumIS;
2627277ea44aSLisandro Dalcin   PetscErrorCode ierr;
2628277ea44aSLisandro Dalcin 
2629277ea44aSLisandro Dalcin   PetscFunctionBegin;
2630277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
2631277ea44aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
2632277ea44aSLisandro Dalcin   {
2633277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
2634277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
2635277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
2636277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
2637277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2638277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2639277ea44aSLisandro Dalcin     }
2640277ea44aSLisandro Dalcin     if (overlap) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_PLIB, "New depth %D range [%D,%D) overlaps with depth %D range [%D,%D)", depth, pStart, pEnd, level, qStart, qEnd);
2641277ea44aSLisandro Dalcin   }
2642277ea44aSLisandro Dalcin #endif
2643277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
2644277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
2645277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
2646277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
2647277ea44aSLisandro Dalcin }
2648277ea44aSLisandro Dalcin 
26497d5acc75SStefano Zampini static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);
26507d5acc75SStefano Zampini 
2651552f7358SJed Brown /*@
2652eaf898f9SPatrick Sanan   DMPlexStratify - The DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2653eaf898f9SPatrick Sanan   can be illustrated by a Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2654552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2655552f7358SJed Brown   the DAG.
2656552f7358SJed Brown 
2657bf4602e4SToby Isaac   Collective on dm
2658552f7358SJed Brown 
2659552f7358SJed Brown   Input Parameter:
2660552f7358SJed Brown . mesh - The DMPlex
2661552f7358SJed Brown 
2662552f7358SJed Brown   Output Parameter:
2663552f7358SJed Brown 
2664552f7358SJed Brown   Notes:
2665150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2666150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2667c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2668150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2669552f7358SJed Brown 
2670150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2671552f7358SJed Brown 
2672552f7358SJed Brown   Level: beginner
2673552f7358SJed Brown 
2674552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2675552f7358SJed Brown @*/
2676552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2677552f7358SJed Brown {
2678df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2679aa50250dSMatthew G. Knepley   DMLabel        label;
2680552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2681552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
26827d5acc75SStefano Zampini   PetscInt       cMax, fMax, eMax, vMax;
2683552f7358SJed Brown   PetscErrorCode ierr;
2684552f7358SJed Brown 
2685552f7358SJed Brown   PetscFunctionBegin;
2686552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2687552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2688277ea44aSLisandro Dalcin 
2689277ea44aSLisandro Dalcin   /* Create depth label */
2690aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2691c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2692aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2693277ea44aSLisandro Dalcin 
2694277ea44aSLisandro Dalcin   {
2695552f7358SJed Brown     /* Initialize roots and count leaves */
2696277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2697277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2698552f7358SJed Brown     PetscInt coneSize, supportSize;
2699552f7358SJed Brown 
2700277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2701552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2702552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2703552f7358SJed Brown       if (!coneSize && supportSize) {
2704277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2705277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2706552f7358SJed Brown         ++numRoots;
2707552f7358SJed Brown       } else if (!supportSize && coneSize) {
2708552f7358SJed Brown         ++numLeaves;
2709552f7358SJed Brown       } else if (!supportSize && !coneSize) {
2710552f7358SJed Brown         /* Isolated points */
2711277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2712277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2713552f7358SJed Brown       }
2714552f7358SJed Brown     }
2715277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
2716277ea44aSLisandro Dalcin   }
2717277ea44aSLisandro Dalcin 
2718552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2719277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2720277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2721552f7358SJed Brown     PetscInt coneSize, supportSize;
2722552f7358SJed Brown 
2723277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2724552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2725552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2726552f7358SJed Brown       if (!supportSize && coneSize) {
2727277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2728277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2729552f7358SJed Brown       }
2730552f7358SJed Brown     }
2731277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
2732552f7358SJed Brown   } else {
2733277ea44aSLisandro Dalcin     PetscInt level = 0;
2734277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
2735552f7358SJed Brown 
2736277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2737277ea44aSLisandro Dalcin     while (qEnd > qStart) {
2738277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
2739277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
274074ef644bSMatthew G. Knepley 
2741277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
274274ef644bSMatthew G. Knepley         const PetscInt *support;
274374ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
274474ef644bSMatthew G. Knepley 
2745277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
2746277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
274774ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
2748277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
2749277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
2750552f7358SJed Brown         }
2751552f7358SJed Brown       }
2752277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
2753277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
2754277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
275574ef644bSMatthew G. Knepley     }
275674ef644bSMatthew G. Knepley   }
2757bf4602e4SToby Isaac   { /* just in case there is an empty process */
2758bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2759bf4602e4SToby Isaac 
2760bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
27616d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2762bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2763367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
2764bf4602e4SToby Isaac     }
2765bf4602e4SToby Isaac   }
2766d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
27677d5acc75SStefano Zampini 
27687d5acc75SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
27697d5acc75SStefano Zampini   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
27707d5acc75SStefano Zampini     PetscInt dim;
2771277ea44aSLisandro Dalcin     DMLabel  dimLabel;
27727d5acc75SStefano Zampini 
27737d5acc75SStefano Zampini     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
27747d5acc75SStefano Zampini     ierr = DMCreateLabel(dm, "dim");CHKERRQ(ierr);
27757d5acc75SStefano Zampini     ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
27767d5acc75SStefano Zampini     if (cMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);CHKERRQ(ierr);}
27777d5acc75SStefano Zampini     if (fMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);CHKERRQ(ierr);}
27787d5acc75SStefano Zampini     if (eMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);CHKERRQ(ierr);}
27797d5acc75SStefano Zampini     if (vMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);CHKERRQ(ierr);}
27807d5acc75SStefano Zampini   }
2781552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2782552f7358SJed Brown   PetscFunctionReturn(0);
2783552f7358SJed Brown }
2784552f7358SJed Brown 
2785552f7358SJed Brown /*@C
2786552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2787552f7358SJed Brown 
2788552f7358SJed Brown   Not Collective
2789552f7358SJed Brown 
2790552f7358SJed Brown   Input Parameters:
2791552f7358SJed Brown + dm - The DMPlex object
2792552f7358SJed Brown . numPoints - The number of input points for the join
2793552f7358SJed Brown - points - The input points
2794552f7358SJed Brown 
2795552f7358SJed Brown   Output Parameters:
2796552f7358SJed Brown + numCoveredPoints - The number of points in the join
2797552f7358SJed Brown - coveredPoints - The points in the join
2798552f7358SJed Brown 
2799552f7358SJed Brown   Level: intermediate
2800552f7358SJed Brown 
2801552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2802552f7358SJed Brown 
28033813dfbdSMatthew G Knepley   Fortran Notes:
28043813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
28053813dfbdSMatthew G Knepley   include petsc.h90 in your code.
28063813dfbdSMatthew G Knepley 
28073813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
28083813dfbdSMatthew G Knepley 
2809552f7358SJed Brown .keywords: mesh
2810552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2811552f7358SJed Brown @*/
2812552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2813552f7358SJed Brown {
2814552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2815552f7358SJed Brown   PetscInt      *join[2];
2816552f7358SJed Brown   PetscInt       joinSize, i = 0;
2817552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2818552f7358SJed Brown   PetscErrorCode ierr;
2819552f7358SJed Brown 
2820552f7358SJed Brown   PetscFunctionBegin;
2821552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
282248bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
282348bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
282448bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
282569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
282669291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2827552f7358SJed Brown   /* Copy in support of first point */
2828552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2829552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2830552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2831552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2832552f7358SJed Brown   }
2833552f7358SJed Brown   /* Check each successive support */
2834552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2835552f7358SJed Brown     PetscInt newJoinSize = 0;
2836552f7358SJed Brown 
2837552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2838552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2839552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2840552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2841552f7358SJed Brown 
2842552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2843552f7358SJed Brown         if (point == join[i][m]) {
2844552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2845552f7358SJed Brown           break;
2846552f7358SJed Brown         }
2847552f7358SJed Brown       }
2848552f7358SJed Brown     }
2849552f7358SJed Brown     joinSize = newJoinSize;
2850552f7358SJed Brown     i        = 1-i;
2851552f7358SJed Brown   }
2852552f7358SJed Brown   *numCoveredPoints = joinSize;
2853552f7358SJed Brown   *coveredPoints    = join[i];
285469291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2855552f7358SJed Brown   PetscFunctionReturn(0);
2856552f7358SJed Brown }
2857552f7358SJed Brown 
2858552f7358SJed Brown /*@C
2859552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2860552f7358SJed Brown 
2861552f7358SJed Brown   Not Collective
2862552f7358SJed Brown 
2863552f7358SJed Brown   Input Parameters:
2864552f7358SJed Brown + dm - The DMPlex object
2865552f7358SJed Brown . numPoints - The number of input points for the join
2866552f7358SJed Brown - points - The input points
2867552f7358SJed Brown 
2868552f7358SJed Brown   Output Parameters:
2869552f7358SJed Brown + numCoveredPoints - The number of points in the join
2870552f7358SJed Brown - coveredPoints - The points in the join
2871552f7358SJed Brown 
28723813dfbdSMatthew G Knepley   Fortran Notes:
28733813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
28743813dfbdSMatthew G Knepley   include petsc.h90 in your code.
28753813dfbdSMatthew G Knepley 
28763813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
28773813dfbdSMatthew G Knepley 
2878552f7358SJed Brown   Level: intermediate
2879552f7358SJed Brown 
2880552f7358SJed Brown .keywords: mesh
2881552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2882552f7358SJed Brown @*/
2883552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2884552f7358SJed Brown {
2885552f7358SJed Brown   PetscErrorCode ierr;
2886552f7358SJed Brown 
2887552f7358SJed Brown   PetscFunctionBegin;
2888552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2889d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2890d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2891d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
289269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2893d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2894552f7358SJed Brown   PetscFunctionReturn(0);
2895552f7358SJed Brown }
2896552f7358SJed Brown 
2897552f7358SJed Brown /*@C
2898552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2899552f7358SJed Brown 
2900552f7358SJed Brown   Not Collective
2901552f7358SJed Brown 
2902552f7358SJed Brown   Input Parameters:
2903552f7358SJed Brown + dm - The DMPlex object
2904552f7358SJed Brown . numPoints - The number of input points for the join
2905552f7358SJed Brown - points - The input points
2906552f7358SJed Brown 
2907552f7358SJed Brown   Output Parameters:
2908552f7358SJed Brown + numCoveredPoints - The number of points in the join
2909552f7358SJed Brown - coveredPoints - The points in the join
2910552f7358SJed Brown 
29113813dfbdSMatthew G Knepley   Fortran Notes:
29123813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29133813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29143813dfbdSMatthew G Knepley 
29153813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29163813dfbdSMatthew G Knepley 
2917552f7358SJed Brown   Level: intermediate
2918552f7358SJed Brown 
2919552f7358SJed Brown .keywords: mesh
2920552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2921552f7358SJed Brown @*/
2922552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2923552f7358SJed Brown {
2924552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2925552f7358SJed Brown   PetscInt      *offsets, **closures;
2926552f7358SJed Brown   PetscInt      *join[2];
2927552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
292824c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2929552f7358SJed Brown   PetscErrorCode ierr;
2930552f7358SJed Brown 
2931552f7358SJed Brown   PetscFunctionBegin;
2932552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
293348bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
293448bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
293548bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
2936552f7358SJed Brown 
2937552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
29381795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
293969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
294024c766afSToby Isaac   ms      = mesh->maxSupportSize;
294124c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
294269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
294369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2944552f7358SJed Brown 
2945552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2946552f7358SJed Brown     PetscInt closureSize;
2947552f7358SJed Brown 
2948552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
29490d644c17SKarl Rupp 
2950552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2951552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2952552f7358SJed Brown       PetscInt pStart, pEnd, i;
2953552f7358SJed Brown 
2954552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2955552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2956552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2957552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2958552f7358SJed Brown           break;
2959552f7358SJed Brown         }
2960552f7358SJed Brown       }
2961552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2962552f7358SJed Brown     }
296382f516ccSBarry 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);
2964552f7358SJed Brown   }
2965552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2966552f7358SJed Brown     PetscInt dof;
2967552f7358SJed Brown 
2968552f7358SJed Brown     /* Copy in support of first point */
2969552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2970552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2971552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2972552f7358SJed Brown     }
2973552f7358SJed Brown     /* Check each successive cone */
2974552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2975552f7358SJed Brown       PetscInt newJoinSize = 0;
2976552f7358SJed Brown 
2977552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2978552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2979552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2980552f7358SJed Brown 
2981552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2982552f7358SJed Brown           if (point == join[i][m]) {
2983552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2984552f7358SJed Brown             break;
2985552f7358SJed Brown           }
2986552f7358SJed Brown         }
2987552f7358SJed Brown       }
2988552f7358SJed Brown       joinSize = newJoinSize;
2989552f7358SJed Brown       i        = 1-i;
2990552f7358SJed Brown     }
2991552f7358SJed Brown     if (joinSize) break;
2992552f7358SJed Brown   }
2993552f7358SJed Brown   *numCoveredPoints = joinSize;
2994552f7358SJed Brown   *coveredPoints    = join[i];
2995552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
29960298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2997552f7358SJed Brown   }
2998552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
299969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
300069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3001552f7358SJed Brown   PetscFunctionReturn(0);
3002552f7358SJed Brown }
3003552f7358SJed Brown 
3004552f7358SJed Brown /*@C
3005552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3006552f7358SJed Brown 
3007552f7358SJed Brown   Not Collective
3008552f7358SJed Brown 
3009552f7358SJed Brown   Input Parameters:
3010552f7358SJed Brown + dm - The DMPlex object
3011552f7358SJed Brown . numPoints - The number of input points for the meet
3012552f7358SJed Brown - points - The input points
3013552f7358SJed Brown 
3014552f7358SJed Brown   Output Parameters:
3015552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3016552f7358SJed Brown - coveredPoints - The points in the meet
3017552f7358SJed Brown 
3018552f7358SJed Brown   Level: intermediate
3019552f7358SJed Brown 
3020552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3021552f7358SJed Brown 
30223813dfbdSMatthew G Knepley   Fortran Notes:
30233813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
30243813dfbdSMatthew G Knepley   include petsc.h90 in your code.
30253813dfbdSMatthew G Knepley 
30263813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
30273813dfbdSMatthew G Knepley 
3028552f7358SJed Brown .keywords: mesh
3029552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3030552f7358SJed Brown @*/
3031552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3032552f7358SJed Brown {
3033552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3034552f7358SJed Brown   PetscInt      *meet[2];
3035552f7358SJed Brown   PetscInt       meetSize, i = 0;
3036552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3037552f7358SJed Brown   PetscErrorCode ierr;
3038552f7358SJed Brown 
3039552f7358SJed Brown   PetscFunctionBegin;
3040552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3041552f7358SJed Brown   PetscValidPointer(points, 2);
3042552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
3043552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
304469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
304569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3046552f7358SJed Brown   /* Copy in cone of first point */
3047552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3048552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3049552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3050552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3051552f7358SJed Brown   }
3052552f7358SJed Brown   /* Check each successive cone */
3053552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3054552f7358SJed Brown     PetscInt newMeetSize = 0;
3055552f7358SJed Brown 
3056552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3057552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3058552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3059552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3060552f7358SJed Brown 
3061552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3062552f7358SJed Brown         if (point == meet[i][m]) {
3063552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3064552f7358SJed Brown           break;
3065552f7358SJed Brown         }
3066552f7358SJed Brown       }
3067552f7358SJed Brown     }
3068552f7358SJed Brown     meetSize = newMeetSize;
3069552f7358SJed Brown     i        = 1-i;
3070552f7358SJed Brown   }
3071552f7358SJed Brown   *numCoveringPoints = meetSize;
3072552f7358SJed Brown   *coveringPoints    = meet[i];
307369291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3074552f7358SJed Brown   PetscFunctionReturn(0);
3075552f7358SJed Brown }
3076552f7358SJed Brown 
3077552f7358SJed Brown /*@C
3078552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3079552f7358SJed Brown 
3080552f7358SJed Brown   Not Collective
3081552f7358SJed Brown 
3082552f7358SJed Brown   Input Parameters:
3083552f7358SJed Brown + dm - The DMPlex object
3084552f7358SJed Brown . numPoints - The number of input points for the meet
3085552f7358SJed Brown - points - The input points
3086552f7358SJed Brown 
3087552f7358SJed Brown   Output Parameters:
3088552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3089552f7358SJed Brown - coveredPoints - The points in the meet
3090552f7358SJed Brown 
3091552f7358SJed Brown   Level: intermediate
3092552f7358SJed Brown 
30933813dfbdSMatthew G Knepley   Fortran Notes:
30943813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
30953813dfbdSMatthew G Knepley   include petsc.h90 in your code.
30963813dfbdSMatthew G Knepley 
30973813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
30983813dfbdSMatthew G Knepley 
3099552f7358SJed Brown .keywords: mesh
3100552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3101552f7358SJed Brown @*/
3102552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3103552f7358SJed Brown {
3104552f7358SJed Brown   PetscErrorCode ierr;
3105552f7358SJed Brown 
3106552f7358SJed Brown   PetscFunctionBegin;
3107552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3108d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3109d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3110d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
311169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3112d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3113552f7358SJed Brown   PetscFunctionReturn(0);
3114552f7358SJed Brown }
3115552f7358SJed Brown 
3116552f7358SJed Brown /*@C
3117552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3118552f7358SJed Brown 
3119552f7358SJed Brown   Not Collective
3120552f7358SJed Brown 
3121552f7358SJed Brown   Input Parameters:
3122552f7358SJed Brown + dm - The DMPlex object
3123552f7358SJed Brown . numPoints - The number of input points for the meet
3124552f7358SJed Brown - points - The input points
3125552f7358SJed Brown 
3126552f7358SJed Brown   Output Parameters:
3127552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3128552f7358SJed Brown - coveredPoints - The points in the meet
3129552f7358SJed Brown 
3130552f7358SJed Brown   Level: intermediate
3131552f7358SJed Brown 
31323813dfbdSMatthew G Knepley   Fortran Notes:
31333813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31343813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31353813dfbdSMatthew G Knepley 
31363813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31373813dfbdSMatthew G Knepley 
3138552f7358SJed Brown .keywords: mesh
3139552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3140552f7358SJed Brown @*/
3141552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3142552f7358SJed Brown {
3143552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3144552f7358SJed Brown   PetscInt      *offsets, **closures;
3145552f7358SJed Brown   PetscInt      *meet[2];
3146552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
314724c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3148552f7358SJed Brown   PetscErrorCode ierr;
3149552f7358SJed Brown 
3150552f7358SJed Brown   PetscFunctionBegin;
3151552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3152552f7358SJed Brown   PetscValidPointer(points, 2);
3153552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3154552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3155552f7358SJed Brown 
3156552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3157785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
315869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
315924c766afSToby Isaac   mc      = mesh->maxConeSize;
316024c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
316169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
316269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3163552f7358SJed Brown 
3164552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3165552f7358SJed Brown     PetscInt closureSize;
3166552f7358SJed Brown 
3167552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
31680d644c17SKarl Rupp 
3169552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3170552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3171552f7358SJed Brown       PetscInt pStart, pEnd, i;
3172552f7358SJed Brown 
3173552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3174552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3175552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3176552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3177552f7358SJed Brown           break;
3178552f7358SJed Brown         }
3179552f7358SJed Brown       }
3180552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3181552f7358SJed Brown     }
318282f516ccSBarry 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);
3183552f7358SJed Brown   }
3184552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3185552f7358SJed Brown     PetscInt dof;
3186552f7358SJed Brown 
3187552f7358SJed Brown     /* Copy in cone of first point */
3188552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3189552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3190552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3191552f7358SJed Brown     }
3192552f7358SJed Brown     /* Check each successive cone */
3193552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3194552f7358SJed Brown       PetscInt newMeetSize = 0;
3195552f7358SJed Brown 
3196552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3197552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3198552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3199552f7358SJed Brown 
3200552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3201552f7358SJed Brown           if (point == meet[i][m]) {
3202552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3203552f7358SJed Brown             break;
3204552f7358SJed Brown           }
3205552f7358SJed Brown         }
3206552f7358SJed Brown       }
3207552f7358SJed Brown       meetSize = newMeetSize;
3208552f7358SJed Brown       i        = 1-i;
3209552f7358SJed Brown     }
3210552f7358SJed Brown     if (meetSize) break;
3211552f7358SJed Brown   }
3212552f7358SJed Brown   *numCoveredPoints = meetSize;
3213552f7358SJed Brown   *coveredPoints    = meet[i];
3214552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
32150298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3216552f7358SJed Brown   }
3217552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
321869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
321969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3220552f7358SJed Brown   PetscFunctionReturn(0);
3221552f7358SJed Brown }
3222552f7358SJed Brown 
32234e3744c5SMatthew G. Knepley /*@C
32244e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
32254e3744c5SMatthew G. Knepley 
32264e3744c5SMatthew G. Knepley   Not Collective
32274e3744c5SMatthew G. Knepley 
32284e3744c5SMatthew G. Knepley   Input Parameters:
32294e3744c5SMatthew G. Knepley + dmA - A DMPlex object
32304e3744c5SMatthew G. Knepley - dmB - A DMPlex object
32314e3744c5SMatthew G. Knepley 
32324e3744c5SMatthew G. Knepley   Output Parameters:
32334e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
32344e3744c5SMatthew G. Knepley 
32354e3744c5SMatthew G. Knepley   Level: intermediate
32364e3744c5SMatthew G. Knepley 
32374e3744c5SMatthew G. Knepley   Notes:
32384e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
32394e3744c5SMatthew G. Knepley 
32404e3744c5SMatthew G. Knepley .keywords: mesh
32414e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
32424e3744c5SMatthew G. Knepley @*/
32434e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
32444e3744c5SMatthew G. Knepley {
32454e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
32464e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
32474e3744c5SMatthew G. Knepley 
32484e3744c5SMatthew G. Knepley   PetscFunctionBegin;
32494e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
32504e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
32514e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
32524e3744c5SMatthew G. Knepley 
32534e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
32544e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
32554e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
32564e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
32574e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
32584e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
32594e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
32604e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
32614e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
32624e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
32634e3744c5SMatthew G. Knepley 
32644e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
32654e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
32664e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
32674e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
32684e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
32694e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
32704e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
32714e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
32724e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
32734e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
32744e3744c5SMatthew G. Knepley     }
32754e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
32764e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
32774e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
32784e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
32794e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
32804e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
32814e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
32824e3744c5SMatthew G. Knepley     }
32834e3744c5SMatthew G. Knepley   }
32844e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
32854e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
32864e3744c5SMatthew G. Knepley }
32874e3744c5SMatthew G. Knepley 
32887cd05799SMatthew G. Knepley /*@C
32897cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
32907cd05799SMatthew G. Knepley 
32917cd05799SMatthew G. Knepley   Not Collective
32927cd05799SMatthew G. Knepley 
32937cd05799SMatthew G. Knepley   Input Parameters:
32947cd05799SMatthew G. Knepley + dm         - The DMPlex
32957cd05799SMatthew G. Knepley . cellDim    - The cell dimension
32967cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
32977cd05799SMatthew G. Knepley 
32987cd05799SMatthew G. Knepley   Output Parameters:
32997cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
33007cd05799SMatthew G. Knepley 
33017cd05799SMatthew G. Knepley   Level: developer
33027cd05799SMatthew G. Knepley 
33037cd05799SMatthew G. Knepley   Notes:
33047cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
33057cd05799SMatthew G. Knepley 
33067cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
33077cd05799SMatthew G. Knepley @*/
330818ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3309a6dfd86eSKarl Rupp {
331082f516ccSBarry Smith   MPI_Comm       comm;
3311552f7358SJed Brown   PetscErrorCode ierr;
3312552f7358SJed Brown 
3313552f7358SJed Brown   PetscFunctionBegin;
331482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3315552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3316552f7358SJed Brown   switch (cellDim) {
3317552f7358SJed Brown   case 0:
3318552f7358SJed Brown     *numFaceVertices = 0;
3319552f7358SJed Brown     break;
3320552f7358SJed Brown   case 1:
3321552f7358SJed Brown     *numFaceVertices = 1;
3322552f7358SJed Brown     break;
3323552f7358SJed Brown   case 2:
3324552f7358SJed Brown     switch (numCorners) {
332519436ca2SJed Brown     case 3: /* triangle */
332619436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3327552f7358SJed Brown       break;
332819436ca2SJed Brown     case 4: /* quadrilateral */
332919436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3330552f7358SJed Brown       break;
333119436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
333219436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3333552f7358SJed Brown       break;
333419436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
333519436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3336552f7358SJed Brown       break;
3337552f7358SJed Brown     default:
3338f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3339552f7358SJed Brown     }
3340552f7358SJed Brown     break;
3341552f7358SJed Brown   case 3:
3342552f7358SJed Brown     switch (numCorners) {
334319436ca2SJed Brown     case 4: /* tetradehdron */
334419436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3345552f7358SJed Brown       break;
334619436ca2SJed Brown     case 6: /* tet cohesive cells */
334719436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3348552f7358SJed Brown       break;
334919436ca2SJed Brown     case 8: /* hexahedron */
335019436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3351552f7358SJed Brown       break;
335219436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
335319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3354552f7358SJed Brown       break;
335519436ca2SJed Brown     case 10: /* quadratic tetrahedron */
335619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3357552f7358SJed Brown       break;
335819436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
335919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3360552f7358SJed Brown       break;
336119436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
336219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3363552f7358SJed Brown       break;
336419436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
336519436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3366552f7358SJed Brown       break;
3367552f7358SJed Brown     default:
3368f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3369552f7358SJed Brown     }
3370552f7358SJed Brown     break;
3371552f7358SJed Brown   default:
3372f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3373552f7358SJed Brown   }
3374552f7358SJed Brown   PetscFunctionReturn(0);
3375552f7358SJed Brown }
3376552f7358SJed Brown 
3377552f7358SJed Brown /*@
3378aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3379552f7358SJed Brown 
3380552f7358SJed Brown   Not Collective
3381552f7358SJed Brown 
3382aa50250dSMatthew G. Knepley   Input Parameter:
3383552f7358SJed Brown . dm    - The DMPlex object
3384552f7358SJed Brown 
3385aa50250dSMatthew G. Knepley   Output Parameter:
3386aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3387552f7358SJed Brown 
3388552f7358SJed Brown   Level: developer
3389552f7358SJed Brown 
3390aa50250dSMatthew G. Knepley .keywords: mesh, points
3391aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3392aa50250dSMatthew G. Knepley @*/
3393aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3394aa50250dSMatthew G. Knepley {
3395aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
3396aa50250dSMatthew G. Knepley 
3397aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3398aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3399aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3400c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
3401c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3402aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3403aa50250dSMatthew G. Knepley }
3404aa50250dSMatthew G. Knepley 
3405aa50250dSMatthew G. Knepley /*@
3406aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3407aa50250dSMatthew G. Knepley 
3408aa50250dSMatthew G. Knepley   Not Collective
3409aa50250dSMatthew G. Knepley 
3410aa50250dSMatthew G. Knepley   Input Parameter:
3411aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3412aa50250dSMatthew G. Knepley 
3413aa50250dSMatthew G. Knepley   Output Parameter:
3414aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3415aa50250dSMatthew G. Knepley 
3416aa50250dSMatthew G. Knepley   Level: developer
3417552f7358SJed Brown 
3418552f7358SJed Brown .keywords: mesh, points
3419aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3420552f7358SJed Brown @*/
3421552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3422552f7358SJed Brown {
3423aa50250dSMatthew G. Knepley   DMLabel        label;
3424aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3425552f7358SJed Brown   PetscErrorCode ierr;
3426552f7358SJed Brown 
3427552f7358SJed Brown   PetscFunctionBegin;
3428552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3429552f7358SJed Brown   PetscValidPointer(depth, 2);
3430aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3431aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3432552f7358SJed Brown   *depth = d-1;
3433552f7358SJed Brown   PetscFunctionReturn(0);
3434552f7358SJed Brown }
3435552f7358SJed Brown 
3436552f7358SJed Brown /*@
3437552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3438552f7358SJed Brown 
3439552f7358SJed Brown   Not Collective
3440552f7358SJed Brown 
3441552f7358SJed Brown   Input Parameters:
3442552f7358SJed Brown + dm           - The DMPlex object
3443552f7358SJed Brown - stratumValue - The requested depth
3444552f7358SJed Brown 
3445552f7358SJed Brown   Output Parameters:
3446552f7358SJed Brown + start - The first point at this depth
3447552f7358SJed Brown - end   - One beyond the last point at this depth
3448552f7358SJed Brown 
3449647867b2SJed Brown   Notes:
3450647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
3451647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
3452647867b2SJed Brown   higher dimension, e.g., "edges".
3453647867b2SJed Brown 
3454552f7358SJed Brown   Level: developer
3455552f7358SJed Brown 
3456552f7358SJed Brown .keywords: mesh, points
3457552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3458552f7358SJed Brown @*/
34590adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
34600adebc6cSBarry Smith {
3461aa50250dSMatthew G. Knepley   DMLabel        label;
346263d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3463552f7358SJed Brown   PetscErrorCode ierr;
3464552f7358SJed Brown 
3465552f7358SJed Brown   PetscFunctionBegin;
3466552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
346763d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
346863d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3469552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
34700d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
347163d1a920SMatthew G. Knepley   if (stratumValue < 0) {
347263d1a920SMatthew G. Knepley     if (start) *start = pStart;
347363d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
347463d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3475552f7358SJed Brown   }
3476aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
347763d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
347863d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3479552f7358SJed Brown   PetscFunctionReturn(0);
3480552f7358SJed Brown }
3481552f7358SJed Brown 
3482552f7358SJed Brown /*@
3483552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3484552f7358SJed Brown 
3485552f7358SJed Brown   Not Collective
3486552f7358SJed Brown 
3487552f7358SJed Brown   Input Parameters:
3488552f7358SJed Brown + dm           - The DMPlex object
3489552f7358SJed Brown - stratumValue - The requested height
3490552f7358SJed Brown 
3491552f7358SJed Brown   Output Parameters:
3492552f7358SJed Brown + start - The first point at this height
3493552f7358SJed Brown - end   - One beyond the last point at this height
3494552f7358SJed Brown 
3495647867b2SJed Brown   Notes:
3496647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
3497647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
3498647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
3499647867b2SJed Brown 
3500552f7358SJed Brown   Level: developer
3501552f7358SJed Brown 
3502552f7358SJed Brown .keywords: mesh, points
3503552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3504552f7358SJed Brown @*/
35050adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
35060adebc6cSBarry Smith {
3507aa50250dSMatthew G. Knepley   DMLabel        label;
350863d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3509552f7358SJed Brown   PetscErrorCode ierr;
3510552f7358SJed Brown 
3511552f7358SJed Brown   PetscFunctionBegin;
3512552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
351363d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
351463d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3515552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
35160d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
351763d1a920SMatthew G. Knepley   if (stratumValue < 0) {
351863d1a920SMatthew G. Knepley     if (start) *start = pStart;
351963d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
352063d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3521552f7358SJed Brown   }
3522aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
352313903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
352463d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
352563d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3526552f7358SJed Brown   PetscFunctionReturn(0);
3527552f7358SJed Brown }
3528552f7358SJed Brown 
35290adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
35300adebc6cSBarry Smith {
3531efe440bfSMatthew G. Knepley   PetscSection   section, s;
3532efe440bfSMatthew G. Knepley   Mat            m;
35333e922f36SToby Isaac   PetscInt       maxHeight;
3534552f7358SJed Brown   PetscErrorCode ierr;
3535552f7358SJed Brown 
3536552f7358SJed Brown   PetscFunctionBegin;
353738221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
35383e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
35393e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
354082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3541e87a4003SBarry Smith   ierr = DMSetSection(*cdm, section);CHKERRQ(ierr);
35421d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3543efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3544efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3545efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3546efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3547efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
35488f4c458bSMatthew G. Knepley 
35498f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
35508f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
3551552f7358SJed Brown   PetscFunctionReturn(0);
3552552f7358SJed Brown }
3553552f7358SJed Brown 
3554f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3555f19dbd58SToby Isaac {
3556f19dbd58SToby Isaac   Vec            coordsLocal;
3557f19dbd58SToby Isaac   DM             coordsDM;
3558f19dbd58SToby Isaac   PetscErrorCode ierr;
3559f19dbd58SToby Isaac 
3560f19dbd58SToby Isaac   PetscFunctionBegin;
3561f19dbd58SToby Isaac   *field = NULL;
3562f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3563f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3564f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3565f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3566f19dbd58SToby Isaac   }
3567f19dbd58SToby Isaac   PetscFunctionReturn(0);
3568f19dbd58SToby Isaac }
3569f19dbd58SToby Isaac 
35707cd05799SMatthew G. Knepley /*@C
35717cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
35727cd05799SMatthew G. Knepley 
35737cd05799SMatthew G. Knepley   Not Collective
35747cd05799SMatthew G. Knepley 
35757cd05799SMatthew G. Knepley   Input Parameters:
35767cd05799SMatthew G. Knepley . dm        - The DMPlex object
35777cd05799SMatthew G. Knepley 
35787cd05799SMatthew G. Knepley   Output Parameter:
35797cd05799SMatthew G. Knepley . section - The PetscSection object
35807cd05799SMatthew G. Knepley 
35817cd05799SMatthew G. Knepley   Level: developer
35827cd05799SMatthew G. Knepley 
35837cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
35847cd05799SMatthew G. Knepley @*/
35850adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
35860adebc6cSBarry Smith {
3587552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3588552f7358SJed Brown 
3589552f7358SJed Brown   PetscFunctionBegin;
3590552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3591552f7358SJed Brown   if (section) *section = mesh->coneSection;
3592552f7358SJed Brown   PetscFunctionReturn(0);
3593552f7358SJed Brown }
3594552f7358SJed Brown 
35957cd05799SMatthew G. Knepley /*@C
35967cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
35977cd05799SMatthew G. Knepley 
35987cd05799SMatthew G. Knepley   Not Collective
35997cd05799SMatthew G. Knepley 
36007cd05799SMatthew G. Knepley   Input Parameters:
36017cd05799SMatthew G. Knepley . dm        - The DMPlex object
36027cd05799SMatthew G. Knepley 
36037cd05799SMatthew G. Knepley   Output Parameter:
36047cd05799SMatthew G. Knepley . section - The PetscSection object
36057cd05799SMatthew G. Knepley 
36067cd05799SMatthew G. Knepley   Level: developer
36077cd05799SMatthew G. Knepley 
36087cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
36097cd05799SMatthew G. Knepley @*/
36108cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
36118cb4d582SMatthew G. Knepley {
36128cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
36138cb4d582SMatthew G. Knepley 
36148cb4d582SMatthew G. Knepley   PetscFunctionBegin;
36158cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36168cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
36178cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
36188cb4d582SMatthew G. Knepley }
36198cb4d582SMatthew G. Knepley 
36207cd05799SMatthew G. Knepley /*@C
36217cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
36227cd05799SMatthew G. Knepley 
36237cd05799SMatthew G. Knepley   Not Collective
36247cd05799SMatthew G. Knepley 
36257cd05799SMatthew G. Knepley   Input Parameters:
36267cd05799SMatthew G. Knepley . dm        - The DMPlex object
36277cd05799SMatthew G. Knepley 
36287cd05799SMatthew G. Knepley   Output Parameter:
36297cd05799SMatthew G. Knepley . cones - The cone for each point
36307cd05799SMatthew G. Knepley 
36317cd05799SMatthew G. Knepley   Level: developer
36327cd05799SMatthew G. Knepley 
36337cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
36347cd05799SMatthew G. Knepley @*/
3635a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3636a6dfd86eSKarl Rupp {
3637552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3638552f7358SJed Brown 
3639552f7358SJed Brown   PetscFunctionBegin;
3640552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3641552f7358SJed Brown   if (cones) *cones = mesh->cones;
3642552f7358SJed Brown   PetscFunctionReturn(0);
3643552f7358SJed Brown }
3644552f7358SJed Brown 
36457cd05799SMatthew G. Knepley /*@C
36467cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
36477cd05799SMatthew G. Knepley 
36487cd05799SMatthew G. Knepley   Not Collective
36497cd05799SMatthew G. Knepley 
36507cd05799SMatthew G. Knepley   Input Parameters:
36517cd05799SMatthew G. Knepley . dm        - The DMPlex object
36527cd05799SMatthew G. Knepley 
36537cd05799SMatthew G. Knepley   Output Parameter:
36547cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
36557cd05799SMatthew G. Knepley 
36567cd05799SMatthew G. Knepley   Level: developer
36577cd05799SMatthew G. Knepley 
36587cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
36597cd05799SMatthew G. Knepley @*/
3660a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3661a6dfd86eSKarl Rupp {
3662552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3663552f7358SJed Brown 
3664552f7358SJed Brown   PetscFunctionBegin;
3665552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3666552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3667552f7358SJed Brown   PetscFunctionReturn(0);
3668552f7358SJed Brown }
3669552f7358SJed Brown 
3670552f7358SJed Brown /******************************** FEM Support **********************************/
3671552f7358SJed Brown 
3672a4355906SMatthew Knepley /*@
3673bc1eb3faSJed Brown 
3674bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
3675bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
3676bc1eb3faSJed Brown   section provided (or default section of the DM).
3677a4355906SMatthew Knepley 
3678a4355906SMatthew Knepley   Input Parameters:
3679a4355906SMatthew Knepley + dm      - The DM
3680a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
3681a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
3682a4355906SMatthew Knepley 
3683a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
3684a4355906SMatthew Knepley   degree of the basis.
3685a4355906SMatthew Knepley 
3686bc1eb3faSJed Brown   Example:
3687bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
3688bc1eb3faSJed Brown .vb
3689bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
3690bc1eb3faSJed Brown 
3691bc1eb3faSJed Brown   v4 -- e6 -- v3
3692bc1eb3faSJed Brown   |           |
3693bc1eb3faSJed Brown   e7    c0    e8
3694bc1eb3faSJed Brown   |           |
3695bc1eb3faSJed Brown   v1 -- e5 -- v2
3696bc1eb3faSJed Brown .ve
3697bc1eb3faSJed Brown 
3698bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
3699bc1eb3faSJed Brown   dofs in the order of points, e.g.,
3700bc1eb3faSJed Brown .vb
3701bc1eb3faSJed Brown     c0 -> [0,1,2,3]
3702bc1eb3faSJed Brown     v1 -> [4]
3703bc1eb3faSJed Brown     ...
3704bc1eb3faSJed Brown     e5 -> [8, 9]
3705bc1eb3faSJed Brown .ve
3706bc1eb3faSJed Brown 
3707bc1eb3faSJed Brown   which corresponds to the dofs
3708bc1eb3faSJed Brown .vb
3709bc1eb3faSJed Brown     6   10  11  7
3710bc1eb3faSJed Brown     13  2   3   15
3711bc1eb3faSJed Brown     12  0   1   14
3712bc1eb3faSJed Brown     4   8   9   5
3713bc1eb3faSJed Brown .ve
3714bc1eb3faSJed Brown 
3715bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
3716bc1eb3faSJed Brown .vb
3717bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
3718bc1eb3faSJed Brown .ve
3719bc1eb3faSJed Brown 
3720bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
3721bc1eb3faSJed Brown .vb
3722bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
3723bc1eb3faSJed Brown .ve
3724bc1eb3faSJed Brown 
3725a4355906SMatthew Knepley   Level: developer
3726a4355906SMatthew Knepley 
3727a4355906SMatthew Knepley .seealso: DMGetSection(), PetscSectionSetClosurePermutation()
3728a4355906SMatthew Knepley @*/
3729bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
37303194fc30SMatthew G. Knepley {
37317391a63aSMatthew G. Knepley   DMLabel        label;
37323194fc30SMatthew G. Knepley   PetscInt      *perm;
37337391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
37343194fc30SMatthew G. Knepley   PetscErrorCode ierr;
37353194fc30SMatthew G. Knepley 
37363194fc30SMatthew G. Knepley   PetscFunctionBegin;
37377391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
37383194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
37397391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
37407391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
37417391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
37427391a63aSMatthew G. Knepley   else if  (depth == dim) {
37437391a63aSMatthew G. Knepley     const PetscInt *cone;
37447391a63aSMatthew G. Knepley 
37457391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3746d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
3747d4e6627bSStefano Zampini     else if (dim == 3) {
3748d4e6627bSStefano Zampini       const PetscInt *cone2;
3749d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
3750d4e6627bSStefano Zampini       eStart = cone2[0];
3751d4e6627bSStefano 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);
3752d4e6627bSStefano 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);
3753e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
37543194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
3755babf31e0SJed Brown   if (dim < 1) PetscFunctionReturn(0);
37563194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
37573194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
37583194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
37593194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
37603194fc30SMatthew G. Knepley     k = k/Nc + 1;
376189eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
37623194fc30SMatthew G. Knepley   }
37633194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
37643194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
376589eabcffSMatthew G. Knepley     switch (dim) {
3766babf31e0SJed Brown     case 1:
3767babf31e0SJed Brown       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
3768babf31e0SJed Brown       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
3769babf31e0SJed Brown       k = k/Nc + 1;
3770babf31e0SJed Brown       /*
3771babf31e0SJed Brown         Original ordering is [ edge of length k-1; vtx0; vtx1 ]
3772babf31e0SJed Brown         We want              [ vtx0; edge of length k-1; vtx1 ]
3773babf31e0SJed Brown       */
3774babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
3775babf31e0SJed Brown       for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
3776babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
3777babf31e0SJed Brown       foffset = offset;
3778babf31e0SJed Brown       break;
377989eabcffSMatthew G. Knepley     case 2:
37803194fc30SMatthew 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} */
37813194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
37823194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
37833194fc30SMatthew G. Knepley       k = k/Nc + 1;
37843194fc30SMatthew G. Knepley       /* The SEM order is
37853194fc30SMatthew G. Knepley 
37863194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
378789eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
37883194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
37893194fc30SMatthew G. Knepley       */
37903194fc30SMatthew G. Knepley       {
37913194fc30SMatthew G. Knepley         const PetscInt of   = 0;
37923194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
37933194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
37943194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
37953194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
37963194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
37973194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
37983194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
37993194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
38003194fc30SMatthew G. Knepley         PetscInt       o;
38013194fc30SMatthew G. Knepley 
38023194fc30SMatthew G. Knepley         /* bottom */
38033194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
38043194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
38053194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
38063194fc30SMatthew G. Knepley         /* middle */
38073194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
38083194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
38093194fc30SMatthew 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;
38103194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
38113194fc30SMatthew G. Knepley         }
38123194fc30SMatthew G. Knepley         /* top */
38133194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
38143194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
38153194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
38163194fc30SMatthew G. Knepley         foffset = offset;
38173194fc30SMatthew G. Knepley       }
381889eabcffSMatthew G. Knepley       break;
381989eabcffSMatthew G. Knepley     case 3:
382089eabcffSMatthew G. Knepley       /* The original hex closure is
382189eabcffSMatthew G. Knepley 
382289eabcffSMatthew G. Knepley          {c,
382389eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
382489eabcffSMatthew 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,
382589eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
382689eabcffSMatthew G. Knepley       */
382789eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
382889eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
382989eabcffSMatthew G. Knepley       k = k/Nc + 1;
383089eabcffSMatthew G. Knepley       /* The SEM order is
383189eabcffSMatthew G. Knepley          Bottom Slice
383289eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
383389eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
383489eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
383589eabcffSMatthew G. Knepley 
383689eabcffSMatthew G. Knepley          Middle Slice (j)
383789eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
383889eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
383989eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
384089eabcffSMatthew G. Knepley 
384189eabcffSMatthew G. Knepley          Top Slice
384289eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
384389eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
384489eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
384589eabcffSMatthew G. Knepley       */
384689eabcffSMatthew G. Knepley       {
384789eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
384889eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
384989eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
385089eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
385189eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
385289eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
385389eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
385489eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
385589eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
385689eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
385789eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
385889eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
385989eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
386089eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
386189eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
386289eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
386389eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
386489eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
386589eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
386689eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
386789eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
386889eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
386989eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
387089eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
387189eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
387289eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
387389eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
387489eabcffSMatthew G. Knepley         PetscInt       o, n;
387589eabcffSMatthew G. Knepley 
387689eabcffSMatthew G. Knepley         /* Bottom Slice */
387789eabcffSMatthew G. Knepley         /*   bottom */
387889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
387989eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
388089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
388189eabcffSMatthew G. Knepley         /*   middle */
388289eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
388389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3884316b7f87SMax 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;}
388589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
38863194fc30SMatthew G. Knepley         }
388789eabcffSMatthew G. Knepley         /*   top */
388889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
388989eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
389089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
389189eabcffSMatthew G. Knepley 
389289eabcffSMatthew G. Knepley         /* Middle Slice */
389389eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
389489eabcffSMatthew G. Knepley           /*   bottom */
389589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
389689eabcffSMatthew 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;
389789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
389889eabcffSMatthew G. Knepley           /*   middle */
389989eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
390089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
390189eabcffSMatthew 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;
390289eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
390389eabcffSMatthew G. Knepley           }
390489eabcffSMatthew G. Knepley           /*   top */
390589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
390689eabcffSMatthew 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;
390789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
390889eabcffSMatthew G. Knepley         }
390989eabcffSMatthew G. Knepley 
391089eabcffSMatthew G. Knepley         /* Top Slice */
391189eabcffSMatthew G. Knepley         /*   bottom */
391289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
391389eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
391489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
391589eabcffSMatthew G. Knepley         /*   middle */
391689eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
391789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
391889eabcffSMatthew 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;
391989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
392089eabcffSMatthew G. Knepley         }
392189eabcffSMatthew G. Knepley         /*   top */
392289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
392389eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
392489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
392589eabcffSMatthew G. Knepley 
392689eabcffSMatthew G. Knepley         foffset = offset;
392789eabcffSMatthew G. Knepley       }
392889eabcffSMatthew G. Knepley       break;
392989eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
393089eabcffSMatthew G. Knepley     }
393189eabcffSMatthew G. Knepley   }
393289eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
39333194fc30SMatthew G. Knepley   /* Check permutation */
39343194fc30SMatthew G. Knepley   {
39353194fc30SMatthew G. Knepley     PetscInt *check;
39363194fc30SMatthew G. Knepley 
39373194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
39383194fc30SMatthew 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]);}
39393194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
39403194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
39413194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
39423194fc30SMatthew G. Knepley   }
39431fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
39443194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
39453194fc30SMatthew G. Knepley }
39463194fc30SMatthew G. Knepley 
3947e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3948e071409bSToby Isaac {
3949e071409bSToby Isaac   PetscDS        prob;
3950e071409bSToby Isaac   PetscInt       depth, Nf, h;
3951e071409bSToby Isaac   DMLabel        label;
3952e071409bSToby Isaac   PetscErrorCode ierr;
3953e071409bSToby Isaac 
3954e071409bSToby Isaac   PetscFunctionBeginHot;
3955e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
3956e071409bSToby Isaac   Nf      = prob->Nf;
3957e071409bSToby Isaac   label   = dm->depthLabel;
3958e071409bSToby Isaac   *dspace = NULL;
3959e071409bSToby Isaac   if (field < Nf) {
3960e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3961e071409bSToby Isaac 
3962e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3963e071409bSToby Isaac       PetscDualSpace dsp;
3964e071409bSToby Isaac 
3965e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3966e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3967e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3968e071409bSToby Isaac       h    = depth - 1 - h;
3969e071409bSToby Isaac       if (h) {
3970e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3971e071409bSToby Isaac       } else {
3972e071409bSToby Isaac         *dspace = dsp;
3973e071409bSToby Isaac       }
3974e071409bSToby Isaac     }
3975e071409bSToby Isaac   }
3976e071409bSToby Isaac   PetscFunctionReturn(0);
3977e071409bSToby Isaac }
3978e071409bSToby Isaac 
3979e071409bSToby Isaac 
39801a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3981a6dfd86eSKarl Rupp {
3982552f7358SJed Brown   PetscScalar    *array, *vArray;
3983d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
39841a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3985552f7358SJed Brown   PetscErrorCode  ierr;
3986552f7358SJed Brown 
39871b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
39882a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
39895a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
39905a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
39915a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
39923f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
39939df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
39949df71ca4SMatthew G. Knepley       PetscInt dof;
3995d9917b9dSMatthew G. Knepley 
39969df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39979df71ca4SMatthew G. Knepley       size += dof;
39989df71ca4SMatthew G. Knepley     }
39999df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
40009df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
40012a3aaacfSMatthew G. Knepley       PetscInt       dof;
40025a1bb5cfSMatthew G. Knepley 
40035a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
40042a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
40055a1bb5cfSMatthew G. Knepley       size += dof;
40065a1bb5cfSMatthew G. Knepley     }
40073f7cbbe7SMatthew G. Knepley     if (!values) {
40083f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
40093f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
40103f7cbbe7SMatthew G. Knepley     }
401169291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4012982e9ed1SMatthew G. Knepley   } else {
4013982e9ed1SMatthew G. Knepley     array = *values;
4014982e9ed1SMatthew G. Knepley   }
40159df71ca4SMatthew G. Knepley   size = 0;
40165a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
40179df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
40189df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
40199df71ca4SMatthew G. Knepley     PetscScalar *varr;
4020d9917b9dSMatthew G. Knepley 
40219df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
40229df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
40239df71ca4SMatthew G. Knepley     varr = &vArray[off];
40241a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
40251a271a75SMatthew G. Knepley       array[offset] = varr[d];
40269df71ca4SMatthew G. Knepley     }
40279df71ca4SMatthew G. Knepley     size += dof;
40289df71ca4SMatthew G. Knepley   }
40299df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
40309df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
40319df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
40325a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
40335a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
40345a1bb5cfSMatthew G. Knepley 
403552ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
40365a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
40375a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
40385a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
40395a1bb5cfSMatthew G. Knepley     if (o >= 0) {
40401a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
40411a271a75SMatthew G. Knepley         array[offset] = varr[d];
40425a1bb5cfSMatthew G. Knepley       }
40435a1bb5cfSMatthew G. Knepley     } else {
40441a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
40451a271a75SMatthew G. Knepley         array[offset] = varr[d];
40465a1bb5cfSMatthew G. Knepley       }
40475a1bb5cfSMatthew G. Knepley     }
40489df71ca4SMatthew G. Knepley     size += dof;
40495a1bb5cfSMatthew G. Knepley   }
40505a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
40519df71ca4SMatthew G. Knepley   if (!*values) {
40525a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
40535a1bb5cfSMatthew G. Knepley     *values = array;
40549df71ca4SMatthew G. Knepley   } else {
40558ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
40568c312ff3SMatthew G. Knepley     *csize = size;
40579df71ca4SMatthew G. Knepley   }
40585a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
40595a1bb5cfSMatthew G. Knepley }
4060d9917b9dSMatthew G. Knepley 
40611dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4062923c78e0SToby Isaac {
4063923c78e0SToby Isaac   const PetscInt *cla;
4064923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4065923c78e0SToby Isaac   PetscErrorCode ierr;
4066923c78e0SToby Isaac 
4067923c78e0SToby Isaac   PetscFunctionBeginHot;
4068923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
4069923c78e0SToby Isaac   if (!*clPoints) {
4070923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
4071923c78e0SToby Isaac 
4072923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4073923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
4074923c78e0SToby Isaac     /* Compress out points not in the section */
4075923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
4076923c78e0SToby Isaac       PetscInt r = pts[2*p];
4077923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
4078923c78e0SToby Isaac         pts[q*2]   = r;
4079923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
4080923c78e0SToby Isaac         ++q;
4081923c78e0SToby Isaac       }
4082923c78e0SToby Isaac     }
4083923c78e0SToby Isaac     np = q;
4084923c78e0SToby Isaac     cla = NULL;
4085923c78e0SToby Isaac   } else {
4086923c78e0SToby Isaac     PetscInt dof, off;
4087923c78e0SToby Isaac 
4088923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4089923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4090923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4091923c78e0SToby Isaac     np   = dof/2;
4092923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
4093923c78e0SToby Isaac   }
4094923c78e0SToby Isaac   *numPoints = np;
4095923c78e0SToby Isaac   *points    = pts;
4096923c78e0SToby Isaac   *clp       = cla;
4097923c78e0SToby Isaac 
4098923c78e0SToby Isaac   PetscFunctionReturn(0);
4099923c78e0SToby Isaac }
4100923c78e0SToby Isaac 
41011dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4102923c78e0SToby Isaac {
4103923c78e0SToby Isaac   PetscErrorCode ierr;
4104923c78e0SToby Isaac 
4105923c78e0SToby Isaac   PetscFunctionBeginHot;
4106923c78e0SToby Isaac   if (!*clPoints) {
4107923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4108923c78e0SToby Isaac   } else {
4109923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4110923c78e0SToby Isaac   }
4111923c78e0SToby Isaac   *numPoints = 0;
4112923c78e0SToby Isaac   *points    = NULL;
4113923c78e0SToby Isaac   *clSec     = NULL;
4114923c78e0SToby Isaac   *clPoints  = NULL;
4115923c78e0SToby Isaac   *clp       = NULL;
4116923c78e0SToby Isaac   PetscFunctionReturn(0);
4117923c78e0SToby Isaac }
4118923c78e0SToby Isaac 
411997e99dd9SToby 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[])
41201a271a75SMatthew G. Knepley {
41211a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
412297e99dd9SToby Isaac   const PetscInt    **perms = NULL;
412397e99dd9SToby Isaac   const PetscScalar **flips = NULL;
41241a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
41251a271a75SMatthew G. Knepley 
41261a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4127fe02ba77SJed Brown   *size = 0;
412897e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
412997e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
413097e99dd9SToby Isaac     const PetscInt    point = points[2*p];
413197e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
413297e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
41331a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
41341a271a75SMatthew G. Knepley     const PetscScalar *varr;
41351a271a75SMatthew G. Knepley 
41361a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
41371a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
41381a271a75SMatthew G. Knepley     varr = &vArray[off];
413997e99dd9SToby Isaac     if (clperm) {
414097e99dd9SToby Isaac       if (perm) {
414197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
41421a271a75SMatthew G. Knepley       } else {
414397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
414497e99dd9SToby Isaac       }
414597e99dd9SToby Isaac       if (flip) {
414697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
414797e99dd9SToby Isaac       }
414897e99dd9SToby Isaac     } else {
414997e99dd9SToby Isaac       if (perm) {
415097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
415197e99dd9SToby Isaac       } else {
415297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
415397e99dd9SToby Isaac       }
415497e99dd9SToby Isaac       if (flip) {
415597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
41561a271a75SMatthew G. Knepley       }
41571a271a75SMatthew G. Knepley     }
415897e99dd9SToby Isaac     offset += dof;
415997e99dd9SToby Isaac   }
416097e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
41611a271a75SMatthew G. Knepley   *size = offset;
41621a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
41631a271a75SMatthew G. Knepley }
41641a271a75SMatthew G. Knepley 
416597e99dd9SToby 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[])
41661a271a75SMatthew G. Knepley {
41671a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
41681a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
41691a271a75SMatthew G. Knepley 
41701a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4171fe02ba77SJed Brown   *size = 0;
41721a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
417397e99dd9SToby Isaac     PetscInt          p;
417497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
417597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
41761a271a75SMatthew G. Knepley 
417797e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
417897e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
417997e99dd9SToby Isaac       const PetscInt    point = points[2*p];
418097e99dd9SToby Isaac       PetscInt          fdof, foff, b;
41811a271a75SMatthew G. Knepley       const PetscScalar *varr;
418297e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
418397e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
41841a271a75SMatthew G. Knepley 
41851a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
41861a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41871a271a75SMatthew G. Knepley       varr = &vArray[foff];
418897e99dd9SToby Isaac       if (clperm) {
418997e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
419097e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
419197e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
41921a271a75SMatthew G. Knepley       } else {
419397e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
419497e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
419597e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
41961a271a75SMatthew G. Knepley       }
419797e99dd9SToby Isaac       offset += fdof;
41981a271a75SMatthew G. Knepley     }
419997e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
42001a271a75SMatthew G. Knepley   }
42011a271a75SMatthew G. Knepley   *size = offset;
42021a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
42031a271a75SMatthew G. Knepley }
42041a271a75SMatthew G. Knepley 
4205552f7358SJed Brown /*@C
4206552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4207552f7358SJed Brown 
4208552f7358SJed Brown   Not collective
4209552f7358SJed Brown 
4210552f7358SJed Brown   Input Parameters:
4211552f7358SJed Brown + dm - The DM
4212552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4213552f7358SJed Brown . v - The local vector
421422c1ee49SMatthew G. Knepley . point - The point in the DM
421522c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
421622c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4217552f7358SJed Brown 
4218552f7358SJed Brown   Output Parameters:
421922c1ee49SMatthew G. Knepley + csize - The number of values in the closure
422022c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
422122c1ee49SMatthew G. Knepley 
422222c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
422322c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
422422c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
422522c1ee49SMatthew G. Knepley $
422622c1ee49SMatthew G. Knepley $ A typical use could be
422722c1ee49SMatthew G. Knepley $
422822c1ee49SMatthew G. Knepley $  values = NULL;
422922c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
423022c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
423122c1ee49SMatthew G. Knepley $    <Compute on closure>
423222c1ee49SMatthew G. Knepley $  }
423322c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
423422c1ee49SMatthew G. Knepley $
423522c1ee49SMatthew G. Knepley $ or
423622c1ee49SMatthew G. Knepley $
423722c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
423822c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
423922c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
424022c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
424122c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
424222c1ee49SMatthew G. Knepley $      <Compute on closure>
424322c1ee49SMatthew G. Knepley $    }
424422c1ee49SMatthew G. Knepley $  }
424522c1ee49SMatthew G. Knepley $  PetscFree(values);
4246552f7358SJed Brown 
4247552f7358SJed Brown   Fortran Notes:
4248552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4249552f7358SJed Brown   include petsc.h90 in your code.
4250552f7358SJed Brown 
4251552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4252552f7358SJed Brown 
4253552f7358SJed Brown   Level: intermediate
4254552f7358SJed Brown 
4255552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4256552f7358SJed Brown @*/
4257552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4258552f7358SJed Brown {
4259552f7358SJed Brown   PetscSection       clSection;
4260d9917b9dSMatthew G. Knepley   IS                 clPoints;
42618a84db2dSMatthew G. Knepley   PetscScalar       *array;
42628a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4263552f7358SJed Brown   PetscInt          *points = NULL;
42648f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
42651a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4266552f7358SJed Brown   PetscErrorCode     ierr;
4267552f7358SJed Brown 
4268d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4269552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4270e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
42711a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
42721a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4273552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4274552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4275552f7358SJed Brown   if (depth == 1 && numFields < 2) {
42761a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4277552f7358SJed Brown     PetscFunctionReturn(0);
4278552f7358SJed Brown   }
42791a271a75SMatthew G. Knepley   /* Get points */
4280923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
42811fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
42821a271a75SMatthew G. Knepley   /* Get array */
4283bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
42841a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4285552f7358SJed Brown 
42861a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4287552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
42881a271a75SMatthew G. Knepley       asize += dof;
4289552f7358SJed Brown     }
4290bd6c0d32SMatthew G. Knepley     if (!values) {
4291923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
42921a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4293bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4294bd6c0d32SMatthew G. Knepley     }
429569291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4296d0f6b257SMatthew G. Knepley   } else {
4297d0f6b257SMatthew G. Knepley     array = *values;
4298d0f6b257SMatthew G. Knepley   }
42998a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
43001a271a75SMatthew G. Knepley   /* Get values */
430197e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
430297e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
43031a271a75SMatthew G. Knepley   /* Cleanup points */
4304923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
43051a271a75SMatthew G. Knepley   /* Cleanup array */
43068a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4307d0f6b257SMatthew G. Knepley   if (!*values) {
4308552f7358SJed Brown     if (csize) *csize = size;
4309552f7358SJed Brown     *values = array;
4310d0f6b257SMatthew G. Knepley   } else {
4311f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4312d0f6b257SMatthew G. Knepley     *csize = size;
4313d0f6b257SMatthew G. Knepley   }
4314552f7358SJed Brown   PetscFunctionReturn(0);
4315552f7358SJed Brown }
4316552f7358SJed Brown 
4317552f7358SJed Brown /*@C
4318552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4319552f7358SJed Brown 
4320552f7358SJed Brown   Not collective
4321552f7358SJed Brown 
4322552f7358SJed Brown   Input Parameters:
4323552f7358SJed Brown + dm - The DM
43240298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4325552f7358SJed Brown . v - The local vector
4326eaf898f9SPatrick Sanan . point - The point in the DM
43270298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4328552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4329552f7358SJed Brown 
433022c1ee49SMatthew 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()
433122c1ee49SMatthew G. Knepley 
43323813dfbdSMatthew G Knepley   Fortran Notes:
43333813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
43343813dfbdSMatthew G Knepley   include petsc.h90 in your code.
43353813dfbdSMatthew G Knepley 
43363813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
43373813dfbdSMatthew G Knepley 
4338552f7358SJed Brown   Level: intermediate
4339552f7358SJed Brown 
4340552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4341552f7358SJed Brown @*/
43427c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4343a6dfd86eSKarl Rupp {
4344552f7358SJed Brown   PetscInt       size = 0;
4345552f7358SJed Brown   PetscErrorCode ierr;
4346552f7358SJed Brown 
4347552f7358SJed Brown   PetscFunctionBegin;
4348552f7358SJed Brown   /* Should work without recalculating size */
434969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4350c9fdaa05SMatthew G. Knepley   *values = NULL;
4351552f7358SJed Brown   PetscFunctionReturn(0);
4352552f7358SJed Brown }
4353552f7358SJed Brown 
4354552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4355552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4356552f7358SJed Brown 
435797e99dd9SToby 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[])
4358552f7358SJed Brown {
4359552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4360552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4361552f7358SJed Brown   PetscScalar    *a;
4362552f7358SJed Brown   PetscInt        off, cind = 0, k;
4363552f7358SJed Brown   PetscErrorCode  ierr;
4364552f7358SJed Brown 
4365552f7358SJed Brown   PetscFunctionBegin;
4366552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4367552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4368552f7358SJed Brown   a    = &array[off];
4369552f7358SJed Brown   if (!cdof || setBC) {
437097e99dd9SToby Isaac     if (clperm) {
437197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
437297e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4373552f7358SJed Brown     } else {
437497e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
437597e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4376552f7358SJed Brown     }
4377552f7358SJed Brown   } else {
4378552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
437997e99dd9SToby Isaac     if (clperm) {
438097e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4381552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
438297e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4383552f7358SJed Brown         }
4384552f7358SJed Brown       } else {
4385552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4386552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
438797e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
438897e99dd9SToby Isaac         }
438997e99dd9SToby Isaac       }
439097e99dd9SToby Isaac     } else {
439197e99dd9SToby Isaac       if (perm) {
439297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
439397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
439497e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
439597e99dd9SToby Isaac         }
439697e99dd9SToby Isaac       } else {
439797e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
439897e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
439997e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
440097e99dd9SToby Isaac         }
4401552f7358SJed Brown       }
4402552f7358SJed Brown     }
4403552f7358SJed Brown   }
4404552f7358SJed Brown   PetscFunctionReturn(0);
4405552f7358SJed Brown }
4406552f7358SJed Brown 
440797e99dd9SToby 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[])
4408a5e93ea8SMatthew G. Knepley {
4409a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4410a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4411a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4412a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4413a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4414a5e93ea8SMatthew G. Knepley 
4415a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4416a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4417a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4418a5e93ea8SMatthew G. Knepley   a    = &array[off];
4419a5e93ea8SMatthew G. Knepley   if (cdof) {
4420a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
442197e99dd9SToby Isaac     if (clperm) {
442297e99dd9SToby Isaac       if (perm) {
4423a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4424a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
442597e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
442697e99dd9SToby Isaac             cind++;
4427a5e93ea8SMatthew G. Knepley           }
4428a5e93ea8SMatthew G. Knepley         }
4429a5e93ea8SMatthew G. Knepley       } else {
4430a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4431a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
443297e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
443397e99dd9SToby Isaac             cind++;
443497e99dd9SToby Isaac           }
443597e99dd9SToby Isaac         }
443697e99dd9SToby Isaac       }
443797e99dd9SToby Isaac     } else {
443897e99dd9SToby Isaac       if (perm) {
443997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
444097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
444197e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
444297e99dd9SToby Isaac             cind++;
444397e99dd9SToby Isaac           }
444497e99dd9SToby Isaac         }
444597e99dd9SToby Isaac       } else {
444697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
444797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
444897e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
444997e99dd9SToby Isaac             cind++;
445097e99dd9SToby Isaac           }
4451a5e93ea8SMatthew G. Knepley         }
4452a5e93ea8SMatthew G. Knepley       }
4453a5e93ea8SMatthew G. Knepley     }
4454a5e93ea8SMatthew G. Knepley   }
4455a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4456a5e93ea8SMatthew G. Knepley }
4457a5e93ea8SMatthew G. Knepley 
445897e99dd9SToby 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[])
4459a6dfd86eSKarl Rupp {
4460552f7358SJed Brown   PetscScalar    *a;
44611a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
44621a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
446397e99dd9SToby Isaac   PetscInt        cind = 0, b;
4464552f7358SJed Brown   PetscErrorCode  ierr;
4465552f7358SJed Brown 
4466552f7358SJed Brown   PetscFunctionBegin;
4467552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4468552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
44691a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
44701a271a75SMatthew G. Knepley   a    = &array[foff];
4471552f7358SJed Brown   if (!fcdof || setBC) {
447297e99dd9SToby Isaac     if (clperm) {
447397e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
447497e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4475552f7358SJed Brown     } else {
447697e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
447797e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4478552f7358SJed Brown     }
4479552f7358SJed Brown   } else {
4480552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
448197e99dd9SToby Isaac     if (clperm) {
448297e99dd9SToby Isaac       if (perm) {
448397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
448497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
448597e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4486552f7358SJed Brown         }
4487552f7358SJed Brown       } else {
448897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
448997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
449097e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
449197e99dd9SToby Isaac         }
449297e99dd9SToby Isaac       }
449397e99dd9SToby Isaac     } else {
449497e99dd9SToby Isaac       if (perm) {
449597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
449697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
449797e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
449897e99dd9SToby Isaac         }
449997e99dd9SToby Isaac       } else {
450097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
450197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
450297e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4503552f7358SJed Brown         }
4504552f7358SJed Brown       }
4505552f7358SJed Brown     }
4506552f7358SJed Brown   }
45071a271a75SMatthew G. Knepley   *offset += fdof;
4508552f7358SJed Brown   PetscFunctionReturn(0);
4509552f7358SJed Brown }
4510552f7358SJed Brown 
4511ba322698SMatthew 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[])
4512a5e93ea8SMatthew G. Knepley {
4513a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
45141a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
45151a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4516ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4517ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4518a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4519a5e93ea8SMatthew G. Knepley 
4520a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4521a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4522a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
45231a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
45241a271a75SMatthew G. Knepley   a    = &array[foff];
4525a5e93ea8SMatthew G. Knepley   if (fcdof) {
4526ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4527a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
452897e99dd9SToby Isaac     if (clperm) {
452997e99dd9SToby Isaac       if (perm) {
4530ba322698SMatthew G. Knepley         if (comps) {
4531ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4532ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4533ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4534ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4535ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4536ba322698SMatthew G. Knepley           }
4537ba322698SMatthew G. Knepley         } else {
453897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
453997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
454097e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4541a5e93ea8SMatthew G. Knepley               ++cind;
4542a5e93ea8SMatthew G. Knepley             }
4543a5e93ea8SMatthew G. Knepley           }
4544ba322698SMatthew G. Knepley         }
4545ba322698SMatthew G. Knepley       } else {
4546ba322698SMatthew G. Knepley         if (comps) {
4547ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4548ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4549ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4550ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4551ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4552ba322698SMatthew G. Knepley           }
4553a5e93ea8SMatthew G. Knepley         } else {
455497e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
455597e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
455697e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
455797e99dd9SToby Isaac               ++cind;
455897e99dd9SToby Isaac             }
455997e99dd9SToby Isaac           }
456097e99dd9SToby Isaac         }
4561ba322698SMatthew G. Knepley       }
456297e99dd9SToby Isaac     } else {
456397e99dd9SToby Isaac       if (perm) {
4564ba322698SMatthew G. Knepley         if (comps) {
4565ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4566ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4567ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4568ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4569ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4570ba322698SMatthew G. Knepley           }
4571ba322698SMatthew G. Knepley         } else {
457297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
457397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
457497e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
457597e99dd9SToby Isaac               ++cind;
457697e99dd9SToby Isaac             }
457797e99dd9SToby Isaac           }
4578ba322698SMatthew G. Knepley         }
4579ba322698SMatthew G. Knepley       } else {
4580ba322698SMatthew G. Knepley         if (comps) {
4581ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4582ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4583ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4584ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4585ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4586ba322698SMatthew G. Knepley           }
458797e99dd9SToby Isaac         } else {
458897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
458997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
459097e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4591a5e93ea8SMatthew G. Knepley               ++cind;
4592a5e93ea8SMatthew G. Knepley             }
4593a5e93ea8SMatthew G. Knepley           }
4594a5e93ea8SMatthew G. Knepley         }
4595a5e93ea8SMatthew G. Knepley       }
4596a5e93ea8SMatthew G. Knepley     }
4597ba322698SMatthew G. Knepley   }
45981a271a75SMatthew G. Knepley   *offset += fdof;
4599a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4600a5e93ea8SMatthew G. Knepley }
4601a5e93ea8SMatthew G. Knepley 
46028f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4603a6dfd86eSKarl Rupp {
4604552f7358SJed Brown   PetscScalar    *array;
46051b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
46061b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4607552f7358SJed Brown   PetscErrorCode  ierr;
4608552f7358SJed Brown 
46091b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4610b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4611b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4612b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4613b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4614b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4615b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4616b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4617b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4618b6ebb6e6SMatthew G. Knepley 
4619b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4620b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4621b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4622b6ebb6e6SMatthew G. Knepley     {
4623b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4624b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4625b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4626b6ebb6e6SMatthew G. Knepley 
4627b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4628b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4629b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4630b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4631b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4632b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4633b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4634b6ebb6e6SMatthew G. Knepley           }
4635b6ebb6e6SMatthew G. Knepley         } else {
4636b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4637b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4638b6ebb6e6SMatthew G. Knepley           }
4639b6ebb6e6SMatthew G. Knepley         }
4640b6ebb6e6SMatthew G. Knepley       } else {
4641b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4642b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4643b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4644b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4645b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4646b6ebb6e6SMatthew G. Knepley           }
4647b6ebb6e6SMatthew G. Knepley         } else {
4648b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4649b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4650b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4651b6ebb6e6SMatthew G. Knepley           }
4652b6ebb6e6SMatthew G. Knepley         }
4653b6ebb6e6SMatthew G. Knepley       }
4654b6ebb6e6SMatthew G. Knepley     }
4655b6ebb6e6SMatthew G. Knepley   }
4656b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4657b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4658b6ebb6e6SMatthew G. Knepley }
46591b406b76SMatthew G. Knepley 
46601b406b76SMatthew G. Knepley /*@C
46611b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
46621b406b76SMatthew G. Knepley 
46631b406b76SMatthew G. Knepley   Not collective
46641b406b76SMatthew G. Knepley 
46651b406b76SMatthew G. Knepley   Input Parameters:
46661b406b76SMatthew G. Knepley + dm - The DM
46671b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
46681b406b76SMatthew G. Knepley . v - The local vector
4669eaf898f9SPatrick Sanan . point - The point in the DM
46701b406b76SMatthew G. Knepley . values - The array of values
467122c1ee49SMatthew 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,
467222c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
46731b406b76SMatthew G. Knepley 
46741b406b76SMatthew G. Knepley   Fortran Notes:
46751b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
46761b406b76SMatthew G. Knepley 
46771b406b76SMatthew G. Knepley   Level: intermediate
46781b406b76SMatthew G. Knepley 
46791b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
46801b406b76SMatthew G. Knepley @*/
46811b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
46821b406b76SMatthew G. Knepley {
46831b406b76SMatthew G. Knepley   PetscSection    clSection;
46841b406b76SMatthew G. Knepley   IS              clPoints;
46851b406b76SMatthew G. Knepley   PetscScalar    *array;
46861b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
468797e99dd9SToby Isaac   const PetscInt *clp, *clperm;
46881a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
46891b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
46901b406b76SMatthew G. Knepley 
46911a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
46921b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4693e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
46941a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46951a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
46961b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
46971b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
46981b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
46998f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
47001b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
47011b406b76SMatthew G. Knepley   }
47021a271a75SMatthew G. Knepley   /* Get points */
470397e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4704923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
47051a271a75SMatthew G. Knepley   /* Get array */
4706552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
47071a271a75SMatthew G. Knepley   /* Get values */
4708ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
470997e99dd9SToby Isaac     PetscInt offset = 0, f;
4710552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
471197e99dd9SToby Isaac       const PetscInt    **perms = NULL;
471297e99dd9SToby Isaac       const PetscScalar **flips = NULL;
471397e99dd9SToby Isaac 
471497e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4715552f7358SJed Brown       switch (mode) {
4716552f7358SJed Brown       case INSERT_VALUES:
471797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
471897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
471997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
472097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
472197e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4722552f7358SJed Brown         } break;
4723552f7358SJed Brown       case INSERT_ALL_VALUES:
472497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
472597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
472697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
472797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
472897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4729552f7358SJed Brown         } break;
4730a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
473197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
473297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
473397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
473497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4735ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4736a5e93ea8SMatthew G. Knepley         } break;
4737552f7358SJed Brown       case ADD_VALUES:
473897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
473997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
474097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
474197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
474297e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4743552f7358SJed Brown         } break;
4744552f7358SJed Brown       case ADD_ALL_VALUES:
474597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
474697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
474797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
474897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
474997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4750552f7358SJed Brown         } break;
4751304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
475297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
475397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
475497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
475597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4756ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4757304ab55fSMatthew G. Knepley         } break;
4758552f7358SJed Brown       default:
4759f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4760552f7358SJed Brown       }
476197e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
47621a271a75SMatthew G. Knepley     }
4763552f7358SJed Brown   } else {
47641a271a75SMatthew G. Knepley     PetscInt dof, off;
476597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
476697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
47671a271a75SMatthew G. Knepley 
476897e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4769552f7358SJed Brown     switch (mode) {
4770552f7358SJed Brown     case INSERT_VALUES:
477197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
477297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
477397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
477497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
477597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
477697e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4777552f7358SJed Brown       } break;
4778552f7358SJed Brown     case INSERT_ALL_VALUES:
477997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
478097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
478197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
478297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
478397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
478497e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4785552f7358SJed Brown       } break;
4786a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
478797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
478897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
478997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
479097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
479197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
479297e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4793a5e93ea8SMatthew G. Knepley       } break;
4794552f7358SJed Brown     case ADD_VALUES:
479597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
479697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
479797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
479897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
479997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
480097e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4801552f7358SJed Brown       } break;
4802552f7358SJed Brown     case ADD_ALL_VALUES:
480397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
480497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
480597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
480697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
480797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
480897e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4809552f7358SJed Brown       } break;
4810304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
481197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
481297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
481397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
481497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
481597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
481697e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4817304ab55fSMatthew G. Knepley       } break;
4818552f7358SJed Brown     default:
4819f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4820552f7358SJed Brown     }
482197e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4822552f7358SJed Brown   }
48231a271a75SMatthew G. Knepley   /* Cleanup points */
4824923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
48251a271a75SMatthew G. Knepley   /* Cleanup array */
4826552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4827552f7358SJed Brown   PetscFunctionReturn(0);
4828552f7358SJed Brown }
4829552f7358SJed Brown 
4830ba322698SMatthew 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)
4831e07394fbSMatthew G. Knepley {
4832e07394fbSMatthew G. Knepley   PetscSection      clSection;
4833e07394fbSMatthew G. Knepley   IS                clPoints;
4834e07394fbSMatthew G. Knepley   PetscScalar       *array;
4835e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4836b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4837e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
483897e99dd9SToby Isaac   PetscInt          offset = 0, f;
4839e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4840e07394fbSMatthew G. Knepley 
4841e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4842e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4843e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
4844e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4845e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4846e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4847e07394fbSMatthew G. Knepley   /* Get points */
4848b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4849923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4850e07394fbSMatthew G. Knepley   /* Get array */
4851e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4852e07394fbSMatthew G. Knepley   /* Get values */
4853e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
485497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
485597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
485697e99dd9SToby Isaac 
4857e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4858e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4859e07394fbSMatthew G. Knepley         PetscInt fdof;
4860e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4861e07394fbSMatthew G. Knepley         offset += fdof;
4862e07394fbSMatthew G. Knepley       }
4863e07394fbSMatthew G. Knepley       continue;
4864e07394fbSMatthew G. Knepley     }
486597e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4866e07394fbSMatthew G. Knepley     switch (mode) {
4867e07394fbSMatthew G. Knepley     case INSERT_VALUES:
486897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
486997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
487097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
487197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4872b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4873e07394fbSMatthew G. Knepley       } break;
4874e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
487597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
487697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
487797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
487897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4879b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4880e07394fbSMatthew G. Knepley         } break;
4881e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
488297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
488397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
488497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
488597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4886ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
4887e07394fbSMatthew G. Knepley       } break;
4888e07394fbSMatthew G. Knepley     case ADD_VALUES:
488997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
489097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
489197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
489297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4893b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4894e07394fbSMatthew G. Knepley       } break;
4895e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
489697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
489797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
489897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
489997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4900b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4901e07394fbSMatthew G. Knepley       } break;
4902e07394fbSMatthew G. Knepley     default:
4903f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4904e07394fbSMatthew G. Knepley     }
490597e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4906e07394fbSMatthew G. Knepley   }
4907e07394fbSMatthew G. Knepley   /* Cleanup points */
4908923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4909e07394fbSMatthew G. Knepley   /* Cleanup array */
4910e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4911e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4912e07394fbSMatthew G. Knepley }
4913e07394fbSMatthew G. Knepley 
49147cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4915552f7358SJed Brown {
4916552f7358SJed Brown   PetscMPIInt    rank;
4917552f7358SJed Brown   PetscInt       i, j;
4918552f7358SJed Brown   PetscErrorCode ierr;
4919552f7358SJed Brown 
4920552f7358SJed Brown   PetscFunctionBegin;
492182f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4922eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
4923e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4924e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4925b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4926b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4927e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4928b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4929519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
49307eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4931552f7358SJed Brown #else
4932b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4933552f7358SJed Brown #endif
4934552f7358SJed Brown     }
493577a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4936552f7358SJed Brown   }
4937552f7358SJed Brown   PetscFunctionReturn(0);
4938552f7358SJed Brown }
4939552f7358SJed Brown 
494005586334SMatthew G. Knepley /*
494105586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
494205586334SMatthew G. Knepley 
494305586334SMatthew G. Knepley   Input Parameters:
494405586334SMatthew G. Knepley + section - The section for this data layout
494505586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
494605586334SMatthew G. Knepley . off     - The global offset of this point
494705586334SMatthew G. Knepley . loff    - The local offset of each field
494805586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
494905586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
495005586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
495105586334SMatthew G. Knepley 
495205586334SMatthew G. Knepley   Output Parameter:
495305586334SMatthew G. Knepley . indices - Indices for dofs on this point
495405586334SMatthew G. Knepley 
495505586334SMatthew G. Knepley   Level: developer
495605586334SMatthew G. Knepley 
495705586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
495805586334SMatthew G. Knepley */
495905586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
4960a6dfd86eSKarl Rupp {
4961e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
4962552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
4963552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4964552f7358SJed Brown   PetscInt        cind = 0, k;
4965552f7358SJed Brown   PetscErrorCode  ierr;
4966552f7358SJed Brown 
4967552f7358SJed Brown   PetscFunctionBegin;
4968552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4969552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4970552f7358SJed Brown   if (!cdof || setBC) {
497105586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
497205586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
497305586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
497405586334SMatthew G. Knepley 
497505586334SMatthew G. Knepley       indices[ind] = off + k;
4976552f7358SJed Brown     }
4977552f7358SJed Brown   } else {
4978552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
49794acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
498005586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
498105586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
498205586334SMatthew G. Knepley 
49834acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
49844acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
498505586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
49864acb8e1eSToby Isaac         ++cind;
49874acb8e1eSToby Isaac       } else {
498805586334SMatthew G. Knepley         indices[ind] = off+k-cind;
4989552f7358SJed Brown       }
4990552f7358SJed Brown     }
4991552f7358SJed Brown   }
4992e6ccafaeSMatthew G Knepley   *loff += dof;
4993552f7358SJed Brown   PetscFunctionReturn(0);
4994552f7358SJed Brown }
4995552f7358SJed Brown 
49967e29afd2SMatthew G. Knepley /*
49977e29afd2SMatthew G. Knepley   This version only believes the point offset from the globalSection
49987e29afd2SMatthew G. Knepley 
49997e29afd2SMatthew G. Knepley  . off - The global offset of this point
50007e29afd2SMatthew G. Knepley */
500105586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
5002a6dfd86eSKarl Rupp {
5003552f7358SJed Brown   PetscInt       numFields, foff, f;
5004552f7358SJed Brown   PetscErrorCode ierr;
5005552f7358SJed Brown 
5006552f7358SJed Brown   PetscFunctionBegin;
5007552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5008552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
50094acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5010552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
50114acb8e1eSToby Isaac     PetscInt        cind = 0, b;
50124acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5013552f7358SJed Brown 
5014552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5015552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5016552f7358SJed Brown     if (!cfdof || setBC) {
501705586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
501805586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
501905586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
502005586334SMatthew G. Knepley 
502105586334SMatthew G. Knepley         indices[ind] = off+foff+b;
502205586334SMatthew G. Knepley       }
5023552f7358SJed Brown     } else {
5024552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
502505586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
502605586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
502705586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
502805586334SMatthew G. Knepley 
50294acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
503005586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
5031552f7358SJed Brown           ++cind;
5032552f7358SJed Brown         } else {
503305586334SMatthew G. Knepley           indices[ind] = off+foff+b-cind;
5034552f7358SJed Brown         }
5035552f7358SJed Brown       }
5036552f7358SJed Brown     }
5037a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
5038552f7358SJed Brown     foffs[f] += fdof;
5039552f7358SJed Brown   }
5040552f7358SJed Brown   PetscFunctionReturn(0);
5041552f7358SJed Brown }
5042552f7358SJed Brown 
50437e29afd2SMatthew G. Knepley /*
50447e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
50457e29afd2SMatthew G. Knepley 
50467e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
50477e29afd2SMatthew G. Knepley */
504805586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
50497e29afd2SMatthew G. Knepley {
50507e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
50517e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
50527e29afd2SMatthew G. Knepley 
50537e29afd2SMatthew G. Knepley   PetscFunctionBegin;
50547e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
50557e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
50567e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
50577e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
50587e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
50597e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
50607e29afd2SMatthew G. Knepley 
50617e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
50627e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
50637e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
50647e29afd2SMatthew G. Knepley     if (!cfdof || setBC) {
506505586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
506605586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
506705586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
506805586334SMatthew G. Knepley 
506905586334SMatthew G. Knepley         indices[ind] = foff+b;
507005586334SMatthew G. Knepley       }
50717e29afd2SMatthew G. Knepley     } else {
50727e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
507305586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
507405586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
507505586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
507605586334SMatthew G. Knepley 
50777e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
507805586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
50797e29afd2SMatthew G. Knepley           ++cind;
50807e29afd2SMatthew G. Knepley         } else {
508105586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
50827e29afd2SMatthew G. Knepley         }
50837e29afd2SMatthew G. Knepley       }
50847e29afd2SMatthew G. Knepley     }
50857e29afd2SMatthew G. Knepley     foffs[f] += fdof;
50867e29afd2SMatthew G. Knepley   }
50877e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
50887e29afd2SMatthew G. Knepley }
50897e29afd2SMatthew G. Knepley 
50904acb8e1eSToby 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)
5091d3d1a6afSToby Isaac {
5092d3d1a6afSToby Isaac   Mat             cMat;
5093d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
5094d3d1a6afSToby Isaac   IS              aIS;
5095d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
5096d3d1a6afSToby Isaac   const PetscInt  *anchors;
5097e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
5098d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
5099d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
5100d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
5101d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
5102d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
5103d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
5104d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
51056ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
5106d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
5107d3d1a6afSToby Isaac   PetscErrorCode  ierr;
5108d3d1a6afSToby Isaac 
5109d3d1a6afSToby Isaac   PetscFunctionBegin;
5110d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5111d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5112d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5113d3d1a6afSToby Isaac 
5114a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
5115d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
5116d3d1a6afSToby Isaac   if (aSec) {
5117d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5118d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
5119d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
5120d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
5121d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
5122d3d1a6afSToby Isaac      * into the global matrix anyway) */
5123d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
5124d3d1a6afSToby Isaac       PetscInt b    = points[p];
51254b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5126d3d1a6afSToby Isaac 
51274b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
51284b2f2278SToby Isaac       if (!bSecDof) {
51294b2f2278SToby Isaac         continue;
51304b2f2278SToby Isaac       }
5131d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5132d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
5133d3d1a6afSToby Isaac       }
5134d3d1a6afSToby Isaac       if (bDof) {
5135d3d1a6afSToby Isaac         /* this point is constrained */
5136d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
5137d3d1a6afSToby Isaac         PetscInt bOff, q;
5138d3d1a6afSToby Isaac 
5139d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
5140d3d1a6afSToby Isaac         newNumPoints  += bDof;
5141d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
5142d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5143d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5144d3d1a6afSToby Isaac           PetscInt aDof;
5145d3d1a6afSToby Isaac 
5146d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5147d3d1a6afSToby Isaac           newNumIndices += aDof;
5148d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5149d3d1a6afSToby Isaac             PetscInt fDof;
5150d3d1a6afSToby Isaac 
5151d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5152d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5153d3d1a6afSToby Isaac           }
5154d3d1a6afSToby Isaac         }
5155d3d1a6afSToby Isaac       }
5156d3d1a6afSToby Isaac       else {
5157d3d1a6afSToby Isaac         /* this point is not constrained */
5158d3d1a6afSToby Isaac         newNumPoints++;
51594b2f2278SToby Isaac         newNumIndices += bSecDof;
5160d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5161d3d1a6afSToby Isaac           PetscInt fDof;
5162d3d1a6afSToby Isaac 
5163d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5164d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5165d3d1a6afSToby Isaac         }
5166d3d1a6afSToby Isaac       }
5167d3d1a6afSToby Isaac     }
5168d3d1a6afSToby Isaac   }
5169d3d1a6afSToby Isaac   if (!anyConstrained) {
517072b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
517172b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
517272b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
517372b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
517472b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5175d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5176d3d1a6afSToby Isaac   }
5177d3d1a6afSToby Isaac 
51786ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
51796ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
51806ecaa68aSToby Isaac 
5181f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5182d3d1a6afSToby Isaac 
51836ecaa68aSToby Isaac   if (!outPoints && !outValues) {
51846ecaa68aSToby Isaac     if (offsets) {
51856ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
51866ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
51876ecaa68aSToby Isaac       }
51886ecaa68aSToby Isaac     }
51896ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
51906ecaa68aSToby Isaac     PetscFunctionReturn(0);
51916ecaa68aSToby Isaac   }
51926ecaa68aSToby Isaac 
5193f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5194d3d1a6afSToby Isaac 
5195f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5196d3d1a6afSToby Isaac 
5197d3d1a6afSToby Isaac   /* workspaces */
5198d3d1a6afSToby Isaac   if (numFields) {
5199d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
520069291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
520169291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5202d3d1a6afSToby Isaac     }
5203d3d1a6afSToby Isaac   }
5204d3d1a6afSToby Isaac   else {
520569291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
520669291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5207d3d1a6afSToby Isaac   }
5208d3d1a6afSToby Isaac 
5209d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5210d3d1a6afSToby Isaac   if (numFields) {
52114b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
52124b2f2278SToby Isaac 
5213d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5214d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
52154b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5216d3d1a6afSToby Isaac 
52174b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
52184b2f2278SToby Isaac       if (!bSecDof) {
52194b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
52204b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
52214b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
52224b2f2278SToby Isaac         }
52234b2f2278SToby Isaac         continue;
52244b2f2278SToby Isaac       }
5225d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5226d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5227d3d1a6afSToby Isaac       }
5228d3d1a6afSToby Isaac       if (bDof) {
5229d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5230d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5231d3d1a6afSToby Isaac 
5232d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5233d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5234d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5235d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5236d3d1a6afSToby Isaac             PetscInt aFDof;
5237d3d1a6afSToby Isaac 
5238d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5239d3d1a6afSToby Isaac             allFDof += aFDof;
5240d3d1a6afSToby Isaac           }
5241d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5242d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5243d3d1a6afSToby Isaac         }
5244d3d1a6afSToby Isaac       }
5245d3d1a6afSToby Isaac       else {
5246d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5247d3d1a6afSToby Isaac           PetscInt fDof;
5248d3d1a6afSToby Isaac 
5249d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5250d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5251d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5252d3d1a6afSToby Isaac         }
5253d3d1a6afSToby Isaac       }
5254d3d1a6afSToby Isaac     }
52554b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
52564b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
52574b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5258d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5259d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5260d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5261d3d1a6afSToby Isaac       }
526219f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
526319f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
526469291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5265d3d1a6afSToby Isaac     }
5266d3d1a6afSToby Isaac   }
5267d3d1a6afSToby Isaac   else {
5268d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5269d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
52704b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5271d3d1a6afSToby Isaac 
52724b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
52734b2f2278SToby Isaac       if (!bSecDof) {
52744b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
52754b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
52764b2f2278SToby Isaac         continue;
52774b2f2278SToby Isaac       }
5278d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5279d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5280d3d1a6afSToby Isaac       }
5281d3d1a6afSToby Isaac       if (bDof) {
52824b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5283d3d1a6afSToby Isaac 
5284d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5285d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5286d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5287d3d1a6afSToby Isaac 
5288d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5289d3d1a6afSToby Isaac           allDof += aDof;
5290d3d1a6afSToby Isaac         }
5291d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
52924b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5293d3d1a6afSToby Isaac       }
5294d3d1a6afSToby Isaac       else {
52954b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5296d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5297d3d1a6afSToby Isaac       }
5298d3d1a6afSToby Isaac     }
5299d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5300d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5301d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5302d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5303d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5304d3d1a6afSToby Isaac     }
530569291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5306d3d1a6afSToby Isaac   }
5307d3d1a6afSToby Isaac 
53086ecaa68aSToby Isaac   /* output arrays */
530969291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
53106ecaa68aSToby Isaac 
5311d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5312d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5313d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
531469291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
531569291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5316d3d1a6afSToby Isaac   if (numFields) {
5317d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5318d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5319d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
53204b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5321d3d1a6afSToby Isaac 
53224b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
53234b2f2278SToby Isaac       if (!bSecDof) {
53244b2f2278SToby Isaac         continue;
53254b2f2278SToby Isaac       }
5326d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5327d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5328d3d1a6afSToby Isaac       }
5329d3d1a6afSToby Isaac       if (bDof) {
5330d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5331d3d1a6afSToby Isaac 
5332d3d1a6afSToby Isaac         fStart[0] = 0;
5333d3d1a6afSToby Isaac         fEnd[0]   = 0;
5334d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5335d3d1a6afSToby Isaac           PetscInt fDof;
5336d3d1a6afSToby Isaac 
5337d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5338d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5339d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5340d3d1a6afSToby Isaac         }
5341d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
534205586334SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
5343d3d1a6afSToby Isaac 
5344d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5345d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5346d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5347d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5348d3d1a6afSToby Isaac 
5349d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5350d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5351d3d1a6afSToby Isaac         }
5352d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5353d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5354d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5355d3d1a6afSToby Isaac 
5356d3d1a6afSToby 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 */
5357d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5358d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5359302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
536005586334SMatthew G. Knepley           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
5361d3d1a6afSToby Isaac         }
5362d3d1a6afSToby Isaac         newP += bDof;
5363d3d1a6afSToby Isaac 
53646ecaa68aSToby Isaac         if (outValues) {
5365d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5366d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5367d3d1a6afSToby 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);
5368d3d1a6afSToby Isaac           }
5369d3d1a6afSToby Isaac         }
53706ecaa68aSToby Isaac       }
5371d3d1a6afSToby Isaac       else {
5372d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5373d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5374d3d1a6afSToby Isaac         newP++;
5375d3d1a6afSToby Isaac       }
5376d3d1a6afSToby Isaac     }
5377d3d1a6afSToby Isaac   } else {
5378d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5379d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5380d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
53814b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5382d3d1a6afSToby Isaac 
53834b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
53844b2f2278SToby Isaac       if (!bSecDof) {
53854b2f2278SToby Isaac         continue;
53864b2f2278SToby Isaac       }
5387d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5388d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5389d3d1a6afSToby Isaac       }
5390d3d1a6afSToby Isaac       if (bDof) {
5391d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5392d3d1a6afSToby Isaac 
5393d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
539405586334SMatthew G. Knepley         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
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], aOff;
5399d3d1a6afSToby Isaac 
5400d3d1a6afSToby 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 */
5401d3d1a6afSToby Isaac 
5402d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5403d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5404302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
540505586334SMatthew G. Knepley           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
5406d3d1a6afSToby Isaac         }
5407d3d1a6afSToby Isaac         newP += bDof;
5408d3d1a6afSToby Isaac 
5409d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
54106ecaa68aSToby Isaac         if (outValues) {
5411d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5412d3d1a6afSToby Isaac         }
54136ecaa68aSToby Isaac       }
5414d3d1a6afSToby Isaac       else {
5415d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5416d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5417d3d1a6afSToby Isaac         newP++;
5418d3d1a6afSToby Isaac       }
5419d3d1a6afSToby Isaac     }
5420d3d1a6afSToby Isaac   }
5421d3d1a6afSToby Isaac 
54226ecaa68aSToby Isaac   if (outValues) {
542369291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5424d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5425d3d1a6afSToby Isaac     /* multiply constraints on the right */
5426d3d1a6afSToby Isaac     if (numFields) {
5427d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5428d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5429d3d1a6afSToby Isaac 
5430d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5431d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5432d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5433d3d1a6afSToby Isaac           PetscInt c, r, k;
5434d3d1a6afSToby Isaac           PetscInt dof;
5435d3d1a6afSToby Isaac 
5436d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
54374b2f2278SToby Isaac           if (!dof) {
54384b2f2278SToby Isaac             continue;
54394b2f2278SToby Isaac           }
5440d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5441d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5442d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5443d3d1a6afSToby Isaac 
5444d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5445d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5446d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
54474acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5448d3d1a6afSToby Isaac                 }
5449d3d1a6afSToby Isaac               }
5450d3d1a6afSToby Isaac             }
5451d3d1a6afSToby Isaac           }
5452d3d1a6afSToby Isaac           else {
5453d3d1a6afSToby Isaac             /* copy this column as is */
5454d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5455d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5456d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5457d3d1a6afSToby Isaac               }
5458d3d1a6afSToby Isaac             }
5459d3d1a6afSToby Isaac           }
5460d3d1a6afSToby Isaac           oldOff += dof;
5461d3d1a6afSToby Isaac         }
5462d3d1a6afSToby Isaac       }
5463d3d1a6afSToby Isaac     }
5464d3d1a6afSToby Isaac     else {
5465d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5466d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5467d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5468d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5469d3d1a6afSToby Isaac         PetscInt c, r, k;
5470d3d1a6afSToby Isaac         PetscInt dof;
5471d3d1a6afSToby Isaac 
5472d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
54734b2f2278SToby Isaac         if (!dof) {
54744b2f2278SToby Isaac           continue;
54754b2f2278SToby Isaac         }
5476d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5477d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5478d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5479d3d1a6afSToby Isaac 
5480d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5481d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5482d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5483d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5484d3d1a6afSToby Isaac               }
5485d3d1a6afSToby Isaac             }
5486d3d1a6afSToby Isaac           }
5487d3d1a6afSToby Isaac         }
5488d3d1a6afSToby Isaac         else {
5489d3d1a6afSToby Isaac           /* copy this column as is */
5490d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5491d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5492d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5493d3d1a6afSToby Isaac             }
5494d3d1a6afSToby Isaac           }
5495d3d1a6afSToby Isaac         }
5496d3d1a6afSToby Isaac         oldOff += dof;
5497d3d1a6afSToby Isaac       }
5498d3d1a6afSToby Isaac     }
5499d3d1a6afSToby Isaac 
55006ecaa68aSToby Isaac     if (multiplyLeft) {
550169291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5502d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5503d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5504d3d1a6afSToby Isaac       if (numFields) {
5505d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5506d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5507d3d1a6afSToby Isaac 
5508d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5509d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5510d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5511d3d1a6afSToby Isaac             PetscInt c, r, k;
5512d3d1a6afSToby Isaac             PetscInt dof;
5513d3d1a6afSToby Isaac 
5514d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5515d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5516d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5517d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5518d3d1a6afSToby Isaac 
5519d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5520d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5521d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5522d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5523d3d1a6afSToby Isaac                   }
5524d3d1a6afSToby Isaac                 }
5525d3d1a6afSToby Isaac               }
5526d3d1a6afSToby Isaac             }
5527d3d1a6afSToby Isaac             else {
5528d3d1a6afSToby Isaac               /* copy this row as is */
5529d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5530d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5531d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5532d3d1a6afSToby Isaac                 }
5533d3d1a6afSToby Isaac               }
5534d3d1a6afSToby Isaac             }
5535d3d1a6afSToby Isaac             oldOff += dof;
5536d3d1a6afSToby Isaac           }
5537d3d1a6afSToby Isaac         }
5538d3d1a6afSToby Isaac       }
5539d3d1a6afSToby Isaac       else {
5540d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5541d3d1a6afSToby Isaac 
5542d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5543d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5544d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5545d3d1a6afSToby Isaac           PetscInt c, r, k;
5546d3d1a6afSToby Isaac           PetscInt dof;
5547d3d1a6afSToby Isaac 
5548d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5549d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5550d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5551d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5552d3d1a6afSToby Isaac 
5553d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5554d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5555d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5556d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5557d3d1a6afSToby Isaac                 }
5558d3d1a6afSToby Isaac               }
5559d3d1a6afSToby Isaac             }
5560d3d1a6afSToby Isaac           }
5561d3d1a6afSToby Isaac           else {
5562d3d1a6afSToby Isaac             /* copy this row as is */
55639fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5564d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5565d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5566d3d1a6afSToby Isaac               }
5567d3d1a6afSToby Isaac             }
5568d3d1a6afSToby Isaac           }
5569d3d1a6afSToby Isaac           oldOff += dof;
5570d3d1a6afSToby Isaac         }
5571d3d1a6afSToby Isaac       }
5572d3d1a6afSToby Isaac 
557369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
55746ecaa68aSToby Isaac     }
55756ecaa68aSToby Isaac     else {
55766ecaa68aSToby Isaac       newValues = tmpValues;
55776ecaa68aSToby Isaac     }
55786ecaa68aSToby Isaac   }
55796ecaa68aSToby Isaac 
5580d3d1a6afSToby Isaac   /* clean up */
558169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
558269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
55836ecaa68aSToby Isaac 
5584d3d1a6afSToby Isaac   if (numFields) {
5585d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
558669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
558769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
558869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5589d3d1a6afSToby Isaac     }
5590d3d1a6afSToby Isaac   }
5591d3d1a6afSToby Isaac   else {
559269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
559369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
559469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5595d3d1a6afSToby Isaac   }
5596d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5597d3d1a6afSToby Isaac 
5598d3d1a6afSToby Isaac   /* output */
55996ecaa68aSToby Isaac   if (outPoints) {
5600d3d1a6afSToby Isaac     *outPoints = newPoints;
56016ecaa68aSToby Isaac   }
56026ecaa68aSToby Isaac   else {
560369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
56046ecaa68aSToby Isaac   }
560531620726SToby Isaac   if (outValues) {
5606d3d1a6afSToby Isaac     *outValues = newValues;
56076ecaa68aSToby Isaac   }
56086ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5609d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5610d3d1a6afSToby Isaac   }
5611d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5612d3d1a6afSToby Isaac }
5613d3d1a6afSToby Isaac 
56147cd05799SMatthew G. Knepley /*@C
5615a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
56167cd05799SMatthew G. Knepley 
56177cd05799SMatthew G. Knepley   Not collective
56187cd05799SMatthew G. Knepley 
56197cd05799SMatthew G. Knepley   Input Parameters:
56207cd05799SMatthew G. Knepley + dm - The DM
56217cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
56227cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
56237cd05799SMatthew G. Knepley - point - The mesh point
56247cd05799SMatthew G. Knepley 
56257cd05799SMatthew G. Knepley   Output parameters:
56267cd05799SMatthew G. Knepley + numIndices - The number of indices
56277cd05799SMatthew G. Knepley . indices - The indices
56287cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
56297cd05799SMatthew G. Knepley 
56307cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
56317cd05799SMatthew G. Knepley 
56327cd05799SMatthew G. Knepley   Level: advanced
56337cd05799SMatthew G. Knepley 
56347cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
56357cd05799SMatthew G. Knepley @*/
56366ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
56377773e69fSMatthew G. Knepley {
56387773e69fSMatthew G. Knepley   PetscSection    clSection;
56397773e69fSMatthew G. Knepley   IS              clPoints;
564005586334SMatthew G. Knepley   const PetscInt *clp, *clperm;
56414acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
56427773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
56437773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
56447773e69fSMatthew G. Knepley   PetscInt        offsets[32];
56457773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
56467773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
56477773e69fSMatthew G. Knepley 
56487773e69fSMatthew G. Knepley   PetscFunctionBegin;
56497773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56507773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
56517773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
56527773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
56537773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
56547773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
56557773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
56567773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56577773e69fSMatthew G. Knepley   /* Get points in closure */
5658923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
565905586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
56607773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
56617773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
56627773e69fSMatthew G. Knepley     PetscInt dof, fdof;
56637773e69fSMatthew G. Knepley 
56647773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
56657773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
56667773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
56677773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
56687773e69fSMatthew G. Knepley     }
56697773e69fSMatthew G. Knepley     Nind += dof;
56707773e69fSMatthew G. Knepley   }
56717773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
56728ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
56734acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
56744acb8e1eSToby Isaac   /* Get dual space symmetries */
56754acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
56764acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
56774acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
56784acb8e1eSToby Isaac   }
56797773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
56807773e69fSMatthew G. Knepley   {
56814acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
56827773e69fSMatthew G. Knepley     if (numPointsNew) {
56834acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
56844acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
56854acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
56864acb8e1eSToby Isaac       }
56874acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
56884acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
56894acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
56904acb8e1eSToby Isaac       }
5691923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
56927773e69fSMatthew G. Knepley       numPoints = numPointsNew;
56937773e69fSMatthew G. Knepley       Nind      = NindNew;
56947773e69fSMatthew G. Knepley       points    = pointsNew;
56957773e69fSMatthew G. Knepley     }
56967773e69fSMatthew G. Knepley   }
56977773e69fSMatthew G. Knepley   /* Calculate indices */
569869291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
56997773e69fSMatthew G. Knepley   if (Nf) {
57006ecaa68aSToby Isaac     if (outOffsets) {
57016ecaa68aSToby Isaac       PetscInt f;
57026ecaa68aSToby Isaac 
570346bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
57046ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
57056ecaa68aSToby Isaac       }
57066ecaa68aSToby Isaac     }
57074acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
57084acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
570905586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, *indices);
57107773e69fSMatthew G. Knepley     }
57117773e69fSMatthew G. Knepley   } else {
57124acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
57134acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
57144acb8e1eSToby Isaac 
57154acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
571605586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, *indices);
57177773e69fSMatthew G. Knepley     }
57187773e69fSMatthew G. Knepley   }
57197773e69fSMatthew G. Knepley   /* Cleanup points */
57204acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
57214acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
57224acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
57234acb8e1eSToby Isaac   }
57247773e69fSMatthew G. Knepley   if (numPointsNew) {
572569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
57267773e69fSMatthew G. Knepley   } else {
5727923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
57287773e69fSMatthew G. Knepley   }
57297773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
57307773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
57317773e69fSMatthew G. Knepley }
57327773e69fSMatthew G. Knepley 
57337cd05799SMatthew G. Knepley /*@C
57347cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
57357cd05799SMatthew G. Knepley 
57367cd05799SMatthew G. Knepley   Not collective
57377cd05799SMatthew G. Knepley 
57387cd05799SMatthew G. Knepley   Input Parameters:
57397cd05799SMatthew G. Knepley + dm - The DM
57407cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
57417cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
57427cd05799SMatthew G. Knepley . point - The mesh point
57437cd05799SMatthew G. Knepley . numIndices - The number of indices
57447cd05799SMatthew G. Knepley . indices - The indices
57457cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
57467cd05799SMatthew G. Knepley 
57477cd05799SMatthew G. Knepley   Level: advanced
57487cd05799SMatthew G. Knepley 
57497cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
57507cd05799SMatthew G. Knepley @*/
575146bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
57527773e69fSMatthew G. Knepley {
57537773e69fSMatthew G. Knepley   PetscErrorCode ierr;
57547773e69fSMatthew G. Knepley 
57557773e69fSMatthew G. Knepley   PetscFunctionBegin;
57567773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57577773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
575869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
57597773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
57607773e69fSMatthew G. Knepley }
57617773e69fSMatthew G. Knepley 
57627f5d1fdeSMatthew G. Knepley /*@C
57637f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
57647f5d1fdeSMatthew G. Knepley 
57657f5d1fdeSMatthew G. Knepley   Not collective
57667f5d1fdeSMatthew G. Knepley 
57677f5d1fdeSMatthew G. Knepley   Input Parameters:
57687f5d1fdeSMatthew G. Knepley + dm - The DM
5769ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5770ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
57717f5d1fdeSMatthew G. Knepley . A - The matrix
5772eaf898f9SPatrick Sanan . point - The point in the DM
57737f5d1fdeSMatthew G. Knepley . values - The array of values
57747f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
57757f5d1fdeSMatthew G. Knepley 
57767f5d1fdeSMatthew G. Knepley   Fortran Notes:
57777f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
57787f5d1fdeSMatthew G. Knepley 
57797f5d1fdeSMatthew G. Knepley   Level: intermediate
57807f5d1fdeSMatthew G. Knepley 
57817f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
57827f5d1fdeSMatthew G. Knepley @*/
57837c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5784552f7358SJed Brown {
5785552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
57861b406b76SMatthew G. Knepley   PetscSection        clSection;
57871b406b76SMatthew G. Knepley   IS                  clPoints;
5788d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
578905586334SMatthew G. Knepley   const PetscInt     *clp, *clperm;
5790552f7358SJed Brown   PetscInt           *indices;
5791552f7358SJed Brown   PetscInt            offsets[32];
57924acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
57934acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5794923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
57954acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5796d3d1a6afSToby Isaac   PetscScalar        *newValues;
5797552f7358SJed Brown   PetscErrorCode      ierr;
5798552f7358SJed Brown 
5799552f7358SJed Brown   PetscFunctionBegin;
5800552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5801e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
58023dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5803e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
58043dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
58053dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5806552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
580782f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5808552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
580905586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
5810923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5811552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5812552f7358SJed Brown     PetscInt fdof;
5813552f7358SJed Brown 
5814552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5815552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5816552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5817552f7358SJed Brown       offsets[f+1] += fdof;
5818552f7358SJed Brown     }
5819552f7358SJed Brown     numIndices += dof;
5820552f7358SJed Brown   }
58210d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
58220d644c17SKarl Rupp 
58238ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
58244acb8e1eSToby Isaac   /* Get symmetries */
58254acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
58264acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
58274acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
58284acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
58294acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
58304acb8e1eSToby Isaac 
58314acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
58324acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
58334acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
58344acb8e1eSToby Isaac 
58354acb8e1eSToby Isaac         if (!numFields) {
58364acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
58374acb8e1eSToby Isaac         } else {
58384acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
58394acb8e1eSToby Isaac         }
58404acb8e1eSToby Isaac         if (flip) {
58414acb8e1eSToby Isaac           PetscInt i, j, k;
58424acb8e1eSToby Isaac 
58434acb8e1eSToby Isaac           if (!valCopy) {
584469291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
58454acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
58464acb8e1eSToby Isaac             values = valCopy;
58474acb8e1eSToby Isaac           }
58484acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5849775f7be2SToby Isaac             PetscScalar fval = flip[i];
58504acb8e1eSToby Isaac 
58514acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
58524acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
58534acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
58544acb8e1eSToby Isaac             }
58554acb8e1eSToby Isaac           }
58564acb8e1eSToby Isaac         }
58574acb8e1eSToby Isaac         foffset += fdof;
58584acb8e1eSToby Isaac       }
58594acb8e1eSToby Isaac     }
58604acb8e1eSToby Isaac   }
58614acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5862d3d1a6afSToby Isaac   if (newNumPoints) {
58634acb8e1eSToby Isaac     if (valCopy) {
586469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
58654acb8e1eSToby Isaac     }
58664acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
58674acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
58684acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
58694acb8e1eSToby Isaac     }
58704acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
58714acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
58724acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
58734acb8e1eSToby Isaac     }
5874923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5875d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5876d3d1a6afSToby Isaac     numIndices = newNumIndices;
5877d3d1a6afSToby Isaac     points     = newPoints;
5878d3d1a6afSToby Isaac     values     = newValues;
5879d3d1a6afSToby Isaac   }
588069291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5881552f7358SJed Brown   if (numFields) {
58827e29afd2SMatthew G. Knepley     PetscBool useFieldOffsets;
58837e29afd2SMatthew G. Knepley 
58847e29afd2SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(globalSection, &useFieldOffsets);CHKERRQ(ierr);
58857e29afd2SMatthew G. Knepley     if (useFieldOffsets) {
58867e29afd2SMatthew G. Knepley       for (p = 0; p < numPoints; p++) {
588705586334SMatthew G. Knepley         DMPlexGetIndicesPointFieldsSplit_Internal(section, globalSection, points[2*p], offsets, PETSC_FALSE, perms, p, clperm, indices);
58887e29afd2SMatthew G. Knepley       }
58897e29afd2SMatthew G. Knepley     } else {
58904acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
58914acb8e1eSToby Isaac         ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
589205586334SMatthew G. Knepley         DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, indices);
5893552f7358SJed Brown       }
58947e29afd2SMatthew G. Knepley     }
5895552f7358SJed Brown   } else {
58964acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
58974acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
58984acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
589905586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, indices);
5900552f7358SJed Brown     }
5901552f7358SJed Brown   }
5902b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5903552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5904ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5905ab1d0545SMatthew G. Knepley     PetscInt i;
5906ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
59078ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5908ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5909ab1d0545SMatthew G. Knepley   }
5910552f7358SJed Brown   if (ierr) {
5911552f7358SJed Brown     PetscMPIInt    rank;
5912552f7358SJed Brown     PetscErrorCode ierr2;
5913552f7358SJed Brown 
591482f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5915e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5916b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
591769291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
5918552f7358SJed Brown     CHKERRQ(ierr);
5919552f7358SJed Brown   }
59204acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
59214acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
59224acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
59234acb8e1eSToby Isaac   }
5924d3d1a6afSToby Isaac   if (newNumPoints) {
592569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
592669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
5927d3d1a6afSToby Isaac   }
5928d3d1a6afSToby Isaac   else {
59294acb8e1eSToby Isaac     if (valCopy) {
593069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
59314acb8e1eSToby Isaac     }
5932923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5933d3d1a6afSToby Isaac   }
593469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5935552f7358SJed Brown   PetscFunctionReturn(0);
5936552f7358SJed Brown }
5937552f7358SJed Brown 
5938de41b84cSMatthew 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)
5939de41b84cSMatthew G. Knepley {
5940de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5941de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5942de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5943de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
594405586334SMatthew G. Knepley   const PetscInt *fclperm, *cclperm;
5945de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5946de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
59474ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5948de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5949de41b84cSMatthew G. Knepley 
5950de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5951de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5952de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5953e87a4003SBarry Smith   if (!fsection) {ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);}
5954de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5955e87a4003SBarry Smith   if (!csection) {ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);}
5956de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5957e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5958de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5959e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5960de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5961de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5962de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5963de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5964de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5965de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
596605586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(fsection, (PetscObject) dmf, NULL, &fclperm);CHKERRQ(ierr);
596705586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(csection, (PetscObject) dmc, NULL, &cclperm);CHKERRQ(ierr);
5968de41b84cSMatthew G. Knepley   /* Column indices */
5969de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
59704ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5971de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5972de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5973de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5974de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5975de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5976de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5977de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5978de41b84cSMatthew G. Knepley       ++q;
5979de41b84cSMatthew G. Knepley     }
5980de41b84cSMatthew G. Knepley   }
5981de41b84cSMatthew G. Knepley   numCPoints = q;
5982de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5983de41b84cSMatthew G. Knepley     PetscInt fdof;
5984de41b84cSMatthew G. Knepley 
5985de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
59864ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5987de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5988de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5989de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5990de41b84cSMatthew G. Knepley     }
5991de41b84cSMatthew G. Knepley     numCIndices += dof;
5992de41b84cSMatthew G. Knepley   }
5993de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5994de41b84cSMatthew G. Knepley   /* Row indices */
5995de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5996de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
599769291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5998de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5999de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
6000de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6001de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
6002de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
6003de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
6004de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
60054ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
60064ca5e9f5SMatthew G. Knepley         if (!dof) continue;
60074ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
60084ca5e9f5SMatthew G. Knepley         if (s < q) continue;
6009de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
6010de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
6011de41b84cSMatthew G. Knepley         ++q;
6012de41b84cSMatthew G. Knepley       }
6013de41b84cSMatthew G. Knepley     }
6014de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6015de41b84cSMatthew G. Knepley   }
6016de41b84cSMatthew G. Knepley   numFPoints = q;
6017de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6018de41b84cSMatthew G. Knepley     PetscInt fdof;
6019de41b84cSMatthew G. Knepley 
6020de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
60214ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6022de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6023de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
6024de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
6025de41b84cSMatthew G. Knepley     }
6026de41b84cSMatthew G. Knepley     numFIndices += dof;
6027de41b84cSMatthew G. Knepley   }
6028de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
6029de41b84cSMatthew G. Knepley 
60308ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
60318ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
603269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
603369291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6034de41b84cSMatthew G. Knepley   if (numFields) {
60354acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
60364acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
60374acb8e1eSToby Isaac 
60384acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
60394acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
60404acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6041de41b84cSMatthew G. Knepley     }
60424acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
60434acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
604405586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
60454acb8e1eSToby Isaac     }
60464acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
60474acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
604805586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
60494acb8e1eSToby Isaac     }
60504acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
60514acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
60524acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6053de41b84cSMatthew G. Knepley     }
6054de41b84cSMatthew G. Knepley   } else {
60554acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
60564acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
60574acb8e1eSToby Isaac 
60584acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
60594acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
60604acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
60614acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
60624acb8e1eSToby Isaac 
60634acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
606405586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
6065de41b84cSMatthew G. Knepley     }
60664acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
60674acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
60684acb8e1eSToby Isaac 
60694acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
607005586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
6071de41b84cSMatthew G. Knepley     }
60724acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
60734acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
6074de41b84cSMatthew G. Knepley   }
6075de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
60764acb8e1eSToby Isaac   /* TODO: flips */
6077de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6078de41b84cSMatthew G. Knepley   if (ierr) {
6079de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
6080de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
6081de41b84cSMatthew G. Knepley 
6082de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6083e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6084de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
608569291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
608669291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6087de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
6088de41b84cSMatthew G. Knepley   }
608969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6090de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
609169291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
609269291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6093de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
6094de41b84cSMatthew G. Knepley }
6095de41b84cSMatthew G. Knepley 
60967c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
60977c927364SMatthew G. Knepley {
60987c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
60997c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
61007c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
610105586334SMatthew G. Knepley   const PetscInt *fclperm, *cclperm;
61027c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
61037c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
61047c927364SMatthew G. Knepley   PetscErrorCode ierr;
61057c927364SMatthew G. Knepley 
61067c927364SMatthew G. Knepley   PetscFunctionBegin;
61077c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
61087c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
6109e87a4003SBarry Smith   if (!fsection) {ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);}
61107c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
6111e87a4003SBarry Smith   if (!csection) {ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);}
61127c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6113e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
61147c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6115e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
61167c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
61177c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
61187c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
61197c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
61207c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
612105586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(fsection, (PetscObject) dmf, NULL, &fclperm);CHKERRQ(ierr);
612205586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(csection, (PetscObject) dmc, NULL, &cclperm);CHKERRQ(ierr);
61237c927364SMatthew G. Knepley   /* Column indices */
61247c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
61257c927364SMatthew G. Knepley   maxFPoints = numCPoints;
61267c927364SMatthew G. Knepley   /* Compress out points not in the section */
61277c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
61287c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
61297c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
61307c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
61317c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
61327c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
61337c927364SMatthew G. Knepley       ++q;
61347c927364SMatthew G. Knepley     }
61357c927364SMatthew G. Knepley   }
61367c927364SMatthew G. Knepley   numCPoints = q;
61377c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
61387c927364SMatthew G. Knepley     PetscInt fdof;
61397c927364SMatthew G. Knepley 
61407c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
61417c927364SMatthew G. Knepley     if (!dof) continue;
61427c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
61437c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
61447c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
61457c927364SMatthew G. Knepley     }
61467c927364SMatthew G. Knepley     numCIndices += dof;
61477c927364SMatthew G. Knepley   }
61487c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
61497c927364SMatthew G. Knepley   /* Row indices */
61507c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
61517c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
615269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
61537c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
61547c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
61557c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
61567c927364SMatthew G. Knepley     /* Compress out points not in the section */
61577c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
61587c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
61597c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
61607c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
61617c927364SMatthew G. Knepley         if (!dof) continue;
61627c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
61637c927364SMatthew G. Knepley         if (s < q) continue;
61647c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
61657c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
61667c927364SMatthew G. Knepley         ++q;
61677c927364SMatthew G. Knepley       }
61687c927364SMatthew G. Knepley     }
61697c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
61707c927364SMatthew G. Knepley   }
61717c927364SMatthew G. Knepley   numFPoints = q;
61727c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
61737c927364SMatthew G. Knepley     PetscInt fdof;
61747c927364SMatthew G. Knepley 
61757c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
61767c927364SMatthew G. Knepley     if (!dof) continue;
61777c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
61787c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
61797c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
61807c927364SMatthew G. Knepley     }
61817c927364SMatthew G. Knepley     numFIndices += dof;
61827c927364SMatthew G. Knepley   }
61837c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
61847c927364SMatthew G. Knepley 
61858ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
61868ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
61877c927364SMatthew G. Knepley   if (numFields) {
61884acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
61894acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
61904acb8e1eSToby Isaac 
61914acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
61924acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
61934acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
61947c927364SMatthew G. Knepley     }
61954acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
61964acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
619705586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
61984acb8e1eSToby Isaac     }
61994acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
62004acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
620105586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
62024acb8e1eSToby Isaac     }
62034acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
62044acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
62054acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
62067c927364SMatthew G. Knepley     }
62077c927364SMatthew G. Knepley   } else {
62084acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
62094acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
62104acb8e1eSToby Isaac 
62114acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
62124acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
62134acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
62144acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
62154acb8e1eSToby Isaac 
62164acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
621705586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
62187c927364SMatthew G. Knepley     }
62194acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
62204acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
62214acb8e1eSToby Isaac 
62224acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
622305586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
62247c927364SMatthew G. Knepley     }
62254acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
62264acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
62277c927364SMatthew G. Knepley   }
622869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
62297c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
62307c927364SMatthew G. Knepley   PetscFunctionReturn(0);
62317c927364SMatthew G. Knepley }
62327c927364SMatthew G. Knepley 
6233f96281f8SMatthew G. Knepley /*@
6234f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6235f96281f8SMatthew G. Knepley 
6236f96281f8SMatthew G. Knepley   Input Parameter:
6237f96281f8SMatthew G. Knepley . dm - The DMPlex object
6238f96281f8SMatthew G. Knepley 
6239f96281f8SMatthew G. Knepley   Output Parameters:
6240f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6241dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6242dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6243dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6244f96281f8SMatthew G. Knepley 
6245f96281f8SMatthew G. Knepley   Level: developer
6246f96281f8SMatthew G. Knepley 
6247dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6248f96281f8SMatthew G. Knepley @*/
6249770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6250552f7358SJed Brown {
6251552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6252770b213bSMatthew G Knepley   PetscInt       dim;
6253770b213bSMatthew G Knepley   PetscErrorCode ierr;
6254552f7358SJed Brown 
6255552f7358SJed Brown   PetscFunctionBegin;
6256552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6257c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
62587d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6259770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
62607d5acc75SStefano Zampini   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6261770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6262770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6263552f7358SJed Brown   PetscFunctionReturn(0);
6264552f7358SJed Brown }
6265552f7358SJed Brown 
6266aeadca18SToby Isaac static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
62674a3e9fdbSToby Isaac {
62684a3e9fdbSToby Isaac   IS             is, his;
62697d5acc75SStefano Zampini   PetscInt       first = 0, stride;
62704a3e9fdbSToby Isaac   PetscBool      isStride;
62714a3e9fdbSToby Isaac   PetscErrorCode ierr;
62724a3e9fdbSToby Isaac 
62734a3e9fdbSToby Isaac   PetscFunctionBegin;
62744a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel, d, &is);CHKERRQ(ierr);
62754a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);CHKERRQ(ierr);
62764a3e9fdbSToby Isaac   if (isStride) {
62774a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(is, &first, &stride);CHKERRQ(ierr);
62784a3e9fdbSToby Isaac   }
62797d5acc75SStefano 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);
62804a3e9fdbSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);CHKERRQ(ierr);
6281aeadca18SToby Isaac   ierr = DMLabelSetStratumIS(dimLabel, d, his);CHKERRQ(ierr);
62824a3e9fdbSToby Isaac   ierr = ISDestroy(&his);CHKERRQ(ierr);
62834a3e9fdbSToby Isaac   ierr = ISDestroy(&is);CHKERRQ(ierr);
62844a3e9fdbSToby Isaac   PetscFunctionReturn(0);
62854a3e9fdbSToby Isaac }
62864a3e9fdbSToby Isaac 
6287dc5a3409SMatthew G. Knepley /*@
6288dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6289dc5a3409SMatthew G. Knepley 
6290dc5a3409SMatthew G. Knepley   Input Parameters:
6291dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
6292dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6293dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6294dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6295dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6296dc5a3409SMatthew G. Knepley 
6297dc5a3409SMatthew G. Knepley   Level: developer
6298dc5a3409SMatthew G. Knepley 
6299dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6300dc5a3409SMatthew G. Knepley @*/
6301770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6302552f7358SJed Brown {
6303552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6304770b213bSMatthew G Knepley   PetscInt       dim;
6305770b213bSMatthew G Knepley   PetscErrorCode ierr;
6306552f7358SJed Brown 
6307552f7358SJed Brown   PetscFunctionBegin;
6308552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6309c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
63107d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6311770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
63127d5acc75SStefano Zampini   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6313770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6314770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6315552f7358SJed Brown   PetscFunctionReturn(0);
6316552f7358SJed Brown }
6317552f7358SJed Brown 
63187cd05799SMatthew G. Knepley /*@C
63197cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
63207cd05799SMatthew G. Knepley 
63217cd05799SMatthew G. Knepley   Input Parameter:
63227cd05799SMatthew G. Knepley . dm   - The DMPlex object
63237cd05799SMatthew G. Knepley 
63247cd05799SMatthew G. Knepley   Output Parameter:
63257cd05799SMatthew G. Knepley . cellHeight - The height of a cell
63267cd05799SMatthew G. Knepley 
63277cd05799SMatthew G. Knepley   Level: developer
63287cd05799SMatthew G. Knepley 
63297cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
63307cd05799SMatthew G. Knepley @*/
6331552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6332552f7358SJed Brown {
6333552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6334552f7358SJed Brown 
6335552f7358SJed Brown   PetscFunctionBegin;
6336552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6337552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6338552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6339552f7358SJed Brown   PetscFunctionReturn(0);
6340552f7358SJed Brown }
6341552f7358SJed Brown 
63427cd05799SMatthew G. Knepley /*@C
63437cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
63447cd05799SMatthew G. Knepley 
63457cd05799SMatthew G. Knepley   Input Parameters:
63467cd05799SMatthew G. Knepley + dm   - The DMPlex object
63477cd05799SMatthew G. Knepley - cellHeight - The height of a cell
63487cd05799SMatthew G. Knepley 
63497cd05799SMatthew G. Knepley   Level: developer
63507cd05799SMatthew G. Knepley 
63517cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
63527cd05799SMatthew G. Knepley @*/
6353552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6354552f7358SJed Brown {
6355552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6356552f7358SJed Brown 
6357552f7358SJed Brown   PetscFunctionBegin;
6358552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6359552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6360552f7358SJed Brown   PetscFunctionReturn(0);
6361552f7358SJed Brown }
6362552f7358SJed Brown 
6363552f7358SJed Brown /* We can easily have a form that takes an IS instead */
636450ad7711SMatthew G. Knepley PetscErrorCode DMPlexCreateNumbering_Internal(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6365552f7358SJed Brown {
6366552f7358SJed Brown   PetscSection   section, globalSection;
6367552f7358SJed Brown   PetscInt      *numbers, p;
6368552f7358SJed Brown   PetscErrorCode ierr;
6369552f7358SJed Brown 
6370552f7358SJed Brown   PetscFunctionBegin;
637182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6372552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6373552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6374552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6375552f7358SJed Brown   }
6376552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
637715b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6378854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6379552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6380552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6381ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6382ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6383552f7358SJed Brown   }
638482f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6385ef48cebcSMatthew G. Knepley   if (globalSize) {
6386ef48cebcSMatthew G. Knepley     PetscLayout layout;
6387ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6388ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6389ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6390ef48cebcSMatthew G. Knepley   }
6391552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6392552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6393552f7358SJed Brown   PetscFunctionReturn(0);
6394552f7358SJed Brown }
6395552f7358SJed Brown 
639681ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6397552f7358SJed Brown {
6398552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6399552f7358SJed Brown   PetscErrorCode ierr;
6400552f7358SJed Brown 
6401552f7358SJed Brown   PetscFunctionBegin;
6402552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6403552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
64040298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
640581ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
640650ad7711SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Internal(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
640781ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6408552f7358SJed Brown }
640981ed3555SMatthew G. Knepley 
64108dab3259SMatthew G. Knepley /*@
64117cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
64127cd05799SMatthew G. Knepley 
64137cd05799SMatthew G. Knepley   Input Parameter:
64147cd05799SMatthew G. Knepley . dm   - The DMPlex object
64157cd05799SMatthew G. Knepley 
64167cd05799SMatthew G. Knepley   Output Parameter:
64177cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
64187cd05799SMatthew G. Knepley 
64197cd05799SMatthew G. Knepley   Level: developer
64207cd05799SMatthew G. Knepley 
64217cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
64227cd05799SMatthew G. Knepley @*/
642381ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
642481ed3555SMatthew G. Knepley {
642581ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
642681ed3555SMatthew G. Knepley   PetscErrorCode ierr;
642781ed3555SMatthew G. Knepley 
642881ed3555SMatthew G. Knepley   PetscFunctionBegin;
642981ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
643081ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6431552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6432552f7358SJed Brown   PetscFunctionReturn(0);
6433552f7358SJed Brown }
6434552f7358SJed Brown 
643581ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
643681ed3555SMatthew G. Knepley {
643781ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
643881ed3555SMatthew G. Knepley   PetscErrorCode ierr;
643981ed3555SMatthew G. Knepley 
644081ed3555SMatthew G. Knepley   PetscFunctionBegin;
644181ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
644281ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
644381ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
644481ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
644550ad7711SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Internal(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
644681ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
644781ed3555SMatthew G. Knepley }
644881ed3555SMatthew G. Knepley 
64498dab3259SMatthew G. Knepley /*@
64507cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
64517cd05799SMatthew G. Knepley 
64527cd05799SMatthew G. Knepley   Input Parameter:
64537cd05799SMatthew G. Knepley . dm   - The DMPlex object
64547cd05799SMatthew G. Knepley 
64557cd05799SMatthew G. Knepley   Output Parameter:
64567cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
64577cd05799SMatthew G. Knepley 
64587cd05799SMatthew G. Knepley   Level: developer
64597cd05799SMatthew G. Knepley 
64607cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
64617cd05799SMatthew G. Knepley @*/
6462552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6463552f7358SJed Brown {
6464552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6465552f7358SJed Brown   PetscErrorCode ierr;
6466552f7358SJed Brown 
6467552f7358SJed Brown   PetscFunctionBegin;
6468552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
646981ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6470552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6471552f7358SJed Brown   PetscFunctionReturn(0);
6472552f7358SJed Brown }
6473552f7358SJed Brown 
64748dab3259SMatthew G. Knepley /*@
64757cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
64767cd05799SMatthew G. Knepley 
64777cd05799SMatthew G. Knepley   Input Parameter:
64787cd05799SMatthew G. Knepley . dm   - The DMPlex object
64797cd05799SMatthew G. Knepley 
64807cd05799SMatthew G. Knepley   Output Parameter:
64817cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
64827cd05799SMatthew G. Knepley 
64837cd05799SMatthew G. Knepley   Level: developer
64847cd05799SMatthew G. Knepley 
64857cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
64867cd05799SMatthew G. Knepley @*/
6487ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6488ef48cebcSMatthew G. Knepley {
6489ef48cebcSMatthew G. Knepley   IS             nums[4];
6490862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
6491ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6492ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6493ef48cebcSMatthew G. Knepley 
6494ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6495ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6496ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
64978abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
64988abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6499862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6500862913ffSStefano Zampini     PetscInt end;
6501862913ffSStefano Zampini 
6502862913ffSStefano Zampini     depths[d] = depth-d;
6503862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
6504862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
6505862913ffSStefano Zampini   }
6506862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
6507862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
6508862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6509862913ffSStefano 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]);
6510862913ffSStefano Zampini   }
6511ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6512ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6513ef48cebcSMatthew G. Knepley 
6514862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
651550ad7711SMatthew G. Knepley     ierr = DMPlexCreateNumbering_Internal(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6516ef48cebcSMatthew G. Knepley     shift += gsize;
6517ef48cebcSMatthew G. Knepley   }
6518302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6519ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6520ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6521ef48cebcSMatthew G. Knepley }
6522ef48cebcSMatthew G. Knepley 
652308a22f4bSMatthew G. Knepley 
652408a22f4bSMatthew G. Knepley /*@
652508a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
652608a22f4bSMatthew G. Knepley 
652708a22f4bSMatthew G. Knepley   Input Parameter:
652808a22f4bSMatthew G. Knepley . dm - The DMPlex object
652908a22f4bSMatthew G. Knepley 
653008a22f4bSMatthew G. Knepley   Output Parameter:
653108a22f4bSMatthew G. Knepley . ranks - The rank field
653208a22f4bSMatthew G. Knepley 
653308a22f4bSMatthew G. Knepley   Options Database Keys:
653408a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
653508a22f4bSMatthew G. Knepley 
653608a22f4bSMatthew G. Knepley   Level: intermediate
653708a22f4bSMatthew G. Knepley 
653808a22f4bSMatthew G. Knepley .seealso: DMView()
653908a22f4bSMatthew G. Knepley @*/
654008a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
654108a22f4bSMatthew G. Knepley {
654208a22f4bSMatthew G. Knepley   DM             rdm;
654308a22f4bSMatthew G. Knepley   PetscFE        fe;
654408a22f4bSMatthew G. Knepley   PetscScalar   *r;
654508a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
654608a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
654708a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
654808a22f4bSMatthew G. Knepley 
654908a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
6550f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6551f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
655208a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
655308a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
655408a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
6555f95ace6aSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
655608a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
6557e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
655808a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
6559e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
656008a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
656108a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
656208a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
656308a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
656408a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
656508a22f4bSMatthew G. Knepley     PetscScalar *lr;
656608a22f4bSMatthew G. Knepley 
656708a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
656808a22f4bSMatthew G. Knepley     *lr = rank;
656908a22f4bSMatthew G. Knepley   }
657008a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
657108a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
657208a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
657308a22f4bSMatthew G. Knepley }
657408a22f4bSMatthew G. Knepley 
6575ca8062c8SMatthew G. Knepley /*@
657618e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
657718e14f0cSMatthew G. Knepley 
657818e14f0cSMatthew G. Knepley   Input Parameters:
657918e14f0cSMatthew G. Knepley + dm    - The DMPlex
658018e14f0cSMatthew G. Knepley - label - The DMLabel
658118e14f0cSMatthew G. Knepley 
658218e14f0cSMatthew G. Knepley   Output Parameter:
658318e14f0cSMatthew G. Knepley . val - The label value field
658418e14f0cSMatthew G. Knepley 
658518e14f0cSMatthew G. Knepley   Options Database Keys:
658618e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
658718e14f0cSMatthew G. Knepley 
658818e14f0cSMatthew G. Knepley   Level: intermediate
658918e14f0cSMatthew G. Knepley 
659018e14f0cSMatthew G. Knepley .seealso: DMView()
659118e14f0cSMatthew G. Knepley @*/
659218e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
659318e14f0cSMatthew G. Knepley {
659418e14f0cSMatthew G. Knepley   DM             rdm;
659518e14f0cSMatthew G. Knepley   PetscFE        fe;
659618e14f0cSMatthew G. Knepley   PetscScalar   *v;
659718e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
659818e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
659918e14f0cSMatthew G. Knepley 
660018e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
660118e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
660218e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
660318e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
660418e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
660518e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
660618e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
660718e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
6608e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
660918e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
6610e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
661118e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
661218e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
6613effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
661418e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
661518e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
661618e14f0cSMatthew G. Knepley     PetscScalar *lv;
661718e14f0cSMatthew G. Knepley     PetscInt     cval;
661818e14f0cSMatthew G. Knepley 
661918e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
662018e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
662118e14f0cSMatthew G. Knepley     *lv = cval;
662218e14f0cSMatthew G. Knepley   }
662318e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
662418e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
662518e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
662618e14f0cSMatthew G. Knepley }
662718e14f0cSMatthew G. Knepley 
662818e14f0cSMatthew G. Knepley /*@
6629ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6630ca8062c8SMatthew G. Knepley 
663169916449SMatthew G. Knepley   Input Parameter:
663269916449SMatthew G. Knepley . dm - The DMPlex object
6633ca8062c8SMatthew G. Knepley 
6634ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6635ca8062c8SMatthew G. Knepley 
6636ca8062c8SMatthew G. Knepley   Level: developer
6637ca8062c8SMatthew G. Knepley 
66387d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6639ca8062c8SMatthew G. Knepley @*/
6640ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6641ca8062c8SMatthew G. Knepley {
6642ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6643ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6644ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
664557beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
664657beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
6647ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6648ca8062c8SMatthew G. Knepley 
6649ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6650ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6651ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6652ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6653ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6654ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6655ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6656ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6657ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6658ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
665942e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
666042e66dfaSMatthew G. Knepley       PetscInt  d;
666142e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
666242e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
666342e66dfaSMatthew G. Knepley       }
6664ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6665ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6666ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6667ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6668ca8062c8SMatthew G. Knepley       }
666942e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
66708ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6671ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
66728ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6673ca8062c8SMatthew G. Knepley         }
6674302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
66758ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6676ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
66778ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6678ca8062c8SMatthew G. Knepley         }
6679302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
66808ccfff9cSToby 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]);
66818ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6682ca8062c8SMatthew G. Knepley       }
668342e66dfaSMatthew G. Knepley     }
668457beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
668557beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
6686ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6687ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6688ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6689ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6690ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6691ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
669257beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
669357beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
6694ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6695ca8062c8SMatthew G. Knepley       }
6696ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
66978ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6698ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
66998ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6700ca8062c8SMatthew G. Knepley         }
6701302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
67028ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6703ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
67048ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6705ca8062c8SMatthew G. Knepley         }
6706302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
67078ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6708ca8062c8SMatthew G. Knepley       }
6709ca8062c8SMatthew G. Knepley     }
6710ca8062c8SMatthew G. Knepley   }
671157beb4faSStefano Zampini   if (storagecheck) {
6712ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6713ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
67148ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
671557beb4faSStefano Zampini   }
6716ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6717ca8062c8SMatthew G. Knepley }
6718ca8062c8SMatthew G. Knepley 
6719ca8062c8SMatthew G. Knepley /*@
6720ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6721ca8062c8SMatthew G. Knepley 
6722ca8062c8SMatthew G. Knepley   Input Parameters:
6723ca8062c8SMatthew G. Knepley + dm - The DMPlex object
672458723a97SMatthew G. Knepley - cellHeight - Normally 0
6725ca8062c8SMatthew G. Knepley 
6726ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
672725c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
6728ca8062c8SMatthew G. Knepley 
6729ca8062c8SMatthew G. Knepley   Level: developer
6730ca8062c8SMatthew G. Knepley 
67317d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckFaces()
6732ca8062c8SMatthew G. Knepley @*/
673325c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
6734ca8062c8SMatthew G. Knepley {
673542363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
673625c50c26SVaclav Hapla   PetscBool      isSimplex = PETSC_FALSE;
6737ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6738ca8062c8SMatthew G. Knepley 
6739ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6740ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6741c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
674225c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
674325c50c26SVaclav Hapla   if (cStart < cEnd) {
674425c50c26SVaclav Hapla     ierr = DMPlexGetConeSize(dm, cStart, &c);CHKERRQ(ierr);
674525c50c26SVaclav Hapla     isSimplex = c == dim+1 ? PETSC_TRUE : PETSC_FALSE;
674625c50c26SVaclav Hapla   }
6747ca8062c8SMatthew G. Knepley   switch (dim) {
674842363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
674942363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
675042363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6751ca8062c8SMatthew G. Knepley   default:
67528ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6753ca8062c8SMatthew G. Knepley   }
675458723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
6755ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6756ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6757ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
675858723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
675958723a97SMatthew G. Knepley 
676058723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
676158723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
676258723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
676358723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
676458723a97SMatthew G. Knepley     }
676558723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
67668ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6767ca8062c8SMatthew G. Knepley   }
676842363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
676942363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
677042363296SMatthew G. Knepley 
677142363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
677242363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
677342363296SMatthew G. Knepley       const PetscInt p = closure[cl];
677442363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
677542363296SMatthew G. Knepley     }
677642363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
67778ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
677842363296SMatthew G. Knepley   }
6779ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6780ca8062c8SMatthew G. Knepley }
67819bf0dad6SMatthew G. Knepley 
67829bf0dad6SMatthew G. Knepley /*@
67839bf0dad6SMatthew 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
67849bf0dad6SMatthew G. Knepley 
67859bf0dad6SMatthew G. Knepley   Input Parameters:
67869bf0dad6SMatthew G. Knepley + dm - The DMPlex object
67879bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
67889bf0dad6SMatthew G. Knepley 
67899bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
67909bf0dad6SMatthew G. Knepley 
67919bf0dad6SMatthew G. Knepley   Level: developer
67929bf0dad6SMatthew G. Knepley 
67937d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton()
67949bf0dad6SMatthew G. Knepley @*/
679525c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
67969bf0dad6SMatthew G. Knepley {
67973554e41dSMatthew G. Knepley   PetscInt       pMax[4];
6798ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
67999bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
68009bf0dad6SMatthew G. Knepley 
68019bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
68029bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6803c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6804ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
68059bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
680625abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
6807ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
68083554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
68093554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
68109bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
68119bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
68129bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
68139bf0dad6SMatthew G. Knepley 
681425abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
68159bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
68169bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
68179bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
68189bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
68199bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
68209bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
68219bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
68229bf0dad6SMatthew G. Knepley       }
68233554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
68248ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
68259bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
68269bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
68279bf0dad6SMatthew G. Knepley 
68289bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
68299bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
68309bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
68319bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
68329bf0dad6SMatthew G. Knepley         }
68338ccfff9cSToby 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);
68349bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
68358ccfff9cSToby 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]);
68369bf0dad6SMatthew G. Knepley         }
68379bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
68389bf0dad6SMatthew G. Knepley       }
68399bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
68409bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
68419bf0dad6SMatthew G. Knepley     }
68423554e41dSMatthew G. Knepley   }
6843552f7358SJed Brown   PetscFunctionReturn(0);
6844552f7358SJed Brown }
68453913d7c8SMatthew G. Knepley 
6846bb6a34a8SMatthew G. Knepley /*@
6847bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
6848bb6a34a8SMatthew G. Knepley 
6849bb6a34a8SMatthew G. Knepley   Input Parameter:
6850bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
6851bb6a34a8SMatthew G. Knepley 
6852bb6a34a8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6853bb6a34a8SMatthew G. Knepley 
6854bb6a34a8SMatthew G. Knepley   Level: developer
6855bb6a34a8SMatthew G. Knepley 
6856bb6a34a8SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton(), DMCheckFaces()
6857bb6a34a8SMatthew G. Knepley @*/
6858bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
6859bb6a34a8SMatthew G. Knepley {
6860bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
6861bb6a34a8SMatthew G. Knepley   PetscReal      vol;
6862bb6a34a8SMatthew G. Knepley   PetscInt       dim, depth, d, cStart, cEnd, c;
6863bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
6864bb6a34a8SMatthew G. Knepley 
6865bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
6866bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6867bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6868bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
6869bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6870bb6a34a8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
6871bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
6872bb6a34a8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted, |J| = %g", c, (double) detJ);
6873bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
6874bb6a34a8SMatthew G. Knepley     if (depth > 1) {
6875bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
6876bb6a34a8SMatthew G. Knepley       if (vol <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %d is inverted, vol = %g", c, (double) vol);
6877bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
6878bb6a34a8SMatthew G. Knepley     }
6879bb6a34a8SMatthew G. Knepley   }
6880bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
6881bb6a34a8SMatthew G. Knepley }
6882bb6a34a8SMatthew G. Knepley 
688303da9461SVaclav Hapla static PetscErrorCode DMPlexAreAllConePointsInArray_Private(DM dm, PetscInt p, PetscInt npoints, const PetscInt *points, PetscInt *missingPoint)
688403da9461SVaclav Hapla {
688503da9461SVaclav Hapla   PetscInt i,l,n;
688603da9461SVaclav Hapla   const PetscInt *cone;
688703da9461SVaclav Hapla   PetscErrorCode ierr;
688803da9461SVaclav Hapla 
688903da9461SVaclav Hapla   PetscFunctionBegin;
689003da9461SVaclav Hapla   *missingPoint = -1;
689103da9461SVaclav Hapla   ierr = DMPlexGetConeSize(dm, p, &n);CHKERRQ(ierr);
689203da9461SVaclav Hapla   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
689303da9461SVaclav Hapla   for (i=0; i<n; i++) {
689403da9461SVaclav Hapla     ierr = PetscFindInt(cone[i], npoints, points, &l);CHKERRQ(ierr);
689503da9461SVaclav Hapla     if (l < 0) {
689603da9461SVaclav Hapla       *missingPoint = cone[i];
689703da9461SVaclav Hapla       break;
689803da9461SVaclav Hapla     }
689903da9461SVaclav Hapla   }
690003da9461SVaclav Hapla   PetscFunctionReturn(0);
690103da9461SVaclav Hapla }
690203da9461SVaclav Hapla 
690303da9461SVaclav Hapla /*@
690403da9461SVaclav Hapla   DMPlexCheckPointSF - Check that several sufficient conditions are met for the point SF of this plex.
690503da9461SVaclav Hapla 
690603da9461SVaclav Hapla   Input Parameters:
690703da9461SVaclav Hapla . dm - The DMPlex object
690803da9461SVaclav Hapla 
690903da9461SVaclav Hapla   Note: This is mainly intended for debugging/testing purposes.
691003da9461SVaclav Hapla 
691103da9461SVaclav Hapla   Level: developer
691203da9461SVaclav Hapla 
691303da9461SVaclav Hapla .seealso: DMGetPointSF(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
691403da9461SVaclav Hapla @*/
691503da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
691603da9461SVaclav Hapla {
691703da9461SVaclav Hapla   PetscSF sf;
691803da9461SVaclav Hapla   PetscInt d,depth,i,nleaves,p,plo,phi,missingPoint;
691903da9461SVaclav Hapla   const PetscInt *locals;
692003da9461SVaclav Hapla   PetscErrorCode ierr;
692103da9461SVaclav Hapla 
692203da9461SVaclav Hapla   PetscFunctionBegin;
692303da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
692403da9461SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
692503da9461SVaclav Hapla   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
692603da9461SVaclav Hapla   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &locals, NULL);CHKERRQ(ierr);
692703da9461SVaclav Hapla 
6928ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
692903da9461SVaclav Hapla   ierr = DMPlexGetVTKCellHeight(dm, &d);CHKERRQ(ierr);
693003da9461SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, d, &plo, &phi);CHKERRQ(ierr);
693103da9461SVaclav Hapla   for (i=0; i<nleaves; i++) {
693203da9461SVaclav Hapla     p = locals[i];
693303da9461SVaclav Hapla     if (p >= plo && p < phi) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d which is a cell",p);
693403da9461SVaclav Hapla   }
6935ece87651SVaclav Hapla 
6936ece87651SVaclav Hapla   /* 2) if some point is in interface, then all its cone points must be also in interface  */
6937ece87651SVaclav Hapla   for (i=0; i<nleaves; i++) {
6938ece87651SVaclav Hapla     p = locals[i];
6939ece87651SVaclav Hapla     ierr = DMPlexAreAllConePointsInArray_Private(dm, p, nleaves, locals, &missingPoint);CHKERRQ(ierr);
6940ece87651SVaclav Hapla     if (missingPoint >= 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d but not %d from its cone",p,missingPoint);
6941ece87651SVaclav Hapla   }
694203da9461SVaclav Hapla   PetscFunctionReturn(0);
694303da9461SVaclav Hapla }
694403da9461SVaclav Hapla 
6945068a5610SStefano Zampini typedef struct cell_stats
6946068a5610SStefano Zampini {
6947068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
6948068a5610SStefano Zampini   PetscInt  count;
6949068a5610SStefano Zampini } cell_stats_t;
6950068a5610SStefano Zampini 
6951068a5610SStefano Zampini static void cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
6952068a5610SStefano Zampini {
6953068a5610SStefano Zampini   PetscInt i, N = *len;
6954068a5610SStefano Zampini 
6955068a5610SStefano Zampini   for (i = 0; i < N; i++) {
6956068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
6957068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
6958068a5610SStefano Zampini 
6959068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
6960068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
6961068a5610SStefano Zampini     B->sum += A->sum;
6962068a5610SStefano Zampini     B->squaresum += A->squaresum;
6963068a5610SStefano Zampini     B->count += A->count;
6964068a5610SStefano Zampini   }
6965068a5610SStefano Zampini }
6966068a5610SStefano Zampini 
6967068a5610SStefano Zampini /*@
6968068a5610SStefano Zampini   DMPlexCheckCellShape - Checks the Jacobian of the mapping and computes some minimal statistics.
6969068a5610SStefano Zampini 
6970068a5610SStefano Zampini   Input Parameters:
6971068a5610SStefano Zampini + dm - The DMPlex object
6972068a5610SStefano Zampini - output - If true, statistics will be displayed on stdout
6973068a5610SStefano Zampini 
6974068a5610SStefano Zampini   Note: This is mainly intended for debugging/testing purposes.
6975068a5610SStefano Zampini 
6976068a5610SStefano Zampini   Level: developer
6977068a5610SStefano Zampini 
6978068a5610SStefano Zampini .seealso: DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6979068a5610SStefano Zampini @*/
6980068a5610SStefano Zampini PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output)
6981068a5610SStefano Zampini {
6982068a5610SStefano Zampini   PetscMPIInt    rank,size;
6983068a5610SStefano Zampini   PetscInt       dim, c, cStart, cEnd, cMax, count = 0;
6984068a5610SStefano Zampini   cell_stats_t   stats, globalStats;
6985068a5610SStefano Zampini   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
6986068a5610SStefano Zampini   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
6987068a5610SStefano Zampini   DM             dmCoarse;
6988068a5610SStefano Zampini   PetscErrorCode ierr;
6989068a5610SStefano Zampini 
6990068a5610SStefano Zampini   PetscFunctionBegin;
6991068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6992068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
6993068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
6994068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
6995068a5610SStefano Zampini   stats.count = 0;
6996068a5610SStefano Zampini 
6997068a5610SStefano Zampini   ierr = DMGetCoordinateDim(dm,&dim);CHKERRQ(ierr);
6998068a5610SStefano Zampini   ierr = PetscMalloc2(dim * dim, &J, dim * dim, &invJ);CHKERRQ(ierr);
6999068a5610SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
7000068a5610SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,NULL,NULL,NULL);CHKERRQ(ierr);
7001068a5610SStefano Zampini   cMax = cMax < 0 ? cEnd : cMax;
7002068a5610SStefano Zampini   for (c = cStart; c < cMax; c++) {
7003068a5610SStefano Zampini     PetscInt  i;
7004068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
7005068a5610SStefano Zampini 
7006068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
7007068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
7008068a5610SStefano Zampini     for (i = 0; i < dim * dim; i++) {
7009068a5610SStefano Zampini       frobJ    += J[i] * J[i];
7010068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
7011068a5610SStefano Zampini     }
7012068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
7013068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
7014068a5610SStefano Zampini 
7015068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
7016068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
7017068a5610SStefano Zampini     stats.sum       += cond;
7018068a5610SStefano Zampini     stats.squaresum += cond2;
7019068a5610SStefano Zampini     stats.count++;
7020068a5610SStefano Zampini   }
7021068a5610SStefano Zampini 
7022068a5610SStefano Zampini   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
7023068a5610SStefano Zampini   if (size > 1) {
7024068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
7025068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7026068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7027068a5610SStefano Zampini     MPI_Op        statReduce;
7028068a5610SStefano Zampini 
7029068a5610SStefano Zampini     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRQ(ierr);
7030068a5610SStefano Zampini     ierr = MPI_Type_commit(&statType);CHKERRQ(ierr);
7031068a5610SStefano Zampini     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRQ(ierr);
7032068a5610SStefano Zampini     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRQ(ierr);
7033068a5610SStefano Zampini     ierr = MPI_Op_free(&statReduce);CHKERRQ(ierr);
7034068a5610SStefano Zampini     ierr = MPI_Type_free(&statType);CHKERRQ(ierr);
7035068a5610SStefano Zampini   } else {
7036068a5610SStefano Zampini     ierr = PetscMemcpy(&globalStats,&stats,sizeof(stats));CHKERRQ(ierr);
7037068a5610SStefano Zampini   }
7038068a5610SStefano Zampini 
7039068a5610SStefano Zampini   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
7040068a5610SStefano Zampini   if (!rank) {
7041068a5610SStefano Zampini     count = globalStats.count;
7042068a5610SStefano Zampini     min   = globalStats.min;
7043068a5610SStefano Zampini     max   = globalStats.max;
7044068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
7045068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7046068a5610SStefano Zampini   }
7047068a5610SStefano Zampini 
7048068a5610SStefano Zampini   if (output) {
7049068a5610SStefano Zampini     ierr = PetscPrintf(comm,"Mesh with %D cells, shape condition numbers: min = %g, max = %g, mean = %g, stddev = %g\n", count, (double) min, (double) max, (double) mean, (double) stdev);CHKERRQ(ierr);
7050068a5610SStefano Zampini   }
7051068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
7052068a5610SStefano Zampini 
7053068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
7054068a5610SStefano Zampini   if (dmCoarse) {
7055068a5610SStefano Zampini     PetscBool isplex;
7056068a5610SStefano Zampini 
7057068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
7058068a5610SStefano Zampini     if (isplex) {
7059068a5610SStefano Zampini       ierr = DMPlexCheckCellShape(dmCoarse,output);CHKERRQ(ierr);
7060068a5610SStefano Zampini     }
7061068a5610SStefano Zampini   }
7062068a5610SStefano Zampini   PetscFunctionReturn(0);
7063068a5610SStefano Zampini }
7064068a5610SStefano Zampini 
7065bceba477SMatthew G. Knepley /* Pointwise interpolation
7066bceba477SMatthew G. Knepley      Just code FEM for now
7067bceba477SMatthew G. Knepley      u^f = I u^c
70684ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
70694ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
70704ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
7071bceba477SMatthew G. Knepley */
7072bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
7073bceba477SMatthew G. Knepley {
7074bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
7075bceba477SMatthew G. Knepley   PetscInt       m, n;
7076a063dac3SMatthew G. Knepley   void          *ctx;
707768132eb9SMatthew G. Knepley   DM             cdm;
7078fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
7079bceba477SMatthew G. Knepley   PetscErrorCode ierr;
7080bceba477SMatthew G. Knepley 
7081bceba477SMatthew G. Knepley   PetscFunctionBegin;
7082e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7083bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7084e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7085bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
708668132eb9SMatthew G. Knepley 
7087fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
7088bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
7089bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7090fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
7091a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
709268132eb9SMatthew G. Knepley 
7093a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
709468132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
709568132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
709668132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
709768132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
70984db47ee9SStefano Zampini   if (scaling) {
70995d1c2e58SMatthew G. Knepley     /* Use naive scaling */
71005d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
71014db47ee9SStefano Zampini   }
7102a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
7103a063dac3SMatthew G. Knepley }
7104bceba477SMatthew G. Knepley 
71056dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
7106a063dac3SMatthew G. Knepley {
710790748bafSMatthew G. Knepley   PetscErrorCode ierr;
71086dbf9973SLawrence Mitchell   VecScatter     ctx;
710990748bafSMatthew G. Knepley 
7110a063dac3SMatthew G. Knepley   PetscFunctionBegin;
71116dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
71126dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
71136dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
7114bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
7115bceba477SMatthew G. Knepley }
7116bceba477SMatthew G. Knepley 
7117bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
7118bd041c0cSMatthew G. Knepley {
7119bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
7120bd041c0cSMatthew G. Knepley   PetscInt       m, n;
7121bd041c0cSMatthew G. Knepley   void          *ctx;
7122bd041c0cSMatthew G. Knepley   DM             cdm;
7123bd041c0cSMatthew G. Knepley   PetscBool      regular;
7124bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
7125bd041c0cSMatthew G. Knepley 
7126bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
7127e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7128bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7129e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7130bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
7131bd041c0cSMatthew G. Knepley 
7132bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
7133bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7134bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
7135bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
7136bd041c0cSMatthew G. Knepley 
7137bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
7138bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
7139bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7140bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7141bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
7142bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
7143bd041c0cSMatthew G. Knepley }
7144bd041c0cSMatthew G. Knepley 
71450aef6b92SMatthew G. Knepley /*@
71460aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
71470aef6b92SMatthew G. Knepley 
71480aef6b92SMatthew G. Knepley   Input Parameter:
71490aef6b92SMatthew G. Knepley . dm - The DMPlex object
71500aef6b92SMatthew G. Knepley 
71510aef6b92SMatthew G. Knepley   Output Parameter:
71520aef6b92SMatthew G. Knepley . regular - The flag
71530aef6b92SMatthew G. Knepley 
71540aef6b92SMatthew G. Knepley   Level: intermediate
71550aef6b92SMatthew G. Knepley 
71560aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
71570aef6b92SMatthew G. Knepley @*/
71580aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
71590aef6b92SMatthew G. Knepley {
71600aef6b92SMatthew G. Knepley   PetscFunctionBegin;
71610aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71620aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
71630aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
71640aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
71650aef6b92SMatthew G. Knepley }
71660aef6b92SMatthew G. Knepley 
71670aef6b92SMatthew G. Knepley /*@
71680aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
71690aef6b92SMatthew G. Knepley 
71700aef6b92SMatthew G. Knepley   Input Parameters:
71710aef6b92SMatthew G. Knepley + dm - The DMPlex object
71720aef6b92SMatthew G. Knepley - regular - The flag
71730aef6b92SMatthew G. Knepley 
71740aef6b92SMatthew G. Knepley   Level: intermediate
71750aef6b92SMatthew G. Knepley 
71760aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
71770aef6b92SMatthew G. Knepley @*/
71780aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
71790aef6b92SMatthew G. Knepley {
71800aef6b92SMatthew G. Knepley   PetscFunctionBegin;
71810aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71820aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
71830aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
71840aef6b92SMatthew G. Knepley }
71850aef6b92SMatthew G. Knepley 
7186f7c74593SToby Isaac /* anchors */
7187a68b90caSToby Isaac /*@
7188f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
7189f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
7190a68b90caSToby Isaac 
7191e228b242SToby Isaac   not collective
7192a68b90caSToby Isaac 
7193a68b90caSToby Isaac   Input Parameters:
7194a68b90caSToby Isaac . dm - The DMPlex object
7195a68b90caSToby Isaac 
7196a68b90caSToby Isaac   Output Parameters:
7197a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
7198a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
7199a68b90caSToby Isaac 
7200a68b90caSToby Isaac 
7201a68b90caSToby Isaac   Level: intermediate
7202a68b90caSToby Isaac 
7203f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
7204a68b90caSToby Isaac @*/
7205a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
7206a68b90caSToby Isaac {
7207a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
720841e6d900SToby Isaac   PetscErrorCode ierr;
7209a68b90caSToby Isaac 
7210a68b90caSToby Isaac   PetscFunctionBegin;
7211a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
721241e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
7213a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
7214a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
7215a68b90caSToby Isaac   PetscFunctionReturn(0);
7216a68b90caSToby Isaac }
7217a68b90caSToby Isaac 
7218a68b90caSToby Isaac /*@
7219f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
7220f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
7221a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
7222a68b90caSToby Isaac 
7223a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
7224f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
7225a68b90caSToby Isaac 
7226e228b242SToby Isaac   collective on dm
7227a68b90caSToby Isaac 
7228a68b90caSToby Isaac   Input Parameters:
7229a68b90caSToby Isaac + dm - The DMPlex object
7230e228b242SToby 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).
7231e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
7232a68b90caSToby Isaac 
7233a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
7234a68b90caSToby Isaac 
7235a68b90caSToby Isaac   Level: intermediate
7236a68b90caSToby Isaac 
7237f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
7238a68b90caSToby Isaac @*/
7239a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
7240a68b90caSToby Isaac {
7241a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7242e228b242SToby Isaac   PetscMPIInt    result;
7243a68b90caSToby Isaac   PetscErrorCode ierr;
7244a68b90caSToby Isaac 
7245a68b90caSToby Isaac   PetscFunctionBegin;
7246a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7247e228b242SToby Isaac   if (anchorSection) {
7248e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
7249e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
7250f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
7251e228b242SToby Isaac   }
7252e228b242SToby Isaac   if (anchorIS) {
7253e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
7254e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
7255f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
7256e228b242SToby Isaac   }
7257a68b90caSToby Isaac 
7258a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
7259a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
7260a68b90caSToby Isaac   plex->anchorSection = anchorSection;
7261a68b90caSToby Isaac 
7262a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
7263a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
7264a68b90caSToby Isaac   plex->anchorIS = anchorIS;
7265a68b90caSToby Isaac 
7266a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
7267a68b90caSToby Isaac   if (anchorIS && anchorSection) {
7268a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
7269a68b90caSToby Isaac     const PetscInt *anchors;
7270a68b90caSToby Isaac 
7271a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
7272a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
7273a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
7274a68b90caSToby Isaac     for (a = 0; a < size; a++) {
7275a68b90caSToby Isaac       PetscInt p;
7276a68b90caSToby Isaac 
7277a68b90caSToby Isaac       p = anchors[a];
7278a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
7279a68b90caSToby Isaac         PetscInt dof;
7280a68b90caSToby Isaac 
7281a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7282a68b90caSToby Isaac         if (dof) {
7283a68b90caSToby Isaac           PetscErrorCode ierr2;
7284a68b90caSToby Isaac 
7285a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
72868ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
7287a68b90caSToby Isaac         }
7288a68b90caSToby Isaac       }
7289a68b90caSToby Isaac     }
7290a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
7291a68b90caSToby Isaac   }
7292a68b90caSToby Isaac #endif
7293f7c74593SToby Isaac   /* reset the generic constraints */
7294f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
7295a68b90caSToby Isaac   PetscFunctionReturn(0);
7296a68b90caSToby Isaac }
7297a68b90caSToby Isaac 
7298f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7299a68b90caSToby Isaac {
7300f7c74593SToby Isaac   PetscSection anchorSection;
73016995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
7302a68b90caSToby Isaac   PetscErrorCode ierr;
7303a68b90caSToby Isaac 
7304a68b90caSToby Isaac   PetscFunctionBegin;
7305a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7306a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7307e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7308a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
73096995de1eSToby Isaac   if (numFields) {
7310719ab38cSToby Isaac     PetscInt f;
7311a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7312719ab38cSToby Isaac 
7313719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7314719ab38cSToby Isaac       PetscInt numComp;
7315719ab38cSToby Isaac 
7316719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7317719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7318719ab38cSToby Isaac     }
73196995de1eSToby Isaac   }
7320a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
73216995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
73226995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
73236995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
73246995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7325a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7326a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7327a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7328a68b90caSToby Isaac     if (dof) {
7329a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7330a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7331a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7332a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7333a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7334a68b90caSToby Isaac       }
7335a68b90caSToby Isaac     }
7336a68b90caSToby Isaac   }
7337a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7338a68b90caSToby Isaac   PetscFunctionReturn(0);
7339a68b90caSToby Isaac }
7340a68b90caSToby Isaac 
7341f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7342a68b90caSToby Isaac {
7343f7c74593SToby Isaac   PetscSection aSec;
73440ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
73450ac89760SToby Isaac   const PetscInt *anchors;
73460ac89760SToby Isaac   PetscInt numFields, f;
734766ad2231SToby Isaac   IS aIS;
73480ac89760SToby Isaac   PetscErrorCode ierr;
73490ac89760SToby Isaac 
73500ac89760SToby Isaac   PetscFunctionBegin;
73510ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73520ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
73530ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
73540ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
73550ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7356302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7357a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
735866ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
73596995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
73606995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
73610ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
73620ac89760SToby Isaac   i[0] = 0;
73630ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
73640ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7365f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7366f19733c5SToby Isaac 
7367f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7368f19733c5SToby Isaac     if (!rDof) continue;
7369f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
73700ac89760SToby Isaac     if (numFields) {
73710ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
73720ac89760SToby Isaac         annz = 0;
7373f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7374f19733c5SToby Isaac           a = anchors[rOff + r];
73750ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
73760ac89760SToby Isaac           annz += aDof;
73770ac89760SToby Isaac         }
73780ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
73790ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
73800ac89760SToby Isaac         for (q = 0; q < dof; q++) {
73810ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
73820ac89760SToby Isaac         }
73830ac89760SToby Isaac       }
73840ac89760SToby Isaac     }
73850ac89760SToby Isaac     else {
73860ac89760SToby Isaac       annz = 0;
73870ac89760SToby Isaac       for (q = 0; q < dof; q++) {
73880ac89760SToby Isaac         a = anchors[off + q];
73890ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
73900ac89760SToby Isaac         annz += aDof;
73910ac89760SToby Isaac       }
73920ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
73930ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
73940ac89760SToby Isaac       for (q = 0; q < dof; q++) {
73950ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
73960ac89760SToby Isaac       }
73970ac89760SToby Isaac     }
73980ac89760SToby Isaac   }
73990ac89760SToby Isaac   nnz = i[m];
74000ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
74010ac89760SToby Isaac   offset = 0;
74020ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
74030ac89760SToby Isaac     if (numFields) {
74040ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
74050ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
74060ac89760SToby Isaac         for (q = 0; q < dof; q++) {
74070ac89760SToby Isaac           PetscInt rDof, rOff, r;
74080ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
74090ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
74100ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
74110ac89760SToby Isaac             PetscInt s;
74120ac89760SToby Isaac 
74130ac89760SToby Isaac             a = anchors[rOff + r];
74140ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
74150ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
74160ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
74170ac89760SToby Isaac               j[offset++] = aOff + s;
74180ac89760SToby Isaac             }
74190ac89760SToby Isaac           }
74200ac89760SToby Isaac         }
74210ac89760SToby Isaac       }
74220ac89760SToby Isaac     }
74230ac89760SToby Isaac     else {
74240ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
74250ac89760SToby Isaac       for (q = 0; q < dof; q++) {
74260ac89760SToby Isaac         PetscInt rDof, rOff, r;
74270ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
74280ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
74290ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
74300ac89760SToby Isaac           PetscInt s;
74310ac89760SToby Isaac 
74320ac89760SToby Isaac           a = anchors[rOff + r];
74330ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
74340ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
74350ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
74360ac89760SToby Isaac             j[offset++] = aOff + s;
74370ac89760SToby Isaac           }
74380ac89760SToby Isaac         }
74390ac89760SToby Isaac       }
74400ac89760SToby Isaac     }
74410ac89760SToby Isaac   }
74420ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
744325570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
744425570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
744566ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
74460ac89760SToby Isaac   PetscFunctionReturn(0);
74470ac89760SToby Isaac }
74480ac89760SToby Isaac 
744966ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
745066ad2231SToby Isaac {
7451f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7452f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
745366ad2231SToby Isaac   Mat            cMat;
745466ad2231SToby Isaac   PetscErrorCode ierr;
745566ad2231SToby Isaac 
745666ad2231SToby Isaac   PetscFunctionBegin;
745766ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7458a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
745966ad2231SToby Isaac   if (anchorSection) {
746044a7f3ddSMatthew G. Knepley     PetscInt Nf;
7461e228b242SToby Isaac 
7462e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
7463f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7464f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
746544a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
746644a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
746766ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
746866ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
746966ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
747066ad2231SToby Isaac   }
747166ad2231SToby Isaac   PetscFunctionReturn(0);
747266ad2231SToby Isaac }
7473a93c429eSMatthew G. Knepley 
7474a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
7475a93c429eSMatthew G. Knepley {
7476a93c429eSMatthew G. Knepley   IS             subis;
7477a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
7478a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
7479a93c429eSMatthew G. Knepley 
7480a93c429eSMatthew G. Knepley   PetscFunctionBegin;
7481a93c429eSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
7482a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
7483a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
7484a93c429eSMatthew G. Knepley   /* Create subdomain */
7485a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
7486a93c429eSMatthew G. Knepley   /* Create submodel */
7487a93c429eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(*subdm, &subis);CHKERRQ(ierr);
7488a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
7489a93c429eSMatthew G. Knepley   ierr = ISDestroy(&subis);CHKERRQ(ierr);
7490a93c429eSMatthew G. Knepley   ierr = DMSetDefaultSection(*subdm, subsection);CHKERRQ(ierr);
7491a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
7492e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
7493a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
7494a93c429eSMatthew G. Knepley   if (is) {
7495a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
7496a93c429eSMatthew G. Knepley     IS              spIS;
7497a93c429eSMatthew G. Knepley     const PetscInt *spmap;
7498a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
7499a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
7500a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
7501a93c429eSMatthew G. Knepley 
7502a93c429eSMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
7503a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
7504a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
7505a93c429eSMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
7506a93c429eSMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
7507a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
7508a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7509a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
7510a93c429eSMatthew G. Knepley 
7511a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
7512a93c429eSMatthew G. Knepley       if (gdof > 0) {
7513a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7514a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
7515a93c429eSMatthew G. Knepley 
7516a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
7517a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
7518a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
7519a93c429eSMatthew G. Knepley         }
7520a93c429eSMatthew G. Knepley         subSize += pSubSize;
7521a93c429eSMatthew G. Knepley         if (pSubSize) {
7522a93c429eSMatthew G. Knepley           if (bs < 0) {
7523a93c429eSMatthew G. Knepley             bs = pSubSize;
7524a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
7525a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
7526a93c429eSMatthew G. Knepley             bs = 1;
7527a93c429eSMatthew G. Knepley           }
7528a93c429eSMatthew G. Knepley         }
7529a93c429eSMatthew G. Knepley       }
7530a93c429eSMatthew G. Knepley     }
7531a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
7532a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
7533a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
7534a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
7535a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
7536a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
7537a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7538a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
7539a93c429eSMatthew G. Knepley 
7540a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
7541a93c429eSMatthew G. Knepley       if (gdof > 0) {
7542a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
7543a93c429eSMatthew G. Knepley 
7544a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
7545a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7546a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
7547a93c429eSMatthew G. Knepley 
7548a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
7549a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
7550a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
7551a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
7552a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
7553a93c429eSMatthew G. Knepley           }
7554a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
7555a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
7556a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
7557a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
7558a93c429eSMatthew G. Knepley           }
7559a93c429eSMatthew G. Knepley         }
7560a93c429eSMatthew G. Knepley       }
7561a93c429eSMatthew G. Knepley     }
7562a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
7563a93c429eSMatthew G. Knepley     ierr = ISDestroy(&spIS);CHKERRQ(ierr);
7564a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
7565a93c429eSMatthew G. Knepley     if (bs > 1) {
7566a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
7567a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
7568a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
7569a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
7570a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
7571a93c429eSMatthew G. Knepley         }
7572a93c429eSMatthew G. Knepley       }
7573a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
7574a93c429eSMatthew G. Knepley     }
7575a93c429eSMatthew G. Knepley     /* Attach nullspace */
7576a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
7577a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
7578a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
7579a93c429eSMatthew G. Knepley     }
7580a93c429eSMatthew G. Knepley     if (f < Nf) {
7581a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
7582a93c429eSMatthew G. Knepley 
7583a93c429eSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);CHKERRQ(ierr);
7584a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
7585a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
7586a93c429eSMatthew G. Knepley     }
7587a93c429eSMatthew G. Knepley   }
7588a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
7589a93c429eSMatthew G. Knepley }
7590