xref: /petsc/src/dm/impls/plex/plex.c (revision 509517efe059b8a0bd2f39cfeb3c611f1ca18388)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2af0996ceSBarry Smith #include <petsc/private/isimpl.h>
3e5c6e791SKarl Rupp #include <petsc/private/vecimpl.h>
48135c375SStefano Zampini #include <petsc/private/glvisvecimpl.h>
50c312b8eSJed Brown #include <petscsf.h>
6e228b242SToby Isaac #include <petscds.h>
7e412dcbdSMatthew G. Knepley #include <petscdraw.h>
8f19dbd58SToby Isaac #include <petscdmfield.h>
9552f7358SJed Brown 
10552f7358SJed Brown /* Logging support */
1167eb269bSLisandro Dalcin PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_InterpolateSF, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh;
12552f7358SJed Brown 
135a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
14552f7358SJed Brown 
15e5337592SStefano Zampini /*@
16e5337592SStefano Zampini   DMPlexRefineSimplexToTensor - Uniformly refines simplicial cells into tensor product cells.
17e5337592SStefano Zampini   3 quadrilaterals per triangle in 2D and 4 hexahedra per tetrahedron in 3D.
18e5337592SStefano Zampini 
19e5337592SStefano Zampini   Collective
20e5337592SStefano Zampini 
21e5337592SStefano Zampini   Input Parameters:
22e5337592SStefano Zampini . dm - The DMPlex object
23e5337592SStefano Zampini 
24e5337592SStefano Zampini   Output Parameters:
25e5337592SStefano Zampini . dmRefined - The refined DMPlex object
26e5337592SStefano Zampini 
27e5337592SStefano Zampini   Note: Returns NULL if the mesh is already a tensor product mesh.
28e5337592SStefano Zampini 
29e5337592SStefano Zampini   Level: intermediate
30e5337592SStefano Zampini 
31e5337592SStefano Zampini .seealso: DMPlexCreate(), DMPlexSetRefinementUniform()
32e5337592SStefano Zampini @*/
33e5337592SStefano Zampini PetscErrorCode DMPlexRefineSimplexToTensor(DM dm, DM *dmRefined)
34e5337592SStefano Zampini {
35e5337592SStefano Zampini   PetscInt         dim, cMax, fMax, cStart, cEnd, coneSize;
36e5337592SStefano Zampini   CellRefiner      cellRefiner;
37e5337592SStefano Zampini   PetscBool        lop, allnoop, localized;
38e5337592SStefano Zampini   PetscErrorCode   ierr;
39e5337592SStefano Zampini 
40e5337592SStefano Zampini   PetscFunctionBegin;
41e5337592SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
424330a3fcSStefano Zampini   PetscValidPointer(dmRefined, 1);
43e5337592SStefano Zampini   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
44e5337592SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,&fMax,NULL,NULL);CHKERRQ(ierr);
45e5337592SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
46e5337592SStefano Zampini   if (!(cEnd - cStart)) cellRefiner = REFINER_NOOP;
47e5337592SStefano Zampini   else {
48e5337592SStefano Zampini     ierr = DMPlexGetConeSize(dm,cStart,&coneSize);CHKERRQ(ierr);
49e5337592SStefano Zampini     switch (dim) {
50e5337592SStefano Zampini     case 1:
51e5337592SStefano Zampini       cellRefiner = REFINER_NOOP;
52e5337592SStefano Zampini     break;
53e5337592SStefano Zampini     case 2:
54e5337592SStefano Zampini       switch (coneSize) {
55e5337592SStefano Zampini       case 3:
564330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
574330a3fcSStefano Zampini         else cellRefiner = REFINER_SIMPLEX_TO_HEX_2D;
58e5337592SStefano Zampini       break;
59e5337592SStefano Zampini       case 4:
604330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
614330a3fcSStefano Zampini         else cellRefiner = REFINER_NOOP;
62e5337592SStefano Zampini       break;
63e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
64e5337592SStefano Zampini       }
65e5337592SStefano Zampini     break;
66e5337592SStefano Zampini     case 3:
67e5337592SStefano Zampini       switch (coneSize) {
68e5337592SStefano Zampini       case 4:
694330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
704330a3fcSStefano Zampini         else cellRefiner = REFINER_SIMPLEX_TO_HEX_3D;
714330a3fcSStefano Zampini       break;
724330a3fcSStefano Zampini       case 5:
734330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
744330a3fcSStefano Zampini         else cellRefiner = REFINER_NOOP;
75e5337592SStefano Zampini       break;
76e5337592SStefano Zampini       case 6:
774330a3fcSStefano Zampini         if (cMax >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Simplex2Tensor in 3D with Hybrid mesh not yet done");
78e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
79e5337592SStefano Zampini       break;
80e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
81e5337592SStefano Zampini       }
82e5337592SStefano Zampini     break;
83e5337592SStefano Zampini     default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle dimension %D",dim);
84e5337592SStefano Zampini     }
85e5337592SStefano Zampini   }
86e5337592SStefano Zampini   /* return if we don't need to refine */
87e5337592SStefano Zampini   lop = (cellRefiner == REFINER_NOOP) ? PETSC_TRUE : PETSC_FALSE;
88e5337592SStefano Zampini   ierr = MPIU_Allreduce(&lop,&allnoop,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
89e5337592SStefano Zampini   if (allnoop) {
90e5337592SStefano Zampini     *dmRefined = NULL;
91e5337592SStefano Zampini     PetscFunctionReturn(0);
92e5337592SStefano Zampini   }
93e5337592SStefano Zampini   ierr = DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);CHKERRQ(ierr);
94e5337592SStefano Zampini   ierr = DMCopyBoundary(dm, *dmRefined);CHKERRQ(ierr);
95e5337592SStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
96e5337592SStefano Zampini   if (localized) {
97e5337592SStefano Zampini     ierr = DMLocalizeCoordinates(*dmRefined);CHKERRQ(ierr);
98e5337592SStefano Zampini   }
99e5337592SStefano Zampini   PetscFunctionReturn(0);
100e5337592SStefano Zampini }
101e5337592SStefano Zampini 
1027afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
1037e42fee7SMatthew G. Knepley {
104a99a26bcSAdrian Croucher   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior;
105a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
1067e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
1077e42fee7SMatthew G. Knepley 
1087e42fee7SMatthew G. Knepley   PetscFunctionBegin;
1097e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
110c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1117e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1127e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11380d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11480d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1157e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1167e42fee7SMatthew G. Knepley   if (field >= 0) {
117a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
118a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
1197e42fee7SMatthew G. Knepley   } else {
120a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
121a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
1227e42fee7SMatthew G. Knepley   }
1235483465cSMatthew G. Knepley   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
124a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
1257e42fee7SMatthew G. Knepley     *sStart = vStart;
1267e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
127a99a26bcSAdrian Croucher     if (globalvcdof[0] == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1287e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_POINT_FIELD;
129a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
1307e42fee7SMatthew G. Knepley     *sStart = cStart;
1317e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
132a99a26bcSAdrian Croucher     if (globalvcdof[1] == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1337e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_CELL_FIELD;
1347e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
1357e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1367e42fee7SMatthew G. Knepley }
1377e42fee7SMatthew G. Knepley 
1387cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
139e412dcbdSMatthew G. Knepley {
140e412dcbdSMatthew G. Knepley   DM                 dm;
141d1df6f1dSMatthew G. Knepley   PetscSection       s;
142e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
143e412dcbdSMatthew G. Knepley   DM                 cdm;
144e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
145e412dcbdSMatthew G. Knepley   Vec                coordinates;
146e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
147e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
148339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
149339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
150d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
151e412dcbdSMatthew G. Knepley   const char        *name;
152339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
153e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
154e412dcbdSMatthew G. Knepley 
155e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
156d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
157d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
158d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
159d1df6f1dSMatthew G. Knepley 
160e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
161e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1628135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
163e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
164d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
165e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
166e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
167e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
168e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
169e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
170e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
171e412dcbdSMatthew G. Knepley 
172e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
173339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley 
175e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
176e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
177e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1780c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1790c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
180e412dcbdSMatthew G. Knepley   }
181e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
182e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
183e412dcbdSMatthew G. Knepley 
184d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
185d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
186d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
187d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
188d1df6f1dSMatthew G. Knepley     IS   fis;
189d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
190d1df6f1dSMatthew G. Knepley     const char *fname;
191d1df6f1dSMatthew G. Knepley 
192d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
193d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
194d1df6f1dSMatthew G. Knepley 
195a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
196d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
197d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
198d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
199d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
200a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
201a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
202d1df6f1dSMatthew G. Knepley     }
203d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
204d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
205d1df6f1dSMatthew G. Knepley 
206d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
207d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
208d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
209d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
210d1df6f1dSMatthew G. Knepley 
211d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
212d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
213339e3443SMatthew G. Knepley       if (!flg) {
214d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
215d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
216d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
217339e3443SMatthew G. Knepley       }
218e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
219339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
220d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
221e412dcbdSMatthew G. Knepley 
222d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
223e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
22499a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
225e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
226e412dcbdSMatthew G. Knepley 
227d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
228339e3443SMatthew G. Knepley         if (a) {
229d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
230339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
231339e3443SMatthew G. Knepley         } else {
232339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
233339e3443SMatthew G. Knepley           PetscInt     numVals, va;
234339e3443SMatthew G. Knepley 
235d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
236d1df6f1dSMatthew G. Knepley           if (numVals % Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of components %D does not divide the number of values in the closure %D", Nc, numVals);
237d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
238d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
239d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
240d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
241339e3443SMatthew G. Knepley             break;
242d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
243d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
244d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/(Nc*2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp + numVals/(Nc*2)]), vbound[0], vbound[1]);
245d1df6f1dSMatthew G. Knepley             break;
246d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
247339e3443SMatthew G. Knepley           }
248d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
249339e3443SMatthew G. Knepley         }
250e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
251e412dcbdSMatthew G. Knepley         switch (numCoords) {
252e412dcbdSMatthew G. Knepley         case 6:
253339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);CHKERRQ(ierr);
254e412dcbdSMatthew G. Knepley           break;
255e412dcbdSMatthew G. Knepley         case 8:
256339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);CHKERRQ(ierr);
257339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), color[2], color[3], color[0]);CHKERRQ(ierr);
258e412dcbdSMatthew G. Knepley           break;
259e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
260e412dcbdSMatthew G. Knepley         }
261e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
262e412dcbdSMatthew G. Knepley       }
263d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
264e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
265e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
266e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
267d1df6f1dSMatthew G. Knepley     }
268d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
269d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
270d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
271d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
272d1df6f1dSMatthew G. Knepley     }
273d1df6f1dSMatthew G. Knepley   }
274e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
275e412dcbdSMatthew G. Knepley }
276e412dcbdSMatthew G. Knepley 
277684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
278684b87d9SLisandro Dalcin {
279684b87d9SLisandro Dalcin   DM                      dm;
280684b87d9SLisandro Dalcin   Vec                     locv;
281684b87d9SLisandro Dalcin   const char              *name;
282684b87d9SLisandro Dalcin   PetscSection            section;
283684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
284684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
285684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
286684b87d9SLisandro Dalcin 
287684b87d9SLisandro Dalcin   PetscFunctionBegin;
288684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
289684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
290684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
291684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
292684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
293e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
294684b87d9SLisandro Dalcin   ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
295a8f87f1dSPatrick Sanan   ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
296684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
297684b87d9SLisandro Dalcin }
298684b87d9SLisandro Dalcin 
299552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
300552f7358SJed Brown {
301552f7358SJed Brown   DM             dm;
302684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
303552f7358SJed Brown   PetscErrorCode ierr;
304552f7358SJed Brown 
305552f7358SJed Brown   PetscFunctionBegin;
306552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
30782f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
308552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
309b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
310f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3118135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
312684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
313684b87d9SLisandro Dalcin     PetscInt    i,numFields;
314684b87d9SLisandro Dalcin     PetscObject fe;
315ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
316684b87d9SLisandro Dalcin     Vec         locv = v;
317684b87d9SLisandro Dalcin     const char  *name;
318684b87d9SLisandro Dalcin     PetscInt    step;
319684b87d9SLisandro Dalcin     PetscReal   time;
320ef31f671SMatthew G. Knepley 
321ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
322684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
323684b87d9SLisandro Dalcin       ierr = DMGetField(dm, i, &fe);CHKERRQ(ierr);
324684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
325ef31f671SMatthew G. Knepley     }
326684b87d9SLisandro Dalcin     if (fem) {
327684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
328684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
329684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
330684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
331684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
332684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
333ef31f671SMatthew G. Knepley     }
334552f7358SJed Brown     if (isvtk) {
335684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
336b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
337b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
338684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
339b136c2c9SMatthew G. Knepley #else
340b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
341b136c2c9SMatthew G. Knepley #endif
342f13a32a3SMatthew G. Knepley     } else if (isdraw) {
343684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
344684b87d9SLisandro Dalcin     } else if (isglvis) {
345684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
346684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
347684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
348684b87d9SLisandro Dalcin     }
349684b87d9SLisandro Dalcin     if (fem) {ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);}
350552f7358SJed Brown   } else {
351684b87d9SLisandro Dalcin     PetscBool isseq;
352684b87d9SLisandro Dalcin 
353684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
35455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
35555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
356552f7358SJed Brown   }
357552f7358SJed Brown   PetscFunctionReturn(0);
358552f7358SJed Brown }
359552f7358SJed Brown 
360552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
361552f7358SJed Brown {
362552f7358SJed Brown   DM             dm;
363684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
364552f7358SJed Brown   PetscErrorCode ierr;
365552f7358SJed Brown 
366552f7358SJed Brown   PetscFunctionBegin;
367552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
36882f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
369552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
37033c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
371e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3728135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
373684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
374552f7358SJed Brown     Vec         locv;
375552f7358SJed Brown     const char *name;
376552f7358SJed Brown 
377c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
378552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
379552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
380552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
381552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
382552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
383c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
384b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
385b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
38639d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
387b136c2c9SMatthew G. Knepley #else
388b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
389b136c2c9SMatthew G. Knepley #endif
390552f7358SJed Brown   } else {
391684b87d9SLisandro Dalcin     PetscBool isseq;
392684b87d9SLisandro Dalcin 
393684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
39455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
39555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
396552f7358SJed Brown   }
397552f7358SJed Brown   PetscFunctionReturn(0);
398552f7358SJed Brown }
399552f7358SJed Brown 
400d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
401d930f514SMatthew G. Knepley {
402d930f514SMatthew G. Knepley   DM                dm;
403d930f514SMatthew G. Knepley   MPI_Comm          comm;
404d930f514SMatthew G. Knepley   PetscViewerFormat format;
405d930f514SMatthew G. Knepley   Vec               v;
406d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
407d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
408d930f514SMatthew G. Knepley 
409d930f514SMatthew G. Knepley   PetscFunctionBegin;
410d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
411d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4122c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
413d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
414d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
415d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
416d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
417d930f514SMatthew G. Knepley     const char *vecname;
418d930f514SMatthew G. Knepley     PetscInt    n, nroots;
419d930f514SMatthew G. Knepley 
420d930f514SMatthew G. Knepley     if (dm->sfNatural) {
421ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
422d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
423d930f514SMatthew G. Knepley       if (n == nroots) {
424d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
425d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
426d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
427d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
429d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
430d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
431d930f514SMatthew G. Knepley   } else {
432d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
433d930f514SMatthew G. Knepley     v = originalv;
434d930f514SMatthew G. Knepley   }
435d930f514SMatthew G. Knepley   if (ishdf5) {
436d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
43739d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
438d930f514SMatthew G. Knepley #else
439d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
440d930f514SMatthew G. Knepley #endif
441d930f514SMatthew G. Knepley   } else if (isvtk) {
442d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
443d930f514SMatthew G. Knepley   } else {
444d930f514SMatthew G. Knepley     PetscBool isseq;
445d930f514SMatthew G. Knepley 
446d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
447d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
448d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
449d930f514SMatthew G. Knepley   }
450d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
451d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
452d930f514SMatthew G. Knepley }
453d930f514SMatthew G. Knepley 
4542c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4552c40f234SMatthew G. Knepley {
4562c40f234SMatthew G. Knepley   DM             dm;
4572c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4582c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4592c40f234SMatthew G. Knepley 
4602c40f234SMatthew G. Knepley   PetscFunctionBegin;
4612c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4622c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4632c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley   if (ishdf5) {
4652c40f234SMatthew G. Knepley     DM          dmBC;
4662c40f234SMatthew G. Knepley     Vec         gv;
4672c40f234SMatthew G. Knepley     const char *name;
4682c40f234SMatthew G. Knepley 
4692c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4702c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4712c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4722c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4732c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4742c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4752c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4762c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4772c40f234SMatthew G. Knepley   } else {
4782c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4792c40f234SMatthew G. Knepley   }
4802c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4812c40f234SMatthew G. Knepley }
4822c40f234SMatthew G. Knepley 
4832c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4842c40f234SMatthew G. Knepley {
4852c40f234SMatthew G. Knepley   DM             dm;
4862c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4872c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4882c40f234SMatthew G. Knepley 
4892c40f234SMatthew G. Knepley   PetscFunctionBegin;
4902c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4912c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4922c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4932c40f234SMatthew G. Knepley   if (ishdf5) {
494878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
49539d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
496b136c2c9SMatthew G. Knepley #else
497b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
498878b459fSMatthew G. Knepley #endif
4992c40f234SMatthew G. Knepley   } else {
5002c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
501552f7358SJed Brown   }
502552f7358SJed Brown   PetscFunctionReturn(0);
503552f7358SJed Brown }
504552f7358SJed Brown 
505d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
506d930f514SMatthew G. Knepley {
507d930f514SMatthew G. Knepley   DM                dm;
508d930f514SMatthew G. Knepley   PetscViewerFormat format;
509d930f514SMatthew G. Knepley   PetscBool         ishdf5;
510d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
511d930f514SMatthew G. Knepley 
512d930f514SMatthew G. Knepley   PetscFunctionBegin;
513d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
514d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
515d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
516d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
517d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
518d930f514SMatthew G. Knepley     if (dm->sfNatural) {
519d930f514SMatthew G. Knepley       if (ishdf5) {
520d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
521d930f514SMatthew G. Knepley         Vec         v;
522d930f514SMatthew G. Knepley         const char *vecname;
523d930f514SMatthew G. Knepley 
524d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
525d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
526d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
52739d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
528d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
529d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
530d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
531d930f514SMatthew G. Knepley #else
532d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
533d930f514SMatthew G. Knepley #endif
534d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
535d930f514SMatthew G. Knepley     }
536d930f514SMatthew G. Knepley   }
537d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
538d930f514SMatthew G. Knepley }
539d930f514SMatthew G. Knepley 
5407cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
541731e8ddeSMatthew G. Knepley {
542731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
543731e8ddeSMatthew G. Knepley   Vec                coordinates;
544731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
545731e8ddeSMatthew G. Knepley   const char        *name[4];
546731e8ddeSMatthew G. Knepley   const PetscScalar *a;
547731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
548731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
549731e8ddeSMatthew G. Knepley 
550731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
551731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
552731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
553731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
554731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
555731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
556731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
557731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
559731e8ddeSMatthew G. Knepley   name[1]     = "edge";
560731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
561731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
562731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
563731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
564731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
565731e8ddeSMatthew G. Knepley 
566f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
567731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
568731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
569731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
570731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
571731e8ddeSMatthew G. Knepley 
572731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
573731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley       if (!dof) continue;
575731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
577f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
578731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
579731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
580731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
581731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
58241477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
583731e8ddeSMatthew G. Knepley         }
584731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
585731e8ddeSMatthew G. Knepley       }
586731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
587731e8ddeSMatthew G. Knepley     }
588731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
589731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
590731e8ddeSMatthew G. Knepley   }
591731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
592731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
593731e8ddeSMatthew G. Knepley }
594731e8ddeSMatthew G. Knepley 
5957cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
596552f7358SJed Brown {
597552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
598552f7358SJed Brown   DM                cdm;
599552f7358SJed Brown   DMLabel           markers;
600552f7358SJed Brown   PetscSection      coordSection;
601552f7358SJed Brown   Vec               coordinates;
602552f7358SJed Brown   PetscViewerFormat format;
603552f7358SJed Brown   PetscErrorCode    ierr;
604552f7358SJed Brown 
605552f7358SJed Brown   PetscFunctionBegin;
606552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
607e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
608552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
609552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
610552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
611552f7358SJed Brown     const char *name;
612f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
613552f7358SJed Brown     PetscInt    pStart, pEnd, p;
614552f7358SJed Brown     PetscMPIInt rank, size;
615552f7358SJed Brown 
61682f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
61782f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
618552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
619552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
620552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
621f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
622f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
623f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
624f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
625f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
626e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6274d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
628e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
629552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
630552f7358SJed Brown       PetscInt dof, off, s;
631552f7358SJed Brown 
632552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
633552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
634552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
635e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
636552f7358SJed Brown       }
637552f7358SJed Brown     }
638552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
639e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
640e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
641552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
642552f7358SJed Brown       PetscInt dof, off, c;
643552f7358SJed Brown 
644552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
645552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
646552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
647e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
648552f7358SJed Brown       }
649552f7358SJed Brown     }
650552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6514d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
65280180ce3SStefano Zampini     ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
653c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
6547422c0d1SMatthew G. Knepley     if (markers) {ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);}
655552f7358SJed Brown     if (size > 1) {
656552f7358SJed Brown       PetscSF sf;
657552f7358SJed Brown 
658552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
659552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
660552f7358SJed Brown     }
661552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
662552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6630588280cSMatthew G. Knepley     const char  *name, *color;
6640588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6650588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
666552f7358SJed Brown     PetscReal    scale         = 2.0;
66778081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
6680588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6690588280cSMatthew G. Knepley     double       tcoords[3];
670552f7358SJed Brown     PetscScalar *coords;
6710588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
672552f7358SJed Brown     PetscMPIInt  rank, size;
6730588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
674202fd40aSStefano Zampini     PetscBool    plotEdges, flg;
675552f7358SJed Brown 
6760588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
677552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
678c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6790588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6800588280cSMatthew G. Knepley     numColors  = 10;
6810588280cSMatthew G. Knepley     numLColors = 10;
6820588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
683c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
68478081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
685c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
686c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6870588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
688c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6890588280cSMatthew G. Knepley     if (!useColors) {
6900588280cSMatthew G. Knepley       numColors = 3;
6910588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6920588280cSMatthew G. Knepley     }
693c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6940588280cSMatthew G. Knepley     if (!useColors) {
6950588280cSMatthew G. Knepley       numLColors = 4;
6960588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
6970588280cSMatthew G. Knepley     }
698202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
699202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
700202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
701202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
70282f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
70382f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
704552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
705770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7060588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
707552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
708552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
709552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7100588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7110588280cSMatthew G. Knepley     if (size > 1) {
712f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
713770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
714770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
715770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
716770b213bSMatthew G Knepley         } else if (p > 0) {
717770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
718770b213bSMatthew G Knepley         }
719770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
720770b213bSMatthew G Knepley       }
7210588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7220588280cSMatthew G. Knepley     }
72378081901SStefano Zampini     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", tikzscale);CHKERRQ(ierr);
724552f7358SJed Brown     /* Plot vertices */
725552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
726552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7274d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
728552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
729552f7358SJed Brown       PetscInt  off, dof, d;
7300588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
731552f7358SJed Brown 
732552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
733552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7340588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
735f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7360588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7370588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
738c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7390588280cSMatthew G. Knepley       }
7400588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7410588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
742552f7358SJed Brown       for (d = 0; d < dof; ++d) {
743552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7440588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
745552f7358SJed Brown       }
7460588280cSMatthew G. Knepley       color = colors[rank%numColors];
7470588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7480588280cSMatthew G. Knepley         PetscInt val;
749c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7500588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7510588280cSMatthew G. Knepley       }
7520588280cSMatthew G. Knepley       if (useNumbers) {
753e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7540588280cSMatthew G. Knepley       } else {
755e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7560588280cSMatthew G. Knepley       }
757552f7358SJed Brown     }
758552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
759552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
760846a3e8bSMatthew G. Knepley     /* Plot cells */
761846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
76278081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
763846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
764846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
765846a3e8bSMatthew G. Knepley         const PetscInt *cone;
766846a3e8bSMatthew G. Knepley 
767846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
768846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
769846a3e8bSMatthew G. Knepley           PetscInt val;
770846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
771846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
772846a3e8bSMatthew G. Knepley         }
773846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
774846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
775846a3e8bSMatthew G. Knepley       }
776846a3e8bSMatthew G. Knepley     } else {
777846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
778846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
779846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
780846a3e8bSMatthew G. Knepley 
781846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
782846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
783846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
784846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
785846a3e8bSMatthew G. Knepley 
786846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
787846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
788846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
789846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
790846a3e8bSMatthew G. Knepley         }
791846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
792846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
793846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
794846a3e8bSMatthew G. Knepley       }
795846a3e8bSMatthew G. Knepley     }
796846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
797846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
798846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
799846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
800846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
801846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
802846a3e8bSMatthew G. Knepley 
803846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
804846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
805846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
806846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
807846a3e8bSMatthew G. Knepley         PetscInt       off;
808846a3e8bSMatthew G. Knepley 
809846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
810846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
811846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
812846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
813846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
814846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
815846a3e8bSMatthew G. Knepley         }
816846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
817846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
818846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
819846a3e8bSMatthew G. Knepley         ++n;
820846a3e8bSMatthew G. Knepley       }
821846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
822846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
823846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
824846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
825846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", ccoords[d]);CHKERRQ(ierr);
826846a3e8bSMatthew G. Knepley       }
827846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
828846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
829846a3e8bSMatthew G. Knepley         PetscInt val;
830846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
831846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
832846a3e8bSMatthew G. Knepley       }
833846a3e8bSMatthew G. Knepley       if (useNumbers) {
834846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
835846a3e8bSMatthew G. Knepley       } else {
836846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", c, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
837846a3e8bSMatthew G. Knepley       }
838846a3e8bSMatthew G. Knepley     }
839846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
840552f7358SJed Brown     /* Plot edges */
841202fd40aSStefano Zampini     if (plotEdges) {
842552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
843552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
844552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
845552f7358SJed Brown         const PetscInt *cone;
846552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
847552f7358SJed Brown 
848552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
849f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
850552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
851552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
852552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
853552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
854552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
855552f7358SJed Brown         for (d = 0; d < dof; ++d) {
856202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
857c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
858552f7358SJed Brown         }
8590588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
8600588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
8610588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
8620588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
863f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
8640588280cSMatthew G. Knepley         }
8650588280cSMatthew G. Knepley         color = colors[rank%numColors];
8660588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
8670588280cSMatthew G. Knepley           PetscInt val;
868c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
8690750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
8700588280cSMatthew G. Knepley         }
871e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
872552f7358SJed Brown       }
873552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
874552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
875552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
8760588280cSMatthew G. Knepley     }
877552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8784d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8790588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
880770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
8810588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
8820588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
8830588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
8840588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
885552f7358SJed Brown   } else {
88682f516ccSBarry Smith     MPI_Comm    comm;
887834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
888f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
889552f7358SJed Brown     PetscInt    pStart, pEnd, p;
890a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
8911143a3c0SMatthew G. Knepley     const char *name;
892552f7358SJed Brown     PetscMPIInt size;
893552f7358SJed Brown 
89482f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
895552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
896c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
897f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8985f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
899f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
900f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
901f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
902552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
903b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
904a04427b4SStefano Zampini     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, depth > 1 ? &pMax[depth - 2] : NULL, &pMax[0]);CHKERRQ(ierr);
905b0138d5dSStefano Zampini     ierr = PetscCalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
906552f7358SJed Brown     if (depth == 1) {
907552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
908552f7358SJed Brown       pEnd = pEnd - pStart;
909a04427b4SStefano Zampini       pMax[0] -= pStart;
910552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
911a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[0], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
912f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
913a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
914a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
915a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
916a04427b4SStefano Zampini       }
917552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
918552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
919552f7358SJed Brown       pEnd = pEnd - pStart;
920a04427b4SStefano Zampini       pMax[depth] -= pStart;
921552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
922a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[depth], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
923552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
924a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
925a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
926a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
927a04427b4SStefano Zampini       }
928552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
929552f7358SJed Brown     } else {
930cbb7f117SMark Adams       PetscMPIInt rank;
931cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
932552f7358SJed Brown       for (d = 0; d <= dim; d++) {
933552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
934834065abSMatthew G. Knepley         pEnd    -= pStart;
935834065abSMatthew G. Knepley         pMax[d] -= pStart;
936552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
937834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
938552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
939834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
940cbb7f117SMark Adams           if (!rank) {
941834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
942834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
943834065abSMatthew G. Knepley           }
944cbb7f117SMark Adams         }
945552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
946552f7358SJed Brown       }
947552f7358SJed Brown     }
948834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
949c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
950a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
951a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
952a57dd577SMatthew G Knepley       DMLabel         label;
953a57dd577SMatthew G Knepley       const char     *name;
954a57dd577SMatthew G Knepley       IS              valueIS;
955a57dd577SMatthew G Knepley       const PetscInt *values;
956a57dd577SMatthew G Knepley       PetscInt        numValues, v;
957a57dd577SMatthew G Knepley 
958c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
959c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
960a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
961d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
962a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
963a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
964120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
965a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
966a57dd577SMatthew G Knepley         PetscInt size;
967a57dd577SMatthew G Knepley 
968a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
969a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
970d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
971a57dd577SMatthew G Knepley       }
972a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
973120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
974a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
9754d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
976a57dd577SMatthew G Knepley     }
977a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
9788e7ff633SMatthew G. Knepley     if (cdm) {
9798e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
9808e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
9818e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
9828e7ff633SMatthew G. Knepley     }
983552f7358SJed Brown   }
984552f7358SJed Brown   PetscFunctionReturn(0);
985552f7358SJed Brown }
986552f7358SJed Brown 
9877cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
988e412dcbdSMatthew G. Knepley {
989e412dcbdSMatthew G. Knepley   PetscDraw          draw;
990e412dcbdSMatthew G. Knepley   DM                 cdm;
991e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
992e412dcbdSMatthew G. Knepley   Vec                coordinates;
993e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
99429494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
995e412dcbdSMatthew G. Knepley   PetscBool          isnull;
996e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
997cf3064d3SMatthew G. Knepley   PetscMPIInt        rank;
998e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
999e412dcbdSMatthew G. Knepley 
1000e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1001e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1002e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1003e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1004e87a4003SBarry Smith   ierr = DMGetSection(cdm, &coordSection);CHKERRQ(ierr);
1005e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1006e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1007e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1008e412dcbdSMatthew G. Knepley 
1009e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1010e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1011e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1012e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1013e412dcbdSMatthew G. Knepley 
1014e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1015e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1016e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
10170c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
10180c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1019e412dcbdSMatthew G. Knepley   }
1020e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
102129494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
102229494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
102329494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1024e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1025e412dcbdSMatthew G. Knepley 
1026cf3064d3SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1027cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1028cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1029cf3064d3SMatthew G. Knepley     PetscInt     numCoords,coneSize;
1030cf3064d3SMatthew G. Knepley 
1031cf3064d3SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1032cf3064d3SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1033cf3064d3SMatthew G. Knepley     switch (coneSize) {
1034cf3064d3SMatthew G. Knepley     case 3:
1035cf3064d3SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1036cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1037cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1038cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1039cf3064d3SMatthew G. Knepley       break;
1040cf3064d3SMatthew G. Knepley     case 4:
1041cf3064d3SMatthew G. Knepley       ierr = PetscDrawRectangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1042cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1043cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1044cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1045cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1046cf3064d3SMatthew G. Knepley       break;
1047cf3064d3SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1048cf3064d3SMatthew G. Knepley     }
1049cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1050cf3064d3SMatthew G. Knepley   }
1051e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1052e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
105329494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
1054e412dcbdSMatthew G. Knepley 
105529494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1056e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
105729494db1SLisandro Dalcin     switch (coneSize) {
105829494db1SLisandro Dalcin     case 3:
10590c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10600c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10610c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1062e412dcbdSMatthew G. Knepley       break;
106329494db1SLisandro Dalcin     case 4:
10640c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10650c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10660c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
10670c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1068e412dcbdSMatthew G. Knepley       break;
106929494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1070e412dcbdSMatthew G. Knepley     }
1071e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1072e412dcbdSMatthew G. Knepley   }
1073e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1074e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1075e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1076e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1077e412dcbdSMatthew G. Knepley }
1078e412dcbdSMatthew G. Knepley 
1079552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1080552f7358SJed Brown {
1081e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
1082002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1083552f7358SJed Brown   PetscErrorCode ierr;
1084552f7358SJed Brown 
1085552f7358SJed Brown   PetscFunctionBegin;
1086552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1087552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1088552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1089fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
1090c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
1091e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
10928135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
1093552f7358SJed Brown   if (iascii) {
10948135c375SStefano Zampini     PetscViewerFormat format;
10958135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
10968135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
10978135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
10988135c375SStefano Zampini     } else {
1099552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
11008135c375SStefano Zampini     }
1101c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1102c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
110339d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1104c6ccd67eSMatthew G. Knepley #else
1105c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1106552f7358SJed Brown #endif
1107e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1108fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1109e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1110e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
11118135c375SStefano Zampini   } else if (isglvis) {
11128135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
111362201deeSVaclav Hapla   } else {
1114a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1115fcf6c8fdSToby Isaac   }
1116cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1117cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1118cb3ba0daSMatthew G. Knepley   if (flg) {
1119cb3ba0daSMatthew G. Knepley     Vec ranks;
1120cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1121cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1122cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1123cb3ba0daSMatthew G. Knepley   }
1124002a2709SMatthew G. Knepley   /* Optionally view a label */
1125002a2709SMatthew G. Knepley   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
1126002a2709SMatthew G. Knepley   if (flg) {
1127002a2709SMatthew G. Knepley     DMLabel label;
1128002a2709SMatthew G. Knepley     Vec     val;
1129002a2709SMatthew G. Knepley 
1130002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1131002a2709SMatthew G. Knepley     if (!label) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %s provided to -dm_label_view does not exist in this DM", name);
1132002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1133002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1134002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1135002a2709SMatthew G. Knepley   }
1136552f7358SJed Brown   PetscFunctionReturn(0);
1137552f7358SJed Brown }
1138552f7358SJed Brown 
11392c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
11402c40f234SMatthew G. Knepley {
1141d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
11422c40f234SMatthew G. Knepley   PetscErrorCode ierr;
11432c40f234SMatthew G. Knepley 
11442c40f234SMatthew G. Knepley   PetscFunctionBegin;
11452c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11462c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
11472c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1148d4f5a9a0SVaclav Hapla   if (ishdf5) {
11492c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
11509c48423bSVaclav Hapla     PetscViewerFormat format;
11519c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
11529c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
11539c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1154*509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
115539d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1156*509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
11572c40f234SMatthew G. Knepley #else
11582c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1159552f7358SJed Brown #endif
1160d4f5a9a0SVaclav Hapla   } else {
1161d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1162552f7358SJed Brown   }
1163552f7358SJed Brown   PetscFunctionReturn(0);
1164552f7358SJed Brown }
1165552f7358SJed Brown 
1166552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1167552f7358SJed Brown {
1168552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1169552f7358SJed Brown   PetscErrorCode ierr;
1170552f7358SJed Brown 
1171552f7358SJed Brown   PetscFunctionBegin;
11728135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
11738135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
11740d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1175552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1176552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1177552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1178552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1179be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1180552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1181552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1182d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1183d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
118477623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1185a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1186552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1187552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1188a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1189a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1190d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1191d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1192d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1193d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1194d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1195d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1196fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1197552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1198552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1199552f7358SJed Brown   PetscFunctionReturn(0);
1200552f7358SJed Brown }
1201552f7358SJed Brown 
1202b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1203552f7358SJed Brown {
12048d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1205acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1206552f7358SJed Brown   PetscInt               localSize;
1207837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1208552f7358SJed Brown   PetscErrorCode         ierr;
1209b412c318SBarry Smith   MatType                mtype;
12101428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1211552f7358SJed Brown 
1212552f7358SJed Brown   PetscFunctionBegin;
1213607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1214b412c318SBarry Smith   mtype = dm->mattype;
1215e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1216552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1217552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
121882f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1219552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1220552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1221552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1222acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1223acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1224552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1225552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1226552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1227552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1228552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1229552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1230552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1231837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1232552f7358SJed Brown   if (!isShell) {
1233be36d101SStefano Zampini     PetscSection subSection;
1234837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
12350be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1236fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1237552f7358SJed Brown 
123800140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1239be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1240be36d101SStefano Zampini       PetscSection section;
1241be36d101SStefano Zampini       PetscInt     size;
1242be36d101SStefano Zampini 
1243e87a4003SBarry Smith       ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1244be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1245be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1246be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1247be36d101SStefano Zampini     } else {
124800140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1249be36d101SStefano Zampini     }
1250552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1251be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1252a9d99c84SMatthew G. Knepley       PetscInt bdof;
1253a9d99c84SMatthew G. Knepley 
1254552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1255fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
12561d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
12571d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
12581d17a0a3SMatthew G. Knepley       if (dof) {
12591d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1260be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1261be36d101SStefano Zampini       }
1262be36d101SStefano Zampini       if (isMatIS) {
1263be36d101SStefano Zampini         PetscInt loff,c,off;
1264be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1265be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1266be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1267552f7358SJed Brown       }
12682a28c762SMatthew G Knepley     }
12692a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
12700be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
12710be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
12720be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
12730be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
12744fa26246SMatthew G. Knepley     bs = bs < 0 ? 1 : bs;
1275be36d101SStefano Zampini     if (isMatIS) {
12764fa26246SMatthew G. Knepley       PetscInt l;
12774fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
12784fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
12794fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1280be36d101SStefano Zampini     }
1281be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1282be36d101SStefano Zampini     if (isMatIS) {
1283be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1284be36d101SStefano Zampini     }
12851795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
12868d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1287552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1288552f7358SJed Brown   }
1289b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1290552f7358SJed Brown   PetscFunctionReturn(0);
1291552f7358SJed Brown }
1292552f7358SJed Brown 
12937cd05799SMatthew G. Knepley /*@
1294a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1295be36d101SStefano Zampini 
1296be36d101SStefano Zampini   Not collective
1297be36d101SStefano Zampini 
1298be36d101SStefano Zampini   Input Parameter:
1299be36d101SStefano Zampini . mesh - The DMPlex
1300be36d101SStefano Zampini 
1301be36d101SStefano Zampini   Output Parameters:
1302be36d101SStefano Zampini . subsection - The subdomain section
1303be36d101SStefano Zampini 
1304be36d101SStefano Zampini   Level: developer
1305be36d101SStefano Zampini 
1306be36d101SStefano Zampini .seealso:
13077cd05799SMatthew G. Knepley @*/
1308be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1309be36d101SStefano Zampini {
1310be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1311be36d101SStefano Zampini   PetscErrorCode ierr;
1312be36d101SStefano Zampini 
1313be36d101SStefano Zampini   PetscFunctionBegin;
1314be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1315be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1316be36d101SStefano Zampini     PetscSection section;
1317be36d101SStefano Zampini     PetscSF      sf;
1318be36d101SStefano Zampini 
1319be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1320e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
1321be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1322be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1323be36d101SStefano Zampini   }
1324be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1325be36d101SStefano Zampini   PetscFunctionReturn(0);
1326be36d101SStefano Zampini }
1327be36d101SStefano Zampini 
1328552f7358SJed Brown /*@
1329552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1330552f7358SJed Brown 
1331552f7358SJed Brown   Not collective
1332552f7358SJed Brown 
1333552f7358SJed Brown   Input Parameter:
1334552f7358SJed Brown . mesh - The DMPlex
1335552f7358SJed Brown 
1336552f7358SJed Brown   Output Parameters:
1337552f7358SJed Brown + pStart - The first mesh point
1338552f7358SJed Brown - pEnd   - The upper bound for mesh points
1339552f7358SJed Brown 
1340552f7358SJed Brown   Level: beginner
1341552f7358SJed Brown 
1342552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1343552f7358SJed Brown @*/
1344552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1345552f7358SJed Brown {
1346552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1347552f7358SJed Brown   PetscErrorCode ierr;
1348552f7358SJed Brown 
1349552f7358SJed Brown   PetscFunctionBegin;
1350552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1351552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1352552f7358SJed Brown   PetscFunctionReturn(0);
1353552f7358SJed Brown }
1354552f7358SJed Brown 
1355552f7358SJed Brown /*@
1356552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1357552f7358SJed Brown 
1358552f7358SJed Brown   Not collective
1359552f7358SJed Brown 
1360552f7358SJed Brown   Input Parameters:
1361552f7358SJed Brown + mesh - The DMPlex
1362552f7358SJed Brown . pStart - The first mesh point
1363552f7358SJed Brown - pEnd   - The upper bound for mesh points
1364552f7358SJed Brown 
1365552f7358SJed Brown   Output Parameters:
1366552f7358SJed Brown 
1367552f7358SJed Brown   Level: beginner
1368552f7358SJed Brown 
1369552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1370552f7358SJed Brown @*/
1371552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1372552f7358SJed Brown {
1373552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1374552f7358SJed Brown   PetscErrorCode ierr;
1375552f7358SJed Brown 
1376552f7358SJed Brown   PetscFunctionBegin;
1377552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1378552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1379552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1380552f7358SJed Brown   PetscFunctionReturn(0);
1381552f7358SJed Brown }
1382552f7358SJed Brown 
1383552f7358SJed Brown /*@
1384eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of 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 . size - The cone size for point p
1394552f7358SJed Brown 
1395552f7358SJed Brown   Level: beginner
1396552f7358SJed Brown 
1397552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1398552f7358SJed Brown @*/
1399552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1400552f7358SJed Brown {
1401552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1402552f7358SJed Brown   PetscErrorCode ierr;
1403552f7358SJed Brown 
1404552f7358SJed Brown   PetscFunctionBegin;
1405552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1406552f7358SJed Brown   PetscValidPointer(size, 3);
1407552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1408552f7358SJed Brown   PetscFunctionReturn(0);
1409552f7358SJed Brown }
1410552f7358SJed Brown 
1411552f7358SJed Brown /*@
1412eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1413552f7358SJed Brown 
1414552f7358SJed Brown   Not collective
1415552f7358SJed Brown 
1416552f7358SJed Brown   Input Parameters:
1417552f7358SJed Brown + mesh - The DMPlex
1418eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1419552f7358SJed Brown - size - The cone size for point p
1420552f7358SJed Brown 
1421552f7358SJed Brown   Output Parameter:
1422552f7358SJed Brown 
1423552f7358SJed Brown   Note:
1424552f7358SJed Brown   This should be called after DMPlexSetChart().
1425552f7358SJed Brown 
1426552f7358SJed Brown   Level: beginner
1427552f7358SJed Brown 
1428552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1429552f7358SJed Brown @*/
1430552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1431552f7358SJed Brown {
1432552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1433552f7358SJed Brown   PetscErrorCode ierr;
1434552f7358SJed Brown 
1435552f7358SJed Brown   PetscFunctionBegin;
1436552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1437552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
14380d644c17SKarl Rupp 
1439552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1440552f7358SJed Brown   PetscFunctionReturn(0);
1441552f7358SJed Brown }
1442552f7358SJed Brown 
1443f5a469b9SMatthew G. Knepley /*@
1444eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1445f5a469b9SMatthew G. Knepley 
1446f5a469b9SMatthew G. Knepley   Not collective
1447f5a469b9SMatthew G. Knepley 
1448f5a469b9SMatthew G. Knepley   Input Parameters:
1449f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1450eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1451f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1452f5a469b9SMatthew G. Knepley 
1453f5a469b9SMatthew G. Knepley   Output Parameter:
1454f5a469b9SMatthew G. Knepley 
1455f5a469b9SMatthew G. Knepley   Note:
1456f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1457f5a469b9SMatthew G. Knepley 
1458f5a469b9SMatthew G. Knepley   Level: beginner
1459f5a469b9SMatthew G. Knepley 
1460f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1461f5a469b9SMatthew G. Knepley @*/
1462f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1463f5a469b9SMatthew G. Knepley {
1464f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1465f5a469b9SMatthew G. Knepley   PetscInt       csize;
1466f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1467f5a469b9SMatthew G. Knepley 
1468f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1469f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1470f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1471f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1472f5a469b9SMatthew G. Knepley 
1473f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1474f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1475f5a469b9SMatthew G. Knepley }
1476f5a469b9SMatthew G. Knepley 
1477552f7358SJed Brown /*@C
1478eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1479552f7358SJed Brown 
1480552f7358SJed Brown   Not collective
1481552f7358SJed Brown 
1482552f7358SJed Brown   Input Parameters:
1483833c876bSVaclav Hapla + dm - The DMPlex
1484eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1485552f7358SJed Brown 
1486552f7358SJed Brown   Output Parameter:
1487552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1488552f7358SJed Brown 
1489552f7358SJed Brown   Level: beginner
1490552f7358SJed Brown 
14913813dfbdSMatthew G Knepley   Fortran Notes:
14923813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14933813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14943813dfbdSMatthew G Knepley 
14950ce7577fSVaclav Hapla .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1496552f7358SJed Brown @*/
1497552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1498552f7358SJed Brown {
1499552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1500552f7358SJed Brown   PetscInt       off;
1501552f7358SJed Brown   PetscErrorCode ierr;
1502552f7358SJed Brown 
1503552f7358SJed Brown   PetscFunctionBegin;
1504552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1505552f7358SJed Brown   PetscValidPointer(cone, 3);
1506552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1507552f7358SJed Brown   *cone = &mesh->cones[off];
1508552f7358SJed Brown   PetscFunctionReturn(0);
1509552f7358SJed Brown }
1510552f7358SJed Brown 
15110ce7577fSVaclav Hapla /*@C
15120ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
15130ce7577fSVaclav Hapla 
15140ce7577fSVaclav Hapla   Not collective
15150ce7577fSVaclav Hapla 
15160ce7577fSVaclav Hapla   Input Parameters:
15170ce7577fSVaclav Hapla + dm - The DMPlex
15180ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
15190ce7577fSVaclav Hapla 
15200ce7577fSVaclav Hapla   Output Parameter:
15210ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
15220ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
15230ce7577fSVaclav Hapla 
15240ce7577fSVaclav Hapla   Level: intermediate
15250ce7577fSVaclav Hapla 
1526d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
15270ce7577fSVaclav Hapla @*/
15280ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
15290ce7577fSVaclav Hapla {
15300ce7577fSVaclav Hapla   PetscSection        cs, newcs;
15310ce7577fSVaclav Hapla   PetscInt            *cones;
15320ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
15330ce7577fSVaclav Hapla   PetscInt            n;
15340ce7577fSVaclav Hapla   PetscErrorCode      ierr;
15350ce7577fSVaclav Hapla 
15360ce7577fSVaclav Hapla   PetscFunctionBegin;
15370ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
15380ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
15390ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
15400ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
15410ce7577fSVaclav Hapla   if (pCones) {
15420ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
15430ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
15440ce7577fSVaclav Hapla   }
15450ce7577fSVaclav Hapla   PetscFunctionReturn(0);
15460ce7577fSVaclav Hapla }
15470ce7577fSVaclav Hapla 
1548d4636a37SVaclav Hapla static PetscErrorCode DMPlexGetConeRecursive_Private(DM dm, PetscInt *n_inout, const PetscInt points[], PetscInt *offset_inout, PetscInt buf[])
1549d4636a37SVaclav Hapla {
1550d4636a37SVaclav Hapla   PetscInt p, n, cn, i;
1551d4636a37SVaclav Hapla   const PetscInt *cone;
1552d4636a37SVaclav Hapla   PetscErrorCode ierr;
1553d4636a37SVaclav Hapla 
1554d4636a37SVaclav Hapla   PetscFunctionBegin;
1555d4636a37SVaclav Hapla   n = *n_inout;
1556d4636a37SVaclav Hapla   *n_inout = 0;
1557d4636a37SVaclav Hapla   for (i=0; i<n; i++) {
1558d4636a37SVaclav Hapla     p = points[i];
1559d4636a37SVaclav Hapla     ierr = DMPlexGetConeSize(dm, p, &cn);CHKERRQ(ierr);
1560d4636a37SVaclav Hapla     if (!cn) {
1561d4636a37SVaclav Hapla       cn = 1;
1562d4636a37SVaclav Hapla       if (buf) {
1563d4636a37SVaclav Hapla         buf[*offset_inout] = p;
1564d4636a37SVaclav Hapla         ++(*offset_inout);
1565d4636a37SVaclav Hapla       }
1566d4636a37SVaclav Hapla     } else {
1567d4636a37SVaclav Hapla       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
1568d4636a37SVaclav Hapla       ierr = DMPlexGetConeRecursive_Private(dm, &cn, cone, offset_inout, buf);CHKERRQ(ierr);
1569d4636a37SVaclav Hapla     }
1570d4636a37SVaclav Hapla     *n_inout += cn;
1571d4636a37SVaclav Hapla   }
1572d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1573d4636a37SVaclav Hapla }
1574d4636a37SVaclav Hapla 
1575d4636a37SVaclav Hapla /*@C
1576d4636a37SVaclav Hapla   DMPlexGetConeRecursive - Like DMPlexGetConeTuple() but recursive, i.e. each cone point is expanded into a set of its own cone points until a vertex (DAG point with no cone) is reached.
1577d4636a37SVaclav Hapla 
1578d4636a37SVaclav Hapla   Not collective
1579d4636a37SVaclav Hapla 
1580d4636a37SVaclav Hapla   Input Parameters:
1581d4636a37SVaclav Hapla + dm - The DMPlex
1582d4636a37SVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
1583d4636a37SVaclav Hapla 
1584d4636a37SVaclav Hapla   Output Parameter:
1585d4636a37SVaclav Hapla . pCones - An array of recursively expanded cones, i.e. containing only vertices, and each of them can be present multiple times
1586d4636a37SVaclav Hapla 
1587d4636a37SVaclav Hapla   Level: advanced
1588d4636a37SVaclav Hapla 
1589d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple()
1590d4636a37SVaclav Hapla @*/
1591d4636a37SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS p, IS *pCones)
1592d4636a37SVaclav Hapla {
1593d4636a37SVaclav Hapla   const PetscInt      *arr=NULL;
1594d4636a37SVaclav Hapla   PetscInt            *cpoints=NULL;
1595d4636a37SVaclav Hapla   PetscInt            n, cn;
1596d4636a37SVaclav Hapla   PetscInt            zero;
1597d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1598d4636a37SVaclav Hapla 
1599d4636a37SVaclav Hapla   PetscFunctionBegin;
1600d4636a37SVaclav Hapla   ierr = ISGetLocalSize(p, &n);CHKERRQ(ierr);
1601d4636a37SVaclav Hapla   ierr = ISGetIndices(p, &arr);CHKERRQ(ierr);
1602d4636a37SVaclav Hapla   zero = 0;
1603d4636a37SVaclav Hapla   /* first figure out the total number of returned points */
1604d4636a37SVaclav Hapla   cn = n;
1605d4636a37SVaclav Hapla   ierr = DMPlexGetConeRecursive_Private(dm, &cn, arr, &zero, NULL);CHKERRQ(ierr);
1606d4636a37SVaclav Hapla   ierr = PetscMalloc1(cn, &cpoints);CHKERRQ(ierr);
1607d4636a37SVaclav Hapla   /* now get recursive cones themselves */
1608d4636a37SVaclav Hapla   ierr = DMPlexGetConeRecursive_Private(dm, &n, arr, &zero, cpoints);CHKERRQ(ierr);
1609d4636a37SVaclav Hapla   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, cpoints, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
1610d4636a37SVaclav Hapla   ierr = ISRestoreIndices(p, &arr);CHKERRQ(ierr);
1611d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1612d4636a37SVaclav Hapla }
1613d4636a37SVaclav Hapla 
1614552f7358SJed Brown /*@
161592371b87SBarry 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
1616552f7358SJed Brown 
1617552f7358SJed Brown   Not collective
1618552f7358SJed Brown 
1619552f7358SJed Brown   Input Parameters:
1620552f7358SJed Brown + mesh - The DMPlex
1621eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1622552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1623552f7358SJed Brown 
1624552f7358SJed Brown   Output Parameter:
1625552f7358SJed Brown 
1626552f7358SJed Brown   Note:
1627552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1628552f7358SJed Brown 
162992371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
163092371b87SBarry Smith 
1631552f7358SJed Brown   Level: beginner
1632552f7358SJed Brown 
163392371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1634552f7358SJed Brown @*/
1635552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1636552f7358SJed Brown {
1637552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1638552f7358SJed Brown   PetscInt       pStart, pEnd;
1639552f7358SJed Brown   PetscInt       dof, off, c;
1640552f7358SJed Brown   PetscErrorCode ierr;
1641552f7358SJed Brown 
1642552f7358SJed Brown   PetscFunctionBegin;
1643552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1644552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1645552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1646552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1647552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
164882f516ccSBarry 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);
1649552f7358SJed Brown   for (c = 0; c < dof; ++c) {
165082f516ccSBarry 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);
1651552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1652552f7358SJed Brown   }
1653552f7358SJed Brown   PetscFunctionReturn(0);
1654552f7358SJed Brown }
1655552f7358SJed Brown 
1656552f7358SJed Brown /*@C
1657eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1658552f7358SJed Brown 
1659552f7358SJed Brown   Not collective
1660552f7358SJed Brown 
1661552f7358SJed Brown   Input Parameters:
1662552f7358SJed Brown + mesh - The DMPlex
1663eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1664552f7358SJed Brown 
1665552f7358SJed Brown   Output Parameter:
1666552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1667552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1668552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1669552f7358SJed Brown                     the index of the cone point on which to start.
1670552f7358SJed Brown 
1671552f7358SJed Brown   Level: beginner
1672552f7358SJed Brown 
16733813dfbdSMatthew G Knepley   Fortran Notes:
16743813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16753813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16763813dfbdSMatthew G Knepley 
16773813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1678552f7358SJed Brown 
1679552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1680552f7358SJed Brown @*/
1681552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1682552f7358SJed Brown {
1683552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1684552f7358SJed Brown   PetscInt       off;
1685552f7358SJed Brown   PetscErrorCode ierr;
1686552f7358SJed Brown 
1687552f7358SJed Brown   PetscFunctionBegin;
1688552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1689552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1690552f7358SJed Brown   {
1691552f7358SJed Brown     PetscInt dof;
1692552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1693552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1694552f7358SJed Brown   }
1695552f7358SJed Brown #endif
1696552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
16970d644c17SKarl Rupp 
1698552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1699552f7358SJed Brown   PetscFunctionReturn(0);
1700552f7358SJed Brown }
1701552f7358SJed Brown 
1702552f7358SJed Brown /*@
1703eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1704552f7358SJed Brown 
1705552f7358SJed Brown   Not collective
1706552f7358SJed Brown 
1707552f7358SJed Brown   Input Parameters:
1708552f7358SJed Brown + mesh - The DMPlex
1709eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1710552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1711552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1712552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1713552f7358SJed Brown                     the index of the cone point on which to start.
1714552f7358SJed Brown 
1715552f7358SJed Brown   Output Parameter:
1716552f7358SJed Brown 
1717552f7358SJed Brown   Note:
1718552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1719552f7358SJed Brown 
1720552f7358SJed Brown   Level: beginner
1721552f7358SJed Brown 
1722552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1723552f7358SJed Brown @*/
1724552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1725552f7358SJed Brown {
1726552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1727552f7358SJed Brown   PetscInt       pStart, pEnd;
1728552f7358SJed Brown   PetscInt       dof, off, c;
1729552f7358SJed Brown   PetscErrorCode ierr;
1730552f7358SJed Brown 
1731552f7358SJed Brown   PetscFunctionBegin;
1732552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1733552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1734552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1735552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1736552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
173782f516ccSBarry 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);
1738552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1739552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1740552f7358SJed Brown 
1741552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
174282f516ccSBarry 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);
1743552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1744552f7358SJed Brown   }
1745552f7358SJed Brown   PetscFunctionReturn(0);
1746552f7358SJed Brown }
1747552f7358SJed Brown 
17487cd05799SMatthew G. Knepley /*@
1749eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
17507cd05799SMatthew G. Knepley 
17517cd05799SMatthew G. Knepley   Not collective
17527cd05799SMatthew G. Knepley 
17537cd05799SMatthew G. Knepley   Input Parameters:
17547cd05799SMatthew G. Knepley + mesh - The DMPlex
1755eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17567cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
17577cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
17587cd05799SMatthew G. Knepley 
17597cd05799SMatthew G. Knepley   Level: beginner
17607cd05799SMatthew G. Knepley 
17617cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17627cd05799SMatthew G. Knepley @*/
1763552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1764552f7358SJed Brown {
1765552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1766552f7358SJed Brown   PetscInt       pStart, pEnd;
1767552f7358SJed Brown   PetscInt       dof, off;
1768552f7358SJed Brown   PetscErrorCode ierr;
1769552f7358SJed Brown 
1770552f7358SJed Brown   PetscFunctionBegin;
1771552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1772552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
177382f516ccSBarry 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);
177482f516ccSBarry 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);
177577c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
177677c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
177777c88f5bSMatthew 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);
1778552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1779552f7358SJed Brown   PetscFunctionReturn(0);
1780552f7358SJed Brown }
1781552f7358SJed Brown 
17827cd05799SMatthew G. Knepley /*@
1783eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
17847cd05799SMatthew G. Knepley 
17857cd05799SMatthew G. Knepley   Not collective
17867cd05799SMatthew G. Knepley 
17877cd05799SMatthew G. Knepley   Input Parameters:
17887cd05799SMatthew G. Knepley + mesh - The DMPlex
1789eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17907cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
17917cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
17927cd05799SMatthew G. Knepley 
17937cd05799SMatthew G. Knepley   Level: beginner
17947cd05799SMatthew G. Knepley 
17957cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17967cd05799SMatthew G. Knepley @*/
179777c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
179877c88f5bSMatthew G Knepley {
179977c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
180077c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
180177c88f5bSMatthew G Knepley   PetscInt       dof, off;
180277c88f5bSMatthew G Knepley   PetscErrorCode ierr;
180377c88f5bSMatthew G Knepley 
180477c88f5bSMatthew G Knepley   PetscFunctionBegin;
180577c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
180677c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
180777c88f5bSMatthew 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);
180877c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
180977c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
181077c88f5bSMatthew 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);
181177c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
181277c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
181377c88f5bSMatthew G Knepley }
181477c88f5bSMatthew G Knepley 
1815552f7358SJed Brown /*@
1816eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
1817552f7358SJed Brown 
1818552f7358SJed Brown   Not collective
1819552f7358SJed Brown 
1820552f7358SJed Brown   Input Parameters:
1821552f7358SJed Brown + mesh - The DMPlex
1822eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1823552f7358SJed Brown 
1824552f7358SJed Brown   Output Parameter:
1825552f7358SJed Brown . size - The support size for point p
1826552f7358SJed Brown 
1827552f7358SJed Brown   Level: beginner
1828552f7358SJed Brown 
1829552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1830552f7358SJed Brown @*/
1831552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1832552f7358SJed Brown {
1833552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1834552f7358SJed Brown   PetscErrorCode ierr;
1835552f7358SJed Brown 
1836552f7358SJed Brown   PetscFunctionBegin;
1837552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1838552f7358SJed Brown   PetscValidPointer(size, 3);
1839552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1840552f7358SJed Brown   PetscFunctionReturn(0);
1841552f7358SJed Brown }
1842552f7358SJed Brown 
1843552f7358SJed Brown /*@
1844eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
1845552f7358SJed Brown 
1846552f7358SJed Brown   Not collective
1847552f7358SJed Brown 
1848552f7358SJed Brown   Input Parameters:
1849552f7358SJed Brown + mesh - The DMPlex
1850eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1851552f7358SJed Brown - size - The support size for point p
1852552f7358SJed Brown 
1853552f7358SJed Brown   Output Parameter:
1854552f7358SJed Brown 
1855552f7358SJed Brown   Note:
1856552f7358SJed Brown   This should be called after DMPlexSetChart().
1857552f7358SJed Brown 
1858552f7358SJed Brown   Level: beginner
1859552f7358SJed Brown 
1860552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1861552f7358SJed Brown @*/
1862552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1863552f7358SJed Brown {
1864552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1865552f7358SJed Brown   PetscErrorCode ierr;
1866552f7358SJed Brown 
1867552f7358SJed Brown   PetscFunctionBegin;
1868552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1869552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
18700d644c17SKarl Rupp 
1871552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1872552f7358SJed Brown   PetscFunctionReturn(0);
1873552f7358SJed Brown }
1874552f7358SJed Brown 
1875552f7358SJed Brown /*@C
1876eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
1877552f7358SJed Brown 
1878552f7358SJed Brown   Not collective
1879552f7358SJed Brown 
1880552f7358SJed Brown   Input Parameters:
1881552f7358SJed Brown + mesh - The DMPlex
1882eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1883552f7358SJed Brown 
1884552f7358SJed Brown   Output Parameter:
1885552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1886552f7358SJed Brown 
1887552f7358SJed Brown   Level: beginner
1888552f7358SJed Brown 
18893813dfbdSMatthew G Knepley   Fortran Notes:
18903813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18913813dfbdSMatthew G Knepley   include petsc.h90 in your code.
18923813dfbdSMatthew G Knepley 
18933813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
18943813dfbdSMatthew G Knepley 
1895552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1896552f7358SJed Brown @*/
1897552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1898552f7358SJed Brown {
1899552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1900552f7358SJed Brown   PetscInt       off;
1901552f7358SJed Brown   PetscErrorCode ierr;
1902552f7358SJed Brown 
1903552f7358SJed Brown   PetscFunctionBegin;
1904552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1905552f7358SJed Brown   PetscValidPointer(support, 3);
1906552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1907552f7358SJed Brown   *support = &mesh->supports[off];
1908552f7358SJed Brown   PetscFunctionReturn(0);
1909552f7358SJed Brown }
1910552f7358SJed Brown 
1911552f7358SJed Brown /*@
191292371b87SBarry 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
1913552f7358SJed Brown 
1914552f7358SJed Brown   Not collective
1915552f7358SJed Brown 
1916552f7358SJed Brown   Input Parameters:
1917552f7358SJed Brown + mesh - The DMPlex
1918eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
191992371b87SBarry Smith - support - An array of points which are on the out-edges for point p
1920552f7358SJed Brown 
1921552f7358SJed Brown   Output Parameter:
1922552f7358SJed Brown 
1923552f7358SJed Brown   Note:
1924552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1925552f7358SJed Brown 
1926552f7358SJed Brown   Level: beginner
1927552f7358SJed Brown 
192892371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1929552f7358SJed Brown @*/
1930552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1931552f7358SJed Brown {
1932552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1933552f7358SJed Brown   PetscInt       pStart, pEnd;
1934552f7358SJed Brown   PetscInt       dof, off, c;
1935552f7358SJed Brown   PetscErrorCode ierr;
1936552f7358SJed Brown 
1937552f7358SJed Brown   PetscFunctionBegin;
1938552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1939552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1940552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1941552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1942552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
194382f516ccSBarry 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);
1944552f7358SJed Brown   for (c = 0; c < dof; ++c) {
194582f516ccSBarry 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);
1946552f7358SJed Brown     mesh->supports[off+c] = support[c];
1947552f7358SJed Brown   }
1948552f7358SJed Brown   PetscFunctionReturn(0);
1949552f7358SJed Brown }
1950552f7358SJed Brown 
19517cd05799SMatthew G. Knepley /*@
1952eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
19537cd05799SMatthew G. Knepley 
19547cd05799SMatthew G. Knepley   Not collective
19557cd05799SMatthew G. Knepley 
19567cd05799SMatthew G. Knepley   Input Parameters:
19577cd05799SMatthew G. Knepley + mesh - The DMPlex
1958eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
19597cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
19607cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
19617cd05799SMatthew G. Knepley 
19627cd05799SMatthew G. Knepley   Level: beginner
19637cd05799SMatthew G. Knepley 
19647cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
19657cd05799SMatthew G. Knepley @*/
1966552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1967552f7358SJed Brown {
1968552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1969552f7358SJed Brown   PetscInt       pStart, pEnd;
1970552f7358SJed Brown   PetscInt       dof, off;
1971552f7358SJed Brown   PetscErrorCode ierr;
1972552f7358SJed Brown 
1973552f7358SJed Brown   PetscFunctionBegin;
1974552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1975552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1976552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1977552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
197882f516ccSBarry 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);
197982f516ccSBarry 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);
198082f516ccSBarry 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);
1981552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1982552f7358SJed Brown   PetscFunctionReturn(0);
1983552f7358SJed Brown }
1984552f7358SJed Brown 
1985552f7358SJed Brown /*@C
1986eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
1987552f7358SJed Brown 
1988552f7358SJed Brown   Not collective
1989552f7358SJed Brown 
1990552f7358SJed Brown   Input Parameters:
1991552f7358SJed Brown + mesh - The DMPlex
1992eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1993552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
19940298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1995552f7358SJed Brown 
1996552f7358SJed Brown   Output Parameters:
1997552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1998552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1999552f7358SJed Brown 
2000552f7358SJed Brown   Note:
20010298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2002552f7358SJed Brown 
20033813dfbdSMatthew G Knepley   Fortran Notes:
20043813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20053813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20063813dfbdSMatthew G Knepley 
20073813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
20083813dfbdSMatthew G Knepley 
2009552f7358SJed Brown   Level: beginner
2010552f7358SJed Brown 
2011552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2012552f7358SJed Brown @*/
2013552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2014552f7358SJed Brown {
2015552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2016552f7358SJed Brown   PetscInt       *closure, *fifo;
20170298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2018552f7358SJed Brown   PetscInt        tmpSize, t;
2019552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2020552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2021552f7358SJed Brown   PetscErrorCode  ierr;
2022552f7358SJed Brown 
2023552f7358SJed Brown   PetscFunctionBegin;
2024552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2025552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2026552f7358SJed Brown   /* This is only 1-level */
2027552f7358SJed Brown   if (useCone) {
2028552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2029552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2030552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2031552f7358SJed Brown   } else {
2032552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2033552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2034552f7358SJed Brown   }
2035bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2036bfbcdd7aSMatthew G. Knepley     if (*points) {
2037bfbcdd7aSMatthew G. Knepley       closure = *points;
2038bfbcdd7aSMatthew G. Knepley     } else {
2039bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
204069291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2041bfbcdd7aSMatthew G. Knepley     }
2042bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2043bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2044bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2045bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2046bfbcdd7aSMatthew G. Knepley     }
2047bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2048bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2049bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2050bfbcdd7aSMatthew G. Knepley   }
205124c766afSToby Isaac   {
205224c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
205324c766afSToby Isaac 
205424c766afSToby Isaac     c = mesh->maxConeSize;
205524c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
205624c766afSToby Isaac     s = mesh->maxSupportSize;
205724c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
205824c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
205924c766afSToby Isaac   }
206069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2061bfbcdd7aSMatthew G. Knepley   if (*points) {
2062bfbcdd7aSMatthew G. Knepley     closure = *points;
2063bfbcdd7aSMatthew G. Knepley   } else {
206469291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2065bfbcdd7aSMatthew G. Knepley   }
2066bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2067552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2068552f7358SJed Brown     const PetscInt cp = tmp[t];
2069552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2070552f7358SJed Brown 
2071552f7358SJed Brown     closure[closureSize]   = cp;
2072552f7358SJed Brown     closure[closureSize+1] = co;
2073552f7358SJed Brown     fifo[fifoSize]         = cp;
2074552f7358SJed Brown     fifo[fifoSize+1]       = co;
2075552f7358SJed Brown   }
2076bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2077552f7358SJed Brown   while (fifoSize - fifoStart) {
2078552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2079552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2080552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2081552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2082552f7358SJed Brown 
2083552f7358SJed Brown     if (useCone) {
2084552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2085552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2086552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2087552f7358SJed Brown     } else {
2088552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2089552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
20900298fd71SBarry Smith       tmpO = NULL;
2091552f7358SJed Brown     }
2092552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2093552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2094552f7358SJed Brown       const PetscInt cp = tmp[i];
20952e1b13c2SMatthew 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. */
20962e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
20972e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
20982e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2099552f7358SJed Brown       PetscInt       c;
2100552f7358SJed Brown 
21012e1b13c2SMatthew G. Knepley       if (rev) {
21022e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
21032e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
21042e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
21052e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
21062e1b13c2SMatthew G. Knepley       }
2107552f7358SJed Brown       /* Check for duplicate */
2108552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2109552f7358SJed Brown         if (closure[c] == cp) break;
2110552f7358SJed Brown       }
2111552f7358SJed Brown       if (c == closureSize) {
2112552f7358SJed Brown         closure[closureSize]   = cp;
2113552f7358SJed Brown         closure[closureSize+1] = co;
2114552f7358SJed Brown         fifo[fifoSize]         = cp;
2115552f7358SJed Brown         fifo[fifoSize+1]       = co;
2116552f7358SJed Brown         closureSize           += 2;
2117552f7358SJed Brown         fifoSize              += 2;
2118552f7358SJed Brown       }
2119552f7358SJed Brown     }
2120552f7358SJed Brown     fifoStart += 2;
2121552f7358SJed Brown   }
2122552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2123552f7358SJed Brown   if (points)    *points    = closure;
212469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2125552f7358SJed Brown   PetscFunctionReturn(0);
2126552f7358SJed Brown }
2127552f7358SJed Brown 
21289bf0dad6SMatthew G. Knepley /*@C
2129eaf898f9SPatrick 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
21309bf0dad6SMatthew G. Knepley 
21319bf0dad6SMatthew G. Knepley   Not collective
21329bf0dad6SMatthew G. Knepley 
21339bf0dad6SMatthew G. Knepley   Input Parameters:
21349bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2135eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
21369bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
21379bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
21389bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
21399bf0dad6SMatthew G. Knepley 
21409bf0dad6SMatthew G. Knepley   Output Parameters:
21419bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
21429bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
21439bf0dad6SMatthew G. Knepley 
21449bf0dad6SMatthew G. Knepley   Note:
21459bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
21469bf0dad6SMatthew G. Knepley 
21479bf0dad6SMatthew G. Knepley   Fortran Notes:
21489bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21499bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
21509bf0dad6SMatthew G. Knepley 
21519bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
21529bf0dad6SMatthew G. Knepley 
21539bf0dad6SMatthew G. Knepley   Level: beginner
21549bf0dad6SMatthew G. Knepley 
21559bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
21569bf0dad6SMatthew G. Knepley @*/
21579bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
21589bf0dad6SMatthew G. Knepley {
21599bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
21609bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
21619bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
21629bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
21639bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
21649bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
21659bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
21669bf0dad6SMatthew G. Knepley 
21679bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
21689bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21699bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
21709bf0dad6SMatthew G. Knepley   /* This is only 1-level */
21719bf0dad6SMatthew G. Knepley   if (useCone) {
21729bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
21739bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
21749bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
21759bf0dad6SMatthew G. Knepley   } else {
21769bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
21779bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
21789bf0dad6SMatthew G. Knepley   }
21799bf0dad6SMatthew G. Knepley   if (depth == 1) {
21809bf0dad6SMatthew G. Knepley     if (*points) {
21819bf0dad6SMatthew G. Knepley       closure = *points;
21829bf0dad6SMatthew G. Knepley     } else {
21839bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
218469291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
21859bf0dad6SMatthew G. Knepley     }
21869bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
21879bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
21889bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
21899bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
21909bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
21919bf0dad6SMatthew G. Knepley     }
21929bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
21939bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
21949bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
21959bf0dad6SMatthew G. Knepley   }
219624c766afSToby Isaac   {
219724c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
219824c766afSToby Isaac 
219924c766afSToby Isaac     c = mesh->maxConeSize;
220024c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
220124c766afSToby Isaac     s = mesh->maxSupportSize;
220224c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
220324c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
220424c766afSToby Isaac   }
220569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
22069bf0dad6SMatthew G. Knepley   if (*points) {
22079bf0dad6SMatthew G. Knepley     closure = *points;
22089bf0dad6SMatthew G. Knepley   } else {
220969291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
22109bf0dad6SMatthew G. Knepley   }
22119bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
22129bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
22139bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
22149bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
22159bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
22169bf0dad6SMatthew G. Knepley 
22179bf0dad6SMatthew G. Knepley     if (ornt < 0) {
22189bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
22199bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
222086b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
22219bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
22229bf0dad6SMatthew G. Knepley     }
22239bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
22249bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
22259bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
22269bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
22279bf0dad6SMatthew G. Knepley   }
22289bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
22299bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
22309bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
22319bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
22329bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
22339bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
22349bf0dad6SMatthew G. Knepley 
22359bf0dad6SMatthew G. Knepley     if (useCone) {
22369bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
22379bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
22389bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
22399bf0dad6SMatthew G. Knepley     } else {
22409bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
22419bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
22429bf0dad6SMatthew G. Knepley       tmpO = NULL;
22439bf0dad6SMatthew G. Knepley     }
22449bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
22459bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
22469bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
22479bf0dad6SMatthew 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. */
22489bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
22499bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
22509bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
22519bf0dad6SMatthew G. Knepley       PetscInt       c;
22529bf0dad6SMatthew G. Knepley 
22539bf0dad6SMatthew G. Knepley       if (rev) {
22549bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
22559bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
22569bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
22579bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
22589bf0dad6SMatthew G. Knepley       }
22599bf0dad6SMatthew G. Knepley       /* Check for duplicate */
22609bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
22619bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
22629bf0dad6SMatthew G. Knepley       }
22639bf0dad6SMatthew G. Knepley       if (c == closureSize) {
22649bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
22659bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
22669bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
22679bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
22689bf0dad6SMatthew G. Knepley         closureSize           += 2;
22699bf0dad6SMatthew G. Knepley         fifoSize              += 2;
22709bf0dad6SMatthew G. Knepley       }
22719bf0dad6SMatthew G. Knepley     }
22729bf0dad6SMatthew G. Knepley     fifoStart += 2;
22739bf0dad6SMatthew G. Knepley   }
22749bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
22759bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
227669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
22779bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
22789bf0dad6SMatthew G. Knepley }
22799bf0dad6SMatthew G. Knepley 
2280552f7358SJed Brown /*@C
2281eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2282552f7358SJed Brown 
2283552f7358SJed Brown   Not collective
2284552f7358SJed Brown 
2285552f7358SJed Brown   Input Parameters:
2286552f7358SJed Brown + mesh - The DMPlex
2287eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2288552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2289e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2290e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2291552f7358SJed Brown 
2292552f7358SJed Brown   Note:
22930298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2294552f7358SJed Brown 
22953813dfbdSMatthew G Knepley   Fortran Notes:
22963813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22973813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22983813dfbdSMatthew G Knepley 
22993813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23003813dfbdSMatthew G Knepley 
2301552f7358SJed Brown   Level: beginner
2302552f7358SJed Brown 
2303552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2304552f7358SJed Brown @*/
2305552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2306552f7358SJed Brown {
2307552f7358SJed Brown   PetscErrorCode ierr;
2308552f7358SJed Brown 
2309552f7358SJed Brown   PetscFunctionBegin;
2310552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2311e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2312e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
231369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
23144ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2315552f7358SJed Brown   PetscFunctionReturn(0);
2316552f7358SJed Brown }
2317552f7358SJed Brown 
2318552f7358SJed Brown /*@
2319eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2320552f7358SJed Brown 
2321552f7358SJed Brown   Not collective
2322552f7358SJed Brown 
2323552f7358SJed Brown   Input Parameter:
2324552f7358SJed Brown . mesh - The DMPlex
2325552f7358SJed Brown 
2326552f7358SJed Brown   Output Parameters:
2327552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2328552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2329552f7358SJed Brown 
2330552f7358SJed Brown   Level: beginner
2331552f7358SJed Brown 
2332552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2333552f7358SJed Brown @*/
2334552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2335552f7358SJed Brown {
2336552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2337552f7358SJed Brown 
2338552f7358SJed Brown   PetscFunctionBegin;
2339552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2340552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2341552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2342552f7358SJed Brown   PetscFunctionReturn(0);
2343552f7358SJed Brown }
2344552f7358SJed Brown 
2345552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2346552f7358SJed Brown {
2347552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2348552f7358SJed Brown   PetscInt       size;
2349552f7358SJed Brown   PetscErrorCode ierr;
2350552f7358SJed Brown 
2351552f7358SJed Brown   PetscFunctionBegin;
2352552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2353552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2354552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
23551795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
23561795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2357552f7358SJed Brown   if (mesh->maxSupportSize) {
2358552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2359552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
23601795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2361552f7358SJed Brown   }
2362552f7358SJed Brown   PetscFunctionReturn(0);
2363552f7358SJed Brown }
2364552f7358SJed Brown 
2365276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2366552f7358SJed Brown {
2367552f7358SJed Brown   PetscErrorCode ierr;
2368552f7358SJed Brown 
2369552f7358SJed Brown   PetscFunctionBegin;
23704d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
23714d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2372c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2373736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2374f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2375f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2376f94b4a02SBlaise Bourdin 
23773dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
23783dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2379e87a4003SBarry Smith     ierr = DMGetSection((*subdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
2380f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2381f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2382f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2383f94b4a02SBlaise Bourdin 
2384f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2385c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2386f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2387f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2388f94b4a02SBlaise Bourdin   }
2389552f7358SJed Brown   PetscFunctionReturn(0);
2390552f7358SJed Brown }
2391552f7358SJed Brown 
23922adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
23932adcc780SMatthew G. Knepley {
23942adcc780SMatthew G. Knepley   PetscErrorCode ierr;
23953dcd263cSBlaise Bourdin   PetscInt       i = 0;
23962adcc780SMatthew G. Knepley 
23972adcc780SMatthew G. Knepley   PetscFunctionBegin;
2398435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
23992adcc780SMatthew G. Knepley   ierr = DMCreateSuperDM_Section_Private(dms, len, is, superdm);CHKERRQ(ierr);
2400c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
24013dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
24023dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
24033dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
24043dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
24053dcd263cSBlaise Bourdin 
24063dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
24073dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2408c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
2409cf0b7c11SKarl Rupp       ierr = DMGetSection((*superdm), &section);CHKERRQ(ierr);
24103dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
24113dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
24123dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
24133dcd263cSBlaise Bourdin 
24143dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2415c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
24163dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
24173dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
24183dcd263cSBlaise Bourdin       break;
24193dcd263cSBlaise Bourdin     }
24203dcd263cSBlaise Bourdin   }
24212adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
24222adcc780SMatthew G. Knepley }
24232adcc780SMatthew G. Knepley 
2424552f7358SJed Brown /*@
2425eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2426552f7358SJed Brown 
2427552f7358SJed Brown   Not collective
2428552f7358SJed Brown 
2429552f7358SJed Brown   Input Parameter:
2430552f7358SJed Brown . mesh - The DMPlex
2431552f7358SJed Brown 
2432552f7358SJed Brown   Output Parameter:
2433552f7358SJed Brown 
2434552f7358SJed Brown   Note:
2435552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2436552f7358SJed Brown 
2437552f7358SJed Brown   Level: beginner
2438552f7358SJed Brown 
2439552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2440552f7358SJed Brown @*/
2441552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2442552f7358SJed Brown {
2443552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2444552f7358SJed Brown   PetscInt      *offsets;
2445552f7358SJed Brown   PetscInt       supportSize;
2446552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2447552f7358SJed Brown   PetscErrorCode ierr;
2448552f7358SJed Brown 
2449552f7358SJed Brown   PetscFunctionBegin;
2450552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
245182f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2452552f7358SJed Brown   /* Calculate support sizes */
2453552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2454552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2455552f7358SJed Brown     PetscInt dof, off, c;
2456552f7358SJed Brown 
2457552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2458552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2459552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2460552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2461552f7358SJed Brown     }
2462552f7358SJed Brown   }
2463552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2464552f7358SJed Brown     PetscInt dof;
2465552f7358SJed Brown 
2466552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
24670d644c17SKarl Rupp 
2468552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2469552f7358SJed Brown   }
2470552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2471552f7358SJed Brown   /* Calculate supports */
2472552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
24731795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
24741795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2475552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2476552f7358SJed Brown     PetscInt dof, off, c;
2477552f7358SJed Brown 
2478552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2479552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2480552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2481552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2482552f7358SJed Brown       PetscInt       offS;
2483552f7358SJed Brown 
2484552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
24850d644c17SKarl Rupp 
2486552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2487552f7358SJed Brown       ++offsets[q];
2488552f7358SJed Brown     }
2489552f7358SJed Brown   }
2490552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2491552f7358SJed Brown   PetscFunctionReturn(0);
2492552f7358SJed Brown }
2493552f7358SJed Brown 
24947d5acc75SStefano Zampini static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);
24957d5acc75SStefano Zampini 
2496552f7358SJed Brown /*@
2497eaf898f9SPatrick Sanan   DMPlexStratify - The DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2498eaf898f9SPatrick Sanan   can be illustrated by a Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2499552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2500552f7358SJed Brown   the DAG.
2501552f7358SJed Brown 
2502bf4602e4SToby Isaac   Collective on dm
2503552f7358SJed Brown 
2504552f7358SJed Brown   Input Parameter:
2505552f7358SJed Brown . mesh - The DMPlex
2506552f7358SJed Brown 
2507552f7358SJed Brown   Output Parameter:
2508552f7358SJed Brown 
2509552f7358SJed Brown   Notes:
2510150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2511150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2512c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2513150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2514552f7358SJed Brown 
2515150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2516552f7358SJed Brown 
2517552f7358SJed Brown   Level: beginner
2518552f7358SJed Brown 
2519552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2520552f7358SJed Brown @*/
2521552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2522552f7358SJed Brown {
2523df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2524aa50250dSMatthew G. Knepley   DMLabel        label;
2525552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2526552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
25277d5acc75SStefano Zampini   PetscInt       cMax, fMax, eMax, vMax;
2528552f7358SJed Brown   PetscErrorCode ierr;
2529552f7358SJed Brown 
2530552f7358SJed Brown   PetscFunctionBegin;
2531552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2532552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2533552f7358SJed Brown   /* Calculate depth */
2534aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2535c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2536aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2537552f7358SJed Brown   /* Initialize roots and count leaves */
2538552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2539552f7358SJed Brown     PetscInt coneSize, supportSize;
2540552f7358SJed Brown 
2541552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2542552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2543552f7358SJed Brown     if (!coneSize && supportSize) {
2544552f7358SJed Brown       ++numRoots;
2545aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2546552f7358SJed Brown     } else if (!supportSize && coneSize) {
2547552f7358SJed Brown       ++numLeaves;
2548552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2549552f7358SJed Brown       /* Isolated points */
2550aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2551552f7358SJed Brown     }
2552552f7358SJed Brown   }
2553552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2554552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2555552f7358SJed Brown       PetscInt coneSize, supportSize;
2556552f7358SJed Brown 
2557552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2558552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2559552f7358SJed Brown       if (!supportSize && coneSize) {
2560aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2561552f7358SJed Brown       }
2562552f7358SJed Brown     }
2563552f7358SJed Brown   } else {
256474ef644bSMatthew G. Knepley     IS       pointIS;
256574ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2566552f7358SJed Brown 
256774ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
256874ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
256974ef644bSMatthew G. Knepley     while (numPoints) {
257074ef644bSMatthew G. Knepley       const PetscInt *points;
257174ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
257274ef644bSMatthew G. Knepley 
257374ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
257474ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
257574ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
257674ef644bSMatthew G. Knepley         const PetscInt *support;
257774ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
257874ef644bSMatthew G. Knepley 
257974ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
258074ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
258174ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
258274ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2583552f7358SJed Brown         }
2584552f7358SJed Brown       }
25855edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
258674ef644bSMatthew G. Knepley       ++level;
258774ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
258874ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
258974ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
259074ef644bSMatthew G. Knepley       else         {numPoints = 0;}
259174ef644bSMatthew G. Knepley     }
259274ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
259374ef644bSMatthew G. Knepley   }
2594bf4602e4SToby Isaac   { /* just in case there is an empty process */
2595bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2596bf4602e4SToby Isaac 
2597bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
25984de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
25994de306b1SToby Isaac       IS pointIS;
26004de306b1SToby Isaac 
26014de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
26024de306b1SToby Isaac       if (pointIS) {
26034de306b1SToby Isaac         PetscInt  min, max, numPoints;
26044de306b1SToby Isaac         PetscInt  start;
26054de306b1SToby Isaac         PetscBool contig;
26064de306b1SToby Isaac 
26074de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
26084de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
26094de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
26104de306b1SToby Isaac         if (start == 0 && contig) {
26114de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
26124de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
26134de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
26144de306b1SToby Isaac         }
26154de306b1SToby Isaac       }
26164de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
26174de306b1SToby Isaac     }
26186d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2619bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2620367003a6SStefano Zampini       ierr = DMLabelAddStratum(label,v);CHKERRQ(ierr);
2621bf4602e4SToby Isaac     }
2622bf4602e4SToby Isaac   }
2623d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
26247d5acc75SStefano Zampini 
26257d5acc75SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
26267d5acc75SStefano Zampini   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
26277d5acc75SStefano Zampini     DMLabel  dimLabel;
26287d5acc75SStefano Zampini     PetscInt dim;
26297d5acc75SStefano Zampini 
26307d5acc75SStefano Zampini     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
26317d5acc75SStefano Zampini     ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
26327d5acc75SStefano Zampini     if (!dimLabel) {
26337d5acc75SStefano Zampini       ierr = DMCreateLabel(dm, "dim");CHKERRQ(ierr);
26347d5acc75SStefano Zampini       ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
26357d5acc75SStefano Zampini     }
26367d5acc75SStefano Zampini     if (cMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);CHKERRQ(ierr);}
26377d5acc75SStefano Zampini     if (fMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);CHKERRQ(ierr);}
26387d5acc75SStefano Zampini     if (eMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);CHKERRQ(ierr);}
26397d5acc75SStefano Zampini     if (vMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);CHKERRQ(ierr);}
26407d5acc75SStefano Zampini   }
2641552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2642552f7358SJed Brown   PetscFunctionReturn(0);
2643552f7358SJed Brown }
2644552f7358SJed Brown 
2645552f7358SJed Brown /*@C
2646552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2647552f7358SJed Brown 
2648552f7358SJed Brown   Not Collective
2649552f7358SJed Brown 
2650552f7358SJed Brown   Input Parameters:
2651552f7358SJed Brown + dm - The DMPlex object
2652552f7358SJed Brown . numPoints - The number of input points for the join
2653552f7358SJed Brown - points - The input points
2654552f7358SJed Brown 
2655552f7358SJed Brown   Output Parameters:
2656552f7358SJed Brown + numCoveredPoints - The number of points in the join
2657552f7358SJed Brown - coveredPoints - The points in the join
2658552f7358SJed Brown 
2659552f7358SJed Brown   Level: intermediate
2660552f7358SJed Brown 
2661552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2662552f7358SJed Brown 
26633813dfbdSMatthew G Knepley   Fortran Notes:
26643813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26653813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26663813dfbdSMatthew G Knepley 
26673813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26683813dfbdSMatthew G Knepley 
2669552f7358SJed Brown .keywords: mesh
2670552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2671552f7358SJed Brown @*/
2672552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2673552f7358SJed Brown {
2674552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2675552f7358SJed Brown   PetscInt      *join[2];
2676552f7358SJed Brown   PetscInt       joinSize, i = 0;
2677552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2678552f7358SJed Brown   PetscErrorCode ierr;
2679552f7358SJed Brown 
2680552f7358SJed Brown   PetscFunctionBegin;
2681552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2682552f7358SJed Brown   PetscValidPointer(points, 2);
2683552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2684552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
268569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
268669291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2687552f7358SJed Brown   /* Copy in support of first point */
2688552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2689552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2690552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2691552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2692552f7358SJed Brown   }
2693552f7358SJed Brown   /* Check each successive support */
2694552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2695552f7358SJed Brown     PetscInt newJoinSize = 0;
2696552f7358SJed Brown 
2697552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2698552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2699552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2700552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2701552f7358SJed Brown 
2702552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2703552f7358SJed Brown         if (point == join[i][m]) {
2704552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2705552f7358SJed Brown           break;
2706552f7358SJed Brown         }
2707552f7358SJed Brown       }
2708552f7358SJed Brown     }
2709552f7358SJed Brown     joinSize = newJoinSize;
2710552f7358SJed Brown     i        = 1-i;
2711552f7358SJed Brown   }
2712552f7358SJed Brown   *numCoveredPoints = joinSize;
2713552f7358SJed Brown   *coveredPoints    = join[i];
271469291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2715552f7358SJed Brown   PetscFunctionReturn(0);
2716552f7358SJed Brown }
2717552f7358SJed Brown 
2718552f7358SJed Brown /*@C
2719552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2720552f7358SJed Brown 
2721552f7358SJed Brown   Not Collective
2722552f7358SJed Brown 
2723552f7358SJed Brown   Input Parameters:
2724552f7358SJed Brown + dm - The DMPlex object
2725552f7358SJed Brown . numPoints - The number of input points for the join
2726552f7358SJed Brown - points - The input points
2727552f7358SJed Brown 
2728552f7358SJed Brown   Output Parameters:
2729552f7358SJed Brown + numCoveredPoints - The number of points in the join
2730552f7358SJed Brown - coveredPoints - The points in the join
2731552f7358SJed Brown 
27323813dfbdSMatthew G Knepley   Fortran Notes:
27333813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27343813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27353813dfbdSMatthew G Knepley 
27363813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27373813dfbdSMatthew G Knepley 
2738552f7358SJed Brown   Level: intermediate
2739552f7358SJed Brown 
2740552f7358SJed Brown .keywords: mesh
2741552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2742552f7358SJed Brown @*/
2743552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2744552f7358SJed Brown {
2745552f7358SJed Brown   PetscErrorCode ierr;
2746552f7358SJed Brown 
2747552f7358SJed Brown   PetscFunctionBegin;
2748552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2749d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2750d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2751d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
275269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2753d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2754552f7358SJed Brown   PetscFunctionReturn(0);
2755552f7358SJed Brown }
2756552f7358SJed Brown 
2757552f7358SJed Brown /*@C
2758552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2759552f7358SJed Brown 
2760552f7358SJed Brown   Not Collective
2761552f7358SJed Brown 
2762552f7358SJed Brown   Input Parameters:
2763552f7358SJed Brown + dm - The DMPlex object
2764552f7358SJed Brown . numPoints - The number of input points for the join
2765552f7358SJed Brown - points - The input points
2766552f7358SJed Brown 
2767552f7358SJed Brown   Output Parameters:
2768552f7358SJed Brown + numCoveredPoints - The number of points in the join
2769552f7358SJed Brown - coveredPoints - The points in the join
2770552f7358SJed Brown 
27713813dfbdSMatthew G Knepley   Fortran Notes:
27723813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27733813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27743813dfbdSMatthew G Knepley 
27753813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27763813dfbdSMatthew G Knepley 
2777552f7358SJed Brown   Level: intermediate
2778552f7358SJed Brown 
2779552f7358SJed Brown .keywords: mesh
2780552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2781552f7358SJed Brown @*/
2782552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2783552f7358SJed Brown {
2784552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2785552f7358SJed Brown   PetscInt      *offsets, **closures;
2786552f7358SJed Brown   PetscInt      *join[2];
2787552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
278824c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2789552f7358SJed Brown   PetscErrorCode ierr;
2790552f7358SJed Brown 
2791552f7358SJed Brown   PetscFunctionBegin;
2792552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2793552f7358SJed Brown   PetscValidPointer(points, 2);
2794552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2795552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2796552f7358SJed Brown 
2797552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
27981795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
279969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
280024c766afSToby Isaac   ms      = mesh->maxSupportSize;
280124c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
280269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
280369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2804552f7358SJed Brown 
2805552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2806552f7358SJed Brown     PetscInt closureSize;
2807552f7358SJed Brown 
2808552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
28090d644c17SKarl Rupp 
2810552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2811552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2812552f7358SJed Brown       PetscInt pStart, pEnd, i;
2813552f7358SJed Brown 
2814552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2815552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2816552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2817552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2818552f7358SJed Brown           break;
2819552f7358SJed Brown         }
2820552f7358SJed Brown       }
2821552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2822552f7358SJed Brown     }
282382f516ccSBarry 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);
2824552f7358SJed Brown   }
2825552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2826552f7358SJed Brown     PetscInt dof;
2827552f7358SJed Brown 
2828552f7358SJed Brown     /* Copy in support of first point */
2829552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2830552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2831552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2832552f7358SJed Brown     }
2833552f7358SJed Brown     /* Check each successive cone */
2834552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2835552f7358SJed Brown       PetscInt newJoinSize = 0;
2836552f7358SJed Brown 
2837552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2838552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2839552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2840552f7358SJed Brown 
2841552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2842552f7358SJed Brown           if (point == join[i][m]) {
2843552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2844552f7358SJed Brown             break;
2845552f7358SJed Brown           }
2846552f7358SJed Brown         }
2847552f7358SJed Brown       }
2848552f7358SJed Brown       joinSize = newJoinSize;
2849552f7358SJed Brown       i        = 1-i;
2850552f7358SJed Brown     }
2851552f7358SJed Brown     if (joinSize) break;
2852552f7358SJed Brown   }
2853552f7358SJed Brown   *numCoveredPoints = joinSize;
2854552f7358SJed Brown   *coveredPoints    = join[i];
2855552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
28560298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2857552f7358SJed Brown   }
2858552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
285969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
286069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2861552f7358SJed Brown   PetscFunctionReturn(0);
2862552f7358SJed Brown }
2863552f7358SJed Brown 
2864552f7358SJed Brown /*@C
2865552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2866552f7358SJed Brown 
2867552f7358SJed Brown   Not Collective
2868552f7358SJed Brown 
2869552f7358SJed Brown   Input Parameters:
2870552f7358SJed Brown + dm - The DMPlex object
2871552f7358SJed Brown . numPoints - The number of input points for the meet
2872552f7358SJed Brown - points - The input points
2873552f7358SJed Brown 
2874552f7358SJed Brown   Output Parameters:
2875552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2876552f7358SJed Brown - coveredPoints - The points in the meet
2877552f7358SJed Brown 
2878552f7358SJed Brown   Level: intermediate
2879552f7358SJed Brown 
2880552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2881552f7358SJed Brown 
28823813dfbdSMatthew G Knepley   Fortran Notes:
28833813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
28843813dfbdSMatthew G Knepley   include petsc.h90 in your code.
28853813dfbdSMatthew G Knepley 
28863813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
28873813dfbdSMatthew G Knepley 
2888552f7358SJed Brown .keywords: mesh
2889552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2890552f7358SJed Brown @*/
2891552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2892552f7358SJed Brown {
2893552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2894552f7358SJed Brown   PetscInt      *meet[2];
2895552f7358SJed Brown   PetscInt       meetSize, i = 0;
2896552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2897552f7358SJed Brown   PetscErrorCode ierr;
2898552f7358SJed Brown 
2899552f7358SJed Brown   PetscFunctionBegin;
2900552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2901552f7358SJed Brown   PetscValidPointer(points, 2);
2902552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2903552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
290469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
290569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2906552f7358SJed Brown   /* Copy in cone of first point */
2907552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2908552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2909552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2910552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2911552f7358SJed Brown   }
2912552f7358SJed Brown   /* Check each successive cone */
2913552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2914552f7358SJed Brown     PetscInt newMeetSize = 0;
2915552f7358SJed Brown 
2916552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2917552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2918552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2919552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2920552f7358SJed Brown 
2921552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2922552f7358SJed Brown         if (point == meet[i][m]) {
2923552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2924552f7358SJed Brown           break;
2925552f7358SJed Brown         }
2926552f7358SJed Brown       }
2927552f7358SJed Brown     }
2928552f7358SJed Brown     meetSize = newMeetSize;
2929552f7358SJed Brown     i        = 1-i;
2930552f7358SJed Brown   }
2931552f7358SJed Brown   *numCoveringPoints = meetSize;
2932552f7358SJed Brown   *coveringPoints    = meet[i];
293369291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2934552f7358SJed Brown   PetscFunctionReturn(0);
2935552f7358SJed Brown }
2936552f7358SJed Brown 
2937552f7358SJed Brown /*@C
2938552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2939552f7358SJed Brown 
2940552f7358SJed Brown   Not Collective
2941552f7358SJed Brown 
2942552f7358SJed Brown   Input Parameters:
2943552f7358SJed Brown + dm - The DMPlex object
2944552f7358SJed Brown . numPoints - The number of input points for the meet
2945552f7358SJed Brown - points - The input points
2946552f7358SJed Brown 
2947552f7358SJed Brown   Output Parameters:
2948552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2949552f7358SJed Brown - coveredPoints - The points in the meet
2950552f7358SJed Brown 
2951552f7358SJed Brown   Level: intermediate
2952552f7358SJed Brown 
29533813dfbdSMatthew G Knepley   Fortran Notes:
29543813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29553813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29563813dfbdSMatthew G Knepley 
29573813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29583813dfbdSMatthew G Knepley 
2959552f7358SJed Brown .keywords: mesh
2960552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2961552f7358SJed Brown @*/
2962552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2963552f7358SJed Brown {
2964552f7358SJed Brown   PetscErrorCode ierr;
2965552f7358SJed Brown 
2966552f7358SJed Brown   PetscFunctionBegin;
2967552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2968d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2969d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2970d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
297169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2972d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2973552f7358SJed Brown   PetscFunctionReturn(0);
2974552f7358SJed Brown }
2975552f7358SJed Brown 
2976552f7358SJed Brown /*@C
2977552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2978552f7358SJed Brown 
2979552f7358SJed Brown   Not Collective
2980552f7358SJed Brown 
2981552f7358SJed Brown   Input Parameters:
2982552f7358SJed Brown + dm - The DMPlex object
2983552f7358SJed Brown . numPoints - The number of input points for the meet
2984552f7358SJed Brown - points - The input points
2985552f7358SJed Brown 
2986552f7358SJed Brown   Output Parameters:
2987552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2988552f7358SJed Brown - coveredPoints - The points in the meet
2989552f7358SJed Brown 
2990552f7358SJed Brown   Level: intermediate
2991552f7358SJed Brown 
29923813dfbdSMatthew G Knepley   Fortran Notes:
29933813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29943813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29953813dfbdSMatthew G Knepley 
29963813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29973813dfbdSMatthew G Knepley 
2998552f7358SJed Brown .keywords: mesh
2999552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3000552f7358SJed Brown @*/
3001552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3002552f7358SJed Brown {
3003552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3004552f7358SJed Brown   PetscInt      *offsets, **closures;
3005552f7358SJed Brown   PetscInt      *meet[2];
3006552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
300724c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3008552f7358SJed Brown   PetscErrorCode ierr;
3009552f7358SJed Brown 
3010552f7358SJed Brown   PetscFunctionBegin;
3011552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3012552f7358SJed Brown   PetscValidPointer(points, 2);
3013552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3014552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3015552f7358SJed Brown 
3016552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3017785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
301869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
301924c766afSToby Isaac   mc      = mesh->maxConeSize;
302024c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
302169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
302269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3023552f7358SJed Brown 
3024552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3025552f7358SJed Brown     PetscInt closureSize;
3026552f7358SJed Brown 
3027552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
30280d644c17SKarl Rupp 
3029552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3030552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3031552f7358SJed Brown       PetscInt pStart, pEnd, i;
3032552f7358SJed Brown 
3033552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3034552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3035552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3036552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3037552f7358SJed Brown           break;
3038552f7358SJed Brown         }
3039552f7358SJed Brown       }
3040552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3041552f7358SJed Brown     }
304282f516ccSBarry 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);
3043552f7358SJed Brown   }
3044552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3045552f7358SJed Brown     PetscInt dof;
3046552f7358SJed Brown 
3047552f7358SJed Brown     /* Copy in cone of first point */
3048552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3049552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3050552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3051552f7358SJed Brown     }
3052552f7358SJed Brown     /* Check each successive cone */
3053552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3054552f7358SJed Brown       PetscInt newMeetSize = 0;
3055552f7358SJed Brown 
3056552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3057552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3058552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3059552f7358SJed Brown 
3060552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3061552f7358SJed Brown           if (point == meet[i][m]) {
3062552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3063552f7358SJed Brown             break;
3064552f7358SJed Brown           }
3065552f7358SJed Brown         }
3066552f7358SJed Brown       }
3067552f7358SJed Brown       meetSize = newMeetSize;
3068552f7358SJed Brown       i        = 1-i;
3069552f7358SJed Brown     }
3070552f7358SJed Brown     if (meetSize) break;
3071552f7358SJed Brown   }
3072552f7358SJed Brown   *numCoveredPoints = meetSize;
3073552f7358SJed Brown   *coveredPoints    = meet[i];
3074552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
30750298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3076552f7358SJed Brown   }
3077552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
307869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
307969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3080552f7358SJed Brown   PetscFunctionReturn(0);
3081552f7358SJed Brown }
3082552f7358SJed Brown 
30834e3744c5SMatthew G. Knepley /*@C
30844e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
30854e3744c5SMatthew G. Knepley 
30864e3744c5SMatthew G. Knepley   Not Collective
30874e3744c5SMatthew G. Knepley 
30884e3744c5SMatthew G. Knepley   Input Parameters:
30894e3744c5SMatthew G. Knepley + dmA - A DMPlex object
30904e3744c5SMatthew G. Knepley - dmB - A DMPlex object
30914e3744c5SMatthew G. Knepley 
30924e3744c5SMatthew G. Knepley   Output Parameters:
30934e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
30944e3744c5SMatthew G. Knepley 
30954e3744c5SMatthew G. Knepley   Level: intermediate
30964e3744c5SMatthew G. Knepley 
30974e3744c5SMatthew G. Knepley   Notes:
30984e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
30994e3744c5SMatthew G. Knepley 
31004e3744c5SMatthew G. Knepley .keywords: mesh
31014e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
31024e3744c5SMatthew G. Knepley @*/
31034e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
31044e3744c5SMatthew G. Knepley {
31054e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
31064e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
31074e3744c5SMatthew G. Knepley 
31084e3744c5SMatthew G. Knepley   PetscFunctionBegin;
31094e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
31104e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
31114e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
31124e3744c5SMatthew G. Knepley 
31134e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
31144e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
31154e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
31164e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
31174e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
31184e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
31194e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
31204e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
31214e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
31224e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
31234e3744c5SMatthew G. Knepley 
31244e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
31254e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
31264e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
31274e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
31284e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
31294e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
31304e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
31314e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
31324e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
31334e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
31344e3744c5SMatthew G. Knepley     }
31354e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
31364e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
31374e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
31384e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
31394e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
31404e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
31414e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
31424e3744c5SMatthew G. Knepley     }
31434e3744c5SMatthew G. Knepley   }
31444e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
31454e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
31464e3744c5SMatthew G. Knepley }
31474e3744c5SMatthew G. Knepley 
31487cd05799SMatthew G. Knepley /*@C
31497cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
31507cd05799SMatthew G. Knepley 
31517cd05799SMatthew G. Knepley   Not Collective
31527cd05799SMatthew G. Knepley 
31537cd05799SMatthew G. Knepley   Input Parameters:
31547cd05799SMatthew G. Knepley + dm         - The DMPlex
31557cd05799SMatthew G. Knepley . cellDim    - The cell dimension
31567cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
31577cd05799SMatthew G. Knepley 
31587cd05799SMatthew G. Knepley   Output Parameters:
31597cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
31607cd05799SMatthew G. Knepley 
31617cd05799SMatthew G. Knepley   Level: developer
31627cd05799SMatthew G. Knepley 
31637cd05799SMatthew G. Knepley   Notes:
31647cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
31657cd05799SMatthew G. Knepley 
31667cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
31677cd05799SMatthew G. Knepley @*/
316818ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3169a6dfd86eSKarl Rupp {
317082f516ccSBarry Smith   MPI_Comm       comm;
3171552f7358SJed Brown   PetscErrorCode ierr;
3172552f7358SJed Brown 
3173552f7358SJed Brown   PetscFunctionBegin;
317482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3175552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3176552f7358SJed Brown   switch (cellDim) {
3177552f7358SJed Brown   case 0:
3178552f7358SJed Brown     *numFaceVertices = 0;
3179552f7358SJed Brown     break;
3180552f7358SJed Brown   case 1:
3181552f7358SJed Brown     *numFaceVertices = 1;
3182552f7358SJed Brown     break;
3183552f7358SJed Brown   case 2:
3184552f7358SJed Brown     switch (numCorners) {
318519436ca2SJed Brown     case 3: /* triangle */
318619436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3187552f7358SJed Brown       break;
318819436ca2SJed Brown     case 4: /* quadrilateral */
318919436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3190552f7358SJed Brown       break;
319119436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
319219436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3193552f7358SJed Brown       break;
319419436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
319519436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3196552f7358SJed Brown       break;
3197552f7358SJed Brown     default:
3198f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3199552f7358SJed Brown     }
3200552f7358SJed Brown     break;
3201552f7358SJed Brown   case 3:
3202552f7358SJed Brown     switch (numCorners) {
320319436ca2SJed Brown     case 4: /* tetradehdron */
320419436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3205552f7358SJed Brown       break;
320619436ca2SJed Brown     case 6: /* tet cohesive cells */
320719436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3208552f7358SJed Brown       break;
320919436ca2SJed Brown     case 8: /* hexahedron */
321019436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3211552f7358SJed Brown       break;
321219436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
321319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3214552f7358SJed Brown       break;
321519436ca2SJed Brown     case 10: /* quadratic tetrahedron */
321619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3217552f7358SJed Brown       break;
321819436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
321919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3220552f7358SJed Brown       break;
322119436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
322219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3223552f7358SJed Brown       break;
322419436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
322519436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3226552f7358SJed Brown       break;
3227552f7358SJed Brown     default:
3228f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3229552f7358SJed Brown     }
3230552f7358SJed Brown     break;
3231552f7358SJed Brown   default:
3232f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3233552f7358SJed Brown   }
3234552f7358SJed Brown   PetscFunctionReturn(0);
3235552f7358SJed Brown }
3236552f7358SJed Brown 
3237552f7358SJed Brown /*@
3238aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3239552f7358SJed Brown 
3240552f7358SJed Brown   Not Collective
3241552f7358SJed Brown 
3242aa50250dSMatthew G. Knepley   Input Parameter:
3243552f7358SJed Brown . dm    - The DMPlex object
3244552f7358SJed Brown 
3245aa50250dSMatthew G. Knepley   Output Parameter:
3246aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3247552f7358SJed Brown 
3248552f7358SJed Brown   Level: developer
3249552f7358SJed Brown 
3250aa50250dSMatthew G. Knepley .keywords: mesh, points
3251aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3252aa50250dSMatthew G. Knepley @*/
3253aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3254aa50250dSMatthew G. Knepley {
3255aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
3256aa50250dSMatthew G. Knepley 
3257aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3258aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3259aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3260c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
3261c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3262aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3263aa50250dSMatthew G. Knepley }
3264aa50250dSMatthew G. Knepley 
3265aa50250dSMatthew G. Knepley /*@
3266aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3267aa50250dSMatthew G. Knepley 
3268aa50250dSMatthew G. Knepley   Not Collective
3269aa50250dSMatthew G. Knepley 
3270aa50250dSMatthew G. Knepley   Input Parameter:
3271aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3272aa50250dSMatthew G. Knepley 
3273aa50250dSMatthew G. Knepley   Output Parameter:
3274aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3275aa50250dSMatthew G. Knepley 
3276aa50250dSMatthew G. Knepley   Level: developer
3277552f7358SJed Brown 
3278552f7358SJed Brown .keywords: mesh, points
3279aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3280552f7358SJed Brown @*/
3281552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3282552f7358SJed Brown {
3283aa50250dSMatthew G. Knepley   DMLabel        label;
3284aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3285552f7358SJed Brown   PetscErrorCode ierr;
3286552f7358SJed Brown 
3287552f7358SJed Brown   PetscFunctionBegin;
3288552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3289552f7358SJed Brown   PetscValidPointer(depth, 2);
3290aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3291aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3292552f7358SJed Brown   *depth = d-1;
3293552f7358SJed Brown   PetscFunctionReturn(0);
3294552f7358SJed Brown }
3295552f7358SJed Brown 
3296552f7358SJed Brown /*@
3297552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3298552f7358SJed Brown 
3299552f7358SJed Brown   Not Collective
3300552f7358SJed Brown 
3301552f7358SJed Brown   Input Parameters:
3302552f7358SJed Brown + dm           - The DMPlex object
3303552f7358SJed Brown - stratumValue - The requested depth
3304552f7358SJed Brown 
3305552f7358SJed Brown   Output Parameters:
3306552f7358SJed Brown + start - The first point at this depth
3307552f7358SJed Brown - end   - One beyond the last point at this depth
3308552f7358SJed Brown 
3309552f7358SJed Brown   Level: developer
3310552f7358SJed Brown 
3311552f7358SJed Brown .keywords: mesh, points
3312552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3313552f7358SJed Brown @*/
33140adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
33150adebc6cSBarry Smith {
3316aa50250dSMatthew G. Knepley   DMLabel        label;
331763d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3318552f7358SJed Brown   PetscErrorCode ierr;
3319552f7358SJed Brown 
3320552f7358SJed Brown   PetscFunctionBegin;
3321552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
332263d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
332363d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3324552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
33250d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
332663d1a920SMatthew G. Knepley   if (stratumValue < 0) {
332763d1a920SMatthew G. Knepley     if (start) *start = pStart;
332863d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
332963d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3330552f7358SJed Brown   }
3331aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
333263d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
333363d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3334552f7358SJed Brown   PetscFunctionReturn(0);
3335552f7358SJed Brown }
3336552f7358SJed Brown 
3337552f7358SJed Brown /*@
3338552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3339552f7358SJed Brown 
3340552f7358SJed Brown   Not Collective
3341552f7358SJed Brown 
3342552f7358SJed Brown   Input Parameters:
3343552f7358SJed Brown + dm           - The DMPlex object
3344552f7358SJed Brown - stratumValue - The requested height
3345552f7358SJed Brown 
3346552f7358SJed Brown   Output Parameters:
3347552f7358SJed Brown + start - The first point at this height
3348552f7358SJed Brown - end   - One beyond the last point at this height
3349552f7358SJed Brown 
3350552f7358SJed Brown   Level: developer
3351552f7358SJed Brown 
3352552f7358SJed Brown .keywords: mesh, points
3353552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3354552f7358SJed Brown @*/
33550adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
33560adebc6cSBarry Smith {
3357aa50250dSMatthew G. Knepley   DMLabel        label;
335863d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3359552f7358SJed Brown   PetscErrorCode ierr;
3360552f7358SJed Brown 
3361552f7358SJed Brown   PetscFunctionBegin;
3362552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
336363d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
336463d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3365552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
33660d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
336763d1a920SMatthew G. Knepley   if (stratumValue < 0) {
336863d1a920SMatthew G. Knepley     if (start) *start = pStart;
336963d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
337063d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3371552f7358SJed Brown   }
3372aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
337313903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
337463d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
337563d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3376552f7358SJed Brown   PetscFunctionReturn(0);
3377552f7358SJed Brown }
3378552f7358SJed Brown 
33790adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
33800adebc6cSBarry Smith {
3381efe440bfSMatthew G. Knepley   PetscSection   section, s;
3382efe440bfSMatthew G. Knepley   Mat            m;
33833e922f36SToby Isaac   PetscInt       maxHeight;
3384552f7358SJed Brown   PetscErrorCode ierr;
3385552f7358SJed Brown 
3386552f7358SJed Brown   PetscFunctionBegin;
338738221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
33883e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
33893e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
339082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3391e87a4003SBarry Smith   ierr = DMSetSection(*cdm, section);CHKERRQ(ierr);
33921d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3393efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3394efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3395efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3396efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3397efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3398552f7358SJed Brown   PetscFunctionReturn(0);
3399552f7358SJed Brown }
3400552f7358SJed Brown 
3401f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3402f19dbd58SToby Isaac {
3403f19dbd58SToby Isaac   Vec            coordsLocal;
3404f19dbd58SToby Isaac   DM             coordsDM;
3405f19dbd58SToby Isaac   PetscErrorCode ierr;
3406f19dbd58SToby Isaac 
3407f19dbd58SToby Isaac   PetscFunctionBegin;
3408f19dbd58SToby Isaac   *field = NULL;
3409f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3410f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3411f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3412f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3413f19dbd58SToby Isaac   }
3414f19dbd58SToby Isaac   PetscFunctionReturn(0);
3415f19dbd58SToby Isaac }
3416f19dbd58SToby Isaac 
34177cd05799SMatthew G. Knepley /*@C
34187cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
34197cd05799SMatthew G. Knepley 
34207cd05799SMatthew G. Knepley   Not Collective
34217cd05799SMatthew G. Knepley 
34227cd05799SMatthew G. Knepley   Input Parameters:
34237cd05799SMatthew G. Knepley . dm        - The DMPlex object
34247cd05799SMatthew G. Knepley 
34257cd05799SMatthew G. Knepley   Output Parameter:
34267cd05799SMatthew G. Knepley . section - The PetscSection object
34277cd05799SMatthew G. Knepley 
34287cd05799SMatthew G. Knepley   Level: developer
34297cd05799SMatthew G. Knepley 
34307cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
34317cd05799SMatthew G. Knepley @*/
34320adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
34330adebc6cSBarry Smith {
3434552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3435552f7358SJed Brown 
3436552f7358SJed Brown   PetscFunctionBegin;
3437552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3438552f7358SJed Brown   if (section) *section = mesh->coneSection;
3439552f7358SJed Brown   PetscFunctionReturn(0);
3440552f7358SJed Brown }
3441552f7358SJed Brown 
34427cd05799SMatthew G. Knepley /*@C
34437cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
34447cd05799SMatthew G. Knepley 
34457cd05799SMatthew G. Knepley   Not Collective
34467cd05799SMatthew G. Knepley 
34477cd05799SMatthew G. Knepley   Input Parameters:
34487cd05799SMatthew G. Knepley . dm        - The DMPlex object
34497cd05799SMatthew G. Knepley 
34507cd05799SMatthew G. Knepley   Output Parameter:
34517cd05799SMatthew G. Knepley . section - The PetscSection object
34527cd05799SMatthew G. Knepley 
34537cd05799SMatthew G. Knepley   Level: developer
34547cd05799SMatthew G. Knepley 
34557cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34567cd05799SMatthew G. Knepley @*/
34578cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
34588cb4d582SMatthew G. Knepley {
34598cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
34608cb4d582SMatthew G. Knepley 
34618cb4d582SMatthew G. Knepley   PetscFunctionBegin;
34628cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34638cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
34648cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
34658cb4d582SMatthew G. Knepley }
34668cb4d582SMatthew G. Knepley 
34677cd05799SMatthew G. Knepley /*@C
34687cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
34697cd05799SMatthew G. Knepley 
34707cd05799SMatthew G. Knepley   Not Collective
34717cd05799SMatthew G. Knepley 
34727cd05799SMatthew G. Knepley   Input Parameters:
34737cd05799SMatthew G. Knepley . dm        - The DMPlex object
34747cd05799SMatthew G. Knepley 
34757cd05799SMatthew G. Knepley   Output Parameter:
34767cd05799SMatthew G. Knepley . cones - The cone for each point
34777cd05799SMatthew G. Knepley 
34787cd05799SMatthew G. Knepley   Level: developer
34797cd05799SMatthew G. Knepley 
34807cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34817cd05799SMatthew G. Knepley @*/
3482a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3483a6dfd86eSKarl Rupp {
3484552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3485552f7358SJed Brown 
3486552f7358SJed Brown   PetscFunctionBegin;
3487552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3488552f7358SJed Brown   if (cones) *cones = mesh->cones;
3489552f7358SJed Brown   PetscFunctionReturn(0);
3490552f7358SJed Brown }
3491552f7358SJed Brown 
34927cd05799SMatthew G. Knepley /*@C
34937cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
34947cd05799SMatthew G. Knepley 
34957cd05799SMatthew G. Knepley   Not Collective
34967cd05799SMatthew G. Knepley 
34977cd05799SMatthew G. Knepley   Input Parameters:
34987cd05799SMatthew G. Knepley . dm        - The DMPlex object
34997cd05799SMatthew G. Knepley 
35007cd05799SMatthew G. Knepley   Output Parameter:
35017cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
35027cd05799SMatthew G. Knepley 
35037cd05799SMatthew G. Knepley   Level: developer
35047cd05799SMatthew G. Knepley 
35057cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35067cd05799SMatthew G. Knepley @*/
3507a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3508a6dfd86eSKarl Rupp {
3509552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3510552f7358SJed Brown 
3511552f7358SJed Brown   PetscFunctionBegin;
3512552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3513552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3514552f7358SJed Brown   PetscFunctionReturn(0);
3515552f7358SJed Brown }
3516552f7358SJed Brown 
3517552f7358SJed Brown /******************************** FEM Support **********************************/
3518552f7358SJed Brown 
35197391a63aSMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscInt point, PetscSection section)
35203194fc30SMatthew G. Knepley {
35217391a63aSMatthew G. Knepley   DMLabel        label;
35223194fc30SMatthew G. Knepley   PetscInt      *perm;
35237391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
35243194fc30SMatthew G. Knepley   PetscErrorCode ierr;
35253194fc30SMatthew G. Knepley 
35263194fc30SMatthew G. Knepley   PetscFunctionBegin;
35277391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
35283194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
35297391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
35307391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
35317391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
35327391a63aSMatthew G. Knepley   else if  (depth == dim) {
35337391a63aSMatthew G. Knepley     const PetscInt *cone;
35347391a63aSMatthew G. Knepley 
35357391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3536d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
3537d4e6627bSStefano Zampini     else if (dim == 3) {
3538d4e6627bSStefano Zampini       const PetscInt *cone2;
3539d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
3540d4e6627bSStefano Zampini       eStart = cone2[0];
3541d4e6627bSStefano Zampini     } else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering for dim %D", point, depth, dim);
3542d4e6627bSStefano Zampini   } else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering for dim %D", point, depth, dim);
3543e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
35443194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
354589eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
35463194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
35473194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
35483194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35493194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
35503194fc30SMatthew G. Knepley     k = k/Nc + 1;
355189eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
35523194fc30SMatthew G. Knepley   }
35533194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
35543194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
355589eabcffSMatthew G. Knepley     switch (dim) {
355689eabcffSMatthew G. Knepley     case 2:
35573194fc30SMatthew 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} */
35583194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35593194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
35603194fc30SMatthew G. Knepley       k = k/Nc + 1;
35613194fc30SMatthew G. Knepley       /* The SEM order is
35623194fc30SMatthew G. Knepley 
35633194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
356489eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
35653194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
35663194fc30SMatthew G. Knepley       */
35673194fc30SMatthew G. Knepley       {
35683194fc30SMatthew G. Knepley         const PetscInt of   = 0;
35693194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
35703194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
35713194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
35723194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
35733194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
35743194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
35753194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
35763194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
35773194fc30SMatthew G. Knepley         PetscInt       o;
35783194fc30SMatthew G. Knepley 
35793194fc30SMatthew G. Knepley         /* bottom */
35803194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
35813194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
35823194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
35833194fc30SMatthew G. Knepley         /* middle */
35843194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
35853194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
35863194fc30SMatthew 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;
35873194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
35883194fc30SMatthew G. Knepley         }
35893194fc30SMatthew G. Knepley         /* top */
35903194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
35913194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
35923194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
35933194fc30SMatthew G. Knepley         foffset = offset;
35943194fc30SMatthew G. Knepley       }
359589eabcffSMatthew G. Knepley       break;
359689eabcffSMatthew G. Knepley     case 3:
359789eabcffSMatthew G. Knepley       /* The original hex closure is
359889eabcffSMatthew G. Knepley 
359989eabcffSMatthew G. Knepley          {c,
360089eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
360189eabcffSMatthew 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,
360289eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
360389eabcffSMatthew G. Knepley       */
360489eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
360589eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
360689eabcffSMatthew G. Knepley       k = k/Nc + 1;
360789eabcffSMatthew G. Knepley       /* The SEM order is
360889eabcffSMatthew G. Knepley          Bottom Slice
360989eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
361089eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
361189eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
361289eabcffSMatthew G. Knepley 
361389eabcffSMatthew G. Knepley          Middle Slice (j)
361489eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
361589eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
361689eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
361789eabcffSMatthew G. Knepley 
361889eabcffSMatthew G. Knepley          Top Slice
361989eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
362089eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
362189eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
362289eabcffSMatthew G. Knepley       */
362389eabcffSMatthew G. Knepley       {
362489eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
362589eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
362689eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
362789eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
362889eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
362989eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
363089eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
363189eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
363289eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
363389eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
363489eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
363589eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
363689eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
363789eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
363889eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
363989eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
364089eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
364189eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
364289eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
364389eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
364489eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
364589eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
364689eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
364789eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
364889eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
364989eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
365089eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
365189eabcffSMatthew G. Knepley         PetscInt       o, n;
365289eabcffSMatthew G. Knepley 
365389eabcffSMatthew G. Knepley         /* Bottom Slice */
365489eabcffSMatthew G. Knepley         /*   bottom */
365589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
365689eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
365789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
365889eabcffSMatthew G. Knepley         /*   middle */
365989eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
366089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3661316b7f87SMax 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;}
366289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
36633194fc30SMatthew G. Knepley         }
366489eabcffSMatthew G. Knepley         /*   top */
366589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
366689eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
366789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
366889eabcffSMatthew G. Knepley 
366989eabcffSMatthew G. Knepley         /* Middle Slice */
367089eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
367189eabcffSMatthew G. Knepley           /*   bottom */
367289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
367389eabcffSMatthew 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;
367489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
367589eabcffSMatthew G. Knepley           /*   middle */
367689eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
367789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
367889eabcffSMatthew 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;
367989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
368089eabcffSMatthew G. Knepley           }
368189eabcffSMatthew G. Knepley           /*   top */
368289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
368389eabcffSMatthew 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;
368489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
368589eabcffSMatthew G. Knepley         }
368689eabcffSMatthew G. Knepley 
368789eabcffSMatthew G. Knepley         /* Top Slice */
368889eabcffSMatthew G. Knepley         /*   bottom */
368989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
369089eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
369189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
369289eabcffSMatthew G. Knepley         /*   middle */
369389eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
369489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
369589eabcffSMatthew 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;
369689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
369789eabcffSMatthew G. Knepley         }
369889eabcffSMatthew G. Knepley         /*   top */
369989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
370089eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
370189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
370289eabcffSMatthew G. Knepley 
370389eabcffSMatthew G. Knepley         foffset = offset;
370489eabcffSMatthew G. Knepley       }
370589eabcffSMatthew G. Knepley       break;
370689eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
370789eabcffSMatthew G. Knepley     }
370889eabcffSMatthew G. Knepley   }
370989eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
37103194fc30SMatthew G. Knepley   /* Check permutation */
37113194fc30SMatthew G. Knepley   {
37123194fc30SMatthew G. Knepley     PetscInt *check;
37133194fc30SMatthew G. Knepley 
37143194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
37153194fc30SMatthew 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]);}
37163194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
37173194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
37183194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
37193194fc30SMatthew G. Knepley   }
37201fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
37213194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
37223194fc30SMatthew G. Knepley }
37233194fc30SMatthew G. Knepley 
3724e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3725e071409bSToby Isaac {
3726e071409bSToby Isaac   PetscDS        prob;
3727e071409bSToby Isaac   PetscInt       depth, Nf, h;
3728e071409bSToby Isaac   DMLabel        label;
3729e071409bSToby Isaac   PetscErrorCode ierr;
3730e071409bSToby Isaac 
3731e071409bSToby Isaac   PetscFunctionBeginHot;
3732e071409bSToby Isaac   prob    = dm->prob;
3733e071409bSToby Isaac   Nf      = prob->Nf;
3734e071409bSToby Isaac   label   = dm->depthLabel;
3735e071409bSToby Isaac   *dspace = NULL;
3736e071409bSToby Isaac   if (field < Nf) {
3737e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3738e071409bSToby Isaac 
3739e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3740e071409bSToby Isaac       PetscDualSpace dsp;
3741e071409bSToby Isaac 
3742e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3743e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3744e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3745e071409bSToby Isaac       h    = depth - 1 - h;
3746e071409bSToby Isaac       if (h) {
3747e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3748e071409bSToby Isaac       } else {
3749e071409bSToby Isaac         *dspace = dsp;
3750e071409bSToby Isaac       }
3751e071409bSToby Isaac     }
3752e071409bSToby Isaac   }
3753e071409bSToby Isaac   PetscFunctionReturn(0);
3754e071409bSToby Isaac }
3755e071409bSToby Isaac 
3756e071409bSToby Isaac 
37571a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3758a6dfd86eSKarl Rupp {
3759552f7358SJed Brown   PetscScalar    *array, *vArray;
3760d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
37611a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3762552f7358SJed Brown   PetscErrorCode  ierr;
3763552f7358SJed Brown 
37641b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
37652a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
37665a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
37675a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
37685a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
37693f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
37709df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
37719df71ca4SMatthew G. Knepley       PetscInt dof;
3772d9917b9dSMatthew G. Knepley 
37739df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37749df71ca4SMatthew G. Knepley       size += dof;
37759df71ca4SMatthew G. Knepley     }
37769df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
37779df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
37782a3aaacfSMatthew G. Knepley       PetscInt       dof;
37795a1bb5cfSMatthew G. Knepley 
37805a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
37812a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
37825a1bb5cfSMatthew G. Knepley       size += dof;
37835a1bb5cfSMatthew G. Knepley     }
37843f7cbbe7SMatthew G. Knepley     if (!values) {
37853f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
37863f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
37873f7cbbe7SMatthew G. Knepley     }
378869291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
3789982e9ed1SMatthew G. Knepley   } else {
3790982e9ed1SMatthew G. Knepley     array = *values;
3791982e9ed1SMatthew G. Knepley   }
37929df71ca4SMatthew G. Knepley   size = 0;
37935a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
37949df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
37959df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
37969df71ca4SMatthew G. Knepley     PetscScalar *varr;
3797d9917b9dSMatthew G. Knepley 
37989df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37999df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
38009df71ca4SMatthew G. Knepley     varr = &vArray[off];
38011a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
38021a271a75SMatthew G. Knepley       array[offset] = varr[d];
38039df71ca4SMatthew G. Knepley     }
38049df71ca4SMatthew G. Knepley     size += dof;
38059df71ca4SMatthew G. Knepley   }
38069df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
38079df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
38089df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
38095a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
38105a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
38115a1bb5cfSMatthew G. Knepley 
381252ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
38135a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38145a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
38155a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
38165a1bb5cfSMatthew G. Knepley     if (o >= 0) {
38171a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
38181a271a75SMatthew G. Knepley         array[offset] = varr[d];
38195a1bb5cfSMatthew G. Knepley       }
38205a1bb5cfSMatthew G. Knepley     } else {
38211a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
38221a271a75SMatthew G. Knepley         array[offset] = varr[d];
38235a1bb5cfSMatthew G. Knepley       }
38245a1bb5cfSMatthew G. Knepley     }
38259df71ca4SMatthew G. Knepley     size += dof;
38265a1bb5cfSMatthew G. Knepley   }
38275a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
38289df71ca4SMatthew G. Knepley   if (!*values) {
38295a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
38305a1bb5cfSMatthew G. Knepley     *values = array;
38319df71ca4SMatthew G. Knepley   } else {
38328ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
38338c312ff3SMatthew G. Knepley     *csize = size;
38349df71ca4SMatthew G. Knepley   }
38355a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
38365a1bb5cfSMatthew G. Knepley }
3837d9917b9dSMatthew G. Knepley 
3838923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3839923c78e0SToby Isaac {
3840923c78e0SToby Isaac   const PetscInt *cla;
3841923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3842923c78e0SToby Isaac   PetscErrorCode ierr;
3843923c78e0SToby Isaac 
3844923c78e0SToby Isaac   PetscFunctionBeginHot;
3845923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3846923c78e0SToby Isaac   if (!*clPoints) {
3847923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3848923c78e0SToby Isaac 
3849923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3850923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3851923c78e0SToby Isaac     /* Compress out points not in the section */
3852923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3853923c78e0SToby Isaac       PetscInt r = pts[2*p];
3854923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3855923c78e0SToby Isaac         pts[q*2]   = r;
3856923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3857923c78e0SToby Isaac         ++q;
3858923c78e0SToby Isaac       }
3859923c78e0SToby Isaac     }
3860923c78e0SToby Isaac     np = q;
3861923c78e0SToby Isaac     cla = NULL;
3862923c78e0SToby Isaac   } else {
3863923c78e0SToby Isaac     PetscInt dof, off;
3864923c78e0SToby Isaac 
3865923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3866923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3867923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3868923c78e0SToby Isaac     np   = dof/2;
3869923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3870923c78e0SToby Isaac   }
3871923c78e0SToby Isaac   *numPoints = np;
3872923c78e0SToby Isaac   *points    = pts;
3873923c78e0SToby Isaac   *clp       = cla;
3874923c78e0SToby Isaac 
3875923c78e0SToby Isaac   PetscFunctionReturn(0);
3876923c78e0SToby Isaac }
3877923c78e0SToby Isaac 
3878923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3879923c78e0SToby Isaac {
3880923c78e0SToby Isaac   PetscErrorCode ierr;
3881923c78e0SToby Isaac 
3882923c78e0SToby Isaac   PetscFunctionBeginHot;
3883923c78e0SToby Isaac   if (!*clPoints) {
3884923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3885923c78e0SToby Isaac   } else {
3886923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3887923c78e0SToby Isaac   }
3888923c78e0SToby Isaac   *numPoints = 0;
3889923c78e0SToby Isaac   *points    = NULL;
3890923c78e0SToby Isaac   *clSec     = NULL;
3891923c78e0SToby Isaac   *clPoints  = NULL;
3892923c78e0SToby Isaac   *clp       = NULL;
3893923c78e0SToby Isaac   PetscFunctionReturn(0);
3894923c78e0SToby Isaac }
3895923c78e0SToby Isaac 
389697e99dd9SToby 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[])
38971a271a75SMatthew G. Knepley {
38981a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
389997e99dd9SToby Isaac   const PetscInt    **perms = NULL;
390097e99dd9SToby Isaac   const PetscScalar **flips = NULL;
39011a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39021a271a75SMatthew G. Knepley 
39031a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3904fe02ba77SJed Brown   *size = 0;
390597e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
390697e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
390797e99dd9SToby Isaac     const PetscInt    point = points[2*p];
390897e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
390997e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
39101a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
39111a271a75SMatthew G. Knepley     const PetscScalar *varr;
39121a271a75SMatthew G. Knepley 
39131a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39141a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
39151a271a75SMatthew G. Knepley     varr = &vArray[off];
391697e99dd9SToby Isaac     if (clperm) {
391797e99dd9SToby Isaac       if (perm) {
391897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
39191a271a75SMatthew G. Knepley       } else {
392097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
392197e99dd9SToby Isaac       }
392297e99dd9SToby Isaac       if (flip) {
392397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
392497e99dd9SToby Isaac       }
392597e99dd9SToby Isaac     } else {
392697e99dd9SToby Isaac       if (perm) {
392797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
392897e99dd9SToby Isaac       } else {
392997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
393097e99dd9SToby Isaac       }
393197e99dd9SToby Isaac       if (flip) {
393297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
39331a271a75SMatthew G. Knepley       }
39341a271a75SMatthew G. Knepley     }
393597e99dd9SToby Isaac     offset += dof;
393697e99dd9SToby Isaac   }
393797e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
39381a271a75SMatthew G. Knepley   *size = offset;
39391a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
39401a271a75SMatthew G. Knepley }
39411a271a75SMatthew G. Knepley 
394297e99dd9SToby 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[])
39431a271a75SMatthew G. Knepley {
39441a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
39451a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39461a271a75SMatthew G. Knepley 
39471a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3948fe02ba77SJed Brown   *size = 0;
39491a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
395097e99dd9SToby Isaac     PetscInt          p;
395197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
395297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
39531a271a75SMatthew G. Knepley 
395497e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
395597e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
395697e99dd9SToby Isaac       const PetscInt    point = points[2*p];
395797e99dd9SToby Isaac       PetscInt          fdof, foff, b;
39581a271a75SMatthew G. Knepley       const PetscScalar *varr;
395997e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
396097e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
39611a271a75SMatthew G. Knepley 
39621a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
39631a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
39641a271a75SMatthew G. Knepley       varr = &vArray[foff];
396597e99dd9SToby Isaac       if (clperm) {
396697e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
396797e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
396897e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
39691a271a75SMatthew G. Knepley       } else {
397097e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
397197e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
397297e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
39731a271a75SMatthew G. Knepley       }
397497e99dd9SToby Isaac       offset += fdof;
39751a271a75SMatthew G. Knepley     }
397697e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
39771a271a75SMatthew G. Knepley   }
39781a271a75SMatthew G. Knepley   *size = offset;
39791a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
39801a271a75SMatthew G. Knepley }
39811a271a75SMatthew G. Knepley 
3982552f7358SJed Brown /*@C
3983552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3984552f7358SJed Brown 
3985552f7358SJed Brown   Not collective
3986552f7358SJed Brown 
3987552f7358SJed Brown   Input Parameters:
3988552f7358SJed Brown + dm - The DM
3989552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3990552f7358SJed Brown . v - The local vector
399122c1ee49SMatthew G. Knepley . point - The point in the DM
399222c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
399322c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
3994552f7358SJed Brown 
3995552f7358SJed Brown   Output Parameters:
399622c1ee49SMatthew G. Knepley + csize - The number of values in the closure
399722c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
399822c1ee49SMatthew G. Knepley 
399922c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
400022c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
400122c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
400222c1ee49SMatthew G. Knepley $
400322c1ee49SMatthew G. Knepley $ A typical use could be
400422c1ee49SMatthew G. Knepley $
400522c1ee49SMatthew G. Knepley $  values = NULL;
400622c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
400722c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
400822c1ee49SMatthew G. Knepley $    <Compute on closure>
400922c1ee49SMatthew G. Knepley $  }
401022c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
401122c1ee49SMatthew G. Knepley $
401222c1ee49SMatthew G. Knepley $ or
401322c1ee49SMatthew G. Knepley $
401422c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
401522c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
401622c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
401722c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
401822c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
401922c1ee49SMatthew G. Knepley $      <Compute on closure>
402022c1ee49SMatthew G. Knepley $    }
402122c1ee49SMatthew G. Knepley $  }
402222c1ee49SMatthew G. Knepley $  PetscFree(values);
4023552f7358SJed Brown 
4024552f7358SJed Brown   Fortran Notes:
4025552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4026552f7358SJed Brown   include petsc.h90 in your code.
4027552f7358SJed Brown 
4028552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4029552f7358SJed Brown 
4030552f7358SJed Brown   Level: intermediate
4031552f7358SJed Brown 
4032552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4033552f7358SJed Brown @*/
4034552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4035552f7358SJed Brown {
4036552f7358SJed Brown   PetscSection       clSection;
4037d9917b9dSMatthew G. Knepley   IS                 clPoints;
40388a84db2dSMatthew G. Knepley   PetscScalar       *array;
40398a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4040552f7358SJed Brown   PetscInt          *points = NULL;
40418f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
40421a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4043552f7358SJed Brown   PetscErrorCode     ierr;
4044552f7358SJed Brown 
4045d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4046552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4047e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
40481a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
40491a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4050552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4051552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4052552f7358SJed Brown   if (depth == 1 && numFields < 2) {
40531a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4054552f7358SJed Brown     PetscFunctionReturn(0);
4055552f7358SJed Brown   }
40561a271a75SMatthew G. Knepley   /* Get points */
4057923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40581fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
40591a271a75SMatthew G. Knepley   /* Get array */
4060bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
40611a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4062552f7358SJed Brown 
40631a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4064552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
40651a271a75SMatthew G. Knepley       asize += dof;
4066552f7358SJed Brown     }
4067bd6c0d32SMatthew G. Knepley     if (!values) {
4068923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40691a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4070bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4071bd6c0d32SMatthew G. Knepley     }
407269291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4073d0f6b257SMatthew G. Knepley   } else {
4074d0f6b257SMatthew G. Knepley     array = *values;
4075d0f6b257SMatthew G. Knepley   }
40768a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
40771a271a75SMatthew G. Knepley   /* Get values */
407897e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
407997e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
40801a271a75SMatthew G. Knepley   /* Cleanup points */
4081923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40821a271a75SMatthew G. Knepley   /* Cleanup array */
40838a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4084d0f6b257SMatthew G. Knepley   if (!*values) {
4085552f7358SJed Brown     if (csize) *csize = size;
4086552f7358SJed Brown     *values = array;
4087d0f6b257SMatthew G. Knepley   } else {
4088f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4089d0f6b257SMatthew G. Knepley     *csize = size;
4090d0f6b257SMatthew G. Knepley   }
4091552f7358SJed Brown   PetscFunctionReturn(0);
4092552f7358SJed Brown }
4093552f7358SJed Brown 
4094552f7358SJed Brown /*@C
4095552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4096552f7358SJed Brown 
4097552f7358SJed Brown   Not collective
4098552f7358SJed Brown 
4099552f7358SJed Brown   Input Parameters:
4100552f7358SJed Brown + dm - The DM
41010298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4102552f7358SJed Brown . v - The local vector
4103eaf898f9SPatrick Sanan . point - The point in the DM
41040298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4105552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4106552f7358SJed Brown 
410722c1ee49SMatthew 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()
410822c1ee49SMatthew G. Knepley 
41093813dfbdSMatthew G Knepley   Fortran Notes:
41103813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
41113813dfbdSMatthew G Knepley   include petsc.h90 in your code.
41123813dfbdSMatthew G Knepley 
41133813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
41143813dfbdSMatthew G Knepley 
4115552f7358SJed Brown   Level: intermediate
4116552f7358SJed Brown 
4117552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4118552f7358SJed Brown @*/
41197c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4120a6dfd86eSKarl Rupp {
4121552f7358SJed Brown   PetscInt       size = 0;
4122552f7358SJed Brown   PetscErrorCode ierr;
4123552f7358SJed Brown 
4124552f7358SJed Brown   PetscFunctionBegin;
4125552f7358SJed Brown   /* Should work without recalculating size */
412669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4127c9fdaa05SMatthew G. Knepley   *values = NULL;
4128552f7358SJed Brown   PetscFunctionReturn(0);
4129552f7358SJed Brown }
4130552f7358SJed Brown 
4131552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4132552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4133552f7358SJed Brown 
413497e99dd9SToby 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[])
4135552f7358SJed Brown {
4136552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4137552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4138552f7358SJed Brown   PetscScalar    *a;
4139552f7358SJed Brown   PetscInt        off, cind = 0, k;
4140552f7358SJed Brown   PetscErrorCode  ierr;
4141552f7358SJed Brown 
4142552f7358SJed Brown   PetscFunctionBegin;
4143552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4144552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4145552f7358SJed Brown   a    = &array[off];
4146552f7358SJed Brown   if (!cdof || setBC) {
414797e99dd9SToby Isaac     if (clperm) {
414897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
414997e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4150552f7358SJed Brown     } else {
415197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
415297e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4153552f7358SJed Brown     }
4154552f7358SJed Brown   } else {
4155552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
415697e99dd9SToby Isaac     if (clperm) {
415797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4158552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
415997e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4160552f7358SJed Brown         }
4161552f7358SJed Brown       } else {
4162552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4163552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
416497e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
416597e99dd9SToby Isaac         }
416697e99dd9SToby Isaac       }
416797e99dd9SToby Isaac     } else {
416897e99dd9SToby Isaac       if (perm) {
416997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
417097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
417197e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
417297e99dd9SToby Isaac         }
417397e99dd9SToby Isaac       } else {
417497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
417597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
417697e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
417797e99dd9SToby Isaac         }
4178552f7358SJed Brown       }
4179552f7358SJed Brown     }
4180552f7358SJed Brown   }
4181552f7358SJed Brown   PetscFunctionReturn(0);
4182552f7358SJed Brown }
4183552f7358SJed Brown 
418497e99dd9SToby 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[])
4185a5e93ea8SMatthew G. Knepley {
4186a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4187a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4188a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4189a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4190a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4191a5e93ea8SMatthew G. Knepley 
4192a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4193a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4194a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4195a5e93ea8SMatthew G. Knepley   a    = &array[off];
4196a5e93ea8SMatthew G. Knepley   if (cdof) {
4197a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
419897e99dd9SToby Isaac     if (clperm) {
419997e99dd9SToby Isaac       if (perm) {
4200a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4201a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
420297e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
420397e99dd9SToby Isaac             cind++;
4204a5e93ea8SMatthew G. Knepley           }
4205a5e93ea8SMatthew G. Knepley         }
4206a5e93ea8SMatthew G. Knepley       } else {
4207a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4208a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
420997e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
421097e99dd9SToby Isaac             cind++;
421197e99dd9SToby Isaac           }
421297e99dd9SToby Isaac         }
421397e99dd9SToby Isaac       }
421497e99dd9SToby Isaac     } else {
421597e99dd9SToby Isaac       if (perm) {
421697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
421797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
421897e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
421997e99dd9SToby Isaac             cind++;
422097e99dd9SToby Isaac           }
422197e99dd9SToby Isaac         }
422297e99dd9SToby Isaac       } else {
422397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
422497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
422597e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
422697e99dd9SToby Isaac             cind++;
422797e99dd9SToby Isaac           }
4228a5e93ea8SMatthew G. Knepley         }
4229a5e93ea8SMatthew G. Knepley       }
4230a5e93ea8SMatthew G. Knepley     }
4231a5e93ea8SMatthew G. Knepley   }
4232a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4233a5e93ea8SMatthew G. Knepley }
4234a5e93ea8SMatthew G. Knepley 
423597e99dd9SToby 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[])
4236a6dfd86eSKarl Rupp {
4237552f7358SJed Brown   PetscScalar    *a;
42381a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
42391a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
424097e99dd9SToby Isaac   PetscInt        cind = 0, b;
4241552f7358SJed Brown   PetscErrorCode  ierr;
4242552f7358SJed Brown 
4243552f7358SJed Brown   PetscFunctionBegin;
4244552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4245552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
42461a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
42471a271a75SMatthew G. Knepley   a    = &array[foff];
4248552f7358SJed Brown   if (!fcdof || setBC) {
424997e99dd9SToby Isaac     if (clperm) {
425097e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
425197e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4252552f7358SJed Brown     } else {
425397e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
425497e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4255552f7358SJed Brown     }
4256552f7358SJed Brown   } else {
4257552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
425897e99dd9SToby Isaac     if (clperm) {
425997e99dd9SToby Isaac       if (perm) {
426097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
426197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
426297e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4263552f7358SJed Brown         }
4264552f7358SJed Brown       } else {
426597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
426697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
426797e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
426897e99dd9SToby Isaac         }
426997e99dd9SToby Isaac       }
427097e99dd9SToby Isaac     } else {
427197e99dd9SToby Isaac       if (perm) {
427297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
427397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
427497e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
427597e99dd9SToby Isaac         }
427697e99dd9SToby Isaac       } else {
427797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
427897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
427997e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4280552f7358SJed Brown         }
4281552f7358SJed Brown       }
4282552f7358SJed Brown     }
4283552f7358SJed Brown   }
42841a271a75SMatthew G. Knepley   *offset += fdof;
4285552f7358SJed Brown   PetscFunctionReturn(0);
4286552f7358SJed Brown }
4287552f7358SJed Brown 
4288ba322698SMatthew 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[])
4289a5e93ea8SMatthew G. Knepley {
4290a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
42911a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
42921a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4293ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4294ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4295a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4296a5e93ea8SMatthew G. Knepley 
4297a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4298a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4299a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
43001a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43011a271a75SMatthew G. Knepley   a    = &array[foff];
4302a5e93ea8SMatthew G. Knepley   if (fcdof) {
4303ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4304a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
430597e99dd9SToby Isaac     if (clperm) {
430697e99dd9SToby Isaac       if (perm) {
4307ba322698SMatthew G. Knepley         if (comps) {
4308ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4309ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4310ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4311ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4312ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4313ba322698SMatthew G. Knepley           }
4314ba322698SMatthew G. Knepley         } else {
431597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
431697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
431797e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4318a5e93ea8SMatthew G. Knepley               ++cind;
4319a5e93ea8SMatthew G. Knepley             }
4320a5e93ea8SMatthew G. Knepley           }
4321ba322698SMatthew G. Knepley         }
4322ba322698SMatthew G. Knepley       } else {
4323ba322698SMatthew G. Knepley         if (comps) {
4324ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4325ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4326ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4327ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4328ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4329ba322698SMatthew G. Knepley           }
4330a5e93ea8SMatthew G. Knepley         } else {
433197e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
433297e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
433397e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
433497e99dd9SToby Isaac               ++cind;
433597e99dd9SToby Isaac             }
433697e99dd9SToby Isaac           }
433797e99dd9SToby Isaac         }
4338ba322698SMatthew G. Knepley       }
433997e99dd9SToby Isaac     } else {
434097e99dd9SToby Isaac       if (perm) {
4341ba322698SMatthew G. Knepley         if (comps) {
4342ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4343ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4344ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4345ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4346ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4347ba322698SMatthew G. Knepley           }
4348ba322698SMatthew G. Knepley         } else {
434997e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
435097e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
435197e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
435297e99dd9SToby Isaac               ++cind;
435397e99dd9SToby Isaac             }
435497e99dd9SToby Isaac           }
4355ba322698SMatthew G. Knepley         }
4356ba322698SMatthew G. Knepley       } else {
4357ba322698SMatthew G. Knepley         if (comps) {
4358ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4359ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4360ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4361ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4362ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4363ba322698SMatthew G. Knepley           }
436497e99dd9SToby Isaac         } else {
436597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
436697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
436797e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4368a5e93ea8SMatthew G. Knepley               ++cind;
4369a5e93ea8SMatthew G. Knepley             }
4370a5e93ea8SMatthew G. Knepley           }
4371a5e93ea8SMatthew G. Knepley         }
4372a5e93ea8SMatthew G. Knepley       }
4373a5e93ea8SMatthew G. Knepley     }
4374ba322698SMatthew G. Knepley   }
43751a271a75SMatthew G. Knepley   *offset += fdof;
4376a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4377a5e93ea8SMatthew G. Knepley }
4378a5e93ea8SMatthew G. Knepley 
43798f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4380a6dfd86eSKarl Rupp {
4381552f7358SJed Brown   PetscScalar    *array;
43821b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
43831b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4384552f7358SJed Brown   PetscErrorCode  ierr;
4385552f7358SJed Brown 
43861b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4387b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4388b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4389b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4390b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4391b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4392b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4393b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4394b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4395b6ebb6e6SMatthew G. Knepley 
4396b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4397b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4398b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4399b6ebb6e6SMatthew G. Knepley     {
4400b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4401b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4402b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4403b6ebb6e6SMatthew G. Knepley 
4404b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4405b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4406b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4407b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4408b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4409b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4410b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4411b6ebb6e6SMatthew G. Knepley           }
4412b6ebb6e6SMatthew G. Knepley         } else {
4413b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4414b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4415b6ebb6e6SMatthew G. Knepley           }
4416b6ebb6e6SMatthew G. Knepley         }
4417b6ebb6e6SMatthew G. Knepley       } else {
4418b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4419b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4420b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4421b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4422b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4423b6ebb6e6SMatthew G. Knepley           }
4424b6ebb6e6SMatthew G. Knepley         } else {
4425b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4426b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4427b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4428b6ebb6e6SMatthew G. Knepley           }
4429b6ebb6e6SMatthew G. Knepley         }
4430b6ebb6e6SMatthew G. Knepley       }
4431b6ebb6e6SMatthew G. Knepley     }
4432b6ebb6e6SMatthew G. Knepley   }
4433b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4434b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4435b6ebb6e6SMatthew G. Knepley }
44361b406b76SMatthew G. Knepley 
44371b406b76SMatthew G. Knepley /*@C
44381b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
44391b406b76SMatthew G. Knepley 
44401b406b76SMatthew G. Knepley   Not collective
44411b406b76SMatthew G. Knepley 
44421b406b76SMatthew G. Knepley   Input Parameters:
44431b406b76SMatthew G. Knepley + dm - The DM
44441b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
44451b406b76SMatthew G. Knepley . v - The local vector
4446eaf898f9SPatrick Sanan . point - The point in the DM
44471b406b76SMatthew G. Knepley . values - The array of values
444822c1ee49SMatthew 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,
444922c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
44501b406b76SMatthew G. Knepley 
44511b406b76SMatthew G. Knepley   Fortran Notes:
44521b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
44531b406b76SMatthew G. Knepley 
44541b406b76SMatthew G. Knepley   Level: intermediate
44551b406b76SMatthew G. Knepley 
44561b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
44571b406b76SMatthew G. Knepley @*/
44581b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
44591b406b76SMatthew G. Knepley {
44601b406b76SMatthew G. Knepley   PetscSection    clSection;
44611b406b76SMatthew G. Knepley   IS              clPoints;
44621b406b76SMatthew G. Knepley   PetscScalar    *array;
44631b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
446497e99dd9SToby Isaac   const PetscInt *clp, *clperm;
44651a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
44661b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
44671b406b76SMatthew G. Knepley 
44681a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
44691b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4470e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
44711a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44721a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
44731b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
44741b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
44751b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
44768f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
44771b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
44781b406b76SMatthew G. Knepley   }
44791a271a75SMatthew G. Knepley   /* Get points */
448097e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4481923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44821a271a75SMatthew G. Knepley   /* Get array */
4483552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
44841a271a75SMatthew G. Knepley   /* Get values */
4485ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
448697e99dd9SToby Isaac     PetscInt offset = 0, f;
4487552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
448897e99dd9SToby Isaac       const PetscInt    **perms = NULL;
448997e99dd9SToby Isaac       const PetscScalar **flips = NULL;
449097e99dd9SToby Isaac 
449197e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4492552f7358SJed Brown       switch (mode) {
4493552f7358SJed Brown       case INSERT_VALUES:
449497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
449597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
449697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
449797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
449897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4499552f7358SJed Brown         } break;
4500552f7358SJed Brown       case INSERT_ALL_VALUES:
450197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
450297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
450397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
450497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
450597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4506552f7358SJed Brown         } break;
4507a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
450897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
450997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
451097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
451197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4512ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4513a5e93ea8SMatthew G. Knepley         } break;
4514552f7358SJed Brown       case ADD_VALUES:
451597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
451697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
451797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
451897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
451997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4520552f7358SJed Brown         } break;
4521552f7358SJed Brown       case ADD_ALL_VALUES:
452297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
452397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
452497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
452597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
452697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4527552f7358SJed Brown         } break;
4528304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
452997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
453097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
453197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
453297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4533ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4534304ab55fSMatthew G. Knepley         } break;
4535552f7358SJed Brown       default:
4536f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4537552f7358SJed Brown       }
453897e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
45391a271a75SMatthew G. Knepley     }
4540552f7358SJed Brown   } else {
45411a271a75SMatthew G. Knepley     PetscInt dof, off;
454297e99dd9SToby Isaac     const PetscInt    **perms = NULL;
454397e99dd9SToby Isaac     const PetscScalar **flips = NULL;
45441a271a75SMatthew G. Knepley 
454597e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4546552f7358SJed Brown     switch (mode) {
4547552f7358SJed Brown     case INSERT_VALUES:
454897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
454997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
455097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
455197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
455297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
455397e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4554552f7358SJed Brown       } break;
4555552f7358SJed Brown     case INSERT_ALL_VALUES:
455697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
455797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
455897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
455997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
456097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
456197e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4562552f7358SJed Brown       } break;
4563a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
456497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
456597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
456697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
456797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
456897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
456997e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4570a5e93ea8SMatthew G. Knepley       } break;
4571552f7358SJed Brown     case ADD_VALUES:
457297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
457397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
457497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
457597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
457697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
457797e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4578552f7358SJed Brown       } break;
4579552f7358SJed Brown     case ADD_ALL_VALUES:
458097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
458197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
458297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
458397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
458497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
458597e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4586552f7358SJed Brown       } break;
4587304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
458897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
458997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
459097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
459197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
459297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
459397e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4594304ab55fSMatthew G. Knepley       } break;
4595552f7358SJed Brown     default:
4596f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4597552f7358SJed Brown     }
459897e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4599552f7358SJed Brown   }
46001a271a75SMatthew G. Knepley   /* Cleanup points */
4601923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
46021a271a75SMatthew G. Knepley   /* Cleanup array */
4603552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4604552f7358SJed Brown   PetscFunctionReturn(0);
4605552f7358SJed Brown }
4606552f7358SJed Brown 
4607ba322698SMatthew 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)
4608e07394fbSMatthew G. Knepley {
4609e07394fbSMatthew G. Knepley   PetscSection      clSection;
4610e07394fbSMatthew G. Knepley   IS                clPoints;
4611e07394fbSMatthew G. Knepley   PetscScalar       *array;
4612e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4613b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4614e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
461597e99dd9SToby Isaac   PetscInt          offset = 0, f;
4616e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4617e07394fbSMatthew G. Knepley 
4618e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4619e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4620e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
4621e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4622e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4623e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4624e07394fbSMatthew G. Knepley   /* Get points */
4625b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4626923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4627e07394fbSMatthew G. Knepley   /* Get array */
4628e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4629e07394fbSMatthew G. Knepley   /* Get values */
4630e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
463197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
463297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
463397e99dd9SToby Isaac 
4634e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4635e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4636e07394fbSMatthew G. Knepley         PetscInt fdof;
4637e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4638e07394fbSMatthew G. Knepley         offset += fdof;
4639e07394fbSMatthew G. Knepley       }
4640e07394fbSMatthew G. Knepley       continue;
4641e07394fbSMatthew G. Knepley     }
464297e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4643e07394fbSMatthew G. Knepley     switch (mode) {
4644e07394fbSMatthew G. Knepley     case INSERT_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;
4649b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4650e07394fbSMatthew G. Knepley       } break;
4651e07394fbSMatthew G. Knepley     case INSERT_ALL_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;
4656b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4657e07394fbSMatthew G. Knepley         } break;
4658e07394fbSMatthew G. Knepley     case INSERT_BC_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;
4663ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
4664e07394fbSMatthew G. Knepley       } break;
4665e07394fbSMatthew G. Knepley     case ADD_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;
4670b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4671e07394fbSMatthew G. Knepley       } break;
4672e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
467397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
467497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
467597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
467697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4677b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4678e07394fbSMatthew G. Knepley       } break;
4679e07394fbSMatthew G. Knepley     default:
4680f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4681e07394fbSMatthew G. Knepley     }
468297e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4683e07394fbSMatthew G. Knepley   }
4684e07394fbSMatthew G. Knepley   /* Cleanup points */
4685923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4686e07394fbSMatthew G. Knepley   /* Cleanup array */
4687e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4688e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4689e07394fbSMatthew G. Knepley }
4690e07394fbSMatthew G. Knepley 
46917cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4692552f7358SJed Brown {
4693552f7358SJed Brown   PetscMPIInt    rank;
4694552f7358SJed Brown   PetscInt       i, j;
4695552f7358SJed Brown   PetscErrorCode ierr;
4696552f7358SJed Brown 
4697552f7358SJed Brown   PetscFunctionBegin;
469882f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4699eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
4700e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4701e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4702b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4703b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4704e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4705b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4706519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
47077eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4708552f7358SJed Brown #else
4709b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4710552f7358SJed Brown #endif
4711552f7358SJed Brown     }
471277a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4713552f7358SJed Brown   }
4714552f7358SJed Brown   PetscFunctionReturn(0);
4715552f7358SJed Brown }
4716552f7358SJed Brown 
4717552f7358SJed Brown /* . off - The global offset of this point */
47184acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4719a6dfd86eSKarl Rupp {
4720e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4721552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4722552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4723552f7358SJed Brown   PetscInt        cind = 0, k;
4724552f7358SJed Brown   PetscErrorCode  ierr;
4725552f7358SJed Brown 
4726552f7358SJed Brown   PetscFunctionBegin;
4727552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4728552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4729552f7358SJed Brown   if (!cdof || setBC) {
47304acb8e1eSToby Isaac     if (perm) {
47314acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4732552f7358SJed Brown     } else {
47334acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4734552f7358SJed Brown     }
4735552f7358SJed Brown   } else {
4736552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
47374acb8e1eSToby Isaac     if (perm) {
47384acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
47394acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
47404acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
47414acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
47424acb8e1eSToby Isaac           ++cind;
47434acb8e1eSToby Isaac         } else {
47444acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
47454acb8e1eSToby Isaac         }
47464acb8e1eSToby Isaac       }
47474acb8e1eSToby Isaac     } else {
4748552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4749552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4750552f7358SJed Brown           /* Insert check for returning constrained indices */
4751e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4752552f7358SJed Brown           ++cind;
4753552f7358SJed Brown         } else {
4754e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4755552f7358SJed Brown         }
4756552f7358SJed Brown       }
4757552f7358SJed Brown     }
4758552f7358SJed Brown   }
4759e6ccafaeSMatthew G Knepley   *loff += dof;
4760552f7358SJed Brown   PetscFunctionReturn(0);
4761552f7358SJed Brown }
4762552f7358SJed Brown 
47637e29afd2SMatthew G. Knepley /*
47647e29afd2SMatthew G. Knepley   This version only believes the point offset from the globalSection
47657e29afd2SMatthew G. Knepley 
47667e29afd2SMatthew G. Knepley  . off - The global offset of this point
47677e29afd2SMatthew G. Knepley */
47684acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4769a6dfd86eSKarl Rupp {
4770552f7358SJed Brown   PetscInt       numFields, foff, f;
4771552f7358SJed Brown   PetscErrorCode ierr;
4772552f7358SJed Brown 
4773552f7358SJed Brown   PetscFunctionBegin;
4774552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4775552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
47764acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4777552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
47784acb8e1eSToby Isaac     PetscInt        cind = 0, b;
47794acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4780552f7358SJed Brown 
4781552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4782552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4783552f7358SJed Brown     if (!cfdof || setBC) {
47844acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
47854acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4786552f7358SJed Brown     } else {
4787552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
47884acb8e1eSToby Isaac       if (perm) {
47894acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
47904acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
47914acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4792552f7358SJed Brown             ++cind;
4793552f7358SJed Brown           } else {
47944acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4795552f7358SJed Brown           }
4796552f7358SJed Brown         }
4797552f7358SJed Brown       } else {
47984acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
47994acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
48004acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4801552f7358SJed Brown             ++cind;
4802552f7358SJed Brown           } else {
48034acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4804552f7358SJed Brown           }
4805552f7358SJed Brown         }
4806552f7358SJed Brown       }
4807552f7358SJed Brown     }
4808a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4809552f7358SJed Brown     foffs[f] += fdof;
4810552f7358SJed Brown   }
4811552f7358SJed Brown   PetscFunctionReturn(0);
4812552f7358SJed Brown }
4813552f7358SJed Brown 
48147e29afd2SMatthew G. Knepley /*
48157e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
48167e29afd2SMatthew G. Knepley 
48177e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
48187e29afd2SMatthew G. Knepley */
48197e29afd2SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
48207e29afd2SMatthew G. Knepley {
48217e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
48227e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
48237e29afd2SMatthew G. Knepley 
48247e29afd2SMatthew G. Knepley   PetscFunctionBegin;
48257e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
48267e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
48277e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
48287e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
48297e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
48307e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
48317e29afd2SMatthew G. Knepley 
48327e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
48337e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
48347e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
48357e29afd2SMatthew G. Knepley     if (!cfdof || setBC) {
48367e29afd2SMatthew G. Knepley       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = foff+b;}}
48377e29afd2SMatthew G. Knepley       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = foff+b;}}
48387e29afd2SMatthew G. Knepley     } else {
48397e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
48407e29afd2SMatthew G. Knepley       if (perm) {
48417e29afd2SMatthew G. Knepley         for (b = 0; b < fdof; b++) {
48427e29afd2SMatthew G. Knepley           if ((cind < cfdof) && (b == fcdofs[cind])) {
48437e29afd2SMatthew G. Knepley             indices[foffs[f]+perm[b]] = -(foff+b+1);
48447e29afd2SMatthew G. Knepley             ++cind;
48457e29afd2SMatthew G. Knepley           } else {
48467e29afd2SMatthew G. Knepley             indices[foffs[f]+perm[b]] = foff+b-cind;
48477e29afd2SMatthew G. Knepley           }
48487e29afd2SMatthew G. Knepley         }
48497e29afd2SMatthew G. Knepley       } else {
48507e29afd2SMatthew G. Knepley         for (b = 0; b < fdof; b++) {
48517e29afd2SMatthew G. Knepley           if ((cind < cfdof) && (b == fcdofs[cind])) {
48527e29afd2SMatthew G. Knepley             indices[foffs[f]+b] = -(foff+b+1);
48537e29afd2SMatthew G. Knepley             ++cind;
48547e29afd2SMatthew G. Knepley           } else {
48557e29afd2SMatthew G. Knepley             indices[foffs[f]+b] = foff+b-cind;
48567e29afd2SMatthew G. Knepley           }
48577e29afd2SMatthew G. Knepley         }
48587e29afd2SMatthew G. Knepley       }
48597e29afd2SMatthew G. Knepley     }
48607e29afd2SMatthew G. Knepley     foffs[f] += fdof;
48617e29afd2SMatthew G. Knepley   }
48627e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
48637e29afd2SMatthew G. Knepley }
48647e29afd2SMatthew G. Knepley 
48654acb8e1eSToby 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)
4866d3d1a6afSToby Isaac {
4867d3d1a6afSToby Isaac   Mat             cMat;
4868d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4869d3d1a6afSToby Isaac   IS              aIS;
4870d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4871d3d1a6afSToby Isaac   const PetscInt  *anchors;
4872e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4873d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4874d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4875d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4876d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4877d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4878d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4879d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
48806ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4881d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4882d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4883d3d1a6afSToby Isaac 
4884d3d1a6afSToby Isaac   PetscFunctionBegin;
4885d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4886d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4887d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4888d3d1a6afSToby Isaac 
4889a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4890d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4891d3d1a6afSToby Isaac   if (aSec) {
4892d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4893d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4894d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4895d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4896d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4897d3d1a6afSToby Isaac      * into the global matrix anyway) */
4898d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4899d3d1a6afSToby Isaac       PetscInt b    = points[p];
49004b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4901d3d1a6afSToby Isaac 
49024b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
49034b2f2278SToby Isaac       if (!bSecDof) {
49044b2f2278SToby Isaac         continue;
49054b2f2278SToby Isaac       }
4906d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4907d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4908d3d1a6afSToby Isaac       }
4909d3d1a6afSToby Isaac       if (bDof) {
4910d3d1a6afSToby Isaac         /* this point is constrained */
4911d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4912d3d1a6afSToby Isaac         PetscInt bOff, q;
4913d3d1a6afSToby Isaac 
4914d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4915d3d1a6afSToby Isaac         newNumPoints  += bDof;
4916d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4917d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4918d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4919d3d1a6afSToby Isaac           PetscInt aDof;
4920d3d1a6afSToby Isaac 
4921d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4922d3d1a6afSToby Isaac           newNumIndices += aDof;
4923d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4924d3d1a6afSToby Isaac             PetscInt fDof;
4925d3d1a6afSToby Isaac 
4926d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4927d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4928d3d1a6afSToby Isaac           }
4929d3d1a6afSToby Isaac         }
4930d3d1a6afSToby Isaac       }
4931d3d1a6afSToby Isaac       else {
4932d3d1a6afSToby Isaac         /* this point is not constrained */
4933d3d1a6afSToby Isaac         newNumPoints++;
49344b2f2278SToby Isaac         newNumIndices += bSecDof;
4935d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4936d3d1a6afSToby Isaac           PetscInt fDof;
4937d3d1a6afSToby Isaac 
4938d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4939d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4940d3d1a6afSToby Isaac         }
4941d3d1a6afSToby Isaac       }
4942d3d1a6afSToby Isaac     }
4943d3d1a6afSToby Isaac   }
4944d3d1a6afSToby Isaac   if (!anyConstrained) {
494572b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
494672b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
494772b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
494872b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
494972b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4950d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4951d3d1a6afSToby Isaac   }
4952d3d1a6afSToby Isaac 
49536ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
49546ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
49556ecaa68aSToby Isaac 
4956f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4957d3d1a6afSToby Isaac 
49586ecaa68aSToby Isaac   if (!outPoints && !outValues) {
49596ecaa68aSToby Isaac     if (offsets) {
49606ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
49616ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
49626ecaa68aSToby Isaac       }
49636ecaa68aSToby Isaac     }
49646ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
49656ecaa68aSToby Isaac     PetscFunctionReturn(0);
49666ecaa68aSToby Isaac   }
49676ecaa68aSToby Isaac 
4968f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4969d3d1a6afSToby Isaac 
4970f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4971d3d1a6afSToby Isaac 
4972d3d1a6afSToby Isaac   /* workspaces */
4973d3d1a6afSToby Isaac   if (numFields) {
4974d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
497569291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
497669291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4977d3d1a6afSToby Isaac     }
4978d3d1a6afSToby Isaac   }
4979d3d1a6afSToby Isaac   else {
498069291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
498169291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4982d3d1a6afSToby Isaac   }
4983d3d1a6afSToby Isaac 
4984d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4985d3d1a6afSToby Isaac   if (numFields) {
49864b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
49874b2f2278SToby Isaac 
4988d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4989d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
49904b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4991d3d1a6afSToby Isaac 
49924b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
49934b2f2278SToby Isaac       if (!bSecDof) {
49944b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
49954b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
49964b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
49974b2f2278SToby Isaac         }
49984b2f2278SToby Isaac         continue;
49994b2f2278SToby Isaac       }
5000d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5001d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5002d3d1a6afSToby Isaac       }
5003d3d1a6afSToby Isaac       if (bDof) {
5004d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5005d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5006d3d1a6afSToby Isaac 
5007d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5008d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5009d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5010d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5011d3d1a6afSToby Isaac             PetscInt aFDof;
5012d3d1a6afSToby Isaac 
5013d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5014d3d1a6afSToby Isaac             allFDof += aFDof;
5015d3d1a6afSToby Isaac           }
5016d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5017d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5018d3d1a6afSToby Isaac         }
5019d3d1a6afSToby Isaac       }
5020d3d1a6afSToby Isaac       else {
5021d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5022d3d1a6afSToby Isaac           PetscInt fDof;
5023d3d1a6afSToby Isaac 
5024d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5025d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5026d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5027d3d1a6afSToby Isaac         }
5028d3d1a6afSToby Isaac       }
5029d3d1a6afSToby Isaac     }
50304b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
50314b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
50324b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5033d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5034d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5035d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5036d3d1a6afSToby Isaac       }
503719f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
503819f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
503969291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5040d3d1a6afSToby Isaac     }
5041d3d1a6afSToby Isaac   }
5042d3d1a6afSToby Isaac   else {
5043d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5044d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
50454b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5046d3d1a6afSToby Isaac 
50474b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
50484b2f2278SToby Isaac       if (!bSecDof) {
50494b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
50504b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
50514b2f2278SToby Isaac         continue;
50524b2f2278SToby Isaac       }
5053d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5054d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5055d3d1a6afSToby Isaac       }
5056d3d1a6afSToby Isaac       if (bDof) {
50574b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5058d3d1a6afSToby Isaac 
5059d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5060d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5061d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5062d3d1a6afSToby Isaac 
5063d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5064d3d1a6afSToby Isaac           allDof += aDof;
5065d3d1a6afSToby Isaac         }
5066d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
50674b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5068d3d1a6afSToby Isaac       }
5069d3d1a6afSToby Isaac       else {
50704b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5071d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5072d3d1a6afSToby Isaac       }
5073d3d1a6afSToby Isaac     }
5074d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5075d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5076d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5077d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5078d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5079d3d1a6afSToby Isaac     }
508069291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5081d3d1a6afSToby Isaac   }
5082d3d1a6afSToby Isaac 
50836ecaa68aSToby Isaac   /* output arrays */
508469291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
50856ecaa68aSToby Isaac 
5086d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5087d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5088d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
508969291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
509069291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5091d3d1a6afSToby Isaac   if (numFields) {
5092d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5093d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5094d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
50954b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5096d3d1a6afSToby Isaac 
50974b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
50984b2f2278SToby Isaac       if (!bSecDof) {
50994b2f2278SToby Isaac         continue;
51004b2f2278SToby Isaac       }
5101d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5102d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5103d3d1a6afSToby Isaac       }
5104d3d1a6afSToby Isaac       if (bDof) {
5105d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5106d3d1a6afSToby Isaac 
5107d3d1a6afSToby Isaac         fStart[0] = 0;
5108d3d1a6afSToby Isaac         fEnd[0]   = 0;
5109d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5110d3d1a6afSToby Isaac           PetscInt fDof;
5111d3d1a6afSToby Isaac 
5112d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5113d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5114d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5115d3d1a6afSToby Isaac         }
5116d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
51174acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
5118d3d1a6afSToby Isaac 
5119d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5120d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5121d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5122d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5123d3d1a6afSToby Isaac 
5124d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5125d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5126d3d1a6afSToby Isaac         }
5127d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5128d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5129d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5130d3d1a6afSToby Isaac 
5131d3d1a6afSToby 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 */
5132d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5133d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5134302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
51354acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
5136d3d1a6afSToby Isaac         }
5137d3d1a6afSToby Isaac         newP += bDof;
5138d3d1a6afSToby Isaac 
51396ecaa68aSToby Isaac         if (outValues) {
5140d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5141d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5142d3d1a6afSToby 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);
5143d3d1a6afSToby Isaac           }
5144d3d1a6afSToby Isaac         }
51456ecaa68aSToby Isaac       }
5146d3d1a6afSToby Isaac       else {
5147d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5148d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5149d3d1a6afSToby Isaac         newP++;
5150d3d1a6afSToby Isaac       }
5151d3d1a6afSToby Isaac     }
5152d3d1a6afSToby Isaac   } else {
5153d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5154d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5155d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
51564b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5157d3d1a6afSToby Isaac 
51584b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
51594b2f2278SToby Isaac       if (!bSecDof) {
51604b2f2278SToby Isaac         continue;
51614b2f2278SToby Isaac       }
5162d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5163d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5164d3d1a6afSToby Isaac       }
5165d3d1a6afSToby Isaac       if (bDof) {
5166d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5167d3d1a6afSToby Isaac 
5168d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
51694acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
5170d3d1a6afSToby Isaac 
5171d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5172d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5173d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5174d3d1a6afSToby Isaac 
5175d3d1a6afSToby 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 */
5176d3d1a6afSToby Isaac 
5177d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5178d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5179302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
51804acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
5181d3d1a6afSToby Isaac         }
5182d3d1a6afSToby Isaac         newP += bDof;
5183d3d1a6afSToby Isaac 
5184d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
51856ecaa68aSToby Isaac         if (outValues) {
5186d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5187d3d1a6afSToby Isaac         }
51886ecaa68aSToby Isaac       }
5189d3d1a6afSToby Isaac       else {
5190d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5191d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5192d3d1a6afSToby Isaac         newP++;
5193d3d1a6afSToby Isaac       }
5194d3d1a6afSToby Isaac     }
5195d3d1a6afSToby Isaac   }
5196d3d1a6afSToby Isaac 
51976ecaa68aSToby Isaac   if (outValues) {
519869291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5199d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5200d3d1a6afSToby Isaac     /* multiply constraints on the right */
5201d3d1a6afSToby Isaac     if (numFields) {
5202d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5203d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5204d3d1a6afSToby Isaac 
5205d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5206d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5207d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5208d3d1a6afSToby Isaac           PetscInt c, r, k;
5209d3d1a6afSToby Isaac           PetscInt dof;
5210d3d1a6afSToby Isaac 
5211d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
52124b2f2278SToby Isaac           if (!dof) {
52134b2f2278SToby Isaac             continue;
52144b2f2278SToby Isaac           }
5215d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5216d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5217d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5218d3d1a6afSToby Isaac 
5219d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5220d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5221d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
52224acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5223d3d1a6afSToby Isaac                 }
5224d3d1a6afSToby Isaac               }
5225d3d1a6afSToby Isaac             }
5226d3d1a6afSToby Isaac           }
5227d3d1a6afSToby Isaac           else {
5228d3d1a6afSToby Isaac             /* copy this column as is */
5229d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5230d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5231d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5232d3d1a6afSToby Isaac               }
5233d3d1a6afSToby Isaac             }
5234d3d1a6afSToby Isaac           }
5235d3d1a6afSToby Isaac           oldOff += dof;
5236d3d1a6afSToby Isaac         }
5237d3d1a6afSToby Isaac       }
5238d3d1a6afSToby Isaac     }
5239d3d1a6afSToby Isaac     else {
5240d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5241d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5242d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5243d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5244d3d1a6afSToby Isaac         PetscInt c, r, k;
5245d3d1a6afSToby Isaac         PetscInt dof;
5246d3d1a6afSToby Isaac 
5247d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
52484b2f2278SToby Isaac         if (!dof) {
52494b2f2278SToby Isaac           continue;
52504b2f2278SToby Isaac         }
5251d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5252d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5253d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5254d3d1a6afSToby Isaac 
5255d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5256d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5257d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5258d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5259d3d1a6afSToby Isaac               }
5260d3d1a6afSToby Isaac             }
5261d3d1a6afSToby Isaac           }
5262d3d1a6afSToby Isaac         }
5263d3d1a6afSToby Isaac         else {
5264d3d1a6afSToby Isaac           /* copy this column as is */
5265d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5266d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5267d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5268d3d1a6afSToby Isaac             }
5269d3d1a6afSToby Isaac           }
5270d3d1a6afSToby Isaac         }
5271d3d1a6afSToby Isaac         oldOff += dof;
5272d3d1a6afSToby Isaac       }
5273d3d1a6afSToby Isaac     }
5274d3d1a6afSToby Isaac 
52756ecaa68aSToby Isaac     if (multiplyLeft) {
527669291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5277d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5278d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5279d3d1a6afSToby Isaac       if (numFields) {
5280d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5281d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5282d3d1a6afSToby Isaac 
5283d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5284d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5285d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5286d3d1a6afSToby Isaac             PetscInt c, r, k;
5287d3d1a6afSToby Isaac             PetscInt dof;
5288d3d1a6afSToby Isaac 
5289d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5290d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5291d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5292d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5293d3d1a6afSToby Isaac 
5294d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5295d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5296d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5297d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5298d3d1a6afSToby Isaac                   }
5299d3d1a6afSToby Isaac                 }
5300d3d1a6afSToby Isaac               }
5301d3d1a6afSToby Isaac             }
5302d3d1a6afSToby Isaac             else {
5303d3d1a6afSToby Isaac               /* copy this row as is */
5304d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5305d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5306d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5307d3d1a6afSToby Isaac                 }
5308d3d1a6afSToby Isaac               }
5309d3d1a6afSToby Isaac             }
5310d3d1a6afSToby Isaac             oldOff += dof;
5311d3d1a6afSToby Isaac           }
5312d3d1a6afSToby Isaac         }
5313d3d1a6afSToby Isaac       }
5314d3d1a6afSToby Isaac       else {
5315d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5316d3d1a6afSToby Isaac 
5317d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5318d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5319d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5320d3d1a6afSToby Isaac           PetscInt c, r, k;
5321d3d1a6afSToby Isaac           PetscInt dof;
5322d3d1a6afSToby Isaac 
5323d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5324d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5325d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5326d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5327d3d1a6afSToby Isaac 
5328d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5329d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5330d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5331d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5332d3d1a6afSToby Isaac                 }
5333d3d1a6afSToby Isaac               }
5334d3d1a6afSToby Isaac             }
5335d3d1a6afSToby Isaac           }
5336d3d1a6afSToby Isaac           else {
5337d3d1a6afSToby Isaac             /* copy this row as is */
53389fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5339d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5340d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5341d3d1a6afSToby Isaac               }
5342d3d1a6afSToby Isaac             }
5343d3d1a6afSToby Isaac           }
5344d3d1a6afSToby Isaac           oldOff += dof;
5345d3d1a6afSToby Isaac         }
5346d3d1a6afSToby Isaac       }
5347d3d1a6afSToby Isaac 
534869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
53496ecaa68aSToby Isaac     }
53506ecaa68aSToby Isaac     else {
53516ecaa68aSToby Isaac       newValues = tmpValues;
53526ecaa68aSToby Isaac     }
53536ecaa68aSToby Isaac   }
53546ecaa68aSToby Isaac 
5355d3d1a6afSToby Isaac   /* clean up */
535669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
535769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
53586ecaa68aSToby Isaac 
5359d3d1a6afSToby Isaac   if (numFields) {
5360d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
536169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
536269291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
536369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5364d3d1a6afSToby Isaac     }
5365d3d1a6afSToby Isaac   }
5366d3d1a6afSToby Isaac   else {
536769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
536869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
536969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5370d3d1a6afSToby Isaac   }
5371d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5372d3d1a6afSToby Isaac 
5373d3d1a6afSToby Isaac   /* output */
53746ecaa68aSToby Isaac   if (outPoints) {
5375d3d1a6afSToby Isaac     *outPoints = newPoints;
53766ecaa68aSToby Isaac   }
53776ecaa68aSToby Isaac   else {
537869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
53796ecaa68aSToby Isaac   }
538031620726SToby Isaac   if (outValues) {
5381d3d1a6afSToby Isaac     *outValues = newValues;
53826ecaa68aSToby Isaac   }
53836ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5384d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5385d3d1a6afSToby Isaac   }
5386d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5387d3d1a6afSToby Isaac }
5388d3d1a6afSToby Isaac 
53897cd05799SMatthew G. Knepley /*@C
5390a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
53917cd05799SMatthew G. Knepley 
53927cd05799SMatthew G. Knepley   Not collective
53937cd05799SMatthew G. Knepley 
53947cd05799SMatthew G. Knepley   Input Parameters:
53957cd05799SMatthew G. Knepley + dm - The DM
53967cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
53977cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
53987cd05799SMatthew G. Knepley - point - The mesh point
53997cd05799SMatthew G. Knepley 
54007cd05799SMatthew G. Knepley   Output parameters:
54017cd05799SMatthew G. Knepley + numIndices - The number of indices
54027cd05799SMatthew G. Knepley . indices - The indices
54037cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
54047cd05799SMatthew G. Knepley 
54057cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
54067cd05799SMatthew G. Knepley 
54077cd05799SMatthew G. Knepley   Level: advanced
54087cd05799SMatthew G. Knepley 
54097cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
54107cd05799SMatthew G. Knepley @*/
54116ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
54127773e69fSMatthew G. Knepley {
54137773e69fSMatthew G. Knepley   PetscSection    clSection;
54147773e69fSMatthew G. Knepley   IS              clPoints;
54157773e69fSMatthew G. Knepley   const PetscInt *clp;
54164acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
54177773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
54187773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
54197773e69fSMatthew G. Knepley   PetscInt        offsets[32];
54207773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
54217773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
54227773e69fSMatthew G. Knepley 
54237773e69fSMatthew G. Knepley   PetscFunctionBegin;
54247773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54257773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
54267773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
54277773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
54287773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
54297773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
54307773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
54317773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
54327773e69fSMatthew G. Knepley   /* Get points in closure */
5433923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54347773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
54357773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
54367773e69fSMatthew G. Knepley     PetscInt dof, fdof;
54377773e69fSMatthew G. Knepley 
54387773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
54397773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
54407773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
54417773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
54427773e69fSMatthew G. Knepley     }
54437773e69fSMatthew G. Knepley     Nind += dof;
54447773e69fSMatthew G. Knepley   }
54457773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
54468ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
54474acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
54484acb8e1eSToby Isaac   /* Get dual space symmetries */
54494acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
54504acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54514acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54524acb8e1eSToby Isaac   }
54537773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
54547773e69fSMatthew G. Knepley   {
54554acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
54567773e69fSMatthew G. Knepley     if (numPointsNew) {
54574acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
54584acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54594acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54604acb8e1eSToby Isaac       }
54614acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
54624acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
54634acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
54644acb8e1eSToby Isaac       }
5465923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54667773e69fSMatthew G. Knepley       numPoints = numPointsNew;
54677773e69fSMatthew G. Knepley       Nind      = NindNew;
54687773e69fSMatthew G. Knepley       points    = pointsNew;
54697773e69fSMatthew G. Knepley     }
54707773e69fSMatthew G. Knepley   }
54717773e69fSMatthew G. Knepley   /* Calculate indices */
547269291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
54737773e69fSMatthew G. Knepley   if (Nf) {
54746ecaa68aSToby Isaac     if (outOffsets) {
54756ecaa68aSToby Isaac       PetscInt f;
54766ecaa68aSToby Isaac 
547746bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
54786ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
54796ecaa68aSToby Isaac       }
54806ecaa68aSToby Isaac     }
54814acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
54824acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54834acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
54847773e69fSMatthew G. Knepley     }
54857773e69fSMatthew G. Knepley   } else {
54864acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
54874acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
54884acb8e1eSToby Isaac 
54894acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54904acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
54917773e69fSMatthew G. Knepley     }
54927773e69fSMatthew G. Knepley   }
54937773e69fSMatthew G. Knepley   /* Cleanup points */
54944acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
54954acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54964acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54974acb8e1eSToby Isaac   }
54987773e69fSMatthew G. Knepley   if (numPointsNew) {
549969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
55007773e69fSMatthew G. Knepley   } else {
5501923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55027773e69fSMatthew G. Knepley   }
55037773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
55047773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55057773e69fSMatthew G. Knepley }
55067773e69fSMatthew G. Knepley 
55077cd05799SMatthew G. Knepley /*@C
55087cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
55097cd05799SMatthew G. Knepley 
55107cd05799SMatthew G. Knepley   Not collective
55117cd05799SMatthew G. Knepley 
55127cd05799SMatthew G. Knepley   Input Parameters:
55137cd05799SMatthew G. Knepley + dm - The DM
55147cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
55157cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
55167cd05799SMatthew G. Knepley . point - The mesh point
55177cd05799SMatthew G. Knepley . numIndices - The number of indices
55187cd05799SMatthew G. Knepley . indices - The indices
55197cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
55207cd05799SMatthew G. Knepley 
55217cd05799SMatthew G. Knepley   Level: advanced
55227cd05799SMatthew G. Knepley 
55237cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
55247cd05799SMatthew G. Knepley @*/
552546bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
55267773e69fSMatthew G. Knepley {
55277773e69fSMatthew G. Knepley   PetscErrorCode ierr;
55287773e69fSMatthew G. Knepley 
55297773e69fSMatthew G. Knepley   PetscFunctionBegin;
55307773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55317773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
553269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
55337773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55347773e69fSMatthew G. Knepley }
55357773e69fSMatthew G. Knepley 
55367f5d1fdeSMatthew G. Knepley /*@C
55377f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
55387f5d1fdeSMatthew G. Knepley 
55397f5d1fdeSMatthew G. Knepley   Not collective
55407f5d1fdeSMatthew G. Knepley 
55417f5d1fdeSMatthew G. Knepley   Input Parameters:
55427f5d1fdeSMatthew G. Knepley + dm - The DM
5543ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5544ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
55457f5d1fdeSMatthew G. Knepley . A - The matrix
5546eaf898f9SPatrick Sanan . point - The point in the DM
55477f5d1fdeSMatthew G. Knepley . values - The array of values
55487f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
55497f5d1fdeSMatthew G. Knepley 
55507f5d1fdeSMatthew G. Knepley   Fortran Notes:
55517f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
55527f5d1fdeSMatthew G. Knepley 
55537f5d1fdeSMatthew G. Knepley   Level: intermediate
55547f5d1fdeSMatthew G. Knepley 
55557f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
55567f5d1fdeSMatthew G. Knepley @*/
55577c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5558552f7358SJed Brown {
5559552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
55601b406b76SMatthew G. Knepley   PetscSection        clSection;
55611b406b76SMatthew G. Knepley   IS                  clPoints;
5562d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
55631b406b76SMatthew G. Knepley   const PetscInt     *clp;
5564552f7358SJed Brown   PetscInt           *indices;
5565552f7358SJed Brown   PetscInt            offsets[32];
55664acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
55674acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5568923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
55694acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5570d3d1a6afSToby Isaac   PetscScalar        *newValues;
5571552f7358SJed Brown   PetscErrorCode      ierr;
5572552f7358SJed Brown 
5573552f7358SJed Brown   PetscFunctionBegin;
5574552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5575e87a4003SBarry Smith   if (!section) {ierr = DMGetSection(dm, &section);CHKERRQ(ierr);}
55763dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5577e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
55783dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
55793dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5580552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
558182f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5582552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5583923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5584552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5585552f7358SJed Brown     PetscInt fdof;
5586552f7358SJed Brown 
5587552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5588552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5589552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5590552f7358SJed Brown       offsets[f+1] += fdof;
5591552f7358SJed Brown     }
5592552f7358SJed Brown     numIndices += dof;
5593552f7358SJed Brown   }
55940d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
55950d644c17SKarl Rupp 
55968ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
55974acb8e1eSToby Isaac   /* Get symmetries */
55984acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
55994acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56004acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56014acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
56024acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
56034acb8e1eSToby Isaac 
56044acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
56054acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
56064acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
56074acb8e1eSToby Isaac 
56084acb8e1eSToby Isaac         if (!numFields) {
56094acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
56104acb8e1eSToby Isaac         } else {
56114acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
56124acb8e1eSToby Isaac         }
56134acb8e1eSToby Isaac         if (flip) {
56144acb8e1eSToby Isaac           PetscInt i, j, k;
56154acb8e1eSToby Isaac 
56164acb8e1eSToby Isaac           if (!valCopy) {
561769291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56184acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
56194acb8e1eSToby Isaac             values = valCopy;
56204acb8e1eSToby Isaac           }
56214acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5622775f7be2SToby Isaac             PetscScalar fval = flip[i];
56234acb8e1eSToby Isaac 
56244acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
56254acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
56264acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
56274acb8e1eSToby Isaac             }
56284acb8e1eSToby Isaac           }
56294acb8e1eSToby Isaac         }
56304acb8e1eSToby Isaac         foffset += fdof;
56314acb8e1eSToby Isaac       }
56324acb8e1eSToby Isaac     }
56334acb8e1eSToby Isaac   }
56344acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5635d3d1a6afSToby Isaac   if (newNumPoints) {
56364acb8e1eSToby Isaac     if (valCopy) {
563769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56384acb8e1eSToby Isaac     }
56394acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56404acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56414acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56424acb8e1eSToby Isaac     }
56434acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56444acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56454acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56464acb8e1eSToby Isaac     }
5647923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5648d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5649d3d1a6afSToby Isaac     numIndices = newNumIndices;
5650d3d1a6afSToby Isaac     points     = newPoints;
5651d3d1a6afSToby Isaac     values     = newValues;
5652d3d1a6afSToby Isaac   }
565369291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5654552f7358SJed Brown   if (numFields) {
56557e29afd2SMatthew G. Knepley     PetscBool useFieldOffsets;
56567e29afd2SMatthew G. Knepley 
56577e29afd2SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(globalSection, &useFieldOffsets);CHKERRQ(ierr);
56587e29afd2SMatthew G. Knepley     if (useFieldOffsets) {
56597e29afd2SMatthew G. Knepley       for (p = 0; p < numPoints; p++) {
56607e29afd2SMatthew G. Knepley         DMPlexGetIndicesPointFieldsSplit_Internal(section, globalSection, points[2*p], offsets, PETSC_FALSE, perms, p, indices);
56617e29afd2SMatthew G. Knepley       }
56627e29afd2SMatthew G. Knepley     } else {
56634acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
56644acb8e1eSToby Isaac         ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
56654acb8e1eSToby Isaac         DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5666552f7358SJed Brown       }
56677e29afd2SMatthew G. Knepley     }
5668552f7358SJed Brown   } else {
56694acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
56704acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
56714acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
56724acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5673552f7358SJed Brown     }
5674552f7358SJed Brown   }
5675b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5676552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5677ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5678ab1d0545SMatthew G. Knepley     PetscInt i;
5679ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
56808ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5681ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5682ab1d0545SMatthew G. Knepley   }
5683552f7358SJed Brown   if (ierr) {
5684552f7358SJed Brown     PetscMPIInt    rank;
5685552f7358SJed Brown     PetscErrorCode ierr2;
5686552f7358SJed Brown 
568782f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5688e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5689b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
569069291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
5691552f7358SJed Brown     CHKERRQ(ierr);
5692552f7358SJed Brown   }
56934acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
56944acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56954acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56964acb8e1eSToby Isaac   }
5697d3d1a6afSToby Isaac   if (newNumPoints) {
569869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
569969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
5700d3d1a6afSToby Isaac   }
5701d3d1a6afSToby Isaac   else {
57024acb8e1eSToby Isaac     if (valCopy) {
570369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
57044acb8e1eSToby Isaac     }
5705923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5706d3d1a6afSToby Isaac   }
570769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5708552f7358SJed Brown   PetscFunctionReturn(0);
5709552f7358SJed Brown }
5710552f7358SJed Brown 
5711de41b84cSMatthew 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)
5712de41b84cSMatthew G. Knepley {
5713de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5714de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5715de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5716de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5717de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5718de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
57194ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5720de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5721de41b84cSMatthew G. Knepley 
5722de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5723de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5724de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5725e87a4003SBarry Smith   if (!fsection) {ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);}
5726de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5727e87a4003SBarry Smith   if (!csection) {ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);}
5728de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5729e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5730de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5731e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5732de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5733de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5734de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5735de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5736de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5737de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5738de41b84cSMatthew G. Knepley   /* Column indices */
5739de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57404ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5741de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5742de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5743de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5744de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5745de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5746de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5747de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5748de41b84cSMatthew G. Knepley       ++q;
5749de41b84cSMatthew G. Knepley     }
5750de41b84cSMatthew G. Knepley   }
5751de41b84cSMatthew G. Knepley   numCPoints = q;
5752de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5753de41b84cSMatthew G. Knepley     PetscInt fdof;
5754de41b84cSMatthew G. Knepley 
5755de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
57564ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5757de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5758de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5759de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5760de41b84cSMatthew G. Knepley     }
5761de41b84cSMatthew G. Knepley     numCIndices += dof;
5762de41b84cSMatthew G. Knepley   }
5763de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5764de41b84cSMatthew G. Knepley   /* Row indices */
5765de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5766de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
576769291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5768de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5769de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5770de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5771de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5772de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5773de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5774de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
57754ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
57764ca5e9f5SMatthew G. Knepley         if (!dof) continue;
57774ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
57784ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5779de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5780de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5781de41b84cSMatthew G. Knepley         ++q;
5782de41b84cSMatthew G. Knepley       }
5783de41b84cSMatthew G. Knepley     }
5784de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5785de41b84cSMatthew G. Knepley   }
5786de41b84cSMatthew G. Knepley   numFPoints = q;
5787de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5788de41b84cSMatthew G. Knepley     PetscInt fdof;
5789de41b84cSMatthew G. Knepley 
5790de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
57914ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5792de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5793de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5794de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5795de41b84cSMatthew G. Knepley     }
5796de41b84cSMatthew G. Knepley     numFIndices += dof;
5797de41b84cSMatthew G. Knepley   }
5798de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5799de41b84cSMatthew G. Knepley 
58008ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
58018ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
580269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
580369291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5804de41b84cSMatthew G. Knepley   if (numFields) {
58054acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
58064acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
58074acb8e1eSToby Isaac 
58084acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58094acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58104acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5811de41b84cSMatthew G. Knepley     }
58124acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
58134acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
5814367003a6SStefano Zampini       ierr = DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);CHKERRQ(ierr);
58154acb8e1eSToby Isaac     }
58164acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
58174acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
5818367003a6SStefano Zampini       ierr = DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);CHKERRQ(ierr);
58194acb8e1eSToby Isaac     }
58204acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58214acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58224acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5823de41b84cSMatthew G. Knepley     }
5824de41b84cSMatthew G. Knepley   } else {
58254acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
58264acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
58274acb8e1eSToby Isaac 
58284acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58294acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
58304acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
58314acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
58324acb8e1eSToby Isaac 
58334acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
58344acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5835de41b84cSMatthew G. Knepley     }
58364acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
58374acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
58384acb8e1eSToby Isaac 
58394acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
58404acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5841de41b84cSMatthew G. Knepley     }
58424acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58434acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5844de41b84cSMatthew G. Knepley   }
5845de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
58464acb8e1eSToby Isaac   /* TODO: flips */
5847de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5848de41b84cSMatthew G. Knepley   if (ierr) {
5849de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5850de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5851de41b84cSMatthew G. Knepley 
5852de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5853e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5854de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
585569291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
585669291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
5857de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5858de41b84cSMatthew G. Knepley   }
585969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5860de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
586169291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
586269291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5863de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5864de41b84cSMatthew G. Knepley }
5865de41b84cSMatthew G. Knepley 
58667c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
58677c927364SMatthew G. Knepley {
58687c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
58697c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
58707c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
58717c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
58727c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
58737c927364SMatthew G. Knepley   PetscErrorCode ierr;
58747c927364SMatthew G. Knepley 
58757c927364SMatthew G. Knepley   PetscFunctionBegin;
58767c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
58777c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5878e87a4003SBarry Smith   if (!fsection) {ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);}
58797c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5880e87a4003SBarry Smith   if (!csection) {ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);}
58817c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5882e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
58837c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5884e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
58857c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
58867c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
58877c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
58887c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
58897c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
58907c927364SMatthew G. Knepley   /* Column indices */
58917c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
58927c927364SMatthew G. Knepley   maxFPoints = numCPoints;
58937c927364SMatthew G. Knepley   /* Compress out points not in the section */
58947c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
58957c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
58967c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
58977c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
58987c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
58997c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
59007c927364SMatthew G. Knepley       ++q;
59017c927364SMatthew G. Knepley     }
59027c927364SMatthew G. Knepley   }
59037c927364SMatthew G. Knepley   numCPoints = q;
59047c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
59057c927364SMatthew G. Knepley     PetscInt fdof;
59067c927364SMatthew G. Knepley 
59077c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
59087c927364SMatthew G. Knepley     if (!dof) continue;
59097c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59107c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
59117c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
59127c927364SMatthew G. Knepley     }
59137c927364SMatthew G. Knepley     numCIndices += dof;
59147c927364SMatthew G. Knepley   }
59157c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
59167c927364SMatthew G. Knepley   /* Row indices */
59177c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
59187c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
591969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
59207c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
59217c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
59227c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59237c927364SMatthew G. Knepley     /* Compress out points not in the section */
59247c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
59257c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
59267c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
59277c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
59287c927364SMatthew G. Knepley         if (!dof) continue;
59297c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
59307c927364SMatthew G. Knepley         if (s < q) continue;
59317c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
59327c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
59337c927364SMatthew G. Knepley         ++q;
59347c927364SMatthew G. Knepley       }
59357c927364SMatthew G. Knepley     }
59367c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59377c927364SMatthew G. Knepley   }
59387c927364SMatthew G. Knepley   numFPoints = q;
59397c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
59407c927364SMatthew G. Knepley     PetscInt fdof;
59417c927364SMatthew G. Knepley 
59427c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
59437c927364SMatthew G. Knepley     if (!dof) continue;
59447c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59457c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
59467c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
59477c927364SMatthew G. Knepley     }
59487c927364SMatthew G. Knepley     numFIndices += dof;
59497c927364SMatthew G. Knepley   }
59507c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
59517c927364SMatthew G. Knepley 
59528ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
59538ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
59547c927364SMatthew G. Knepley   if (numFields) {
59554acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
59564acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
59574acb8e1eSToby Isaac 
59584acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
59594acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
59604acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
59617c927364SMatthew G. Knepley     }
59624acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
59634acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
59644acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
59654acb8e1eSToby Isaac     }
59664acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
59674acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
59684acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
59694acb8e1eSToby Isaac     }
59704acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
59714acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
59724acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
59737c927364SMatthew G. Knepley     }
59747c927364SMatthew G. Knepley   } else {
59754acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
59764acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
59774acb8e1eSToby Isaac 
59784acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59794acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
59804acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
59814acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
59824acb8e1eSToby Isaac 
59834acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
59844acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
59857c927364SMatthew G. Knepley     }
59864acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
59874acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
59884acb8e1eSToby Isaac 
59894acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
59904acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
59917c927364SMatthew G. Knepley     }
59924acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59934acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
59947c927364SMatthew G. Knepley   }
599569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
59967c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
59977c927364SMatthew G. Knepley   PetscFunctionReturn(0);
59987c927364SMatthew G. Knepley }
59997c927364SMatthew G. Knepley 
6000f96281f8SMatthew G. Knepley /*@
6001f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6002f96281f8SMatthew G. Knepley 
6003f96281f8SMatthew G. Knepley   Input Parameter:
6004f96281f8SMatthew G. Knepley . dm - The DMPlex object
6005f96281f8SMatthew G. Knepley 
6006f96281f8SMatthew G. Knepley   Output Parameters:
6007f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6008dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6009dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6010dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6011f96281f8SMatthew G. Knepley 
6012f96281f8SMatthew G. Knepley   Level: developer
6013f96281f8SMatthew G. Knepley 
6014dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6015f96281f8SMatthew G. Knepley @*/
6016770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6017552f7358SJed Brown {
6018552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6019770b213bSMatthew G Knepley   PetscInt       dim;
6020770b213bSMatthew G Knepley   PetscErrorCode ierr;
6021552f7358SJed Brown 
6022552f7358SJed Brown   PetscFunctionBegin;
6023552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6024c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
60257d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6026770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
60277d5acc75SStefano Zampini   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6028770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6029770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6030552f7358SJed Brown   PetscFunctionReturn(0);
6031552f7358SJed Brown }
6032552f7358SJed Brown 
6033aeadca18SToby Isaac static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
60344a3e9fdbSToby Isaac {
60354a3e9fdbSToby Isaac   IS             is, his;
60367d5acc75SStefano Zampini   PetscInt       first = 0, stride;
60374a3e9fdbSToby Isaac   PetscBool      isStride;
60384a3e9fdbSToby Isaac   PetscErrorCode ierr;
60394a3e9fdbSToby Isaac 
60404a3e9fdbSToby Isaac   PetscFunctionBegin;
60414a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel, d, &is);CHKERRQ(ierr);
60424a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);CHKERRQ(ierr);
60434a3e9fdbSToby Isaac   if (isStride) {
60444a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(is, &first, &stride);CHKERRQ(ierr);
60454a3e9fdbSToby Isaac   }
60467d5acc75SStefano 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);
60474a3e9fdbSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);CHKERRQ(ierr);
6048aeadca18SToby Isaac   ierr = DMLabelSetStratumIS(dimLabel, d, his);CHKERRQ(ierr);
60494a3e9fdbSToby Isaac   ierr = ISDestroy(&his);CHKERRQ(ierr);
60504a3e9fdbSToby Isaac   ierr = ISDestroy(&is);CHKERRQ(ierr);
60514a3e9fdbSToby Isaac   PetscFunctionReturn(0);
60524a3e9fdbSToby Isaac }
60534a3e9fdbSToby Isaac 
6054dc5a3409SMatthew G. Knepley /*@
6055dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6056dc5a3409SMatthew G. Knepley 
6057dc5a3409SMatthew G. Knepley   Input Parameters:
6058dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
6059dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6060dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6061dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6062dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6063dc5a3409SMatthew G. Knepley 
6064dc5a3409SMatthew G. Knepley   Level: developer
6065dc5a3409SMatthew G. Knepley 
6066dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6067dc5a3409SMatthew G. Knepley @*/
6068770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6069552f7358SJed Brown {
6070552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6071770b213bSMatthew G Knepley   PetscInt       dim;
6072770b213bSMatthew G Knepley   PetscErrorCode ierr;
6073552f7358SJed Brown 
6074552f7358SJed Brown   PetscFunctionBegin;
6075552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6076c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
60777d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6078770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
60797d5acc75SStefano Zampini   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6080770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6081770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6082552f7358SJed Brown   PetscFunctionReturn(0);
6083552f7358SJed Brown }
6084552f7358SJed Brown 
60857cd05799SMatthew G. Knepley /*@C
60867cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
60877cd05799SMatthew G. Knepley 
60887cd05799SMatthew G. Knepley   Input Parameter:
60897cd05799SMatthew G. Knepley . dm   - The DMPlex object
60907cd05799SMatthew G. Knepley 
60917cd05799SMatthew G. Knepley   Output Parameter:
60927cd05799SMatthew G. Knepley . cellHeight - The height of a cell
60937cd05799SMatthew G. Knepley 
60947cd05799SMatthew G. Knepley   Level: developer
60957cd05799SMatthew G. Knepley 
60967cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
60977cd05799SMatthew G. Knepley @*/
6098552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6099552f7358SJed Brown {
6100552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6101552f7358SJed Brown 
6102552f7358SJed Brown   PetscFunctionBegin;
6103552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6104552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6105552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6106552f7358SJed Brown   PetscFunctionReturn(0);
6107552f7358SJed Brown }
6108552f7358SJed Brown 
61097cd05799SMatthew G. Knepley /*@C
61107cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
61117cd05799SMatthew G. Knepley 
61127cd05799SMatthew G. Knepley   Input Parameters:
61137cd05799SMatthew G. Knepley + dm   - The DMPlex object
61147cd05799SMatthew G. Knepley - cellHeight - The height of a cell
61157cd05799SMatthew G. Knepley 
61167cd05799SMatthew G. Knepley   Level: developer
61177cd05799SMatthew G. Knepley 
61187cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
61197cd05799SMatthew G. Knepley @*/
6120552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6121552f7358SJed Brown {
6122552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6123552f7358SJed Brown 
6124552f7358SJed Brown   PetscFunctionBegin;
6125552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6126552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6127552f7358SJed Brown   PetscFunctionReturn(0);
6128552f7358SJed Brown }
6129552f7358SJed Brown 
6130552f7358SJed Brown /* We can easily have a form that takes an IS instead */
6131ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6132552f7358SJed Brown {
6133552f7358SJed Brown   PetscSection   section, globalSection;
6134552f7358SJed Brown   PetscInt      *numbers, p;
6135552f7358SJed Brown   PetscErrorCode ierr;
6136552f7358SJed Brown 
6137552f7358SJed Brown   PetscFunctionBegin;
613882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6139552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6140552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6141552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6142552f7358SJed Brown   }
6143552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
614415b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6145854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6146552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6147552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6148ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6149ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6150552f7358SJed Brown   }
615182f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6152ef48cebcSMatthew G. Knepley   if (globalSize) {
6153ef48cebcSMatthew G. Knepley     PetscLayout layout;
6154ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6155ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6156ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6157ef48cebcSMatthew G. Knepley   }
6158552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6159552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6160552f7358SJed Brown   PetscFunctionReturn(0);
6161552f7358SJed Brown }
6162552f7358SJed Brown 
616381ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6164552f7358SJed Brown {
6165552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6166552f7358SJed Brown   PetscErrorCode ierr;
6167552f7358SJed Brown 
6168552f7358SJed Brown   PetscFunctionBegin;
6169552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6170552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
61710298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
617281ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
617381ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
617481ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6175552f7358SJed Brown }
617681ed3555SMatthew G. Knepley 
61778dab3259SMatthew G. Knepley /*@
61787cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
61797cd05799SMatthew G. Knepley 
61807cd05799SMatthew G. Knepley   Input Parameter:
61817cd05799SMatthew G. Knepley . dm   - The DMPlex object
61827cd05799SMatthew G. Knepley 
61837cd05799SMatthew G. Knepley   Output Parameter:
61847cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
61857cd05799SMatthew G. Knepley 
61867cd05799SMatthew G. Knepley   Level: developer
61877cd05799SMatthew G. Knepley 
61887cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
61897cd05799SMatthew G. Knepley @*/
619081ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
619181ed3555SMatthew G. Knepley {
619281ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
619381ed3555SMatthew G. Knepley   PetscErrorCode ierr;
619481ed3555SMatthew G. Knepley 
619581ed3555SMatthew G. Knepley   PetscFunctionBegin;
619681ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
619781ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6198552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6199552f7358SJed Brown   PetscFunctionReturn(0);
6200552f7358SJed Brown }
6201552f7358SJed Brown 
620281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
620381ed3555SMatthew G. Knepley {
620481ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
620581ed3555SMatthew G. Knepley   PetscErrorCode ierr;
620681ed3555SMatthew G. Knepley 
620781ed3555SMatthew G. Knepley   PetscFunctionBegin;
620881ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
620981ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
621081ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
621181ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
621281ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
621381ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
621481ed3555SMatthew G. Knepley }
621581ed3555SMatthew G. Knepley 
62168dab3259SMatthew G. Knepley /*@
62177cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
62187cd05799SMatthew G. Knepley 
62197cd05799SMatthew G. Knepley   Input Parameter:
62207cd05799SMatthew G. Knepley . dm   - The DMPlex object
62217cd05799SMatthew G. Knepley 
62227cd05799SMatthew G. Knepley   Output Parameter:
62237cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
62247cd05799SMatthew G. Knepley 
62257cd05799SMatthew G. Knepley   Level: developer
62267cd05799SMatthew G. Knepley 
62277cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
62287cd05799SMatthew G. Knepley @*/
6229552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6230552f7358SJed Brown {
6231552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6232552f7358SJed Brown   PetscErrorCode ierr;
6233552f7358SJed Brown 
6234552f7358SJed Brown   PetscFunctionBegin;
6235552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
623681ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6237552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6238552f7358SJed Brown   PetscFunctionReturn(0);
6239552f7358SJed Brown }
6240552f7358SJed Brown 
62418dab3259SMatthew G. Knepley /*@
62427cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
62437cd05799SMatthew G. Knepley 
62447cd05799SMatthew G. Knepley   Input Parameter:
62457cd05799SMatthew G. Knepley . dm   - The DMPlex object
62467cd05799SMatthew G. Knepley 
62477cd05799SMatthew G. Knepley   Output Parameter:
62487cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
62497cd05799SMatthew G. Knepley 
62507cd05799SMatthew G. Knepley   Level: developer
62517cd05799SMatthew G. Knepley 
62527cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
62537cd05799SMatthew G. Knepley @*/
6254ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6255ef48cebcSMatthew G. Knepley {
6256ef48cebcSMatthew G. Knepley   IS             nums[4];
6257862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
6258ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6259ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6260ef48cebcSMatthew G. Knepley 
6261ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6262ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6263ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
62648abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
62658abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6266862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6267862913ffSStefano Zampini     PetscInt end;
6268862913ffSStefano Zampini 
6269862913ffSStefano Zampini     depths[d] = depth-d;
6270862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
6271862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
6272862913ffSStefano Zampini   }
6273862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
6274862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
6275862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6276862913ffSStefano Zampini     if (starts[d] >= 0 && depths[d] != gdepths[d]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expected depth %D, found %D",depths[d],gdepths[d]);
6277862913ffSStefano Zampini   }
6278ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6279ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6280ef48cebcSMatthew G. Knepley 
6281862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
6282ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6283ef48cebcSMatthew G. Knepley     shift += gsize;
6284ef48cebcSMatthew G. Knepley   }
6285302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6286ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6287ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6288ef48cebcSMatthew G. Knepley }
6289ef48cebcSMatthew G. Knepley 
629008a22f4bSMatthew G. Knepley 
629108a22f4bSMatthew G. Knepley /*@
629208a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
629308a22f4bSMatthew G. Knepley 
629408a22f4bSMatthew G. Knepley   Input Parameter:
629508a22f4bSMatthew G. Knepley . dm - The DMPlex object
629608a22f4bSMatthew G. Knepley 
629708a22f4bSMatthew G. Knepley   Output Parameter:
629808a22f4bSMatthew G. Knepley . ranks - The rank field
629908a22f4bSMatthew G. Knepley 
630008a22f4bSMatthew G. Knepley   Options Database Keys:
630108a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
630208a22f4bSMatthew G. Knepley 
630308a22f4bSMatthew G. Knepley   Level: intermediate
630408a22f4bSMatthew G. Knepley 
630508a22f4bSMatthew G. Knepley .seealso: DMView()
630608a22f4bSMatthew G. Knepley @*/
630708a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
630808a22f4bSMatthew G. Knepley {
630908a22f4bSMatthew G. Knepley   DM             rdm;
631008a22f4bSMatthew G. Knepley   PetscDS        prob;
631108a22f4bSMatthew G. Knepley   PetscFE        fe;
631208a22f4bSMatthew G. Knepley   PetscScalar   *r;
631308a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
631408a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
631508a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
631608a22f4bSMatthew G. Knepley 
631708a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
6318f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6319f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
632008a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
632108a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
632208a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
6323f95ace6aSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
632408a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
632508a22f4bSMatthew G. Knepley   ierr = DMGetDS(rdm, &prob);CHKERRQ(ierr);
632608a22f4bSMatthew G. Knepley   ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe);CHKERRQ(ierr);
632708a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
632808a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
632908a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
633008a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
633108a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
633208a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
633308a22f4bSMatthew G. Knepley     PetscScalar *lr;
633408a22f4bSMatthew G. Knepley 
633508a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
633608a22f4bSMatthew G. Knepley     *lr = rank;
633708a22f4bSMatthew G. Knepley   }
633808a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
633908a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
634008a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
634108a22f4bSMatthew G. Knepley }
634208a22f4bSMatthew G. Knepley 
6343ca8062c8SMatthew G. Knepley /*@
634418e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
634518e14f0cSMatthew G. Knepley 
634618e14f0cSMatthew G. Knepley   Input Parameters:
634718e14f0cSMatthew G. Knepley + dm    - The DMPlex
634818e14f0cSMatthew G. Knepley - label - The DMLabel
634918e14f0cSMatthew G. Knepley 
635018e14f0cSMatthew G. Knepley   Output Parameter:
635118e14f0cSMatthew G. Knepley . val - The label value field
635218e14f0cSMatthew G. Knepley 
635318e14f0cSMatthew G. Knepley   Options Database Keys:
635418e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
635518e14f0cSMatthew G. Knepley 
635618e14f0cSMatthew G. Knepley   Level: intermediate
635718e14f0cSMatthew G. Knepley 
635818e14f0cSMatthew G. Knepley .seealso: DMView()
635918e14f0cSMatthew G. Knepley @*/
636018e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
636118e14f0cSMatthew G. Knepley {
636218e14f0cSMatthew G. Knepley   DM             rdm;
636318e14f0cSMatthew G. Knepley   PetscDS        prob;
636418e14f0cSMatthew G. Knepley   PetscFE        fe;
636518e14f0cSMatthew G. Knepley   PetscScalar   *v;
636618e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
636718e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
636818e14f0cSMatthew G. Knepley 
636918e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
637018e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
637118e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
637218e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
637318e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
637418e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
637518e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
637618e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
637718e14f0cSMatthew G. Knepley   ierr = DMGetDS(rdm, &prob);CHKERRQ(ierr);
637818e14f0cSMatthew G. Knepley   ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe);CHKERRQ(ierr);
637918e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
638018e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
638118e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
6382effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
638318e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
638418e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
638518e14f0cSMatthew G. Knepley     PetscScalar *lv;
638618e14f0cSMatthew G. Knepley     PetscInt     cval;
638718e14f0cSMatthew G. Knepley 
638818e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
638918e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
639018e14f0cSMatthew G. Knepley     *lv = cval;
639118e14f0cSMatthew G. Knepley   }
639218e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
639318e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
639418e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
639518e14f0cSMatthew G. Knepley }
639618e14f0cSMatthew G. Knepley 
639718e14f0cSMatthew G. Knepley /*@
6398ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6399ca8062c8SMatthew G. Knepley 
640069916449SMatthew G. Knepley   Input Parameter:
640169916449SMatthew G. Knepley . dm - The DMPlex object
6402ca8062c8SMatthew G. Knepley 
6403ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6404ca8062c8SMatthew G. Knepley 
6405ca8062c8SMatthew G. Knepley   Level: developer
6406ca8062c8SMatthew G. Knepley 
64077d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6408ca8062c8SMatthew G. Knepley @*/
6409ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6410ca8062c8SMatthew G. Knepley {
6411ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6412ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6413ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
641457beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
641557beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
6416ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6417ca8062c8SMatthew G. Knepley 
6418ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6419ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6420ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6421ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6422ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6423ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6424ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6425ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6426ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6427ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
642842e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
642942e66dfaSMatthew G. Knepley       PetscInt  d;
643042e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
643142e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
643242e66dfaSMatthew G. Knepley       }
6433ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6434ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6435ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6436ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6437ca8062c8SMatthew G. Knepley       }
643842e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
64398ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6440ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
64418ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6442ca8062c8SMatthew G. Knepley         }
6443302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64448ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6445ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
64468ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6447ca8062c8SMatthew G. Knepley         }
6448302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64498ccfff9cSToby 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]);
64508ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6451ca8062c8SMatthew G. Knepley       }
645242e66dfaSMatthew G. Knepley     }
645357beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
645457beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
6455ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6456ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6457ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6458ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6459ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6460ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
646157beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
646257beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
6463ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6464ca8062c8SMatthew G. Knepley       }
6465ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
64668ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6467ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
64688ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6469ca8062c8SMatthew G. Knepley         }
6470302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64718ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6472ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
64738ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6474ca8062c8SMatthew G. Knepley         }
6475302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64768ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6477ca8062c8SMatthew G. Knepley       }
6478ca8062c8SMatthew G. Knepley     }
6479ca8062c8SMatthew G. Knepley   }
648057beb4faSStefano Zampini   if (storagecheck) {
6481ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6482ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
64838ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
648457beb4faSStefano Zampini   }
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 
661003da9461SVaclav Hapla static PetscErrorCode DMPlexAreAllConePointsInArray_Private(DM dm, PetscInt p, PetscInt npoints, const PetscInt *points, PetscInt *missingPoint)
661103da9461SVaclav Hapla {
661203da9461SVaclav Hapla   PetscInt i,l,n;
661303da9461SVaclav Hapla   const PetscInt *cone;
661403da9461SVaclav Hapla   PetscErrorCode ierr;
661503da9461SVaclav Hapla 
661603da9461SVaclav Hapla   PetscFunctionBegin;
661703da9461SVaclav Hapla   *missingPoint = -1;
661803da9461SVaclav Hapla   ierr = DMPlexGetConeSize(dm, p, &n);CHKERRQ(ierr);
661903da9461SVaclav Hapla   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
662003da9461SVaclav Hapla   for (i=0; i<n; i++) {
662103da9461SVaclav Hapla     ierr = PetscFindInt(cone[i], npoints, points, &l);CHKERRQ(ierr);
662203da9461SVaclav Hapla     if (l < 0) {
662303da9461SVaclav Hapla       *missingPoint = cone[i];
662403da9461SVaclav Hapla       break;
662503da9461SVaclav Hapla     }
662603da9461SVaclav Hapla   }
662703da9461SVaclav Hapla   PetscFunctionReturn(0);
662803da9461SVaclav Hapla }
662903da9461SVaclav Hapla 
663003da9461SVaclav Hapla /*@
663103da9461SVaclav Hapla   DMPlexCheckPointSF - Check that several sufficient conditions are met for the point SF of this plex.
663203da9461SVaclav Hapla 
663303da9461SVaclav Hapla   Input Parameters:
663403da9461SVaclav Hapla . dm - The DMPlex object
663503da9461SVaclav Hapla 
663603da9461SVaclav Hapla   Note: This is mainly intended for debugging/testing purposes.
663703da9461SVaclav Hapla 
663803da9461SVaclav Hapla   Level: developer
663903da9461SVaclav Hapla 
664003da9461SVaclav Hapla .seealso: DMGetPointSF(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
664103da9461SVaclav Hapla @*/
664203da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
664303da9461SVaclav Hapla {
664403da9461SVaclav Hapla   PetscSF sf;
664503da9461SVaclav Hapla   PetscInt d,depth,i,nleaves,p,plo,phi,missingPoint;
664603da9461SVaclav Hapla   const PetscInt *locals;
664703da9461SVaclav Hapla   PetscErrorCode ierr;
664803da9461SVaclav Hapla 
664903da9461SVaclav Hapla   PetscFunctionBegin;
665003da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
665103da9461SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
665203da9461SVaclav Hapla   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
665303da9461SVaclav Hapla   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &locals, NULL);CHKERRQ(ierr);
665403da9461SVaclav Hapla 
6655ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
665603da9461SVaclav Hapla   ierr = DMPlexGetVTKCellHeight(dm, &d);CHKERRQ(ierr);
665703da9461SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, d, &plo, &phi);CHKERRQ(ierr);
665803da9461SVaclav Hapla   for (i=0; i<nleaves; i++) {
665903da9461SVaclav Hapla     p = locals[i];
666003da9461SVaclav Hapla     if (p >= plo && p < phi) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d which is a cell",p);
666103da9461SVaclav Hapla   }
6662ece87651SVaclav Hapla 
6663ece87651SVaclav Hapla   /* 2) if some point is in interface, then all its cone points must be also in interface  */
6664ece87651SVaclav Hapla   for (i=0; i<nleaves; i++) {
6665ece87651SVaclav Hapla     p = locals[i];
6666ece87651SVaclav Hapla     ierr = DMPlexAreAllConePointsInArray_Private(dm, p, nleaves, locals, &missingPoint);CHKERRQ(ierr);
6667ece87651SVaclav Hapla     if (missingPoint >= 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d but not %d from its cone",p,missingPoint);
6668ece87651SVaclav Hapla   }
666903da9461SVaclav Hapla   PetscFunctionReturn(0);
667003da9461SVaclav Hapla }
667103da9461SVaclav Hapla 
6672068a5610SStefano Zampini typedef struct cell_stats
6673068a5610SStefano Zampini {
6674068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
6675068a5610SStefano Zampini   PetscInt  count;
6676068a5610SStefano Zampini } cell_stats_t;
6677068a5610SStefano Zampini 
6678068a5610SStefano Zampini static void cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
6679068a5610SStefano Zampini {
6680068a5610SStefano Zampini   PetscInt i, N = *len;
6681068a5610SStefano Zampini 
6682068a5610SStefano Zampini   for (i = 0; i < N; i++) {
6683068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
6684068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
6685068a5610SStefano Zampini 
6686068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
6687068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
6688068a5610SStefano Zampini     B->sum += A->sum;
6689068a5610SStefano Zampini     B->squaresum += A->squaresum;
6690068a5610SStefano Zampini     B->count += A->count;
6691068a5610SStefano Zampini   }
6692068a5610SStefano Zampini }
6693068a5610SStefano Zampini 
6694068a5610SStefano Zampini /*@
6695068a5610SStefano Zampini   DMPlexCheckCellShape - Checks the Jacobian of the mapping and computes some minimal statistics.
6696068a5610SStefano Zampini 
6697068a5610SStefano Zampini   Input Parameters:
6698068a5610SStefano Zampini + dm - The DMPlex object
6699068a5610SStefano Zampini - output - If true, statistics will be displayed on stdout
6700068a5610SStefano Zampini 
6701068a5610SStefano Zampini   Note: This is mainly intended for debugging/testing purposes.
6702068a5610SStefano Zampini 
6703068a5610SStefano Zampini   Level: developer
6704068a5610SStefano Zampini 
6705068a5610SStefano Zampini .seealso: DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6706068a5610SStefano Zampini @*/
6707068a5610SStefano Zampini PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output)
6708068a5610SStefano Zampini {
6709068a5610SStefano Zampini   PetscMPIInt    rank,size;
6710068a5610SStefano Zampini   PetscInt       dim, c, cStart, cEnd, cMax, count = 0;
6711068a5610SStefano Zampini   cell_stats_t   stats, globalStats;
6712068a5610SStefano Zampini   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
6713068a5610SStefano Zampini   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
6714068a5610SStefano Zampini   DM             dmCoarse;
6715068a5610SStefano Zampini   PetscErrorCode ierr;
6716068a5610SStefano Zampini 
6717068a5610SStefano Zampini   PetscFunctionBegin;
6718068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6719068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
6720068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
6721068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
6722068a5610SStefano Zampini   stats.count = 0;
6723068a5610SStefano Zampini 
6724068a5610SStefano Zampini   ierr = DMGetCoordinateDim(dm,&dim);CHKERRQ(ierr);
6725068a5610SStefano Zampini   ierr = PetscMalloc2(dim * dim, &J, dim * dim, &invJ);CHKERRQ(ierr);
6726068a5610SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
6727068a5610SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,NULL,NULL,NULL);CHKERRQ(ierr);
6728068a5610SStefano Zampini   cMax = cMax < 0 ? cEnd : cMax;
6729068a5610SStefano Zampini   for (c = cStart; c < cMax; c++) {
6730068a5610SStefano Zampini     PetscInt  i;
6731068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
6732068a5610SStefano Zampini 
6733068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
6734068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
6735068a5610SStefano Zampini     for (i = 0; i < dim * dim; i++) {
6736068a5610SStefano Zampini       frobJ    += J[i] * J[i];
6737068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
6738068a5610SStefano Zampini     }
6739068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
6740068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
6741068a5610SStefano Zampini 
6742068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
6743068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
6744068a5610SStefano Zampini     stats.sum       += cond;
6745068a5610SStefano Zampini     stats.squaresum += cond2;
6746068a5610SStefano Zampini     stats.count++;
6747068a5610SStefano Zampini   }
6748068a5610SStefano Zampini 
6749068a5610SStefano Zampini   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
6750068a5610SStefano Zampini   if (size > 1) {
6751068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
6752068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
6753068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
6754068a5610SStefano Zampini     MPI_Op        statReduce;
6755068a5610SStefano Zampini 
6756068a5610SStefano Zampini     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRQ(ierr);
6757068a5610SStefano Zampini     ierr = MPI_Type_commit(&statType);CHKERRQ(ierr);
6758068a5610SStefano Zampini     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRQ(ierr);
6759068a5610SStefano Zampini     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRQ(ierr);
6760068a5610SStefano Zampini     ierr = MPI_Op_free(&statReduce);CHKERRQ(ierr);
6761068a5610SStefano Zampini     ierr = MPI_Type_free(&statType);CHKERRQ(ierr);
6762068a5610SStefano Zampini   } else {
6763068a5610SStefano Zampini     ierr = PetscMemcpy(&globalStats,&stats,sizeof(stats));CHKERRQ(ierr);
6764068a5610SStefano Zampini   }
6765068a5610SStefano Zampini 
6766068a5610SStefano Zampini   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
6767068a5610SStefano Zampini   if (!rank) {
6768068a5610SStefano Zampini     count = globalStats.count;
6769068a5610SStefano Zampini     min   = globalStats.min;
6770068a5610SStefano Zampini     max   = globalStats.max;
6771068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
6772068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
6773068a5610SStefano Zampini   }
6774068a5610SStefano Zampini 
6775068a5610SStefano Zampini   if (output) {
6776068a5610SStefano Zampini     ierr = PetscPrintf(comm,"Mesh with %D cells, shape condition numbers: min = %g, max = %g, mean = %g, stddev = %g\n", count, (double) min, (double) max, (double) mean, (double) stdev);CHKERRQ(ierr);
6777068a5610SStefano Zampini   }
6778068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
6779068a5610SStefano Zampini 
6780068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
6781068a5610SStefano Zampini   if (dmCoarse) {
6782068a5610SStefano Zampini     PetscBool isplex;
6783068a5610SStefano Zampini 
6784068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
6785068a5610SStefano Zampini     if (isplex) {
6786068a5610SStefano Zampini       ierr = DMPlexCheckCellShape(dmCoarse,output);CHKERRQ(ierr);
6787068a5610SStefano Zampini     }
6788068a5610SStefano Zampini   }
6789068a5610SStefano Zampini   PetscFunctionReturn(0);
6790068a5610SStefano Zampini }
6791068a5610SStefano Zampini 
6792bceba477SMatthew G. Knepley /* Pointwise interpolation
6793bceba477SMatthew G. Knepley      Just code FEM for now
6794bceba477SMatthew G. Knepley      u^f = I u^c
67954ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
67964ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
67974ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6798bceba477SMatthew G. Knepley */
6799bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6800bceba477SMatthew G. Knepley {
6801bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6802bceba477SMatthew G. Knepley   PetscInt       m, n;
6803a063dac3SMatthew G. Knepley   void          *ctx;
680468132eb9SMatthew G. Knepley   DM             cdm;
6805fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
6806bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6807bceba477SMatthew G. Knepley 
6808bceba477SMatthew G. Knepley   PetscFunctionBegin;
6809e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6810bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6811e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6812bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
681368132eb9SMatthew G. Knepley 
6814fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
6815bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6816bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6817fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
6818a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
681968132eb9SMatthew G. Knepley 
6820a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
682168132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
682268132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
682368132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
682468132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
68254db47ee9SStefano Zampini   if (scaling) {
68265d1c2e58SMatthew G. Knepley     /* Use naive scaling */
68275d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
68284db47ee9SStefano Zampini   }
6829a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6830a063dac3SMatthew G. Knepley }
6831bceba477SMatthew G. Knepley 
68326dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6833a063dac3SMatthew G. Knepley {
683490748bafSMatthew G. Knepley   PetscErrorCode ierr;
68356dbf9973SLawrence Mitchell   VecScatter     ctx;
683690748bafSMatthew G. Knepley 
6837a063dac3SMatthew G. Knepley   PetscFunctionBegin;
68386dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
68396dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
68406dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6841bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6842bceba477SMatthew G. Knepley }
6843bceba477SMatthew G. Knepley 
6844bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
6845bd041c0cSMatthew G. Knepley {
6846bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
6847bd041c0cSMatthew G. Knepley   PetscInt       m, n;
6848bd041c0cSMatthew G. Knepley   void          *ctx;
6849bd041c0cSMatthew G. Knepley   DM             cdm;
6850bd041c0cSMatthew G. Knepley   PetscBool      regular;
6851bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
6852bd041c0cSMatthew G. Knepley 
6853bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
6854e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6855bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6856e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6857bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
6858bd041c0cSMatthew G. Knepley 
6859bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
6860bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6861bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
6862bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
6863bd041c0cSMatthew G. Knepley 
6864bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
6865bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
6866bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6867bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6868bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
6869bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
6870bd041c0cSMatthew G. Knepley }
6871bd041c0cSMatthew G. Knepley 
68720aef6b92SMatthew G. Knepley /*@
68730aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
68740aef6b92SMatthew G. Knepley 
68750aef6b92SMatthew G. Knepley   Input Parameter:
68760aef6b92SMatthew G. Knepley . dm - The DMPlex object
68770aef6b92SMatthew G. Knepley 
68780aef6b92SMatthew G. Knepley   Output Parameter:
68790aef6b92SMatthew G. Knepley . regular - The flag
68800aef6b92SMatthew G. Knepley 
68810aef6b92SMatthew G. Knepley   Level: intermediate
68820aef6b92SMatthew G. Knepley 
68830aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
68840aef6b92SMatthew G. Knepley @*/
68850aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
68860aef6b92SMatthew G. Knepley {
68870aef6b92SMatthew G. Knepley   PetscFunctionBegin;
68880aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68890aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
68900aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
68910aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
68920aef6b92SMatthew G. Knepley }
68930aef6b92SMatthew G. Knepley 
68940aef6b92SMatthew G. Knepley /*@
68950aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
68960aef6b92SMatthew G. Knepley 
68970aef6b92SMatthew G. Knepley   Input Parameters:
68980aef6b92SMatthew G. Knepley + dm - The DMPlex object
68990aef6b92SMatthew G. Knepley - regular - The flag
69000aef6b92SMatthew G. Knepley 
69010aef6b92SMatthew G. Knepley   Level: intermediate
69020aef6b92SMatthew G. Knepley 
69030aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
69040aef6b92SMatthew G. Knepley @*/
69050aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
69060aef6b92SMatthew G. Knepley {
69070aef6b92SMatthew G. Knepley   PetscFunctionBegin;
69080aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69090aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
69100aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
69110aef6b92SMatthew G. Knepley }
69120aef6b92SMatthew G. Knepley 
6913f7c74593SToby Isaac /* anchors */
6914a68b90caSToby Isaac /*@
6915f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6916f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6917a68b90caSToby Isaac 
6918e228b242SToby Isaac   not collective
6919a68b90caSToby Isaac 
6920a68b90caSToby Isaac   Input Parameters:
6921a68b90caSToby Isaac . dm - The DMPlex object
6922a68b90caSToby Isaac 
6923a68b90caSToby Isaac   Output Parameters:
6924a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6925a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6926a68b90caSToby Isaac 
6927a68b90caSToby Isaac 
6928a68b90caSToby Isaac   Level: intermediate
6929a68b90caSToby Isaac 
6930f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6931a68b90caSToby Isaac @*/
6932a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6933a68b90caSToby Isaac {
6934a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
693541e6d900SToby Isaac   PetscErrorCode ierr;
6936a68b90caSToby Isaac 
6937a68b90caSToby Isaac   PetscFunctionBegin;
6938a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
693941e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6940a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6941a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6942a68b90caSToby Isaac   PetscFunctionReturn(0);
6943a68b90caSToby Isaac }
6944a68b90caSToby Isaac 
6945a68b90caSToby Isaac /*@
6946f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6947f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6948a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6949a68b90caSToby Isaac 
6950a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6951f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6952a68b90caSToby Isaac 
6953e228b242SToby Isaac   collective on dm
6954a68b90caSToby Isaac 
6955a68b90caSToby Isaac   Input Parameters:
6956a68b90caSToby Isaac + dm - The DMPlex object
6957e228b242SToby 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).
6958e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6959a68b90caSToby Isaac 
6960a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6961a68b90caSToby Isaac 
6962a68b90caSToby Isaac   Level: intermediate
6963a68b90caSToby Isaac 
6964f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6965a68b90caSToby Isaac @*/
6966a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6967a68b90caSToby Isaac {
6968a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6969e228b242SToby Isaac   PetscMPIInt    result;
6970a68b90caSToby Isaac   PetscErrorCode ierr;
6971a68b90caSToby Isaac 
6972a68b90caSToby Isaac   PetscFunctionBegin;
6973a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6974e228b242SToby Isaac   if (anchorSection) {
6975e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6976e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6977f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6978e228b242SToby Isaac   }
6979e228b242SToby Isaac   if (anchorIS) {
6980e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6981e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6982f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6983e228b242SToby Isaac   }
6984a68b90caSToby Isaac 
6985a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6986a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6987a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6988a68b90caSToby Isaac 
6989a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6990a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6991a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6992a68b90caSToby Isaac 
6993a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6994a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6995a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6996a68b90caSToby Isaac     const PetscInt *anchors;
6997a68b90caSToby Isaac 
6998a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6999a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
7000a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
7001a68b90caSToby Isaac     for (a = 0; a < size; a++) {
7002a68b90caSToby Isaac       PetscInt p;
7003a68b90caSToby Isaac 
7004a68b90caSToby Isaac       p = anchors[a];
7005a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
7006a68b90caSToby Isaac         PetscInt dof;
7007a68b90caSToby Isaac 
7008a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7009a68b90caSToby Isaac         if (dof) {
7010a68b90caSToby Isaac           PetscErrorCode ierr2;
7011a68b90caSToby Isaac 
7012a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
70138ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
7014a68b90caSToby Isaac         }
7015a68b90caSToby Isaac       }
7016a68b90caSToby Isaac     }
7017a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
7018a68b90caSToby Isaac   }
7019a68b90caSToby Isaac #endif
7020f7c74593SToby Isaac   /* reset the generic constraints */
7021f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
7022a68b90caSToby Isaac   PetscFunctionReturn(0);
7023a68b90caSToby Isaac }
7024a68b90caSToby Isaac 
7025f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7026a68b90caSToby Isaac {
7027f7c74593SToby Isaac   PetscSection anchorSection;
70286995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
7029a68b90caSToby Isaac   PetscErrorCode ierr;
7030a68b90caSToby Isaac 
7031a68b90caSToby Isaac   PetscFunctionBegin;
7032a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7033a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7034e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7035a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
70366995de1eSToby Isaac   if (numFields) {
7037719ab38cSToby Isaac     PetscInt f;
7038a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7039719ab38cSToby Isaac 
7040719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7041719ab38cSToby Isaac       PetscInt numComp;
7042719ab38cSToby Isaac 
7043719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7044719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7045719ab38cSToby Isaac     }
70466995de1eSToby Isaac   }
7047a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
70486995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
70496995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
70506995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
70516995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7052a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7053a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7054a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7055a68b90caSToby Isaac     if (dof) {
7056a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7057a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7058a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7059a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7060a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7061a68b90caSToby Isaac       }
7062a68b90caSToby Isaac     }
7063a68b90caSToby Isaac   }
7064a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7065a68b90caSToby Isaac   PetscFunctionReturn(0);
7066a68b90caSToby Isaac }
7067a68b90caSToby Isaac 
7068f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7069a68b90caSToby Isaac {
7070f7c74593SToby Isaac   PetscSection aSec;
70710ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
70720ac89760SToby Isaac   const PetscInt *anchors;
70730ac89760SToby Isaac   PetscInt numFields, f;
707466ad2231SToby Isaac   IS aIS;
70750ac89760SToby Isaac   PetscErrorCode ierr;
70760ac89760SToby Isaac 
70770ac89760SToby Isaac   PetscFunctionBegin;
70780ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70790ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
70800ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
70810ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
70820ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7083302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7084a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
708566ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
70866995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
70876995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
70880ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
70890ac89760SToby Isaac   i[0] = 0;
70900ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
70910ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7092f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7093f19733c5SToby Isaac 
7094f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7095f19733c5SToby Isaac     if (!rDof) continue;
7096f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
70970ac89760SToby Isaac     if (numFields) {
70980ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
70990ac89760SToby Isaac         annz = 0;
7100f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7101f19733c5SToby Isaac           a = anchors[rOff + r];
71020ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
71030ac89760SToby Isaac           annz += aDof;
71040ac89760SToby Isaac         }
71050ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
71060ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
71070ac89760SToby Isaac         for (q = 0; q < dof; q++) {
71080ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
71090ac89760SToby Isaac         }
71100ac89760SToby Isaac       }
71110ac89760SToby Isaac     }
71120ac89760SToby Isaac     else {
71130ac89760SToby Isaac       annz = 0;
71140ac89760SToby Isaac       for (q = 0; q < dof; q++) {
71150ac89760SToby Isaac         a = anchors[off + q];
71160ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
71170ac89760SToby Isaac         annz += aDof;
71180ac89760SToby Isaac       }
71190ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
71200ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
71210ac89760SToby Isaac       for (q = 0; q < dof; q++) {
71220ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
71230ac89760SToby Isaac       }
71240ac89760SToby Isaac     }
71250ac89760SToby Isaac   }
71260ac89760SToby Isaac   nnz = i[m];
71270ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
71280ac89760SToby Isaac   offset = 0;
71290ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
71300ac89760SToby Isaac     if (numFields) {
71310ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
71320ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
71330ac89760SToby Isaac         for (q = 0; q < dof; q++) {
71340ac89760SToby Isaac           PetscInt rDof, rOff, r;
71350ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
71360ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
71370ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
71380ac89760SToby Isaac             PetscInt s;
71390ac89760SToby Isaac 
71400ac89760SToby Isaac             a = anchors[rOff + r];
71410ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
71420ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
71430ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
71440ac89760SToby Isaac               j[offset++] = aOff + s;
71450ac89760SToby Isaac             }
71460ac89760SToby Isaac           }
71470ac89760SToby Isaac         }
71480ac89760SToby Isaac       }
71490ac89760SToby Isaac     }
71500ac89760SToby Isaac     else {
71510ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
71520ac89760SToby Isaac       for (q = 0; q < dof; q++) {
71530ac89760SToby Isaac         PetscInt rDof, rOff, r;
71540ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
71550ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
71560ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
71570ac89760SToby Isaac           PetscInt s;
71580ac89760SToby Isaac 
71590ac89760SToby Isaac           a = anchors[rOff + r];
71600ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
71610ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
71620ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
71630ac89760SToby Isaac             j[offset++] = aOff + s;
71640ac89760SToby Isaac           }
71650ac89760SToby Isaac         }
71660ac89760SToby Isaac       }
71670ac89760SToby Isaac     }
71680ac89760SToby Isaac   }
71690ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
717025570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
717125570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
717266ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
71730ac89760SToby Isaac   PetscFunctionReturn(0);
71740ac89760SToby Isaac }
71750ac89760SToby Isaac 
717666ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
717766ad2231SToby Isaac {
7178f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7179f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
718066ad2231SToby Isaac   Mat            cMat;
718166ad2231SToby Isaac   PetscErrorCode ierr;
718266ad2231SToby Isaac 
718366ad2231SToby Isaac   PetscFunctionBegin;
718466ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7185a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
718666ad2231SToby Isaac   if (anchorSection) {
7187e228b242SToby Isaac     PetscDS  ds;
7188e228b242SToby Isaac     PetscInt nf;
7189e228b242SToby Isaac 
7190e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
7191f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7192f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
7193e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
7194302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
7195e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
719666ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
719766ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
719866ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
719966ad2231SToby Isaac   }
720066ad2231SToby Isaac   PetscFunctionReturn(0);
720166ad2231SToby Isaac }
7202a93c429eSMatthew G. Knepley 
7203a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
7204a93c429eSMatthew G. Knepley {
7205a93c429eSMatthew G. Knepley   PetscDS        prob;
7206a93c429eSMatthew G. Knepley   IS             subis;
7207a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
7208a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
7209a93c429eSMatthew G. Knepley 
7210a93c429eSMatthew G. Knepley   PetscFunctionBegin;
7211a93c429eSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
7212a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
7213a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
7214a93c429eSMatthew G. Knepley   /* Create subdomain */
7215a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
7216a93c429eSMatthew G. Knepley   /* Create submodel */
7217a93c429eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(*subdm, &subis);CHKERRQ(ierr);
7218a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
7219a93c429eSMatthew G. Knepley   ierr = ISDestroy(&subis);CHKERRQ(ierr);
7220a93c429eSMatthew G. Knepley   ierr = DMSetDefaultSection(*subdm, subsection);CHKERRQ(ierr);
7221a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
7222a93c429eSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
7223a93c429eSMatthew G. Knepley   ierr = DMSetDS(*subdm, prob);CHKERRQ(ierr);
7224a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
7225a93c429eSMatthew G. Knepley   if (is) {
7226a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
7227a93c429eSMatthew G. Knepley     IS              spIS;
7228a93c429eSMatthew G. Knepley     const PetscInt *spmap;
7229a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
7230a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
7231a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
7232a93c429eSMatthew G. Knepley 
7233a93c429eSMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
7234a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
7235a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
7236a93c429eSMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
7237a93c429eSMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
7238a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
7239a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7240a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
7241a93c429eSMatthew G. Knepley 
7242a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
7243a93c429eSMatthew G. Knepley       if (gdof > 0) {
7244a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7245a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
7246a93c429eSMatthew G. Knepley 
7247a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
7248a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
7249a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
7250a93c429eSMatthew G. Knepley         }
7251a93c429eSMatthew G. Knepley         subSize += pSubSize;
7252a93c429eSMatthew G. Knepley         if (pSubSize) {
7253a93c429eSMatthew G. Knepley           if (bs < 0) {
7254a93c429eSMatthew G. Knepley             bs = pSubSize;
7255a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
7256a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
7257a93c429eSMatthew G. Knepley             bs = 1;
7258a93c429eSMatthew G. Knepley           }
7259a93c429eSMatthew G. Knepley         }
7260a93c429eSMatthew G. Knepley       }
7261a93c429eSMatthew G. Knepley     }
7262a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
7263a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
7264a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
7265a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
7266a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
7267a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
7268a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7269a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
7270a93c429eSMatthew G. Knepley 
7271a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
7272a93c429eSMatthew G. Knepley       if (gdof > 0) {
7273a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
7274a93c429eSMatthew G. Knepley 
7275a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
7276a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7277a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
7278a93c429eSMatthew G. Knepley 
7279a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
7280a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
7281a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
7282a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
7283a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
7284a93c429eSMatthew G. Knepley           }
7285a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
7286a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
7287a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
7288a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
7289a93c429eSMatthew G. Knepley           }
7290a93c429eSMatthew G. Knepley         }
7291a93c429eSMatthew G. Knepley       }
7292a93c429eSMatthew G. Knepley     }
7293a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
7294a93c429eSMatthew G. Knepley     ierr = ISDestroy(&spIS);CHKERRQ(ierr);
7295a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
7296a93c429eSMatthew G. Knepley     if (bs > 1) {
7297a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
7298a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
7299a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
7300a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
7301a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
7302a93c429eSMatthew G. Knepley         }
7303a93c429eSMatthew G. Knepley       }
7304a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
7305a93c429eSMatthew G. Knepley     }
7306a93c429eSMatthew G. Knepley     /* Attach nullspace */
7307a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
7308a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
7309a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
7310a93c429eSMatthew G. Knepley     }
7311a93c429eSMatthew G. Knepley     if (f < Nf) {
7312a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
7313a93c429eSMatthew G. Knepley 
7314a93c429eSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);CHKERRQ(ierr);
7315a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
7316a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
7317a93c429eSMatthew G. Knepley     }
7318a93c429eSMatthew G. Knepley   }
7319a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
7320a93c429eSMatthew G. Knepley }
7321