xref: /petsc/src/dm/impls/plex/plex.c (revision 202fd40a21a56cda9fdd84cf4f646d797f41654a)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2af0996ceSBarry Smith #include <petsc/private/isimpl.h>
3e5c6e791SKarl Rupp #include <petsc/private/vecimpl.h>
48135c375SStefano Zampini #include <petsc/private/glvisvecimpl.h>
50c312b8eSJed Brown #include <petscsf.h>
6e228b242SToby Isaac #include <petscds.h>
7e412dcbdSMatthew G. Knepley #include <petscdraw.h>
8f19dbd58SToby Isaac #include <petscdmfield.h>
9552f7358SJed Brown 
10552f7358SJed Brown /* Logging support */
1167eb269bSLisandro Dalcin PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_InterpolateSF, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh;
12552f7358SJed Brown 
135a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
14552f7358SJed Brown 
15e5337592SStefano Zampini /*@
16e5337592SStefano Zampini   DMPlexRefineSimplexToTensor - Uniformly refines simplicial cells into tensor product cells.
17e5337592SStefano Zampini   3 quadrilaterals per triangle in 2D and 4 hexahedra per tetrahedron in 3D.
18e5337592SStefano Zampini 
19e5337592SStefano Zampini   Collective
20e5337592SStefano Zampini 
21e5337592SStefano Zampini   Input Parameters:
22e5337592SStefano Zampini . dm - The DMPlex object
23e5337592SStefano Zampini 
24e5337592SStefano Zampini   Output Parameters:
25e5337592SStefano Zampini . dmRefined - The refined DMPlex object
26e5337592SStefano Zampini 
27e5337592SStefano Zampini   Note: Returns NULL if the mesh is already a tensor product mesh.
28e5337592SStefano Zampini 
29e5337592SStefano Zampini   Level: intermediate
30e5337592SStefano Zampini 
31e5337592SStefano Zampini .seealso: DMPlexCreate(), DMPlexSetRefinementUniform()
32e5337592SStefano Zampini @*/
33e5337592SStefano Zampini PetscErrorCode DMPlexRefineSimplexToTensor(DM dm, DM *dmRefined)
34e5337592SStefano Zampini {
35e5337592SStefano Zampini   PetscInt         dim, cMax, fMax, cStart, cEnd, coneSize;
36e5337592SStefano Zampini   CellRefiner      cellRefiner;
37e5337592SStefano Zampini   PetscBool        lop, allnoop, localized;
38e5337592SStefano Zampini   PetscErrorCode   ierr;
39e5337592SStefano Zampini 
40e5337592SStefano Zampini   PetscFunctionBegin;
41e5337592SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
424330a3fcSStefano Zampini   PetscValidPointer(dmRefined, 1);
43e5337592SStefano Zampini   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
44e5337592SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,&fMax,NULL,NULL);CHKERRQ(ierr);
45e5337592SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
46e5337592SStefano Zampini   if (!(cEnd - cStart)) cellRefiner = REFINER_NOOP;
47e5337592SStefano Zampini   else {
48e5337592SStefano Zampini     ierr = DMPlexGetConeSize(dm,cStart,&coneSize);CHKERRQ(ierr);
49e5337592SStefano Zampini     switch (dim) {
50e5337592SStefano Zampini     case 1:
51e5337592SStefano Zampini       cellRefiner = REFINER_NOOP;
52e5337592SStefano Zampini     break;
53e5337592SStefano Zampini     case 2:
54e5337592SStefano Zampini       switch (coneSize) {
55e5337592SStefano Zampini       case 3:
564330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
574330a3fcSStefano Zampini         else cellRefiner = REFINER_SIMPLEX_TO_HEX_2D;
58e5337592SStefano Zampini       break;
59e5337592SStefano Zampini       case 4:
604330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
614330a3fcSStefano Zampini         else cellRefiner = REFINER_NOOP;
62e5337592SStefano Zampini       break;
63e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
64e5337592SStefano Zampini       }
65e5337592SStefano Zampini     break;
66e5337592SStefano Zampini     case 3:
67e5337592SStefano Zampini       switch (coneSize) {
68e5337592SStefano Zampini       case 4:
694330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
704330a3fcSStefano Zampini         else cellRefiner = REFINER_SIMPLEX_TO_HEX_3D;
714330a3fcSStefano Zampini       break;
724330a3fcSStefano Zampini       case 5:
734330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
744330a3fcSStefano Zampini         else cellRefiner = REFINER_NOOP;
75e5337592SStefano Zampini       break;
76e5337592SStefano Zampini       case 6:
774330a3fcSStefano Zampini         if (cMax >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Simplex2Tensor in 3D with Hybrid mesh not yet done");
78e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
79e5337592SStefano Zampini       break;
80e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
81e5337592SStefano Zampini       }
82e5337592SStefano Zampini     break;
83e5337592SStefano Zampini     default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle dimension %D",dim);
84e5337592SStefano Zampini     }
85e5337592SStefano Zampini   }
86e5337592SStefano Zampini   /* return if we don't need to refine */
87e5337592SStefano Zampini   lop = (cellRefiner == REFINER_NOOP) ? PETSC_TRUE : PETSC_FALSE;
88e5337592SStefano Zampini   ierr = MPIU_Allreduce(&lop,&allnoop,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
89e5337592SStefano Zampini   if (allnoop) {
90e5337592SStefano Zampini     *dmRefined = NULL;
91e5337592SStefano Zampini     PetscFunctionReturn(0);
92e5337592SStefano Zampini   }
93e5337592SStefano Zampini   ierr = DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);CHKERRQ(ierr);
94e5337592SStefano Zampini   ierr = DMCopyBoundary(dm, *dmRefined);CHKERRQ(ierr);
95e5337592SStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
96e5337592SStefano Zampini   if (localized) {
97e5337592SStefano Zampini     ierr = DMLocalizeCoordinates(*dmRefined);CHKERRQ(ierr);
98e5337592SStefano Zampini   }
99e5337592SStefano Zampini   PetscFunctionReturn(0);
100e5337592SStefano Zampini }
101e5337592SStefano Zampini 
1027afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
1037e42fee7SMatthew G. Knepley {
104a99a26bcSAdrian Croucher   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior;
105a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
1067e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
1077e42fee7SMatthew G. Knepley 
1087e42fee7SMatthew G. Knepley   PetscFunctionBegin;
1097e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
110c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1117e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1127e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11380d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11480d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1157e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1167e42fee7SMatthew G. Knepley   if (field >= 0) {
117a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
118a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
1197e42fee7SMatthew G. Knepley   } else {
120a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
121a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
1227e42fee7SMatthew G. Knepley   }
1235483465cSMatthew G. Knepley   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
124a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
1257e42fee7SMatthew G. Knepley     *sStart = vStart;
1267e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
127a99a26bcSAdrian Croucher     if (globalvcdof[0] == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1287e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_POINT_FIELD;
129a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
1307e42fee7SMatthew G. Knepley     *sStart = cStart;
1317e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
132a99a26bcSAdrian Croucher     if (globalvcdof[1] == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1337e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_CELL_FIELD;
1347e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
1357e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1367e42fee7SMatthew G. Knepley }
1377e42fee7SMatthew G. Knepley 
1387cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
139e412dcbdSMatthew G. Knepley {
140e412dcbdSMatthew G. Knepley   DM                 dm;
141d1df6f1dSMatthew G. Knepley   PetscSection       s;
142e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
143e412dcbdSMatthew G. Knepley   DM                 cdm;
144e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
145e412dcbdSMatthew G. Knepley   Vec                coordinates;
146e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
147e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
148339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
149339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
150d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
151e412dcbdSMatthew G. Knepley   const char        *name;
152339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
153e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
154e412dcbdSMatthew G. Knepley 
155e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
156d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
157d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
158d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
159d1df6f1dSMatthew G. Knepley 
160e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
161e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1628135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
163d1df6f1dSMatthew G. Knepley   ierr = DMGetDefaultSection(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);
167e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(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);
293684b87d9SLisandro Dalcin   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
294684b87d9SLisandro Dalcin   ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
295684b87d9SLisandro Dalcin   ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) locv);CHKERRQ(ierr);
296684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
297684b87d9SLisandro Dalcin }
298684b87d9SLisandro Dalcin 
299552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
300552f7358SJed Brown {
301552f7358SJed Brown   DM             dm;
302684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
303552f7358SJed Brown   PetscErrorCode ierr;
304552f7358SJed Brown 
305552f7358SJed Brown   PetscFunctionBegin;
306552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
30782f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
308552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
309b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
310f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3118135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
312684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
313684b87d9SLisandro Dalcin     PetscInt    i,numFields;
314684b87d9SLisandro Dalcin     PetscObject fe;
315ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
316684b87d9SLisandro Dalcin     Vec         locv = v;
317684b87d9SLisandro Dalcin     const char  *name;
318684b87d9SLisandro Dalcin     PetscInt    step;
319684b87d9SLisandro Dalcin     PetscReal   time;
320ef31f671SMatthew G. Knepley 
321ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
322684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
323684b87d9SLisandro Dalcin       ierr = DMGetField(dm, i, &fe);CHKERRQ(ierr);
324684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
325ef31f671SMatthew G. Knepley     }
326684b87d9SLisandro Dalcin     if (fem) {
327684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
328684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
329684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
330684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
331684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
332684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
333ef31f671SMatthew G. Knepley     }
334552f7358SJed Brown     if (isvtk) {
335684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
336b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
337b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
338684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
339b136c2c9SMatthew G. Knepley #else
340b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
341b136c2c9SMatthew G. Knepley #endif
342f13a32a3SMatthew G. Knepley     } else if (isdraw) {
343684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
344684b87d9SLisandro Dalcin     } else if (isglvis) {
345684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
346684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
347684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
348684b87d9SLisandro Dalcin     }
349684b87d9SLisandro Dalcin     if (fem) {ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);}
350552f7358SJed Brown   } else {
351684b87d9SLisandro Dalcin     PetscBool isseq;
352684b87d9SLisandro Dalcin 
353684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
35455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
35555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
356552f7358SJed Brown   }
357552f7358SJed Brown   PetscFunctionReturn(0);
358552f7358SJed Brown }
359552f7358SJed Brown 
360552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
361552f7358SJed Brown {
362552f7358SJed Brown   DM             dm;
363684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
364552f7358SJed Brown   PetscErrorCode ierr;
365552f7358SJed Brown 
366552f7358SJed Brown   PetscFunctionBegin;
367552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
36882f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
369552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
37033c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
371e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3728135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
373684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
374552f7358SJed Brown     Vec         locv;
375552f7358SJed Brown     const char *name;
376552f7358SJed Brown 
377c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
378552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
379552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
380552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
381552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
382552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
383c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
384b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
385b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
38639d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
387b136c2c9SMatthew G. Knepley #else
388b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
389b136c2c9SMatthew G. Knepley #endif
390552f7358SJed Brown   } else {
391684b87d9SLisandro Dalcin     PetscBool isseq;
392684b87d9SLisandro Dalcin 
393684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
39455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
39555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
396552f7358SJed Brown   }
397552f7358SJed Brown   PetscFunctionReturn(0);
398552f7358SJed Brown }
399552f7358SJed Brown 
400d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
401d930f514SMatthew G. Knepley {
402d930f514SMatthew G. Knepley   DM                dm;
403d930f514SMatthew G. Knepley   MPI_Comm          comm;
404d930f514SMatthew G. Knepley   PetscViewerFormat format;
405d930f514SMatthew G. Knepley   Vec               v;
406d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
407d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
408d930f514SMatthew G. Knepley 
409d930f514SMatthew G. Knepley   PetscFunctionBegin;
410d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
411d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4122c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
413d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
414d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
415d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
416d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
417d930f514SMatthew G. Knepley     const char *vecname;
418d930f514SMatthew G. Knepley     PetscInt    n, nroots;
419d930f514SMatthew G. Knepley 
420d930f514SMatthew G. Knepley     if (dm->sfNatural) {
421ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
422d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
423d930f514SMatthew G. Knepley       if (n == nroots) {
424d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
425d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
426d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
427d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
429d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
430d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
431d930f514SMatthew G. Knepley   } else {
432d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
433d930f514SMatthew G. Knepley     v = originalv;
434d930f514SMatthew G. Knepley   }
435d930f514SMatthew G. Knepley   if (ishdf5) {
436d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
43739d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
438d930f514SMatthew G. Knepley #else
439d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
440d930f514SMatthew G. Knepley #endif
441d930f514SMatthew G. Knepley   } else if (isvtk) {
442d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
443d930f514SMatthew G. Knepley   } else {
444d930f514SMatthew G. Knepley     PetscBool isseq;
445d930f514SMatthew G. Knepley 
446d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
447d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
448d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
449d930f514SMatthew G. Knepley   }
450d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
451d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
452d930f514SMatthew G. Knepley }
453d930f514SMatthew G. Knepley 
4542c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4552c40f234SMatthew G. Knepley {
4562c40f234SMatthew G. Knepley   DM             dm;
4572c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4582c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4592c40f234SMatthew G. Knepley 
4602c40f234SMatthew G. Knepley   PetscFunctionBegin;
4612c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4622c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4632c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley   if (ishdf5) {
4652c40f234SMatthew G. Knepley     DM          dmBC;
4662c40f234SMatthew G. Knepley     Vec         gv;
4672c40f234SMatthew G. Knepley     const char *name;
4682c40f234SMatthew G. Knepley 
4692c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4702c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4712c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4722c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4732c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4742c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4752c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4762c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4772c40f234SMatthew G. Knepley   } else {
4782c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4792c40f234SMatthew G. Knepley   }
4802c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4812c40f234SMatthew G. Knepley }
4822c40f234SMatthew G. Knepley 
4832c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4842c40f234SMatthew G. Knepley {
4852c40f234SMatthew G. Knepley   DM             dm;
4862c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4872c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4882c40f234SMatthew G. Knepley 
4892c40f234SMatthew G. Knepley   PetscFunctionBegin;
4902c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4912c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4922c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4932c40f234SMatthew G. Knepley   if (ishdf5) {
494878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
49539d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
496b136c2c9SMatthew G. Knepley #else
497b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
498878b459fSMatthew G. Knepley #endif
4992c40f234SMatthew G. Knepley   } else {
5002c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
501552f7358SJed Brown   }
502552f7358SJed Brown   PetscFunctionReturn(0);
503552f7358SJed Brown }
504552f7358SJed Brown 
505d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
506d930f514SMatthew G. Knepley {
507d930f514SMatthew G. Knepley   DM                dm;
508d930f514SMatthew G. Knepley   PetscViewerFormat format;
509d930f514SMatthew G. Knepley   PetscBool         ishdf5;
510d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
511d930f514SMatthew G. Knepley 
512d930f514SMatthew G. Knepley   PetscFunctionBegin;
513d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
514d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
515d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
516d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
517d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
518d930f514SMatthew G. Knepley     if (dm->sfNatural) {
519d930f514SMatthew G. Knepley       if (ishdf5) {
520d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
521d930f514SMatthew G. Knepley         Vec         v;
522d930f514SMatthew G. Knepley         const char *vecname;
523d930f514SMatthew G. Knepley 
524d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
525d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
526d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
52739d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
528d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
529d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
530d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
531d930f514SMatthew G. Knepley #else
532d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
533d930f514SMatthew G. Knepley #endif
534d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
535d930f514SMatthew G. Knepley     }
536d930f514SMatthew G. Knepley   }
537d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
538d930f514SMatthew G. Knepley }
539d930f514SMatthew G. Knepley 
5407cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
541731e8ddeSMatthew G. Knepley {
542731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
543731e8ddeSMatthew G. Knepley   Vec                coordinates;
544731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
545731e8ddeSMatthew G. Knepley   const char        *name[4];
546731e8ddeSMatthew G. Knepley   const PetscScalar *a;
547731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
548731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
549731e8ddeSMatthew G. Knepley 
550731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
551731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
552731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
553731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
554731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
555731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
556731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
557731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
559731e8ddeSMatthew G. Knepley   name[1]     = "edge";
560731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
561731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
562731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
563731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
564731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
565731e8ddeSMatthew G. Knepley 
566f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
567731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
568731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
569731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
570731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
571731e8ddeSMatthew G. Knepley 
572731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
573731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley       if (!dof) continue;
575731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
577f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
578731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
579731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
580731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
581731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
58241477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
583731e8ddeSMatthew G. Knepley         }
584731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
585731e8ddeSMatthew G. Knepley       }
586731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
587731e8ddeSMatthew G. Knepley     }
588731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
589731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
590731e8ddeSMatthew G. Knepley   }
591731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
592731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
593731e8ddeSMatthew G. Knepley }
594731e8ddeSMatthew G. Knepley 
5957cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
596552f7358SJed Brown {
597552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
598552f7358SJed Brown   DM                cdm;
599552f7358SJed Brown   DMLabel           markers;
600552f7358SJed Brown   PetscSection      coordSection;
601552f7358SJed Brown   Vec               coordinates;
602552f7358SJed Brown   PetscViewerFormat format;
603552f7358SJed Brown   PetscErrorCode    ierr;
604552f7358SJed Brown 
605552f7358SJed Brown   PetscFunctionBegin;
606552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
607552f7358SJed Brown   ierr = DMGetDefaultSection(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);
6520298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
653552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
654c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
655552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
656552f7358SJed Brown     if (size > 1) {
657552f7358SJed Brown       PetscSF sf;
658552f7358SJed Brown 
659552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
660552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
661552f7358SJed Brown     }
662552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
663552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6640588280cSMatthew G. Knepley     const char  *name, *color;
6650588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6660588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
667552f7358SJed Brown     PetscReal    scale         = 2.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;
674*202fd40aSStefano 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);
684c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
685c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6860588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
687c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6880588280cSMatthew G. Knepley     if (!useColors) {
6890588280cSMatthew G. Knepley       numColors = 3;
6900588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6910588280cSMatthew G. Knepley     }
692c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6930588280cSMatthew G. Knepley     if (!useColors) {
6940588280cSMatthew G. Knepley       numLColors = 4;
6950588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
6960588280cSMatthew G. Knepley     }
697*202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
698*202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
699*202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
700*202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
70182f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
70282f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
703552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
704770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7050588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
706552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
707552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
708552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7090588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7100588280cSMatthew G. Knepley     if (size > 1) {
711f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
712770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
713770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
714770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
715770b213bSMatthew G Knepley         } else if (p > 0) {
716770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
717770b213bSMatthew G Knepley         }
718770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
719770b213bSMatthew G Knepley       }
7200588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7210588280cSMatthew G. Knepley     }
7220588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
723552f7358SJed Brown     /* Plot vertices */
724552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
725552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7264d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
727552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
728552f7358SJed Brown       PetscInt  off, dof, d;
7290588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
730552f7358SJed Brown 
731552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
732552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7330588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
734f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7350588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7360588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
737c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7380588280cSMatthew G. Knepley       }
7390588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7400588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
741552f7358SJed Brown       for (d = 0; d < dof; ++d) {
742552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7430588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
744552f7358SJed Brown       }
7450588280cSMatthew G. Knepley       color = colors[rank%numColors];
7460588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7470588280cSMatthew G. Knepley         PetscInt val;
748c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7490588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7500588280cSMatthew G. Knepley       }
7510588280cSMatthew G. Knepley       if (useNumbers) {
752e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7530588280cSMatthew G. Knepley       } else {
754e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7550588280cSMatthew G. Knepley       }
756552f7358SJed Brown     }
757552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
758552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
759552f7358SJed Brown     /* Plot edges */
760*202fd40aSStefano Zampini     if (depth > 1 || plotEdges) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
761*202fd40aSStefano Zampini     if (plotEdges) {
762552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
763552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
764552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
765552f7358SJed Brown         const PetscInt *cone;
766552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
767552f7358SJed Brown 
768552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
769f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
770552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
771552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
772552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
773552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
774552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
775552f7358SJed Brown         for (d = 0; d < dof; ++d) {
776*202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
777c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
778552f7358SJed Brown         }
7790588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
7800588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
7810588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
7820588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
783f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
7840588280cSMatthew G. Knepley         }
7850588280cSMatthew G. Knepley         color = colors[rank%numColors];
7860588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7870588280cSMatthew G. Knepley           PetscInt val;
788c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7890750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7900588280cSMatthew G. Knepley         }
791e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
792552f7358SJed Brown       }
793552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
794552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
795552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
7960588280cSMatthew G. Knepley     }
797552f7358SJed Brown     /* Plot cells */
7980588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
7990588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
8000588280cSMatthew G. Knepley         const PetscInt *cone;
8010588280cSMatthew G. Knepley 
8020588280cSMatthew G. Knepley         color = colors[rank%numColors];
8030588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
8040588280cSMatthew G. Knepley           PetscInt val;
805c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
8060588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
8070588280cSMatthew G. Knepley         }
8080588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
809e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
8100588280cSMatthew G. Knepley       }
8110588280cSMatthew G. Knepley     } else {
812552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
813552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
8140298fd71SBarry Smith         PetscInt *closure = NULL;
815552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
816552f7358SJed Brown 
817552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
818552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
819552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
820552f7358SJed Brown           const PetscInt point = closure[p];
821552f7358SJed Brown 
822552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
823552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
824e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
825552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
826552f7358SJed Brown         }
827552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
828e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
829552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
830552f7358SJed Brown       }
8310588280cSMatthew G. Knepley     }
832552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8334d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8340588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
835770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
8360588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
8370588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
8380588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
8390588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
840552f7358SJed Brown   } else {
84182f516ccSBarry Smith     MPI_Comm    comm;
842834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
843f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
844552f7358SJed Brown     PetscInt    pStart, pEnd, p;
845a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
8461143a3c0SMatthew G. Knepley     const char *name;
847552f7358SJed Brown     PetscMPIInt size;
848552f7358SJed Brown 
84982f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
850552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
851c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
852f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8535f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
854f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
855f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
856f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
857552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
858b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
859bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
860dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
861552f7358SJed Brown     if (depth == 1) {
862552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
863552f7358SJed Brown       pEnd = pEnd - pStart;
864552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
865f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
866552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
867552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
868552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
869552f7358SJed Brown       pEnd = pEnd - pStart;
870552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
871552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
872552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
873552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
874552f7358SJed Brown     } else {
875cbb7f117SMark Adams       PetscMPIInt rank;
876cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
877552f7358SJed Brown       for (d = 0; d <= dim; d++) {
878552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
879834065abSMatthew G. Knepley         pEnd    -= pStart;
880834065abSMatthew G. Knepley         pMax[d] -= pStart;
881552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
882834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
883552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
884834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
885cbb7f117SMark Adams           if (!rank) {
886834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
887834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
888834065abSMatthew G. Knepley           }
889cbb7f117SMark Adams         }
890552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
891552f7358SJed Brown       }
892552f7358SJed Brown     }
893834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
894c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
895a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
896a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
897a57dd577SMatthew G Knepley       DMLabel         label;
898a57dd577SMatthew G Knepley       const char     *name;
899a57dd577SMatthew G Knepley       IS              valueIS;
900a57dd577SMatthew G Knepley       const PetscInt *values;
901a57dd577SMatthew G Knepley       PetscInt        numValues, v;
902a57dd577SMatthew G Knepley 
903c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
904c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
905a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
906d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
907a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
908a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
909120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
910a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
911a57dd577SMatthew G Knepley         PetscInt size;
912a57dd577SMatthew G Knepley 
913a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
914a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
915d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
916a57dd577SMatthew G Knepley       }
917a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
918120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
919a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
9204d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
921a57dd577SMatthew G Knepley     }
922a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
9238e7ff633SMatthew G. Knepley     if (cdm) {
9248e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
9258e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
9268e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
9278e7ff633SMatthew G. Knepley     }
928552f7358SJed Brown   }
929552f7358SJed Brown   PetscFunctionReturn(0);
930552f7358SJed Brown }
931552f7358SJed Brown 
9327cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
933e412dcbdSMatthew G. Knepley {
934e412dcbdSMatthew G. Knepley   PetscDraw          draw;
935e412dcbdSMatthew G. Knepley   DM                 cdm;
936e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
937e412dcbdSMatthew G. Knepley   Vec                coordinates;
938e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
93929494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
940e412dcbdSMatthew G. Knepley   PetscBool          isnull;
941e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
942e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
943e412dcbdSMatthew G. Knepley 
944e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
945e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
946e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
947e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
948e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
949e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
950e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
951e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
952e412dcbdSMatthew G. Knepley 
953e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
954e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
955e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
956e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
957e412dcbdSMatthew G. Knepley 
958e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
959e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
960e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
9610c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
9620c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
963e412dcbdSMatthew G. Knepley   }
964e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
96529494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
96629494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
96729494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
968e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
969e412dcbdSMatthew G. Knepley 
970e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
971e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
97229494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
973e412dcbdSMatthew G. Knepley 
97429494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
975e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
97629494db1SLisandro Dalcin     switch (coneSize) {
97729494db1SLisandro Dalcin     case 3:
9780c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9790c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9800c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
981e412dcbdSMatthew G. Knepley       break;
98229494db1SLisandro Dalcin     case 4:
9830c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9840c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9850c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9860c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
987e412dcbdSMatthew G. Knepley       break;
98829494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
989e412dcbdSMatthew G. Knepley     }
990e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
991e412dcbdSMatthew G. Knepley   }
992e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
993e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
994e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
995e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
996e412dcbdSMatthew G. Knepley }
997e412dcbdSMatthew G. Knepley 
998552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
999552f7358SJed Brown {
1000e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
1001552f7358SJed Brown   PetscErrorCode    ierr;
1002552f7358SJed Brown 
1003552f7358SJed Brown   PetscFunctionBegin;
1004552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1005552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1006552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1007fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
1008c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
1009e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
10108135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
1011552f7358SJed Brown   if (iascii) {
10128135c375SStefano Zampini     PetscViewerFormat format;
10138135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
10148135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
10158135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
10168135c375SStefano Zampini     } else {
1017552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
10188135c375SStefano Zampini     }
1019c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1020c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
102139d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1022c6ccd67eSMatthew G. Knepley #else
1023c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1024552f7358SJed Brown #endif
1025e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1026fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1027e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1028e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
10298135c375SStefano Zampini   } else if (isglvis) {
10308135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
103162201deeSVaclav Hapla   } else {
1032a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1033fcf6c8fdSToby Isaac   }
1034cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1035cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1036cb3ba0daSMatthew G. Knepley   if (flg) {
1037cb3ba0daSMatthew G. Knepley     Vec ranks;
1038cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1039cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1040cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1041cb3ba0daSMatthew G. Knepley   }
1042552f7358SJed Brown   PetscFunctionReturn(0);
1043552f7358SJed Brown }
1044552f7358SJed Brown 
10452c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
10462c40f234SMatthew G. Knepley {
1047d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
10482c40f234SMatthew G. Knepley   PetscErrorCode ierr;
10492c40f234SMatthew G. Knepley 
10502c40f234SMatthew G. Knepley   PetscFunctionBegin;
10512c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10522c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
10532c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1054d4f5a9a0SVaclav Hapla   if (ishdf5) {
10552c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
10569c48423bSVaclav Hapla     PetscViewerFormat format;
10579c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
10589c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
10599c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
10609c48423bSVaclav Hapla     } else {
106139d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
10629c48423bSVaclav Hapla     }
10632c40f234SMatthew G. Knepley #else
10642c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1065552f7358SJed Brown #endif
1066d4f5a9a0SVaclav Hapla   } else {
1067d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1068552f7358SJed Brown   }
1069552f7358SJed Brown   PetscFunctionReturn(0);
1070552f7358SJed Brown }
1071552f7358SJed Brown 
1072552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1073552f7358SJed Brown {
1074552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1075552f7358SJed Brown   PetscErrorCode ierr;
1076552f7358SJed Brown 
1077552f7358SJed Brown   PetscFunctionBegin;
10788135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
10798135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
10800d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1081552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1082552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1083552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1084552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1085be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1086552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1087552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1088d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1089d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
109077623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1091a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1092552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1093552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1094a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1095a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1096d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1097d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1098d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1099d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1100d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1101d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1102fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1103552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1104552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1105552f7358SJed Brown   PetscFunctionReturn(0);
1106552f7358SJed Brown }
1107552f7358SJed Brown 
1108b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1109552f7358SJed Brown {
11108d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1111acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1112552f7358SJed Brown   PetscInt               localSize;
1113837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1114552f7358SJed Brown   PetscErrorCode         ierr;
1115b412c318SBarry Smith   MatType                mtype;
11161428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1117552f7358SJed Brown 
1118552f7358SJed Brown   PetscFunctionBegin;
1119607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1120b412c318SBarry Smith   mtype = dm->mattype;
1121552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1122552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1123552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
112482f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1125552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1126552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1127552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1128acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1129acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1130552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1131552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1132552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1133552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1134552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1135552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1136552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1137837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1138552f7358SJed Brown   if (!isShell) {
1139be36d101SStefano Zampini     PetscSection subSection;
1140837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
11410be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1142fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1143552f7358SJed Brown 
114400140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1145be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1146be36d101SStefano Zampini       PetscSection section;
1147be36d101SStefano Zampini       PetscInt     size;
1148be36d101SStefano Zampini 
1149be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1150be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1151be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1152be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1153be36d101SStefano Zampini     } else {
115400140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1155be36d101SStefano Zampini     }
1156552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1157be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1158a9d99c84SMatthew G. Knepley       PetscInt bdof;
1159a9d99c84SMatthew G. Knepley 
1160552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1161fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
11621d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
11631d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
11641d17a0a3SMatthew G. Knepley       if (dof) {
11651d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1166be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1167be36d101SStefano Zampini       }
1168be36d101SStefano Zampini       if (isMatIS) {
1169be36d101SStefano Zampini         PetscInt loff,c,off;
1170be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1171be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1172be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1173552f7358SJed Brown       }
11742a28c762SMatthew G Knepley     }
11752a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
11760be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11770be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11780be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11790be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
11804fa26246SMatthew G. Knepley     bs = bs < 0 ? 1 : bs;
1181be36d101SStefano Zampini     if (isMatIS) {
11824fa26246SMatthew G. Knepley       PetscInt l;
11834fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
11844fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
11854fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1186be36d101SStefano Zampini     }
1187be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1188be36d101SStefano Zampini     if (isMatIS) {
1189be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1190be36d101SStefano Zampini     }
11911795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
11928d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1193552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1194552f7358SJed Brown   }
1195b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1196552f7358SJed Brown   PetscFunctionReturn(0);
1197552f7358SJed Brown }
1198552f7358SJed Brown 
11997cd05799SMatthew G. Knepley /*@
1200a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1201be36d101SStefano Zampini 
1202be36d101SStefano Zampini   Not collective
1203be36d101SStefano Zampini 
1204be36d101SStefano Zampini   Input Parameter:
1205be36d101SStefano Zampini . mesh - The DMPlex
1206be36d101SStefano Zampini 
1207be36d101SStefano Zampini   Output Parameters:
1208be36d101SStefano Zampini . subsection - The subdomain section
1209be36d101SStefano Zampini 
1210be36d101SStefano Zampini   Level: developer
1211be36d101SStefano Zampini 
1212be36d101SStefano Zampini .seealso:
12137cd05799SMatthew G. Knepley @*/
1214be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1215be36d101SStefano Zampini {
1216be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1217be36d101SStefano Zampini   PetscErrorCode ierr;
1218be36d101SStefano Zampini 
1219be36d101SStefano Zampini   PetscFunctionBegin;
1220be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1221be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1222be36d101SStefano Zampini     PetscSection section;
1223be36d101SStefano Zampini     PetscSF      sf;
1224be36d101SStefano Zampini 
1225be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1226be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1227be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1228be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1229be36d101SStefano Zampini   }
1230be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1231be36d101SStefano Zampini   PetscFunctionReturn(0);
1232be36d101SStefano Zampini }
1233be36d101SStefano Zampini 
1234552f7358SJed Brown /*@
1235552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1236552f7358SJed Brown 
1237552f7358SJed Brown   Not collective
1238552f7358SJed Brown 
1239552f7358SJed Brown   Input Parameter:
1240552f7358SJed Brown . mesh - The DMPlex
1241552f7358SJed Brown 
1242552f7358SJed Brown   Output Parameters:
1243552f7358SJed Brown + pStart - The first mesh point
1244552f7358SJed Brown - pEnd   - The upper bound for mesh points
1245552f7358SJed Brown 
1246552f7358SJed Brown   Level: beginner
1247552f7358SJed Brown 
1248552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1249552f7358SJed Brown @*/
1250552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1251552f7358SJed Brown {
1252552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1253552f7358SJed Brown   PetscErrorCode ierr;
1254552f7358SJed Brown 
1255552f7358SJed Brown   PetscFunctionBegin;
1256552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1257552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1258552f7358SJed Brown   PetscFunctionReturn(0);
1259552f7358SJed Brown }
1260552f7358SJed Brown 
1261552f7358SJed Brown /*@
1262552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1263552f7358SJed Brown 
1264552f7358SJed Brown   Not collective
1265552f7358SJed Brown 
1266552f7358SJed Brown   Input Parameters:
1267552f7358SJed Brown + mesh - The DMPlex
1268552f7358SJed Brown . pStart - The first mesh point
1269552f7358SJed Brown - pEnd   - The upper bound for mesh points
1270552f7358SJed Brown 
1271552f7358SJed Brown   Output Parameters:
1272552f7358SJed Brown 
1273552f7358SJed Brown   Level: beginner
1274552f7358SJed Brown 
1275552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1276552f7358SJed Brown @*/
1277552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1278552f7358SJed Brown {
1279552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1280552f7358SJed Brown   PetscErrorCode ierr;
1281552f7358SJed Brown 
1282552f7358SJed Brown   PetscFunctionBegin;
1283552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1284552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1285552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1286552f7358SJed Brown   PetscFunctionReturn(0);
1287552f7358SJed Brown }
1288552f7358SJed Brown 
1289552f7358SJed Brown /*@
1290eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1291552f7358SJed Brown 
1292552f7358SJed Brown   Not collective
1293552f7358SJed Brown 
1294552f7358SJed Brown   Input Parameters:
1295552f7358SJed Brown + mesh - The DMPlex
1296eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1297552f7358SJed Brown 
1298552f7358SJed Brown   Output Parameter:
1299552f7358SJed Brown . size - The cone size for point p
1300552f7358SJed Brown 
1301552f7358SJed Brown   Level: beginner
1302552f7358SJed Brown 
1303552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1304552f7358SJed Brown @*/
1305552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1306552f7358SJed Brown {
1307552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1308552f7358SJed Brown   PetscErrorCode ierr;
1309552f7358SJed Brown 
1310552f7358SJed Brown   PetscFunctionBegin;
1311552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1312552f7358SJed Brown   PetscValidPointer(size, 3);
1313552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1314552f7358SJed Brown   PetscFunctionReturn(0);
1315552f7358SJed Brown }
1316552f7358SJed Brown 
1317552f7358SJed Brown /*@
1318eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1319552f7358SJed Brown 
1320552f7358SJed Brown   Not collective
1321552f7358SJed Brown 
1322552f7358SJed Brown   Input Parameters:
1323552f7358SJed Brown + mesh - The DMPlex
1324eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1325552f7358SJed Brown - size - The cone size for point p
1326552f7358SJed Brown 
1327552f7358SJed Brown   Output Parameter:
1328552f7358SJed Brown 
1329552f7358SJed Brown   Note:
1330552f7358SJed Brown   This should be called after DMPlexSetChart().
1331552f7358SJed Brown 
1332552f7358SJed Brown   Level: beginner
1333552f7358SJed Brown 
1334552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1335552f7358SJed Brown @*/
1336552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1337552f7358SJed Brown {
1338552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1339552f7358SJed Brown   PetscErrorCode ierr;
1340552f7358SJed Brown 
1341552f7358SJed Brown   PetscFunctionBegin;
1342552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1343552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
13440d644c17SKarl Rupp 
1345552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1346552f7358SJed Brown   PetscFunctionReturn(0);
1347552f7358SJed Brown }
1348552f7358SJed Brown 
1349f5a469b9SMatthew G. Knepley /*@
1350eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1351f5a469b9SMatthew G. Knepley 
1352f5a469b9SMatthew G. Knepley   Not collective
1353f5a469b9SMatthew G. Knepley 
1354f5a469b9SMatthew G. Knepley   Input Parameters:
1355f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1356eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1357f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1358f5a469b9SMatthew G. Knepley 
1359f5a469b9SMatthew G. Knepley   Output Parameter:
1360f5a469b9SMatthew G. Knepley 
1361f5a469b9SMatthew G. Knepley   Note:
1362f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1363f5a469b9SMatthew G. Knepley 
1364f5a469b9SMatthew G. Knepley   Level: beginner
1365f5a469b9SMatthew G. Knepley 
1366f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1367f5a469b9SMatthew G. Knepley @*/
1368f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1369f5a469b9SMatthew G. Knepley {
1370f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1371f5a469b9SMatthew G. Knepley   PetscInt       csize;
1372f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1373f5a469b9SMatthew G. Knepley 
1374f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1375f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1376f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1377f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1378f5a469b9SMatthew G. Knepley 
1379f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1380f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1381f5a469b9SMatthew G. Knepley }
1382f5a469b9SMatthew G. Knepley 
1383552f7358SJed Brown /*@C
1384eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1385552f7358SJed Brown 
1386552f7358SJed Brown   Not collective
1387552f7358SJed Brown 
1388552f7358SJed Brown   Input Parameters:
1389552f7358SJed Brown + mesh - The DMPlex
1390eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1391552f7358SJed Brown 
1392552f7358SJed Brown   Output Parameter:
1393552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1394552f7358SJed Brown 
1395552f7358SJed Brown   Level: beginner
1396552f7358SJed Brown 
13973813dfbdSMatthew G Knepley   Fortran Notes:
13983813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13993813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14003813dfbdSMatthew G Knepley 
14013813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1402552f7358SJed Brown 
1403552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1404552f7358SJed Brown @*/
1405552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1406552f7358SJed Brown {
1407552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1408552f7358SJed Brown   PetscInt       off;
1409552f7358SJed Brown   PetscErrorCode ierr;
1410552f7358SJed Brown 
1411552f7358SJed Brown   PetscFunctionBegin;
1412552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1413552f7358SJed Brown   PetscValidPointer(cone, 3);
1414552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1415552f7358SJed Brown   *cone = &mesh->cones[off];
1416552f7358SJed Brown   PetscFunctionReturn(0);
1417552f7358SJed Brown }
1418552f7358SJed Brown 
1419552f7358SJed Brown /*@
142092371b87SBarry 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
1421552f7358SJed Brown 
1422552f7358SJed Brown   Not collective
1423552f7358SJed Brown 
1424552f7358SJed Brown   Input Parameters:
1425552f7358SJed Brown + mesh - The DMPlex
1426eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1427552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1428552f7358SJed Brown 
1429552f7358SJed Brown   Output Parameter:
1430552f7358SJed Brown 
1431552f7358SJed Brown   Note:
1432552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1433552f7358SJed Brown 
143492371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
143592371b87SBarry Smith 
1436552f7358SJed Brown   Level: beginner
1437552f7358SJed Brown 
143892371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1439552f7358SJed Brown @*/
1440552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1441552f7358SJed Brown {
1442552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1443552f7358SJed Brown   PetscInt       pStart, pEnd;
1444552f7358SJed Brown   PetscInt       dof, off, c;
1445552f7358SJed Brown   PetscErrorCode ierr;
1446552f7358SJed Brown 
1447552f7358SJed Brown   PetscFunctionBegin;
1448552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1449552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1450552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1451552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1452552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
145382f516ccSBarry 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);
1454552f7358SJed Brown   for (c = 0; c < dof; ++c) {
145582f516ccSBarry 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);
1456552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1457552f7358SJed Brown   }
1458552f7358SJed Brown   PetscFunctionReturn(0);
1459552f7358SJed Brown }
1460552f7358SJed Brown 
1461552f7358SJed Brown /*@C
1462eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1463552f7358SJed Brown 
1464552f7358SJed Brown   Not collective
1465552f7358SJed Brown 
1466552f7358SJed Brown   Input Parameters:
1467552f7358SJed Brown + mesh - The DMPlex
1468eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1469552f7358SJed Brown 
1470552f7358SJed Brown   Output Parameter:
1471552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1472552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1473552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1474552f7358SJed Brown                     the index of the cone point on which to start.
1475552f7358SJed Brown 
1476552f7358SJed Brown   Level: beginner
1477552f7358SJed Brown 
14783813dfbdSMatthew G Knepley   Fortran Notes:
14793813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14803813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14813813dfbdSMatthew G Knepley 
14823813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1483552f7358SJed Brown 
1484552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1485552f7358SJed Brown @*/
1486552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1487552f7358SJed Brown {
1488552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1489552f7358SJed Brown   PetscInt       off;
1490552f7358SJed Brown   PetscErrorCode ierr;
1491552f7358SJed Brown 
1492552f7358SJed Brown   PetscFunctionBegin;
1493552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1494552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1495552f7358SJed Brown   {
1496552f7358SJed Brown     PetscInt dof;
1497552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1498552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1499552f7358SJed Brown   }
1500552f7358SJed Brown #endif
1501552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
15020d644c17SKarl Rupp 
1503552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1504552f7358SJed Brown   PetscFunctionReturn(0);
1505552f7358SJed Brown }
1506552f7358SJed Brown 
1507552f7358SJed Brown /*@
1508eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1509552f7358SJed Brown 
1510552f7358SJed Brown   Not collective
1511552f7358SJed Brown 
1512552f7358SJed Brown   Input Parameters:
1513552f7358SJed Brown + mesh - The DMPlex
1514eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1515552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1516552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1517552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1518552f7358SJed Brown                     the index of the cone point on which to start.
1519552f7358SJed Brown 
1520552f7358SJed Brown   Output Parameter:
1521552f7358SJed Brown 
1522552f7358SJed Brown   Note:
1523552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1524552f7358SJed Brown 
1525552f7358SJed Brown   Level: beginner
1526552f7358SJed Brown 
1527552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1528552f7358SJed Brown @*/
1529552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1530552f7358SJed Brown {
1531552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1532552f7358SJed Brown   PetscInt       pStart, pEnd;
1533552f7358SJed Brown   PetscInt       dof, off, c;
1534552f7358SJed Brown   PetscErrorCode ierr;
1535552f7358SJed Brown 
1536552f7358SJed Brown   PetscFunctionBegin;
1537552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1538552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1539552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1540552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1541552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
154282f516ccSBarry 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);
1543552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1544552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1545552f7358SJed Brown 
1546552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
154782f516ccSBarry 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);
1548552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1549552f7358SJed Brown   }
1550552f7358SJed Brown   PetscFunctionReturn(0);
1551552f7358SJed Brown }
1552552f7358SJed Brown 
15537cd05799SMatthew G. Knepley /*@
1554eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
15557cd05799SMatthew G. Knepley 
15567cd05799SMatthew G. Knepley   Not collective
15577cd05799SMatthew G. Knepley 
15587cd05799SMatthew G. Knepley   Input Parameters:
15597cd05799SMatthew G. Knepley + mesh - The DMPlex
1560eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15617cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15627cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
15637cd05799SMatthew G. Knepley 
15647cd05799SMatthew G. Knepley   Level: beginner
15657cd05799SMatthew G. Knepley 
15667cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15677cd05799SMatthew G. Knepley @*/
1568552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1569552f7358SJed Brown {
1570552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1571552f7358SJed Brown   PetscInt       pStart, pEnd;
1572552f7358SJed Brown   PetscInt       dof, off;
1573552f7358SJed Brown   PetscErrorCode ierr;
1574552f7358SJed Brown 
1575552f7358SJed Brown   PetscFunctionBegin;
1576552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1577552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
157882f516ccSBarry 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);
157982f516ccSBarry 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);
158077c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
158177c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
158277c88f5bSMatthew 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);
1583552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1584552f7358SJed Brown   PetscFunctionReturn(0);
1585552f7358SJed Brown }
1586552f7358SJed Brown 
15877cd05799SMatthew G. Knepley /*@
1588eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
15897cd05799SMatthew G. Knepley 
15907cd05799SMatthew G. Knepley   Not collective
15917cd05799SMatthew G. Knepley 
15927cd05799SMatthew G. Knepley   Input Parameters:
15937cd05799SMatthew G. Knepley + mesh - The DMPlex
1594eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15957cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15967cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
15977cd05799SMatthew G. Knepley 
15987cd05799SMatthew G. Knepley   Level: beginner
15997cd05799SMatthew G. Knepley 
16007cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
16017cd05799SMatthew G. Knepley @*/
160277c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
160377c88f5bSMatthew G Knepley {
160477c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
160577c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
160677c88f5bSMatthew G Knepley   PetscInt       dof, off;
160777c88f5bSMatthew G Knepley   PetscErrorCode ierr;
160877c88f5bSMatthew G Knepley 
160977c88f5bSMatthew G Knepley   PetscFunctionBegin;
161077c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
161177c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
161277c88f5bSMatthew 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);
161377c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
161477c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
161577c88f5bSMatthew 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);
161677c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
161777c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
161877c88f5bSMatthew G Knepley }
161977c88f5bSMatthew G Knepley 
1620552f7358SJed Brown /*@
1621eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
1622552f7358SJed Brown 
1623552f7358SJed Brown   Not collective
1624552f7358SJed Brown 
1625552f7358SJed Brown   Input Parameters:
1626552f7358SJed Brown + mesh - The DMPlex
1627eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1628552f7358SJed Brown 
1629552f7358SJed Brown   Output Parameter:
1630552f7358SJed Brown . size - The support size for point p
1631552f7358SJed Brown 
1632552f7358SJed Brown   Level: beginner
1633552f7358SJed Brown 
1634552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1635552f7358SJed Brown @*/
1636552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1637552f7358SJed Brown {
1638552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1639552f7358SJed Brown   PetscErrorCode ierr;
1640552f7358SJed Brown 
1641552f7358SJed Brown   PetscFunctionBegin;
1642552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1643552f7358SJed Brown   PetscValidPointer(size, 3);
1644552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1645552f7358SJed Brown   PetscFunctionReturn(0);
1646552f7358SJed Brown }
1647552f7358SJed Brown 
1648552f7358SJed Brown /*@
1649eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
1650552f7358SJed Brown 
1651552f7358SJed Brown   Not collective
1652552f7358SJed Brown 
1653552f7358SJed Brown   Input Parameters:
1654552f7358SJed Brown + mesh - The DMPlex
1655eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1656552f7358SJed Brown - size - The support size for point p
1657552f7358SJed Brown 
1658552f7358SJed Brown   Output Parameter:
1659552f7358SJed Brown 
1660552f7358SJed Brown   Note:
1661552f7358SJed Brown   This should be called after DMPlexSetChart().
1662552f7358SJed Brown 
1663552f7358SJed Brown   Level: beginner
1664552f7358SJed Brown 
1665552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1666552f7358SJed Brown @*/
1667552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1668552f7358SJed Brown {
1669552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1670552f7358SJed Brown   PetscErrorCode ierr;
1671552f7358SJed Brown 
1672552f7358SJed Brown   PetscFunctionBegin;
1673552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1674552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
16750d644c17SKarl Rupp 
1676552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1677552f7358SJed Brown   PetscFunctionReturn(0);
1678552f7358SJed Brown }
1679552f7358SJed Brown 
1680552f7358SJed Brown /*@C
1681eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
1682552f7358SJed Brown 
1683552f7358SJed Brown   Not collective
1684552f7358SJed Brown 
1685552f7358SJed Brown   Input Parameters:
1686552f7358SJed Brown + mesh - The DMPlex
1687eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1688552f7358SJed Brown 
1689552f7358SJed Brown   Output Parameter:
1690552f7358SJed Brown . support - An array of points which are on the out-edges for point p
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 DMPlexRestoreSupport() after you finish using the returned array.
16993813dfbdSMatthew G Knepley 
1700552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1701552f7358SJed Brown @*/
1702552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
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   PetscValidPointer(support, 3);
1711552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1712552f7358SJed Brown   *support = &mesh->supports[off];
1713552f7358SJed Brown   PetscFunctionReturn(0);
1714552f7358SJed Brown }
1715552f7358SJed Brown 
1716552f7358SJed Brown /*@
171792371b87SBarry 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
1718552f7358SJed Brown 
1719552f7358SJed Brown   Not collective
1720552f7358SJed Brown 
1721552f7358SJed Brown   Input Parameters:
1722552f7358SJed Brown + mesh - The DMPlex
1723eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
172492371b87SBarry Smith - support - An array of points which are on the out-edges for point p
1725552f7358SJed Brown 
1726552f7358SJed Brown   Output Parameter:
1727552f7358SJed Brown 
1728552f7358SJed Brown   Note:
1729552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1730552f7358SJed Brown 
1731552f7358SJed Brown   Level: beginner
1732552f7358SJed Brown 
173392371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1734552f7358SJed Brown @*/
1735552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1736552f7358SJed Brown {
1737552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1738552f7358SJed Brown   PetscInt       pStart, pEnd;
1739552f7358SJed Brown   PetscInt       dof, off, c;
1740552f7358SJed Brown   PetscErrorCode ierr;
1741552f7358SJed Brown 
1742552f7358SJed Brown   PetscFunctionBegin;
1743552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1744552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1745552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1746552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1747552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
174882f516ccSBarry 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);
1749552f7358SJed Brown   for (c = 0; c < dof; ++c) {
175082f516ccSBarry 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);
1751552f7358SJed Brown     mesh->supports[off+c] = support[c];
1752552f7358SJed Brown   }
1753552f7358SJed Brown   PetscFunctionReturn(0);
1754552f7358SJed Brown }
1755552f7358SJed Brown 
17567cd05799SMatthew G. Knepley /*@
1757eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
17587cd05799SMatthew G. Knepley 
17597cd05799SMatthew G. Knepley   Not collective
17607cd05799SMatthew G. Knepley 
17617cd05799SMatthew G. Knepley   Input Parameters:
17627cd05799SMatthew G. Knepley + mesh - The DMPlex
1763eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17647cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
17657cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
17667cd05799SMatthew G. Knepley 
17677cd05799SMatthew G. Knepley   Level: beginner
17687cd05799SMatthew G. Knepley 
17697cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17707cd05799SMatthew G. Knepley @*/
1771552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1772552f7358SJed Brown {
1773552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1774552f7358SJed Brown   PetscInt       pStart, pEnd;
1775552f7358SJed Brown   PetscInt       dof, off;
1776552f7358SJed Brown   PetscErrorCode ierr;
1777552f7358SJed Brown 
1778552f7358SJed Brown   PetscFunctionBegin;
1779552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1780552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1781552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1782552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
178382f516ccSBarry 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);
178482f516ccSBarry 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);
178582f516ccSBarry 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);
1786552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1787552f7358SJed Brown   PetscFunctionReturn(0);
1788552f7358SJed Brown }
1789552f7358SJed Brown 
1790552f7358SJed Brown /*@C
1791eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
1792552f7358SJed Brown 
1793552f7358SJed Brown   Not collective
1794552f7358SJed Brown 
1795552f7358SJed Brown   Input Parameters:
1796552f7358SJed Brown + mesh - The DMPlex
1797eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1798552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17990298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1800552f7358SJed Brown 
1801552f7358SJed Brown   Output Parameters:
1802552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1803552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1804552f7358SJed Brown 
1805552f7358SJed Brown   Note:
18060298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1807552f7358SJed Brown 
18083813dfbdSMatthew G Knepley   Fortran Notes:
18093813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18103813dfbdSMatthew G Knepley   include petsc.h90 in your code.
18113813dfbdSMatthew G Knepley 
18123813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
18133813dfbdSMatthew G Knepley 
1814552f7358SJed Brown   Level: beginner
1815552f7358SJed Brown 
1816552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1817552f7358SJed Brown @*/
1818552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1819552f7358SJed Brown {
1820552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1821552f7358SJed Brown   PetscInt       *closure, *fifo;
18220298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1823552f7358SJed Brown   PetscInt        tmpSize, t;
1824552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1825552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1826552f7358SJed Brown   PetscErrorCode  ierr;
1827552f7358SJed Brown 
1828552f7358SJed Brown   PetscFunctionBegin;
1829552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1830552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1831552f7358SJed Brown   /* This is only 1-level */
1832552f7358SJed Brown   if (useCone) {
1833552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1834552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1835552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1836552f7358SJed Brown   } else {
1837552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1838552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1839552f7358SJed Brown   }
1840bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1841bfbcdd7aSMatthew G. Knepley     if (*points) {
1842bfbcdd7aSMatthew G. Knepley       closure = *points;
1843bfbcdd7aSMatthew G. Knepley     } else {
1844bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
184569291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1846bfbcdd7aSMatthew G. Knepley     }
1847bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1848bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1849bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1850bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1851bfbcdd7aSMatthew G. Knepley     }
1852bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1853bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1854bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1855bfbcdd7aSMatthew G. Knepley   }
185624c766afSToby Isaac   {
185724c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
185824c766afSToby Isaac 
185924c766afSToby Isaac     c = mesh->maxConeSize;
186024c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
186124c766afSToby Isaac     s = mesh->maxSupportSize;
186224c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
186324c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
186424c766afSToby Isaac   }
186569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1866bfbcdd7aSMatthew G. Knepley   if (*points) {
1867bfbcdd7aSMatthew G. Knepley     closure = *points;
1868bfbcdd7aSMatthew G. Knepley   } else {
186969291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1870bfbcdd7aSMatthew G. Knepley   }
1871bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1872552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1873552f7358SJed Brown     const PetscInt cp = tmp[t];
1874552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1875552f7358SJed Brown 
1876552f7358SJed Brown     closure[closureSize]   = cp;
1877552f7358SJed Brown     closure[closureSize+1] = co;
1878552f7358SJed Brown     fifo[fifoSize]         = cp;
1879552f7358SJed Brown     fifo[fifoSize+1]       = co;
1880552f7358SJed Brown   }
1881bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1882552f7358SJed Brown   while (fifoSize - fifoStart) {
1883552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1884552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1885552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1886552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1887552f7358SJed Brown 
1888552f7358SJed Brown     if (useCone) {
1889552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1890552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1891552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1892552f7358SJed Brown     } else {
1893552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1894552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18950298fd71SBarry Smith       tmpO = NULL;
1896552f7358SJed Brown     }
1897552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1898552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1899552f7358SJed Brown       const PetscInt cp = tmp[i];
19002e1b13c2SMatthew 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. */
19012e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
19022e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
19032e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1904552f7358SJed Brown       PetscInt       c;
1905552f7358SJed Brown 
19062e1b13c2SMatthew G. Knepley       if (rev) {
19072e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
19082e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
19092e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
19102e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
19112e1b13c2SMatthew G. Knepley       }
1912552f7358SJed Brown       /* Check for duplicate */
1913552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1914552f7358SJed Brown         if (closure[c] == cp) break;
1915552f7358SJed Brown       }
1916552f7358SJed Brown       if (c == closureSize) {
1917552f7358SJed Brown         closure[closureSize]   = cp;
1918552f7358SJed Brown         closure[closureSize+1] = co;
1919552f7358SJed Brown         fifo[fifoSize]         = cp;
1920552f7358SJed Brown         fifo[fifoSize+1]       = co;
1921552f7358SJed Brown         closureSize           += 2;
1922552f7358SJed Brown         fifoSize              += 2;
1923552f7358SJed Brown       }
1924552f7358SJed Brown     }
1925552f7358SJed Brown     fifoStart += 2;
1926552f7358SJed Brown   }
1927552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1928552f7358SJed Brown   if (points)    *points    = closure;
192969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1930552f7358SJed Brown   PetscFunctionReturn(0);
1931552f7358SJed Brown }
1932552f7358SJed Brown 
19339bf0dad6SMatthew G. Knepley /*@C
1934eaf898f9SPatrick 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
19359bf0dad6SMatthew G. Knepley 
19369bf0dad6SMatthew G. Knepley   Not collective
19379bf0dad6SMatthew G. Knepley 
19389bf0dad6SMatthew G. Knepley   Input Parameters:
19399bf0dad6SMatthew G. Knepley + mesh - The DMPlex
1940eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
19419bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
19429bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
19439bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
19449bf0dad6SMatthew G. Knepley 
19459bf0dad6SMatthew G. Knepley   Output Parameters:
19469bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
19479bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
19489bf0dad6SMatthew G. Knepley 
19499bf0dad6SMatthew G. Knepley   Note:
19509bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
19519bf0dad6SMatthew G. Knepley 
19529bf0dad6SMatthew G. Knepley   Fortran Notes:
19539bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19549bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
19559bf0dad6SMatthew G. Knepley 
19569bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19579bf0dad6SMatthew G. Knepley 
19589bf0dad6SMatthew G. Knepley   Level: beginner
19599bf0dad6SMatthew G. Knepley 
19609bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
19619bf0dad6SMatthew G. Knepley @*/
19629bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
19639bf0dad6SMatthew G. Knepley {
19649bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
19659bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
19669bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
19679bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
19689bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
19699bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
19709bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
19719bf0dad6SMatthew G. Knepley 
19729bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
19739bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19749bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
19759bf0dad6SMatthew G. Knepley   /* This is only 1-level */
19769bf0dad6SMatthew G. Knepley   if (useCone) {
19779bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
19789bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
19799bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
19809bf0dad6SMatthew G. Knepley   } else {
19819bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
19829bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
19839bf0dad6SMatthew G. Knepley   }
19849bf0dad6SMatthew G. Knepley   if (depth == 1) {
19859bf0dad6SMatthew G. Knepley     if (*points) {
19869bf0dad6SMatthew G. Knepley       closure = *points;
19879bf0dad6SMatthew G. Knepley     } else {
19889bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
198969291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
19909bf0dad6SMatthew G. Knepley     }
19919bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
19929bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
19939bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19949bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
19959bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
19969bf0dad6SMatthew G. Knepley     }
19979bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
19989bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
19999bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
20009bf0dad6SMatthew G. Knepley   }
200124c766afSToby Isaac   {
200224c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
200324c766afSToby Isaac 
200424c766afSToby Isaac     c = mesh->maxConeSize;
200524c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
200624c766afSToby Isaac     s = mesh->maxSupportSize;
200724c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
200824c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
200924c766afSToby Isaac   }
201069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
20119bf0dad6SMatthew G. Knepley   if (*points) {
20129bf0dad6SMatthew G. Knepley     closure = *points;
20139bf0dad6SMatthew G. Knepley   } else {
201469291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
20159bf0dad6SMatthew G. Knepley   }
20169bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
20179bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
20189bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
20199bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
20209bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
20219bf0dad6SMatthew G. Knepley 
20229bf0dad6SMatthew G. Knepley     if (ornt < 0) {
20239bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
20249bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
202586b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
20269bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
20279bf0dad6SMatthew G. Knepley     }
20289bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
20299bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
20309bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
20319bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
20329bf0dad6SMatthew G. Knepley   }
20339bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
20349bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
20359bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
20369bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
20379bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
20389bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
20399bf0dad6SMatthew G. Knepley 
20409bf0dad6SMatthew G. Knepley     if (useCone) {
20419bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
20429bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
20439bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
20449bf0dad6SMatthew G. Knepley     } else {
20459bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
20469bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
20479bf0dad6SMatthew G. Knepley       tmpO = NULL;
20489bf0dad6SMatthew G. Knepley     }
20499bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
20509bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
20519bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
20529bf0dad6SMatthew 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. */
20539bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
20549bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
20559bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
20569bf0dad6SMatthew G. Knepley       PetscInt       c;
20579bf0dad6SMatthew G. Knepley 
20589bf0dad6SMatthew G. Knepley       if (rev) {
20599bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
20609bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
20619bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
20629bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
20639bf0dad6SMatthew G. Knepley       }
20649bf0dad6SMatthew G. Knepley       /* Check for duplicate */
20659bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
20669bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
20679bf0dad6SMatthew G. Knepley       }
20689bf0dad6SMatthew G. Knepley       if (c == closureSize) {
20699bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
20709bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
20719bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
20729bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
20739bf0dad6SMatthew G. Knepley         closureSize           += 2;
20749bf0dad6SMatthew G. Knepley         fifoSize              += 2;
20759bf0dad6SMatthew G. Knepley       }
20769bf0dad6SMatthew G. Knepley     }
20779bf0dad6SMatthew G. Knepley     fifoStart += 2;
20789bf0dad6SMatthew G. Knepley   }
20799bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
20809bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
208169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
20829bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
20839bf0dad6SMatthew G. Knepley }
20849bf0dad6SMatthew G. Knepley 
2085552f7358SJed Brown /*@C
2086eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2087552f7358SJed Brown 
2088552f7358SJed Brown   Not collective
2089552f7358SJed Brown 
2090552f7358SJed Brown   Input Parameters:
2091552f7358SJed Brown + mesh - The DMPlex
2092eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2093552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2094e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2095e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2096552f7358SJed Brown 
2097552f7358SJed Brown   Note:
20980298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2099552f7358SJed Brown 
21003813dfbdSMatthew G Knepley   Fortran Notes:
21013813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21023813dfbdSMatthew G Knepley   include petsc.h90 in your code.
21033813dfbdSMatthew G Knepley 
21043813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
21053813dfbdSMatthew G Knepley 
2106552f7358SJed Brown   Level: beginner
2107552f7358SJed Brown 
2108552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2109552f7358SJed Brown @*/
2110552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2111552f7358SJed Brown {
2112552f7358SJed Brown   PetscErrorCode ierr;
2113552f7358SJed Brown 
2114552f7358SJed Brown   PetscFunctionBegin;
2115552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2116e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2117e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
211869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
21194ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2120552f7358SJed Brown   PetscFunctionReturn(0);
2121552f7358SJed Brown }
2122552f7358SJed Brown 
2123552f7358SJed Brown /*@
2124eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2125552f7358SJed Brown 
2126552f7358SJed Brown   Not collective
2127552f7358SJed Brown 
2128552f7358SJed Brown   Input Parameter:
2129552f7358SJed Brown . mesh - The DMPlex
2130552f7358SJed Brown 
2131552f7358SJed Brown   Output Parameters:
2132552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2133552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2134552f7358SJed Brown 
2135552f7358SJed Brown   Level: beginner
2136552f7358SJed Brown 
2137552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2138552f7358SJed Brown @*/
2139552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2140552f7358SJed Brown {
2141552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2142552f7358SJed Brown 
2143552f7358SJed Brown   PetscFunctionBegin;
2144552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2145552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2146552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2147552f7358SJed Brown   PetscFunctionReturn(0);
2148552f7358SJed Brown }
2149552f7358SJed Brown 
2150552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2151552f7358SJed Brown {
2152552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2153552f7358SJed Brown   PetscInt       size;
2154552f7358SJed Brown   PetscErrorCode ierr;
2155552f7358SJed Brown 
2156552f7358SJed Brown   PetscFunctionBegin;
2157552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2158552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2159552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
21601795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
21611795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2162552f7358SJed Brown   if (mesh->maxSupportSize) {
2163552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2164552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
21651795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2166552f7358SJed Brown   }
2167552f7358SJed Brown   PetscFunctionReturn(0);
2168552f7358SJed Brown }
2169552f7358SJed Brown 
2170276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2171552f7358SJed Brown {
2172552f7358SJed Brown   PetscErrorCode ierr;
2173552f7358SJed Brown 
2174552f7358SJed Brown   PetscFunctionBegin;
21754d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
21764d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2177c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2178736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2179f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2180f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2181f94b4a02SBlaise Bourdin 
21823dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
21833dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2184f94b4a02SBlaise Bourdin     ierr = DMGetDefaultSection((*subdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
2185f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2186f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2187f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2188f94b4a02SBlaise Bourdin 
2189f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2190c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2191f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2192f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2193f94b4a02SBlaise Bourdin   }
2194552f7358SJed Brown   PetscFunctionReturn(0);
2195552f7358SJed Brown }
2196552f7358SJed Brown 
21972adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
21982adcc780SMatthew G. Knepley {
21992adcc780SMatthew G. Knepley   PetscErrorCode ierr;
22003dcd263cSBlaise Bourdin   PetscInt       i = 0;
22012adcc780SMatthew G. Knepley 
22022adcc780SMatthew G. Knepley   PetscFunctionBegin;
22032adcc780SMatthew G. Knepley   if (superdm) {ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);}
22042adcc780SMatthew G. Knepley   ierr = DMCreateSuperDM_Section_Private(dms, len, is, superdm);CHKERRQ(ierr);
2205c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
22063dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
22073dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
22083dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
22093dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
22103dcd263cSBlaise Bourdin 
22113dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
22123dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2213c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
22143dcd263cSBlaise Bourdin       ierr = DMGetDefaultSection((*superdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
22153dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
22163dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
22173dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
22183dcd263cSBlaise Bourdin 
22193dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2220c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
22213dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
22223dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
22233dcd263cSBlaise Bourdin       break;
22243dcd263cSBlaise Bourdin     }
22253dcd263cSBlaise Bourdin   }
22262adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
22272adcc780SMatthew G. Knepley }
22282adcc780SMatthew G. Knepley 
2229552f7358SJed Brown /*@
2230eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2231552f7358SJed Brown 
2232552f7358SJed Brown   Not collective
2233552f7358SJed Brown 
2234552f7358SJed Brown   Input Parameter:
2235552f7358SJed Brown . mesh - The DMPlex
2236552f7358SJed Brown 
2237552f7358SJed Brown   Output Parameter:
2238552f7358SJed Brown 
2239552f7358SJed Brown   Note:
2240552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2241552f7358SJed Brown 
2242552f7358SJed Brown   Level: beginner
2243552f7358SJed Brown 
2244552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2245552f7358SJed Brown @*/
2246552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2247552f7358SJed Brown {
2248552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2249552f7358SJed Brown   PetscInt      *offsets;
2250552f7358SJed Brown   PetscInt       supportSize;
2251552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2252552f7358SJed Brown   PetscErrorCode ierr;
2253552f7358SJed Brown 
2254552f7358SJed Brown   PetscFunctionBegin;
2255552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
225682f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2257552f7358SJed Brown   /* Calculate support sizes */
2258552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2259552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2260552f7358SJed Brown     PetscInt dof, off, c;
2261552f7358SJed Brown 
2262552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2263552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2264552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2265552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2266552f7358SJed Brown     }
2267552f7358SJed Brown   }
2268552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2269552f7358SJed Brown     PetscInt dof;
2270552f7358SJed Brown 
2271552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
22720d644c17SKarl Rupp 
2273552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2274552f7358SJed Brown   }
2275552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2276552f7358SJed Brown   /* Calculate supports */
2277552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
22781795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
22791795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2280552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2281552f7358SJed Brown     PetscInt dof, off, c;
2282552f7358SJed Brown 
2283552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2284552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2285552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2286552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2287552f7358SJed Brown       PetscInt       offS;
2288552f7358SJed Brown 
2289552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
22900d644c17SKarl Rupp 
2291552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2292552f7358SJed Brown       ++offsets[q];
2293552f7358SJed Brown     }
2294552f7358SJed Brown   }
2295552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2296552f7358SJed Brown   PetscFunctionReturn(0);
2297552f7358SJed Brown }
2298552f7358SJed Brown 
22997d5acc75SStefano Zampini static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);
23007d5acc75SStefano Zampini 
2301552f7358SJed Brown /*@
2302eaf898f9SPatrick Sanan   DMPlexStratify - The DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2303eaf898f9SPatrick Sanan   can be illustrated by a Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2304552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2305552f7358SJed Brown   the DAG.
2306552f7358SJed Brown 
2307bf4602e4SToby Isaac   Collective on dm
2308552f7358SJed Brown 
2309552f7358SJed Brown   Input Parameter:
2310552f7358SJed Brown . mesh - The DMPlex
2311552f7358SJed Brown 
2312552f7358SJed Brown   Output Parameter:
2313552f7358SJed Brown 
2314552f7358SJed Brown   Notes:
2315150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2316150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2317c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2318150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2319552f7358SJed Brown 
2320150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2321552f7358SJed Brown 
2322552f7358SJed Brown   Level: beginner
2323552f7358SJed Brown 
2324552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2325552f7358SJed Brown @*/
2326552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2327552f7358SJed Brown {
2328df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2329aa50250dSMatthew G. Knepley   DMLabel        label;
2330552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2331552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
23327d5acc75SStefano Zampini   PetscInt       cMax, fMax, eMax, vMax;
2333552f7358SJed Brown   PetscErrorCode ierr;
2334552f7358SJed Brown 
2335552f7358SJed Brown   PetscFunctionBegin;
2336552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2337552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2338552f7358SJed Brown   /* Calculate depth */
2339aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2340c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2341aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2342552f7358SJed Brown   /* Initialize roots and count leaves */
2343552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2344552f7358SJed Brown     PetscInt coneSize, supportSize;
2345552f7358SJed Brown 
2346552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2347552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2348552f7358SJed Brown     if (!coneSize && supportSize) {
2349552f7358SJed Brown       ++numRoots;
2350aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2351552f7358SJed Brown     } else if (!supportSize && coneSize) {
2352552f7358SJed Brown       ++numLeaves;
2353552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2354552f7358SJed Brown       /* Isolated points */
2355aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2356552f7358SJed Brown     }
2357552f7358SJed Brown   }
2358552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2359552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2360552f7358SJed Brown       PetscInt coneSize, supportSize;
2361552f7358SJed Brown 
2362552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2363552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2364552f7358SJed Brown       if (!supportSize && coneSize) {
2365aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2366552f7358SJed Brown       }
2367552f7358SJed Brown     }
2368552f7358SJed Brown   } else {
236974ef644bSMatthew G. Knepley     IS       pointIS;
237074ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2371552f7358SJed Brown 
237274ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
237374ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
237474ef644bSMatthew G. Knepley     while (numPoints) {
237574ef644bSMatthew G. Knepley       const PetscInt *points;
237674ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
237774ef644bSMatthew G. Knepley 
237874ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
237974ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
238074ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
238174ef644bSMatthew G. Knepley         const PetscInt *support;
238274ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
238374ef644bSMatthew G. Knepley 
238474ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
238574ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
238674ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
238774ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2388552f7358SJed Brown         }
2389552f7358SJed Brown       }
23905edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
239174ef644bSMatthew G. Knepley       ++level;
239274ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
239374ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
239474ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
239574ef644bSMatthew G. Knepley       else         {numPoints = 0;}
239674ef644bSMatthew G. Knepley     }
239774ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
239874ef644bSMatthew G. Knepley   }
2399bf4602e4SToby Isaac   { /* just in case there is an empty process */
2400bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2401bf4602e4SToby Isaac 
2402bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
24034de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
24044de306b1SToby Isaac       IS pointIS;
24054de306b1SToby Isaac 
24064de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
24074de306b1SToby Isaac       if (pointIS) {
24084de306b1SToby Isaac         PetscInt  min, max, numPoints;
24094de306b1SToby Isaac         PetscInt  start;
24104de306b1SToby Isaac         PetscBool contig;
24114de306b1SToby Isaac 
24124de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
24134de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
24144de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
24154de306b1SToby Isaac         if (start == 0 && contig) {
24164de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
24174de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
24184de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
24194de306b1SToby Isaac         }
24204de306b1SToby Isaac       }
24214de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
24224de306b1SToby Isaac     }
24236d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2424bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2425bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2426bf4602e4SToby Isaac     }
2427bf4602e4SToby Isaac   }
2428df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
24297d5acc75SStefano Zampini 
24307d5acc75SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
24317d5acc75SStefano Zampini   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
24327d5acc75SStefano Zampini     DMLabel  dimLabel;
24337d5acc75SStefano Zampini     PetscInt dim;
24347d5acc75SStefano Zampini 
24357d5acc75SStefano Zampini     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
24367d5acc75SStefano Zampini     ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
24377d5acc75SStefano Zampini     if (!dimLabel) {
24387d5acc75SStefano Zampini       ierr = DMCreateLabel(dm, "dim");CHKERRQ(ierr);
24397d5acc75SStefano Zampini       ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
24407d5acc75SStefano Zampini     }
24417d5acc75SStefano Zampini     if (cMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);CHKERRQ(ierr);}
24427d5acc75SStefano Zampini     if (fMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);CHKERRQ(ierr);}
24437d5acc75SStefano Zampini     if (eMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);CHKERRQ(ierr);}
24447d5acc75SStefano Zampini     if (vMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);CHKERRQ(ierr);}
24457d5acc75SStefano Zampini   }
2446552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2447552f7358SJed Brown   PetscFunctionReturn(0);
2448552f7358SJed Brown }
2449552f7358SJed Brown 
2450552f7358SJed Brown /*@C
2451552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2452552f7358SJed Brown 
2453552f7358SJed Brown   Not Collective
2454552f7358SJed Brown 
2455552f7358SJed Brown   Input Parameters:
2456552f7358SJed Brown + dm - The DMPlex object
2457552f7358SJed Brown . numPoints - The number of input points for the join
2458552f7358SJed Brown - points - The input points
2459552f7358SJed Brown 
2460552f7358SJed Brown   Output Parameters:
2461552f7358SJed Brown + numCoveredPoints - The number of points in the join
2462552f7358SJed Brown - coveredPoints - The points in the join
2463552f7358SJed Brown 
2464552f7358SJed Brown   Level: intermediate
2465552f7358SJed Brown 
2466552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2467552f7358SJed Brown 
24683813dfbdSMatthew G Knepley   Fortran Notes:
24693813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24703813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24713813dfbdSMatthew G Knepley 
24723813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24733813dfbdSMatthew G Knepley 
2474552f7358SJed Brown .keywords: mesh
2475552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2476552f7358SJed Brown @*/
2477552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2478552f7358SJed Brown {
2479552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2480552f7358SJed Brown   PetscInt      *join[2];
2481552f7358SJed Brown   PetscInt       joinSize, i = 0;
2482552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2483552f7358SJed Brown   PetscErrorCode ierr;
2484552f7358SJed Brown 
2485552f7358SJed Brown   PetscFunctionBegin;
2486552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2487552f7358SJed Brown   PetscValidPointer(points, 2);
2488552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2489552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
249069291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
249169291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2492552f7358SJed Brown   /* Copy in support of first point */
2493552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2494552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2495552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2496552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2497552f7358SJed Brown   }
2498552f7358SJed Brown   /* Check each successive support */
2499552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2500552f7358SJed Brown     PetscInt newJoinSize = 0;
2501552f7358SJed Brown 
2502552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2503552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2504552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2505552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2506552f7358SJed Brown 
2507552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2508552f7358SJed Brown         if (point == join[i][m]) {
2509552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2510552f7358SJed Brown           break;
2511552f7358SJed Brown         }
2512552f7358SJed Brown       }
2513552f7358SJed Brown     }
2514552f7358SJed Brown     joinSize = newJoinSize;
2515552f7358SJed Brown     i        = 1-i;
2516552f7358SJed Brown   }
2517552f7358SJed Brown   *numCoveredPoints = joinSize;
2518552f7358SJed Brown   *coveredPoints    = join[i];
251969291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2520552f7358SJed Brown   PetscFunctionReturn(0);
2521552f7358SJed Brown }
2522552f7358SJed Brown 
2523552f7358SJed Brown /*@C
2524552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2525552f7358SJed Brown 
2526552f7358SJed Brown   Not Collective
2527552f7358SJed Brown 
2528552f7358SJed Brown   Input Parameters:
2529552f7358SJed Brown + dm - The DMPlex object
2530552f7358SJed Brown . numPoints - The number of input points for the join
2531552f7358SJed Brown - points - The input points
2532552f7358SJed Brown 
2533552f7358SJed Brown   Output Parameters:
2534552f7358SJed Brown + numCoveredPoints - The number of points in the join
2535552f7358SJed Brown - coveredPoints - The points in the join
2536552f7358SJed Brown 
25373813dfbdSMatthew G Knepley   Fortran Notes:
25383813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25393813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25403813dfbdSMatthew G Knepley 
25413813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25423813dfbdSMatthew G Knepley 
2543552f7358SJed Brown   Level: intermediate
2544552f7358SJed Brown 
2545552f7358SJed Brown .keywords: mesh
2546552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2547552f7358SJed Brown @*/
2548552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2549552f7358SJed Brown {
2550552f7358SJed Brown   PetscErrorCode ierr;
2551552f7358SJed Brown 
2552552f7358SJed Brown   PetscFunctionBegin;
2553552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2554d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2555d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2556d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
255769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2558d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2559552f7358SJed Brown   PetscFunctionReturn(0);
2560552f7358SJed Brown }
2561552f7358SJed Brown 
2562552f7358SJed Brown /*@C
2563552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2564552f7358SJed Brown 
2565552f7358SJed Brown   Not Collective
2566552f7358SJed Brown 
2567552f7358SJed Brown   Input Parameters:
2568552f7358SJed Brown + dm - The DMPlex object
2569552f7358SJed Brown . numPoints - The number of input points for the join
2570552f7358SJed Brown - points - The input points
2571552f7358SJed Brown 
2572552f7358SJed Brown   Output Parameters:
2573552f7358SJed Brown + numCoveredPoints - The number of points in the join
2574552f7358SJed Brown - coveredPoints - The points in the join
2575552f7358SJed Brown 
25763813dfbdSMatthew G Knepley   Fortran Notes:
25773813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25783813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25793813dfbdSMatthew G Knepley 
25803813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25813813dfbdSMatthew G Knepley 
2582552f7358SJed Brown   Level: intermediate
2583552f7358SJed Brown 
2584552f7358SJed Brown .keywords: mesh
2585552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2586552f7358SJed Brown @*/
2587552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2588552f7358SJed Brown {
2589552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2590552f7358SJed Brown   PetscInt      *offsets, **closures;
2591552f7358SJed Brown   PetscInt      *join[2];
2592552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
259324c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2594552f7358SJed Brown   PetscErrorCode ierr;
2595552f7358SJed Brown 
2596552f7358SJed Brown   PetscFunctionBegin;
2597552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2598552f7358SJed Brown   PetscValidPointer(points, 2);
2599552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2600552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2601552f7358SJed Brown 
2602552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
26031795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
260469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
260524c766afSToby Isaac   ms      = mesh->maxSupportSize;
260624c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
260769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
260869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2609552f7358SJed Brown 
2610552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2611552f7358SJed Brown     PetscInt closureSize;
2612552f7358SJed Brown 
2613552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
26140d644c17SKarl Rupp 
2615552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2616552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2617552f7358SJed Brown       PetscInt pStart, pEnd, i;
2618552f7358SJed Brown 
2619552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2620552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2621552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2622552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2623552f7358SJed Brown           break;
2624552f7358SJed Brown         }
2625552f7358SJed Brown       }
2626552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2627552f7358SJed Brown     }
262882f516ccSBarry 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);
2629552f7358SJed Brown   }
2630552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2631552f7358SJed Brown     PetscInt dof;
2632552f7358SJed Brown 
2633552f7358SJed Brown     /* Copy in support of first point */
2634552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2635552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2636552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2637552f7358SJed Brown     }
2638552f7358SJed Brown     /* Check each successive cone */
2639552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2640552f7358SJed Brown       PetscInt newJoinSize = 0;
2641552f7358SJed Brown 
2642552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2643552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2644552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2645552f7358SJed Brown 
2646552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2647552f7358SJed Brown           if (point == join[i][m]) {
2648552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2649552f7358SJed Brown             break;
2650552f7358SJed Brown           }
2651552f7358SJed Brown         }
2652552f7358SJed Brown       }
2653552f7358SJed Brown       joinSize = newJoinSize;
2654552f7358SJed Brown       i        = 1-i;
2655552f7358SJed Brown     }
2656552f7358SJed Brown     if (joinSize) break;
2657552f7358SJed Brown   }
2658552f7358SJed Brown   *numCoveredPoints = joinSize;
2659552f7358SJed Brown   *coveredPoints    = join[i];
2660552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26610298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2662552f7358SJed Brown   }
2663552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
266469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
266569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2666552f7358SJed Brown   PetscFunctionReturn(0);
2667552f7358SJed Brown }
2668552f7358SJed Brown 
2669552f7358SJed Brown /*@C
2670552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2671552f7358SJed Brown 
2672552f7358SJed Brown   Not Collective
2673552f7358SJed Brown 
2674552f7358SJed Brown   Input Parameters:
2675552f7358SJed Brown + dm - The DMPlex object
2676552f7358SJed Brown . numPoints - The number of input points for the meet
2677552f7358SJed Brown - points - The input points
2678552f7358SJed Brown 
2679552f7358SJed Brown   Output Parameters:
2680552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2681552f7358SJed Brown - coveredPoints - The points in the meet
2682552f7358SJed Brown 
2683552f7358SJed Brown   Level: intermediate
2684552f7358SJed Brown 
2685552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2686552f7358SJed Brown 
26873813dfbdSMatthew G Knepley   Fortran Notes:
26883813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26893813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26903813dfbdSMatthew G Knepley 
26913813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26923813dfbdSMatthew G Knepley 
2693552f7358SJed Brown .keywords: mesh
2694552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2695552f7358SJed Brown @*/
2696552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2697552f7358SJed Brown {
2698552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2699552f7358SJed Brown   PetscInt      *meet[2];
2700552f7358SJed Brown   PetscInt       meetSize, i = 0;
2701552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2702552f7358SJed Brown   PetscErrorCode ierr;
2703552f7358SJed Brown 
2704552f7358SJed Brown   PetscFunctionBegin;
2705552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2706552f7358SJed Brown   PetscValidPointer(points, 2);
2707552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2708552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
270969291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
271069291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2711552f7358SJed Brown   /* Copy in cone of first point */
2712552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2713552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2714552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2715552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2716552f7358SJed Brown   }
2717552f7358SJed Brown   /* Check each successive cone */
2718552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2719552f7358SJed Brown     PetscInt newMeetSize = 0;
2720552f7358SJed Brown 
2721552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2722552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2723552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2724552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2725552f7358SJed Brown 
2726552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2727552f7358SJed Brown         if (point == meet[i][m]) {
2728552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2729552f7358SJed Brown           break;
2730552f7358SJed Brown         }
2731552f7358SJed Brown       }
2732552f7358SJed Brown     }
2733552f7358SJed Brown     meetSize = newMeetSize;
2734552f7358SJed Brown     i        = 1-i;
2735552f7358SJed Brown   }
2736552f7358SJed Brown   *numCoveringPoints = meetSize;
2737552f7358SJed Brown   *coveringPoints    = meet[i];
273869291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2739552f7358SJed Brown   PetscFunctionReturn(0);
2740552f7358SJed Brown }
2741552f7358SJed Brown 
2742552f7358SJed Brown /*@C
2743552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2744552f7358SJed Brown 
2745552f7358SJed Brown   Not Collective
2746552f7358SJed Brown 
2747552f7358SJed Brown   Input Parameters:
2748552f7358SJed Brown + dm - The DMPlex object
2749552f7358SJed Brown . numPoints - The number of input points for the meet
2750552f7358SJed Brown - points - The input points
2751552f7358SJed Brown 
2752552f7358SJed Brown   Output Parameters:
2753552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2754552f7358SJed Brown - coveredPoints - The points in the meet
2755552f7358SJed Brown 
2756552f7358SJed Brown   Level: intermediate
2757552f7358SJed Brown 
27583813dfbdSMatthew G Knepley   Fortran Notes:
27593813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27603813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27613813dfbdSMatthew G Knepley 
27623813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27633813dfbdSMatthew G Knepley 
2764552f7358SJed Brown .keywords: mesh
2765552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2766552f7358SJed Brown @*/
2767552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2768552f7358SJed Brown {
2769552f7358SJed Brown   PetscErrorCode ierr;
2770552f7358SJed Brown 
2771552f7358SJed Brown   PetscFunctionBegin;
2772552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2773d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2774d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2775d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
277669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2777d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2778552f7358SJed Brown   PetscFunctionReturn(0);
2779552f7358SJed Brown }
2780552f7358SJed Brown 
2781552f7358SJed Brown /*@C
2782552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2783552f7358SJed Brown 
2784552f7358SJed Brown   Not Collective
2785552f7358SJed Brown 
2786552f7358SJed Brown   Input Parameters:
2787552f7358SJed Brown + dm - The DMPlex object
2788552f7358SJed Brown . numPoints - The number of input points for the meet
2789552f7358SJed Brown - points - The input points
2790552f7358SJed Brown 
2791552f7358SJed Brown   Output Parameters:
2792552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2793552f7358SJed Brown - coveredPoints - The points in the meet
2794552f7358SJed Brown 
2795552f7358SJed Brown   Level: intermediate
2796552f7358SJed Brown 
27973813dfbdSMatthew G Knepley   Fortran Notes:
27983813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27993813dfbdSMatthew G Knepley   include petsc.h90 in your code.
28003813dfbdSMatthew G Knepley 
28013813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
28023813dfbdSMatthew G Knepley 
2803552f7358SJed Brown .keywords: mesh
2804552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2805552f7358SJed Brown @*/
2806552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2807552f7358SJed Brown {
2808552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2809552f7358SJed Brown   PetscInt      *offsets, **closures;
2810552f7358SJed Brown   PetscInt      *meet[2];
2811552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
281224c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2813552f7358SJed Brown   PetscErrorCode ierr;
2814552f7358SJed Brown 
2815552f7358SJed Brown   PetscFunctionBegin;
2816552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2817552f7358SJed Brown   PetscValidPointer(points, 2);
2818552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2819552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2820552f7358SJed Brown 
2821552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2822785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
282369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
282424c766afSToby Isaac   mc      = mesh->maxConeSize;
282524c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
282669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
282769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2828552f7358SJed Brown 
2829552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2830552f7358SJed Brown     PetscInt closureSize;
2831552f7358SJed Brown 
2832552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
28330d644c17SKarl Rupp 
2834552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2835552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2836552f7358SJed Brown       PetscInt pStart, pEnd, i;
2837552f7358SJed Brown 
2838552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2839552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2840552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2841552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2842552f7358SJed Brown           break;
2843552f7358SJed Brown         }
2844552f7358SJed Brown       }
2845552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2846552f7358SJed Brown     }
284782f516ccSBarry 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);
2848552f7358SJed Brown   }
2849552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2850552f7358SJed Brown     PetscInt dof;
2851552f7358SJed Brown 
2852552f7358SJed Brown     /* Copy in cone of first point */
2853552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2854552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2855552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2856552f7358SJed Brown     }
2857552f7358SJed Brown     /* Check each successive cone */
2858552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2859552f7358SJed Brown       PetscInt newMeetSize = 0;
2860552f7358SJed Brown 
2861552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2862552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2863552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2864552f7358SJed Brown 
2865552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2866552f7358SJed Brown           if (point == meet[i][m]) {
2867552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2868552f7358SJed Brown             break;
2869552f7358SJed Brown           }
2870552f7358SJed Brown         }
2871552f7358SJed Brown       }
2872552f7358SJed Brown       meetSize = newMeetSize;
2873552f7358SJed Brown       i        = 1-i;
2874552f7358SJed Brown     }
2875552f7358SJed Brown     if (meetSize) break;
2876552f7358SJed Brown   }
2877552f7358SJed Brown   *numCoveredPoints = meetSize;
2878552f7358SJed Brown   *coveredPoints    = meet[i];
2879552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
28800298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2881552f7358SJed Brown   }
2882552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
288369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
288469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2885552f7358SJed Brown   PetscFunctionReturn(0);
2886552f7358SJed Brown }
2887552f7358SJed Brown 
28884e3744c5SMatthew G. Knepley /*@C
28894e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
28904e3744c5SMatthew G. Knepley 
28914e3744c5SMatthew G. Knepley   Not Collective
28924e3744c5SMatthew G. Knepley 
28934e3744c5SMatthew G. Knepley   Input Parameters:
28944e3744c5SMatthew G. Knepley + dmA - A DMPlex object
28954e3744c5SMatthew G. Knepley - dmB - A DMPlex object
28964e3744c5SMatthew G. Knepley 
28974e3744c5SMatthew G. Knepley   Output Parameters:
28984e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
28994e3744c5SMatthew G. Knepley 
29004e3744c5SMatthew G. Knepley   Level: intermediate
29014e3744c5SMatthew G. Knepley 
29024e3744c5SMatthew G. Knepley   Notes:
29034e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
29044e3744c5SMatthew G. Knepley 
29054e3744c5SMatthew G. Knepley .keywords: mesh
29064e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
29074e3744c5SMatthew G. Knepley @*/
29084e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
29094e3744c5SMatthew G. Knepley {
29104e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
29114e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
29124e3744c5SMatthew G. Knepley 
29134e3744c5SMatthew G. Knepley   PetscFunctionBegin;
29144e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
29154e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
29164e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
29174e3744c5SMatthew G. Knepley 
29184e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
29194e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
29204e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
29214e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
29224e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
29234e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
29244e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
29254e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
29264e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
29274e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
29284e3744c5SMatthew G. Knepley 
29294e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
29304e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
29314e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
29324e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
29334e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
29344e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
29354e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
29364e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
29374e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
29384e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
29394e3744c5SMatthew G. Knepley     }
29404e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
29414e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
29424e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
29434e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
29444e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
29454e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
29464e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
29474e3744c5SMatthew G. Knepley     }
29484e3744c5SMatthew G. Knepley   }
29494e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
29504e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
29514e3744c5SMatthew G. Knepley }
29524e3744c5SMatthew G. Knepley 
29537cd05799SMatthew G. Knepley /*@C
29547cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
29557cd05799SMatthew G. Knepley 
29567cd05799SMatthew G. Knepley   Not Collective
29577cd05799SMatthew G. Knepley 
29587cd05799SMatthew G. Knepley   Input Parameters:
29597cd05799SMatthew G. Knepley + dm         - The DMPlex
29607cd05799SMatthew G. Knepley . cellDim    - The cell dimension
29617cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
29627cd05799SMatthew G. Knepley 
29637cd05799SMatthew G. Knepley   Output Parameters:
29647cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
29657cd05799SMatthew G. Knepley 
29667cd05799SMatthew G. Knepley   Level: developer
29677cd05799SMatthew G. Knepley 
29687cd05799SMatthew G. Knepley   Notes:
29697cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
29707cd05799SMatthew G. Knepley 
29717cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
29727cd05799SMatthew G. Knepley @*/
297318ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2974a6dfd86eSKarl Rupp {
297582f516ccSBarry Smith   MPI_Comm       comm;
2976552f7358SJed Brown   PetscErrorCode ierr;
2977552f7358SJed Brown 
2978552f7358SJed Brown   PetscFunctionBegin;
297982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2980552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2981552f7358SJed Brown   switch (cellDim) {
2982552f7358SJed Brown   case 0:
2983552f7358SJed Brown     *numFaceVertices = 0;
2984552f7358SJed Brown     break;
2985552f7358SJed Brown   case 1:
2986552f7358SJed Brown     *numFaceVertices = 1;
2987552f7358SJed Brown     break;
2988552f7358SJed Brown   case 2:
2989552f7358SJed Brown     switch (numCorners) {
299019436ca2SJed Brown     case 3: /* triangle */
299119436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2992552f7358SJed Brown       break;
299319436ca2SJed Brown     case 4: /* quadrilateral */
299419436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2995552f7358SJed Brown       break;
299619436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
299719436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2998552f7358SJed Brown       break;
299919436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
300019436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3001552f7358SJed Brown       break;
3002552f7358SJed Brown     default:
3003f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3004552f7358SJed Brown     }
3005552f7358SJed Brown     break;
3006552f7358SJed Brown   case 3:
3007552f7358SJed Brown     switch (numCorners) {
300819436ca2SJed Brown     case 4: /* tetradehdron */
300919436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3010552f7358SJed Brown       break;
301119436ca2SJed Brown     case 6: /* tet cohesive cells */
301219436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3013552f7358SJed Brown       break;
301419436ca2SJed Brown     case 8: /* hexahedron */
301519436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3016552f7358SJed Brown       break;
301719436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
301819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3019552f7358SJed Brown       break;
302019436ca2SJed Brown     case 10: /* quadratic tetrahedron */
302119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3022552f7358SJed Brown       break;
302319436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
302419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3025552f7358SJed Brown       break;
302619436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
302719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3028552f7358SJed Brown       break;
302919436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
303019436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3031552f7358SJed Brown       break;
3032552f7358SJed Brown     default:
3033f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3034552f7358SJed Brown     }
3035552f7358SJed Brown     break;
3036552f7358SJed Brown   default:
3037f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3038552f7358SJed Brown   }
3039552f7358SJed Brown   PetscFunctionReturn(0);
3040552f7358SJed Brown }
3041552f7358SJed Brown 
3042552f7358SJed Brown /*@
3043aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3044552f7358SJed Brown 
3045552f7358SJed Brown   Not Collective
3046552f7358SJed Brown 
3047aa50250dSMatthew G. Knepley   Input Parameter:
3048552f7358SJed Brown . dm    - The DMPlex object
3049552f7358SJed Brown 
3050aa50250dSMatthew G. Knepley   Output Parameter:
3051aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3052552f7358SJed Brown 
3053552f7358SJed Brown   Level: developer
3054552f7358SJed Brown 
3055aa50250dSMatthew G. Knepley .keywords: mesh, points
3056aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3057aa50250dSMatthew G. Knepley @*/
3058aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3059aa50250dSMatthew G. Knepley {
3060aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
3061aa50250dSMatthew G. Knepley 
3062aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3063aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3064aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3065c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
3066c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3067aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3068aa50250dSMatthew G. Knepley }
3069aa50250dSMatthew G. Knepley 
3070aa50250dSMatthew G. Knepley /*@
3071aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3072aa50250dSMatthew G. Knepley 
3073aa50250dSMatthew G. Knepley   Not Collective
3074aa50250dSMatthew G. Knepley 
3075aa50250dSMatthew G. Knepley   Input Parameter:
3076aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3077aa50250dSMatthew G. Knepley 
3078aa50250dSMatthew G. Knepley   Output Parameter:
3079aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3080aa50250dSMatthew G. Knepley 
3081aa50250dSMatthew G. Knepley   Level: developer
3082552f7358SJed Brown 
3083552f7358SJed Brown .keywords: mesh, points
3084aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3085552f7358SJed Brown @*/
3086552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3087552f7358SJed Brown {
3088aa50250dSMatthew G. Knepley   DMLabel        label;
3089aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3090552f7358SJed Brown   PetscErrorCode ierr;
3091552f7358SJed Brown 
3092552f7358SJed Brown   PetscFunctionBegin;
3093552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3094552f7358SJed Brown   PetscValidPointer(depth, 2);
3095aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3096aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3097552f7358SJed Brown   *depth = d-1;
3098552f7358SJed Brown   PetscFunctionReturn(0);
3099552f7358SJed Brown }
3100552f7358SJed Brown 
3101552f7358SJed Brown /*@
3102552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3103552f7358SJed Brown 
3104552f7358SJed Brown   Not Collective
3105552f7358SJed Brown 
3106552f7358SJed Brown   Input Parameters:
3107552f7358SJed Brown + dm           - The DMPlex object
3108552f7358SJed Brown - stratumValue - The requested depth
3109552f7358SJed Brown 
3110552f7358SJed Brown   Output Parameters:
3111552f7358SJed Brown + start - The first point at this depth
3112552f7358SJed Brown - end   - One beyond the last point at this depth
3113552f7358SJed Brown 
3114552f7358SJed Brown   Level: developer
3115552f7358SJed Brown 
3116552f7358SJed Brown .keywords: mesh, points
3117552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3118552f7358SJed Brown @*/
31190adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
31200adebc6cSBarry Smith {
3121aa50250dSMatthew G. Knepley   DMLabel        label;
312263d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3123552f7358SJed Brown   PetscErrorCode ierr;
3124552f7358SJed Brown 
3125552f7358SJed Brown   PetscFunctionBegin;
3126552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
312763d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
312863d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3129552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
31300d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
313163d1a920SMatthew G. Knepley   if (stratumValue < 0) {
313263d1a920SMatthew G. Knepley     if (start) *start = pStart;
313363d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
313463d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3135552f7358SJed Brown   }
3136aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
313763d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
313863d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3139552f7358SJed Brown   PetscFunctionReturn(0);
3140552f7358SJed Brown }
3141552f7358SJed Brown 
3142552f7358SJed Brown /*@
3143552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3144552f7358SJed Brown 
3145552f7358SJed Brown   Not Collective
3146552f7358SJed Brown 
3147552f7358SJed Brown   Input Parameters:
3148552f7358SJed Brown + dm           - The DMPlex object
3149552f7358SJed Brown - stratumValue - The requested height
3150552f7358SJed Brown 
3151552f7358SJed Brown   Output Parameters:
3152552f7358SJed Brown + start - The first point at this height
3153552f7358SJed Brown - end   - One beyond the last point at this height
3154552f7358SJed Brown 
3155552f7358SJed Brown   Level: developer
3156552f7358SJed Brown 
3157552f7358SJed Brown .keywords: mesh, points
3158552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3159552f7358SJed Brown @*/
31600adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
31610adebc6cSBarry Smith {
3162aa50250dSMatthew G. Knepley   DMLabel        label;
316363d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3164552f7358SJed Brown   PetscErrorCode ierr;
3165552f7358SJed Brown 
3166552f7358SJed Brown   PetscFunctionBegin;
3167552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
316863d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
316963d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3170552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
31710d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
317263d1a920SMatthew G. Knepley   if (stratumValue < 0) {
317363d1a920SMatthew G. Knepley     if (start) *start = pStart;
317463d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
317563d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3176552f7358SJed Brown   }
3177aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
317813903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
317963d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
318063d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3181552f7358SJed Brown   PetscFunctionReturn(0);
3182552f7358SJed Brown }
3183552f7358SJed Brown 
3184552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
31857cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
3186a6dfd86eSKarl Rupp {
3187ee55978aSMatthew G. Knepley   PetscInt      *pMax;
3188470f9322SMatthew G. Knepley   PetscInt       depth, cellHeight, pStart = 0, pEnd = 0;
3189ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
3190fa716be8SMatthew G. Knepley   PetscBool     *isFE;
3191552f7358SJed Brown   PetscErrorCode ierr;
3192552f7358SJed Brown 
3193552f7358SJed Brown   PetscFunctionBegin;
3194fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
3195ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
3196fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
3197fa716be8SMatthew G. Knepley     PetscObject  obj;
3198fa716be8SMatthew G. Knepley     PetscClassId id;
3199fa716be8SMatthew G. Knepley 
3200ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
3201ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
3202fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
3203fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3204fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
3205fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
3206552f7358SJed Brown   }
320782f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
3208552f7358SJed Brown   if (numFields > 0) {
3209552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
3210552f7358SJed Brown     if (numComp) {
3211552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3212552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
3213d484f18aSToby Isaac         if (isFE[f]) {
3214d484f18aSToby Isaac           PetscFE           fe;
3215d484f18aSToby Isaac           PetscDualSpace    dspace;
3216d484f18aSToby Isaac           const PetscInt    ***perms;
3217d484f18aSToby Isaac           const PetscScalar ***flips;
3218d484f18aSToby Isaac           const PetscInt    *numDof;
3219d484f18aSToby Isaac 
3220d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
3221d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
3222d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
3223d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
3224d484f18aSToby Isaac           if (perms || flips) {
3225d484f18aSToby Isaac             DM               K;
3226d484f18aSToby Isaac             DMLabel          depthLabel;
3227d484f18aSToby Isaac             PetscInt         depth, h;
3228d484f18aSToby Isaac             PetscSectionSym  sym;
3229d484f18aSToby Isaac 
3230d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
3231d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
3232d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
3233d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
3234d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
3235d484f18aSToby Isaac               PetscDualSpace    hspace;
3236d484f18aSToby Isaac               PetscInt          kStart, kEnd;
3237d484f18aSToby Isaac               PetscInt          kConeSize;
3238d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3239d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3240d484f18aSToby Isaac 
3241d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3242d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3243d484f18aSToby Isaac               if (!hspace) continue;
3244d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3245d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3246d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3247d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3248d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3249d484f18aSToby Isaac               ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3250d484f18aSToby Isaac             }
3251d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3252d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3253d484f18aSToby Isaac           }
3254d484f18aSToby Isaac         }
3255552f7358SJed Brown       }
3256552f7358SJed Brown     }
3257552f7358SJed Brown   }
3258552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3259552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3260916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
32619ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
32629ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
3263470f9322SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
3264470f9322SMatthew G. Knepley   for (dep = 0; dep <= depth - cellHeight; ++dep) {
3265916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3266916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3267fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3268552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3269ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3270ee55978aSMatthew G. Knepley 
3271552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3272fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3273552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3274ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3275552f7358SJed Brown       }
3276ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3277552f7358SJed Brown     }
3278552f7358SJed Brown   }
32799ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3280fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3281552f7358SJed Brown   PetscFunctionReturn(0);
3282552f7358SJed Brown }
3283552f7358SJed Brown 
3284552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3285ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3286552f7358SJed Brown */
32877cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3288a6dfd86eSKarl Rupp {
3289552f7358SJed Brown   PetscInt       numFields;
3290552f7358SJed Brown   PetscInt       bc;
3291a68b90caSToby Isaac   PetscSection   aSec;
3292552f7358SJed Brown   PetscErrorCode ierr;
3293552f7358SJed Brown 
3294552f7358SJed Brown   PetscFunctionBegin;
3295552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3296552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3297552f7358SJed Brown     PetscInt        field = 0;
3298ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3299552f7358SJed Brown     const PetscInt *idx;
3300ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3301552f7358SJed Brown 
33020d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3303ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3304ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3305552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3306552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3307552f7358SJed Brown     for (i = 0; i < n; ++i) {
3308552f7358SJed Brown       const PetscInt p = idx[i];
330906ff1081SMatthew G. Knepley       PetscInt       numConst;
3310552f7358SJed Brown 
3311552f7358SJed Brown       if (numFields) {
3312552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3313552f7358SJed Brown       } else {
3314552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3315552f7358SJed Brown       }
331606ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
331706ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
331806ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3319552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3320552f7358SJed Brown     }
3321552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
332206ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3323552f7358SJed Brown   }
3324a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3325a68b90caSToby Isaac   if (aSec) {
3326a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3327a68b90caSToby Isaac 
3328a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3329a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3330ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3331a68b90caSToby Isaac 
3332a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3333a68b90caSToby Isaac       if (dof) {
3334a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3335a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3336a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3337a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3338a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3339a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3340a68b90caSToby Isaac         }
3341a68b90caSToby Isaac       }
3342a68b90caSToby Isaac     }
3343a68b90caSToby Isaac   }
3344552f7358SJed Brown   PetscFunctionReturn(0);
3345552f7358SJed Brown }
3346552f7358SJed Brown 
3347ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3348ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3349ab1d0545SMatthew G. Knepley */
33507cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
33510adebc6cSBarry Smith {
3352ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3353ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3354503335f2SSander Arens   PetscInt       numFields, cdof, maxDof = 0, pStart, pEnd, p, bc, f, d;
3355552f7358SJed Brown   PetscErrorCode ierr;
3356552f7358SJed Brown 
3357552f7358SJed Brown   PetscFunctionBegin;
3358552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3359ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3360ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3361552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3362503335f2SSander Arens   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr); maxDof = PetscMax(maxDof, cdof);}
3363ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3364ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3365ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3366ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3367ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3368ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3369ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3370ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3371552f7358SJed Brown 
3372ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3373ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3374ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3375ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3376ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3377ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3378ab1d0545SMatthew G. Knepley       const PetscInt *find;
3379503335f2SSander Arens       PetscInt        fdof, fcdof, c;
3380ab1d0545SMatthew G. Knepley 
3381503335f2SSander Arens       ierr = PetscSectionGetFieldDof(section, p, field, &fdof);CHKERRQ(ierr);
3382503335f2SSander Arens       if (!fdof) continue;
3383ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3384503335f2SSander Arens         for (d = 0; d < fdof; ++d) indices[d] = d;
3385503335f2SSander Arens         fcdof = fdof;
3386552f7358SJed Brown       } else {
3387503335f2SSander Arens         ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3388ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3389ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3390503335f2SSander Arens         for (c = 0; c < Nc; ++c) indices[d++] = comp[c];
3391503335f2SSander Arens         ierr = PetscSortRemoveDupsInt(&d, indices);CHKERRQ(ierr);
3392503335f2SSander Arens         for (c = d; c < fcdof; ++c) indices[c] = -1;
3393503335f2SSander Arens         fcdof = d;
3394552f7358SJed Brown       }
3395503335f2SSander Arens       ierr = PetscSectionSetFieldConstraintDof(section, p, field, fcdof);CHKERRQ(ierr);
3396ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3397552f7358SJed Brown     }
3398ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3399ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3400552f7358SJed Brown   }
3401ab1d0545SMatthew G. Knepley   /* Handle anchors */
3402ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3403ab1d0545SMatthew G. Knepley   if (aSec) {
3404ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3405552f7358SJed Brown 
3406ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3407ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3408ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3409503335f2SSander Arens       PetscInt dof, f;
3410ab1d0545SMatthew G. Knepley 
3411ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3412ab1d0545SMatthew G. Knepley       if (dof) {
3413ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3414ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3415ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3416ab1d0545SMatthew G. Knepley         }
3417ab1d0545SMatthew G. Knepley       }
3418ab1d0545SMatthew G. Knepley     }
3419ab1d0545SMatthew G. Knepley   }
3420ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3421ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3422ab1d0545SMatthew G. Knepley }
3423ab1d0545SMatthew G. Knepley 
3424ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
34257cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3426ab1d0545SMatthew G. Knepley {
3427ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3428ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3429ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3430ab1d0545SMatthew G. Knepley 
3431ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3432ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3433ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3434ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3435ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3436ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3437552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3438552f7358SJed Brown     PetscInt cdof, d;
3439552f7358SJed Brown 
3440552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3441552f7358SJed Brown     if (cdof) {
3442552f7358SJed Brown       if (numFields) {
3443552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3444552f7358SJed Brown 
3445552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3446ab1d0545SMatthew G. Knepley           const PetscInt *find;
3447ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3448552f7358SJed Brown 
3449552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3450ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3451ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3452ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3453ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3454ab1d0545SMatthew G. Knepley           numConst += fcdof;
3455552f7358SJed Brown           foff     += fdof;
3456552f7358SJed Brown         }
3457503335f2SSander Arens         if (cdof != numConst) {ierr = PetscSectionSetConstraintDof(section, p, numConst);CHKERRQ(ierr);}
3458552f7358SJed Brown       } else {
34590d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3460552f7358SJed Brown       }
3461552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3462552f7358SJed Brown     }
3463552f7358SJed Brown   }
3464552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3465552f7358SJed Brown   PetscFunctionReturn(0);
3466552f7358SJed Brown }
3467552f7358SJed Brown 
3468552f7358SJed Brown /*@C
3469552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3470552f7358SJed Brown 
3471552f7358SJed Brown   Not Collective
3472552f7358SJed Brown 
3473552f7358SJed Brown   Input Parameters:
3474552f7358SJed Brown + dm        - The DMPlex object
3475552f7358SJed Brown . dim       - The spatial dimension of the problem
3476552f7358SJed Brown . numFields - The number of fields in the problem
3477552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3478552f7358SJed Brown . numDof    - An array of size numFields*(dim+1) which holds the number of dof for each field on a mesh piece of dimension d
3479552f7358SJed Brown . numBC     - The number of boundary conditions
3480552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3481ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3482ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3483f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3484552f7358SJed Brown 
3485552f7358SJed Brown   Output Parameter:
3486552f7358SJed Brown . section - The PetscSection object
3487552f7358SJed Brown 
348895452b02SPatrick Sanan   Notes:
348995452b02SPatrick Sanan     numDof[f*(dim+1)+d] gives the number of dof for field f on points of dimension d. For instance, numDof[1] is the
3490f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3491f8f126e8SMatthew G. Knepley 
3492f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3493552f7358SJed Brown 
3494552f7358SJed Brown   Level: developer
3495552f7358SJed Brown 
34963813dfbdSMatthew G Knepley   Fortran Notes:
34973813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
34983813dfbdSMatthew G Knepley 
3499552f7358SJed Brown .keywords: mesh, elements
3500f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3501552f7358SJed Brown @*/
3502ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSection(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], IS perm, PetscSection *section)
3503a6dfd86eSKarl Rupp {
3504a68b90caSToby Isaac   PetscSection   aSec;
3505552f7358SJed Brown   PetscErrorCode ierr;
3506552f7358SJed Brown 
3507552f7358SJed Brown   PetscFunctionBegin;
3508552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3509ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3510f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3511552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3512a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3513ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3514ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3515ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3516ab1d0545SMatthew G. Knepley   }
3517ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3518552f7358SJed Brown   PetscFunctionReturn(0);
3519552f7358SJed Brown }
3520552f7358SJed Brown 
35210adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
35220adebc6cSBarry Smith {
3523efe440bfSMatthew G. Knepley   PetscSection   section, s;
3524efe440bfSMatthew G. Knepley   Mat            m;
35253e922f36SToby Isaac   PetscInt       maxHeight;
3526552f7358SJed Brown   PetscErrorCode ierr;
3527552f7358SJed Brown 
3528552f7358SJed Brown   PetscFunctionBegin;
352938221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
35303e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
35313e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
353282f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3533552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
35341d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3535efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3536efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3537efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3538efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3539efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3540552f7358SJed Brown   PetscFunctionReturn(0);
3541552f7358SJed Brown }
3542552f7358SJed Brown 
3543f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3544f19dbd58SToby Isaac {
3545f19dbd58SToby Isaac   Vec            coordsLocal;
3546f19dbd58SToby Isaac   DM             coordsDM;
3547f19dbd58SToby Isaac   PetscErrorCode ierr;
3548f19dbd58SToby Isaac 
3549f19dbd58SToby Isaac   PetscFunctionBegin;
3550f19dbd58SToby Isaac   *field = NULL;
3551f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3552f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3553f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3554f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3555f19dbd58SToby Isaac   }
3556f19dbd58SToby Isaac   PetscFunctionReturn(0);
3557f19dbd58SToby Isaac }
3558f19dbd58SToby Isaac 
35597cd05799SMatthew G. Knepley /*@C
35607cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
35617cd05799SMatthew G. Knepley 
35627cd05799SMatthew G. Knepley   Not Collective
35637cd05799SMatthew G. Knepley 
35647cd05799SMatthew G. Knepley   Input Parameters:
35657cd05799SMatthew G. Knepley . dm        - The DMPlex object
35667cd05799SMatthew G. Knepley 
35677cd05799SMatthew G. Knepley   Output Parameter:
35687cd05799SMatthew G. Knepley . section - The PetscSection object
35697cd05799SMatthew G. Knepley 
35707cd05799SMatthew G. Knepley   Level: developer
35717cd05799SMatthew G. Knepley 
35727cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
35737cd05799SMatthew G. Knepley @*/
35740adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
35750adebc6cSBarry Smith {
3576552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3577552f7358SJed Brown 
3578552f7358SJed Brown   PetscFunctionBegin;
3579552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3580552f7358SJed Brown   if (section) *section = mesh->coneSection;
3581552f7358SJed Brown   PetscFunctionReturn(0);
3582552f7358SJed Brown }
3583552f7358SJed Brown 
35847cd05799SMatthew G. Knepley /*@C
35857cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
35867cd05799SMatthew G. Knepley 
35877cd05799SMatthew G. Knepley   Not Collective
35887cd05799SMatthew G. Knepley 
35897cd05799SMatthew G. Knepley   Input Parameters:
35907cd05799SMatthew G. Knepley . dm        - The DMPlex object
35917cd05799SMatthew G. Knepley 
35927cd05799SMatthew G. Knepley   Output Parameter:
35937cd05799SMatthew G. Knepley . section - The PetscSection object
35947cd05799SMatthew G. Knepley 
35957cd05799SMatthew G. Knepley   Level: developer
35967cd05799SMatthew G. Knepley 
35977cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35987cd05799SMatthew G. Knepley @*/
35998cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
36008cb4d582SMatthew G. Knepley {
36018cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
36028cb4d582SMatthew G. Knepley 
36038cb4d582SMatthew G. Knepley   PetscFunctionBegin;
36048cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36058cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
36068cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
36078cb4d582SMatthew G. Knepley }
36088cb4d582SMatthew G. Knepley 
36097cd05799SMatthew G. Knepley /*@C
36107cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
36117cd05799SMatthew G. Knepley 
36127cd05799SMatthew G. Knepley   Not Collective
36137cd05799SMatthew G. Knepley 
36147cd05799SMatthew G. Knepley   Input Parameters:
36157cd05799SMatthew G. Knepley . dm        - The DMPlex object
36167cd05799SMatthew G. Knepley 
36177cd05799SMatthew G. Knepley   Output Parameter:
36187cd05799SMatthew G. Knepley . cones - The cone for each point
36197cd05799SMatthew G. Knepley 
36207cd05799SMatthew G. Knepley   Level: developer
36217cd05799SMatthew G. Knepley 
36227cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
36237cd05799SMatthew G. Knepley @*/
3624a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3625a6dfd86eSKarl Rupp {
3626552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3627552f7358SJed Brown 
3628552f7358SJed Brown   PetscFunctionBegin;
3629552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3630552f7358SJed Brown   if (cones) *cones = mesh->cones;
3631552f7358SJed Brown   PetscFunctionReturn(0);
3632552f7358SJed Brown }
3633552f7358SJed Brown 
36347cd05799SMatthew G. Knepley /*@C
36357cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
36367cd05799SMatthew G. Knepley 
36377cd05799SMatthew G. Knepley   Not Collective
36387cd05799SMatthew G. Knepley 
36397cd05799SMatthew G. Knepley   Input Parameters:
36407cd05799SMatthew G. Knepley . dm        - The DMPlex object
36417cd05799SMatthew G. Knepley 
36427cd05799SMatthew G. Knepley   Output Parameter:
36437cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
36447cd05799SMatthew G. Knepley 
36457cd05799SMatthew G. Knepley   Level: developer
36467cd05799SMatthew G. Knepley 
36477cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
36487cd05799SMatthew G. Knepley @*/
3649a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3650a6dfd86eSKarl Rupp {
3651552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3652552f7358SJed Brown 
3653552f7358SJed Brown   PetscFunctionBegin;
3654552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3655552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3656552f7358SJed Brown   PetscFunctionReturn(0);
3657552f7358SJed Brown }
3658552f7358SJed Brown 
3659552f7358SJed Brown /******************************** FEM Support **********************************/
3660552f7358SJed Brown 
36617391a63aSMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscInt point, PetscSection section)
36623194fc30SMatthew G. Knepley {
36637391a63aSMatthew G. Knepley   DMLabel        label;
36643194fc30SMatthew G. Knepley   PetscInt      *perm;
36657391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
36663194fc30SMatthew G. Knepley   PetscErrorCode ierr;
36673194fc30SMatthew G. Knepley 
36683194fc30SMatthew G. Knepley   PetscFunctionBegin;
36697391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
36703194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
36717391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
36727391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
36737391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
36747391a63aSMatthew G. Knepley   else if  (depth == dim) {
36757391a63aSMatthew G. Knepley     const PetscInt *cone;
36767391a63aSMatthew G. Knepley 
36777391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
36787391a63aSMatthew G. Knepley     eStart = cone[0];
36797391a63aSMatthew G. Knepley   } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering", point, depth);
36807391a63aSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
36813194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
368289eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
36833194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
36843194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
36853194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
36863194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
36873194fc30SMatthew G. Knepley     k = k/Nc + 1;
368889eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
36893194fc30SMatthew G. Knepley   }
36903194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
36913194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
369289eabcffSMatthew G. Knepley     switch (dim) {
369389eabcffSMatthew G. Knepley     case 2:
36943194fc30SMatthew 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} */
36953194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
36963194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
36973194fc30SMatthew G. Knepley       k = k/Nc + 1;
36983194fc30SMatthew G. Knepley       /* The SEM order is
36993194fc30SMatthew G. Knepley 
37003194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
370189eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
37023194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
37033194fc30SMatthew G. Knepley       */
37043194fc30SMatthew G. Knepley       {
37053194fc30SMatthew G. Knepley         const PetscInt of   = 0;
37063194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
37073194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
37083194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
37093194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
37103194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
37113194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
37123194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
37133194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
37143194fc30SMatthew G. Knepley         PetscInt       o;
37153194fc30SMatthew G. Knepley 
37163194fc30SMatthew G. Knepley         /* bottom */
37173194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
37183194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
37193194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
37203194fc30SMatthew G. Knepley         /* middle */
37213194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
37223194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
37233194fc30SMatthew 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;
37243194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
37253194fc30SMatthew G. Knepley         }
37263194fc30SMatthew G. Knepley         /* top */
37273194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
37283194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
37293194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
37303194fc30SMatthew G. Knepley         foffset = offset;
37313194fc30SMatthew G. Knepley       }
373289eabcffSMatthew G. Knepley       break;
373389eabcffSMatthew G. Knepley     case 3:
373489eabcffSMatthew G. Knepley       /* The original hex closure is
373589eabcffSMatthew G. Knepley 
373689eabcffSMatthew G. Knepley          {c,
373789eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
373889eabcffSMatthew 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,
373989eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
374089eabcffSMatthew G. Knepley       */
374189eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
374289eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
374389eabcffSMatthew G. Knepley       k = k/Nc + 1;
374489eabcffSMatthew G. Knepley       /* The SEM order is
374589eabcffSMatthew G. Knepley          Bottom Slice
374689eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
374789eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
374889eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
374989eabcffSMatthew G. Knepley 
375089eabcffSMatthew G. Knepley          Middle Slice (j)
375189eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
375289eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
375389eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
375489eabcffSMatthew G. Knepley 
375589eabcffSMatthew G. Knepley          Top Slice
375689eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
375789eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
375889eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
375989eabcffSMatthew G. Knepley       */
376089eabcffSMatthew G. Knepley       {
376189eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
376289eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
376389eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
376489eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
376589eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
376689eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
376789eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
376889eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
376989eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
377089eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
377189eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
377289eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
377389eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
377489eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
377589eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
377689eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
377789eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
377889eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
377989eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
378089eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
378189eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
378289eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
378389eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
378489eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
378589eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
378689eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
378789eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
378889eabcffSMatthew G. Knepley         PetscInt       o, n;
378989eabcffSMatthew G. Knepley 
379089eabcffSMatthew G. Knepley         /* Bottom Slice */
379189eabcffSMatthew G. Knepley         /*   bottom */
379289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
379389eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
379489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
379589eabcffSMatthew G. Knepley         /*   middle */
379689eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
379789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3798316b7f87SMax 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;}
379989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
38003194fc30SMatthew G. Knepley         }
380189eabcffSMatthew G. Knepley         /*   top */
380289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
380389eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
380489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
380589eabcffSMatthew G. Knepley 
380689eabcffSMatthew G. Knepley         /* Middle Slice */
380789eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
380889eabcffSMatthew G. Knepley           /*   bottom */
380989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
381089eabcffSMatthew 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;
381189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
381289eabcffSMatthew G. Knepley           /*   middle */
381389eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
381489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
381589eabcffSMatthew 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;
381689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
381789eabcffSMatthew G. Knepley           }
381889eabcffSMatthew G. Knepley           /*   top */
381989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
382089eabcffSMatthew 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;
382189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
382289eabcffSMatthew G. Knepley         }
382389eabcffSMatthew G. Knepley 
382489eabcffSMatthew G. Knepley         /* Top Slice */
382589eabcffSMatthew G. Knepley         /*   bottom */
382689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
382789eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
382889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
382989eabcffSMatthew G. Knepley         /*   middle */
383089eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
383189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
383289eabcffSMatthew 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;
383389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
383489eabcffSMatthew G. Knepley         }
383589eabcffSMatthew G. Knepley         /*   top */
383689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
383789eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
383889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
383989eabcffSMatthew G. Knepley 
384089eabcffSMatthew G. Knepley         foffset = offset;
384189eabcffSMatthew G. Knepley       }
384289eabcffSMatthew G. Knepley       break;
384389eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
384489eabcffSMatthew G. Knepley     }
384589eabcffSMatthew G. Knepley   }
384689eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
38473194fc30SMatthew G. Knepley   /* Check permutation */
38483194fc30SMatthew G. Knepley   {
38493194fc30SMatthew G. Knepley     PetscInt *check;
38503194fc30SMatthew G. Knepley 
38513194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
38523194fc30SMatthew 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]);}
38533194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
38543194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
38553194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
38563194fc30SMatthew G. Knepley   }
38571fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
38583194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
38593194fc30SMatthew G. Knepley }
38603194fc30SMatthew G. Knepley 
3861e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3862e071409bSToby Isaac {
3863e071409bSToby Isaac   PetscDS        prob;
3864e071409bSToby Isaac   PetscInt       depth, Nf, h;
3865e071409bSToby Isaac   DMLabel        label;
3866e071409bSToby Isaac   PetscErrorCode ierr;
3867e071409bSToby Isaac 
3868e071409bSToby Isaac   PetscFunctionBeginHot;
3869e071409bSToby Isaac   prob    = dm->prob;
3870e071409bSToby Isaac   Nf      = prob->Nf;
3871e071409bSToby Isaac   label   = dm->depthLabel;
3872e071409bSToby Isaac   *dspace = NULL;
3873e071409bSToby Isaac   if (field < Nf) {
3874e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3875e071409bSToby Isaac 
3876e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3877e071409bSToby Isaac       PetscDualSpace dsp;
3878e071409bSToby Isaac 
3879e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3880e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3881e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3882e071409bSToby Isaac       h    = depth - 1 - h;
3883e071409bSToby Isaac       if (h) {
3884e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3885e071409bSToby Isaac       } else {
3886e071409bSToby Isaac         *dspace = dsp;
3887e071409bSToby Isaac       }
3888e071409bSToby Isaac     }
3889e071409bSToby Isaac   }
3890e071409bSToby Isaac   PetscFunctionReturn(0);
3891e071409bSToby Isaac }
3892e071409bSToby Isaac 
3893e071409bSToby Isaac 
38941a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3895a6dfd86eSKarl Rupp {
3896552f7358SJed Brown   PetscScalar    *array, *vArray;
3897d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
38981a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3899552f7358SJed Brown   PetscErrorCode  ierr;
3900552f7358SJed Brown 
39011b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
39022a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
39035a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
39045a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
39055a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
39063f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
39079df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
39089df71ca4SMatthew G. Knepley       PetscInt dof;
3909d9917b9dSMatthew G. Knepley 
39109df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39119df71ca4SMatthew G. Knepley       size += dof;
39129df71ca4SMatthew G. Knepley     }
39139df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
39149df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
39152a3aaacfSMatthew G. Knepley       PetscInt       dof;
39165a1bb5cfSMatthew G. Knepley 
39175a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
39182a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
39195a1bb5cfSMatthew G. Knepley       size += dof;
39205a1bb5cfSMatthew G. Knepley     }
39213f7cbbe7SMatthew G. Knepley     if (!values) {
39223f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
39233f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
39243f7cbbe7SMatthew G. Knepley     }
392569291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
3926982e9ed1SMatthew G. Knepley   } else {
3927982e9ed1SMatthew G. Knepley     array = *values;
3928982e9ed1SMatthew G. Knepley   }
39299df71ca4SMatthew G. Knepley   size = 0;
39305a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
39319df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
39329df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
39339df71ca4SMatthew G. Knepley     PetscScalar *varr;
3934d9917b9dSMatthew G. Knepley 
39359df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39369df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
39379df71ca4SMatthew G. Knepley     varr = &vArray[off];
39381a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
39391a271a75SMatthew G. Knepley       array[offset] = varr[d];
39409df71ca4SMatthew G. Knepley     }
39419df71ca4SMatthew G. Knepley     size += dof;
39429df71ca4SMatthew G. Knepley   }
39439df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
39449df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
39459df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
39465a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
39475a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
39485a1bb5cfSMatthew G. Knepley 
394952ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
39505a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
39515a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
39525a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
39535a1bb5cfSMatthew G. Knepley     if (o >= 0) {
39541a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
39551a271a75SMatthew G. Knepley         array[offset] = varr[d];
39565a1bb5cfSMatthew G. Knepley       }
39575a1bb5cfSMatthew G. Knepley     } else {
39581a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
39591a271a75SMatthew G. Knepley         array[offset] = varr[d];
39605a1bb5cfSMatthew G. Knepley       }
39615a1bb5cfSMatthew G. Knepley     }
39629df71ca4SMatthew G. Knepley     size += dof;
39635a1bb5cfSMatthew G. Knepley   }
39645a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
39659df71ca4SMatthew G. Knepley   if (!*values) {
39665a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
39675a1bb5cfSMatthew G. Knepley     *values = array;
39689df71ca4SMatthew G. Knepley   } else {
39698ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
39708c312ff3SMatthew G. Knepley     *csize = size;
39719df71ca4SMatthew G. Knepley   }
39725a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
39735a1bb5cfSMatthew G. Knepley }
3974d9917b9dSMatthew G. Knepley 
3975923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3976923c78e0SToby Isaac {
3977923c78e0SToby Isaac   const PetscInt *cla;
3978923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3979923c78e0SToby Isaac   PetscErrorCode ierr;
3980923c78e0SToby Isaac 
3981923c78e0SToby Isaac   PetscFunctionBeginHot;
3982923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3983923c78e0SToby Isaac   if (!*clPoints) {
3984923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3985923c78e0SToby Isaac 
3986923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3987923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3988923c78e0SToby Isaac     /* Compress out points not in the section */
3989923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3990923c78e0SToby Isaac       PetscInt r = pts[2*p];
3991923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3992923c78e0SToby Isaac         pts[q*2]   = r;
3993923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3994923c78e0SToby Isaac         ++q;
3995923c78e0SToby Isaac       }
3996923c78e0SToby Isaac     }
3997923c78e0SToby Isaac     np = q;
3998923c78e0SToby Isaac     cla = NULL;
3999923c78e0SToby Isaac   } else {
4000923c78e0SToby Isaac     PetscInt dof, off;
4001923c78e0SToby Isaac 
4002923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4003923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4004923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4005923c78e0SToby Isaac     np   = dof/2;
4006923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
4007923c78e0SToby Isaac   }
4008923c78e0SToby Isaac   *numPoints = np;
4009923c78e0SToby Isaac   *points    = pts;
4010923c78e0SToby Isaac   *clp       = cla;
4011923c78e0SToby Isaac 
4012923c78e0SToby Isaac   PetscFunctionReturn(0);
4013923c78e0SToby Isaac }
4014923c78e0SToby Isaac 
4015923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4016923c78e0SToby Isaac {
4017923c78e0SToby Isaac   PetscErrorCode ierr;
4018923c78e0SToby Isaac 
4019923c78e0SToby Isaac   PetscFunctionBeginHot;
4020923c78e0SToby Isaac   if (!*clPoints) {
4021923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4022923c78e0SToby Isaac   } else {
4023923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4024923c78e0SToby Isaac   }
4025923c78e0SToby Isaac   *numPoints = 0;
4026923c78e0SToby Isaac   *points    = NULL;
4027923c78e0SToby Isaac   *clSec     = NULL;
4028923c78e0SToby Isaac   *clPoints  = NULL;
4029923c78e0SToby Isaac   *clp       = NULL;
4030923c78e0SToby Isaac   PetscFunctionReturn(0);
4031923c78e0SToby Isaac }
4032923c78e0SToby Isaac 
403397e99dd9SToby 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[])
40341a271a75SMatthew G. Knepley {
40351a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
403697e99dd9SToby Isaac   const PetscInt    **perms = NULL;
403797e99dd9SToby Isaac   const PetscScalar **flips = NULL;
40381a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
40391a271a75SMatthew G. Knepley 
40401a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4041fe02ba77SJed Brown   *size = 0;
404297e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
404397e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
404497e99dd9SToby Isaac     const PetscInt    point = points[2*p];
404597e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
404697e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
40471a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
40481a271a75SMatthew G. Knepley     const PetscScalar *varr;
40491a271a75SMatthew G. Knepley 
40501a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
40511a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
40521a271a75SMatthew G. Knepley     varr = &vArray[off];
405397e99dd9SToby Isaac     if (clperm) {
405497e99dd9SToby Isaac       if (perm) {
405597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
40561a271a75SMatthew G. Knepley       } else {
405797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
405897e99dd9SToby Isaac       }
405997e99dd9SToby Isaac       if (flip) {
406097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
406197e99dd9SToby Isaac       }
406297e99dd9SToby Isaac     } else {
406397e99dd9SToby Isaac       if (perm) {
406497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
406597e99dd9SToby Isaac       } else {
406697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
406797e99dd9SToby Isaac       }
406897e99dd9SToby Isaac       if (flip) {
406997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
40701a271a75SMatthew G. Knepley       }
40711a271a75SMatthew G. Knepley     }
407297e99dd9SToby Isaac     offset += dof;
407397e99dd9SToby Isaac   }
407497e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
40751a271a75SMatthew G. Knepley   *size = offset;
40761a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
40771a271a75SMatthew G. Knepley }
40781a271a75SMatthew G. Knepley 
407997e99dd9SToby 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[])
40801a271a75SMatthew G. Knepley {
40811a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
40821a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
40831a271a75SMatthew G. Knepley 
40841a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4085fe02ba77SJed Brown   *size = 0;
40861a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
408797e99dd9SToby Isaac     PetscInt          p;
408897e99dd9SToby Isaac     const PetscInt    **perms = NULL;
408997e99dd9SToby Isaac     const PetscScalar **flips = NULL;
40901a271a75SMatthew G. Knepley 
409197e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
409297e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
409397e99dd9SToby Isaac       const PetscInt    point = points[2*p];
409497e99dd9SToby Isaac       PetscInt          fdof, foff, b;
40951a271a75SMatthew G. Knepley       const PetscScalar *varr;
409697e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
409797e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
40981a271a75SMatthew G. Knepley 
40991a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
41001a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41011a271a75SMatthew G. Knepley       varr = &vArray[foff];
410297e99dd9SToby Isaac       if (clperm) {
410397e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
410497e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
410597e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
41061a271a75SMatthew G. Knepley       } else {
410797e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
410897e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
410997e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
41101a271a75SMatthew G. Knepley       }
411197e99dd9SToby Isaac       offset += fdof;
41121a271a75SMatthew G. Knepley     }
411397e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
41141a271a75SMatthew G. Knepley   }
41151a271a75SMatthew G. Knepley   *size = offset;
41161a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
41171a271a75SMatthew G. Knepley }
41181a271a75SMatthew G. Knepley 
4119552f7358SJed Brown /*@C
4120552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4121552f7358SJed Brown 
4122552f7358SJed Brown   Not collective
4123552f7358SJed Brown 
4124552f7358SJed Brown   Input Parameters:
4125552f7358SJed Brown + dm - The DM
4126552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4127552f7358SJed Brown . v - The local vector
412822c1ee49SMatthew G. Knepley . point - The point in the DM
412922c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
413022c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4131552f7358SJed Brown 
4132552f7358SJed Brown   Output Parameters:
413322c1ee49SMatthew G. Knepley + csize - The number of values in the closure
413422c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
413522c1ee49SMatthew G. Knepley 
413622c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
413722c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
413822c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
413922c1ee49SMatthew G. Knepley $
414022c1ee49SMatthew G. Knepley $ A typical use could be
414122c1ee49SMatthew G. Knepley $
414222c1ee49SMatthew G. Knepley $  values = NULL;
414322c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
414422c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
414522c1ee49SMatthew G. Knepley $    <Compute on closure>
414622c1ee49SMatthew G. Knepley $  }
414722c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
414822c1ee49SMatthew G. Knepley $
414922c1ee49SMatthew G. Knepley $ or
415022c1ee49SMatthew G. Knepley $
415122c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
415222c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
415322c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
415422c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
415522c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
415622c1ee49SMatthew G. Knepley $      <Compute on closure>
415722c1ee49SMatthew G. Knepley $    }
415822c1ee49SMatthew G. Knepley $  }
415922c1ee49SMatthew G. Knepley $  PetscFree(values);
4160552f7358SJed Brown 
4161552f7358SJed Brown   Fortran Notes:
4162552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4163552f7358SJed Brown   include petsc.h90 in your code.
4164552f7358SJed Brown 
4165552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4166552f7358SJed Brown 
4167552f7358SJed Brown   Level: intermediate
4168552f7358SJed Brown 
4169552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4170552f7358SJed Brown @*/
4171552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4172552f7358SJed Brown {
4173552f7358SJed Brown   PetscSection       clSection;
4174d9917b9dSMatthew G. Knepley   IS                 clPoints;
41758a84db2dSMatthew G. Knepley   PetscScalar       *array;
41768a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4177552f7358SJed Brown   PetscInt          *points = NULL;
41788f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
41791a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4180552f7358SJed Brown   PetscErrorCode     ierr;
4181552f7358SJed Brown 
4182d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4183552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4184552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
41851a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
41861a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4187552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4188552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4189552f7358SJed Brown   if (depth == 1 && numFields < 2) {
41901a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4191552f7358SJed Brown     PetscFunctionReturn(0);
4192552f7358SJed Brown   }
41931a271a75SMatthew G. Knepley   /* Get points */
4194923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41951fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
41961a271a75SMatthew G. Knepley   /* Get array */
4197bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
41981a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4199552f7358SJed Brown 
42001a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4201552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
42021a271a75SMatthew G. Knepley       asize += dof;
4203552f7358SJed Brown     }
4204bd6c0d32SMatthew G. Knepley     if (!values) {
4205923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
42061a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4207bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4208bd6c0d32SMatthew G. Knepley     }
420969291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4210d0f6b257SMatthew G. Knepley   } else {
4211d0f6b257SMatthew G. Knepley     array = *values;
4212d0f6b257SMatthew G. Knepley   }
42138a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
42141a271a75SMatthew G. Knepley   /* Get values */
421597e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
421697e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
42171a271a75SMatthew G. Knepley   /* Cleanup points */
4218923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
42191a271a75SMatthew G. Knepley   /* Cleanup array */
42208a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4221d0f6b257SMatthew G. Knepley   if (!*values) {
4222552f7358SJed Brown     if (csize) *csize = size;
4223552f7358SJed Brown     *values = array;
4224d0f6b257SMatthew G. Knepley   } else {
4225f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4226d0f6b257SMatthew G. Knepley     *csize = size;
4227d0f6b257SMatthew G. Knepley   }
4228552f7358SJed Brown   PetscFunctionReturn(0);
4229552f7358SJed Brown }
4230552f7358SJed Brown 
4231552f7358SJed Brown /*@C
4232552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4233552f7358SJed Brown 
4234552f7358SJed Brown   Not collective
4235552f7358SJed Brown 
4236552f7358SJed Brown   Input Parameters:
4237552f7358SJed Brown + dm - The DM
42380298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4239552f7358SJed Brown . v - The local vector
4240eaf898f9SPatrick Sanan . point - The point in the DM
42410298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4242552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4243552f7358SJed Brown 
424422c1ee49SMatthew 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()
424522c1ee49SMatthew G. Knepley 
42463813dfbdSMatthew G Knepley   Fortran Notes:
42473813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
42483813dfbdSMatthew G Knepley   include petsc.h90 in your code.
42493813dfbdSMatthew G Knepley 
42503813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
42513813dfbdSMatthew G Knepley 
4252552f7358SJed Brown   Level: intermediate
4253552f7358SJed Brown 
4254552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4255552f7358SJed Brown @*/
42567c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4257a6dfd86eSKarl Rupp {
4258552f7358SJed Brown   PetscInt       size = 0;
4259552f7358SJed Brown   PetscErrorCode ierr;
4260552f7358SJed Brown 
4261552f7358SJed Brown   PetscFunctionBegin;
4262552f7358SJed Brown   /* Should work without recalculating size */
426369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4264c9fdaa05SMatthew G. Knepley   *values = NULL;
4265552f7358SJed Brown   PetscFunctionReturn(0);
4266552f7358SJed Brown }
4267552f7358SJed Brown 
4268552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4269552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4270552f7358SJed Brown 
427197e99dd9SToby 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[])
4272552f7358SJed Brown {
4273552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4274552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4275552f7358SJed Brown   PetscScalar    *a;
4276552f7358SJed Brown   PetscInt        off, cind = 0, k;
4277552f7358SJed Brown   PetscErrorCode  ierr;
4278552f7358SJed Brown 
4279552f7358SJed Brown   PetscFunctionBegin;
4280552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4281552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4282552f7358SJed Brown   a    = &array[off];
4283552f7358SJed Brown   if (!cdof || setBC) {
428497e99dd9SToby Isaac     if (clperm) {
428597e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
428697e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4287552f7358SJed Brown     } else {
428897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
428997e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4290552f7358SJed Brown     }
4291552f7358SJed Brown   } else {
4292552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
429397e99dd9SToby Isaac     if (clperm) {
429497e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4295552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
429697e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4297552f7358SJed Brown         }
4298552f7358SJed Brown       } else {
4299552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4300552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
430197e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
430297e99dd9SToby Isaac         }
430397e99dd9SToby Isaac       }
430497e99dd9SToby Isaac     } else {
430597e99dd9SToby Isaac       if (perm) {
430697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
430797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
430897e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
430997e99dd9SToby Isaac         }
431097e99dd9SToby Isaac       } else {
431197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
431297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
431397e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
431497e99dd9SToby Isaac         }
4315552f7358SJed Brown       }
4316552f7358SJed Brown     }
4317552f7358SJed Brown   }
4318552f7358SJed Brown   PetscFunctionReturn(0);
4319552f7358SJed Brown }
4320552f7358SJed Brown 
432197e99dd9SToby 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[])
4322a5e93ea8SMatthew G. Knepley {
4323a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4324a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4325a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4326a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4327a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4328a5e93ea8SMatthew G. Knepley 
4329a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4330a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4331a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4332a5e93ea8SMatthew G. Knepley   a    = &array[off];
4333a5e93ea8SMatthew G. Knepley   if (cdof) {
4334a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
433597e99dd9SToby Isaac     if (clperm) {
433697e99dd9SToby Isaac       if (perm) {
4337a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4338a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
433997e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
434097e99dd9SToby Isaac             cind++;
4341a5e93ea8SMatthew G. Knepley           }
4342a5e93ea8SMatthew G. Knepley         }
4343a5e93ea8SMatthew G. Knepley       } else {
4344a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4345a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
434697e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
434797e99dd9SToby Isaac             cind++;
434897e99dd9SToby Isaac           }
434997e99dd9SToby Isaac         }
435097e99dd9SToby Isaac       }
435197e99dd9SToby Isaac     } else {
435297e99dd9SToby Isaac       if (perm) {
435397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
435497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
435597e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
435697e99dd9SToby Isaac             cind++;
435797e99dd9SToby Isaac           }
435897e99dd9SToby Isaac         }
435997e99dd9SToby Isaac       } else {
436097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
436197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
436297e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
436397e99dd9SToby Isaac             cind++;
436497e99dd9SToby Isaac           }
4365a5e93ea8SMatthew G. Knepley         }
4366a5e93ea8SMatthew G. Knepley       }
4367a5e93ea8SMatthew G. Knepley     }
4368a5e93ea8SMatthew G. Knepley   }
4369a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4370a5e93ea8SMatthew G. Knepley }
4371a5e93ea8SMatthew G. Knepley 
437297e99dd9SToby 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[])
4373a6dfd86eSKarl Rupp {
4374552f7358SJed Brown   PetscScalar    *a;
43751a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
43761a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
437797e99dd9SToby Isaac   PetscInt        cind = 0, b;
4378552f7358SJed Brown   PetscErrorCode  ierr;
4379552f7358SJed Brown 
4380552f7358SJed Brown   PetscFunctionBegin;
4381552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4382552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
43831a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43841a271a75SMatthew G. Knepley   a    = &array[foff];
4385552f7358SJed Brown   if (!fcdof || setBC) {
438697e99dd9SToby Isaac     if (clperm) {
438797e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
438897e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4389552f7358SJed Brown     } else {
439097e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
439197e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4392552f7358SJed Brown     }
4393552f7358SJed Brown   } else {
4394552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
439597e99dd9SToby Isaac     if (clperm) {
439697e99dd9SToby Isaac       if (perm) {
439797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
439897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
439997e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4400552f7358SJed Brown         }
4401552f7358SJed Brown       } else {
440297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
440397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
440497e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
440597e99dd9SToby Isaac         }
440697e99dd9SToby Isaac       }
440797e99dd9SToby Isaac     } else {
440897e99dd9SToby Isaac       if (perm) {
440997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
441097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
441197e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
441297e99dd9SToby Isaac         }
441397e99dd9SToby Isaac       } else {
441497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
441597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
441697e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4417552f7358SJed Brown         }
4418552f7358SJed Brown       }
4419552f7358SJed Brown     }
4420552f7358SJed Brown   }
44211a271a75SMatthew G. Knepley   *offset += fdof;
4422552f7358SJed Brown   PetscFunctionReturn(0);
4423552f7358SJed Brown }
4424552f7358SJed Brown 
4425ba322698SMatthew 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[])
4426a5e93ea8SMatthew G. Knepley {
4427a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
44281a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
44291a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4430ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4431ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4432a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4433a5e93ea8SMatthew G. Knepley 
4434a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4435a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4436a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
44371a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
44381a271a75SMatthew G. Knepley   a    = &array[foff];
4439a5e93ea8SMatthew G. Knepley   if (fcdof) {
4440ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4441a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
444297e99dd9SToby Isaac     if (clperm) {
444397e99dd9SToby Isaac       if (perm) {
4444ba322698SMatthew G. Knepley         if (comps) {
4445ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4446ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4447ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4448ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4449ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4450ba322698SMatthew G. Knepley           }
4451ba322698SMatthew G. Knepley         } else {
445297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
445397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
445497e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4455a5e93ea8SMatthew G. Knepley               ++cind;
4456a5e93ea8SMatthew G. Knepley             }
4457a5e93ea8SMatthew G. Knepley           }
4458ba322698SMatthew G. Knepley         }
4459ba322698SMatthew G. Knepley       } else {
4460ba322698SMatthew G. Knepley         if (comps) {
4461ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4462ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4463ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4464ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4465ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4466ba322698SMatthew G. Knepley           }
4467a5e93ea8SMatthew G. Knepley         } else {
446897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
446997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
447097e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
447197e99dd9SToby Isaac               ++cind;
447297e99dd9SToby Isaac             }
447397e99dd9SToby Isaac           }
447497e99dd9SToby Isaac         }
4475ba322698SMatthew G. Knepley       }
447697e99dd9SToby Isaac     } else {
447797e99dd9SToby Isaac       if (perm) {
4478ba322698SMatthew G. Knepley         if (comps) {
4479ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4480ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4481ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4482ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4483ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4484ba322698SMatthew G. Knepley           }
4485ba322698SMatthew G. Knepley         } else {
448697e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
448797e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
448897e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
448997e99dd9SToby Isaac               ++cind;
449097e99dd9SToby Isaac             }
449197e99dd9SToby Isaac           }
4492ba322698SMatthew G. Knepley         }
4493ba322698SMatthew G. Knepley       } else {
4494ba322698SMatthew G. Knepley         if (comps) {
4495ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4496ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4497ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4498ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4499ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4500ba322698SMatthew G. Knepley           }
450197e99dd9SToby Isaac         } else {
450297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
450397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
450497e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4505a5e93ea8SMatthew G. Knepley               ++cind;
4506a5e93ea8SMatthew G. Knepley             }
4507a5e93ea8SMatthew G. Knepley           }
4508a5e93ea8SMatthew G. Knepley         }
4509a5e93ea8SMatthew G. Knepley       }
4510a5e93ea8SMatthew G. Knepley     }
4511ba322698SMatthew G. Knepley   }
45121a271a75SMatthew G. Knepley   *offset += fdof;
4513a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4514a5e93ea8SMatthew G. Knepley }
4515a5e93ea8SMatthew G. Knepley 
45168f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4517a6dfd86eSKarl Rupp {
4518552f7358SJed Brown   PetscScalar    *array;
45191b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
45201b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4521552f7358SJed Brown   PetscErrorCode  ierr;
4522552f7358SJed Brown 
45231b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4524b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4525b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4526b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4527b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4528b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4529b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4530b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4531b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4532b6ebb6e6SMatthew G. Knepley 
4533b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4534b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4535b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4536b6ebb6e6SMatthew G. Knepley     {
4537b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4538b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4539b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4540b6ebb6e6SMatthew G. Knepley 
4541b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4542b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4543b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4544b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4545b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4546b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4547b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4548b6ebb6e6SMatthew G. Knepley           }
4549b6ebb6e6SMatthew G. Knepley         } else {
4550b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4551b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4552b6ebb6e6SMatthew G. Knepley           }
4553b6ebb6e6SMatthew G. Knepley         }
4554b6ebb6e6SMatthew G. Knepley       } else {
4555b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4556b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4557b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4558b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4559b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4560b6ebb6e6SMatthew G. Knepley           }
4561b6ebb6e6SMatthew G. Knepley         } else {
4562b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4563b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4564b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4565b6ebb6e6SMatthew G. Knepley           }
4566b6ebb6e6SMatthew G. Knepley         }
4567b6ebb6e6SMatthew G. Knepley       }
4568b6ebb6e6SMatthew G. Knepley     }
4569b6ebb6e6SMatthew G. Knepley   }
4570b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4571b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4572b6ebb6e6SMatthew G. Knepley }
45731b406b76SMatthew G. Knepley 
45741b406b76SMatthew G. Knepley /*@C
45751b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
45761b406b76SMatthew G. Knepley 
45771b406b76SMatthew G. Knepley   Not collective
45781b406b76SMatthew G. Knepley 
45791b406b76SMatthew G. Knepley   Input Parameters:
45801b406b76SMatthew G. Knepley + dm - The DM
45811b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
45821b406b76SMatthew G. Knepley . v - The local vector
4583eaf898f9SPatrick Sanan . point - The point in the DM
45841b406b76SMatthew G. Knepley . values - The array of values
458522c1ee49SMatthew 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,
458622c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
45871b406b76SMatthew G. Knepley 
45881b406b76SMatthew G. Knepley   Fortran Notes:
45891b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
45901b406b76SMatthew G. Knepley 
45911b406b76SMatthew G. Knepley   Level: intermediate
45921b406b76SMatthew G. Knepley 
45931b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
45941b406b76SMatthew G. Knepley @*/
45951b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
45961b406b76SMatthew G. Knepley {
45971b406b76SMatthew G. Knepley   PetscSection    clSection;
45981b406b76SMatthew G. Knepley   IS              clPoints;
45991b406b76SMatthew G. Knepley   PetscScalar    *array;
46001b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
460197e99dd9SToby Isaac   const PetscInt *clp, *clperm;
46021a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
46031b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
46041b406b76SMatthew G. Knepley 
46051a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
46061b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46071b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
46081a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46091a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
46101b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
46111b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
46121b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
46138f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
46141b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
46151b406b76SMatthew G. Knepley   }
46161a271a75SMatthew G. Knepley   /* Get points */
461797e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4618923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
46191a271a75SMatthew G. Knepley   /* Get array */
4620552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
46211a271a75SMatthew G. Knepley   /* Get values */
4622ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
462397e99dd9SToby Isaac     PetscInt offset = 0, f;
4624552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
462597e99dd9SToby Isaac       const PetscInt    **perms = NULL;
462697e99dd9SToby Isaac       const PetscScalar **flips = NULL;
462797e99dd9SToby Isaac 
462897e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4629552f7358SJed Brown       switch (mode) {
4630552f7358SJed Brown       case INSERT_VALUES:
463197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
463297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
463397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
463497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
463597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4636552f7358SJed Brown         } break;
4637552f7358SJed Brown       case INSERT_ALL_VALUES:
463897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
463997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
464097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
464197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
464297e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4643552f7358SJed Brown         } break;
4644a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
464597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
464697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
464797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
464897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4649ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4650a5e93ea8SMatthew G. Knepley         } break;
4651552f7358SJed Brown       case ADD_VALUES:
465297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
465397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
465497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
465597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
465697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4657552f7358SJed Brown         } break;
4658552f7358SJed Brown       case ADD_ALL_VALUES:
465997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
466097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
466197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
466297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
466397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4664552f7358SJed Brown         } break;
4665304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
466697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
466797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
466897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
466997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4670ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4671304ab55fSMatthew G. Knepley         } break;
4672552f7358SJed Brown       default:
4673f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4674552f7358SJed Brown       }
467597e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
46761a271a75SMatthew G. Knepley     }
4677552f7358SJed Brown   } else {
46781a271a75SMatthew G. Knepley     PetscInt dof, off;
467997e99dd9SToby Isaac     const PetscInt    **perms = NULL;
468097e99dd9SToby Isaac     const PetscScalar **flips = NULL;
46811a271a75SMatthew G. Knepley 
468297e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4683552f7358SJed Brown     switch (mode) {
4684552f7358SJed Brown     case INSERT_VALUES:
468597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
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;
468997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
469097e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4691552f7358SJed Brown       } break;
4692552f7358SJed Brown     case INSERT_ALL_VALUES:
469397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
469497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
469597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
469697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
469797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
469897e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4699552f7358SJed Brown       } break;
4700a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
470197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
470297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
470397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
470497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
470597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
470697e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4707a5e93ea8SMatthew G. Knepley       } break;
4708552f7358SJed Brown     case ADD_VALUES:
470997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
471097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
471197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
471297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
471397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
471497e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4715552f7358SJed Brown       } break;
4716552f7358SJed Brown     case ADD_ALL_VALUES:
471797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
471897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
471997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
472097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
472197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
472297e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4723552f7358SJed Brown       } break;
4724304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
472597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
472697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
472797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
472897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
472997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
473097e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4731304ab55fSMatthew G. Knepley       } break;
4732552f7358SJed Brown     default:
4733f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4734552f7358SJed Brown     }
473597e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4736552f7358SJed Brown   }
47371a271a75SMatthew G. Knepley   /* Cleanup points */
4738923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
47391a271a75SMatthew G. Knepley   /* Cleanup array */
4740552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4741552f7358SJed Brown   PetscFunctionReturn(0);
4742552f7358SJed Brown }
4743552f7358SJed Brown 
4744ba322698SMatthew 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)
4745e07394fbSMatthew G. Knepley {
4746e07394fbSMatthew G. Knepley   PetscSection      clSection;
4747e07394fbSMatthew G. Knepley   IS                clPoints;
4748e07394fbSMatthew G. Knepley   PetscScalar       *array;
4749e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4750b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4751e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
475297e99dd9SToby Isaac   PetscInt          offset = 0, f;
4753e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4754e07394fbSMatthew G. Knepley 
4755e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4756e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4757e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4758e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4759e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4760e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4761e07394fbSMatthew G. Knepley   /* Get points */
4762b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4763923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4764e07394fbSMatthew G. Knepley   /* Get array */
4765e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4766e07394fbSMatthew G. Knepley   /* Get values */
4767e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
476897e99dd9SToby Isaac     const PetscInt    **perms = NULL;
476997e99dd9SToby Isaac     const PetscScalar **flips = NULL;
477097e99dd9SToby Isaac 
4771e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4772e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4773e07394fbSMatthew G. Knepley         PetscInt fdof;
4774e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4775e07394fbSMatthew G. Knepley         offset += fdof;
4776e07394fbSMatthew G. Knepley       }
4777e07394fbSMatthew G. Knepley       continue;
4778e07394fbSMatthew G. Knepley     }
477997e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4780e07394fbSMatthew G. Knepley     switch (mode) {
4781e07394fbSMatthew G. Knepley     case INSERT_VALUES:
478297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
478397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
478497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
478597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4786b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4787e07394fbSMatthew G. Knepley       } break;
4788e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
478997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
479097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
479197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
479297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4793b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4794e07394fbSMatthew G. Knepley         } break;
4795e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
479697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
479797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
479897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
479997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4800ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
4801e07394fbSMatthew G. Knepley       } break;
4802e07394fbSMatthew G. Knepley     case ADD_VALUES:
480397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
480497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
480597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
480697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4807b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4808e07394fbSMatthew G. Knepley       } break;
4809e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
481097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
481197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
481297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
481397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4814b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4815e07394fbSMatthew G. Knepley       } break;
4816e07394fbSMatthew G. Knepley     default:
4817f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4818e07394fbSMatthew G. Knepley     }
481997e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4820e07394fbSMatthew G. Knepley   }
4821e07394fbSMatthew G. Knepley   /* Cleanup points */
4822923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4823e07394fbSMatthew G. Knepley   /* Cleanup array */
4824e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4825e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4826e07394fbSMatthew G. Knepley }
4827e07394fbSMatthew G. Knepley 
48287cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4829552f7358SJed Brown {
4830552f7358SJed Brown   PetscMPIInt    rank;
4831552f7358SJed Brown   PetscInt       i, j;
4832552f7358SJed Brown   PetscErrorCode ierr;
4833552f7358SJed Brown 
4834552f7358SJed Brown   PetscFunctionBegin;
483582f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4836eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
4837e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4838e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4839b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4840b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4841e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4842b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4843519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
48447eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4845552f7358SJed Brown #else
4846b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4847552f7358SJed Brown #endif
4848552f7358SJed Brown     }
484977a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4850552f7358SJed Brown   }
4851552f7358SJed Brown   PetscFunctionReturn(0);
4852552f7358SJed Brown }
4853552f7358SJed Brown 
4854552f7358SJed Brown /* . off - The global offset of this point */
48554acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4856a6dfd86eSKarl Rupp {
4857e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4858552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4859552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4860552f7358SJed Brown   PetscInt        cind = 0, k;
4861552f7358SJed Brown   PetscErrorCode  ierr;
4862552f7358SJed Brown 
4863552f7358SJed Brown   PetscFunctionBegin;
4864552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4865552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4866552f7358SJed Brown   if (!cdof || setBC) {
48674acb8e1eSToby Isaac     if (perm) {
48684acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4869552f7358SJed Brown     } else {
48704acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4871552f7358SJed Brown     }
4872552f7358SJed Brown   } else {
4873552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
48744acb8e1eSToby Isaac     if (perm) {
48754acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
48764acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
48774acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
48784acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
48794acb8e1eSToby Isaac           ++cind;
48804acb8e1eSToby Isaac         } else {
48814acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
48824acb8e1eSToby Isaac         }
48834acb8e1eSToby Isaac       }
48844acb8e1eSToby Isaac     } else {
4885552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4886552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4887552f7358SJed Brown           /* Insert check for returning constrained indices */
4888e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4889552f7358SJed Brown           ++cind;
4890552f7358SJed Brown         } else {
4891e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4892552f7358SJed Brown         }
4893552f7358SJed Brown       }
4894552f7358SJed Brown     }
4895552f7358SJed Brown   }
4896e6ccafaeSMatthew G Knepley   *loff += dof;
4897552f7358SJed Brown   PetscFunctionReturn(0);
4898552f7358SJed Brown }
4899552f7358SJed Brown 
4900552f7358SJed Brown /* . off - The global offset of this point */
49014acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4902a6dfd86eSKarl Rupp {
4903552f7358SJed Brown   PetscInt       numFields, foff, f;
4904552f7358SJed Brown   PetscErrorCode ierr;
4905552f7358SJed Brown 
4906552f7358SJed Brown   PetscFunctionBegin;
4907552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4908552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
49094acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4910552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
49114acb8e1eSToby Isaac     PetscInt        cind = 0, b;
49124acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4913552f7358SJed Brown 
4914552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4915552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4916552f7358SJed Brown     if (!cfdof || setBC) {
49174acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
49184acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4919552f7358SJed Brown     } else {
4920552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
49214acb8e1eSToby Isaac       if (perm) {
49224acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
49234acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
49244acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4925552f7358SJed Brown             ++cind;
4926552f7358SJed Brown           } else {
49274acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4928552f7358SJed Brown           }
4929552f7358SJed Brown         }
4930552f7358SJed Brown       } else {
49314acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
49324acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
49334acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4934552f7358SJed Brown             ++cind;
4935552f7358SJed Brown           } else {
49364acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4937552f7358SJed Brown           }
4938552f7358SJed Brown         }
4939552f7358SJed Brown       }
4940552f7358SJed Brown     }
4941a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4942552f7358SJed Brown     foffs[f] += fdof;
4943552f7358SJed Brown   }
4944552f7358SJed Brown   PetscFunctionReturn(0);
4945552f7358SJed Brown }
4946552f7358SJed Brown 
49474acb8e1eSToby 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)
4948d3d1a6afSToby Isaac {
4949d3d1a6afSToby Isaac   Mat             cMat;
4950d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4951d3d1a6afSToby Isaac   IS              aIS;
4952d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4953d3d1a6afSToby Isaac   const PetscInt  *anchors;
4954e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4955d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4956d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4957d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4958d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4959d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4960d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4961d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
49626ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4963d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4964d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4965d3d1a6afSToby Isaac 
4966d3d1a6afSToby Isaac   PetscFunctionBegin;
4967d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4968d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4969d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4970d3d1a6afSToby Isaac 
4971a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4972d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4973d3d1a6afSToby Isaac   if (aSec) {
4974d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4975d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4976d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4977d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4978d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4979d3d1a6afSToby Isaac      * into the global matrix anyway) */
4980d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4981d3d1a6afSToby Isaac       PetscInt b    = points[p];
49824b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4983d3d1a6afSToby Isaac 
49844b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
49854b2f2278SToby Isaac       if (!bSecDof) {
49864b2f2278SToby Isaac         continue;
49874b2f2278SToby Isaac       }
4988d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4989d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4990d3d1a6afSToby Isaac       }
4991d3d1a6afSToby Isaac       if (bDof) {
4992d3d1a6afSToby Isaac         /* this point is constrained */
4993d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4994d3d1a6afSToby Isaac         PetscInt bOff, q;
4995d3d1a6afSToby Isaac 
4996d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4997d3d1a6afSToby Isaac         newNumPoints  += bDof;
4998d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4999d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5000d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5001d3d1a6afSToby Isaac           PetscInt aDof;
5002d3d1a6afSToby Isaac 
5003d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5004d3d1a6afSToby Isaac           newNumIndices += aDof;
5005d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5006d3d1a6afSToby Isaac             PetscInt fDof;
5007d3d1a6afSToby Isaac 
5008d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5009d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5010d3d1a6afSToby Isaac           }
5011d3d1a6afSToby Isaac         }
5012d3d1a6afSToby Isaac       }
5013d3d1a6afSToby Isaac       else {
5014d3d1a6afSToby Isaac         /* this point is not constrained */
5015d3d1a6afSToby Isaac         newNumPoints++;
50164b2f2278SToby Isaac         newNumIndices += bSecDof;
5017d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5018d3d1a6afSToby Isaac           PetscInt fDof;
5019d3d1a6afSToby Isaac 
5020d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5021d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5022d3d1a6afSToby Isaac         }
5023d3d1a6afSToby Isaac       }
5024d3d1a6afSToby Isaac     }
5025d3d1a6afSToby Isaac   }
5026d3d1a6afSToby Isaac   if (!anyConstrained) {
502772b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
502872b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
502972b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
503072b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
503172b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5032d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5033d3d1a6afSToby Isaac   }
5034d3d1a6afSToby Isaac 
50356ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
50366ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
50376ecaa68aSToby Isaac 
5038f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5039d3d1a6afSToby Isaac 
50406ecaa68aSToby Isaac   if (!outPoints && !outValues) {
50416ecaa68aSToby Isaac     if (offsets) {
50426ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
50436ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
50446ecaa68aSToby Isaac       }
50456ecaa68aSToby Isaac     }
50466ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
50476ecaa68aSToby Isaac     PetscFunctionReturn(0);
50486ecaa68aSToby Isaac   }
50496ecaa68aSToby Isaac 
5050f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5051d3d1a6afSToby Isaac 
5052f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5053d3d1a6afSToby Isaac 
5054d3d1a6afSToby Isaac   /* workspaces */
5055d3d1a6afSToby Isaac   if (numFields) {
5056d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
505769291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
505869291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5059d3d1a6afSToby Isaac     }
5060d3d1a6afSToby Isaac   }
5061d3d1a6afSToby Isaac   else {
506269291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
506369291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5064d3d1a6afSToby Isaac   }
5065d3d1a6afSToby Isaac 
5066d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5067d3d1a6afSToby Isaac   if (numFields) {
50684b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
50694b2f2278SToby Isaac 
5070d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5071d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
50724b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5073d3d1a6afSToby Isaac 
50744b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
50754b2f2278SToby Isaac       if (!bSecDof) {
50764b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
50774b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
50784b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
50794b2f2278SToby Isaac         }
50804b2f2278SToby Isaac         continue;
50814b2f2278SToby Isaac       }
5082d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5083d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5084d3d1a6afSToby Isaac       }
5085d3d1a6afSToby Isaac       if (bDof) {
5086d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5087d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5088d3d1a6afSToby Isaac 
5089d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5090d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5091d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5092d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5093d3d1a6afSToby Isaac             PetscInt aFDof;
5094d3d1a6afSToby Isaac 
5095d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5096d3d1a6afSToby Isaac             allFDof += aFDof;
5097d3d1a6afSToby Isaac           }
5098d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5099d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5100d3d1a6afSToby Isaac         }
5101d3d1a6afSToby Isaac       }
5102d3d1a6afSToby Isaac       else {
5103d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5104d3d1a6afSToby Isaac           PetscInt fDof;
5105d3d1a6afSToby Isaac 
5106d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5107d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5108d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5109d3d1a6afSToby Isaac         }
5110d3d1a6afSToby Isaac       }
5111d3d1a6afSToby Isaac     }
51124b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
51134b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
51144b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5115d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5116d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5117d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5118d3d1a6afSToby Isaac       }
511919f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
512019f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
512169291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5122d3d1a6afSToby Isaac     }
5123d3d1a6afSToby Isaac   }
5124d3d1a6afSToby Isaac   else {
5125d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5126d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
51274b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5128d3d1a6afSToby Isaac 
51294b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
51304b2f2278SToby Isaac       if (!bSecDof) {
51314b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
51324b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
51334b2f2278SToby Isaac         continue;
51344b2f2278SToby Isaac       }
5135d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5136d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5137d3d1a6afSToby Isaac       }
5138d3d1a6afSToby Isaac       if (bDof) {
51394b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5140d3d1a6afSToby Isaac 
5141d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5142d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5143d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5144d3d1a6afSToby Isaac 
5145d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5146d3d1a6afSToby Isaac           allDof += aDof;
5147d3d1a6afSToby Isaac         }
5148d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
51494b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5150d3d1a6afSToby Isaac       }
5151d3d1a6afSToby Isaac       else {
51524b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5153d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5154d3d1a6afSToby Isaac       }
5155d3d1a6afSToby Isaac     }
5156d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5157d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5158d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5159d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5160d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5161d3d1a6afSToby Isaac     }
516269291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5163d3d1a6afSToby Isaac   }
5164d3d1a6afSToby Isaac 
51656ecaa68aSToby Isaac   /* output arrays */
516669291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
51676ecaa68aSToby Isaac 
5168d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5169d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5170d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
517169291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
517269291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5173d3d1a6afSToby Isaac   if (numFields) {
5174d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5175d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5176d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
51774b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5178d3d1a6afSToby Isaac 
51794b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
51804b2f2278SToby Isaac       if (!bSecDof) {
51814b2f2278SToby Isaac         continue;
51824b2f2278SToby Isaac       }
5183d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5184d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5185d3d1a6afSToby Isaac       }
5186d3d1a6afSToby Isaac       if (bDof) {
5187d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5188d3d1a6afSToby Isaac 
5189d3d1a6afSToby Isaac         fStart[0] = 0;
5190d3d1a6afSToby Isaac         fEnd[0]   = 0;
5191d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5192d3d1a6afSToby Isaac           PetscInt fDof;
5193d3d1a6afSToby Isaac 
5194d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5195d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5196d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5197d3d1a6afSToby Isaac         }
5198d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
51994acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
5200d3d1a6afSToby Isaac 
5201d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5202d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5203d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5204d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5205d3d1a6afSToby Isaac 
5206d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5207d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5208d3d1a6afSToby Isaac         }
5209d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5210d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5211d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5212d3d1a6afSToby Isaac 
5213d3d1a6afSToby 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 */
5214d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5215d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5216302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
52174acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
5218d3d1a6afSToby Isaac         }
5219d3d1a6afSToby Isaac         newP += bDof;
5220d3d1a6afSToby Isaac 
52216ecaa68aSToby Isaac         if (outValues) {
5222d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5223d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5224d3d1a6afSToby 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);
5225d3d1a6afSToby Isaac           }
5226d3d1a6afSToby Isaac         }
52276ecaa68aSToby Isaac       }
5228d3d1a6afSToby Isaac       else {
5229d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5230d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5231d3d1a6afSToby Isaac         newP++;
5232d3d1a6afSToby Isaac       }
5233d3d1a6afSToby Isaac     }
5234d3d1a6afSToby Isaac   } else {
5235d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5236d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5237d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
52384b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5239d3d1a6afSToby Isaac 
52404b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
52414b2f2278SToby Isaac       if (!bSecDof) {
52424b2f2278SToby Isaac         continue;
52434b2f2278SToby Isaac       }
5244d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5245d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5246d3d1a6afSToby Isaac       }
5247d3d1a6afSToby Isaac       if (bDof) {
5248d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5249d3d1a6afSToby Isaac 
5250d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
52514acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
5252d3d1a6afSToby Isaac 
5253d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5254d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5255d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5256d3d1a6afSToby Isaac 
5257d3d1a6afSToby 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 */
5258d3d1a6afSToby Isaac 
5259d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5260d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5261302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
52624acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
5263d3d1a6afSToby Isaac         }
5264d3d1a6afSToby Isaac         newP += bDof;
5265d3d1a6afSToby Isaac 
5266d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
52676ecaa68aSToby Isaac         if (outValues) {
5268d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5269d3d1a6afSToby Isaac         }
52706ecaa68aSToby Isaac       }
5271d3d1a6afSToby Isaac       else {
5272d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5273d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5274d3d1a6afSToby Isaac         newP++;
5275d3d1a6afSToby Isaac       }
5276d3d1a6afSToby Isaac     }
5277d3d1a6afSToby Isaac   }
5278d3d1a6afSToby Isaac 
52796ecaa68aSToby Isaac   if (outValues) {
528069291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5281d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5282d3d1a6afSToby Isaac     /* multiply constraints on the right */
5283d3d1a6afSToby Isaac     if (numFields) {
5284d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5285d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5286d3d1a6afSToby Isaac 
5287d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5288d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5289d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5290d3d1a6afSToby Isaac           PetscInt c, r, k;
5291d3d1a6afSToby Isaac           PetscInt dof;
5292d3d1a6afSToby Isaac 
5293d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
52944b2f2278SToby Isaac           if (!dof) {
52954b2f2278SToby Isaac             continue;
52964b2f2278SToby Isaac           }
5297d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5298d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5299d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5300d3d1a6afSToby Isaac 
5301d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5302d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5303d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
53044acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5305d3d1a6afSToby Isaac                 }
5306d3d1a6afSToby Isaac               }
5307d3d1a6afSToby Isaac             }
5308d3d1a6afSToby Isaac           }
5309d3d1a6afSToby Isaac           else {
5310d3d1a6afSToby Isaac             /* copy this column as is */
5311d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5312d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5313d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5314d3d1a6afSToby Isaac               }
5315d3d1a6afSToby Isaac             }
5316d3d1a6afSToby Isaac           }
5317d3d1a6afSToby Isaac           oldOff += dof;
5318d3d1a6afSToby Isaac         }
5319d3d1a6afSToby Isaac       }
5320d3d1a6afSToby Isaac     }
5321d3d1a6afSToby Isaac     else {
5322d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5323d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5324d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5325d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5326d3d1a6afSToby Isaac         PetscInt c, r, k;
5327d3d1a6afSToby Isaac         PetscInt dof;
5328d3d1a6afSToby Isaac 
5329d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
53304b2f2278SToby Isaac         if (!dof) {
53314b2f2278SToby Isaac           continue;
53324b2f2278SToby Isaac         }
5333d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5334d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5335d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5336d3d1a6afSToby Isaac 
5337d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5338d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5339d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5340d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5341d3d1a6afSToby Isaac               }
5342d3d1a6afSToby Isaac             }
5343d3d1a6afSToby Isaac           }
5344d3d1a6afSToby Isaac         }
5345d3d1a6afSToby Isaac         else {
5346d3d1a6afSToby Isaac           /* copy this column as is */
5347d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5348d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5349d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5350d3d1a6afSToby Isaac             }
5351d3d1a6afSToby Isaac           }
5352d3d1a6afSToby Isaac         }
5353d3d1a6afSToby Isaac         oldOff += dof;
5354d3d1a6afSToby Isaac       }
5355d3d1a6afSToby Isaac     }
5356d3d1a6afSToby Isaac 
53576ecaa68aSToby Isaac     if (multiplyLeft) {
535869291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5359d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5360d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5361d3d1a6afSToby Isaac       if (numFields) {
5362d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5363d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5364d3d1a6afSToby Isaac 
5365d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5366d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5367d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5368d3d1a6afSToby Isaac             PetscInt c, r, k;
5369d3d1a6afSToby Isaac             PetscInt dof;
5370d3d1a6afSToby Isaac 
5371d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5372d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5373d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5374d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5375d3d1a6afSToby Isaac 
5376d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5377d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5378d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5379d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5380d3d1a6afSToby Isaac                   }
5381d3d1a6afSToby Isaac                 }
5382d3d1a6afSToby Isaac               }
5383d3d1a6afSToby Isaac             }
5384d3d1a6afSToby Isaac             else {
5385d3d1a6afSToby Isaac               /* copy this row as is */
5386d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5387d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5388d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5389d3d1a6afSToby Isaac                 }
5390d3d1a6afSToby Isaac               }
5391d3d1a6afSToby Isaac             }
5392d3d1a6afSToby Isaac             oldOff += dof;
5393d3d1a6afSToby Isaac           }
5394d3d1a6afSToby Isaac         }
5395d3d1a6afSToby Isaac       }
5396d3d1a6afSToby Isaac       else {
5397d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5398d3d1a6afSToby Isaac 
5399d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5400d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5401d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5402d3d1a6afSToby Isaac           PetscInt c, r, k;
5403d3d1a6afSToby Isaac           PetscInt dof;
5404d3d1a6afSToby Isaac 
5405d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5406d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5407d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5408d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5409d3d1a6afSToby Isaac 
5410d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5411d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5412d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5413d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5414d3d1a6afSToby Isaac                 }
5415d3d1a6afSToby Isaac               }
5416d3d1a6afSToby Isaac             }
5417d3d1a6afSToby Isaac           }
5418d3d1a6afSToby Isaac           else {
5419d3d1a6afSToby Isaac             /* copy this row as is */
54209fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5421d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5422d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5423d3d1a6afSToby Isaac               }
5424d3d1a6afSToby Isaac             }
5425d3d1a6afSToby Isaac           }
5426d3d1a6afSToby Isaac           oldOff += dof;
5427d3d1a6afSToby Isaac         }
5428d3d1a6afSToby Isaac       }
5429d3d1a6afSToby Isaac 
543069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
54316ecaa68aSToby Isaac     }
54326ecaa68aSToby Isaac     else {
54336ecaa68aSToby Isaac       newValues = tmpValues;
54346ecaa68aSToby Isaac     }
54356ecaa68aSToby Isaac   }
54366ecaa68aSToby Isaac 
5437d3d1a6afSToby Isaac   /* clean up */
543869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
543969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
54406ecaa68aSToby Isaac 
5441d3d1a6afSToby Isaac   if (numFields) {
5442d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
544369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
544469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
544569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5446d3d1a6afSToby Isaac     }
5447d3d1a6afSToby Isaac   }
5448d3d1a6afSToby Isaac   else {
544969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
545069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
545169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5452d3d1a6afSToby Isaac   }
5453d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5454d3d1a6afSToby Isaac 
5455d3d1a6afSToby Isaac   /* output */
54566ecaa68aSToby Isaac   if (outPoints) {
5457d3d1a6afSToby Isaac     *outPoints = newPoints;
54586ecaa68aSToby Isaac   }
54596ecaa68aSToby Isaac   else {
546069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
54616ecaa68aSToby Isaac   }
546231620726SToby Isaac   if (outValues) {
5463d3d1a6afSToby Isaac     *outValues = newValues;
54646ecaa68aSToby Isaac   }
54656ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5466d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5467d3d1a6afSToby Isaac   }
5468d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5469d3d1a6afSToby Isaac }
5470d3d1a6afSToby Isaac 
54717cd05799SMatthew G. Knepley /*@C
5472a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
54737cd05799SMatthew G. Knepley 
54747cd05799SMatthew G. Knepley   Not collective
54757cd05799SMatthew G. Knepley 
54767cd05799SMatthew G. Knepley   Input Parameters:
54777cd05799SMatthew G. Knepley + dm - The DM
54787cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
54797cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
54807cd05799SMatthew G. Knepley - point - The mesh point
54817cd05799SMatthew G. Knepley 
54827cd05799SMatthew G. Knepley   Output parameters:
54837cd05799SMatthew G. Knepley + numIndices - The number of indices
54847cd05799SMatthew G. Knepley . indices - The indices
54857cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
54867cd05799SMatthew G. Knepley 
54877cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
54887cd05799SMatthew G. Knepley 
54897cd05799SMatthew G. Knepley   Level: advanced
54907cd05799SMatthew G. Knepley 
54917cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
54927cd05799SMatthew G. Knepley @*/
54936ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
54947773e69fSMatthew G. Knepley {
54957773e69fSMatthew G. Knepley   PetscSection    clSection;
54967773e69fSMatthew G. Knepley   IS              clPoints;
54977773e69fSMatthew G. Knepley   const PetscInt *clp;
54984acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
54997773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
55007773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
55017773e69fSMatthew G. Knepley   PetscInt        offsets[32];
55027773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
55037773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
55047773e69fSMatthew G. Knepley 
55057773e69fSMatthew G. Knepley   PetscFunctionBegin;
55067773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55077773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
55087773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
55097773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
55107773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
55117773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
55127773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
55137773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
55147773e69fSMatthew G. Knepley   /* Get points in closure */
5515923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55167773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
55177773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
55187773e69fSMatthew G. Knepley     PetscInt dof, fdof;
55197773e69fSMatthew G. Knepley 
55207773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
55217773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
55227773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
55237773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
55247773e69fSMatthew G. Knepley     }
55257773e69fSMatthew G. Knepley     Nind += dof;
55267773e69fSMatthew G. Knepley   }
55277773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
55288ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
55294acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
55304acb8e1eSToby Isaac   /* Get dual space symmetries */
55314acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
55324acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55334acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55344acb8e1eSToby Isaac   }
55357773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
55367773e69fSMatthew G. Knepley   {
55374acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
55387773e69fSMatthew G. Knepley     if (numPointsNew) {
55394acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
55404acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55414acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55424acb8e1eSToby Isaac       }
55434acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
55444acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
55454acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
55464acb8e1eSToby Isaac       }
5547923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55487773e69fSMatthew G. Knepley       numPoints = numPointsNew;
55497773e69fSMatthew G. Knepley       Nind      = NindNew;
55507773e69fSMatthew G. Knepley       points    = pointsNew;
55517773e69fSMatthew G. Knepley     }
55527773e69fSMatthew G. Knepley   }
55537773e69fSMatthew G. Knepley   /* Calculate indices */
555469291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
55557773e69fSMatthew G. Knepley   if (Nf) {
55566ecaa68aSToby Isaac     if (outOffsets) {
55576ecaa68aSToby Isaac       PetscInt f;
55586ecaa68aSToby Isaac 
555946bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
55606ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
55616ecaa68aSToby Isaac       }
55626ecaa68aSToby Isaac     }
55634acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
55644acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55654acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
55667773e69fSMatthew G. Knepley     }
55677773e69fSMatthew G. Knepley   } else {
55684acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
55694acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
55704acb8e1eSToby Isaac 
55714acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55724acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
55737773e69fSMatthew G. Knepley     }
55747773e69fSMatthew G. Knepley   }
55757773e69fSMatthew G. Knepley   /* Cleanup points */
55764acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
55774acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55784acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55794acb8e1eSToby Isaac   }
55807773e69fSMatthew G. Knepley   if (numPointsNew) {
558169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
55827773e69fSMatthew G. Knepley   } else {
5583923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55847773e69fSMatthew G. Knepley   }
55857773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
55867773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55877773e69fSMatthew G. Knepley }
55887773e69fSMatthew G. Knepley 
55897cd05799SMatthew G. Knepley /*@C
55907cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
55917cd05799SMatthew G. Knepley 
55927cd05799SMatthew G. Knepley   Not collective
55937cd05799SMatthew G. Knepley 
55947cd05799SMatthew G. Knepley   Input Parameters:
55957cd05799SMatthew G. Knepley + dm - The DM
55967cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
55977cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
55987cd05799SMatthew G. Knepley . point - The mesh point
55997cd05799SMatthew G. Knepley . numIndices - The number of indices
56007cd05799SMatthew G. Knepley . indices - The indices
56017cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
56027cd05799SMatthew G. Knepley 
56037cd05799SMatthew G. Knepley   Level: advanced
56047cd05799SMatthew G. Knepley 
56057cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
56067cd05799SMatthew G. Knepley @*/
560746bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
56087773e69fSMatthew G. Knepley {
56097773e69fSMatthew G. Knepley   PetscErrorCode ierr;
56107773e69fSMatthew G. Knepley 
56117773e69fSMatthew G. Knepley   PetscFunctionBegin;
56127773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56137773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
561469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
56157773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
56167773e69fSMatthew G. Knepley }
56177773e69fSMatthew G. Knepley 
56187f5d1fdeSMatthew G. Knepley /*@C
56197f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
56207f5d1fdeSMatthew G. Knepley 
56217f5d1fdeSMatthew G. Knepley   Not collective
56227f5d1fdeSMatthew G. Knepley 
56237f5d1fdeSMatthew G. Knepley   Input Parameters:
56247f5d1fdeSMatthew G. Knepley + dm - The DM
5625ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5626ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
56277f5d1fdeSMatthew G. Knepley . A - The matrix
5628eaf898f9SPatrick Sanan . point - The point in the DM
56297f5d1fdeSMatthew G. Knepley . values - The array of values
56307f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
56317f5d1fdeSMatthew G. Knepley 
56327f5d1fdeSMatthew G. Knepley   Fortran Notes:
56337f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
56347f5d1fdeSMatthew G. Knepley 
56357f5d1fdeSMatthew G. Knepley   Level: intermediate
56367f5d1fdeSMatthew G. Knepley 
56377f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
56387f5d1fdeSMatthew G. Knepley @*/
56397c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5640552f7358SJed Brown {
5641552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
56421b406b76SMatthew G. Knepley   PetscSection        clSection;
56431b406b76SMatthew G. Knepley   IS                  clPoints;
5644d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
56451b406b76SMatthew G. Knepley   const PetscInt     *clp;
5646552f7358SJed Brown   PetscInt           *indices;
5647552f7358SJed Brown   PetscInt            offsets[32];
56484acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
56494acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5650923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
56514acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5652d3d1a6afSToby Isaac   PetscScalar        *newValues;
5653552f7358SJed Brown   PetscErrorCode      ierr;
5654552f7358SJed Brown 
5655552f7358SJed Brown   PetscFunctionBegin;
5656552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5657ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
56583dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5659ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
56603dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
56613dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5662552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
566382f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5664552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5665923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5666552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5667552f7358SJed Brown     PetscInt fdof;
5668552f7358SJed Brown 
5669552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5670552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5671552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5672552f7358SJed Brown       offsets[f+1] += fdof;
5673552f7358SJed Brown     }
5674552f7358SJed Brown     numIndices += dof;
5675552f7358SJed Brown   }
56760d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
56770d644c17SKarl Rupp 
56788ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
56794acb8e1eSToby Isaac   /* Get symmetries */
56804acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
56814acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56824acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56834acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
56844acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
56854acb8e1eSToby Isaac 
56864acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
56874acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
56884acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
56894acb8e1eSToby Isaac 
56904acb8e1eSToby Isaac         if (!numFields) {
56914acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
56924acb8e1eSToby Isaac         } else {
56934acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
56944acb8e1eSToby Isaac         }
56954acb8e1eSToby Isaac         if (flip) {
56964acb8e1eSToby Isaac           PetscInt i, j, k;
56974acb8e1eSToby Isaac 
56984acb8e1eSToby Isaac           if (!valCopy) {
569969291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
57004acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
57014acb8e1eSToby Isaac             values = valCopy;
57024acb8e1eSToby Isaac           }
57034acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5704775f7be2SToby Isaac             PetscScalar fval = flip[i];
57054acb8e1eSToby Isaac 
57064acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
57074acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
57084acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
57094acb8e1eSToby Isaac             }
57104acb8e1eSToby Isaac           }
57114acb8e1eSToby Isaac         }
57124acb8e1eSToby Isaac         foffset += fdof;
57134acb8e1eSToby Isaac       }
57144acb8e1eSToby Isaac     }
57154acb8e1eSToby Isaac   }
57164acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5717d3d1a6afSToby Isaac   if (newNumPoints) {
57184acb8e1eSToby Isaac     if (valCopy) {
571969291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
57204acb8e1eSToby Isaac     }
57214acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
57224acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57234acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57244acb8e1eSToby Isaac     }
57254acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
57264acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
57274acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
57284acb8e1eSToby Isaac     }
5729923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5730d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5731d3d1a6afSToby Isaac     numIndices = newNumIndices;
5732d3d1a6afSToby Isaac     points     = newPoints;
5733d3d1a6afSToby Isaac     values     = newValues;
5734d3d1a6afSToby Isaac   }
573569291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5736552f7358SJed Brown   if (numFields) {
57374acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
57384acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
57394acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5740552f7358SJed Brown     }
5741552f7358SJed Brown   } else {
57424acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
57434acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
57444acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
57454acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5746552f7358SJed Brown     }
5747552f7358SJed Brown   }
5748b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5749552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5750ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5751ab1d0545SMatthew G. Knepley     PetscInt i;
5752ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
57538ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5754ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5755ab1d0545SMatthew G. Knepley   }
5756552f7358SJed Brown   if (ierr) {
5757552f7358SJed Brown     PetscMPIInt    rank;
5758552f7358SJed Brown     PetscErrorCode ierr2;
5759552f7358SJed Brown 
576082f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5761e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5762b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
576369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
5764552f7358SJed Brown     CHKERRQ(ierr);
5765552f7358SJed Brown   }
57664acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
57674acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57684acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57694acb8e1eSToby Isaac   }
5770d3d1a6afSToby Isaac   if (newNumPoints) {
577169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
577269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
5773d3d1a6afSToby Isaac   }
5774d3d1a6afSToby Isaac   else {
57754acb8e1eSToby Isaac     if (valCopy) {
577669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
57774acb8e1eSToby Isaac     }
5778923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5779d3d1a6afSToby Isaac   }
578069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5781552f7358SJed Brown   PetscFunctionReturn(0);
5782552f7358SJed Brown }
5783552f7358SJed Brown 
5784de41b84cSMatthew 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)
5785de41b84cSMatthew G. Knepley {
5786de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5787de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5788de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5789de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5790de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5791de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
57924ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5793de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5794de41b84cSMatthew G. Knepley 
5795de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5796de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5797de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5798de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5799de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5800de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5801de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5802de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5803de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5804de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5805de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5806de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5807de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5808de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5809de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5810de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5811de41b84cSMatthew G. Knepley   /* Column indices */
5812de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
58134ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5814de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5815de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5816de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5817de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5818de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5819de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5820de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5821de41b84cSMatthew G. Knepley       ++q;
5822de41b84cSMatthew G. Knepley     }
5823de41b84cSMatthew G. Knepley   }
5824de41b84cSMatthew G. Knepley   numCPoints = q;
5825de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5826de41b84cSMatthew G. Knepley     PetscInt fdof;
5827de41b84cSMatthew G. Knepley 
5828de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
58294ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5830de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5831de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5832de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5833de41b84cSMatthew G. Knepley     }
5834de41b84cSMatthew G. Knepley     numCIndices += dof;
5835de41b84cSMatthew G. Knepley   }
5836de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5837de41b84cSMatthew G. Knepley   /* Row indices */
5838de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5839de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
584069291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5841de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5842de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5843de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5844de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5845de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5846de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5847de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
58484ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
58494ca5e9f5SMatthew G. Knepley         if (!dof) continue;
58504ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
58514ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5852de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5853de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5854de41b84cSMatthew G. Knepley         ++q;
5855de41b84cSMatthew G. Knepley       }
5856de41b84cSMatthew G. Knepley     }
5857de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5858de41b84cSMatthew G. Knepley   }
5859de41b84cSMatthew G. Knepley   numFPoints = q;
5860de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5861de41b84cSMatthew G. Knepley     PetscInt fdof;
5862de41b84cSMatthew G. Knepley 
5863de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
58644ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5865de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5866de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5867de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5868de41b84cSMatthew G. Knepley     }
5869de41b84cSMatthew G. Knepley     numFIndices += dof;
5870de41b84cSMatthew G. Knepley   }
5871de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5872de41b84cSMatthew G. Knepley 
58738ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
58748ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
587569291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
587669291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5877de41b84cSMatthew G. Knepley   if (numFields) {
58784acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
58794acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
58804acb8e1eSToby Isaac 
58814acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58824acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58834acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5884de41b84cSMatthew G. Knepley     }
58854acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
58864acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
58874acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
58884acb8e1eSToby Isaac     }
58894acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
58904acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
58914acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
58924acb8e1eSToby Isaac     }
58934acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58944acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58954acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5896de41b84cSMatthew G. Knepley     }
5897de41b84cSMatthew G. Knepley   } else {
58984acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
58994acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
59004acb8e1eSToby Isaac 
59014acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59024acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
59034acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
59044acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
59054acb8e1eSToby Isaac 
59064acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
59074acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5908de41b84cSMatthew G. Knepley     }
59094acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
59104acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
59114acb8e1eSToby Isaac 
59124acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
59134acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5914de41b84cSMatthew G. Knepley     }
59154acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59164acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5917de41b84cSMatthew G. Knepley   }
5918de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
59194acb8e1eSToby Isaac   /* TODO: flips */
5920de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5921de41b84cSMatthew G. Knepley   if (ierr) {
5922de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5923de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5924de41b84cSMatthew G. Knepley 
5925de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5926e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5927de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
592869291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
592969291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
5930de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5931de41b84cSMatthew G. Knepley   }
593269291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5933de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
593469291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
593569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5936de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5937de41b84cSMatthew G. Knepley }
5938de41b84cSMatthew G. Knepley 
59397c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
59407c927364SMatthew G. Knepley {
59417c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
59427c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
59437c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
59447c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
59457c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
59467c927364SMatthew G. Knepley   PetscErrorCode ierr;
59477c927364SMatthew G. Knepley 
59487c927364SMatthew G. Knepley   PetscFunctionBegin;
59497c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
59507c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
59517c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
59527c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
59537c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
59547c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
59557c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
59567c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
59577c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
59587c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
59597c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
59607c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
59617c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
59627c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
59637c927364SMatthew G. Knepley   /* Column indices */
59647c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
59657c927364SMatthew G. Knepley   maxFPoints = numCPoints;
59667c927364SMatthew G. Knepley   /* Compress out points not in the section */
59677c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
59687c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
59697c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
59707c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
59717c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
59727c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
59737c927364SMatthew G. Knepley       ++q;
59747c927364SMatthew G. Knepley     }
59757c927364SMatthew G. Knepley   }
59767c927364SMatthew G. Knepley   numCPoints = q;
59777c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
59787c927364SMatthew G. Knepley     PetscInt fdof;
59797c927364SMatthew G. Knepley 
59807c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
59817c927364SMatthew G. Knepley     if (!dof) continue;
59827c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59837c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
59847c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
59857c927364SMatthew G. Knepley     }
59867c927364SMatthew G. Knepley     numCIndices += dof;
59877c927364SMatthew G. Knepley   }
59887c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
59897c927364SMatthew G. Knepley   /* Row indices */
59907c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
59917c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
599269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
59937c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
59947c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
59957c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59967c927364SMatthew G. Knepley     /* Compress out points not in the section */
59977c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
59987c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
59997c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
60007c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
60017c927364SMatthew G. Knepley         if (!dof) continue;
60027c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
60037c927364SMatthew G. Knepley         if (s < q) continue;
60047c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
60057c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
60067c927364SMatthew G. Knepley         ++q;
60077c927364SMatthew G. Knepley       }
60087c927364SMatthew G. Knepley     }
60097c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
60107c927364SMatthew G. Knepley   }
60117c927364SMatthew G. Knepley   numFPoints = q;
60127c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
60137c927364SMatthew G. Knepley     PetscInt fdof;
60147c927364SMatthew G. Knepley 
60157c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
60167c927364SMatthew G. Knepley     if (!dof) continue;
60177c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
60187c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
60197c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
60207c927364SMatthew G. Knepley     }
60217c927364SMatthew G. Knepley     numFIndices += dof;
60227c927364SMatthew G. Knepley   }
60237c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
60247c927364SMatthew G. Knepley 
60258ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
60268ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
60277c927364SMatthew G. Knepley   if (numFields) {
60284acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
60294acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
60304acb8e1eSToby Isaac 
60314acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
60324acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
60334acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
60347c927364SMatthew G. Knepley     }
60354acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
60364acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
60374acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
60384acb8e1eSToby Isaac     }
60394acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
60404acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
60414acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
60424acb8e1eSToby Isaac     }
60434acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
60444acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
60454acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
60467c927364SMatthew G. Knepley     }
60477c927364SMatthew G. Knepley   } else {
60484acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
60494acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
60504acb8e1eSToby Isaac 
60514acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
60524acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
60534acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
60544acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
60554acb8e1eSToby Isaac 
60564acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
60574acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
60587c927364SMatthew G. Knepley     }
60594acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
60604acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
60614acb8e1eSToby Isaac 
60624acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
60634acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
60647c927364SMatthew G. Knepley     }
60654acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
60664acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
60677c927364SMatthew G. Knepley   }
606869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
60697c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
60707c927364SMatthew G. Knepley   PetscFunctionReturn(0);
60717c927364SMatthew G. Knepley }
60727c927364SMatthew G. Knepley 
6073f96281f8SMatthew G. Knepley /*@
6074f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6075f96281f8SMatthew G. Knepley 
6076f96281f8SMatthew G. Knepley   Input Parameter:
6077f96281f8SMatthew G. Knepley . dm - The DMPlex object
6078f96281f8SMatthew G. Knepley 
6079f96281f8SMatthew G. Knepley   Output Parameters:
6080f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6081dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6082dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6083dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6084f96281f8SMatthew G. Knepley 
6085f96281f8SMatthew G. Knepley   Level: developer
6086f96281f8SMatthew G. Knepley 
6087dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6088f96281f8SMatthew G. Knepley @*/
6089770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6090552f7358SJed Brown {
6091552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6092770b213bSMatthew G Knepley   PetscInt       dim;
6093770b213bSMatthew G Knepley   PetscErrorCode ierr;
6094552f7358SJed Brown 
6095552f7358SJed Brown   PetscFunctionBegin;
6096552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6097c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
60987d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6099770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
61007d5acc75SStefano Zampini   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6101770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6102770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6103552f7358SJed Brown   PetscFunctionReturn(0);
6104552f7358SJed Brown }
6105552f7358SJed Brown 
6106aeadca18SToby Isaac static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
61074a3e9fdbSToby Isaac {
61084a3e9fdbSToby Isaac   IS             is, his;
61097d5acc75SStefano Zampini   PetscInt       first = 0, stride;
61104a3e9fdbSToby Isaac   PetscBool      isStride;
61114a3e9fdbSToby Isaac   PetscErrorCode ierr;
61124a3e9fdbSToby Isaac 
61134a3e9fdbSToby Isaac   PetscFunctionBegin;
61144a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel, d, &is);CHKERRQ(ierr);
61154a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);CHKERRQ(ierr);
61164a3e9fdbSToby Isaac   if (isStride) {
61174a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(is, &first, &stride);CHKERRQ(ierr);
61184a3e9fdbSToby Isaac   }
61197d5acc75SStefano 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);
61204a3e9fdbSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);CHKERRQ(ierr);
6121aeadca18SToby Isaac   ierr = DMLabelSetStratumIS(dimLabel, d, his);CHKERRQ(ierr);
61224a3e9fdbSToby Isaac   ierr = ISDestroy(&his);CHKERRQ(ierr);
61234a3e9fdbSToby Isaac   ierr = ISDestroy(&is);CHKERRQ(ierr);
61244a3e9fdbSToby Isaac   PetscFunctionReturn(0);
61254a3e9fdbSToby Isaac }
61264a3e9fdbSToby Isaac 
6127dc5a3409SMatthew G. Knepley /*@
6128dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6129dc5a3409SMatthew G. Knepley 
6130dc5a3409SMatthew G. Knepley   Input Parameters:
6131dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
6132dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6133dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6134dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6135dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6136dc5a3409SMatthew G. Knepley 
6137dc5a3409SMatthew G. Knepley   Level: developer
6138dc5a3409SMatthew G. Knepley 
6139dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6140dc5a3409SMatthew G. Knepley @*/
6141770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6142552f7358SJed Brown {
6143552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6144770b213bSMatthew G Knepley   PetscInt       dim;
6145770b213bSMatthew G Knepley   PetscErrorCode ierr;
6146552f7358SJed Brown 
6147552f7358SJed Brown   PetscFunctionBegin;
6148552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6149c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
61507d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6151770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
61527d5acc75SStefano Zampini   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6153770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6154770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6155552f7358SJed Brown   PetscFunctionReturn(0);
6156552f7358SJed Brown }
6157552f7358SJed Brown 
61587cd05799SMatthew G. Knepley /*@C
61597cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
61607cd05799SMatthew G. Knepley 
61617cd05799SMatthew G. Knepley   Input Parameter:
61627cd05799SMatthew G. Knepley . dm   - The DMPlex object
61637cd05799SMatthew G. Knepley 
61647cd05799SMatthew G. Knepley   Output Parameter:
61657cd05799SMatthew G. Knepley . cellHeight - The height of a cell
61667cd05799SMatthew G. Knepley 
61677cd05799SMatthew G. Knepley   Level: developer
61687cd05799SMatthew G. Knepley 
61697cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
61707cd05799SMatthew G. Knepley @*/
6171552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6172552f7358SJed Brown {
6173552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6174552f7358SJed Brown 
6175552f7358SJed Brown   PetscFunctionBegin;
6176552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6177552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6178552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6179552f7358SJed Brown   PetscFunctionReturn(0);
6180552f7358SJed Brown }
6181552f7358SJed Brown 
61827cd05799SMatthew G. Knepley /*@C
61837cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
61847cd05799SMatthew G. Knepley 
61857cd05799SMatthew G. Knepley   Input Parameters:
61867cd05799SMatthew G. Knepley + dm   - The DMPlex object
61877cd05799SMatthew G. Knepley - cellHeight - The height of a cell
61887cd05799SMatthew G. Knepley 
61897cd05799SMatthew G. Knepley   Level: developer
61907cd05799SMatthew G. Knepley 
61917cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
61927cd05799SMatthew G. Knepley @*/
6193552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6194552f7358SJed Brown {
6195552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6196552f7358SJed Brown 
6197552f7358SJed Brown   PetscFunctionBegin;
6198552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6199552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6200552f7358SJed Brown   PetscFunctionReturn(0);
6201552f7358SJed Brown }
6202552f7358SJed Brown 
6203552f7358SJed Brown /* We can easily have a form that takes an IS instead */
6204ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6205552f7358SJed Brown {
6206552f7358SJed Brown   PetscSection   section, globalSection;
6207552f7358SJed Brown   PetscInt      *numbers, p;
6208552f7358SJed Brown   PetscErrorCode ierr;
6209552f7358SJed Brown 
6210552f7358SJed Brown   PetscFunctionBegin;
621182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6212552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6213552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6214552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6215552f7358SJed Brown   }
6216552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
621715b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6218854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6219552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6220552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6221ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6222ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6223552f7358SJed Brown   }
622482f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6225ef48cebcSMatthew G. Knepley   if (globalSize) {
6226ef48cebcSMatthew G. Knepley     PetscLayout layout;
6227ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6228ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6229ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6230ef48cebcSMatthew G. Knepley   }
6231552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6232552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6233552f7358SJed Brown   PetscFunctionReturn(0);
6234552f7358SJed Brown }
6235552f7358SJed Brown 
623681ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6237552f7358SJed Brown {
6238552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6239552f7358SJed Brown   PetscErrorCode ierr;
6240552f7358SJed Brown 
6241552f7358SJed Brown   PetscFunctionBegin;
6242552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6243552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
62440298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
624581ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
624681ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
624781ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6248552f7358SJed Brown }
624981ed3555SMatthew G. Knepley 
62507cd05799SMatthew G. Knepley /*@C
62517cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
62527cd05799SMatthew G. Knepley 
62537cd05799SMatthew G. Knepley   Input Parameter:
62547cd05799SMatthew G. Knepley . dm   - The DMPlex object
62557cd05799SMatthew G. Knepley 
62567cd05799SMatthew G. Knepley   Output Parameter:
62577cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
62587cd05799SMatthew G. Knepley 
62597cd05799SMatthew G. Knepley   Level: developer
62607cd05799SMatthew G. Knepley 
62617cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
62627cd05799SMatthew G. Knepley @*/
626381ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
626481ed3555SMatthew G. Knepley {
626581ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
626681ed3555SMatthew G. Knepley   PetscErrorCode ierr;
626781ed3555SMatthew G. Knepley 
626881ed3555SMatthew G. Knepley   PetscFunctionBegin;
626981ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
627081ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6271552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6272552f7358SJed Brown   PetscFunctionReturn(0);
6273552f7358SJed Brown }
6274552f7358SJed Brown 
627581ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
627681ed3555SMatthew G. Knepley {
627781ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
627881ed3555SMatthew G. Knepley   PetscErrorCode ierr;
627981ed3555SMatthew G. Knepley 
628081ed3555SMatthew G. Knepley   PetscFunctionBegin;
628181ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
628281ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
628381ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
628481ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
628581ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
628681ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
628781ed3555SMatthew G. Knepley }
628881ed3555SMatthew G. Knepley 
62897cd05799SMatthew G. Knepley /*@C
62907cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
62917cd05799SMatthew G. Knepley 
62927cd05799SMatthew G. Knepley   Input Parameter:
62937cd05799SMatthew G. Knepley . dm   - The DMPlex object
62947cd05799SMatthew G. Knepley 
62957cd05799SMatthew G. Knepley   Output Parameter:
62967cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
62977cd05799SMatthew G. Knepley 
62987cd05799SMatthew G. Knepley   Level: developer
62997cd05799SMatthew G. Knepley 
63007cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
63017cd05799SMatthew G. Knepley @*/
6302552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6303552f7358SJed Brown {
6304552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6305552f7358SJed Brown   PetscErrorCode ierr;
6306552f7358SJed Brown 
6307552f7358SJed Brown   PetscFunctionBegin;
6308552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
630981ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6310552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6311552f7358SJed Brown   PetscFunctionReturn(0);
6312552f7358SJed Brown }
6313552f7358SJed Brown 
63147cd05799SMatthew G. Knepley /*@C
63157cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
63167cd05799SMatthew G. Knepley 
63177cd05799SMatthew G. Knepley   Input Parameter:
63187cd05799SMatthew G. Knepley . dm   - The DMPlex object
63197cd05799SMatthew G. Knepley 
63207cd05799SMatthew G. Knepley   Output Parameter:
63217cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
63227cd05799SMatthew G. Knepley 
63237cd05799SMatthew G. Knepley   Level: developer
63247cd05799SMatthew G. Knepley 
63257cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
63267cd05799SMatthew G. Knepley @*/
6327ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6328ef48cebcSMatthew G. Knepley {
6329ef48cebcSMatthew G. Knepley   IS             nums[4];
6330ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
6331ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6332ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6333ef48cebcSMatthew G. Knepley 
6334ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6335ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6336ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
63378abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
63388abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6339ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
6340ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
6341ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6342ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6343ef48cebcSMatthew G. Knepley 
6344ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
6345ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6346ef48cebcSMatthew G. Knepley     shift += gsize;
6347ef48cebcSMatthew G. Knepley   }
6348302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6349ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6350ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6351ef48cebcSMatthew G. Knepley }
6352ef48cebcSMatthew G. Knepley 
635308a22f4bSMatthew G. Knepley 
635408a22f4bSMatthew G. Knepley /*@
635508a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
635608a22f4bSMatthew G. Knepley 
635708a22f4bSMatthew G. Knepley   Input Parameter:
635808a22f4bSMatthew G. Knepley . dm - The DMPlex object
635908a22f4bSMatthew G. Knepley 
636008a22f4bSMatthew G. Knepley   Output Parameter:
636108a22f4bSMatthew G. Knepley . ranks - The rank field
636208a22f4bSMatthew G. Knepley 
636308a22f4bSMatthew G. Knepley   Options Database Keys:
636408a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
636508a22f4bSMatthew G. Knepley 
636608a22f4bSMatthew G. Knepley   Level: intermediate
636708a22f4bSMatthew G. Knepley 
636808a22f4bSMatthew G. Knepley .seealso: DMView()
636908a22f4bSMatthew G. Knepley @*/
637008a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
637108a22f4bSMatthew G. Knepley {
637208a22f4bSMatthew G. Knepley   DM             rdm;
637308a22f4bSMatthew G. Knepley   PetscDS        prob;
637408a22f4bSMatthew G. Knepley   PetscFE        fe;
637508a22f4bSMatthew G. Knepley   PetscScalar   *r;
637608a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
637708a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
637808a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
637908a22f4bSMatthew G. Knepley 
638008a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
638108a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
638208a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
638308a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
6384e8d98e54SMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, NULL, -1, &fe);CHKERRQ(ierr);
638508a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
638608a22f4bSMatthew G. Knepley   ierr = DMGetDS(rdm, &prob);CHKERRQ(ierr);
638708a22f4bSMatthew G. Knepley   ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe);CHKERRQ(ierr);
638808a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
638908a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
639008a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
639108a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
639208a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
639308a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
639408a22f4bSMatthew G. Knepley     PetscScalar *lr;
639508a22f4bSMatthew G. Knepley 
639608a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
639708a22f4bSMatthew G. Knepley     *lr = rank;
639808a22f4bSMatthew G. Knepley   }
639908a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
640008a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
640108a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
640208a22f4bSMatthew G. Knepley }
640308a22f4bSMatthew G. Knepley 
6404ca8062c8SMatthew G. Knepley /*@
6405ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6406ca8062c8SMatthew G. Knepley 
640769916449SMatthew G. Knepley   Input Parameter:
640869916449SMatthew G. Knepley . dm - The DMPlex object
6409ca8062c8SMatthew G. Knepley 
6410ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6411ca8062c8SMatthew G. Knepley 
6412ca8062c8SMatthew G. Knepley   Level: developer
6413ca8062c8SMatthew G. Knepley 
64147d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6415ca8062c8SMatthew G. Knepley @*/
6416ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6417ca8062c8SMatthew G. Knepley {
6418ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6419ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6420ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
6421ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
6422ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6423ca8062c8SMatthew G. Knepley 
6424ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6425ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6426ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6427ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6428ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6429ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6430ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6431ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6432ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6433ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
643442e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
643542e66dfaSMatthew G. Knepley       PetscInt  d;
643642e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
643742e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
643842e66dfaSMatthew G. Knepley       }
6439ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6440ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6441ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6442ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6443ca8062c8SMatthew G. Knepley       }
644442e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
64458ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6446ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
64478ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6448ca8062c8SMatthew G. Knepley         }
6449302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64508ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6451ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
64528ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6453ca8062c8SMatthew G. Knepley         }
6454302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64558ccfff9cSToby 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]);
64568ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6457ca8062c8SMatthew G. Knepley       }
645842e66dfaSMatthew G. Knepley     }
6459ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6460ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6461ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6462ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6463ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6464ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6465ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6466ca8062c8SMatthew G. Knepley       }
6467ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
64688ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6469ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
64708ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6471ca8062c8SMatthew G. Knepley         }
6472302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64738ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6474ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
64758ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6476ca8062c8SMatthew G. Knepley         }
6477302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64788ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6479ca8062c8SMatthew G. Knepley       }
6480ca8062c8SMatthew G. Knepley     }
6481ca8062c8SMatthew G. Knepley   }
6482ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6483ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
64848ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6485ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6486ca8062c8SMatthew G. Knepley }
6487ca8062c8SMatthew G. Knepley 
6488ca8062c8SMatthew G. Knepley /*@
6489ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6490ca8062c8SMatthew G. Knepley 
6491ca8062c8SMatthew G. Knepley   Input Parameters:
6492ca8062c8SMatthew G. Knepley + dm - The DMPlex object
649358723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
649458723a97SMatthew G. Knepley - cellHeight - Normally 0
6495ca8062c8SMatthew G. Knepley 
6496ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6497ca8062c8SMatthew G. Knepley 
6498ca8062c8SMatthew G. Knepley   Level: developer
6499ca8062c8SMatthew G. Knepley 
65007d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckFaces()
6501ca8062c8SMatthew G. Knepley @*/
650258723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6503ca8062c8SMatthew G. Knepley {
650442363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6505ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6506ca8062c8SMatthew G. Knepley 
6507ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6508ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6509c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6510ca8062c8SMatthew G. Knepley   switch (dim) {
651142363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
651242363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
651342363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6514ca8062c8SMatthew G. Knepley   default:
65158ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6516ca8062c8SMatthew G. Knepley   }
651758723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
651858723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6519ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6520ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6521ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
652258723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
652358723a97SMatthew G. Knepley 
652458723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
652558723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
652658723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
652758723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
652858723a97SMatthew G. Knepley     }
652958723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65308ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6531ca8062c8SMatthew G. Knepley   }
653242363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
653342363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
653442363296SMatthew G. Knepley 
653542363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
653642363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
653742363296SMatthew G. Knepley       const PetscInt p = closure[cl];
653842363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
653942363296SMatthew G. Knepley     }
654042363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65418ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
654242363296SMatthew G. Knepley   }
6543ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6544ca8062c8SMatthew G. Knepley }
65459bf0dad6SMatthew G. Knepley 
65469bf0dad6SMatthew G. Knepley /*@
65479bf0dad6SMatthew 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
65489bf0dad6SMatthew G. Knepley 
65499bf0dad6SMatthew G. Knepley   Input Parameters:
65509bf0dad6SMatthew G. Knepley + dm - The DMPlex object
65519bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
65529bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
65539bf0dad6SMatthew G. Knepley 
65549bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
65559bf0dad6SMatthew G. Knepley 
65569bf0dad6SMatthew G. Knepley   Level: developer
65579bf0dad6SMatthew G. Knepley 
65587d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton()
65599bf0dad6SMatthew G. Knepley @*/
65609bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
65619bf0dad6SMatthew G. Knepley {
65623554e41dSMatthew G. Knepley   PetscInt       pMax[4];
65633554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
65649bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
65659bf0dad6SMatthew G. Knepley 
65669bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
65679bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6568c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
65699bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
657025abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
65713554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
65723554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
65733554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
65749bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
65759bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
65769bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
65779bf0dad6SMatthew G. Knepley 
657825abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
65799bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
65809bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
65819bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
65829bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65839bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
65849bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
65859bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
65869bf0dad6SMatthew G. Knepley       }
65873554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
65888ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
65899bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
65909bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
65919bf0dad6SMatthew G. Knepley 
65929bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
65939bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
65949bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
65959bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
65969bf0dad6SMatthew G. Knepley         }
65978ccfff9cSToby 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);
65989bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
65998ccfff9cSToby 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]);
66009bf0dad6SMatthew G. Knepley         }
66019bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
66029bf0dad6SMatthew G. Knepley       }
66039bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
66049bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
66059bf0dad6SMatthew G. Knepley     }
66063554e41dSMatthew G. Knepley   }
6607552f7358SJed Brown   PetscFunctionReturn(0);
6608552f7358SJed Brown }
66093913d7c8SMatthew G. Knepley 
6610bceba477SMatthew G. Knepley /* Pointwise interpolation
6611bceba477SMatthew G. Knepley      Just code FEM for now
6612bceba477SMatthew G. Knepley      u^f = I u^c
66134ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
66144ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
66154ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6616bceba477SMatthew G. Knepley */
6617bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6618bceba477SMatthew G. Knepley {
6619bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6620bceba477SMatthew G. Knepley   PetscInt       m, n;
6621a063dac3SMatthew G. Knepley   void          *ctx;
662268132eb9SMatthew G. Knepley   DM             cdm;
6623fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
6624bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6625bceba477SMatthew G. Knepley 
6626bceba477SMatthew G. Knepley   PetscFunctionBegin;
6627bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6628bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6629bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6630bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
663168132eb9SMatthew G. Knepley 
6632fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
6633bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6634bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6635fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
6636a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
663768132eb9SMatthew G. Knepley 
6638a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
663968132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
664068132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
664168132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
664268132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
66435d1c2e58SMatthew G. Knepley   /* Use naive scaling */
66445d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6645a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6646a063dac3SMatthew G. Knepley }
6647bceba477SMatthew G. Knepley 
66486dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6649a063dac3SMatthew G. Knepley {
665090748bafSMatthew G. Knepley   PetscErrorCode ierr;
66516dbf9973SLawrence Mitchell   VecScatter     ctx;
665290748bafSMatthew G. Knepley 
6653a063dac3SMatthew G. Knepley   PetscFunctionBegin;
66546dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
66556dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
66566dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6657bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6658bceba477SMatthew G. Knepley }
6659bceba477SMatthew G. Knepley 
6660bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
6661bd041c0cSMatthew G. Knepley {
6662bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
6663bd041c0cSMatthew G. Knepley   PetscInt       m, n;
6664bd041c0cSMatthew G. Knepley   void          *ctx;
6665bd041c0cSMatthew G. Knepley   DM             cdm;
6666bd041c0cSMatthew G. Knepley   PetscBool      regular;
6667bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
6668bd041c0cSMatthew G. Knepley 
6669bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
6670bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6671bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6672bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6673bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
6674bd041c0cSMatthew G. Knepley 
6675bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
6676bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6677bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
6678bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
6679bd041c0cSMatthew G. Knepley 
6680bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
6681bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
6682bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6683bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6684bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
6685bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
6686bd041c0cSMatthew G. Knepley }
6687bd041c0cSMatthew G. Knepley 
6688fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6689fd59a867SMatthew G. Knepley {
6690fd59a867SMatthew G. Knepley   PetscSection   section;
6691ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6692ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6693286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6694ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6695ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6696fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6697fd59a867SMatthew G. Knepley 
6698fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6699ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6700ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6701ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6702ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6703ebb3236fSMatthew G. Knepley     PetscObject  obj;
6704ebb3236fSMatthew G. Knepley     PetscClassId id;
6705ebb3236fSMatthew G. Knepley 
6706ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6707ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6708ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6709ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
67108ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6711ebb3236fSMatthew G. Knepley   }
67122f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6713286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6714c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6715ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6716ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6717dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6718fdafae62SVaclav Hapla   if (!numFields && numBd) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "number of fields is zero and number of boundary conditions is nonzero (this should never happen)");
6719fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
67202f900235SMatthew G. Knepley     PetscInt                field;
6721f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6722385532a5SToby Isaac     const char             *labelName;
6723385532a5SToby Isaac     DMLabel                 label;
67242f900235SMatthew G. Knepley 
6725f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6726385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6727f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6728fd59a867SMatthew G. Knepley   }
67292f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6730ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
67316c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6732ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6733ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6734ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6735ebb3236fSMatthew G. Knepley 
6736ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6737ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6738ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6739ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6740ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6741ebb3236fSMatthew G. Knepley   }
6742ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6743ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6744fd59a867SMatthew G. Knepley     const char             *bdLabel;
6745fd59a867SMatthew G. Knepley     DMLabel                 label;
67460e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6747fd59a867SMatthew G. Knepley     const PetscInt         *values;
67480e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6749f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6750f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6751fd59a867SMatthew G. Knepley 
6752f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6753c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6754385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6755ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
67567391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
67577391c1cbSMatthew G. Knepley       const char *bdname;
6758dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
67597391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
67607391c1cbSMatthew G. Knepley       if (duplicate) break;
67617391c1cbSMatthew G. Knepley     }
6762ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6763b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6764092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
67657391c1cbSMatthew G. Knepley     }
6766ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6767f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
676893a5981aSMatthew G. Knepley       PetscInt       *newidx;
6769ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
677093a5981aSMatthew G. Knepley 
6771fd59a867SMatthew G. Knepley       bcFields[bc] = field;
67720e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6773ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6774ebb3236fSMatthew G. Knepley         IS              tmp;
6775ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6776ebb3236fSMatthew G. Knepley 
6777c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6778ebb3236fSMatthew G. Knepley         if (!tmp) continue;
677993a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
678093a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6781ebb3236fSMatthew G. Knepley         if (isFE[field]) {
678293a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6783ebb3236fSMatthew G. Knepley         } else {
6784ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6785ebb3236fSMatthew G. Knepley         }
678693a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
678793a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6788fd59a867SMatthew G. Knepley       }
6789ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6790ebb3236fSMatthew G. Knepley       newn = 0;
6791ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6792ebb3236fSMatthew G. Knepley         IS              tmp;
6793ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6794ebb3236fSMatthew G. Knepley 
6795c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6796ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6797ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6798ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6799ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6800ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6801ebb3236fSMatthew G. Knepley         } else {
6802ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6803ebb3236fSMatthew G. Knepley         }
6804ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6805ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6806ebb3236fSMatthew G. Knepley       }
6807ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6808ebb3236fSMatthew G. Knepley     }
6809fd59a867SMatthew G. Knepley   }
6810fd59a867SMatthew G. Knepley   /* Handle discretization */
6811ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6812fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
68130bacfa0aSMatthew G. Knepley     PetscObject obj;
68140bacfa0aSMatthew G. Knepley 
68150bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6816ebb3236fSMatthew G. Knepley     if (isFE[f]) {
68170bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6818286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6819fd59a867SMatthew G. Knepley       PetscInt        d;
6820fd59a867SMatthew G. Knepley 
6821fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6822286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6823286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6824ebb3236fSMatthew G. Knepley     } else {
68250bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
68260bacfa0aSMatthew G. Knepley 
68270bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
68280bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6829ebb3236fSMatthew G. Knepley     }
6830fd59a867SMatthew G. Knepley   }
6831286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6832286d8613SMatthew G. Knepley     PetscInt d;
6833286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6834286d8613SMatthew G. Knepley       if ((numDof[f*(dim+1)+d] > 0) && (depth < dim)) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mesh must be interpolated when unknowns are specified on edges or faces.");
6835286d8613SMatthew G. Knepley     }
6836286d8613SMatthew G. Knepley   }
6837ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6838fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6839fd59a867SMatthew G. Knepley     PetscFE     fe;
6840fd59a867SMatthew G. Knepley     const char *name;
684118abd763SMatthew G. Knepley 
684218abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6843fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6844fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6845fd59a867SMatthew G. Knepley   }
6846fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6847fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
68480e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
68490e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6850286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6851ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6852bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6853bceba477SMatthew G. Knepley }
6854bceba477SMatthew G. Knepley 
68550aef6b92SMatthew G. Knepley /*@
68560aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
68570aef6b92SMatthew G. Knepley 
68580aef6b92SMatthew G. Knepley   Input Parameter:
68590aef6b92SMatthew G. Knepley . dm - The DMPlex object
68600aef6b92SMatthew G. Knepley 
68610aef6b92SMatthew G. Knepley   Output Parameter:
68620aef6b92SMatthew G. Knepley . regular - The flag
68630aef6b92SMatthew G. Knepley 
68640aef6b92SMatthew G. Knepley   Level: intermediate
68650aef6b92SMatthew G. Knepley 
68660aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
68670aef6b92SMatthew G. Knepley @*/
68680aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
68690aef6b92SMatthew G. Knepley {
68700aef6b92SMatthew G. Knepley   PetscFunctionBegin;
68710aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68720aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
68730aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
68740aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
68750aef6b92SMatthew G. Knepley }
68760aef6b92SMatthew G. Knepley 
68770aef6b92SMatthew G. Knepley /*@
68780aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
68790aef6b92SMatthew G. Knepley 
68800aef6b92SMatthew G. Knepley   Input Parameters:
68810aef6b92SMatthew G. Knepley + dm - The DMPlex object
68820aef6b92SMatthew G. Knepley - regular - The flag
68830aef6b92SMatthew G. Knepley 
68840aef6b92SMatthew G. Knepley   Level: intermediate
68850aef6b92SMatthew G. Knepley 
68860aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
68870aef6b92SMatthew G. Knepley @*/
68880aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
68890aef6b92SMatthew G. Knepley {
68900aef6b92SMatthew G. Knepley   PetscFunctionBegin;
68910aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68920aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
68930aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
68940aef6b92SMatthew G. Knepley }
68950aef6b92SMatthew G. Knepley 
6896f7c74593SToby Isaac /* anchors */
6897a68b90caSToby Isaac /*@
6898f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6899f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6900a68b90caSToby Isaac 
6901e228b242SToby Isaac   not collective
6902a68b90caSToby Isaac 
6903a68b90caSToby Isaac   Input Parameters:
6904a68b90caSToby Isaac . dm - The DMPlex object
6905a68b90caSToby Isaac 
6906a68b90caSToby Isaac   Output Parameters:
6907a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6908a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6909a68b90caSToby Isaac 
6910a68b90caSToby Isaac 
6911a68b90caSToby Isaac   Level: intermediate
6912a68b90caSToby Isaac 
6913f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6914a68b90caSToby Isaac @*/
6915a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6916a68b90caSToby Isaac {
6917a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
691841e6d900SToby Isaac   PetscErrorCode ierr;
6919a68b90caSToby Isaac 
6920a68b90caSToby Isaac   PetscFunctionBegin;
6921a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
692241e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6923a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6924a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6925a68b90caSToby Isaac   PetscFunctionReturn(0);
6926a68b90caSToby Isaac }
6927a68b90caSToby Isaac 
6928a68b90caSToby Isaac /*@
6929f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6930f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6931a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6932a68b90caSToby Isaac 
6933a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6934f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6935a68b90caSToby Isaac 
6936e228b242SToby Isaac   collective on dm
6937a68b90caSToby Isaac 
6938a68b90caSToby Isaac   Input Parameters:
6939a68b90caSToby Isaac + dm - The DMPlex object
6940e228b242SToby 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).
6941e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6942a68b90caSToby Isaac 
6943a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6944a68b90caSToby Isaac 
6945a68b90caSToby Isaac   Level: intermediate
6946a68b90caSToby Isaac 
6947f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6948a68b90caSToby Isaac @*/
6949a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6950a68b90caSToby Isaac {
6951a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6952e228b242SToby Isaac   PetscMPIInt    result;
6953a68b90caSToby Isaac   PetscErrorCode ierr;
6954a68b90caSToby Isaac 
6955a68b90caSToby Isaac   PetscFunctionBegin;
6956a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6957e228b242SToby Isaac   if (anchorSection) {
6958e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6959e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6960f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6961e228b242SToby Isaac   }
6962e228b242SToby Isaac   if (anchorIS) {
6963e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6964e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6965f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6966e228b242SToby Isaac   }
6967a68b90caSToby Isaac 
6968a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6969a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6970a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6971a68b90caSToby Isaac 
6972a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6973a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6974a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6975a68b90caSToby Isaac 
6976a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6977a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6978a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6979a68b90caSToby Isaac     const PetscInt *anchors;
6980a68b90caSToby Isaac 
6981a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6982a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6983a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6984a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6985a68b90caSToby Isaac       PetscInt p;
6986a68b90caSToby Isaac 
6987a68b90caSToby Isaac       p = anchors[a];
6988a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6989a68b90caSToby Isaac         PetscInt dof;
6990a68b90caSToby Isaac 
6991a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6992a68b90caSToby Isaac         if (dof) {
6993a68b90caSToby Isaac           PetscErrorCode ierr2;
6994a68b90caSToby Isaac 
6995a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
69968ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6997a68b90caSToby Isaac         }
6998a68b90caSToby Isaac       }
6999a68b90caSToby Isaac     }
7000a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
7001a68b90caSToby Isaac   }
7002a68b90caSToby Isaac #endif
7003f7c74593SToby Isaac   /* reset the generic constraints */
7004f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
7005a68b90caSToby Isaac   PetscFunctionReturn(0);
7006a68b90caSToby Isaac }
7007a68b90caSToby Isaac 
7008f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7009a68b90caSToby Isaac {
7010f7c74593SToby Isaac   PetscSection anchorSection;
70116995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
7012a68b90caSToby Isaac   PetscErrorCode ierr;
7013a68b90caSToby Isaac 
7014a68b90caSToby Isaac   PetscFunctionBegin;
7015a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7016a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7017e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7018a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
70196995de1eSToby Isaac   if (numFields) {
7020719ab38cSToby Isaac     PetscInt f;
7021a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7022719ab38cSToby Isaac 
7023719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7024719ab38cSToby Isaac       PetscInt numComp;
7025719ab38cSToby Isaac 
7026719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7027719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7028719ab38cSToby Isaac     }
70296995de1eSToby Isaac   }
7030a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
70316995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
70326995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
70336995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
70346995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7035a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7036a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7037a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7038a68b90caSToby Isaac     if (dof) {
7039a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7040a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7041a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7042a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7043a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7044a68b90caSToby Isaac       }
7045a68b90caSToby Isaac     }
7046a68b90caSToby Isaac   }
7047a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7048a68b90caSToby Isaac   PetscFunctionReturn(0);
7049a68b90caSToby Isaac }
7050a68b90caSToby Isaac 
7051f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7052a68b90caSToby Isaac {
7053f7c74593SToby Isaac   PetscSection aSec;
70540ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
70550ac89760SToby Isaac   const PetscInt *anchors;
70560ac89760SToby Isaac   PetscInt numFields, f;
705766ad2231SToby Isaac   IS aIS;
70580ac89760SToby Isaac   PetscErrorCode ierr;
70590ac89760SToby Isaac 
70600ac89760SToby Isaac   PetscFunctionBegin;
70610ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70620ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
70630ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
70640ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
70650ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7066302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7067a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
706866ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
70696995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
70706995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
70710ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
70720ac89760SToby Isaac   i[0] = 0;
70730ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
70740ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7075f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7076f19733c5SToby Isaac 
7077f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7078f19733c5SToby Isaac     if (!rDof) continue;
7079f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
70800ac89760SToby Isaac     if (numFields) {
70810ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
70820ac89760SToby Isaac         annz = 0;
7083f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7084f19733c5SToby Isaac           a = anchors[rOff + r];
70850ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
70860ac89760SToby Isaac           annz += aDof;
70870ac89760SToby Isaac         }
70880ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
70890ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
70900ac89760SToby Isaac         for (q = 0; q < dof; q++) {
70910ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
70920ac89760SToby Isaac         }
70930ac89760SToby Isaac       }
70940ac89760SToby Isaac     }
70950ac89760SToby Isaac     else {
70960ac89760SToby Isaac       annz = 0;
70970ac89760SToby Isaac       for (q = 0; q < dof; q++) {
70980ac89760SToby Isaac         a = anchors[off + q];
70990ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
71000ac89760SToby Isaac         annz += aDof;
71010ac89760SToby Isaac       }
71020ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
71030ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
71040ac89760SToby Isaac       for (q = 0; q < dof; q++) {
71050ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
71060ac89760SToby Isaac       }
71070ac89760SToby Isaac     }
71080ac89760SToby Isaac   }
71090ac89760SToby Isaac   nnz = i[m];
71100ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
71110ac89760SToby Isaac   offset = 0;
71120ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
71130ac89760SToby Isaac     if (numFields) {
71140ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
71150ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
71160ac89760SToby Isaac         for (q = 0; q < dof; q++) {
71170ac89760SToby Isaac           PetscInt rDof, rOff, r;
71180ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
71190ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
71200ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
71210ac89760SToby Isaac             PetscInt s;
71220ac89760SToby Isaac 
71230ac89760SToby Isaac             a = anchors[rOff + r];
71240ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
71250ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
71260ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
71270ac89760SToby Isaac               j[offset++] = aOff + s;
71280ac89760SToby Isaac             }
71290ac89760SToby Isaac           }
71300ac89760SToby Isaac         }
71310ac89760SToby Isaac       }
71320ac89760SToby Isaac     }
71330ac89760SToby Isaac     else {
71340ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
71350ac89760SToby Isaac       for (q = 0; q < dof; q++) {
71360ac89760SToby Isaac         PetscInt rDof, rOff, r;
71370ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
71380ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
71390ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
71400ac89760SToby Isaac           PetscInt s;
71410ac89760SToby Isaac 
71420ac89760SToby Isaac           a = anchors[rOff + r];
71430ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
71440ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
71450ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
71460ac89760SToby Isaac             j[offset++] = aOff + s;
71470ac89760SToby Isaac           }
71480ac89760SToby Isaac         }
71490ac89760SToby Isaac       }
71500ac89760SToby Isaac     }
71510ac89760SToby Isaac   }
71520ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
715325570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
715425570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
715566ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
71560ac89760SToby Isaac   PetscFunctionReturn(0);
71570ac89760SToby Isaac }
71580ac89760SToby Isaac 
715966ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
716066ad2231SToby Isaac {
7161f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7162f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
716366ad2231SToby Isaac   Mat            cMat;
716466ad2231SToby Isaac   PetscErrorCode ierr;
716566ad2231SToby Isaac 
716666ad2231SToby Isaac   PetscFunctionBegin;
716766ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7168a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
716966ad2231SToby Isaac   if (anchorSection) {
7170e228b242SToby Isaac     PetscDS  ds;
7171e228b242SToby Isaac     PetscInt nf;
7172e228b242SToby Isaac 
7173f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
7174f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7175f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
7176e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
7177302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
7178e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
717966ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
718066ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
718166ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
718266ad2231SToby Isaac   }
718366ad2231SToby Isaac   PetscFunctionReturn(0);
718466ad2231SToby Isaac }
7185