xref: /petsc/src/dm/impls/plex/plex.c (revision a43559064f8c0f3e08ca38b39e98790a3534f60d)
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) {
417d930f514SMatthew G. Knepley     const char *vecname;
418d930f514SMatthew G. Knepley     PetscInt    n, nroots;
419d930f514SMatthew G. Knepley 
420d930f514SMatthew G. Knepley     if (dm->sfNatural) {
421ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
422d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
423d930f514SMatthew G. Knepley       if (n == nroots) {
424d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
425d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
426d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
427d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
429d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
430d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
431d930f514SMatthew G. Knepley   } else {
432d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
433d930f514SMatthew G. Knepley     v = originalv;
434d930f514SMatthew G. Knepley   }
435d930f514SMatthew G. Knepley   if (ishdf5) {
436d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
43739d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
438d930f514SMatthew G. Knepley #else
439d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
440d930f514SMatthew G. Knepley #endif
441d930f514SMatthew G. Knepley   } else if (isvtk) {
442d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
443d930f514SMatthew G. Knepley   } else {
444d930f514SMatthew G. Knepley     PetscBool isseq;
445d930f514SMatthew G. Knepley 
446d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
447d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
448d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
449d930f514SMatthew G. Knepley   }
450d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
451d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
452d930f514SMatthew G. Knepley }
453d930f514SMatthew G. Knepley 
4542c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4552c40f234SMatthew G. Knepley {
4562c40f234SMatthew G. Knepley   DM             dm;
4572c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4582c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4592c40f234SMatthew G. Knepley 
4602c40f234SMatthew G. Knepley   PetscFunctionBegin;
4612c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4622c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4632c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley   if (ishdf5) {
4652c40f234SMatthew G. Knepley     DM          dmBC;
4662c40f234SMatthew G. Knepley     Vec         gv;
4672c40f234SMatthew G. Knepley     const char *name;
4682c40f234SMatthew G. Knepley 
4692c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4702c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4712c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4722c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4732c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4742c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4752c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4762c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4772c40f234SMatthew G. Knepley   } else {
4782c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4792c40f234SMatthew G. Knepley   }
4802c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4812c40f234SMatthew G. Knepley }
4822c40f234SMatthew G. Knepley 
4832c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4842c40f234SMatthew G. Knepley {
4852c40f234SMatthew G. Knepley   DM             dm;
4862c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4872c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4882c40f234SMatthew G. Knepley 
4892c40f234SMatthew G. Knepley   PetscFunctionBegin;
4902c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4912c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4922c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4932c40f234SMatthew G. Knepley   if (ishdf5) {
494878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
49539d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
496b136c2c9SMatthew G. Knepley #else
497b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
498878b459fSMatthew G. Knepley #endif
4992c40f234SMatthew G. Knepley   } else {
5002c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
501552f7358SJed Brown   }
502552f7358SJed Brown   PetscFunctionReturn(0);
503552f7358SJed Brown }
504552f7358SJed Brown 
505d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
506d930f514SMatthew G. Knepley {
507d930f514SMatthew G. Knepley   DM                dm;
508d930f514SMatthew G. Knepley   PetscViewerFormat format;
509d930f514SMatthew G. Knepley   PetscBool         ishdf5;
510d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
511d930f514SMatthew G. Knepley 
512d930f514SMatthew G. Knepley   PetscFunctionBegin;
513d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
514d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
515d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
516d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
517d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
518d930f514SMatthew G. Knepley     if (dm->sfNatural) {
519d930f514SMatthew G. Knepley       if (ishdf5) {
520d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
521d930f514SMatthew G. Knepley         Vec         v;
522d930f514SMatthew G. Knepley         const char *vecname;
523d930f514SMatthew G. Knepley 
524d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
525d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
526d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
52739d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
528d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
529d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
530d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
531d930f514SMatthew G. Knepley #else
532d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
533d930f514SMatthew G. Knepley #endif
534d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
535d930f514SMatthew G. Knepley     }
536d930f514SMatthew G. Knepley   }
537d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
538d930f514SMatthew G. Knepley }
539d930f514SMatthew G. Knepley 
5407cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
541731e8ddeSMatthew G. Knepley {
542731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
543731e8ddeSMatthew G. Knepley   Vec                coordinates;
544731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
545731e8ddeSMatthew G. Knepley   const char        *name[4];
546731e8ddeSMatthew G. Knepley   const PetscScalar *a;
547731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
548731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
549731e8ddeSMatthew G. Knepley 
550731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
551731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
552731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
553731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
554731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
555731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
556731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
557731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
559731e8ddeSMatthew G. Knepley   name[1]     = "edge";
560731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
561731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
562731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
563731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
564731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
565731e8ddeSMatthew G. Knepley 
566f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
567731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
568731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
569731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
570731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
571731e8ddeSMatthew G. Knepley 
572731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
573731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley       if (!dof) continue;
575731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
577f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
578731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
579731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
580731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
581731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
58241477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
583731e8ddeSMatthew G. Knepley         }
584731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
585731e8ddeSMatthew G. Knepley       }
586731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
587731e8ddeSMatthew G. Knepley     }
588731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
589731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
590731e8ddeSMatthew G. Knepley   }
591731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
592731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
593731e8ddeSMatthew G. Knepley }
594731e8ddeSMatthew G. Knepley 
5957cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
596552f7358SJed Brown {
597552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
598552f7358SJed Brown   DM                cdm;
599552f7358SJed Brown   DMLabel           markers;
600552f7358SJed Brown   PetscSection      coordSection;
601552f7358SJed Brown   Vec               coordinates;
602552f7358SJed Brown   PetscViewerFormat format;
603552f7358SJed Brown   PetscErrorCode    ierr;
604552f7358SJed Brown 
605552f7358SJed Brown   PetscFunctionBegin;
606552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
607e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
608552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
609552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
610552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
611552f7358SJed Brown     const char *name;
612f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
613552f7358SJed Brown     PetscInt    pStart, pEnd, p;
614552f7358SJed Brown     PetscMPIInt rank, size;
615552f7358SJed Brown 
61682f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
61782f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
618552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
619552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
620552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
621f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
622f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
623f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
624f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
625f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
626e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6274d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
628e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
629552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
630552f7358SJed Brown       PetscInt dof, off, s;
631552f7358SJed Brown 
632552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
633552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
634552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
635e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
636552f7358SJed Brown       }
637552f7358SJed Brown     }
638552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
639e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
640e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
641552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
642552f7358SJed Brown       PetscInt dof, off, c;
643552f7358SJed Brown 
644552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
645552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
646552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
647e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
648552f7358SJed Brown       }
649552f7358SJed Brown     }
650552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6514d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
65280180ce3SStefano Zampini     ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
653c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
6547422c0d1SMatthew G. Knepley     if (markers) {ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);}
655552f7358SJed Brown     if (size > 1) {
656552f7358SJed Brown       PetscSF sf;
657552f7358SJed Brown 
658552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
659552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
660552f7358SJed Brown     }
661552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
662552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6630588280cSMatthew G. Knepley     const char  *name, *color;
6640588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6650588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
666552f7358SJed Brown     PetscReal    scale         = 2.0;
66778081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
6680588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6690588280cSMatthew G. Knepley     double       tcoords[3];
670552f7358SJed Brown     PetscScalar *coords;
6710588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
672552f7358SJed Brown     PetscMPIInt  rank, size;
6730588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
674202fd40aSStefano Zampini     PetscBool    plotEdges, flg;
675552f7358SJed Brown 
6760588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
677552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
678c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6790588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6800588280cSMatthew G. Knepley     numColors  = 10;
6810588280cSMatthew G. Knepley     numLColors = 10;
6820588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
683c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
68478081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
685c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
686c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6870588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
688c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6890588280cSMatthew G. Knepley     if (!useColors) {
6900588280cSMatthew G. Knepley       numColors = 3;
6910588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6920588280cSMatthew G. Knepley     }
693c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6940588280cSMatthew G. Knepley     if (!useColors) {
6950588280cSMatthew G. Knepley       numLColors = 4;
6960588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
6970588280cSMatthew G. Knepley     }
698202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
699202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
700202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
701202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
70282f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
70382f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
704552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
705770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7060588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
707552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
708552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
709552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7100588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7110588280cSMatthew G. Knepley     if (size > 1) {
712f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
713770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
714770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
715770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
716770b213bSMatthew G Knepley         } else if (p > 0) {
717770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
718770b213bSMatthew G Knepley         }
719770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
720770b213bSMatthew G Knepley       }
7210588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7220588280cSMatthew G. Knepley     }
72378081901SStefano Zampini     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", tikzscale);CHKERRQ(ierr);
724552f7358SJed Brown     /* Plot vertices */
725552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
726552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7274d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
728552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
729552f7358SJed Brown       PetscInt  off, dof, d;
7300588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
731552f7358SJed Brown 
732552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
733552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7340588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
735f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7360588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7370588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
738c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7390588280cSMatthew G. Knepley       }
7400588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7410588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
742552f7358SJed Brown       for (d = 0; d < dof; ++d) {
743552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7440588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
745552f7358SJed Brown       }
7460588280cSMatthew G. Knepley       color = colors[rank%numColors];
7470588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7480588280cSMatthew G. Knepley         PetscInt val;
749c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7500588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7510588280cSMatthew G. Knepley       }
7520588280cSMatthew G. Knepley       if (useNumbers) {
753e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7540588280cSMatthew G. Knepley       } else {
755e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7560588280cSMatthew G. Knepley       }
757552f7358SJed Brown     }
758552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
759552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
760846a3e8bSMatthew G. Knepley     /* Plot cells */
761846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
76278081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
763846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
764846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
765846a3e8bSMatthew G. Knepley         const PetscInt *cone;
766846a3e8bSMatthew G. Knepley 
767846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
768846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
769846a3e8bSMatthew G. Knepley           PetscInt val;
770846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
771846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
772846a3e8bSMatthew G. Knepley         }
773846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
774846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
775846a3e8bSMatthew G. Knepley       }
776846a3e8bSMatthew G. Knepley     } else {
777846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
778846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
779846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
780846a3e8bSMatthew G. Knepley 
781846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
782846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
783846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
784846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
785846a3e8bSMatthew G. Knepley 
786846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
787846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
788846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
789846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
790846a3e8bSMatthew G. Knepley         }
791846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
792846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
793846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
794846a3e8bSMatthew G. Knepley       }
795846a3e8bSMatthew G. Knepley     }
796846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
797846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
798846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
799846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
800846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
801846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
802846a3e8bSMatthew G. Knepley 
803846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
804846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
805846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
806846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
807846a3e8bSMatthew G. Knepley         PetscInt       off;
808846a3e8bSMatthew G. Knepley 
809846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
810846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
811846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
812846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
813846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
814846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
815846a3e8bSMatthew G. Knepley         }
816846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
817846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
818846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
819846a3e8bSMatthew G. Knepley         ++n;
820846a3e8bSMatthew G. Knepley       }
821846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
822846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
823846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
824846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
825846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", ccoords[d]);CHKERRQ(ierr);
826846a3e8bSMatthew G. Knepley       }
827846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
828846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
829846a3e8bSMatthew G. Knepley         PetscInt val;
830846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
831846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
832846a3e8bSMatthew G. Knepley       }
833846a3e8bSMatthew G. Knepley       if (useNumbers) {
834846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
835846a3e8bSMatthew G. Knepley       } else {
836846a3e8bSMatthew 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);
837846a3e8bSMatthew G. Knepley       }
838846a3e8bSMatthew G. Knepley     }
839846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
840552f7358SJed Brown     /* Plot edges */
841202fd40aSStefano Zampini     if (plotEdges) {
842552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
843552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
844552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
845552f7358SJed Brown         const PetscInt *cone;
846552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
847552f7358SJed Brown 
848552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
849f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
850552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
851552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
852552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
853552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
854552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
855552f7358SJed Brown         for (d = 0; d < dof; ++d) {
856202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
857c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
858552f7358SJed Brown         }
8590588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
8600588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
8610588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
8620588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
863f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
8640588280cSMatthew G. Knepley         }
8650588280cSMatthew G. Knepley         color = colors[rank%numColors];
8660588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
8670588280cSMatthew G. Knepley           PetscInt val;
868c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
8690750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
8700588280cSMatthew G. Knepley         }
871e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
872552f7358SJed Brown       }
873552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
874552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
875552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
8760588280cSMatthew G. Knepley     }
877552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8784d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8790588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
880770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
8810588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
8820588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
8830588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
8840588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
885552f7358SJed Brown   } else {
88682f516ccSBarry Smith     MPI_Comm    comm;
887834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
888f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
889552f7358SJed Brown     PetscInt    pStart, pEnd, p;
890a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
8911143a3c0SMatthew G. Knepley     const char *name;
892552f7358SJed Brown     PetscMPIInt size;
893552f7358SJed Brown 
89482f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
895552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
896c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
897f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8985f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
899f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
900f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
901f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
902552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
903b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
904a04427b4SStefano Zampini     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, depth > 1 ? &pMax[depth - 2] : NULL, &pMax[0]);CHKERRQ(ierr);
905b0138d5dSStefano Zampini     ierr = PetscCalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
906552f7358SJed Brown     if (depth == 1) {
907552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
908552f7358SJed Brown       pEnd = pEnd - pStart;
909a04427b4SStefano Zampini       pMax[0] -= pStart;
910552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
911a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[0], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
912f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
913a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
914a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
915a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
916a04427b4SStefano Zampini       }
917552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
918552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
919552f7358SJed Brown       pEnd = pEnd - pStart;
920a04427b4SStefano Zampini       pMax[depth] -= pStart;
921552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
922a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[depth], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
923552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
924a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
925a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
926a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
927a04427b4SStefano Zampini       }
928552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
929552f7358SJed Brown     } else {
930cbb7f117SMark Adams       PetscMPIInt rank;
931cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
932552f7358SJed Brown       for (d = 0; d <= dim; d++) {
933552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
934834065abSMatthew G. Knepley         pEnd    -= pStart;
935834065abSMatthew G. Knepley         pMax[d] -= pStart;
936552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
937834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
938552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
939834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
940cbb7f117SMark Adams           if (!rank) {
941834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
942834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
943834065abSMatthew G. Knepley           }
944cbb7f117SMark Adams         }
945552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
946552f7358SJed Brown       }
947552f7358SJed Brown     }
948834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
949c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
950a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
951a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
952a57dd577SMatthew G Knepley       DMLabel         label;
953a57dd577SMatthew G Knepley       const char     *name;
954a57dd577SMatthew G Knepley       IS              valueIS;
955a57dd577SMatthew G Knepley       const PetscInt *values;
956a57dd577SMatthew G Knepley       PetscInt        numValues, v;
957a57dd577SMatthew G Knepley 
958c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
959c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
960a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
961d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
962a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
963a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
964120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
965a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
966a57dd577SMatthew G Knepley         PetscInt size;
967a57dd577SMatthew G Knepley 
968a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
969a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
970d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
971a57dd577SMatthew G Knepley       }
972a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
973120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
974a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
9754d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
976a57dd577SMatthew G Knepley     }
97734aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
97834aa8a36SMatthew G. Knepley     if (dm->Nf) {
97934aa8a36SMatthew G. Knepley       PetscInt f;
98034aa8a36SMatthew G. Knepley 
98134aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
98234aa8a36SMatthew G. Knepley         const char *name;
98334aa8a36SMatthew G. Knepley 
98434aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
98534aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
98634aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
98734aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
98834aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
98934aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
990ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
99134aa8a36SMatthew G. Knepley         } else {
99234aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
99334aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
99434aa8a36SMatthew G. Knepley         }
99534aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
99634aa8a36SMatthew G. Knepley       }
99734aa8a36SMatthew G. Knepley     }
998a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
9998e7ff633SMatthew G. Knepley     if (cdm) {
10008e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
10018e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
10028e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
10038e7ff633SMatthew G. Knepley     }
1004552f7358SJed Brown   }
1005552f7358SJed Brown   PetscFunctionReturn(0);
1006552f7358SJed Brown }
1007552f7358SJed Brown 
10087cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1009e412dcbdSMatthew G. Knepley {
1010e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1011e412dcbdSMatthew G. Knepley   DM                 cdm;
1012e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1013e412dcbdSMatthew G. Knepley   Vec                coordinates;
1014e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
101529494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1016e412dcbdSMatthew G. Knepley   PetscBool          isnull;
1017e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
1018cf3064d3SMatthew G. Knepley   PetscMPIInt        rank;
1019e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1020e412dcbdSMatthew G. Knepley 
1021e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1022e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1023e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1024e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1025e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
1026e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1027e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1028e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1029e412dcbdSMatthew G. Knepley 
1030e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1031e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1032e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1033e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1034e412dcbdSMatthew G. Knepley 
1035e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1036e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1037e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
10380c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
10390c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1040e412dcbdSMatthew G. Knepley   }
1041e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
104229494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
104329494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
104429494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1045e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1046e412dcbdSMatthew G. Knepley 
1047cf3064d3SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1048cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1049cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1050cf3064d3SMatthew G. Knepley     PetscInt     numCoords,coneSize;
1051cf3064d3SMatthew G. Knepley 
1052cf3064d3SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1053cf3064d3SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1054cf3064d3SMatthew G. Knepley     switch (coneSize) {
1055cf3064d3SMatthew G. Knepley     case 3:
1056cf3064d3SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1057cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1058cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1059cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1060cf3064d3SMatthew G. Knepley       break;
1061cf3064d3SMatthew G. Knepley     case 4:
1062cf3064d3SMatthew G. Knepley       ierr = PetscDrawRectangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1063cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1064cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1065cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1066cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1067cf3064d3SMatthew G. Knepley       break;
1068cf3064d3SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1069cf3064d3SMatthew G. Knepley     }
1070cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1071cf3064d3SMatthew G. Knepley   }
1072e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1073e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
107429494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
1075e412dcbdSMatthew G. Knepley 
107629494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1077e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
107829494db1SLisandro Dalcin     switch (coneSize) {
107929494db1SLisandro Dalcin     case 3:
10800c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10810c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10820c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1083e412dcbdSMatthew G. Knepley       break;
108429494db1SLisandro Dalcin     case 4:
10850c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10860c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10870c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10880c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1089e412dcbdSMatthew G. Knepley       break;
109029494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1091e412dcbdSMatthew G. Knepley     }
1092e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1093e412dcbdSMatthew G. Knepley   }
1094e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1095e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1096e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1097e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1098e412dcbdSMatthew G. Knepley }
1099e412dcbdSMatthew G. Knepley 
1100552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1101552f7358SJed Brown {
1102e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
1103002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1104552f7358SJed Brown   PetscErrorCode ierr;
1105552f7358SJed Brown 
1106552f7358SJed Brown   PetscFunctionBegin;
1107552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1108552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1109552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1110fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
1111c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
1112e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
11138135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
1114552f7358SJed Brown   if (iascii) {
11158135c375SStefano Zampini     PetscViewerFormat format;
11168135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
11178135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
11188135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
11198135c375SStefano Zampini     } else {
1120552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
11218135c375SStefano Zampini     }
1122c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1123c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
112439d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1125c6ccd67eSMatthew G. Knepley #else
1126c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1127552f7358SJed Brown #endif
1128e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1129fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1130e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1131e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
11328135c375SStefano Zampini   } else if (isglvis) {
11338135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
113462201deeSVaclav Hapla   } else {
1135a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1136fcf6c8fdSToby Isaac   }
1137cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1138cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1139cb3ba0daSMatthew G. Knepley   if (flg) {
1140cb3ba0daSMatthew G. Knepley     Vec ranks;
1141cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1142cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1143cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1144cb3ba0daSMatthew G. Knepley   }
1145002a2709SMatthew G. Knepley   /* Optionally view a label */
1146002a2709SMatthew G. Knepley   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
1147002a2709SMatthew G. Knepley   if (flg) {
1148002a2709SMatthew G. Knepley     DMLabel label;
1149002a2709SMatthew G. Knepley     Vec     val;
1150002a2709SMatthew G. Knepley 
1151002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1152002a2709SMatthew 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);
1153002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1154002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1155002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1156002a2709SMatthew G. Knepley   }
1157552f7358SJed Brown   PetscFunctionReturn(0);
1158552f7358SJed Brown }
1159552f7358SJed Brown 
11602c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
11612c40f234SMatthew G. Knepley {
1162d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
11632c40f234SMatthew G. Knepley   PetscErrorCode ierr;
11642c40f234SMatthew G. Knepley 
11652c40f234SMatthew G. Knepley   PetscFunctionBegin;
11662c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11672c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
11682c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1169d4f5a9a0SVaclav Hapla   if (ishdf5) {
11702c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
11719c48423bSVaclav Hapla     PetscViewerFormat format;
11729c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
11739c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
11749c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1175509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
117639d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1177509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
11782c40f234SMatthew G. Knepley #else
11792c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1180552f7358SJed Brown #endif
1181d4f5a9a0SVaclav Hapla   } else {
1182d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1183552f7358SJed Brown   }
1184552f7358SJed Brown   PetscFunctionReturn(0);
1185552f7358SJed Brown }
1186552f7358SJed Brown 
1187552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1188552f7358SJed Brown {
1189552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1190552f7358SJed Brown   PetscErrorCode ierr;
1191552f7358SJed Brown 
1192552f7358SJed Brown   PetscFunctionBegin;
11938135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
11948135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
11950d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1196552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1197552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1198552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1199552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1200be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1201552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1202552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1203d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1204d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
120577623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1206a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1207552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1208552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1209a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1210a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1211d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1212d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1213d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1214d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1215d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1216d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1217fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1218552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1219552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1220552f7358SJed Brown   PetscFunctionReturn(0);
1221552f7358SJed Brown }
1222552f7358SJed Brown 
1223b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1224552f7358SJed Brown {
12258d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1226acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1227552f7358SJed Brown   PetscInt               localSize;
1228837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1229552f7358SJed Brown   PetscErrorCode         ierr;
1230b412c318SBarry Smith   MatType                mtype;
12311428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1232552f7358SJed Brown 
1233552f7358SJed Brown   PetscFunctionBegin;
1234607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1235b412c318SBarry Smith   mtype = dm->mattype;
1236e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1237552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1238552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
123982f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1240552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1241552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1242552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1243acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1244acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1245552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1246552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1247552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1248552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1249552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1250552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1251552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1252837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1253552f7358SJed Brown   if (!isShell) {
1254be36d101SStefano Zampini     PetscSection subSection;
1255837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
12560be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1257fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1258552f7358SJed Brown 
125900140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1260be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1261be36d101SStefano Zampini       PetscSection section;
1262be36d101SStefano Zampini       PetscInt     size;
1263be36d101SStefano Zampini 
1264e87a4003SBarry Smith       ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1265be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1266be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1267be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1268be36d101SStefano Zampini     } else {
126900140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1270be36d101SStefano Zampini     }
1271552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1272be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1273a9d99c84SMatthew G. Knepley       PetscInt bdof;
1274a9d99c84SMatthew G. Knepley 
1275552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1276fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
12771d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
12781d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
12791d17a0a3SMatthew G. Knepley       if (dof) {
12801d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1281be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1282be36d101SStefano Zampini       }
1283be36d101SStefano Zampini       if (isMatIS) {
1284be36d101SStefano Zampini         PetscInt loff,c,off;
1285be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1286be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1287be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1288552f7358SJed Brown       }
12892a28c762SMatthew G Knepley     }
12902a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
12910be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
12920be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
12930be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
12940be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
12954fa26246SMatthew G. Knepley     bs = bs < 0 ? 1 : bs;
1296be36d101SStefano Zampini     if (isMatIS) {
12974fa26246SMatthew G. Knepley       PetscInt l;
12984fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
12994fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
13004fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1301be36d101SStefano Zampini     }
1302be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1303be36d101SStefano Zampini     if (isMatIS) {
1304be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1305be36d101SStefano Zampini     }
13061795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
13078d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1308552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1309552f7358SJed Brown   }
1310b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1311552f7358SJed Brown   PetscFunctionReturn(0);
1312552f7358SJed Brown }
1313552f7358SJed Brown 
13147cd05799SMatthew G. Knepley /*@
1315a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1316be36d101SStefano Zampini 
1317be36d101SStefano Zampini   Not collective
1318be36d101SStefano Zampini 
1319be36d101SStefano Zampini   Input Parameter:
1320be36d101SStefano Zampini . mesh - The DMPlex
1321be36d101SStefano Zampini 
1322be36d101SStefano Zampini   Output Parameters:
1323be36d101SStefano Zampini . subsection - The subdomain section
1324be36d101SStefano Zampini 
1325be36d101SStefano Zampini   Level: developer
1326be36d101SStefano Zampini 
1327be36d101SStefano Zampini .seealso:
13287cd05799SMatthew G. Knepley @*/
1329be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1330be36d101SStefano Zampini {
1331be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1332be36d101SStefano Zampini   PetscErrorCode ierr;
1333be36d101SStefano Zampini 
1334be36d101SStefano Zampini   PetscFunctionBegin;
1335be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1336be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1337be36d101SStefano Zampini     PetscSection section;
1338be36d101SStefano Zampini     PetscSF      sf;
1339be36d101SStefano Zampini 
1340be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1341e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
1342be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1343be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1344be36d101SStefano Zampini   }
1345be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1346be36d101SStefano Zampini   PetscFunctionReturn(0);
1347be36d101SStefano Zampini }
1348be36d101SStefano Zampini 
1349552f7358SJed Brown /*@
1350552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1351552f7358SJed Brown 
1352552f7358SJed Brown   Not collective
1353552f7358SJed Brown 
1354552f7358SJed Brown   Input Parameter:
1355552f7358SJed Brown . mesh - The DMPlex
1356552f7358SJed Brown 
1357552f7358SJed Brown   Output Parameters:
1358552f7358SJed Brown + pStart - The first mesh point
1359552f7358SJed Brown - pEnd   - The upper bound for mesh points
1360552f7358SJed Brown 
1361552f7358SJed Brown   Level: beginner
1362552f7358SJed Brown 
1363552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1364552f7358SJed Brown @*/
1365552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1366552f7358SJed Brown {
1367552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1368552f7358SJed Brown   PetscErrorCode ierr;
1369552f7358SJed Brown 
1370552f7358SJed Brown   PetscFunctionBegin;
1371552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1372552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1373552f7358SJed Brown   PetscFunctionReturn(0);
1374552f7358SJed Brown }
1375552f7358SJed Brown 
1376552f7358SJed Brown /*@
1377552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1378552f7358SJed Brown 
1379552f7358SJed Brown   Not collective
1380552f7358SJed Brown 
1381552f7358SJed Brown   Input Parameters:
1382552f7358SJed Brown + mesh - The DMPlex
1383552f7358SJed Brown . pStart - The first mesh point
1384552f7358SJed Brown - pEnd   - The upper bound for mesh points
1385552f7358SJed Brown 
1386552f7358SJed Brown   Output Parameters:
1387552f7358SJed Brown 
1388552f7358SJed Brown   Level: beginner
1389552f7358SJed Brown 
1390552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1391552f7358SJed Brown @*/
1392552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1393552f7358SJed Brown {
1394552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1395552f7358SJed Brown   PetscErrorCode ierr;
1396552f7358SJed Brown 
1397552f7358SJed Brown   PetscFunctionBegin;
1398552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1399552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1400552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1401552f7358SJed Brown   PetscFunctionReturn(0);
1402552f7358SJed Brown }
1403552f7358SJed Brown 
1404552f7358SJed Brown /*@
1405eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1406552f7358SJed Brown 
1407552f7358SJed Brown   Not collective
1408552f7358SJed Brown 
1409552f7358SJed Brown   Input Parameters:
1410552f7358SJed Brown + mesh - The DMPlex
1411eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1412552f7358SJed Brown 
1413552f7358SJed Brown   Output Parameter:
1414552f7358SJed Brown . size - The cone size for point p
1415552f7358SJed Brown 
1416552f7358SJed Brown   Level: beginner
1417552f7358SJed Brown 
1418552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1419552f7358SJed Brown @*/
1420552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1421552f7358SJed Brown {
1422552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1423552f7358SJed Brown   PetscErrorCode ierr;
1424552f7358SJed Brown 
1425552f7358SJed Brown   PetscFunctionBegin;
1426552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1427552f7358SJed Brown   PetscValidPointer(size, 3);
1428552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1429552f7358SJed Brown   PetscFunctionReturn(0);
1430552f7358SJed Brown }
1431552f7358SJed Brown 
1432552f7358SJed Brown /*@
1433eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1434552f7358SJed Brown 
1435552f7358SJed Brown   Not collective
1436552f7358SJed Brown 
1437552f7358SJed Brown   Input Parameters:
1438552f7358SJed Brown + mesh - The DMPlex
1439eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1440552f7358SJed Brown - size - The cone size for point p
1441552f7358SJed Brown 
1442552f7358SJed Brown   Output Parameter:
1443552f7358SJed Brown 
1444552f7358SJed Brown   Note:
1445552f7358SJed Brown   This should be called after DMPlexSetChart().
1446552f7358SJed Brown 
1447552f7358SJed Brown   Level: beginner
1448552f7358SJed Brown 
1449552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1450552f7358SJed Brown @*/
1451552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1452552f7358SJed Brown {
1453552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1454552f7358SJed Brown   PetscErrorCode ierr;
1455552f7358SJed Brown 
1456552f7358SJed Brown   PetscFunctionBegin;
1457552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1458552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
14590d644c17SKarl Rupp 
1460552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1461552f7358SJed Brown   PetscFunctionReturn(0);
1462552f7358SJed Brown }
1463552f7358SJed Brown 
1464f5a469b9SMatthew G. Knepley /*@
1465eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1466f5a469b9SMatthew G. Knepley 
1467f5a469b9SMatthew G. Knepley   Not collective
1468f5a469b9SMatthew G. Knepley 
1469f5a469b9SMatthew G. Knepley   Input Parameters:
1470f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1471eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1472f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1473f5a469b9SMatthew G. Knepley 
1474f5a469b9SMatthew G. Knepley   Output Parameter:
1475f5a469b9SMatthew G. Knepley 
1476f5a469b9SMatthew G. Knepley   Note:
1477f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1478f5a469b9SMatthew G. Knepley 
1479f5a469b9SMatthew G. Knepley   Level: beginner
1480f5a469b9SMatthew G. Knepley 
1481f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1482f5a469b9SMatthew G. Knepley @*/
1483f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1484f5a469b9SMatthew G. Knepley {
1485f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1486f5a469b9SMatthew G. Knepley   PetscInt       csize;
1487f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1488f5a469b9SMatthew G. Knepley 
1489f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1490f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1491f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1492f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1493f5a469b9SMatthew G. Knepley 
1494f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1495f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1496f5a469b9SMatthew G. Knepley }
1497f5a469b9SMatthew G. Knepley 
1498552f7358SJed Brown /*@C
1499eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1500552f7358SJed Brown 
1501552f7358SJed Brown   Not collective
1502552f7358SJed Brown 
1503552f7358SJed Brown   Input Parameters:
1504833c876bSVaclav Hapla + dm - The DMPlex
1505eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1506552f7358SJed Brown 
1507552f7358SJed Brown   Output Parameter:
1508552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1509552f7358SJed Brown 
1510552f7358SJed Brown   Level: beginner
1511552f7358SJed Brown 
15123813dfbdSMatthew G Knepley   Fortran Notes:
15133813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
15143813dfbdSMatthew G Knepley   include petsc.h90 in your code.
15153813dfbdSMatthew G Knepley 
15160ce7577fSVaclav Hapla .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1517552f7358SJed Brown @*/
1518552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1519552f7358SJed Brown {
1520552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1521552f7358SJed Brown   PetscInt       off;
1522552f7358SJed Brown   PetscErrorCode ierr;
1523552f7358SJed Brown 
1524552f7358SJed Brown   PetscFunctionBegin;
1525552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1526552f7358SJed Brown   PetscValidPointer(cone, 3);
1527552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1528552f7358SJed Brown   *cone = &mesh->cones[off];
1529552f7358SJed Brown   PetscFunctionReturn(0);
1530552f7358SJed Brown }
1531552f7358SJed Brown 
15320ce7577fSVaclav Hapla /*@C
15330ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
15340ce7577fSVaclav Hapla 
15350ce7577fSVaclav Hapla   Not collective
15360ce7577fSVaclav Hapla 
15370ce7577fSVaclav Hapla   Input Parameters:
15380ce7577fSVaclav Hapla + dm - The DMPlex
15390ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
15400ce7577fSVaclav Hapla 
15410ce7577fSVaclav Hapla   Output Parameter:
15420ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
15430ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
15440ce7577fSVaclav Hapla 
15450ce7577fSVaclav Hapla   Level: intermediate
15460ce7577fSVaclav Hapla 
1547d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
15480ce7577fSVaclav Hapla @*/
15490ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
15500ce7577fSVaclav Hapla {
15510ce7577fSVaclav Hapla   PetscSection        cs, newcs;
15520ce7577fSVaclav Hapla   PetscInt            *cones;
15530ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
15540ce7577fSVaclav Hapla   PetscInt            n;
15550ce7577fSVaclav Hapla   PetscErrorCode      ierr;
15560ce7577fSVaclav Hapla 
15570ce7577fSVaclav Hapla   PetscFunctionBegin;
15580ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
15590ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
15600ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
15610ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
15620ce7577fSVaclav Hapla   if (pCones) {
15630ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
15640ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
15650ce7577fSVaclav Hapla   }
15660ce7577fSVaclav Hapla   PetscFunctionReturn(0);
15670ce7577fSVaclav Hapla }
15680ce7577fSVaclav Hapla 
1569d4636a37SVaclav Hapla static PetscErrorCode DMPlexGetConeRecursive_Private(DM dm, PetscInt *n_inout, const PetscInt points[], PetscInt *offset_inout, PetscInt buf[])
1570d4636a37SVaclav Hapla {
1571d4636a37SVaclav Hapla   PetscInt p, n, cn, i;
1572d4636a37SVaclav Hapla   const PetscInt *cone;
1573d4636a37SVaclav Hapla   PetscErrorCode ierr;
1574d4636a37SVaclav Hapla 
1575d4636a37SVaclav Hapla   PetscFunctionBegin;
1576d4636a37SVaclav Hapla   n = *n_inout;
1577d4636a37SVaclav Hapla   *n_inout = 0;
1578d4636a37SVaclav Hapla   for (i=0; i<n; i++) {
1579d4636a37SVaclav Hapla     p = points[i];
1580d4636a37SVaclav Hapla     ierr = DMPlexGetConeSize(dm, p, &cn);CHKERRQ(ierr);
1581d4636a37SVaclav Hapla     if (!cn) {
1582d4636a37SVaclav Hapla       cn = 1;
1583d4636a37SVaclav Hapla       if (buf) {
1584d4636a37SVaclav Hapla         buf[*offset_inout] = p;
1585d4636a37SVaclav Hapla         ++(*offset_inout);
1586d4636a37SVaclav Hapla       }
1587d4636a37SVaclav Hapla     } else {
1588d4636a37SVaclav Hapla       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1589d4636a37SVaclav Hapla       ierr = DMPlexGetConeRecursive_Private(dm, &cn, cone, offset_inout, buf);CHKERRQ(ierr);
1590d4636a37SVaclav Hapla     }
1591d4636a37SVaclav Hapla     *n_inout += cn;
1592d4636a37SVaclav Hapla   }
1593d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1594d4636a37SVaclav Hapla }
1595d4636a37SVaclav Hapla 
1596d4636a37SVaclav Hapla /*@C
1597d4636a37SVaclav 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.
1598d4636a37SVaclav Hapla 
1599d4636a37SVaclav Hapla   Not collective
1600d4636a37SVaclav Hapla 
1601d4636a37SVaclav Hapla   Input Parameters:
1602d4636a37SVaclav Hapla + dm - The DMPlex
1603d4636a37SVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
1604d4636a37SVaclav Hapla 
1605d4636a37SVaclav Hapla   Output Parameter:
1606d4636a37SVaclav Hapla . pCones - An array of recursively expanded cones, i.e. containing only vertices, and each of them can be present multiple times
1607d4636a37SVaclav Hapla 
1608d4636a37SVaclav Hapla   Level: advanced
1609d4636a37SVaclav Hapla 
1610d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple()
1611d4636a37SVaclav Hapla @*/
1612d4636a37SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS p, IS *pCones)
1613d4636a37SVaclav Hapla {
1614d4636a37SVaclav Hapla   const PetscInt      *arr=NULL;
1615d4636a37SVaclav Hapla   PetscInt            *cpoints=NULL;
1616d4636a37SVaclav Hapla   PetscInt            n, cn;
1617d4636a37SVaclav Hapla   PetscInt            zero;
1618d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1619d4636a37SVaclav Hapla 
1620d4636a37SVaclav Hapla   PetscFunctionBegin;
1621d4636a37SVaclav Hapla   ierr = ISGetLocalSize(p, &n);CHKERRQ(ierr);
1622d4636a37SVaclav Hapla   ierr = ISGetIndices(p, &arr);CHKERRQ(ierr);
1623d4636a37SVaclav Hapla   zero = 0;
1624d4636a37SVaclav Hapla   /* first figure out the total number of returned points */
1625d4636a37SVaclav Hapla   cn = n;
1626d4636a37SVaclav Hapla   ierr = DMPlexGetConeRecursive_Private(dm, &cn, arr, &zero, NULL);CHKERRQ(ierr);
1627d4636a37SVaclav Hapla   ierr = PetscMalloc1(cn, &cpoints);CHKERRQ(ierr);
1628d4636a37SVaclav Hapla   /* now get recursive cones themselves */
1629d4636a37SVaclav Hapla   ierr = DMPlexGetConeRecursive_Private(dm, &n, arr, &zero, cpoints);CHKERRQ(ierr);
1630d4636a37SVaclav Hapla   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, cpoints, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
1631d4636a37SVaclav Hapla   ierr = ISRestoreIndices(p, &arr);CHKERRQ(ierr);
1632d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1633d4636a37SVaclav Hapla }
1634d4636a37SVaclav Hapla 
1635552f7358SJed Brown /*@
163692371b87SBarry 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
1637552f7358SJed Brown 
1638552f7358SJed Brown   Not collective
1639552f7358SJed Brown 
1640552f7358SJed Brown   Input Parameters:
1641552f7358SJed Brown + mesh - The DMPlex
1642eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1643552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1644552f7358SJed Brown 
1645552f7358SJed Brown   Output Parameter:
1646552f7358SJed Brown 
1647552f7358SJed Brown   Note:
1648552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1649552f7358SJed Brown 
165092371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
165192371b87SBarry Smith 
1652552f7358SJed Brown   Level: beginner
1653552f7358SJed Brown 
165492371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1655552f7358SJed Brown @*/
1656552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1657552f7358SJed Brown {
1658552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1659552f7358SJed Brown   PetscInt       pStart, pEnd;
1660552f7358SJed Brown   PetscInt       dof, off, c;
1661552f7358SJed Brown   PetscErrorCode ierr;
1662552f7358SJed Brown 
1663552f7358SJed Brown   PetscFunctionBegin;
1664552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1665552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1666552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1667552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1668552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
166982f516ccSBarry 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);
1670552f7358SJed Brown   for (c = 0; c < dof; ++c) {
167182f516ccSBarry 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);
1672552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1673552f7358SJed Brown   }
1674552f7358SJed Brown   PetscFunctionReturn(0);
1675552f7358SJed Brown }
1676552f7358SJed Brown 
1677552f7358SJed Brown /*@C
1678eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1679552f7358SJed Brown 
1680552f7358SJed Brown   Not collective
1681552f7358SJed Brown 
1682552f7358SJed Brown   Input Parameters:
1683552f7358SJed Brown + mesh - The DMPlex
1684eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1685552f7358SJed Brown 
1686552f7358SJed Brown   Output Parameter:
1687552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1688552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1689552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1690552f7358SJed Brown                     the index of the cone point on which to start.
1691552f7358SJed Brown 
1692552f7358SJed Brown   Level: beginner
1693552f7358SJed Brown 
16943813dfbdSMatthew G Knepley   Fortran Notes:
16953813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16963813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16973813dfbdSMatthew G Knepley 
16983813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1699552f7358SJed Brown 
1700552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1701552f7358SJed Brown @*/
1702552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1703552f7358SJed Brown {
1704552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1705552f7358SJed Brown   PetscInt       off;
1706552f7358SJed Brown   PetscErrorCode ierr;
1707552f7358SJed Brown 
1708552f7358SJed Brown   PetscFunctionBegin;
1709552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1710552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1711552f7358SJed Brown   {
1712552f7358SJed Brown     PetscInt dof;
1713552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1714552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1715552f7358SJed Brown   }
1716552f7358SJed Brown #endif
1717552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
17180d644c17SKarl Rupp 
1719552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1720552f7358SJed Brown   PetscFunctionReturn(0);
1721552f7358SJed Brown }
1722552f7358SJed Brown 
1723552f7358SJed Brown /*@
1724eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1725552f7358SJed Brown 
1726552f7358SJed Brown   Not collective
1727552f7358SJed Brown 
1728552f7358SJed Brown   Input Parameters:
1729552f7358SJed Brown + mesh - The DMPlex
1730eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1731552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1732552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1733552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1734552f7358SJed Brown                     the index of the cone point on which to start.
1735552f7358SJed Brown 
1736552f7358SJed Brown   Output Parameter:
1737552f7358SJed Brown 
1738552f7358SJed Brown   Note:
1739552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1740552f7358SJed Brown 
1741552f7358SJed Brown   Level: beginner
1742552f7358SJed Brown 
1743552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1744552f7358SJed Brown @*/
1745552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1746552f7358SJed Brown {
1747552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1748552f7358SJed Brown   PetscInt       pStart, pEnd;
1749552f7358SJed Brown   PetscInt       dof, off, c;
1750552f7358SJed Brown   PetscErrorCode ierr;
1751552f7358SJed Brown 
1752552f7358SJed Brown   PetscFunctionBegin;
1753552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1754552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1755552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1756552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1757552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
175882f516ccSBarry 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);
1759552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1760552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1761552f7358SJed Brown 
1762552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
176382f516ccSBarry 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);
1764552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1765552f7358SJed Brown   }
1766552f7358SJed Brown   PetscFunctionReturn(0);
1767552f7358SJed Brown }
1768552f7358SJed Brown 
17697cd05799SMatthew G. Knepley /*@
1770eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
17717cd05799SMatthew G. Knepley 
17727cd05799SMatthew G. Knepley   Not collective
17737cd05799SMatthew G. Knepley 
17747cd05799SMatthew G. Knepley   Input Parameters:
17757cd05799SMatthew G. Knepley + mesh - The DMPlex
1776eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17777cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
17787cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
17797cd05799SMatthew G. Knepley 
17807cd05799SMatthew G. Knepley   Level: beginner
17817cd05799SMatthew G. Knepley 
17827cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17837cd05799SMatthew G. Knepley @*/
1784552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1785552f7358SJed Brown {
1786552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1787552f7358SJed Brown   PetscInt       pStart, pEnd;
1788552f7358SJed Brown   PetscInt       dof, off;
1789552f7358SJed Brown   PetscErrorCode ierr;
1790552f7358SJed Brown 
1791552f7358SJed Brown   PetscFunctionBegin;
1792552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1793552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
179482f516ccSBarry 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);
179582f516ccSBarry 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);
179677c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
179777c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
179877c88f5bSMatthew 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);
1799552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1800552f7358SJed Brown   PetscFunctionReturn(0);
1801552f7358SJed Brown }
1802552f7358SJed Brown 
18037cd05799SMatthew G. Knepley /*@
1804eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
18057cd05799SMatthew G. Knepley 
18067cd05799SMatthew G. Knepley   Not collective
18077cd05799SMatthew G. Knepley 
18087cd05799SMatthew G. Knepley   Input Parameters:
18097cd05799SMatthew G. Knepley + mesh - The DMPlex
1810eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
18117cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
18127cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
18137cd05799SMatthew G. Knepley 
18147cd05799SMatthew G. Knepley   Level: beginner
18157cd05799SMatthew G. Knepley 
18167cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
18177cd05799SMatthew G. Knepley @*/
181877c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
181977c88f5bSMatthew G Knepley {
182077c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
182177c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
182277c88f5bSMatthew G Knepley   PetscInt       dof, off;
182377c88f5bSMatthew G Knepley   PetscErrorCode ierr;
182477c88f5bSMatthew G Knepley 
182577c88f5bSMatthew G Knepley   PetscFunctionBegin;
182677c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
182777c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
182877c88f5bSMatthew 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);
182977c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
183077c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
183177c88f5bSMatthew 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);
183277c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
183377c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
183477c88f5bSMatthew G Knepley }
183577c88f5bSMatthew G Knepley 
1836552f7358SJed Brown /*@
1837eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
1838552f7358SJed Brown 
1839552f7358SJed Brown   Not collective
1840552f7358SJed Brown 
1841552f7358SJed Brown   Input Parameters:
1842552f7358SJed Brown + mesh - The DMPlex
1843eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1844552f7358SJed Brown 
1845552f7358SJed Brown   Output Parameter:
1846552f7358SJed Brown . size - The support size for point p
1847552f7358SJed Brown 
1848552f7358SJed Brown   Level: beginner
1849552f7358SJed Brown 
1850552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1851552f7358SJed Brown @*/
1852552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1853552f7358SJed Brown {
1854552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1855552f7358SJed Brown   PetscErrorCode ierr;
1856552f7358SJed Brown 
1857552f7358SJed Brown   PetscFunctionBegin;
1858552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1859552f7358SJed Brown   PetscValidPointer(size, 3);
1860552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1861552f7358SJed Brown   PetscFunctionReturn(0);
1862552f7358SJed Brown }
1863552f7358SJed Brown 
1864552f7358SJed Brown /*@
1865eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
1866552f7358SJed Brown 
1867552f7358SJed Brown   Not collective
1868552f7358SJed Brown 
1869552f7358SJed Brown   Input Parameters:
1870552f7358SJed Brown + mesh - The DMPlex
1871eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1872552f7358SJed Brown - size - The support size for point p
1873552f7358SJed Brown 
1874552f7358SJed Brown   Output Parameter:
1875552f7358SJed Brown 
1876552f7358SJed Brown   Note:
1877552f7358SJed Brown   This should be called after DMPlexSetChart().
1878552f7358SJed Brown 
1879552f7358SJed Brown   Level: beginner
1880552f7358SJed Brown 
1881552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1882552f7358SJed Brown @*/
1883552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1884552f7358SJed Brown {
1885552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1886552f7358SJed Brown   PetscErrorCode ierr;
1887552f7358SJed Brown 
1888552f7358SJed Brown   PetscFunctionBegin;
1889552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1890552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
18910d644c17SKarl Rupp 
1892552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1893552f7358SJed Brown   PetscFunctionReturn(0);
1894552f7358SJed Brown }
1895552f7358SJed Brown 
1896552f7358SJed Brown /*@C
1897eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
1898552f7358SJed Brown 
1899552f7358SJed Brown   Not collective
1900552f7358SJed Brown 
1901552f7358SJed Brown   Input Parameters:
1902552f7358SJed Brown + mesh - The DMPlex
1903eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1904552f7358SJed Brown 
1905552f7358SJed Brown   Output Parameter:
1906552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1907552f7358SJed Brown 
1908552f7358SJed Brown   Level: beginner
1909552f7358SJed Brown 
19103813dfbdSMatthew G Knepley   Fortran Notes:
19113813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19123813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19133813dfbdSMatthew G Knepley 
19143813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
19153813dfbdSMatthew G Knepley 
1916552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1917552f7358SJed Brown @*/
1918552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1919552f7358SJed Brown {
1920552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1921552f7358SJed Brown   PetscInt       off;
1922552f7358SJed Brown   PetscErrorCode ierr;
1923552f7358SJed Brown 
1924552f7358SJed Brown   PetscFunctionBegin;
1925552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1926552f7358SJed Brown   PetscValidPointer(support, 3);
1927552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1928552f7358SJed Brown   *support = &mesh->supports[off];
1929552f7358SJed Brown   PetscFunctionReturn(0);
1930552f7358SJed Brown }
1931552f7358SJed Brown 
1932552f7358SJed Brown /*@
193392371b87SBarry 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
1934552f7358SJed Brown 
1935552f7358SJed Brown   Not collective
1936552f7358SJed Brown 
1937552f7358SJed Brown   Input Parameters:
1938552f7358SJed Brown + mesh - The DMPlex
1939eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
194092371b87SBarry Smith - support - An array of points which are on the out-edges for point p
1941552f7358SJed Brown 
1942552f7358SJed Brown   Output Parameter:
1943552f7358SJed Brown 
1944552f7358SJed Brown   Note:
1945552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1946552f7358SJed Brown 
1947552f7358SJed Brown   Level: beginner
1948552f7358SJed Brown 
194992371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1950552f7358SJed Brown @*/
1951552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1952552f7358SJed Brown {
1953552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1954552f7358SJed Brown   PetscInt       pStart, pEnd;
1955552f7358SJed Brown   PetscInt       dof, off, c;
1956552f7358SJed Brown   PetscErrorCode ierr;
1957552f7358SJed Brown 
1958552f7358SJed Brown   PetscFunctionBegin;
1959552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1960552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1961552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1962552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1963552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
196482f516ccSBarry 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);
1965552f7358SJed Brown   for (c = 0; c < dof; ++c) {
196682f516ccSBarry 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);
1967552f7358SJed Brown     mesh->supports[off+c] = support[c];
1968552f7358SJed Brown   }
1969552f7358SJed Brown   PetscFunctionReturn(0);
1970552f7358SJed Brown }
1971552f7358SJed Brown 
19727cd05799SMatthew G. Knepley /*@
1973eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
19747cd05799SMatthew G. Knepley 
19757cd05799SMatthew G. Knepley   Not collective
19767cd05799SMatthew G. Knepley 
19777cd05799SMatthew G. Knepley   Input Parameters:
19787cd05799SMatthew G. Knepley + mesh - The DMPlex
1979eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
19807cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
19817cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
19827cd05799SMatthew G. Knepley 
19837cd05799SMatthew G. Knepley   Level: beginner
19847cd05799SMatthew G. Knepley 
19857cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
19867cd05799SMatthew G. Knepley @*/
1987552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1988552f7358SJed Brown {
1989552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1990552f7358SJed Brown   PetscInt       pStart, pEnd;
1991552f7358SJed Brown   PetscInt       dof, off;
1992552f7358SJed Brown   PetscErrorCode ierr;
1993552f7358SJed Brown 
1994552f7358SJed Brown   PetscFunctionBegin;
1995552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1996552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1997552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1998552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
199982f516ccSBarry 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);
200082f516ccSBarry 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);
200182f516ccSBarry 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);
2002552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2003552f7358SJed Brown   PetscFunctionReturn(0);
2004552f7358SJed Brown }
2005552f7358SJed Brown 
2006552f7358SJed Brown /*@C
2007eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2008552f7358SJed Brown 
2009552f7358SJed Brown   Not collective
2010552f7358SJed Brown 
2011552f7358SJed Brown   Input Parameters:
2012552f7358SJed Brown + mesh - The DMPlex
2013eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2014552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
20150298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2016552f7358SJed Brown 
2017552f7358SJed Brown   Output Parameters:
2018552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2019552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2020552f7358SJed Brown 
2021552f7358SJed Brown   Note:
20220298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2023552f7358SJed Brown 
20243813dfbdSMatthew G Knepley   Fortran Notes:
20253813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20263813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20273813dfbdSMatthew G Knepley 
20283813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
20293813dfbdSMatthew G Knepley 
2030552f7358SJed Brown   Level: beginner
2031552f7358SJed Brown 
2032552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2033552f7358SJed Brown @*/
2034552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2035552f7358SJed Brown {
2036552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2037552f7358SJed Brown   PetscInt       *closure, *fifo;
20380298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2039552f7358SJed Brown   PetscInt        tmpSize, t;
2040552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2041552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2042552f7358SJed Brown   PetscErrorCode  ierr;
2043552f7358SJed Brown 
2044552f7358SJed Brown   PetscFunctionBegin;
2045552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2046552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2047552f7358SJed Brown   /* This is only 1-level */
2048552f7358SJed Brown   if (useCone) {
2049552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2050552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2051552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2052552f7358SJed Brown   } else {
2053552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2054552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2055552f7358SJed Brown   }
2056bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2057bfbcdd7aSMatthew G. Knepley     if (*points) {
2058bfbcdd7aSMatthew G. Knepley       closure = *points;
2059bfbcdd7aSMatthew G. Knepley     } else {
2060bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
206169291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2062bfbcdd7aSMatthew G. Knepley     }
2063bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2064bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2065bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2066bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2067bfbcdd7aSMatthew G. Knepley     }
2068bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2069bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2070bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2071bfbcdd7aSMatthew G. Knepley   }
207224c766afSToby Isaac   {
207324c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
207424c766afSToby Isaac 
207524c766afSToby Isaac     c = mesh->maxConeSize;
207624c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
207724c766afSToby Isaac     s = mesh->maxSupportSize;
207824c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
207924c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
208024c766afSToby Isaac   }
208169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2082bfbcdd7aSMatthew G. Knepley   if (*points) {
2083bfbcdd7aSMatthew G. Knepley     closure = *points;
2084bfbcdd7aSMatthew G. Knepley   } else {
208569291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2086bfbcdd7aSMatthew G. Knepley   }
2087bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2088552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2089552f7358SJed Brown     const PetscInt cp = tmp[t];
2090552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2091552f7358SJed Brown 
2092552f7358SJed Brown     closure[closureSize]   = cp;
2093552f7358SJed Brown     closure[closureSize+1] = co;
2094552f7358SJed Brown     fifo[fifoSize]         = cp;
2095552f7358SJed Brown     fifo[fifoSize+1]       = co;
2096552f7358SJed Brown   }
2097bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2098552f7358SJed Brown   while (fifoSize - fifoStart) {
2099552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2100552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2101552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2102552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2103552f7358SJed Brown 
2104552f7358SJed Brown     if (useCone) {
2105552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2106552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2107552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2108552f7358SJed Brown     } else {
2109552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2110552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
21110298fd71SBarry Smith       tmpO = NULL;
2112552f7358SJed Brown     }
2113552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2114552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2115552f7358SJed Brown       const PetscInt cp = tmp[i];
21162e1b13c2SMatthew 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. */
21172e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
21182e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
21192e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2120552f7358SJed Brown       PetscInt       c;
2121552f7358SJed Brown 
21222e1b13c2SMatthew G. Knepley       if (rev) {
21232e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
21242e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
21252e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
21262e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
21272e1b13c2SMatthew G. Knepley       }
2128552f7358SJed Brown       /* Check for duplicate */
2129552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2130552f7358SJed Brown         if (closure[c] == cp) break;
2131552f7358SJed Brown       }
2132552f7358SJed Brown       if (c == closureSize) {
2133552f7358SJed Brown         closure[closureSize]   = cp;
2134552f7358SJed Brown         closure[closureSize+1] = co;
2135552f7358SJed Brown         fifo[fifoSize]         = cp;
2136552f7358SJed Brown         fifo[fifoSize+1]       = co;
2137552f7358SJed Brown         closureSize           += 2;
2138552f7358SJed Brown         fifoSize              += 2;
2139552f7358SJed Brown       }
2140552f7358SJed Brown     }
2141552f7358SJed Brown     fifoStart += 2;
2142552f7358SJed Brown   }
2143552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2144552f7358SJed Brown   if (points)    *points    = closure;
214569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2146552f7358SJed Brown   PetscFunctionReturn(0);
2147552f7358SJed Brown }
2148552f7358SJed Brown 
21499bf0dad6SMatthew G. Knepley /*@C
2150eaf898f9SPatrick 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
21519bf0dad6SMatthew G. Knepley 
21529bf0dad6SMatthew G. Knepley   Not collective
21539bf0dad6SMatthew G. Knepley 
21549bf0dad6SMatthew G. Knepley   Input Parameters:
21559bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2156eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
21579bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
21589bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
21599bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
21609bf0dad6SMatthew G. Knepley 
21619bf0dad6SMatthew G. Knepley   Output Parameters:
21629bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
21639bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
21649bf0dad6SMatthew G. Knepley 
21659bf0dad6SMatthew G. Knepley   Note:
21669bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
21679bf0dad6SMatthew G. Knepley 
21689bf0dad6SMatthew G. Knepley   Fortran Notes:
21699bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21709bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
21719bf0dad6SMatthew G. Knepley 
21729bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
21739bf0dad6SMatthew G. Knepley 
21749bf0dad6SMatthew G. Knepley   Level: beginner
21759bf0dad6SMatthew G. Knepley 
21769bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
21779bf0dad6SMatthew G. Knepley @*/
21789bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
21799bf0dad6SMatthew G. Knepley {
21809bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
21819bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
21829bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
21839bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
21849bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
21859bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
21869bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
21879bf0dad6SMatthew G. Knepley 
21889bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
21899bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21909bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
21919bf0dad6SMatthew G. Knepley   /* This is only 1-level */
21929bf0dad6SMatthew G. Knepley   if (useCone) {
21939bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
21949bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
21959bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
21969bf0dad6SMatthew G. Knepley   } else {
21979bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
21989bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
21999bf0dad6SMatthew G. Knepley   }
22009bf0dad6SMatthew G. Knepley   if (depth == 1) {
22019bf0dad6SMatthew G. Knepley     if (*points) {
22029bf0dad6SMatthew G. Knepley       closure = *points;
22039bf0dad6SMatthew G. Knepley     } else {
22049bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
220569291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
22069bf0dad6SMatthew G. Knepley     }
22079bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
22089bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
22099bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
22109bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
22119bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
22129bf0dad6SMatthew G. Knepley     }
22139bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
22149bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
22159bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
22169bf0dad6SMatthew G. Knepley   }
221724c766afSToby Isaac   {
221824c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
221924c766afSToby Isaac 
222024c766afSToby Isaac     c = mesh->maxConeSize;
222124c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
222224c766afSToby Isaac     s = mesh->maxSupportSize;
222324c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
222424c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
222524c766afSToby Isaac   }
222669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
22279bf0dad6SMatthew G. Knepley   if (*points) {
22289bf0dad6SMatthew G. Knepley     closure = *points;
22299bf0dad6SMatthew G. Knepley   } else {
223069291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
22319bf0dad6SMatthew G. Knepley   }
22329bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
22339bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
22349bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
22359bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
22369bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
22379bf0dad6SMatthew G. Knepley 
22389bf0dad6SMatthew G. Knepley     if (ornt < 0) {
22399bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
22409bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
224186b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
22429bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
22439bf0dad6SMatthew G. Knepley     }
22449bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
22459bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
22469bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
22479bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
22489bf0dad6SMatthew G. Knepley   }
22499bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
22509bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
22519bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
22529bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
22539bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
22549bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
22559bf0dad6SMatthew G. Knepley 
22569bf0dad6SMatthew G. Knepley     if (useCone) {
22579bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
22589bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
22599bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
22609bf0dad6SMatthew G. Knepley     } else {
22619bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
22629bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
22639bf0dad6SMatthew G. Knepley       tmpO = NULL;
22649bf0dad6SMatthew G. Knepley     }
22659bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
22669bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
22679bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
22689bf0dad6SMatthew 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. */
22699bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
22709bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
22719bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
22729bf0dad6SMatthew G. Knepley       PetscInt       c;
22739bf0dad6SMatthew G. Knepley 
22749bf0dad6SMatthew G. Knepley       if (rev) {
22759bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
22769bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
22779bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
22789bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
22799bf0dad6SMatthew G. Knepley       }
22809bf0dad6SMatthew G. Knepley       /* Check for duplicate */
22819bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
22829bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
22839bf0dad6SMatthew G. Knepley       }
22849bf0dad6SMatthew G. Knepley       if (c == closureSize) {
22859bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
22869bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
22879bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
22889bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
22899bf0dad6SMatthew G. Knepley         closureSize           += 2;
22909bf0dad6SMatthew G. Knepley         fifoSize              += 2;
22919bf0dad6SMatthew G. Knepley       }
22929bf0dad6SMatthew G. Knepley     }
22939bf0dad6SMatthew G. Knepley     fifoStart += 2;
22949bf0dad6SMatthew G. Knepley   }
22959bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
22969bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
229769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
22989bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
22999bf0dad6SMatthew G. Knepley }
23009bf0dad6SMatthew G. Knepley 
2301552f7358SJed Brown /*@C
2302eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2303552f7358SJed Brown 
2304552f7358SJed Brown   Not collective
2305552f7358SJed Brown 
2306552f7358SJed Brown   Input Parameters:
2307552f7358SJed Brown + mesh - The DMPlex
2308eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2309552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2310e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2311e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2312552f7358SJed Brown 
2313552f7358SJed Brown   Note:
23140298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2315552f7358SJed Brown 
23163813dfbdSMatthew G Knepley   Fortran Notes:
23173813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23183813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23193813dfbdSMatthew G Knepley 
23203813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23213813dfbdSMatthew G Knepley 
2322552f7358SJed Brown   Level: beginner
2323552f7358SJed Brown 
2324552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2325552f7358SJed Brown @*/
2326552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2327552f7358SJed Brown {
2328552f7358SJed Brown   PetscErrorCode ierr;
2329552f7358SJed Brown 
2330552f7358SJed Brown   PetscFunctionBegin;
2331552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2332e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2333e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
233469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
23354ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2336552f7358SJed Brown   PetscFunctionReturn(0);
2337552f7358SJed Brown }
2338552f7358SJed Brown 
2339552f7358SJed Brown /*@
2340eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2341552f7358SJed Brown 
2342552f7358SJed Brown   Not collective
2343552f7358SJed Brown 
2344552f7358SJed Brown   Input Parameter:
2345552f7358SJed Brown . mesh - The DMPlex
2346552f7358SJed Brown 
2347552f7358SJed Brown   Output Parameters:
2348552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2349552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2350552f7358SJed Brown 
2351552f7358SJed Brown   Level: beginner
2352552f7358SJed Brown 
2353552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2354552f7358SJed Brown @*/
2355552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2356552f7358SJed Brown {
2357552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2358552f7358SJed Brown 
2359552f7358SJed Brown   PetscFunctionBegin;
2360552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2361552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2362552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2363552f7358SJed Brown   PetscFunctionReturn(0);
2364552f7358SJed Brown }
2365552f7358SJed Brown 
2366552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2367552f7358SJed Brown {
2368552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2369552f7358SJed Brown   PetscInt       size;
2370552f7358SJed Brown   PetscErrorCode ierr;
2371552f7358SJed Brown 
2372552f7358SJed Brown   PetscFunctionBegin;
2373552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2374552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2375552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
23761795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
23771795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2378552f7358SJed Brown   if (mesh->maxSupportSize) {
2379552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2380552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
23811795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2382552f7358SJed Brown   }
2383552f7358SJed Brown   PetscFunctionReturn(0);
2384552f7358SJed Brown }
2385552f7358SJed Brown 
2386276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2387552f7358SJed Brown {
2388552f7358SJed Brown   PetscErrorCode ierr;
2389552f7358SJed Brown 
2390552f7358SJed Brown   PetscFunctionBegin;
23914d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2392792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2393c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2394736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2395f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2396f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2397f94b4a02SBlaise Bourdin 
23983dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
23993dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2400e87a4003SBarry Smith     ierr = DMGetSection((*subdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
2401f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2402f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2403f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2404f94b4a02SBlaise Bourdin 
2405f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2406c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2407f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2408f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2409f94b4a02SBlaise Bourdin   }
2410552f7358SJed Brown   PetscFunctionReturn(0);
2411552f7358SJed Brown }
2412552f7358SJed Brown 
24132adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
24142adcc780SMatthew G. Knepley {
24152adcc780SMatthew G. Knepley   PetscErrorCode ierr;
24163dcd263cSBlaise Bourdin   PetscInt       i = 0;
24172adcc780SMatthew G. Knepley 
24182adcc780SMatthew G. Knepley   PetscFunctionBegin;
2419435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2420792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
2421c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
24223dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
24233dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
24243dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
24253dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
24263dcd263cSBlaise Bourdin 
24273dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
24283dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2429c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
2430cf0b7c11SKarl Rupp       ierr = DMGetSection((*superdm), &section);CHKERRQ(ierr);
24313dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
24323dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
24333dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
24343dcd263cSBlaise Bourdin 
24353dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2436c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
24373dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
24383dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
24393dcd263cSBlaise Bourdin       break;
24403dcd263cSBlaise Bourdin     }
24413dcd263cSBlaise Bourdin   }
24422adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
24432adcc780SMatthew G. Knepley }
24442adcc780SMatthew G. Knepley 
2445552f7358SJed Brown /*@
2446eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2447552f7358SJed Brown 
2448552f7358SJed Brown   Not collective
2449552f7358SJed Brown 
2450552f7358SJed Brown   Input Parameter:
2451552f7358SJed Brown . mesh - The DMPlex
2452552f7358SJed Brown 
2453552f7358SJed Brown   Output Parameter:
2454552f7358SJed Brown 
2455552f7358SJed Brown   Note:
2456552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2457552f7358SJed Brown 
2458552f7358SJed Brown   Level: beginner
2459552f7358SJed Brown 
2460552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2461552f7358SJed Brown @*/
2462552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2463552f7358SJed Brown {
2464552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2465552f7358SJed Brown   PetscInt      *offsets;
2466552f7358SJed Brown   PetscInt       supportSize;
2467552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2468552f7358SJed Brown   PetscErrorCode ierr;
2469552f7358SJed Brown 
2470552f7358SJed Brown   PetscFunctionBegin;
2471552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
247282f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2473552f7358SJed Brown   /* Calculate support sizes */
2474552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2475552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2476552f7358SJed Brown     PetscInt dof, off, c;
2477552f7358SJed Brown 
2478552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2479552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2480552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2481552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2482552f7358SJed Brown     }
2483552f7358SJed Brown   }
2484552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2485552f7358SJed Brown     PetscInt dof;
2486552f7358SJed Brown 
2487552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
24880d644c17SKarl Rupp 
2489552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2490552f7358SJed Brown   }
2491552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2492552f7358SJed Brown   /* Calculate supports */
2493552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
24941795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
24951795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2496552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2497552f7358SJed Brown     PetscInt dof, off, c;
2498552f7358SJed Brown 
2499552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2500552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2501552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2502552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2503552f7358SJed Brown       PetscInt       offS;
2504552f7358SJed Brown 
2505552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
25060d644c17SKarl Rupp 
2507552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2508552f7358SJed Brown       ++offsets[q];
2509552f7358SJed Brown     }
2510552f7358SJed Brown   }
2511552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2512552f7358SJed Brown   PetscFunctionReturn(0);
2513552f7358SJed Brown }
2514552f7358SJed Brown 
25157d5acc75SStefano Zampini static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);
25167d5acc75SStefano Zampini 
2517552f7358SJed Brown /*@
2518eaf898f9SPatrick Sanan   DMPlexStratify - The DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2519eaf898f9SPatrick Sanan   can be illustrated by a Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2520552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2521552f7358SJed Brown   the DAG.
2522552f7358SJed Brown 
2523bf4602e4SToby Isaac   Collective on dm
2524552f7358SJed Brown 
2525552f7358SJed Brown   Input Parameter:
2526552f7358SJed Brown . mesh - The DMPlex
2527552f7358SJed Brown 
2528552f7358SJed Brown   Output Parameter:
2529552f7358SJed Brown 
2530552f7358SJed Brown   Notes:
2531150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2532150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2533c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2534150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2535552f7358SJed Brown 
2536150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2537552f7358SJed Brown 
2538552f7358SJed Brown   Level: beginner
2539552f7358SJed Brown 
2540552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2541552f7358SJed Brown @*/
2542552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2543552f7358SJed Brown {
2544df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2545aa50250dSMatthew G. Knepley   DMLabel        label;
2546552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2547552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
25487d5acc75SStefano Zampini   PetscInt       cMax, fMax, eMax, vMax;
2549552f7358SJed Brown   PetscErrorCode ierr;
2550552f7358SJed Brown 
2551552f7358SJed Brown   PetscFunctionBegin;
2552552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2553552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2554552f7358SJed Brown   /* Calculate depth */
2555aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2556c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2557aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2558552f7358SJed Brown   /* Initialize roots and count leaves */
2559552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2560552f7358SJed Brown     PetscInt coneSize, supportSize;
2561552f7358SJed Brown 
2562552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2563552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2564552f7358SJed Brown     if (!coneSize && supportSize) {
2565552f7358SJed Brown       ++numRoots;
2566aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2567552f7358SJed Brown     } else if (!supportSize && coneSize) {
2568552f7358SJed Brown       ++numLeaves;
2569552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2570552f7358SJed Brown       /* Isolated points */
2571aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2572552f7358SJed Brown     }
2573552f7358SJed Brown   }
2574552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2575552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2576552f7358SJed Brown       PetscInt coneSize, supportSize;
2577552f7358SJed Brown 
2578552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2579552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2580552f7358SJed Brown       if (!supportSize && coneSize) {
2581aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2582552f7358SJed Brown       }
2583552f7358SJed Brown     }
2584552f7358SJed Brown   } else {
258574ef644bSMatthew G. Knepley     IS       pointIS;
258674ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2587552f7358SJed Brown 
258874ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
258974ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
259074ef644bSMatthew G. Knepley     while (numPoints) {
259174ef644bSMatthew G. Knepley       const PetscInt *points;
259274ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
259374ef644bSMatthew G. Knepley 
259474ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
259574ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
259674ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
259774ef644bSMatthew G. Knepley         const PetscInt *support;
259874ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
259974ef644bSMatthew G. Knepley 
260074ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
260174ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
260274ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
260374ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2604552f7358SJed Brown         }
2605552f7358SJed Brown       }
26065edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
260774ef644bSMatthew G. Knepley       ++level;
260874ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
260974ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
261074ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
261174ef644bSMatthew G. Knepley       else         {numPoints = 0;}
261274ef644bSMatthew G. Knepley     }
261374ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
261474ef644bSMatthew G. Knepley   }
2615bf4602e4SToby Isaac   { /* just in case there is an empty process */
2616bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2617bf4602e4SToby Isaac 
2618bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
26194de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
26204de306b1SToby Isaac       IS pointIS;
26214de306b1SToby Isaac 
26224de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
26234de306b1SToby Isaac       if (pointIS) {
26244de306b1SToby Isaac         PetscInt  min, max, numPoints;
26254de306b1SToby Isaac         PetscInt  start;
26264de306b1SToby Isaac         PetscBool contig;
26274de306b1SToby Isaac 
26284de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
26294de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
26304de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
26314de306b1SToby Isaac         if (start == 0 && contig) {
26324de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
26334de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
26344de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
26354de306b1SToby Isaac         }
26364de306b1SToby Isaac       }
26374de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
26384de306b1SToby Isaac     }
26396d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2640bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2641367003a6SStefano Zampini       ierr = DMLabelAddStratum(label,v);CHKERRQ(ierr);
2642bf4602e4SToby Isaac     }
2643bf4602e4SToby Isaac   }
2644d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
26457d5acc75SStefano Zampini 
26467d5acc75SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
26477d5acc75SStefano Zampini   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
26487d5acc75SStefano Zampini     DMLabel  dimLabel;
26497d5acc75SStefano Zampini     PetscInt dim;
26507d5acc75SStefano Zampini 
26517d5acc75SStefano Zampini     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
26527d5acc75SStefano Zampini     ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
26537d5acc75SStefano Zampini     if (!dimLabel) {
26547d5acc75SStefano Zampini       ierr = DMCreateLabel(dm, "dim");CHKERRQ(ierr);
26557d5acc75SStefano Zampini       ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
26567d5acc75SStefano Zampini     }
26577d5acc75SStefano Zampini     if (cMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);CHKERRQ(ierr);}
26587d5acc75SStefano Zampini     if (fMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);CHKERRQ(ierr);}
26597d5acc75SStefano Zampini     if (eMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);CHKERRQ(ierr);}
26607d5acc75SStefano Zampini     if (vMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);CHKERRQ(ierr);}
26617d5acc75SStefano Zampini   }
2662552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2663552f7358SJed Brown   PetscFunctionReturn(0);
2664552f7358SJed Brown }
2665552f7358SJed Brown 
2666552f7358SJed Brown /*@C
2667552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2668552f7358SJed Brown 
2669552f7358SJed Brown   Not Collective
2670552f7358SJed Brown 
2671552f7358SJed Brown   Input Parameters:
2672552f7358SJed Brown + dm - The DMPlex object
2673552f7358SJed Brown . numPoints - The number of input points for the join
2674552f7358SJed Brown - points - The input points
2675552f7358SJed Brown 
2676552f7358SJed Brown   Output Parameters:
2677552f7358SJed Brown + numCoveredPoints - The number of points in the join
2678552f7358SJed Brown - coveredPoints - The points in the join
2679552f7358SJed Brown 
2680552f7358SJed Brown   Level: intermediate
2681552f7358SJed Brown 
2682552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2683552f7358SJed Brown 
26843813dfbdSMatthew G Knepley   Fortran Notes:
26853813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26863813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26873813dfbdSMatthew G Knepley 
26883813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26893813dfbdSMatthew G Knepley 
2690552f7358SJed Brown .keywords: mesh
2691552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2692552f7358SJed Brown @*/
2693552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2694552f7358SJed Brown {
2695552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2696552f7358SJed Brown   PetscInt      *join[2];
2697552f7358SJed Brown   PetscInt       joinSize, i = 0;
2698552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2699552f7358SJed Brown   PetscErrorCode ierr;
2700552f7358SJed Brown 
2701552f7358SJed Brown   PetscFunctionBegin;
2702552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
270348bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
270448bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
270548bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
270669291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
270769291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2708552f7358SJed Brown   /* Copy in support of first point */
2709552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2710552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2711552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2712552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2713552f7358SJed Brown   }
2714552f7358SJed Brown   /* Check each successive support */
2715552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2716552f7358SJed Brown     PetscInt newJoinSize = 0;
2717552f7358SJed Brown 
2718552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2719552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2720552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2721552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2722552f7358SJed Brown 
2723552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2724552f7358SJed Brown         if (point == join[i][m]) {
2725552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2726552f7358SJed Brown           break;
2727552f7358SJed Brown         }
2728552f7358SJed Brown       }
2729552f7358SJed Brown     }
2730552f7358SJed Brown     joinSize = newJoinSize;
2731552f7358SJed Brown     i        = 1-i;
2732552f7358SJed Brown   }
2733552f7358SJed Brown   *numCoveredPoints = joinSize;
2734552f7358SJed Brown   *coveredPoints    = join[i];
273569291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2736552f7358SJed Brown   PetscFunctionReturn(0);
2737552f7358SJed Brown }
2738552f7358SJed Brown 
2739552f7358SJed Brown /*@C
2740552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2741552f7358SJed Brown 
2742552f7358SJed Brown   Not Collective
2743552f7358SJed Brown 
2744552f7358SJed Brown   Input Parameters:
2745552f7358SJed Brown + dm - The DMPlex object
2746552f7358SJed Brown . numPoints - The number of input points for the join
2747552f7358SJed Brown - points - The input points
2748552f7358SJed Brown 
2749552f7358SJed Brown   Output Parameters:
2750552f7358SJed Brown + numCoveredPoints - The number of points in the join
2751552f7358SJed Brown - coveredPoints - The points in the join
2752552f7358SJed Brown 
27533813dfbdSMatthew G Knepley   Fortran Notes:
27543813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27553813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27563813dfbdSMatthew G Knepley 
27573813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27583813dfbdSMatthew G Knepley 
2759552f7358SJed Brown   Level: intermediate
2760552f7358SJed Brown 
2761552f7358SJed Brown .keywords: mesh
2762552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2763552f7358SJed Brown @*/
2764552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2765552f7358SJed Brown {
2766552f7358SJed Brown   PetscErrorCode ierr;
2767552f7358SJed Brown 
2768552f7358SJed Brown   PetscFunctionBegin;
2769552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2770d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2771d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2772d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
277369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2774d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2775552f7358SJed Brown   PetscFunctionReturn(0);
2776552f7358SJed Brown }
2777552f7358SJed Brown 
2778552f7358SJed Brown /*@C
2779552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2780552f7358SJed Brown 
2781552f7358SJed Brown   Not Collective
2782552f7358SJed Brown 
2783552f7358SJed Brown   Input Parameters:
2784552f7358SJed Brown + dm - The DMPlex object
2785552f7358SJed Brown . numPoints - The number of input points for the join
2786552f7358SJed Brown - points - The input points
2787552f7358SJed Brown 
2788552f7358SJed Brown   Output Parameters:
2789552f7358SJed Brown + numCoveredPoints - The number of points in the join
2790552f7358SJed Brown - coveredPoints - The points in the join
2791552f7358SJed Brown 
27923813dfbdSMatthew G Knepley   Fortran Notes:
27933813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27943813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27953813dfbdSMatthew G Knepley 
27963813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27973813dfbdSMatthew G Knepley 
2798552f7358SJed Brown   Level: intermediate
2799552f7358SJed Brown 
2800552f7358SJed Brown .keywords: mesh
2801552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2802552f7358SJed Brown @*/
2803552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2804552f7358SJed Brown {
2805552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2806552f7358SJed Brown   PetscInt      *offsets, **closures;
2807552f7358SJed Brown   PetscInt      *join[2];
2808552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
280924c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2810552f7358SJed Brown   PetscErrorCode ierr;
2811552f7358SJed Brown 
2812552f7358SJed Brown   PetscFunctionBegin;
2813552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
281448bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
281548bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
281648bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
2817552f7358SJed Brown 
2818552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
28191795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
282069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
282124c766afSToby Isaac   ms      = mesh->maxSupportSize;
282224c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
282369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
282469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2825552f7358SJed Brown 
2826552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2827552f7358SJed Brown     PetscInt closureSize;
2828552f7358SJed Brown 
2829552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
28300d644c17SKarl Rupp 
2831552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2832552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2833552f7358SJed Brown       PetscInt pStart, pEnd, i;
2834552f7358SJed Brown 
2835552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2836552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2837552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2838552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2839552f7358SJed Brown           break;
2840552f7358SJed Brown         }
2841552f7358SJed Brown       }
2842552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2843552f7358SJed Brown     }
284482f516ccSBarry 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);
2845552f7358SJed Brown   }
2846552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2847552f7358SJed Brown     PetscInt dof;
2848552f7358SJed Brown 
2849552f7358SJed Brown     /* Copy in support of first point */
2850552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2851552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2852552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2853552f7358SJed Brown     }
2854552f7358SJed Brown     /* Check each successive cone */
2855552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2856552f7358SJed Brown       PetscInt newJoinSize = 0;
2857552f7358SJed Brown 
2858552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2859552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2860552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2861552f7358SJed Brown 
2862552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2863552f7358SJed Brown           if (point == join[i][m]) {
2864552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2865552f7358SJed Brown             break;
2866552f7358SJed Brown           }
2867552f7358SJed Brown         }
2868552f7358SJed Brown       }
2869552f7358SJed Brown       joinSize = newJoinSize;
2870552f7358SJed Brown       i        = 1-i;
2871552f7358SJed Brown     }
2872552f7358SJed Brown     if (joinSize) break;
2873552f7358SJed Brown   }
2874552f7358SJed Brown   *numCoveredPoints = joinSize;
2875552f7358SJed Brown   *coveredPoints    = join[i];
2876552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
28770298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2878552f7358SJed Brown   }
2879552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
288069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
288169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2882552f7358SJed Brown   PetscFunctionReturn(0);
2883552f7358SJed Brown }
2884552f7358SJed Brown 
2885552f7358SJed Brown /*@C
2886552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2887552f7358SJed Brown 
2888552f7358SJed Brown   Not Collective
2889552f7358SJed Brown 
2890552f7358SJed Brown   Input Parameters:
2891552f7358SJed Brown + dm - The DMPlex object
2892552f7358SJed Brown . numPoints - The number of input points for the meet
2893552f7358SJed Brown - points - The input points
2894552f7358SJed Brown 
2895552f7358SJed Brown   Output Parameters:
2896552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2897552f7358SJed Brown - coveredPoints - The points in the meet
2898552f7358SJed Brown 
2899552f7358SJed Brown   Level: intermediate
2900552f7358SJed Brown 
2901552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2902552f7358SJed Brown 
29033813dfbdSMatthew G Knepley   Fortran Notes:
29043813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29053813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29063813dfbdSMatthew G Knepley 
29073813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29083813dfbdSMatthew G Knepley 
2909552f7358SJed Brown .keywords: mesh
2910552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2911552f7358SJed Brown @*/
2912552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2913552f7358SJed Brown {
2914552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2915552f7358SJed Brown   PetscInt      *meet[2];
2916552f7358SJed Brown   PetscInt       meetSize, i = 0;
2917552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2918552f7358SJed Brown   PetscErrorCode ierr;
2919552f7358SJed Brown 
2920552f7358SJed Brown   PetscFunctionBegin;
2921552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2922552f7358SJed Brown   PetscValidPointer(points, 2);
2923552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2924552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
292569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
292669291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2927552f7358SJed Brown   /* Copy in cone of first point */
2928552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2929552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2930552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2931552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2932552f7358SJed Brown   }
2933552f7358SJed Brown   /* Check each successive cone */
2934552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2935552f7358SJed Brown     PetscInt newMeetSize = 0;
2936552f7358SJed Brown 
2937552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2938552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2939552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2940552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2941552f7358SJed Brown 
2942552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2943552f7358SJed Brown         if (point == meet[i][m]) {
2944552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2945552f7358SJed Brown           break;
2946552f7358SJed Brown         }
2947552f7358SJed Brown       }
2948552f7358SJed Brown     }
2949552f7358SJed Brown     meetSize = newMeetSize;
2950552f7358SJed Brown     i        = 1-i;
2951552f7358SJed Brown   }
2952552f7358SJed Brown   *numCoveringPoints = meetSize;
2953552f7358SJed Brown   *coveringPoints    = meet[i];
295469291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2955552f7358SJed Brown   PetscFunctionReturn(0);
2956552f7358SJed Brown }
2957552f7358SJed Brown 
2958552f7358SJed Brown /*@C
2959552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2960552f7358SJed Brown 
2961552f7358SJed Brown   Not Collective
2962552f7358SJed Brown 
2963552f7358SJed Brown   Input Parameters:
2964552f7358SJed Brown + dm - The DMPlex object
2965552f7358SJed Brown . numPoints - The number of input points for the meet
2966552f7358SJed Brown - points - The input points
2967552f7358SJed Brown 
2968552f7358SJed Brown   Output Parameters:
2969552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2970552f7358SJed Brown - coveredPoints - The points in the meet
2971552f7358SJed Brown 
2972552f7358SJed Brown   Level: intermediate
2973552f7358SJed Brown 
29743813dfbdSMatthew G Knepley   Fortran Notes:
29753813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29763813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29773813dfbdSMatthew G Knepley 
29783813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29793813dfbdSMatthew G Knepley 
2980552f7358SJed Brown .keywords: mesh
2981552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2982552f7358SJed Brown @*/
2983552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2984552f7358SJed Brown {
2985552f7358SJed Brown   PetscErrorCode ierr;
2986552f7358SJed Brown 
2987552f7358SJed Brown   PetscFunctionBegin;
2988552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2989d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2990d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2991d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
299269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2993d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2994552f7358SJed Brown   PetscFunctionReturn(0);
2995552f7358SJed Brown }
2996552f7358SJed Brown 
2997552f7358SJed Brown /*@C
2998552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2999552f7358SJed Brown 
3000552f7358SJed Brown   Not Collective
3001552f7358SJed Brown 
3002552f7358SJed Brown   Input Parameters:
3003552f7358SJed Brown + dm - The DMPlex object
3004552f7358SJed Brown . numPoints - The number of input points for the meet
3005552f7358SJed Brown - points - The input points
3006552f7358SJed Brown 
3007552f7358SJed Brown   Output Parameters:
3008552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3009552f7358SJed Brown - coveredPoints - The points in the meet
3010552f7358SJed Brown 
3011552f7358SJed Brown   Level: intermediate
3012552f7358SJed Brown 
30133813dfbdSMatthew G Knepley   Fortran Notes:
30143813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
30153813dfbdSMatthew G Knepley   include petsc.h90 in your code.
30163813dfbdSMatthew G Knepley 
30173813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
30183813dfbdSMatthew G Knepley 
3019552f7358SJed Brown .keywords: mesh
3020552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3021552f7358SJed Brown @*/
3022552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3023552f7358SJed Brown {
3024552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3025552f7358SJed Brown   PetscInt      *offsets, **closures;
3026552f7358SJed Brown   PetscInt      *meet[2];
3027552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
302824c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3029552f7358SJed Brown   PetscErrorCode ierr;
3030552f7358SJed Brown 
3031552f7358SJed Brown   PetscFunctionBegin;
3032552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3033552f7358SJed Brown   PetscValidPointer(points, 2);
3034552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3035552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3036552f7358SJed Brown 
3037552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3038785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
303969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
304024c766afSToby Isaac   mc      = mesh->maxConeSize;
304124c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
304269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
304369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3044552f7358SJed Brown 
3045552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3046552f7358SJed Brown     PetscInt closureSize;
3047552f7358SJed Brown 
3048552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
30490d644c17SKarl Rupp 
3050552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3051552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3052552f7358SJed Brown       PetscInt pStart, pEnd, i;
3053552f7358SJed Brown 
3054552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3055552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3056552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3057552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3058552f7358SJed Brown           break;
3059552f7358SJed Brown         }
3060552f7358SJed Brown       }
3061552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3062552f7358SJed Brown     }
306382f516ccSBarry 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);
3064552f7358SJed Brown   }
3065552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3066552f7358SJed Brown     PetscInt dof;
3067552f7358SJed Brown 
3068552f7358SJed Brown     /* Copy in cone of first point */
3069552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3070552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3071552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3072552f7358SJed Brown     }
3073552f7358SJed Brown     /* Check each successive cone */
3074552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3075552f7358SJed Brown       PetscInt newMeetSize = 0;
3076552f7358SJed Brown 
3077552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3078552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3079552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3080552f7358SJed Brown 
3081552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3082552f7358SJed Brown           if (point == meet[i][m]) {
3083552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3084552f7358SJed Brown             break;
3085552f7358SJed Brown           }
3086552f7358SJed Brown         }
3087552f7358SJed Brown       }
3088552f7358SJed Brown       meetSize = newMeetSize;
3089552f7358SJed Brown       i        = 1-i;
3090552f7358SJed Brown     }
3091552f7358SJed Brown     if (meetSize) break;
3092552f7358SJed Brown   }
3093552f7358SJed Brown   *numCoveredPoints = meetSize;
3094552f7358SJed Brown   *coveredPoints    = meet[i];
3095552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
30960298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3097552f7358SJed Brown   }
3098552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
309969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
310069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3101552f7358SJed Brown   PetscFunctionReturn(0);
3102552f7358SJed Brown }
3103552f7358SJed Brown 
31044e3744c5SMatthew G. Knepley /*@C
31054e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
31064e3744c5SMatthew G. Knepley 
31074e3744c5SMatthew G. Knepley   Not Collective
31084e3744c5SMatthew G. Knepley 
31094e3744c5SMatthew G. Knepley   Input Parameters:
31104e3744c5SMatthew G. Knepley + dmA - A DMPlex object
31114e3744c5SMatthew G. Knepley - dmB - A DMPlex object
31124e3744c5SMatthew G. Knepley 
31134e3744c5SMatthew G. Knepley   Output Parameters:
31144e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
31154e3744c5SMatthew G. Knepley 
31164e3744c5SMatthew G. Knepley   Level: intermediate
31174e3744c5SMatthew G. Knepley 
31184e3744c5SMatthew G. Knepley   Notes:
31194e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
31204e3744c5SMatthew G. Knepley 
31214e3744c5SMatthew G. Knepley .keywords: mesh
31224e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
31234e3744c5SMatthew G. Knepley @*/
31244e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
31254e3744c5SMatthew G. Knepley {
31264e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
31274e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
31284e3744c5SMatthew G. Knepley 
31294e3744c5SMatthew G. Knepley   PetscFunctionBegin;
31304e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
31314e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
31324e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
31334e3744c5SMatthew G. Knepley 
31344e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
31354e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
31364e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
31374e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
31384e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
31394e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
31404e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
31414e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
31424e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
31434e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
31444e3744c5SMatthew G. Knepley 
31454e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
31464e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
31474e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
31484e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
31494e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
31504e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
31514e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
31524e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
31534e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
31544e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
31554e3744c5SMatthew G. Knepley     }
31564e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
31574e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
31584e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
31594e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
31604e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
31614e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
31624e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
31634e3744c5SMatthew G. Knepley     }
31644e3744c5SMatthew G. Knepley   }
31654e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
31664e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
31674e3744c5SMatthew G. Knepley }
31684e3744c5SMatthew G. Knepley 
31697cd05799SMatthew G. Knepley /*@C
31707cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
31717cd05799SMatthew G. Knepley 
31727cd05799SMatthew G. Knepley   Not Collective
31737cd05799SMatthew G. Knepley 
31747cd05799SMatthew G. Knepley   Input Parameters:
31757cd05799SMatthew G. Knepley + dm         - The DMPlex
31767cd05799SMatthew G. Knepley . cellDim    - The cell dimension
31777cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
31787cd05799SMatthew G. Knepley 
31797cd05799SMatthew G. Knepley   Output Parameters:
31807cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
31817cd05799SMatthew G. Knepley 
31827cd05799SMatthew G. Knepley   Level: developer
31837cd05799SMatthew G. Knepley 
31847cd05799SMatthew G. Knepley   Notes:
31857cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
31867cd05799SMatthew G. Knepley 
31877cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
31887cd05799SMatthew G. Knepley @*/
318918ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3190a6dfd86eSKarl Rupp {
319182f516ccSBarry Smith   MPI_Comm       comm;
3192552f7358SJed Brown   PetscErrorCode ierr;
3193552f7358SJed Brown 
3194552f7358SJed Brown   PetscFunctionBegin;
319582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3196552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3197552f7358SJed Brown   switch (cellDim) {
3198552f7358SJed Brown   case 0:
3199552f7358SJed Brown     *numFaceVertices = 0;
3200552f7358SJed Brown     break;
3201552f7358SJed Brown   case 1:
3202552f7358SJed Brown     *numFaceVertices = 1;
3203552f7358SJed Brown     break;
3204552f7358SJed Brown   case 2:
3205552f7358SJed Brown     switch (numCorners) {
320619436ca2SJed Brown     case 3: /* triangle */
320719436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3208552f7358SJed Brown       break;
320919436ca2SJed Brown     case 4: /* quadrilateral */
321019436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3211552f7358SJed Brown       break;
321219436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
321319436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3214552f7358SJed Brown       break;
321519436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
321619436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3217552f7358SJed Brown       break;
3218552f7358SJed Brown     default:
3219f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3220552f7358SJed Brown     }
3221552f7358SJed Brown     break;
3222552f7358SJed Brown   case 3:
3223552f7358SJed Brown     switch (numCorners) {
322419436ca2SJed Brown     case 4: /* tetradehdron */
322519436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3226552f7358SJed Brown       break;
322719436ca2SJed Brown     case 6: /* tet cohesive cells */
322819436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3229552f7358SJed Brown       break;
323019436ca2SJed Brown     case 8: /* hexahedron */
323119436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3232552f7358SJed Brown       break;
323319436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
323419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3235552f7358SJed Brown       break;
323619436ca2SJed Brown     case 10: /* quadratic tetrahedron */
323719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3238552f7358SJed Brown       break;
323919436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
324019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3241552f7358SJed Brown       break;
324219436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
324319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3244552f7358SJed Brown       break;
324519436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
324619436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3247552f7358SJed Brown       break;
3248552f7358SJed Brown     default:
3249f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3250552f7358SJed Brown     }
3251552f7358SJed Brown     break;
3252552f7358SJed Brown   default:
3253f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3254552f7358SJed Brown   }
3255552f7358SJed Brown   PetscFunctionReturn(0);
3256552f7358SJed Brown }
3257552f7358SJed Brown 
3258552f7358SJed Brown /*@
3259aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3260552f7358SJed Brown 
3261552f7358SJed Brown   Not Collective
3262552f7358SJed Brown 
3263aa50250dSMatthew G. Knepley   Input Parameter:
3264552f7358SJed Brown . dm    - The DMPlex object
3265552f7358SJed Brown 
3266aa50250dSMatthew G. Knepley   Output Parameter:
3267aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3268552f7358SJed Brown 
3269552f7358SJed Brown   Level: developer
3270552f7358SJed Brown 
3271aa50250dSMatthew G. Knepley .keywords: mesh, points
3272aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3273aa50250dSMatthew G. Knepley @*/
3274aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3275aa50250dSMatthew G. Knepley {
3276aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
3277aa50250dSMatthew G. Knepley 
3278aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3279aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3280aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3281c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
3282c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3283aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3284aa50250dSMatthew G. Knepley }
3285aa50250dSMatthew G. Knepley 
3286aa50250dSMatthew G. Knepley /*@
3287aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3288aa50250dSMatthew G. Knepley 
3289aa50250dSMatthew G. Knepley   Not Collective
3290aa50250dSMatthew G. Knepley 
3291aa50250dSMatthew G. Knepley   Input Parameter:
3292aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3293aa50250dSMatthew G. Knepley 
3294aa50250dSMatthew G. Knepley   Output Parameter:
3295aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3296aa50250dSMatthew G. Knepley 
3297aa50250dSMatthew G. Knepley   Level: developer
3298552f7358SJed Brown 
3299552f7358SJed Brown .keywords: mesh, points
3300aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3301552f7358SJed Brown @*/
3302552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3303552f7358SJed Brown {
3304aa50250dSMatthew G. Knepley   DMLabel        label;
3305aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3306552f7358SJed Brown   PetscErrorCode ierr;
3307552f7358SJed Brown 
3308552f7358SJed Brown   PetscFunctionBegin;
3309552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3310552f7358SJed Brown   PetscValidPointer(depth, 2);
3311aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3312aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3313552f7358SJed Brown   *depth = d-1;
3314552f7358SJed Brown   PetscFunctionReturn(0);
3315552f7358SJed Brown }
3316552f7358SJed Brown 
3317552f7358SJed Brown /*@
3318552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3319552f7358SJed Brown 
3320552f7358SJed Brown   Not Collective
3321552f7358SJed Brown 
3322552f7358SJed Brown   Input Parameters:
3323552f7358SJed Brown + dm           - The DMPlex object
3324552f7358SJed Brown - stratumValue - The requested depth
3325552f7358SJed Brown 
3326552f7358SJed Brown   Output Parameters:
3327552f7358SJed Brown + start - The first point at this depth
3328552f7358SJed Brown - end   - One beyond the last point at this depth
3329552f7358SJed Brown 
3330552f7358SJed Brown   Level: developer
3331552f7358SJed Brown 
3332552f7358SJed Brown .keywords: mesh, points
3333552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3334552f7358SJed Brown @*/
33350adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
33360adebc6cSBarry Smith {
3337aa50250dSMatthew G. Knepley   DMLabel        label;
333863d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3339552f7358SJed Brown   PetscErrorCode ierr;
3340552f7358SJed Brown 
3341552f7358SJed Brown   PetscFunctionBegin;
3342552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
334363d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
334463d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3345552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
33460d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
334763d1a920SMatthew G. Knepley   if (stratumValue < 0) {
334863d1a920SMatthew G. Knepley     if (start) *start = pStart;
334963d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
335063d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3351552f7358SJed Brown   }
3352aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
335363d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
335463d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3355552f7358SJed Brown   PetscFunctionReturn(0);
3356552f7358SJed Brown }
3357552f7358SJed Brown 
3358552f7358SJed Brown /*@
3359552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3360552f7358SJed Brown 
3361552f7358SJed Brown   Not Collective
3362552f7358SJed Brown 
3363552f7358SJed Brown   Input Parameters:
3364552f7358SJed Brown + dm           - The DMPlex object
3365552f7358SJed Brown - stratumValue - The requested height
3366552f7358SJed Brown 
3367552f7358SJed Brown   Output Parameters:
3368552f7358SJed Brown + start - The first point at this height
3369552f7358SJed Brown - end   - One beyond the last point at this height
3370552f7358SJed Brown 
3371552f7358SJed Brown   Level: developer
3372552f7358SJed Brown 
3373552f7358SJed Brown .keywords: mesh, points
3374552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3375552f7358SJed Brown @*/
33760adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
33770adebc6cSBarry Smith {
3378aa50250dSMatthew G. Knepley   DMLabel        label;
337963d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3380552f7358SJed Brown   PetscErrorCode ierr;
3381552f7358SJed Brown 
3382552f7358SJed Brown   PetscFunctionBegin;
3383552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
338463d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
338563d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3386552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
33870d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
338863d1a920SMatthew G. Knepley   if (stratumValue < 0) {
338963d1a920SMatthew G. Knepley     if (start) *start = pStart;
339063d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
339163d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3392552f7358SJed Brown   }
3393aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
339413903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
339563d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
339663d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3397552f7358SJed Brown   PetscFunctionReturn(0);
3398552f7358SJed Brown }
3399552f7358SJed Brown 
34000adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
34010adebc6cSBarry Smith {
3402efe440bfSMatthew G. Knepley   PetscSection   section, s;
3403efe440bfSMatthew G. Knepley   Mat            m;
34043e922f36SToby Isaac   PetscInt       maxHeight;
3405552f7358SJed Brown   PetscErrorCode ierr;
3406552f7358SJed Brown 
3407552f7358SJed Brown   PetscFunctionBegin;
340838221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
34093e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
34103e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
341182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3412e87a4003SBarry Smith   ierr = DMSetSection(*cdm, section);CHKERRQ(ierr);
34131d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3414efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3415efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3416efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3417efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3418efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
34198f4c458bSMatthew G. Knepley 
34208f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
34218f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
3422552f7358SJed Brown   PetscFunctionReturn(0);
3423552f7358SJed Brown }
3424552f7358SJed Brown 
3425f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3426f19dbd58SToby Isaac {
3427f19dbd58SToby Isaac   Vec            coordsLocal;
3428f19dbd58SToby Isaac   DM             coordsDM;
3429f19dbd58SToby Isaac   PetscErrorCode ierr;
3430f19dbd58SToby Isaac 
3431f19dbd58SToby Isaac   PetscFunctionBegin;
3432f19dbd58SToby Isaac   *field = NULL;
3433f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3434f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3435f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3436f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3437f19dbd58SToby Isaac   }
3438f19dbd58SToby Isaac   PetscFunctionReturn(0);
3439f19dbd58SToby Isaac }
3440f19dbd58SToby Isaac 
34417cd05799SMatthew G. Knepley /*@C
34427cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
34437cd05799SMatthew G. Knepley 
34447cd05799SMatthew G. Knepley   Not Collective
34457cd05799SMatthew G. Knepley 
34467cd05799SMatthew G. Knepley   Input Parameters:
34477cd05799SMatthew G. Knepley . dm        - The DMPlex object
34487cd05799SMatthew G. Knepley 
34497cd05799SMatthew G. Knepley   Output Parameter:
34507cd05799SMatthew G. Knepley . section - The PetscSection object
34517cd05799SMatthew G. Knepley 
34527cd05799SMatthew G. Knepley   Level: developer
34537cd05799SMatthew G. Knepley 
34547cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
34557cd05799SMatthew G. Knepley @*/
34560adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
34570adebc6cSBarry Smith {
3458552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3459552f7358SJed Brown 
3460552f7358SJed Brown   PetscFunctionBegin;
3461552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3462552f7358SJed Brown   if (section) *section = mesh->coneSection;
3463552f7358SJed Brown   PetscFunctionReturn(0);
3464552f7358SJed Brown }
3465552f7358SJed Brown 
34667cd05799SMatthew G. Knepley /*@C
34677cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
34687cd05799SMatthew G. Knepley 
34697cd05799SMatthew G. Knepley   Not Collective
34707cd05799SMatthew G. Knepley 
34717cd05799SMatthew G. Knepley   Input Parameters:
34727cd05799SMatthew G. Knepley . dm        - The DMPlex object
34737cd05799SMatthew G. Knepley 
34747cd05799SMatthew G. Knepley   Output Parameter:
34757cd05799SMatthew G. Knepley . section - The PetscSection object
34767cd05799SMatthew G. Knepley 
34777cd05799SMatthew G. Knepley   Level: developer
34787cd05799SMatthew G. Knepley 
34797cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34807cd05799SMatthew G. Knepley @*/
34818cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
34828cb4d582SMatthew G. Knepley {
34838cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
34848cb4d582SMatthew G. Knepley 
34858cb4d582SMatthew G. Knepley   PetscFunctionBegin;
34868cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34878cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
34888cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
34898cb4d582SMatthew G. Knepley }
34908cb4d582SMatthew G. Knepley 
34917cd05799SMatthew G. Knepley /*@C
34927cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
34937cd05799SMatthew G. Knepley 
34947cd05799SMatthew G. Knepley   Not Collective
34957cd05799SMatthew G. Knepley 
34967cd05799SMatthew G. Knepley   Input Parameters:
34977cd05799SMatthew G. Knepley . dm        - The DMPlex object
34987cd05799SMatthew G. Knepley 
34997cd05799SMatthew G. Knepley   Output Parameter:
35007cd05799SMatthew G. Knepley . cones - The cone for each point
35017cd05799SMatthew G. Knepley 
35027cd05799SMatthew G. Knepley   Level: developer
35037cd05799SMatthew G. Knepley 
35047cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35057cd05799SMatthew G. Knepley @*/
3506a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3507a6dfd86eSKarl Rupp {
3508552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3509552f7358SJed Brown 
3510552f7358SJed Brown   PetscFunctionBegin;
3511552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3512552f7358SJed Brown   if (cones) *cones = mesh->cones;
3513552f7358SJed Brown   PetscFunctionReturn(0);
3514552f7358SJed Brown }
3515552f7358SJed Brown 
35167cd05799SMatthew G. Knepley /*@C
35177cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
35187cd05799SMatthew G. Knepley 
35197cd05799SMatthew G. Knepley   Not Collective
35207cd05799SMatthew G. Knepley 
35217cd05799SMatthew G. Knepley   Input Parameters:
35227cd05799SMatthew G. Knepley . dm        - The DMPlex object
35237cd05799SMatthew G. Knepley 
35247cd05799SMatthew G. Knepley   Output Parameter:
35257cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
35267cd05799SMatthew G. Knepley 
35277cd05799SMatthew G. Knepley   Level: developer
35287cd05799SMatthew G. Knepley 
35297cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35307cd05799SMatthew G. Knepley @*/
3531a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3532a6dfd86eSKarl Rupp {
3533552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3534552f7358SJed Brown 
3535552f7358SJed Brown   PetscFunctionBegin;
3536552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3537552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3538552f7358SJed Brown   PetscFunctionReturn(0);
3539552f7358SJed Brown }
3540552f7358SJed Brown 
3541552f7358SJed Brown /******************************** FEM Support **********************************/
3542552f7358SJed Brown 
3543*a4355906SMatthew Knepley /*@
3544*a4355906SMatthew Knepley   DMPlexCreateSpectralClosurePermutation - Create a permutation from the default (BFS) point ordering in the closure, to
3545*a4355906SMatthew Knepley   a lexicographic ordering over the cell, and set this permutation in the section provided.
3546*a4355906SMatthew Knepley 
3547*a4355906SMatthew Knepley   Input Parameters:
3548*a4355906SMatthew Knepley + dm      - The DM
3549*a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
3550*a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
3551*a4355906SMatthew Knepley 
3552*a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
3553*a4355906SMatthew Knepley   degree of the basis.
3554*a4355906SMatthew Knepley 
3555*a4355906SMatthew Knepley   Level: developer
3556*a4355906SMatthew Knepley 
3557*a4355906SMatthew Knepley .seealso: DMGetSection(), PetscSectionSetClosurePermutation()
3558*a4355906SMatthew Knepley @*/
35597391a63aSMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscInt point, PetscSection section)
35603194fc30SMatthew G. Knepley {
35617391a63aSMatthew G. Knepley   DMLabel        label;
35623194fc30SMatthew G. Knepley   PetscInt      *perm;
35637391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
35643194fc30SMatthew G. Knepley   PetscErrorCode ierr;
35653194fc30SMatthew G. Knepley 
35663194fc30SMatthew G. Knepley   PetscFunctionBegin;
35677391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
35683194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
35697391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
35707391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
35717391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
35727391a63aSMatthew G. Knepley   else if  (depth == dim) {
35737391a63aSMatthew G. Knepley     const PetscInt *cone;
35747391a63aSMatthew G. Knepley 
35757391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3576d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
3577d4e6627bSStefano Zampini     else if (dim == 3) {
3578d4e6627bSStefano Zampini       const PetscInt *cone2;
3579d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
3580d4e6627bSStefano Zampini       eStart = cone2[0];
3581d4e6627bSStefano 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);
3582d4e6627bSStefano 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);
3583e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
35843194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
358589eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
35863194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
35873194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
35883194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35893194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
35903194fc30SMatthew G. Knepley     k = k/Nc + 1;
359189eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
35923194fc30SMatthew G. Knepley   }
35933194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
35943194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
359589eabcffSMatthew G. Knepley     switch (dim) {
359689eabcffSMatthew G. Knepley     case 2:
35973194fc30SMatthew 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} */
35983194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35993194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
36003194fc30SMatthew G. Knepley       k = k/Nc + 1;
36013194fc30SMatthew G. Knepley       /* The SEM order is
36023194fc30SMatthew G. Knepley 
36033194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
360489eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
36053194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
36063194fc30SMatthew G. Knepley       */
36073194fc30SMatthew G. Knepley       {
36083194fc30SMatthew G. Knepley         const PetscInt of   = 0;
36093194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
36103194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
36113194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
36123194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
36133194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
36143194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
36153194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
36163194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
36173194fc30SMatthew G. Knepley         PetscInt       o;
36183194fc30SMatthew G. Knepley 
36193194fc30SMatthew G. Knepley         /* bottom */
36203194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
36213194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
36223194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
36233194fc30SMatthew G. Knepley         /* middle */
36243194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
36253194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
36263194fc30SMatthew 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;
36273194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
36283194fc30SMatthew G. Knepley         }
36293194fc30SMatthew G. Knepley         /* top */
36303194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
36313194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
36323194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
36333194fc30SMatthew G. Knepley         foffset = offset;
36343194fc30SMatthew G. Knepley       }
363589eabcffSMatthew G. Knepley       break;
363689eabcffSMatthew G. Knepley     case 3:
363789eabcffSMatthew G. Knepley       /* The original hex closure is
363889eabcffSMatthew G. Knepley 
363989eabcffSMatthew G. Knepley          {c,
364089eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
364189eabcffSMatthew 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,
364289eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
364389eabcffSMatthew G. Knepley       */
364489eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
364589eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
364689eabcffSMatthew G. Knepley       k = k/Nc + 1;
364789eabcffSMatthew G. Knepley       /* The SEM order is
364889eabcffSMatthew G. Knepley          Bottom Slice
364989eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
365089eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
365189eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
365289eabcffSMatthew G. Knepley 
365389eabcffSMatthew G. Knepley          Middle Slice (j)
365489eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
365589eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
365689eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
365789eabcffSMatthew G. Knepley 
365889eabcffSMatthew G. Knepley          Top Slice
365989eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
366089eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
366189eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
366289eabcffSMatthew G. Knepley       */
366389eabcffSMatthew G. Knepley       {
366489eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
366589eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
366689eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
366789eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
366889eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
366989eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
367089eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
367189eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
367289eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
367389eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
367489eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
367589eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
367689eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
367789eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
367889eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
367989eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
368089eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
368189eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
368289eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
368389eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
368489eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
368589eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
368689eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
368789eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
368889eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
368989eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
369089eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
369189eabcffSMatthew G. Knepley         PetscInt       o, n;
369289eabcffSMatthew G. Knepley 
369389eabcffSMatthew G. Knepley         /* Bottom Slice */
369489eabcffSMatthew G. Knepley         /*   bottom */
369589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
369689eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
369789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
369889eabcffSMatthew G. Knepley         /*   middle */
369989eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
370089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3701316b7f87SMax 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;}
370289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
37033194fc30SMatthew G. Knepley         }
370489eabcffSMatthew G. Knepley         /*   top */
370589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
370689eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
370789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
370889eabcffSMatthew G. Knepley 
370989eabcffSMatthew G. Knepley         /* Middle Slice */
371089eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
371189eabcffSMatthew G. Knepley           /*   bottom */
371289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
371389eabcffSMatthew 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;
371489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
371589eabcffSMatthew G. Knepley           /*   middle */
371689eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
371789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
371889eabcffSMatthew 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;
371989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
372089eabcffSMatthew G. Knepley           }
372189eabcffSMatthew G. Knepley           /*   top */
372289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
372389eabcffSMatthew 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;
372489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
372589eabcffSMatthew G. Knepley         }
372689eabcffSMatthew G. Knepley 
372789eabcffSMatthew G. Knepley         /* Top Slice */
372889eabcffSMatthew G. Knepley         /*   bottom */
372989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
373089eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
373189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
373289eabcffSMatthew G. Knepley         /*   middle */
373389eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
373489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
373589eabcffSMatthew 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;
373689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
373789eabcffSMatthew G. Knepley         }
373889eabcffSMatthew G. Knepley         /*   top */
373989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
374089eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
374189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
374289eabcffSMatthew G. Knepley 
374389eabcffSMatthew G. Knepley         foffset = offset;
374489eabcffSMatthew G. Knepley       }
374589eabcffSMatthew G. Knepley       break;
374689eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
374789eabcffSMatthew G. Knepley     }
374889eabcffSMatthew G. Knepley   }
374989eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
37503194fc30SMatthew G. Knepley   /* Check permutation */
37513194fc30SMatthew G. Knepley   {
37523194fc30SMatthew G. Knepley     PetscInt *check;
37533194fc30SMatthew G. Knepley 
37543194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
37553194fc30SMatthew 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]);}
37563194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
37573194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
37583194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
37593194fc30SMatthew G. Knepley   }
37601fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
37613194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
37623194fc30SMatthew G. Knepley }
37633194fc30SMatthew G. Knepley 
3764e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3765e071409bSToby Isaac {
3766e071409bSToby Isaac   PetscDS        prob;
3767e071409bSToby Isaac   PetscInt       depth, Nf, h;
3768e071409bSToby Isaac   DMLabel        label;
3769e071409bSToby Isaac   PetscErrorCode ierr;
3770e071409bSToby Isaac 
3771e071409bSToby Isaac   PetscFunctionBeginHot;
3772e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
3773e071409bSToby Isaac   Nf      = prob->Nf;
3774e071409bSToby Isaac   label   = dm->depthLabel;
3775e071409bSToby Isaac   *dspace = NULL;
3776e071409bSToby Isaac   if (field < Nf) {
3777e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3778e071409bSToby Isaac 
3779e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3780e071409bSToby Isaac       PetscDualSpace dsp;
3781e071409bSToby Isaac 
3782e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3783e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3784e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3785e071409bSToby Isaac       h    = depth - 1 - h;
3786e071409bSToby Isaac       if (h) {
3787e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3788e071409bSToby Isaac       } else {
3789e071409bSToby Isaac         *dspace = dsp;
3790e071409bSToby Isaac       }
3791e071409bSToby Isaac     }
3792e071409bSToby Isaac   }
3793e071409bSToby Isaac   PetscFunctionReturn(0);
3794e071409bSToby Isaac }
3795e071409bSToby Isaac 
3796e071409bSToby Isaac 
37971a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3798a6dfd86eSKarl Rupp {
3799552f7358SJed Brown   PetscScalar    *array, *vArray;
3800d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
38011a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3802552f7358SJed Brown   PetscErrorCode  ierr;
3803552f7358SJed Brown 
38041b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
38052a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
38065a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
38075a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
38085a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
38093f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
38109df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
38119df71ca4SMatthew G. Knepley       PetscInt dof;
3812d9917b9dSMatthew G. Knepley 
38139df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38149df71ca4SMatthew G. Knepley       size += dof;
38159df71ca4SMatthew G. Knepley     }
38169df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
38179df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
38182a3aaacfSMatthew G. Knepley       PetscInt       dof;
38195a1bb5cfSMatthew G. Knepley 
38205a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
38212a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38225a1bb5cfSMatthew G. Knepley       size += dof;
38235a1bb5cfSMatthew G. Knepley     }
38243f7cbbe7SMatthew G. Knepley     if (!values) {
38253f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
38263f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
38273f7cbbe7SMatthew G. Knepley     }
382869291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
3829982e9ed1SMatthew G. Knepley   } else {
3830982e9ed1SMatthew G. Knepley     array = *values;
3831982e9ed1SMatthew G. Knepley   }
38329df71ca4SMatthew G. Knepley   size = 0;
38335a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
38349df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
38359df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
38369df71ca4SMatthew G. Knepley     PetscScalar *varr;
3837d9917b9dSMatthew G. Knepley 
38389df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38399df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
38409df71ca4SMatthew G. Knepley     varr = &vArray[off];
38411a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
38421a271a75SMatthew G. Knepley       array[offset] = varr[d];
38439df71ca4SMatthew G. Knepley     }
38449df71ca4SMatthew G. Knepley     size += dof;
38459df71ca4SMatthew G. Knepley   }
38469df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
38479df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
38489df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
38495a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
38505a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
38515a1bb5cfSMatthew G. Knepley 
385252ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
38535a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38545a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
38555a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
38565a1bb5cfSMatthew G. Knepley     if (o >= 0) {
38571a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
38581a271a75SMatthew G. Knepley         array[offset] = varr[d];
38595a1bb5cfSMatthew G. Knepley       }
38605a1bb5cfSMatthew G. Knepley     } else {
38611a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
38621a271a75SMatthew G. Knepley         array[offset] = varr[d];
38635a1bb5cfSMatthew G. Knepley       }
38645a1bb5cfSMatthew G. Knepley     }
38659df71ca4SMatthew G. Knepley     size += dof;
38665a1bb5cfSMatthew G. Knepley   }
38675a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
38689df71ca4SMatthew G. Knepley   if (!*values) {
38695a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
38705a1bb5cfSMatthew G. Knepley     *values = array;
38719df71ca4SMatthew G. Knepley   } else {
38728ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
38738c312ff3SMatthew G. Knepley     *csize = size;
38749df71ca4SMatthew G. Knepley   }
38755a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
38765a1bb5cfSMatthew G. Knepley }
3877d9917b9dSMatthew G. Knepley 
3878923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3879923c78e0SToby Isaac {
3880923c78e0SToby Isaac   const PetscInt *cla;
3881923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3882923c78e0SToby Isaac   PetscErrorCode ierr;
3883923c78e0SToby Isaac 
3884923c78e0SToby Isaac   PetscFunctionBeginHot;
3885923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3886923c78e0SToby Isaac   if (!*clPoints) {
3887923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3888923c78e0SToby Isaac 
3889923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3890923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3891923c78e0SToby Isaac     /* Compress out points not in the section */
3892923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3893923c78e0SToby Isaac       PetscInt r = pts[2*p];
3894923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3895923c78e0SToby Isaac         pts[q*2]   = r;
3896923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3897923c78e0SToby Isaac         ++q;
3898923c78e0SToby Isaac       }
3899923c78e0SToby Isaac     }
3900923c78e0SToby Isaac     np = q;
3901923c78e0SToby Isaac     cla = NULL;
3902923c78e0SToby Isaac   } else {
3903923c78e0SToby Isaac     PetscInt dof, off;
3904923c78e0SToby Isaac 
3905923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3906923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3907923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3908923c78e0SToby Isaac     np   = dof/2;
3909923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3910923c78e0SToby Isaac   }
3911923c78e0SToby Isaac   *numPoints = np;
3912923c78e0SToby Isaac   *points    = pts;
3913923c78e0SToby Isaac   *clp       = cla;
3914923c78e0SToby Isaac 
3915923c78e0SToby Isaac   PetscFunctionReturn(0);
3916923c78e0SToby Isaac }
3917923c78e0SToby Isaac 
3918923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3919923c78e0SToby Isaac {
3920923c78e0SToby Isaac   PetscErrorCode ierr;
3921923c78e0SToby Isaac 
3922923c78e0SToby Isaac   PetscFunctionBeginHot;
3923923c78e0SToby Isaac   if (!*clPoints) {
3924923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3925923c78e0SToby Isaac   } else {
3926923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3927923c78e0SToby Isaac   }
3928923c78e0SToby Isaac   *numPoints = 0;
3929923c78e0SToby Isaac   *points    = NULL;
3930923c78e0SToby Isaac   *clSec     = NULL;
3931923c78e0SToby Isaac   *clPoints  = NULL;
3932923c78e0SToby Isaac   *clp       = NULL;
3933923c78e0SToby Isaac   PetscFunctionReturn(0);
3934923c78e0SToby Isaac }
3935923c78e0SToby Isaac 
393697e99dd9SToby 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[])
39371a271a75SMatthew G. Knepley {
39381a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
393997e99dd9SToby Isaac   const PetscInt    **perms = NULL;
394097e99dd9SToby Isaac   const PetscScalar **flips = NULL;
39411a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39421a271a75SMatthew G. Knepley 
39431a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3944fe02ba77SJed Brown   *size = 0;
394597e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
394697e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
394797e99dd9SToby Isaac     const PetscInt    point = points[2*p];
394897e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
394997e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
39501a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
39511a271a75SMatthew G. Knepley     const PetscScalar *varr;
39521a271a75SMatthew G. Knepley 
39531a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39541a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
39551a271a75SMatthew G. Knepley     varr = &vArray[off];
395697e99dd9SToby Isaac     if (clperm) {
395797e99dd9SToby Isaac       if (perm) {
395897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
39591a271a75SMatthew G. Knepley       } else {
396097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
396197e99dd9SToby Isaac       }
396297e99dd9SToby Isaac       if (flip) {
396397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
396497e99dd9SToby Isaac       }
396597e99dd9SToby Isaac     } else {
396697e99dd9SToby Isaac       if (perm) {
396797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
396897e99dd9SToby Isaac       } else {
396997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
397097e99dd9SToby Isaac       }
397197e99dd9SToby Isaac       if (flip) {
397297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
39731a271a75SMatthew G. Knepley       }
39741a271a75SMatthew G. Knepley     }
397597e99dd9SToby Isaac     offset += dof;
397697e99dd9SToby Isaac   }
397797e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
39781a271a75SMatthew G. Knepley   *size = offset;
39791a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
39801a271a75SMatthew G. Knepley }
39811a271a75SMatthew G. Knepley 
398297e99dd9SToby 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[])
39831a271a75SMatthew G. Knepley {
39841a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
39851a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39861a271a75SMatthew G. Knepley 
39871a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3988fe02ba77SJed Brown   *size = 0;
39891a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
399097e99dd9SToby Isaac     PetscInt          p;
399197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
399297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
39931a271a75SMatthew G. Knepley 
399497e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
399597e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
399697e99dd9SToby Isaac       const PetscInt    point = points[2*p];
399797e99dd9SToby Isaac       PetscInt          fdof, foff, b;
39981a271a75SMatthew G. Knepley       const PetscScalar *varr;
399997e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
400097e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
40011a271a75SMatthew G. Knepley 
40021a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
40031a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
40041a271a75SMatthew G. Knepley       varr = &vArray[foff];
400597e99dd9SToby Isaac       if (clperm) {
400697e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
400797e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
400897e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
40091a271a75SMatthew G. Knepley       } else {
401097e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
401197e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
401297e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
40131a271a75SMatthew G. Knepley       }
401497e99dd9SToby Isaac       offset += fdof;
40151a271a75SMatthew G. Knepley     }
401697e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
40171a271a75SMatthew G. Knepley   }
40181a271a75SMatthew G. Knepley   *size = offset;
40191a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
40201a271a75SMatthew G. Knepley }
40211a271a75SMatthew G. Knepley 
4022552f7358SJed Brown /*@C
4023552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4024552f7358SJed Brown 
4025552f7358SJed Brown   Not collective
4026552f7358SJed Brown 
4027552f7358SJed Brown   Input Parameters:
4028552f7358SJed Brown + dm - The DM
4029552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4030552f7358SJed Brown . v - The local vector
403122c1ee49SMatthew G. Knepley . point - The point in the DM
403222c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
403322c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4034552f7358SJed Brown 
4035552f7358SJed Brown   Output Parameters:
403622c1ee49SMatthew G. Knepley + csize - The number of values in the closure
403722c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
403822c1ee49SMatthew G. Knepley 
403922c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
404022c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
404122c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
404222c1ee49SMatthew G. Knepley $
404322c1ee49SMatthew G. Knepley $ A typical use could be
404422c1ee49SMatthew G. Knepley $
404522c1ee49SMatthew G. Knepley $  values = NULL;
404622c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
404722c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
404822c1ee49SMatthew G. Knepley $    <Compute on closure>
404922c1ee49SMatthew G. Knepley $  }
405022c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
405122c1ee49SMatthew G. Knepley $
405222c1ee49SMatthew G. Knepley $ or
405322c1ee49SMatthew G. Knepley $
405422c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
405522c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
405622c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
405722c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
405822c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
405922c1ee49SMatthew G. Knepley $      <Compute on closure>
406022c1ee49SMatthew G. Knepley $    }
406122c1ee49SMatthew G. Knepley $  }
406222c1ee49SMatthew G. Knepley $  PetscFree(values);
4063552f7358SJed Brown 
4064552f7358SJed Brown   Fortran Notes:
4065552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4066552f7358SJed Brown   include petsc.h90 in your code.
4067552f7358SJed Brown 
4068552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4069552f7358SJed Brown 
4070552f7358SJed Brown   Level: intermediate
4071552f7358SJed Brown 
4072552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4073552f7358SJed Brown @*/
4074552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4075552f7358SJed Brown {
4076552f7358SJed Brown   PetscSection       clSection;
4077d9917b9dSMatthew G. Knepley   IS                 clPoints;
40788a84db2dSMatthew G. Knepley   PetscScalar       *array;
40798a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4080552f7358SJed Brown   PetscInt          *points = NULL;
40818f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
40821a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4083552f7358SJed Brown   PetscErrorCode     ierr;
4084552f7358SJed Brown 
4085d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4086552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4087e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
40881a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
40891a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4090552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4091552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4092552f7358SJed Brown   if (depth == 1 && numFields < 2) {
40931a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4094552f7358SJed Brown     PetscFunctionReturn(0);
4095552f7358SJed Brown   }
40961a271a75SMatthew G. Knepley   /* Get points */
4097923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40981fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
40991a271a75SMatthew G. Knepley   /* Get array */
4100bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
41011a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4102552f7358SJed Brown 
41031a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4104552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
41051a271a75SMatthew G. Knepley       asize += dof;
4106552f7358SJed Brown     }
4107bd6c0d32SMatthew G. Knepley     if (!values) {
4108923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41091a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4110bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4111bd6c0d32SMatthew G. Knepley     }
411269291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4113d0f6b257SMatthew G. Knepley   } else {
4114d0f6b257SMatthew G. Knepley     array = *values;
4115d0f6b257SMatthew G. Knepley   }
41168a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
41171a271a75SMatthew G. Knepley   /* Get values */
411897e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
411997e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
41201a271a75SMatthew G. Knepley   /* Cleanup points */
4121923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41221a271a75SMatthew G. Knepley   /* Cleanup array */
41238a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4124d0f6b257SMatthew G. Knepley   if (!*values) {
4125552f7358SJed Brown     if (csize) *csize = size;
4126552f7358SJed Brown     *values = array;
4127d0f6b257SMatthew G. Knepley   } else {
4128f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4129d0f6b257SMatthew G. Knepley     *csize = size;
4130d0f6b257SMatthew G. Knepley   }
4131552f7358SJed Brown   PetscFunctionReturn(0);
4132552f7358SJed Brown }
4133552f7358SJed Brown 
4134552f7358SJed Brown /*@C
4135552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4136552f7358SJed Brown 
4137552f7358SJed Brown   Not collective
4138552f7358SJed Brown 
4139552f7358SJed Brown   Input Parameters:
4140552f7358SJed Brown + dm - The DM
41410298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4142552f7358SJed Brown . v - The local vector
4143eaf898f9SPatrick Sanan . point - The point in the DM
41440298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4145552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4146552f7358SJed Brown 
414722c1ee49SMatthew 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()
414822c1ee49SMatthew G. Knepley 
41493813dfbdSMatthew G Knepley   Fortran Notes:
41503813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
41513813dfbdSMatthew G Knepley   include petsc.h90 in your code.
41523813dfbdSMatthew G Knepley 
41533813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
41543813dfbdSMatthew G Knepley 
4155552f7358SJed Brown   Level: intermediate
4156552f7358SJed Brown 
4157552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4158552f7358SJed Brown @*/
41597c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4160a6dfd86eSKarl Rupp {
4161552f7358SJed Brown   PetscInt       size = 0;
4162552f7358SJed Brown   PetscErrorCode ierr;
4163552f7358SJed Brown 
4164552f7358SJed Brown   PetscFunctionBegin;
4165552f7358SJed Brown   /* Should work without recalculating size */
416669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4167c9fdaa05SMatthew G. Knepley   *values = NULL;
4168552f7358SJed Brown   PetscFunctionReturn(0);
4169552f7358SJed Brown }
4170552f7358SJed Brown 
4171552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4172552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4173552f7358SJed Brown 
417497e99dd9SToby 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[])
4175552f7358SJed Brown {
4176552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4177552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4178552f7358SJed Brown   PetscScalar    *a;
4179552f7358SJed Brown   PetscInt        off, cind = 0, k;
4180552f7358SJed Brown   PetscErrorCode  ierr;
4181552f7358SJed Brown 
4182552f7358SJed Brown   PetscFunctionBegin;
4183552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4184552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4185552f7358SJed Brown   a    = &array[off];
4186552f7358SJed Brown   if (!cdof || setBC) {
418797e99dd9SToby Isaac     if (clperm) {
418897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
418997e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4190552f7358SJed Brown     } else {
419197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
419297e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4193552f7358SJed Brown     }
4194552f7358SJed Brown   } else {
4195552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
419697e99dd9SToby Isaac     if (clperm) {
419797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4198552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
419997e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4200552f7358SJed Brown         }
4201552f7358SJed Brown       } else {
4202552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4203552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
420497e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
420597e99dd9SToby Isaac         }
420697e99dd9SToby Isaac       }
420797e99dd9SToby Isaac     } else {
420897e99dd9SToby Isaac       if (perm) {
420997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
421097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
421197e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
421297e99dd9SToby Isaac         }
421397e99dd9SToby Isaac       } else {
421497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
421597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
421697e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
421797e99dd9SToby Isaac         }
4218552f7358SJed Brown       }
4219552f7358SJed Brown     }
4220552f7358SJed Brown   }
4221552f7358SJed Brown   PetscFunctionReturn(0);
4222552f7358SJed Brown }
4223552f7358SJed Brown 
422497e99dd9SToby 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[])
4225a5e93ea8SMatthew G. Knepley {
4226a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4227a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4228a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4229a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4230a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4231a5e93ea8SMatthew G. Knepley 
4232a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4233a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4234a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4235a5e93ea8SMatthew G. Knepley   a    = &array[off];
4236a5e93ea8SMatthew G. Knepley   if (cdof) {
4237a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
423897e99dd9SToby Isaac     if (clperm) {
423997e99dd9SToby Isaac       if (perm) {
4240a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4241a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
424297e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
424397e99dd9SToby Isaac             cind++;
4244a5e93ea8SMatthew G. Knepley           }
4245a5e93ea8SMatthew G. Knepley         }
4246a5e93ea8SMatthew G. Knepley       } else {
4247a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4248a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
424997e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
425097e99dd9SToby Isaac             cind++;
425197e99dd9SToby Isaac           }
425297e99dd9SToby Isaac         }
425397e99dd9SToby Isaac       }
425497e99dd9SToby Isaac     } else {
425597e99dd9SToby Isaac       if (perm) {
425697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
425797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
425897e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
425997e99dd9SToby Isaac             cind++;
426097e99dd9SToby Isaac           }
426197e99dd9SToby Isaac         }
426297e99dd9SToby Isaac       } else {
426397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
426497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
426597e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
426697e99dd9SToby Isaac             cind++;
426797e99dd9SToby Isaac           }
4268a5e93ea8SMatthew G. Knepley         }
4269a5e93ea8SMatthew G. Knepley       }
4270a5e93ea8SMatthew G. Knepley     }
4271a5e93ea8SMatthew G. Knepley   }
4272a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4273a5e93ea8SMatthew G. Knepley }
4274a5e93ea8SMatthew G. Knepley 
427597e99dd9SToby 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[])
4276a6dfd86eSKarl Rupp {
4277552f7358SJed Brown   PetscScalar    *a;
42781a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
42791a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
428097e99dd9SToby Isaac   PetscInt        cind = 0, b;
4281552f7358SJed Brown   PetscErrorCode  ierr;
4282552f7358SJed Brown 
4283552f7358SJed Brown   PetscFunctionBegin;
4284552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4285552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
42861a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
42871a271a75SMatthew G. Knepley   a    = &array[foff];
4288552f7358SJed Brown   if (!fcdof || setBC) {
428997e99dd9SToby Isaac     if (clperm) {
429097e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
429197e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4292552f7358SJed Brown     } else {
429397e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
429497e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4295552f7358SJed Brown     }
4296552f7358SJed Brown   } else {
4297552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
429897e99dd9SToby Isaac     if (clperm) {
429997e99dd9SToby Isaac       if (perm) {
430097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
430197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
430297e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4303552f7358SJed Brown         }
4304552f7358SJed Brown       } else {
430597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
430697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
430797e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
430897e99dd9SToby Isaac         }
430997e99dd9SToby Isaac       }
431097e99dd9SToby Isaac     } else {
431197e99dd9SToby Isaac       if (perm) {
431297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
431397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
431497e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
431597e99dd9SToby Isaac         }
431697e99dd9SToby Isaac       } else {
431797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
431897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
431997e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4320552f7358SJed Brown         }
4321552f7358SJed Brown       }
4322552f7358SJed Brown     }
4323552f7358SJed Brown   }
43241a271a75SMatthew G. Knepley   *offset += fdof;
4325552f7358SJed Brown   PetscFunctionReturn(0);
4326552f7358SJed Brown }
4327552f7358SJed Brown 
4328ba322698SMatthew 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[])
4329a5e93ea8SMatthew G. Knepley {
4330a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
43311a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
43321a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4333ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4334ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4335a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4336a5e93ea8SMatthew G. Knepley 
4337a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4338a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4339a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
43401a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43411a271a75SMatthew G. Knepley   a    = &array[foff];
4342a5e93ea8SMatthew G. Knepley   if (fcdof) {
4343ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4344a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
434597e99dd9SToby Isaac     if (clperm) {
434697e99dd9SToby Isaac       if (perm) {
4347ba322698SMatthew G. Knepley         if (comps) {
4348ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4349ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4350ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4351ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4352ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4353ba322698SMatthew G. Knepley           }
4354ba322698SMatthew G. Knepley         } else {
435597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
435697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
435797e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4358a5e93ea8SMatthew G. Knepley               ++cind;
4359a5e93ea8SMatthew G. Knepley             }
4360a5e93ea8SMatthew G. Knepley           }
4361ba322698SMatthew G. Knepley         }
4362ba322698SMatthew G. Knepley       } else {
4363ba322698SMatthew G. Knepley         if (comps) {
4364ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4365ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4366ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4367ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4368ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4369ba322698SMatthew G. Knepley           }
4370a5e93ea8SMatthew G. Knepley         } else {
437197e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
437297e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
437397e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
437497e99dd9SToby Isaac               ++cind;
437597e99dd9SToby Isaac             }
437697e99dd9SToby Isaac           }
437797e99dd9SToby Isaac         }
4378ba322698SMatthew G. Knepley       }
437997e99dd9SToby Isaac     } else {
438097e99dd9SToby Isaac       if (perm) {
4381ba322698SMatthew G. Knepley         if (comps) {
4382ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4383ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4384ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4385ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4386ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4387ba322698SMatthew G. Knepley           }
4388ba322698SMatthew G. Knepley         } else {
438997e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
439097e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
439197e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
439297e99dd9SToby Isaac               ++cind;
439397e99dd9SToby Isaac             }
439497e99dd9SToby Isaac           }
4395ba322698SMatthew G. Knepley         }
4396ba322698SMatthew G. Knepley       } else {
4397ba322698SMatthew G. Knepley         if (comps) {
4398ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4399ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4400ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4401ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4402ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4403ba322698SMatthew G. Knepley           }
440497e99dd9SToby Isaac         } else {
440597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
440697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
440797e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4408a5e93ea8SMatthew G. Knepley               ++cind;
4409a5e93ea8SMatthew G. Knepley             }
4410a5e93ea8SMatthew G. Knepley           }
4411a5e93ea8SMatthew G. Knepley         }
4412a5e93ea8SMatthew G. Knepley       }
4413a5e93ea8SMatthew G. Knepley     }
4414ba322698SMatthew G. Knepley   }
44151a271a75SMatthew G. Knepley   *offset += fdof;
4416a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4417a5e93ea8SMatthew G. Knepley }
4418a5e93ea8SMatthew G. Knepley 
44198f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4420a6dfd86eSKarl Rupp {
4421552f7358SJed Brown   PetscScalar    *array;
44221b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
44231b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4424552f7358SJed Brown   PetscErrorCode  ierr;
4425552f7358SJed Brown 
44261b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4427b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4428b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4429b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4430b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4431b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4432b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4433b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4434b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4435b6ebb6e6SMatthew G. Knepley 
4436b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4437b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4438b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4439b6ebb6e6SMatthew G. Knepley     {
4440b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4441b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4442b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4443b6ebb6e6SMatthew G. Knepley 
4444b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4445b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4446b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4447b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4448b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4449b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4450b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4451b6ebb6e6SMatthew G. Knepley           }
4452b6ebb6e6SMatthew G. Knepley         } else {
4453b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4454b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4455b6ebb6e6SMatthew G. Knepley           }
4456b6ebb6e6SMatthew G. Knepley         }
4457b6ebb6e6SMatthew G. Knepley       } else {
4458b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4459b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4460b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4461b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4462b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4463b6ebb6e6SMatthew G. Knepley           }
4464b6ebb6e6SMatthew G. Knepley         } else {
4465b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4466b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4467b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4468b6ebb6e6SMatthew G. Knepley           }
4469b6ebb6e6SMatthew G. Knepley         }
4470b6ebb6e6SMatthew G. Knepley       }
4471b6ebb6e6SMatthew G. Knepley     }
4472b6ebb6e6SMatthew G. Knepley   }
4473b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4474b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4475b6ebb6e6SMatthew G. Knepley }
44761b406b76SMatthew G. Knepley 
44771b406b76SMatthew G. Knepley /*@C
44781b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
44791b406b76SMatthew G. Knepley 
44801b406b76SMatthew G. Knepley   Not collective
44811b406b76SMatthew G. Knepley 
44821b406b76SMatthew G. Knepley   Input Parameters:
44831b406b76SMatthew G. Knepley + dm - The DM
44841b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
44851b406b76SMatthew G. Knepley . v - The local vector
4486eaf898f9SPatrick Sanan . point - The point in the DM
44871b406b76SMatthew G. Knepley . values - The array of values
448822c1ee49SMatthew 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,
448922c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
44901b406b76SMatthew G. Knepley 
44911b406b76SMatthew G. Knepley   Fortran Notes:
44921b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
44931b406b76SMatthew G. Knepley 
44941b406b76SMatthew G. Knepley   Level: intermediate
44951b406b76SMatthew G. Knepley 
44961b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
44971b406b76SMatthew G. Knepley @*/
44981b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
44991b406b76SMatthew G. Knepley {
45001b406b76SMatthew G. Knepley   PetscSection    clSection;
45011b406b76SMatthew G. Knepley   IS              clPoints;
45021b406b76SMatthew G. Knepley   PetscScalar    *array;
45031b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
450497e99dd9SToby Isaac   const PetscInt *clp, *clperm;
45051a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
45061b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
45071b406b76SMatthew G. Knepley 
45081a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
45091b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4510e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
45111a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45121a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
45131b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
45141b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
45151b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
45168f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
45171b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
45181b406b76SMatthew G. Knepley   }
45191a271a75SMatthew G. Knepley   /* Get points */
452097e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4521923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
45221a271a75SMatthew G. Knepley   /* Get array */
4523552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
45241a271a75SMatthew G. Knepley   /* Get values */
4525ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
452697e99dd9SToby Isaac     PetscInt offset = 0, f;
4527552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
452897e99dd9SToby Isaac       const PetscInt    **perms = NULL;
452997e99dd9SToby Isaac       const PetscScalar **flips = NULL;
453097e99dd9SToby Isaac 
453197e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4532552f7358SJed Brown       switch (mode) {
4533552f7358SJed Brown       case INSERT_VALUES:
453497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
453597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
453697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
453797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
453897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4539552f7358SJed Brown         } break;
4540552f7358SJed Brown       case INSERT_ALL_VALUES:
454197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
454297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
454397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
454497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
454597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4546552f7358SJed Brown         } break;
4547a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
454897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
454997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
455097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
455197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4552ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4553a5e93ea8SMatthew G. Knepley         } break;
4554552f7358SJed Brown       case ADD_VALUES:
455597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
455697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
455797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
455897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
455997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4560552f7358SJed Brown         } break;
4561552f7358SJed Brown       case ADD_ALL_VALUES:
456297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
456397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
456497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
456597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
456697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4567552f7358SJed Brown         } break;
4568304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
456997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
457097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
457197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
457297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4573ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4574304ab55fSMatthew G. Knepley         } break;
4575552f7358SJed Brown       default:
4576f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4577552f7358SJed Brown       }
457897e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
45791a271a75SMatthew G. Knepley     }
4580552f7358SJed Brown   } else {
45811a271a75SMatthew G. Knepley     PetscInt dof, off;
458297e99dd9SToby Isaac     const PetscInt    **perms = NULL;
458397e99dd9SToby Isaac     const PetscScalar **flips = NULL;
45841a271a75SMatthew G. Knepley 
458597e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4586552f7358SJed Brown     switch (mode) {
4587552f7358SJed Brown     case INSERT_VALUES:
458897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
458997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
459097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
459197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
459297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
459397e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4594552f7358SJed Brown       } break;
4595552f7358SJed Brown     case INSERT_ALL_VALUES:
459697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
459797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
459897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
459997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
460097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
460197e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4602552f7358SJed Brown       } break;
4603a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
460497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
460597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
460697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
460797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
460897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
460997e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4610a5e93ea8SMatthew G. Knepley       } break;
4611552f7358SJed Brown     case ADD_VALUES:
461297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
461397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
461497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
461597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
461697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
461797e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4618552f7358SJed Brown       } break;
4619552f7358SJed Brown     case ADD_ALL_VALUES:
462097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
462197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
462297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
462397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
462497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
462597e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4626552f7358SJed Brown       } break;
4627304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
462897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
462997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
463097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
463197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
463297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
463397e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4634304ab55fSMatthew G. Knepley       } break;
4635552f7358SJed Brown     default:
4636f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4637552f7358SJed Brown     }
463897e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4639552f7358SJed Brown   }
46401a271a75SMatthew G. Knepley   /* Cleanup points */
4641923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
46421a271a75SMatthew G. Knepley   /* Cleanup array */
4643552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4644552f7358SJed Brown   PetscFunctionReturn(0);
4645552f7358SJed Brown }
4646552f7358SJed Brown 
4647ba322698SMatthew 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)
4648e07394fbSMatthew G. Knepley {
4649e07394fbSMatthew G. Knepley   PetscSection      clSection;
4650e07394fbSMatthew G. Knepley   IS                clPoints;
4651e07394fbSMatthew G. Knepley   PetscScalar       *array;
4652e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4653b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4654e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
465597e99dd9SToby Isaac   PetscInt          offset = 0, f;
4656e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4657e07394fbSMatthew G. Knepley 
4658e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4659e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4660e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
4661e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4662e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4663e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4664e07394fbSMatthew G. Knepley   /* Get points */
4665b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4666923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4667e07394fbSMatthew G. Knepley   /* Get array */
4668e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4669e07394fbSMatthew G. Knepley   /* Get values */
4670e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
467197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
467297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
467397e99dd9SToby Isaac 
4674e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4675e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4676e07394fbSMatthew G. Knepley         PetscInt fdof;
4677e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4678e07394fbSMatthew G. Knepley         offset += fdof;
4679e07394fbSMatthew G. Knepley       }
4680e07394fbSMatthew G. Knepley       continue;
4681e07394fbSMatthew G. Knepley     }
468297e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4683e07394fbSMatthew G. Knepley     switch (mode) {
4684e07394fbSMatthew G. Knepley     case INSERT_VALUES:
468597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
468697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
468797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
468897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4689b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4690e07394fbSMatthew G. Knepley       } break;
4691e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
469297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
469397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
469497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
469597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4696b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4697e07394fbSMatthew G. Knepley         } break;
4698e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
469997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
470097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
470197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
470297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4703ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
4704e07394fbSMatthew G. Knepley       } break;
4705e07394fbSMatthew G. Knepley     case ADD_VALUES:
470697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
470797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
470897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
470997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4710b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4711e07394fbSMatthew G. Knepley       } break;
4712e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
471397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
471497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
471597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
471697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4717b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4718e07394fbSMatthew G. Knepley       } break;
4719e07394fbSMatthew G. Knepley     default:
4720f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4721e07394fbSMatthew G. Knepley     }
472297e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4723e07394fbSMatthew G. Knepley   }
4724e07394fbSMatthew G. Knepley   /* Cleanup points */
4725923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4726e07394fbSMatthew G. Knepley   /* Cleanup array */
4727e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4728e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4729e07394fbSMatthew G. Knepley }
4730e07394fbSMatthew G. Knepley 
47317cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4732552f7358SJed Brown {
4733552f7358SJed Brown   PetscMPIInt    rank;
4734552f7358SJed Brown   PetscInt       i, j;
4735552f7358SJed Brown   PetscErrorCode ierr;
4736552f7358SJed Brown 
4737552f7358SJed Brown   PetscFunctionBegin;
473882f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4739eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
4740e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4741e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4742b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4743b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4744e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4745b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4746519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
47477eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4748552f7358SJed Brown #else
4749b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4750552f7358SJed Brown #endif
4751552f7358SJed Brown     }
475277a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4753552f7358SJed Brown   }
4754552f7358SJed Brown   PetscFunctionReturn(0);
4755552f7358SJed Brown }
4756552f7358SJed Brown 
4757552f7358SJed Brown /* . off - The global offset of this point */
47584acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4759a6dfd86eSKarl Rupp {
4760e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4761552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4762552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4763552f7358SJed Brown   PetscInt        cind = 0, k;
4764552f7358SJed Brown   PetscErrorCode  ierr;
4765552f7358SJed Brown 
4766552f7358SJed Brown   PetscFunctionBegin;
4767552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4768552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4769552f7358SJed Brown   if (!cdof || setBC) {
47704acb8e1eSToby Isaac     if (perm) {
47714acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4772552f7358SJed Brown     } else {
47734acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4774552f7358SJed Brown     }
4775552f7358SJed Brown   } else {
4776552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
47774acb8e1eSToby Isaac     if (perm) {
47784acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
47794acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
47804acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
47814acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
47824acb8e1eSToby Isaac           ++cind;
47834acb8e1eSToby Isaac         } else {
47844acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
47854acb8e1eSToby Isaac         }
47864acb8e1eSToby Isaac       }
47874acb8e1eSToby Isaac     } else {
4788552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4789552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4790552f7358SJed Brown           /* Insert check for returning constrained indices */
4791e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4792552f7358SJed Brown           ++cind;
4793552f7358SJed Brown         } else {
4794e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4795552f7358SJed Brown         }
4796552f7358SJed Brown       }
4797552f7358SJed Brown     }
4798552f7358SJed Brown   }
4799e6ccafaeSMatthew G Knepley   *loff += dof;
4800552f7358SJed Brown   PetscFunctionReturn(0);
4801552f7358SJed Brown }
4802552f7358SJed Brown 
48037e29afd2SMatthew G. Knepley /*
48047e29afd2SMatthew G. Knepley   This version only believes the point offset from the globalSection
48057e29afd2SMatthew G. Knepley 
48067e29afd2SMatthew G. Knepley  . off - The global offset of this point
48077e29afd2SMatthew G. Knepley */
48084acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4809a6dfd86eSKarl Rupp {
4810552f7358SJed Brown   PetscInt       numFields, foff, f;
4811552f7358SJed Brown   PetscErrorCode ierr;
4812552f7358SJed Brown 
4813552f7358SJed Brown   PetscFunctionBegin;
4814552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4815552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
48164acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4817552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
48184acb8e1eSToby Isaac     PetscInt        cind = 0, b;
48194acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4820552f7358SJed Brown 
4821552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4822552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4823552f7358SJed Brown     if (!cfdof || setBC) {
48244acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
48254acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4826552f7358SJed Brown     } else {
4827552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
48284acb8e1eSToby Isaac       if (perm) {
48294acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
48304acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
48314acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4832552f7358SJed Brown             ++cind;
4833552f7358SJed Brown           } else {
48344acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4835552f7358SJed Brown           }
4836552f7358SJed Brown         }
4837552f7358SJed Brown       } else {
48384acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
48394acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
48404acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4841552f7358SJed Brown             ++cind;
4842552f7358SJed Brown           } else {
48434acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4844552f7358SJed Brown           }
4845552f7358SJed Brown         }
4846552f7358SJed Brown       }
4847552f7358SJed Brown     }
4848a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4849552f7358SJed Brown     foffs[f] += fdof;
4850552f7358SJed Brown   }
4851552f7358SJed Brown   PetscFunctionReturn(0);
4852552f7358SJed Brown }
4853552f7358SJed Brown 
48547e29afd2SMatthew G. Knepley /*
48557e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
48567e29afd2SMatthew G. Knepley 
48577e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
48587e29afd2SMatthew G. Knepley */
48597e29afd2SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
48607e29afd2SMatthew G. Knepley {
48617e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
48627e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
48637e29afd2SMatthew G. Knepley 
48647e29afd2SMatthew G. Knepley   PetscFunctionBegin;
48657e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
48667e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
48677e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
48687e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
48697e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
48707e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
48717e29afd2SMatthew G. Knepley 
48727e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
48737e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
48747e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
48757e29afd2SMatthew G. Knepley     if (!cfdof || setBC) {
48767e29afd2SMatthew G. Knepley       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = foff+b;}}
48777e29afd2SMatthew G. Knepley       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = foff+b;}}
48787e29afd2SMatthew G. Knepley     } else {
48797e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
48807e29afd2SMatthew G. Knepley       if (perm) {
48817e29afd2SMatthew G. Knepley         for (b = 0; b < fdof; b++) {
48827e29afd2SMatthew G. Knepley           if ((cind < cfdof) && (b == fcdofs[cind])) {
48837e29afd2SMatthew G. Knepley             indices[foffs[f]+perm[b]] = -(foff+b+1);
48847e29afd2SMatthew G. Knepley             ++cind;
48857e29afd2SMatthew G. Knepley           } else {
48867e29afd2SMatthew G. Knepley             indices[foffs[f]+perm[b]] = foff+b-cind;
48877e29afd2SMatthew G. Knepley           }
48887e29afd2SMatthew G. Knepley         }
48897e29afd2SMatthew G. Knepley       } else {
48907e29afd2SMatthew G. Knepley         for (b = 0; b < fdof; b++) {
48917e29afd2SMatthew G. Knepley           if ((cind < cfdof) && (b == fcdofs[cind])) {
48927e29afd2SMatthew G. Knepley             indices[foffs[f]+b] = -(foff+b+1);
48937e29afd2SMatthew G. Knepley             ++cind;
48947e29afd2SMatthew G. Knepley           } else {
48957e29afd2SMatthew G. Knepley             indices[foffs[f]+b] = foff+b-cind;
48967e29afd2SMatthew G. Knepley           }
48977e29afd2SMatthew G. Knepley         }
48987e29afd2SMatthew G. Knepley       }
48997e29afd2SMatthew G. Knepley     }
49007e29afd2SMatthew G. Knepley     foffs[f] += fdof;
49017e29afd2SMatthew G. Knepley   }
49027e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
49037e29afd2SMatthew G. Knepley }
49047e29afd2SMatthew G. Knepley 
49054acb8e1eSToby 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)
4906d3d1a6afSToby Isaac {
4907d3d1a6afSToby Isaac   Mat             cMat;
4908d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4909d3d1a6afSToby Isaac   IS              aIS;
4910d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4911d3d1a6afSToby Isaac   const PetscInt  *anchors;
4912e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4913d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4914d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4915d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4916d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4917d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4918d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4919d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
49206ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4921d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4922d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4923d3d1a6afSToby Isaac 
4924d3d1a6afSToby Isaac   PetscFunctionBegin;
4925d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4926d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4927d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4928d3d1a6afSToby Isaac 
4929a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4930d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4931d3d1a6afSToby Isaac   if (aSec) {
4932d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4933d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4934d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4935d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4936d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4937d3d1a6afSToby Isaac      * into the global matrix anyway) */
4938d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4939d3d1a6afSToby Isaac       PetscInt b    = points[p];
49404b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4941d3d1a6afSToby Isaac 
49424b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
49434b2f2278SToby Isaac       if (!bSecDof) {
49444b2f2278SToby Isaac         continue;
49454b2f2278SToby Isaac       }
4946d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4947d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4948d3d1a6afSToby Isaac       }
4949d3d1a6afSToby Isaac       if (bDof) {
4950d3d1a6afSToby Isaac         /* this point is constrained */
4951d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4952d3d1a6afSToby Isaac         PetscInt bOff, q;
4953d3d1a6afSToby Isaac 
4954d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4955d3d1a6afSToby Isaac         newNumPoints  += bDof;
4956d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4957d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4958d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4959d3d1a6afSToby Isaac           PetscInt aDof;
4960d3d1a6afSToby Isaac 
4961d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4962d3d1a6afSToby Isaac           newNumIndices += aDof;
4963d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4964d3d1a6afSToby Isaac             PetscInt fDof;
4965d3d1a6afSToby Isaac 
4966d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4967d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4968d3d1a6afSToby Isaac           }
4969d3d1a6afSToby Isaac         }
4970d3d1a6afSToby Isaac       }
4971d3d1a6afSToby Isaac       else {
4972d3d1a6afSToby Isaac         /* this point is not constrained */
4973d3d1a6afSToby Isaac         newNumPoints++;
49744b2f2278SToby Isaac         newNumIndices += bSecDof;
4975d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4976d3d1a6afSToby Isaac           PetscInt fDof;
4977d3d1a6afSToby Isaac 
4978d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4979d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4980d3d1a6afSToby Isaac         }
4981d3d1a6afSToby Isaac       }
4982d3d1a6afSToby Isaac     }
4983d3d1a6afSToby Isaac   }
4984d3d1a6afSToby Isaac   if (!anyConstrained) {
498572b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
498672b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
498772b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
498872b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
498972b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4990d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4991d3d1a6afSToby Isaac   }
4992d3d1a6afSToby Isaac 
49936ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
49946ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
49956ecaa68aSToby Isaac 
4996f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4997d3d1a6afSToby Isaac 
49986ecaa68aSToby Isaac   if (!outPoints && !outValues) {
49996ecaa68aSToby Isaac     if (offsets) {
50006ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
50016ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
50026ecaa68aSToby Isaac       }
50036ecaa68aSToby Isaac     }
50046ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
50056ecaa68aSToby Isaac     PetscFunctionReturn(0);
50066ecaa68aSToby Isaac   }
50076ecaa68aSToby Isaac 
5008f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5009d3d1a6afSToby Isaac 
5010f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5011d3d1a6afSToby Isaac 
5012d3d1a6afSToby Isaac   /* workspaces */
5013d3d1a6afSToby Isaac   if (numFields) {
5014d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
501569291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
501669291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5017d3d1a6afSToby Isaac     }
5018d3d1a6afSToby Isaac   }
5019d3d1a6afSToby Isaac   else {
502069291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
502169291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5022d3d1a6afSToby Isaac   }
5023d3d1a6afSToby Isaac 
5024d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5025d3d1a6afSToby Isaac   if (numFields) {
50264b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
50274b2f2278SToby Isaac 
5028d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5029d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
50304b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5031d3d1a6afSToby Isaac 
50324b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
50334b2f2278SToby Isaac       if (!bSecDof) {
50344b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
50354b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
50364b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
50374b2f2278SToby Isaac         }
50384b2f2278SToby Isaac         continue;
50394b2f2278SToby Isaac       }
5040d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5041d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5042d3d1a6afSToby Isaac       }
5043d3d1a6afSToby Isaac       if (bDof) {
5044d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5045d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5046d3d1a6afSToby Isaac 
5047d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5048d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5049d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5050d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5051d3d1a6afSToby Isaac             PetscInt aFDof;
5052d3d1a6afSToby Isaac 
5053d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5054d3d1a6afSToby Isaac             allFDof += aFDof;
5055d3d1a6afSToby Isaac           }
5056d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5057d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5058d3d1a6afSToby Isaac         }
5059d3d1a6afSToby Isaac       }
5060d3d1a6afSToby Isaac       else {
5061d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5062d3d1a6afSToby Isaac           PetscInt fDof;
5063d3d1a6afSToby Isaac 
5064d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5065d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5066d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5067d3d1a6afSToby Isaac         }
5068d3d1a6afSToby Isaac       }
5069d3d1a6afSToby Isaac     }
50704b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
50714b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
50724b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5073d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5074d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5075d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5076d3d1a6afSToby Isaac       }
507719f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
507819f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
507969291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5080d3d1a6afSToby Isaac     }
5081d3d1a6afSToby Isaac   }
5082d3d1a6afSToby Isaac   else {
5083d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5084d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
50854b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5086d3d1a6afSToby Isaac 
50874b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
50884b2f2278SToby Isaac       if (!bSecDof) {
50894b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
50904b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
50914b2f2278SToby Isaac         continue;
50924b2f2278SToby Isaac       }
5093d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5094d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5095d3d1a6afSToby Isaac       }
5096d3d1a6afSToby Isaac       if (bDof) {
50974b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5098d3d1a6afSToby Isaac 
5099d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5100d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5101d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5102d3d1a6afSToby Isaac 
5103d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5104d3d1a6afSToby Isaac           allDof += aDof;
5105d3d1a6afSToby Isaac         }
5106d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
51074b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5108d3d1a6afSToby Isaac       }
5109d3d1a6afSToby Isaac       else {
51104b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5111d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5112d3d1a6afSToby Isaac       }
5113d3d1a6afSToby Isaac     }
5114d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5115d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5116d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5117d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5118d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5119d3d1a6afSToby Isaac     }
512069291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5121d3d1a6afSToby Isaac   }
5122d3d1a6afSToby Isaac 
51236ecaa68aSToby Isaac   /* output arrays */
512469291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
51256ecaa68aSToby Isaac 
5126d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5127d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5128d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
512969291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
513069291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5131d3d1a6afSToby Isaac   if (numFields) {
5132d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5133d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5134d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
51354b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5136d3d1a6afSToby Isaac 
51374b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
51384b2f2278SToby Isaac       if (!bSecDof) {
51394b2f2278SToby Isaac         continue;
51404b2f2278SToby Isaac       }
5141d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5142d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5143d3d1a6afSToby Isaac       }
5144d3d1a6afSToby Isaac       if (bDof) {
5145d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5146d3d1a6afSToby Isaac 
5147d3d1a6afSToby Isaac         fStart[0] = 0;
5148d3d1a6afSToby Isaac         fEnd[0]   = 0;
5149d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5150d3d1a6afSToby Isaac           PetscInt fDof;
5151d3d1a6afSToby Isaac 
5152d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5153d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5154d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5155d3d1a6afSToby Isaac         }
5156d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
51574acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
5158d3d1a6afSToby Isaac 
5159d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5160d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5161d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5162d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5163d3d1a6afSToby Isaac 
5164d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5165d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5166d3d1a6afSToby Isaac         }
5167d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5168d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5169d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5170d3d1a6afSToby Isaac 
5171d3d1a6afSToby 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 */
5172d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5173d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5174302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
51754acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
5176d3d1a6afSToby Isaac         }
5177d3d1a6afSToby Isaac         newP += bDof;
5178d3d1a6afSToby Isaac 
51796ecaa68aSToby Isaac         if (outValues) {
5180d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5181d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5182d3d1a6afSToby 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);
5183d3d1a6afSToby Isaac           }
5184d3d1a6afSToby Isaac         }
51856ecaa68aSToby Isaac       }
5186d3d1a6afSToby Isaac       else {
5187d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5188d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5189d3d1a6afSToby Isaac         newP++;
5190d3d1a6afSToby Isaac       }
5191d3d1a6afSToby Isaac     }
5192d3d1a6afSToby Isaac   } else {
5193d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5194d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5195d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
51964b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5197d3d1a6afSToby Isaac 
51984b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
51994b2f2278SToby Isaac       if (!bSecDof) {
52004b2f2278SToby Isaac         continue;
52014b2f2278SToby Isaac       }
5202d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5203d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5204d3d1a6afSToby Isaac       }
5205d3d1a6afSToby Isaac       if (bDof) {
5206d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5207d3d1a6afSToby Isaac 
5208d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
52094acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
5210d3d1a6afSToby Isaac 
5211d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5212d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5213d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5214d3d1a6afSToby Isaac 
5215d3d1a6afSToby 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 */
5216d3d1a6afSToby Isaac 
5217d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5218d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5219302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
52204acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
5221d3d1a6afSToby Isaac         }
5222d3d1a6afSToby Isaac         newP += bDof;
5223d3d1a6afSToby Isaac 
5224d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
52256ecaa68aSToby Isaac         if (outValues) {
5226d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5227d3d1a6afSToby Isaac         }
52286ecaa68aSToby Isaac       }
5229d3d1a6afSToby Isaac       else {
5230d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5231d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5232d3d1a6afSToby Isaac         newP++;
5233d3d1a6afSToby Isaac       }
5234d3d1a6afSToby Isaac     }
5235d3d1a6afSToby Isaac   }
5236d3d1a6afSToby Isaac 
52376ecaa68aSToby Isaac   if (outValues) {
523869291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5239d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5240d3d1a6afSToby Isaac     /* multiply constraints on the right */
5241d3d1a6afSToby Isaac     if (numFields) {
5242d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5243d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5244d3d1a6afSToby Isaac 
5245d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5246d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5247d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5248d3d1a6afSToby Isaac           PetscInt c, r, k;
5249d3d1a6afSToby Isaac           PetscInt dof;
5250d3d1a6afSToby Isaac 
5251d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
52524b2f2278SToby Isaac           if (!dof) {
52534b2f2278SToby Isaac             continue;
52544b2f2278SToby Isaac           }
5255d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5256d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5257d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5258d3d1a6afSToby Isaac 
5259d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5260d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5261d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
52624acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5263d3d1a6afSToby Isaac                 }
5264d3d1a6afSToby Isaac               }
5265d3d1a6afSToby Isaac             }
5266d3d1a6afSToby Isaac           }
5267d3d1a6afSToby Isaac           else {
5268d3d1a6afSToby Isaac             /* copy this column as is */
5269d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5270d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5271d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5272d3d1a6afSToby Isaac               }
5273d3d1a6afSToby Isaac             }
5274d3d1a6afSToby Isaac           }
5275d3d1a6afSToby Isaac           oldOff += dof;
5276d3d1a6afSToby Isaac         }
5277d3d1a6afSToby Isaac       }
5278d3d1a6afSToby Isaac     }
5279d3d1a6afSToby Isaac     else {
5280d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5281d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5282d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5283d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5284d3d1a6afSToby Isaac         PetscInt c, r, k;
5285d3d1a6afSToby Isaac         PetscInt dof;
5286d3d1a6afSToby Isaac 
5287d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
52884b2f2278SToby Isaac         if (!dof) {
52894b2f2278SToby Isaac           continue;
52904b2f2278SToby Isaac         }
5291d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5292d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5293d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5294d3d1a6afSToby Isaac 
5295d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5296d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5297d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5298d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5299d3d1a6afSToby Isaac               }
5300d3d1a6afSToby Isaac             }
5301d3d1a6afSToby Isaac           }
5302d3d1a6afSToby Isaac         }
5303d3d1a6afSToby Isaac         else {
5304d3d1a6afSToby Isaac           /* copy this column as is */
5305d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5306d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5307d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5308d3d1a6afSToby Isaac             }
5309d3d1a6afSToby Isaac           }
5310d3d1a6afSToby Isaac         }
5311d3d1a6afSToby Isaac         oldOff += dof;
5312d3d1a6afSToby Isaac       }
5313d3d1a6afSToby Isaac     }
5314d3d1a6afSToby Isaac 
53156ecaa68aSToby Isaac     if (multiplyLeft) {
531669291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5317d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5318d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5319d3d1a6afSToby Isaac       if (numFields) {
5320d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5321d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5322d3d1a6afSToby Isaac 
5323d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5324d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5325d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5326d3d1a6afSToby Isaac             PetscInt c, r, k;
5327d3d1a6afSToby Isaac             PetscInt dof;
5328d3d1a6afSToby Isaac 
5329d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5330d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5331d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5332d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5333d3d1a6afSToby Isaac 
5334d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5335d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5336d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5337d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5338d3d1a6afSToby Isaac                   }
5339d3d1a6afSToby Isaac                 }
5340d3d1a6afSToby Isaac               }
5341d3d1a6afSToby Isaac             }
5342d3d1a6afSToby Isaac             else {
5343d3d1a6afSToby Isaac               /* copy this row as is */
5344d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5345d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5346d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5347d3d1a6afSToby Isaac                 }
5348d3d1a6afSToby Isaac               }
5349d3d1a6afSToby Isaac             }
5350d3d1a6afSToby Isaac             oldOff += dof;
5351d3d1a6afSToby Isaac           }
5352d3d1a6afSToby Isaac         }
5353d3d1a6afSToby Isaac       }
5354d3d1a6afSToby Isaac       else {
5355d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5356d3d1a6afSToby Isaac 
5357d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5358d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5359d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5360d3d1a6afSToby Isaac           PetscInt c, r, k;
5361d3d1a6afSToby Isaac           PetscInt dof;
5362d3d1a6afSToby Isaac 
5363d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5364d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5365d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5366d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5367d3d1a6afSToby Isaac 
5368d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5369d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5370d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5371d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5372d3d1a6afSToby Isaac                 }
5373d3d1a6afSToby Isaac               }
5374d3d1a6afSToby Isaac             }
5375d3d1a6afSToby Isaac           }
5376d3d1a6afSToby Isaac           else {
5377d3d1a6afSToby Isaac             /* copy this row as is */
53789fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5379d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5380d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5381d3d1a6afSToby Isaac               }
5382d3d1a6afSToby Isaac             }
5383d3d1a6afSToby Isaac           }
5384d3d1a6afSToby Isaac           oldOff += dof;
5385d3d1a6afSToby Isaac         }
5386d3d1a6afSToby Isaac       }
5387d3d1a6afSToby Isaac 
538869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
53896ecaa68aSToby Isaac     }
53906ecaa68aSToby Isaac     else {
53916ecaa68aSToby Isaac       newValues = tmpValues;
53926ecaa68aSToby Isaac     }
53936ecaa68aSToby Isaac   }
53946ecaa68aSToby Isaac 
5395d3d1a6afSToby Isaac   /* clean up */
539669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
539769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
53986ecaa68aSToby Isaac 
5399d3d1a6afSToby Isaac   if (numFields) {
5400d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
540169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
540269291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
540369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5404d3d1a6afSToby Isaac     }
5405d3d1a6afSToby Isaac   }
5406d3d1a6afSToby Isaac   else {
540769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
540869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
540969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5410d3d1a6afSToby Isaac   }
5411d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5412d3d1a6afSToby Isaac 
5413d3d1a6afSToby Isaac   /* output */
54146ecaa68aSToby Isaac   if (outPoints) {
5415d3d1a6afSToby Isaac     *outPoints = newPoints;
54166ecaa68aSToby Isaac   }
54176ecaa68aSToby Isaac   else {
541869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
54196ecaa68aSToby Isaac   }
542031620726SToby Isaac   if (outValues) {
5421d3d1a6afSToby Isaac     *outValues = newValues;
54226ecaa68aSToby Isaac   }
54236ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5424d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5425d3d1a6afSToby Isaac   }
5426d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5427d3d1a6afSToby Isaac }
5428d3d1a6afSToby Isaac 
54297cd05799SMatthew G. Knepley /*@C
5430a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
54317cd05799SMatthew G. Knepley 
54327cd05799SMatthew G. Knepley   Not collective
54337cd05799SMatthew G. Knepley 
54347cd05799SMatthew G. Knepley   Input Parameters:
54357cd05799SMatthew G. Knepley + dm - The DM
54367cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
54377cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
54387cd05799SMatthew G. Knepley - point - The mesh point
54397cd05799SMatthew G. Knepley 
54407cd05799SMatthew G. Knepley   Output parameters:
54417cd05799SMatthew G. Knepley + numIndices - The number of indices
54427cd05799SMatthew G. Knepley . indices - The indices
54437cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
54447cd05799SMatthew G. Knepley 
54457cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
54467cd05799SMatthew G. Knepley 
54477cd05799SMatthew G. Knepley   Level: advanced
54487cd05799SMatthew G. Knepley 
54497cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
54507cd05799SMatthew G. Knepley @*/
54516ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
54527773e69fSMatthew G. Knepley {
54537773e69fSMatthew G. Knepley   PetscSection    clSection;
54547773e69fSMatthew G. Knepley   IS              clPoints;
54557773e69fSMatthew G. Knepley   const PetscInt *clp;
54564acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
54577773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
54587773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
54597773e69fSMatthew G. Knepley   PetscInt        offsets[32];
54607773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
54617773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
54627773e69fSMatthew G. Knepley 
54637773e69fSMatthew G. Knepley   PetscFunctionBegin;
54647773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54657773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
54667773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
54677773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
54687773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
54697773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
54707773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
54717773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
54727773e69fSMatthew G. Knepley   /* Get points in closure */
5473923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54747773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
54757773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
54767773e69fSMatthew G. Knepley     PetscInt dof, fdof;
54777773e69fSMatthew G. Knepley 
54787773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
54797773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
54807773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
54817773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
54827773e69fSMatthew G. Knepley     }
54837773e69fSMatthew G. Knepley     Nind += dof;
54847773e69fSMatthew G. Knepley   }
54857773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
54868ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
54874acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
54884acb8e1eSToby Isaac   /* Get dual space symmetries */
54894acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
54904acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54914acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54924acb8e1eSToby Isaac   }
54937773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
54947773e69fSMatthew G. Knepley   {
54954acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
54967773e69fSMatthew G. Knepley     if (numPointsNew) {
54974acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
54984acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54994acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55004acb8e1eSToby Isaac       }
55014acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
55024acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
55034acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
55044acb8e1eSToby Isaac       }
5505923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55067773e69fSMatthew G. Knepley       numPoints = numPointsNew;
55077773e69fSMatthew G. Knepley       Nind      = NindNew;
55087773e69fSMatthew G. Knepley       points    = pointsNew;
55097773e69fSMatthew G. Knepley     }
55107773e69fSMatthew G. Knepley   }
55117773e69fSMatthew G. Knepley   /* Calculate indices */
551269291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
55137773e69fSMatthew G. Knepley   if (Nf) {
55146ecaa68aSToby Isaac     if (outOffsets) {
55156ecaa68aSToby Isaac       PetscInt f;
55166ecaa68aSToby Isaac 
551746bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
55186ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
55196ecaa68aSToby Isaac       }
55206ecaa68aSToby Isaac     }
55214acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
55224acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55234acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
55247773e69fSMatthew G. Knepley     }
55257773e69fSMatthew G. Knepley   } else {
55264acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
55274acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
55284acb8e1eSToby Isaac 
55294acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55304acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
55317773e69fSMatthew G. Knepley     }
55327773e69fSMatthew G. Knepley   }
55337773e69fSMatthew G. Knepley   /* Cleanup points */
55344acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
55354acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55364acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55374acb8e1eSToby Isaac   }
55387773e69fSMatthew G. Knepley   if (numPointsNew) {
553969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
55407773e69fSMatthew G. Knepley   } else {
5541923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55427773e69fSMatthew G. Knepley   }
55437773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
55447773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55457773e69fSMatthew G. Knepley }
55467773e69fSMatthew G. Knepley 
55477cd05799SMatthew G. Knepley /*@C
55487cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
55497cd05799SMatthew G. Knepley 
55507cd05799SMatthew G. Knepley   Not collective
55517cd05799SMatthew G. Knepley 
55527cd05799SMatthew G. Knepley   Input Parameters:
55537cd05799SMatthew G. Knepley + dm - The DM
55547cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
55557cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
55567cd05799SMatthew G. Knepley . point - The mesh point
55577cd05799SMatthew G. Knepley . numIndices - The number of indices
55587cd05799SMatthew G. Knepley . indices - The indices
55597cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
55607cd05799SMatthew G. Knepley 
55617cd05799SMatthew G. Knepley   Level: advanced
55627cd05799SMatthew G. Knepley 
55637cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
55647cd05799SMatthew G. Knepley @*/
556546bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
55667773e69fSMatthew G. Knepley {
55677773e69fSMatthew G. Knepley   PetscErrorCode ierr;
55687773e69fSMatthew G. Knepley 
55697773e69fSMatthew G. Knepley   PetscFunctionBegin;
55707773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55717773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
557269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
55737773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55747773e69fSMatthew G. Knepley }
55757773e69fSMatthew G. Knepley 
55767f5d1fdeSMatthew G. Knepley /*@C
55777f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
55787f5d1fdeSMatthew G. Knepley 
55797f5d1fdeSMatthew G. Knepley   Not collective
55807f5d1fdeSMatthew G. Knepley 
55817f5d1fdeSMatthew G. Knepley   Input Parameters:
55827f5d1fdeSMatthew G. Knepley + dm - The DM
5583ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5584ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
55857f5d1fdeSMatthew G. Knepley . A - The matrix
5586eaf898f9SPatrick Sanan . point - The point in the DM
55877f5d1fdeSMatthew G. Knepley . values - The array of values
55887f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
55897f5d1fdeSMatthew G. Knepley 
55907f5d1fdeSMatthew G. Knepley   Fortran Notes:
55917f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
55927f5d1fdeSMatthew G. Knepley 
55937f5d1fdeSMatthew G. Knepley   Level: intermediate
55947f5d1fdeSMatthew G. Knepley 
55957f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
55967f5d1fdeSMatthew G. Knepley @*/
55977c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5598552f7358SJed Brown {
5599552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
56001b406b76SMatthew G. Knepley   PetscSection        clSection;
56011b406b76SMatthew G. Knepley   IS                  clPoints;
5602d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
56031b406b76SMatthew G. Knepley   const PetscInt     *clp;
5604552f7358SJed Brown   PetscInt           *indices;
5605552f7358SJed Brown   PetscInt            offsets[32];
56064acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
56074acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5608923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
56094acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5610d3d1a6afSToby Isaac   PetscScalar        *newValues;
5611552f7358SJed Brown   PetscErrorCode      ierr;
5612552f7358SJed Brown 
5613552f7358SJed Brown   PetscFunctionBegin;
5614552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5615e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
56163dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5617e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
56183dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
56193dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5620552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
562182f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5622552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5623923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5624552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5625552f7358SJed Brown     PetscInt fdof;
5626552f7358SJed Brown 
5627552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5628552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5629552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5630552f7358SJed Brown       offsets[f+1] += fdof;
5631552f7358SJed Brown     }
5632552f7358SJed Brown     numIndices += dof;
5633552f7358SJed Brown   }
56340d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
56350d644c17SKarl Rupp 
56368ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
56374acb8e1eSToby Isaac   /* Get symmetries */
56384acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
56394acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56404acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56414acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
56424acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
56434acb8e1eSToby Isaac 
56444acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
56454acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
56464acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
56474acb8e1eSToby Isaac 
56484acb8e1eSToby Isaac         if (!numFields) {
56494acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
56504acb8e1eSToby Isaac         } else {
56514acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
56524acb8e1eSToby Isaac         }
56534acb8e1eSToby Isaac         if (flip) {
56544acb8e1eSToby Isaac           PetscInt i, j, k;
56554acb8e1eSToby Isaac 
56564acb8e1eSToby Isaac           if (!valCopy) {
565769291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56584acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
56594acb8e1eSToby Isaac             values = valCopy;
56604acb8e1eSToby Isaac           }
56614acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5662775f7be2SToby Isaac             PetscScalar fval = flip[i];
56634acb8e1eSToby Isaac 
56644acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
56654acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
56664acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
56674acb8e1eSToby Isaac             }
56684acb8e1eSToby Isaac           }
56694acb8e1eSToby Isaac         }
56704acb8e1eSToby Isaac         foffset += fdof;
56714acb8e1eSToby Isaac       }
56724acb8e1eSToby Isaac     }
56734acb8e1eSToby Isaac   }
56744acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5675d3d1a6afSToby Isaac   if (newNumPoints) {
56764acb8e1eSToby Isaac     if (valCopy) {
567769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56784acb8e1eSToby Isaac     }
56794acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56804acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56814acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56824acb8e1eSToby Isaac     }
56834acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56844acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56854acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56864acb8e1eSToby Isaac     }
5687923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5688d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5689d3d1a6afSToby Isaac     numIndices = newNumIndices;
5690d3d1a6afSToby Isaac     points     = newPoints;
5691d3d1a6afSToby Isaac     values     = newValues;
5692d3d1a6afSToby Isaac   }
569369291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5694552f7358SJed Brown   if (numFields) {
56957e29afd2SMatthew G. Knepley     PetscBool useFieldOffsets;
56967e29afd2SMatthew G. Knepley 
56977e29afd2SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(globalSection, &useFieldOffsets);CHKERRQ(ierr);
56987e29afd2SMatthew G. Knepley     if (useFieldOffsets) {
56997e29afd2SMatthew G. Knepley       for (p = 0; p < numPoints; p++) {
57007e29afd2SMatthew G. Knepley         DMPlexGetIndicesPointFieldsSplit_Internal(section, globalSection, points[2*p], offsets, PETSC_FALSE, perms, p, indices);
57017e29afd2SMatthew G. Knepley       }
57027e29afd2SMatthew G. Knepley     } else {
57034acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
57044acb8e1eSToby Isaac         ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
57054acb8e1eSToby Isaac         DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5706552f7358SJed Brown       }
57077e29afd2SMatthew G. Knepley     }
5708552f7358SJed Brown   } else {
57094acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
57104acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
57114acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
57124acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5713552f7358SJed Brown     }
5714552f7358SJed Brown   }
5715b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5716552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5717ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5718ab1d0545SMatthew G. Knepley     PetscInt i;
5719ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
57208ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5721ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5722ab1d0545SMatthew G. Knepley   }
5723552f7358SJed Brown   if (ierr) {
5724552f7358SJed Brown     PetscMPIInt    rank;
5725552f7358SJed Brown     PetscErrorCode ierr2;
5726552f7358SJed Brown 
572782f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5728e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5729b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
573069291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
5731552f7358SJed Brown     CHKERRQ(ierr);
5732552f7358SJed Brown   }
57334acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
57344acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57354acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57364acb8e1eSToby Isaac   }
5737d3d1a6afSToby Isaac   if (newNumPoints) {
573869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
573969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
5740d3d1a6afSToby Isaac   }
5741d3d1a6afSToby Isaac   else {
57424acb8e1eSToby Isaac     if (valCopy) {
574369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
57444acb8e1eSToby Isaac     }
5745923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5746d3d1a6afSToby Isaac   }
574769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5748552f7358SJed Brown   PetscFunctionReturn(0);
5749552f7358SJed Brown }
5750552f7358SJed Brown 
5751de41b84cSMatthew 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)
5752de41b84cSMatthew G. Knepley {
5753de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5754de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5755de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5756de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5757de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5758de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
57594ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5760de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5761de41b84cSMatthew G. Knepley 
5762de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5763de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5764de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5765e87a4003SBarry Smith   if (!fsection) {ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);}
5766de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5767e87a4003SBarry Smith   if (!csection) {ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);}
5768de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5769e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5770de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5771e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5772de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5773de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5774de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5775de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5776de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5777de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5778de41b84cSMatthew G. Knepley   /* Column indices */
5779de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57804ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5781de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5782de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5783de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5784de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5785de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5786de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5787de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5788de41b84cSMatthew G. Knepley       ++q;
5789de41b84cSMatthew G. Knepley     }
5790de41b84cSMatthew G. Knepley   }
5791de41b84cSMatthew G. Knepley   numCPoints = q;
5792de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5793de41b84cSMatthew G. Knepley     PetscInt fdof;
5794de41b84cSMatthew G. Knepley 
5795de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
57964ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5797de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5798de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5799de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5800de41b84cSMatthew G. Knepley     }
5801de41b84cSMatthew G. Knepley     numCIndices += dof;
5802de41b84cSMatthew G. Knepley   }
5803de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5804de41b84cSMatthew G. Knepley   /* Row indices */
5805de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5806de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
580769291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5808de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5809de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5810de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5811de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5812de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5813de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5814de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
58154ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
58164ca5e9f5SMatthew G. Knepley         if (!dof) continue;
58174ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
58184ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5819de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5820de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5821de41b84cSMatthew G. Knepley         ++q;
5822de41b84cSMatthew G. Knepley       }
5823de41b84cSMatthew G. Knepley     }
5824de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5825de41b84cSMatthew G. Knepley   }
5826de41b84cSMatthew G. Knepley   numFPoints = q;
5827de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5828de41b84cSMatthew G. Knepley     PetscInt fdof;
5829de41b84cSMatthew G. Knepley 
5830de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
58314ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5832de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5833de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5834de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5835de41b84cSMatthew G. Knepley     }
5836de41b84cSMatthew G. Knepley     numFIndices += dof;
5837de41b84cSMatthew G. Knepley   }
5838de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5839de41b84cSMatthew G. Knepley 
58408ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
58418ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
584269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
584369291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5844de41b84cSMatthew G. Knepley   if (numFields) {
58454acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
58464acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
58474acb8e1eSToby Isaac 
58484acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58494acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58504acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5851de41b84cSMatthew G. Knepley     }
58524acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
58534acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
5854367003a6SStefano Zampini       ierr = DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);CHKERRQ(ierr);
58554acb8e1eSToby Isaac     }
58564acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
58574acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
5858367003a6SStefano Zampini       ierr = DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);CHKERRQ(ierr);
58594acb8e1eSToby Isaac     }
58604acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58614acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58624acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5863de41b84cSMatthew G. Knepley     }
5864de41b84cSMatthew G. Knepley   } else {
58654acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
58664acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
58674acb8e1eSToby Isaac 
58684acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58694acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
58704acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
58714acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
58724acb8e1eSToby Isaac 
58734acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
58744acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5875de41b84cSMatthew G. Knepley     }
58764acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
58774acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
58784acb8e1eSToby Isaac 
58794acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
58804acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5881de41b84cSMatthew G. Knepley     }
58824acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58834acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5884de41b84cSMatthew G. Knepley   }
5885de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
58864acb8e1eSToby Isaac   /* TODO: flips */
5887de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5888de41b84cSMatthew G. Knepley   if (ierr) {
5889de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5890de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5891de41b84cSMatthew G. Knepley 
5892de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5893e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5894de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
589569291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
589669291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
5897de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5898de41b84cSMatthew G. Knepley   }
589969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5900de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
590169291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
590269291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5903de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5904de41b84cSMatthew G. Knepley }
5905de41b84cSMatthew G. Knepley 
59067c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
59077c927364SMatthew G. Knepley {
59087c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
59097c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
59107c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
59117c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
59127c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
59137c927364SMatthew G. Knepley   PetscErrorCode ierr;
59147c927364SMatthew G. Knepley 
59157c927364SMatthew G. Knepley   PetscFunctionBegin;
59167c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
59177c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5918e87a4003SBarry Smith   if (!fsection) {ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);}
59197c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5920e87a4003SBarry Smith   if (!csection) {ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);}
59217c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5922e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
59237c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5924e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
59257c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
59267c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
59277c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
59287c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
59297c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
59307c927364SMatthew G. Knepley   /* Column indices */
59317c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
59327c927364SMatthew G. Knepley   maxFPoints = numCPoints;
59337c927364SMatthew G. Knepley   /* Compress out points not in the section */
59347c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
59357c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
59367c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
59377c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
59387c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
59397c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
59407c927364SMatthew G. Knepley       ++q;
59417c927364SMatthew G. Knepley     }
59427c927364SMatthew G. Knepley   }
59437c927364SMatthew G. Knepley   numCPoints = q;
59447c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
59457c927364SMatthew G. Knepley     PetscInt fdof;
59467c927364SMatthew G. Knepley 
59477c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
59487c927364SMatthew G. Knepley     if (!dof) continue;
59497c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59507c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
59517c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
59527c927364SMatthew G. Knepley     }
59537c927364SMatthew G. Knepley     numCIndices += dof;
59547c927364SMatthew G. Knepley   }
59557c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
59567c927364SMatthew G. Knepley   /* Row indices */
59577c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
59587c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
595969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
59607c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
59617c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
59627c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59637c927364SMatthew G. Knepley     /* Compress out points not in the section */
59647c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
59657c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
59667c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
59677c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
59687c927364SMatthew G. Knepley         if (!dof) continue;
59697c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
59707c927364SMatthew G. Knepley         if (s < q) continue;
59717c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
59727c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
59737c927364SMatthew G. Knepley         ++q;
59747c927364SMatthew G. Knepley       }
59757c927364SMatthew G. Knepley     }
59767c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59777c927364SMatthew G. Knepley   }
59787c927364SMatthew G. Knepley   numFPoints = q;
59797c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
59807c927364SMatthew G. Knepley     PetscInt fdof;
59817c927364SMatthew G. Knepley 
59827c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
59837c927364SMatthew G. Knepley     if (!dof) continue;
59847c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59857c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
59867c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
59877c927364SMatthew G. Knepley     }
59887c927364SMatthew G. Knepley     numFIndices += dof;
59897c927364SMatthew G. Knepley   }
59907c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
59917c927364SMatthew G. Knepley 
59928ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
59938ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
59947c927364SMatthew G. Knepley   if (numFields) {
59954acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
59964acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
59974acb8e1eSToby Isaac 
59984acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
59994acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
60004acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
60017c927364SMatthew G. Knepley     }
60024acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
60034acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
60044acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
60054acb8e1eSToby Isaac     }
60064acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
60074acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
60084acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
60094acb8e1eSToby Isaac     }
60104acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
60114acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
60124acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
60137c927364SMatthew G. Knepley     }
60147c927364SMatthew G. Knepley   } else {
60154acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
60164acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
60174acb8e1eSToby Isaac 
60184acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
60194acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
60204acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
60214acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
60224acb8e1eSToby Isaac 
60234acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
60244acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
60257c927364SMatthew G. Knepley     }
60264acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
60274acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
60284acb8e1eSToby Isaac 
60294acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
60304acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
60317c927364SMatthew G. Knepley     }
60324acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
60334acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
60347c927364SMatthew G. Knepley   }
603569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
60367c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
60377c927364SMatthew G. Knepley   PetscFunctionReturn(0);
60387c927364SMatthew G. Knepley }
60397c927364SMatthew G. Knepley 
6040f96281f8SMatthew G. Knepley /*@
6041f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6042f96281f8SMatthew G. Knepley 
6043f96281f8SMatthew G. Knepley   Input Parameter:
6044f96281f8SMatthew G. Knepley . dm - The DMPlex object
6045f96281f8SMatthew G. Knepley 
6046f96281f8SMatthew G. Knepley   Output Parameters:
6047f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6048dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6049dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6050dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6051f96281f8SMatthew G. Knepley 
6052f96281f8SMatthew G. Knepley   Level: developer
6053f96281f8SMatthew G. Knepley 
6054dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6055f96281f8SMatthew G. Knepley @*/
6056770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6057552f7358SJed Brown {
6058552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6059770b213bSMatthew G Knepley   PetscInt       dim;
6060770b213bSMatthew G Knepley   PetscErrorCode ierr;
6061552f7358SJed Brown 
6062552f7358SJed Brown   PetscFunctionBegin;
6063552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6064c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
60657d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6066770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
60677d5acc75SStefano Zampini   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6068770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6069770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6070552f7358SJed Brown   PetscFunctionReturn(0);
6071552f7358SJed Brown }
6072552f7358SJed Brown 
6073aeadca18SToby Isaac static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
60744a3e9fdbSToby Isaac {
60754a3e9fdbSToby Isaac   IS             is, his;
60767d5acc75SStefano Zampini   PetscInt       first = 0, stride;
60774a3e9fdbSToby Isaac   PetscBool      isStride;
60784a3e9fdbSToby Isaac   PetscErrorCode ierr;
60794a3e9fdbSToby Isaac 
60804a3e9fdbSToby Isaac   PetscFunctionBegin;
60814a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel, d, &is);CHKERRQ(ierr);
60824a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);CHKERRQ(ierr);
60834a3e9fdbSToby Isaac   if (isStride) {
60844a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(is, &first, &stride);CHKERRQ(ierr);
60854a3e9fdbSToby Isaac   }
60867d5acc75SStefano 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);
60874a3e9fdbSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);CHKERRQ(ierr);
6088aeadca18SToby Isaac   ierr = DMLabelSetStratumIS(dimLabel, d, his);CHKERRQ(ierr);
60894a3e9fdbSToby Isaac   ierr = ISDestroy(&his);CHKERRQ(ierr);
60904a3e9fdbSToby Isaac   ierr = ISDestroy(&is);CHKERRQ(ierr);
60914a3e9fdbSToby Isaac   PetscFunctionReturn(0);
60924a3e9fdbSToby Isaac }
60934a3e9fdbSToby Isaac 
6094dc5a3409SMatthew G. Knepley /*@
6095dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6096dc5a3409SMatthew G. Knepley 
6097dc5a3409SMatthew G. Knepley   Input Parameters:
6098dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
6099dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6100dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6101dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6102dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6103dc5a3409SMatthew G. Knepley 
6104dc5a3409SMatthew G. Knepley   Level: developer
6105dc5a3409SMatthew G. Knepley 
6106dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6107dc5a3409SMatthew G. Knepley @*/
6108770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6109552f7358SJed Brown {
6110552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6111770b213bSMatthew G Knepley   PetscInt       dim;
6112770b213bSMatthew G Knepley   PetscErrorCode ierr;
6113552f7358SJed Brown 
6114552f7358SJed Brown   PetscFunctionBegin;
6115552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6116c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
61177d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6118770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
61197d5acc75SStefano Zampini   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6120770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6121770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6122552f7358SJed Brown   PetscFunctionReturn(0);
6123552f7358SJed Brown }
6124552f7358SJed Brown 
61257cd05799SMatthew G. Knepley /*@C
61267cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
61277cd05799SMatthew G. Knepley 
61287cd05799SMatthew G. Knepley   Input Parameter:
61297cd05799SMatthew G. Knepley . dm   - The DMPlex object
61307cd05799SMatthew G. Knepley 
61317cd05799SMatthew G. Knepley   Output Parameter:
61327cd05799SMatthew G. Knepley . cellHeight - The height of a cell
61337cd05799SMatthew G. Knepley 
61347cd05799SMatthew G. Knepley   Level: developer
61357cd05799SMatthew G. Knepley 
61367cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
61377cd05799SMatthew G. Knepley @*/
6138552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6139552f7358SJed Brown {
6140552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6141552f7358SJed Brown 
6142552f7358SJed Brown   PetscFunctionBegin;
6143552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6144552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6145552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6146552f7358SJed Brown   PetscFunctionReturn(0);
6147552f7358SJed Brown }
6148552f7358SJed Brown 
61497cd05799SMatthew G. Knepley /*@C
61507cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
61517cd05799SMatthew G. Knepley 
61527cd05799SMatthew G. Knepley   Input Parameters:
61537cd05799SMatthew G. Knepley + dm   - The DMPlex object
61547cd05799SMatthew G. Knepley - cellHeight - The height of a cell
61557cd05799SMatthew G. Knepley 
61567cd05799SMatthew G. Knepley   Level: developer
61577cd05799SMatthew G. Knepley 
61587cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
61597cd05799SMatthew G. Knepley @*/
6160552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6161552f7358SJed Brown {
6162552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6163552f7358SJed Brown 
6164552f7358SJed Brown   PetscFunctionBegin;
6165552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6166552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6167552f7358SJed Brown   PetscFunctionReturn(0);
6168552f7358SJed Brown }
6169552f7358SJed Brown 
6170552f7358SJed Brown /* We can easily have a form that takes an IS instead */
617150ad7711SMatthew G. Knepley PetscErrorCode DMPlexCreateNumbering_Internal(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6172552f7358SJed Brown {
6173552f7358SJed Brown   PetscSection   section, globalSection;
6174552f7358SJed Brown   PetscInt      *numbers, p;
6175552f7358SJed Brown   PetscErrorCode ierr;
6176552f7358SJed Brown 
6177552f7358SJed Brown   PetscFunctionBegin;
617882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6179552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6180552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6181552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6182552f7358SJed Brown   }
6183552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
618415b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6185854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6186552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6187552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6188ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6189ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6190552f7358SJed Brown   }
619182f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6192ef48cebcSMatthew G. Knepley   if (globalSize) {
6193ef48cebcSMatthew G. Knepley     PetscLayout layout;
6194ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6195ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6196ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6197ef48cebcSMatthew G. Knepley   }
6198552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6199552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6200552f7358SJed Brown   PetscFunctionReturn(0);
6201552f7358SJed Brown }
6202552f7358SJed Brown 
620381ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6204552f7358SJed Brown {
6205552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6206552f7358SJed Brown   PetscErrorCode ierr;
6207552f7358SJed Brown 
6208552f7358SJed Brown   PetscFunctionBegin;
6209552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6210552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
62110298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
621281ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
621350ad7711SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Internal(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
621481ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6215552f7358SJed Brown }
621681ed3555SMatthew G. Knepley 
62178dab3259SMatthew G. Knepley /*@
62187cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
62197cd05799SMatthew G. Knepley 
62207cd05799SMatthew G. Knepley   Input Parameter:
62217cd05799SMatthew G. Knepley . dm   - The DMPlex object
62227cd05799SMatthew G. Knepley 
62237cd05799SMatthew G. Knepley   Output Parameter:
62247cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
62257cd05799SMatthew G. Knepley 
62267cd05799SMatthew G. Knepley   Level: developer
62277cd05799SMatthew G. Knepley 
62287cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
62297cd05799SMatthew G. Knepley @*/
623081ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
623181ed3555SMatthew G. Knepley {
623281ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
623381ed3555SMatthew G. Knepley   PetscErrorCode ierr;
623481ed3555SMatthew G. Knepley 
623581ed3555SMatthew G. Knepley   PetscFunctionBegin;
623681ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
623781ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6238552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6239552f7358SJed Brown   PetscFunctionReturn(0);
6240552f7358SJed Brown }
6241552f7358SJed Brown 
624281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
624381ed3555SMatthew G. Knepley {
624481ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
624581ed3555SMatthew G. Knepley   PetscErrorCode ierr;
624681ed3555SMatthew G. Knepley 
624781ed3555SMatthew G. Knepley   PetscFunctionBegin;
624881ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
624981ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
625081ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
625181ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
625250ad7711SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Internal(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
625381ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
625481ed3555SMatthew G. Knepley }
625581ed3555SMatthew G. Knepley 
62568dab3259SMatthew G. Knepley /*@
62577cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
62587cd05799SMatthew G. Knepley 
62597cd05799SMatthew G. Knepley   Input Parameter:
62607cd05799SMatthew G. Knepley . dm   - The DMPlex object
62617cd05799SMatthew G. Knepley 
62627cd05799SMatthew G. Knepley   Output Parameter:
62637cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
62647cd05799SMatthew G. Knepley 
62657cd05799SMatthew G. Knepley   Level: developer
62667cd05799SMatthew G. Knepley 
62677cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
62687cd05799SMatthew G. Knepley @*/
6269552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6270552f7358SJed Brown {
6271552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6272552f7358SJed Brown   PetscErrorCode ierr;
6273552f7358SJed Brown 
6274552f7358SJed Brown   PetscFunctionBegin;
6275552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
627681ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6277552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6278552f7358SJed Brown   PetscFunctionReturn(0);
6279552f7358SJed Brown }
6280552f7358SJed Brown 
62818dab3259SMatthew G. Knepley /*@
62827cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
62837cd05799SMatthew G. Knepley 
62847cd05799SMatthew G. Knepley   Input Parameter:
62857cd05799SMatthew G. Knepley . dm   - The DMPlex object
62867cd05799SMatthew G. Knepley 
62877cd05799SMatthew G. Knepley   Output Parameter:
62887cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
62897cd05799SMatthew G. Knepley 
62907cd05799SMatthew G. Knepley   Level: developer
62917cd05799SMatthew G. Knepley 
62927cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
62937cd05799SMatthew G. Knepley @*/
6294ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6295ef48cebcSMatthew G. Knepley {
6296ef48cebcSMatthew G. Knepley   IS             nums[4];
6297862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
6298ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6299ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6300ef48cebcSMatthew G. Knepley 
6301ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6302ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6303ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
63048abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
63058abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6306862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6307862913ffSStefano Zampini     PetscInt end;
6308862913ffSStefano Zampini 
6309862913ffSStefano Zampini     depths[d] = depth-d;
6310862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
6311862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
6312862913ffSStefano Zampini   }
6313862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
6314862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
6315862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6316862913ffSStefano 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]);
6317862913ffSStefano Zampini   }
6318ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6319ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6320ef48cebcSMatthew G. Knepley 
6321862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
632250ad7711SMatthew G. Knepley     ierr = DMPlexCreateNumbering_Internal(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6323ef48cebcSMatthew G. Knepley     shift += gsize;
6324ef48cebcSMatthew G. Knepley   }
6325302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6326ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6327ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6328ef48cebcSMatthew G. Knepley }
6329ef48cebcSMatthew G. Knepley 
633008a22f4bSMatthew G. Knepley 
633108a22f4bSMatthew G. Knepley /*@
633208a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
633308a22f4bSMatthew G. Knepley 
633408a22f4bSMatthew G. Knepley   Input Parameter:
633508a22f4bSMatthew G. Knepley . dm - The DMPlex object
633608a22f4bSMatthew G. Knepley 
633708a22f4bSMatthew G. Knepley   Output Parameter:
633808a22f4bSMatthew G. Knepley . ranks - The rank field
633908a22f4bSMatthew G. Knepley 
634008a22f4bSMatthew G. Knepley   Options Database Keys:
634108a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
634208a22f4bSMatthew G. Knepley 
634308a22f4bSMatthew G. Knepley   Level: intermediate
634408a22f4bSMatthew G. Knepley 
634508a22f4bSMatthew G. Knepley .seealso: DMView()
634608a22f4bSMatthew G. Knepley @*/
634708a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
634808a22f4bSMatthew G. Knepley {
634908a22f4bSMatthew G. Knepley   DM             rdm;
635008a22f4bSMatthew G. Knepley   PetscFE        fe;
635108a22f4bSMatthew G. Knepley   PetscScalar   *r;
635208a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
635308a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
635408a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
635508a22f4bSMatthew G. Knepley 
635608a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
6357f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6358f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
635908a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
636008a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
636108a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
6362f95ace6aSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
636308a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
6364e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
636508a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
6366e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
636708a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
636808a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
636908a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
637008a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
637108a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
637208a22f4bSMatthew G. Knepley     PetscScalar *lr;
637308a22f4bSMatthew G. Knepley 
637408a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
637508a22f4bSMatthew G. Knepley     *lr = rank;
637608a22f4bSMatthew G. Knepley   }
637708a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
637808a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
637908a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
638008a22f4bSMatthew G. Knepley }
638108a22f4bSMatthew G. Knepley 
6382ca8062c8SMatthew G. Knepley /*@
638318e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
638418e14f0cSMatthew G. Knepley 
638518e14f0cSMatthew G. Knepley   Input Parameters:
638618e14f0cSMatthew G. Knepley + dm    - The DMPlex
638718e14f0cSMatthew G. Knepley - label - The DMLabel
638818e14f0cSMatthew G. Knepley 
638918e14f0cSMatthew G. Knepley   Output Parameter:
639018e14f0cSMatthew G. Knepley . val - The label value field
639118e14f0cSMatthew G. Knepley 
639218e14f0cSMatthew G. Knepley   Options Database Keys:
639318e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
639418e14f0cSMatthew G. Knepley 
639518e14f0cSMatthew G. Knepley   Level: intermediate
639618e14f0cSMatthew G. Knepley 
639718e14f0cSMatthew G. Knepley .seealso: DMView()
639818e14f0cSMatthew G. Knepley @*/
639918e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
640018e14f0cSMatthew G. Knepley {
640118e14f0cSMatthew G. Knepley   DM             rdm;
640218e14f0cSMatthew G. Knepley   PetscFE        fe;
640318e14f0cSMatthew G. Knepley   PetscScalar   *v;
640418e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
640518e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
640618e14f0cSMatthew G. Knepley 
640718e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
640818e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
640918e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
641018e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
641118e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
641218e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
641318e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
641418e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
6415e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
641618e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
6417e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
641818e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
641918e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
6420effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
642118e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
642218e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
642318e14f0cSMatthew G. Knepley     PetscScalar *lv;
642418e14f0cSMatthew G. Knepley     PetscInt     cval;
642518e14f0cSMatthew G. Knepley 
642618e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
642718e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
642818e14f0cSMatthew G. Knepley     *lv = cval;
642918e14f0cSMatthew G. Knepley   }
643018e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
643118e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
643218e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
643318e14f0cSMatthew G. Knepley }
643418e14f0cSMatthew G. Knepley 
643518e14f0cSMatthew G. Knepley /*@
6436ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6437ca8062c8SMatthew G. Knepley 
643869916449SMatthew G. Knepley   Input Parameter:
643969916449SMatthew G. Knepley . dm - The DMPlex object
6440ca8062c8SMatthew G. Knepley 
6441ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6442ca8062c8SMatthew G. Knepley 
6443ca8062c8SMatthew G. Knepley   Level: developer
6444ca8062c8SMatthew G. Knepley 
64457d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6446ca8062c8SMatthew G. Knepley @*/
6447ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6448ca8062c8SMatthew G. Knepley {
6449ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6450ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6451ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
645257beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
645357beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
6454ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6455ca8062c8SMatthew G. Knepley 
6456ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6457ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6458ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6459ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6460ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6461ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6462ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6463ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6464ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6465ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
646642e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
646742e66dfaSMatthew G. Knepley       PetscInt  d;
646842e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
646942e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
647042e66dfaSMatthew G. Knepley       }
6471ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6472ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6473ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6474ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6475ca8062c8SMatthew G. Knepley       }
647642e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
64778ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6478ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
64798ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6480ca8062c8SMatthew G. Knepley         }
6481302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64828ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6483ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
64848ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6485ca8062c8SMatthew G. Knepley         }
6486302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64878ccfff9cSToby 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]);
64888ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6489ca8062c8SMatthew G. Knepley       }
649042e66dfaSMatthew G. Knepley     }
649157beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
649257beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
6493ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6494ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6495ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6496ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6497ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6498ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
649957beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
650057beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
6501ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6502ca8062c8SMatthew G. Knepley       }
6503ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
65048ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6505ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
65068ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6507ca8062c8SMatthew G. Knepley         }
6508302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
65098ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6510ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
65118ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6512ca8062c8SMatthew G. Knepley         }
6513302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
65148ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6515ca8062c8SMatthew G. Knepley       }
6516ca8062c8SMatthew G. Knepley     }
6517ca8062c8SMatthew G. Knepley   }
651857beb4faSStefano Zampini   if (storagecheck) {
6519ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6520ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
65218ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
652257beb4faSStefano Zampini   }
6523ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6524ca8062c8SMatthew G. Knepley }
6525ca8062c8SMatthew G. Knepley 
6526ca8062c8SMatthew G. Knepley /*@
6527ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6528ca8062c8SMatthew G. Knepley 
6529ca8062c8SMatthew G. Knepley   Input Parameters:
6530ca8062c8SMatthew G. Knepley + dm - The DMPlex object
653158723a97SMatthew G. Knepley - cellHeight - Normally 0
6532ca8062c8SMatthew G. Knepley 
6533ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
653425c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
6535ca8062c8SMatthew G. Knepley 
6536ca8062c8SMatthew G. Knepley   Level: developer
6537ca8062c8SMatthew G. Knepley 
65387d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckFaces()
6539ca8062c8SMatthew G. Knepley @*/
654025c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
6541ca8062c8SMatthew G. Knepley {
654242363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
654325c50c26SVaclav Hapla   PetscBool      isSimplex = PETSC_FALSE;
6544ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6545ca8062c8SMatthew G. Knepley 
6546ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6547ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6548c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
654925c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
655025c50c26SVaclav Hapla   if (cStart < cEnd) {
655125c50c26SVaclav Hapla     ierr = DMPlexGetConeSize(dm, cStart, &c);CHKERRQ(ierr);
655225c50c26SVaclav Hapla     isSimplex = c == dim+1 ? PETSC_TRUE : PETSC_FALSE;
655325c50c26SVaclav Hapla   }
6554ca8062c8SMatthew G. Knepley   switch (dim) {
655542363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
655642363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
655742363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6558ca8062c8SMatthew G. Knepley   default:
65598ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6560ca8062c8SMatthew G. Knepley   }
656158723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
6562ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6563ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6564ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
656558723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
656658723a97SMatthew G. Knepley 
656758723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
656858723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
656958723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
657058723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
657158723a97SMatthew G. Knepley     }
657258723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65738ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6574ca8062c8SMatthew G. Knepley   }
657542363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
657642363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
657742363296SMatthew G. Knepley 
657842363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
657942363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
658042363296SMatthew G. Knepley       const PetscInt p = closure[cl];
658142363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
658242363296SMatthew G. Knepley     }
658342363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65848ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
658542363296SMatthew G. Knepley   }
6586ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6587ca8062c8SMatthew G. Knepley }
65889bf0dad6SMatthew G. Knepley 
65899bf0dad6SMatthew G. Knepley /*@
65909bf0dad6SMatthew 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
65919bf0dad6SMatthew G. Knepley 
65929bf0dad6SMatthew G. Knepley   Input Parameters:
65939bf0dad6SMatthew G. Knepley + dm - The DMPlex object
65949bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
65959bf0dad6SMatthew G. Knepley 
65969bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
65979bf0dad6SMatthew G. Knepley 
65989bf0dad6SMatthew G. Knepley   Level: developer
65999bf0dad6SMatthew G. Knepley 
66007d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton()
66019bf0dad6SMatthew G. Knepley @*/
660225c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
66039bf0dad6SMatthew G. Knepley {
66043554e41dSMatthew G. Knepley   PetscInt       pMax[4];
6605ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
66069bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
66079bf0dad6SMatthew G. Knepley 
66089bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
66099bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6610c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6611ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
66129bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
661325abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
6614ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
66153554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
66163554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
66179bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
66189bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
66199bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
66209bf0dad6SMatthew G. Knepley 
662125abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
66229bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
66239bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
66249bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
66259bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
66269bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
66279bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
66289bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
66299bf0dad6SMatthew G. Knepley       }
66303554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
66318ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
66329bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
66339bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
66349bf0dad6SMatthew G. Knepley 
66359bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
66369bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
66379bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
66389bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
66399bf0dad6SMatthew G. Knepley         }
66408ccfff9cSToby 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);
66419bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
66428ccfff9cSToby 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]);
66439bf0dad6SMatthew G. Knepley         }
66449bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
66459bf0dad6SMatthew G. Knepley       }
66469bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
66479bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
66489bf0dad6SMatthew G. Knepley     }
66493554e41dSMatthew G. Knepley   }
6650552f7358SJed Brown   PetscFunctionReturn(0);
6651552f7358SJed Brown }
66523913d7c8SMatthew G. Knepley 
6653bb6a34a8SMatthew G. Knepley /*@
6654bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
6655bb6a34a8SMatthew G. Knepley 
6656bb6a34a8SMatthew G. Knepley   Input Parameter:
6657bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
6658bb6a34a8SMatthew G. Knepley 
6659bb6a34a8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6660bb6a34a8SMatthew G. Knepley 
6661bb6a34a8SMatthew G. Knepley   Level: developer
6662bb6a34a8SMatthew G. Knepley 
6663bb6a34a8SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton(), DMCheckFaces()
6664bb6a34a8SMatthew G. Knepley @*/
6665bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
6666bb6a34a8SMatthew G. Knepley {
6667bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
6668bb6a34a8SMatthew G. Knepley   PetscReal      vol;
6669bb6a34a8SMatthew G. Knepley   PetscInt       dim, depth, d, cStart, cEnd, c;
6670bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
6671bb6a34a8SMatthew G. Knepley 
6672bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
6673bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6674bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6675bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
6676bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6677bb6a34a8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
6678bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
6679bb6a34a8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted, |J| = %g", c, (double) detJ);
6680bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
6681bb6a34a8SMatthew G. Knepley     if (depth > 1) {
6682bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
6683bb6a34a8SMatthew G. Knepley       if (vol <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %d is inverted, vol = %g", c, (double) vol);
6684bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
6685bb6a34a8SMatthew G. Knepley     }
6686bb6a34a8SMatthew G. Knepley   }
6687bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
6688bb6a34a8SMatthew G. Knepley }
6689bb6a34a8SMatthew G. Knepley 
669003da9461SVaclav Hapla static PetscErrorCode DMPlexAreAllConePointsInArray_Private(DM dm, PetscInt p, PetscInt npoints, const PetscInt *points, PetscInt *missingPoint)
669103da9461SVaclav Hapla {
669203da9461SVaclav Hapla   PetscInt i,l,n;
669303da9461SVaclav Hapla   const PetscInt *cone;
669403da9461SVaclav Hapla   PetscErrorCode ierr;
669503da9461SVaclav Hapla 
669603da9461SVaclav Hapla   PetscFunctionBegin;
669703da9461SVaclav Hapla   *missingPoint = -1;
669803da9461SVaclav Hapla   ierr = DMPlexGetConeSize(dm, p, &n);CHKERRQ(ierr);
669903da9461SVaclav Hapla   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
670003da9461SVaclav Hapla   for (i=0; i<n; i++) {
670103da9461SVaclav Hapla     ierr = PetscFindInt(cone[i], npoints, points, &l);CHKERRQ(ierr);
670203da9461SVaclav Hapla     if (l < 0) {
670303da9461SVaclav Hapla       *missingPoint = cone[i];
670403da9461SVaclav Hapla       break;
670503da9461SVaclav Hapla     }
670603da9461SVaclav Hapla   }
670703da9461SVaclav Hapla   PetscFunctionReturn(0);
670803da9461SVaclav Hapla }
670903da9461SVaclav Hapla 
671003da9461SVaclav Hapla /*@
671103da9461SVaclav Hapla   DMPlexCheckPointSF - Check that several sufficient conditions are met for the point SF of this plex.
671203da9461SVaclav Hapla 
671303da9461SVaclav Hapla   Input Parameters:
671403da9461SVaclav Hapla . dm - The DMPlex object
671503da9461SVaclav Hapla 
671603da9461SVaclav Hapla   Note: This is mainly intended for debugging/testing purposes.
671703da9461SVaclav Hapla 
671803da9461SVaclav Hapla   Level: developer
671903da9461SVaclav Hapla 
672003da9461SVaclav Hapla .seealso: DMGetPointSF(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
672103da9461SVaclav Hapla @*/
672203da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
672303da9461SVaclav Hapla {
672403da9461SVaclav Hapla   PetscSF sf;
672503da9461SVaclav Hapla   PetscInt d,depth,i,nleaves,p,plo,phi,missingPoint;
672603da9461SVaclav Hapla   const PetscInt *locals;
672703da9461SVaclav Hapla   PetscErrorCode ierr;
672803da9461SVaclav Hapla 
672903da9461SVaclav Hapla   PetscFunctionBegin;
673003da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
673103da9461SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
673203da9461SVaclav Hapla   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
673303da9461SVaclav Hapla   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &locals, NULL);CHKERRQ(ierr);
673403da9461SVaclav Hapla 
6735ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
673603da9461SVaclav Hapla   ierr = DMPlexGetVTKCellHeight(dm, &d);CHKERRQ(ierr);
673703da9461SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, d, &plo, &phi);CHKERRQ(ierr);
673803da9461SVaclav Hapla   for (i=0; i<nleaves; i++) {
673903da9461SVaclav Hapla     p = locals[i];
674003da9461SVaclav Hapla     if (p >= plo && p < phi) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d which is a cell",p);
674103da9461SVaclav Hapla   }
6742ece87651SVaclav Hapla 
6743ece87651SVaclav Hapla   /* 2) if some point is in interface, then all its cone points must be also in interface  */
6744ece87651SVaclav Hapla   for (i=0; i<nleaves; i++) {
6745ece87651SVaclav Hapla     p = locals[i];
6746ece87651SVaclav Hapla     ierr = DMPlexAreAllConePointsInArray_Private(dm, p, nleaves, locals, &missingPoint);CHKERRQ(ierr);
6747ece87651SVaclav Hapla     if (missingPoint >= 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d but not %d from its cone",p,missingPoint);
6748ece87651SVaclav Hapla   }
674903da9461SVaclav Hapla   PetscFunctionReturn(0);
675003da9461SVaclav Hapla }
675103da9461SVaclav Hapla 
6752068a5610SStefano Zampini typedef struct cell_stats
6753068a5610SStefano Zampini {
6754068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
6755068a5610SStefano Zampini   PetscInt  count;
6756068a5610SStefano Zampini } cell_stats_t;
6757068a5610SStefano Zampini 
6758068a5610SStefano Zampini static void cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
6759068a5610SStefano Zampini {
6760068a5610SStefano Zampini   PetscInt i, N = *len;
6761068a5610SStefano Zampini 
6762068a5610SStefano Zampini   for (i = 0; i < N; i++) {
6763068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
6764068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
6765068a5610SStefano Zampini 
6766068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
6767068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
6768068a5610SStefano Zampini     B->sum += A->sum;
6769068a5610SStefano Zampini     B->squaresum += A->squaresum;
6770068a5610SStefano Zampini     B->count += A->count;
6771068a5610SStefano Zampini   }
6772068a5610SStefano Zampini }
6773068a5610SStefano Zampini 
6774068a5610SStefano Zampini /*@
6775068a5610SStefano Zampini   DMPlexCheckCellShape - Checks the Jacobian of the mapping and computes some minimal statistics.
6776068a5610SStefano Zampini 
6777068a5610SStefano Zampini   Input Parameters:
6778068a5610SStefano Zampini + dm - The DMPlex object
6779068a5610SStefano Zampini - output - If true, statistics will be displayed on stdout
6780068a5610SStefano Zampini 
6781068a5610SStefano Zampini   Note: This is mainly intended for debugging/testing purposes.
6782068a5610SStefano Zampini 
6783068a5610SStefano Zampini   Level: developer
6784068a5610SStefano Zampini 
6785068a5610SStefano Zampini .seealso: DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6786068a5610SStefano Zampini @*/
6787068a5610SStefano Zampini PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output)
6788068a5610SStefano Zampini {
6789068a5610SStefano Zampini   PetscMPIInt    rank,size;
6790068a5610SStefano Zampini   PetscInt       dim, c, cStart, cEnd, cMax, count = 0;
6791068a5610SStefano Zampini   cell_stats_t   stats, globalStats;
6792068a5610SStefano Zampini   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
6793068a5610SStefano Zampini   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
6794068a5610SStefano Zampini   DM             dmCoarse;
6795068a5610SStefano Zampini   PetscErrorCode ierr;
6796068a5610SStefano Zampini 
6797068a5610SStefano Zampini   PetscFunctionBegin;
6798068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6799068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
6800068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
6801068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
6802068a5610SStefano Zampini   stats.count = 0;
6803068a5610SStefano Zampini 
6804068a5610SStefano Zampini   ierr = DMGetCoordinateDim(dm,&dim);CHKERRQ(ierr);
6805068a5610SStefano Zampini   ierr = PetscMalloc2(dim * dim, &J, dim * dim, &invJ);CHKERRQ(ierr);
6806068a5610SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
6807068a5610SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,NULL,NULL,NULL);CHKERRQ(ierr);
6808068a5610SStefano Zampini   cMax = cMax < 0 ? cEnd : cMax;
6809068a5610SStefano Zampini   for (c = cStart; c < cMax; c++) {
6810068a5610SStefano Zampini     PetscInt  i;
6811068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
6812068a5610SStefano Zampini 
6813068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
6814068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
6815068a5610SStefano Zampini     for (i = 0; i < dim * dim; i++) {
6816068a5610SStefano Zampini       frobJ    += J[i] * J[i];
6817068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
6818068a5610SStefano Zampini     }
6819068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
6820068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
6821068a5610SStefano Zampini 
6822068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
6823068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
6824068a5610SStefano Zampini     stats.sum       += cond;
6825068a5610SStefano Zampini     stats.squaresum += cond2;
6826068a5610SStefano Zampini     stats.count++;
6827068a5610SStefano Zampini   }
6828068a5610SStefano Zampini 
6829068a5610SStefano Zampini   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
6830068a5610SStefano Zampini   if (size > 1) {
6831068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
6832068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
6833068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
6834068a5610SStefano Zampini     MPI_Op        statReduce;
6835068a5610SStefano Zampini 
6836068a5610SStefano Zampini     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRQ(ierr);
6837068a5610SStefano Zampini     ierr = MPI_Type_commit(&statType);CHKERRQ(ierr);
6838068a5610SStefano Zampini     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRQ(ierr);
6839068a5610SStefano Zampini     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRQ(ierr);
6840068a5610SStefano Zampini     ierr = MPI_Op_free(&statReduce);CHKERRQ(ierr);
6841068a5610SStefano Zampini     ierr = MPI_Type_free(&statType);CHKERRQ(ierr);
6842068a5610SStefano Zampini   } else {
6843068a5610SStefano Zampini     ierr = PetscMemcpy(&globalStats,&stats,sizeof(stats));CHKERRQ(ierr);
6844068a5610SStefano Zampini   }
6845068a5610SStefano Zampini 
6846068a5610SStefano Zampini   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
6847068a5610SStefano Zampini   if (!rank) {
6848068a5610SStefano Zampini     count = globalStats.count;
6849068a5610SStefano Zampini     min   = globalStats.min;
6850068a5610SStefano Zampini     max   = globalStats.max;
6851068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
6852068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
6853068a5610SStefano Zampini   }
6854068a5610SStefano Zampini 
6855068a5610SStefano Zampini   if (output) {
6856068a5610SStefano 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);
6857068a5610SStefano Zampini   }
6858068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
6859068a5610SStefano Zampini 
6860068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
6861068a5610SStefano Zampini   if (dmCoarse) {
6862068a5610SStefano Zampini     PetscBool isplex;
6863068a5610SStefano Zampini 
6864068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
6865068a5610SStefano Zampini     if (isplex) {
6866068a5610SStefano Zampini       ierr = DMPlexCheckCellShape(dmCoarse,output);CHKERRQ(ierr);
6867068a5610SStefano Zampini     }
6868068a5610SStefano Zampini   }
6869068a5610SStefano Zampini   PetscFunctionReturn(0);
6870068a5610SStefano Zampini }
6871068a5610SStefano Zampini 
6872bceba477SMatthew G. Knepley /* Pointwise interpolation
6873bceba477SMatthew G. Knepley      Just code FEM for now
6874bceba477SMatthew G. Knepley      u^f = I u^c
68754ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
68764ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
68774ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6878bceba477SMatthew G. Knepley */
6879bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6880bceba477SMatthew G. Knepley {
6881bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6882bceba477SMatthew G. Knepley   PetscInt       m, n;
6883a063dac3SMatthew G. Knepley   void          *ctx;
688468132eb9SMatthew G. Knepley   DM             cdm;
6885fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
6886bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6887bceba477SMatthew G. Knepley 
6888bceba477SMatthew G. Knepley   PetscFunctionBegin;
6889e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6890bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6891e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6892bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
689368132eb9SMatthew G. Knepley 
6894fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
6895bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6896bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6897fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
6898a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
689968132eb9SMatthew G. Knepley 
6900a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
690168132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
690268132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
690368132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
690468132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
69054db47ee9SStefano Zampini   if (scaling) {
69065d1c2e58SMatthew G. Knepley     /* Use naive scaling */
69075d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
69084db47ee9SStefano Zampini   }
6909a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6910a063dac3SMatthew G. Knepley }
6911bceba477SMatthew G. Knepley 
69126dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6913a063dac3SMatthew G. Knepley {
691490748bafSMatthew G. Knepley   PetscErrorCode ierr;
69156dbf9973SLawrence Mitchell   VecScatter     ctx;
691690748bafSMatthew G. Knepley 
6917a063dac3SMatthew G. Knepley   PetscFunctionBegin;
69186dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
69196dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
69206dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6921bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6922bceba477SMatthew G. Knepley }
6923bceba477SMatthew G. Knepley 
6924bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
6925bd041c0cSMatthew G. Knepley {
6926bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
6927bd041c0cSMatthew G. Knepley   PetscInt       m, n;
6928bd041c0cSMatthew G. Knepley   void          *ctx;
6929bd041c0cSMatthew G. Knepley   DM             cdm;
6930bd041c0cSMatthew G. Knepley   PetscBool      regular;
6931bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
6932bd041c0cSMatthew G. Knepley 
6933bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
6934e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6935bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6936e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6937bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
6938bd041c0cSMatthew G. Knepley 
6939bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
6940bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6941bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
6942bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
6943bd041c0cSMatthew G. Knepley 
6944bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
6945bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
6946bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6947bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6948bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
6949bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
6950bd041c0cSMatthew G. Knepley }
6951bd041c0cSMatthew G. Knepley 
69520aef6b92SMatthew G. Knepley /*@
69530aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
69540aef6b92SMatthew G. Knepley 
69550aef6b92SMatthew G. Knepley   Input Parameter:
69560aef6b92SMatthew G. Knepley . dm - The DMPlex object
69570aef6b92SMatthew G. Knepley 
69580aef6b92SMatthew G. Knepley   Output Parameter:
69590aef6b92SMatthew G. Knepley . regular - The flag
69600aef6b92SMatthew G. Knepley 
69610aef6b92SMatthew G. Knepley   Level: intermediate
69620aef6b92SMatthew G. Knepley 
69630aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
69640aef6b92SMatthew G. Knepley @*/
69650aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
69660aef6b92SMatthew G. Knepley {
69670aef6b92SMatthew G. Knepley   PetscFunctionBegin;
69680aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69690aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
69700aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
69710aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
69720aef6b92SMatthew G. Knepley }
69730aef6b92SMatthew G. Knepley 
69740aef6b92SMatthew G. Knepley /*@
69750aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
69760aef6b92SMatthew G. Knepley 
69770aef6b92SMatthew G. Knepley   Input Parameters:
69780aef6b92SMatthew G. Knepley + dm - The DMPlex object
69790aef6b92SMatthew G. Knepley - regular - The flag
69800aef6b92SMatthew G. Knepley 
69810aef6b92SMatthew G. Knepley   Level: intermediate
69820aef6b92SMatthew G. Knepley 
69830aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
69840aef6b92SMatthew G. Knepley @*/
69850aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
69860aef6b92SMatthew G. Knepley {
69870aef6b92SMatthew G. Knepley   PetscFunctionBegin;
69880aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69890aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
69900aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
69910aef6b92SMatthew G. Knepley }
69920aef6b92SMatthew G. Knepley 
6993f7c74593SToby Isaac /* anchors */
6994a68b90caSToby Isaac /*@
6995f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6996f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6997a68b90caSToby Isaac 
6998e228b242SToby Isaac   not collective
6999a68b90caSToby Isaac 
7000a68b90caSToby Isaac   Input Parameters:
7001a68b90caSToby Isaac . dm - The DMPlex object
7002a68b90caSToby Isaac 
7003a68b90caSToby Isaac   Output Parameters:
7004a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
7005a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
7006a68b90caSToby Isaac 
7007a68b90caSToby Isaac 
7008a68b90caSToby Isaac   Level: intermediate
7009a68b90caSToby Isaac 
7010f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
7011a68b90caSToby Isaac @*/
7012a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
7013a68b90caSToby Isaac {
7014a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
701541e6d900SToby Isaac   PetscErrorCode ierr;
7016a68b90caSToby Isaac 
7017a68b90caSToby Isaac   PetscFunctionBegin;
7018a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
701941e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
7020a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
7021a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
7022a68b90caSToby Isaac   PetscFunctionReturn(0);
7023a68b90caSToby Isaac }
7024a68b90caSToby Isaac 
7025a68b90caSToby Isaac /*@
7026f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
7027f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
7028a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
7029a68b90caSToby Isaac 
7030a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
7031f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
7032a68b90caSToby Isaac 
7033e228b242SToby Isaac   collective on dm
7034a68b90caSToby Isaac 
7035a68b90caSToby Isaac   Input Parameters:
7036a68b90caSToby Isaac + dm - The DMPlex object
7037e228b242SToby 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).
7038e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
7039a68b90caSToby Isaac 
7040a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
7041a68b90caSToby Isaac 
7042a68b90caSToby Isaac   Level: intermediate
7043a68b90caSToby Isaac 
7044f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
7045a68b90caSToby Isaac @*/
7046a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
7047a68b90caSToby Isaac {
7048a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7049e228b242SToby Isaac   PetscMPIInt    result;
7050a68b90caSToby Isaac   PetscErrorCode ierr;
7051a68b90caSToby Isaac 
7052a68b90caSToby Isaac   PetscFunctionBegin;
7053a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7054e228b242SToby Isaac   if (anchorSection) {
7055e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
7056e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
7057f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
7058e228b242SToby Isaac   }
7059e228b242SToby Isaac   if (anchorIS) {
7060e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
7061e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
7062f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
7063e228b242SToby Isaac   }
7064a68b90caSToby Isaac 
7065a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
7066a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
7067a68b90caSToby Isaac   plex->anchorSection = anchorSection;
7068a68b90caSToby Isaac 
7069a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
7070a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
7071a68b90caSToby Isaac   plex->anchorIS = anchorIS;
7072a68b90caSToby Isaac 
7073a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
7074a68b90caSToby Isaac   if (anchorIS && anchorSection) {
7075a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
7076a68b90caSToby Isaac     const PetscInt *anchors;
7077a68b90caSToby Isaac 
7078a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
7079a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
7080a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
7081a68b90caSToby Isaac     for (a = 0; a < size; a++) {
7082a68b90caSToby Isaac       PetscInt p;
7083a68b90caSToby Isaac 
7084a68b90caSToby Isaac       p = anchors[a];
7085a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
7086a68b90caSToby Isaac         PetscInt dof;
7087a68b90caSToby Isaac 
7088a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7089a68b90caSToby Isaac         if (dof) {
7090a68b90caSToby Isaac           PetscErrorCode ierr2;
7091a68b90caSToby Isaac 
7092a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
70938ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
7094a68b90caSToby Isaac         }
7095a68b90caSToby Isaac       }
7096a68b90caSToby Isaac     }
7097a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
7098a68b90caSToby Isaac   }
7099a68b90caSToby Isaac #endif
7100f7c74593SToby Isaac   /* reset the generic constraints */
7101f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
7102a68b90caSToby Isaac   PetscFunctionReturn(0);
7103a68b90caSToby Isaac }
7104a68b90caSToby Isaac 
7105f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7106a68b90caSToby Isaac {
7107f7c74593SToby Isaac   PetscSection anchorSection;
71086995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
7109a68b90caSToby Isaac   PetscErrorCode ierr;
7110a68b90caSToby Isaac 
7111a68b90caSToby Isaac   PetscFunctionBegin;
7112a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7113a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7114e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7115a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
71166995de1eSToby Isaac   if (numFields) {
7117719ab38cSToby Isaac     PetscInt f;
7118a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7119719ab38cSToby Isaac 
7120719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7121719ab38cSToby Isaac       PetscInt numComp;
7122719ab38cSToby Isaac 
7123719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7124719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7125719ab38cSToby Isaac     }
71266995de1eSToby Isaac   }
7127a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
71286995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
71296995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
71306995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
71316995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7132a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7133a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7134a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7135a68b90caSToby Isaac     if (dof) {
7136a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7137a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7138a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7139a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7140a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7141a68b90caSToby Isaac       }
7142a68b90caSToby Isaac     }
7143a68b90caSToby Isaac   }
7144a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7145a68b90caSToby Isaac   PetscFunctionReturn(0);
7146a68b90caSToby Isaac }
7147a68b90caSToby Isaac 
7148f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7149a68b90caSToby Isaac {
7150f7c74593SToby Isaac   PetscSection aSec;
71510ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
71520ac89760SToby Isaac   const PetscInt *anchors;
71530ac89760SToby Isaac   PetscInt numFields, f;
715466ad2231SToby Isaac   IS aIS;
71550ac89760SToby Isaac   PetscErrorCode ierr;
71560ac89760SToby Isaac 
71570ac89760SToby Isaac   PetscFunctionBegin;
71580ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71590ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
71600ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
71610ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
71620ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7163302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7164a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
716566ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
71666995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
71676995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
71680ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
71690ac89760SToby Isaac   i[0] = 0;
71700ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
71710ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7172f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7173f19733c5SToby Isaac 
7174f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7175f19733c5SToby Isaac     if (!rDof) continue;
7176f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
71770ac89760SToby Isaac     if (numFields) {
71780ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
71790ac89760SToby Isaac         annz = 0;
7180f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7181f19733c5SToby Isaac           a = anchors[rOff + r];
71820ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
71830ac89760SToby Isaac           annz += aDof;
71840ac89760SToby Isaac         }
71850ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
71860ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
71870ac89760SToby Isaac         for (q = 0; q < dof; q++) {
71880ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
71890ac89760SToby Isaac         }
71900ac89760SToby Isaac       }
71910ac89760SToby Isaac     }
71920ac89760SToby Isaac     else {
71930ac89760SToby Isaac       annz = 0;
71940ac89760SToby Isaac       for (q = 0; q < dof; q++) {
71950ac89760SToby Isaac         a = anchors[off + q];
71960ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
71970ac89760SToby Isaac         annz += aDof;
71980ac89760SToby Isaac       }
71990ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
72000ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
72010ac89760SToby Isaac       for (q = 0; q < dof; q++) {
72020ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
72030ac89760SToby Isaac       }
72040ac89760SToby Isaac     }
72050ac89760SToby Isaac   }
72060ac89760SToby Isaac   nnz = i[m];
72070ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
72080ac89760SToby Isaac   offset = 0;
72090ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
72100ac89760SToby Isaac     if (numFields) {
72110ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
72120ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
72130ac89760SToby Isaac         for (q = 0; q < dof; q++) {
72140ac89760SToby Isaac           PetscInt rDof, rOff, r;
72150ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
72160ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
72170ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
72180ac89760SToby Isaac             PetscInt s;
72190ac89760SToby Isaac 
72200ac89760SToby Isaac             a = anchors[rOff + r];
72210ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
72220ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
72230ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
72240ac89760SToby Isaac               j[offset++] = aOff + s;
72250ac89760SToby Isaac             }
72260ac89760SToby Isaac           }
72270ac89760SToby Isaac         }
72280ac89760SToby Isaac       }
72290ac89760SToby Isaac     }
72300ac89760SToby Isaac     else {
72310ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
72320ac89760SToby Isaac       for (q = 0; q < dof; q++) {
72330ac89760SToby Isaac         PetscInt rDof, rOff, r;
72340ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
72350ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
72360ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
72370ac89760SToby Isaac           PetscInt s;
72380ac89760SToby Isaac 
72390ac89760SToby Isaac           a = anchors[rOff + r];
72400ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
72410ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
72420ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
72430ac89760SToby Isaac             j[offset++] = aOff + s;
72440ac89760SToby Isaac           }
72450ac89760SToby Isaac         }
72460ac89760SToby Isaac       }
72470ac89760SToby Isaac     }
72480ac89760SToby Isaac   }
72490ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
725025570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
725125570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
725266ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
72530ac89760SToby Isaac   PetscFunctionReturn(0);
72540ac89760SToby Isaac }
72550ac89760SToby Isaac 
725666ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
725766ad2231SToby Isaac {
7258f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7259f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
726066ad2231SToby Isaac   Mat            cMat;
726166ad2231SToby Isaac   PetscErrorCode ierr;
726266ad2231SToby Isaac 
726366ad2231SToby Isaac   PetscFunctionBegin;
726466ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7265a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
726666ad2231SToby Isaac   if (anchorSection) {
726744a7f3ddSMatthew G. Knepley     PetscInt Nf;
7268e228b242SToby Isaac 
7269e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
7270f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7271f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
727244a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
727344a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
727466ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
727566ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
727666ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
727766ad2231SToby Isaac   }
727866ad2231SToby Isaac   PetscFunctionReturn(0);
727966ad2231SToby Isaac }
7280a93c429eSMatthew G. Knepley 
7281a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
7282a93c429eSMatthew G. Knepley {
7283a93c429eSMatthew G. Knepley   IS             subis;
7284a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
7285a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
7286a93c429eSMatthew G. Knepley 
7287a93c429eSMatthew G. Knepley   PetscFunctionBegin;
7288a93c429eSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
7289a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
7290a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
7291a93c429eSMatthew G. Knepley   /* Create subdomain */
7292a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
7293a93c429eSMatthew G. Knepley   /* Create submodel */
7294a93c429eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(*subdm, &subis);CHKERRQ(ierr);
7295a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
7296a93c429eSMatthew G. Knepley   ierr = ISDestroy(&subis);CHKERRQ(ierr);
7297a93c429eSMatthew G. Knepley   ierr = DMSetDefaultSection(*subdm, subsection);CHKERRQ(ierr);
7298a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
7299e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
7300a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
7301a93c429eSMatthew G. Knepley   if (is) {
7302a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
7303a93c429eSMatthew G. Knepley     IS              spIS;
7304a93c429eSMatthew G. Knepley     const PetscInt *spmap;
7305a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
7306a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
7307a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
7308a93c429eSMatthew G. Knepley 
7309a93c429eSMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
7310a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
7311a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
7312a93c429eSMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
7313a93c429eSMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
7314a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
7315a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7316a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
7317a93c429eSMatthew G. Knepley 
7318a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
7319a93c429eSMatthew G. Knepley       if (gdof > 0) {
7320a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7321a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
7322a93c429eSMatthew G. Knepley 
7323a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
7324a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
7325a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
7326a93c429eSMatthew G. Knepley         }
7327a93c429eSMatthew G. Knepley         subSize += pSubSize;
7328a93c429eSMatthew G. Knepley         if (pSubSize) {
7329a93c429eSMatthew G. Knepley           if (bs < 0) {
7330a93c429eSMatthew G. Knepley             bs = pSubSize;
7331a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
7332a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
7333a93c429eSMatthew G. Knepley             bs = 1;
7334a93c429eSMatthew G. Knepley           }
7335a93c429eSMatthew G. Knepley         }
7336a93c429eSMatthew G. Knepley       }
7337a93c429eSMatthew G. Knepley     }
7338a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
7339a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
7340a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
7341a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
7342a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
7343a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
7344a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7345a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
7346a93c429eSMatthew G. Knepley 
7347a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
7348a93c429eSMatthew G. Knepley       if (gdof > 0) {
7349a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
7350a93c429eSMatthew G. Knepley 
7351a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
7352a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7353a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
7354a93c429eSMatthew G. Knepley 
7355a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
7356a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
7357a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
7358a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
7359a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
7360a93c429eSMatthew G. Knepley           }
7361a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
7362a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
7363a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
7364a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
7365a93c429eSMatthew G. Knepley           }
7366a93c429eSMatthew G. Knepley         }
7367a93c429eSMatthew G. Knepley       }
7368a93c429eSMatthew G. Knepley     }
7369a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
7370a93c429eSMatthew G. Knepley     ierr = ISDestroy(&spIS);CHKERRQ(ierr);
7371a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
7372a93c429eSMatthew G. Knepley     if (bs > 1) {
7373a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
7374a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
7375a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
7376a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
7377a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
7378a93c429eSMatthew G. Knepley         }
7379a93c429eSMatthew G. Knepley       }
7380a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
7381a93c429eSMatthew G. Knepley     }
7382a93c429eSMatthew G. Knepley     /* Attach nullspace */
7383a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
7384a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
7385a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
7386a93c429eSMatthew G. Knepley     }
7387a93c429eSMatthew G. Knepley     if (f < Nf) {
7388a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
7389a93c429eSMatthew G. Knepley 
7390a93c429eSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);CHKERRQ(ierr);
7391a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
7392a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
7393a93c429eSMatthew G. Knepley     }
7394a93c429eSMatthew G. Knepley   }
7395a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
7396a93c429eSMatthew G. Knepley }
7397