xref: /petsc/src/dm/impls/plex/plex.c (revision a126751eb7c22282655f26917f2fb121ff6a923a)
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>
8552f7358SJed Brown 
9552f7358SJed Brown /* Logging support */
1025afeb17SMatthew G. Knepley PetscLogEvent DMPLEX_Interpolate, PETSCPARTITIONER_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;
11552f7358SJed Brown 
125a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
13552f7358SJed Brown 
14e5337592SStefano Zampini /*@
15e5337592SStefano Zampini   DMPlexRefineSimplexToTensor - Uniformly refines simplicial cells into tensor product cells.
16e5337592SStefano Zampini   3 quadrilaterals per triangle in 2D and 4 hexahedra per tetrahedron in 3D.
17e5337592SStefano Zampini 
18e5337592SStefano Zampini   Collective
19e5337592SStefano Zampini 
20e5337592SStefano Zampini   Input Parameters:
21e5337592SStefano Zampini . dm - The DMPlex object
22e5337592SStefano Zampini 
23e5337592SStefano Zampini   Output Parameters:
24e5337592SStefano Zampini . dmRefined - The refined DMPlex object
25e5337592SStefano Zampini 
26e5337592SStefano Zampini   Note: Returns NULL if the mesh is already a tensor product mesh.
27e5337592SStefano Zampini 
28e5337592SStefano Zampini   Level: intermediate
29e5337592SStefano Zampini 
30e5337592SStefano Zampini .seealso: DMPlexCreate(), DMPlexSetRefinementUniform()
31e5337592SStefano Zampini @*/
32e5337592SStefano Zampini PetscErrorCode DMPlexRefineSimplexToTensor(DM dm, DM *dmRefined)
33e5337592SStefano Zampini {
34e5337592SStefano Zampini   PetscInt         dim, cMax, fMax, cStart, cEnd, coneSize;
35e5337592SStefano Zampini   CellRefiner      cellRefiner;
36e5337592SStefano Zampini   PetscBool        lop, allnoop, localized;
37e5337592SStefano Zampini   PetscErrorCode   ierr;
38e5337592SStefano Zampini 
39e5337592SStefano Zampini   PetscFunctionBegin;
40e5337592SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41e5337592SStefano Zampini   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
42e5337592SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,&fMax,NULL,NULL);CHKERRQ(ierr);
43e5337592SStefano Zampini   if (cMax >= 0 || fMax >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle hybrid meshes yet");
44e5337592SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
45e5337592SStefano Zampini   if (!(cEnd - cStart)) cellRefiner = REFINER_NOOP;
46e5337592SStefano Zampini   else {
47e5337592SStefano Zampini     ierr = DMPlexGetConeSize(dm,cStart,&coneSize);CHKERRQ(ierr);
48e5337592SStefano Zampini     switch (dim) {
49e5337592SStefano Zampini     case 1:
50e5337592SStefano Zampini       cellRefiner = REFINER_NOOP;
51e5337592SStefano Zampini     break;
52e5337592SStefano Zampini     case 2:
53e5337592SStefano Zampini       switch (coneSize) {
54e5337592SStefano Zampini       case 3:
55e5337592SStefano Zampini         cellRefiner = REFINER_SIMPLEX_TO_HEX_2D;
56e5337592SStefano Zampini       break;
57e5337592SStefano Zampini       case 4:
58e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
59e5337592SStefano Zampini       break;
60e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
61e5337592SStefano Zampini       }
62e5337592SStefano Zampini     break;
63e5337592SStefano Zampini     case 3:
64e5337592SStefano Zampini       switch (coneSize) {
65e5337592SStefano Zampini       case 4:
66e5337592SStefano Zampini         cellRefiner = REFINER_SIMPLEX_TO_HEX_3D;
67e5337592SStefano Zampini       break;
68e5337592SStefano Zampini       case 6:
69e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
70e5337592SStefano Zampini       break;
71e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
72e5337592SStefano Zampini       }
73e5337592SStefano Zampini     break;
74e5337592SStefano Zampini     default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle dimension %D",dim);
75e5337592SStefano Zampini     }
76e5337592SStefano Zampini   }
77e5337592SStefano Zampini   /* return if we don't need to refine */
78e5337592SStefano Zampini   lop = (cellRefiner == REFINER_NOOP) ? PETSC_TRUE : PETSC_FALSE;
79e5337592SStefano Zampini   ierr = MPIU_Allreduce(&lop,&allnoop,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
80e5337592SStefano Zampini   if (allnoop) {
81e5337592SStefano Zampini     *dmRefined = NULL;
82e5337592SStefano Zampini     PetscFunctionReturn(0);
83e5337592SStefano Zampini   }
84e5337592SStefano Zampini   ierr = DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);CHKERRQ(ierr);
85e5337592SStefano Zampini   ierr = DMCopyBoundary(dm, *dmRefined);CHKERRQ(ierr);
86e5337592SStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
87e5337592SStefano Zampini   if (localized) {
88e5337592SStefano Zampini     ierr = DMLocalizeCoordinates(*dmRefined);CHKERRQ(ierr);
89e5337592SStefano Zampini   }
90e5337592SStefano Zampini   PetscFunctionReturn(0);
91e5337592SStefano Zampini }
92e5337592SStefano Zampini 
937afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
947e42fee7SMatthew G. Knepley {
9580d5bdc6SMatthew G. Knepley   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior, vdof = 0, cdof = 0;
967e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
977e42fee7SMatthew G. Knepley 
987e42fee7SMatthew G. Knepley   PetscFunctionBegin;
997e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
100c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1017e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1027e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10380d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
10480d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1057e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1067e42fee7SMatthew G. Knepley   if (field >= 0) {
1077e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vdof);CHKERRQ(ierr);}
1087e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &cdof);CHKERRQ(ierr);}
1097e42fee7SMatthew G. Knepley   } else {
1107e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vdof);CHKERRQ(ierr);}
1117e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &cdof);CHKERRQ(ierr);}
1127e42fee7SMatthew G. Knepley   }
1137e42fee7SMatthew G. Knepley   if (vdof) {
1147e42fee7SMatthew G. Knepley     *sStart = vStart;
1157e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
1167e42fee7SMatthew G. Knepley     if (vdof == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1177e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_POINT_FIELD;
1187e42fee7SMatthew G. Knepley   } else if (cdof) {
1197e42fee7SMatthew G. Knepley     *sStart = cStart;
1207e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
1217e42fee7SMatthew G. Knepley     if (cdof == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1227e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_CELL_FIELD;
1237e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
1247e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1257e42fee7SMatthew G. Knepley }
1267e42fee7SMatthew G. Knepley 
1277cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
128e412dcbdSMatthew G. Knepley {
129e412dcbdSMatthew G. Knepley   DM                 dm;
130d1df6f1dSMatthew G. Knepley   PetscSection       s;
131e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
132e412dcbdSMatthew G. Knepley   DM                 cdm;
133e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
134e412dcbdSMatthew G. Knepley   Vec                coordinates;
135e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
136e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
137339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
138339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
139d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
140e412dcbdSMatthew G. Knepley   const char        *name;
141339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
142e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
143e412dcbdSMatthew G. Knepley 
144e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
145d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
146d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
147d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
148d1df6f1dSMatthew G. Knepley 
149e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
150e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1518135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
152d1df6f1dSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
153d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
154e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
155e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
156e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
157e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
158e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
159e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
160e412dcbdSMatthew G. Knepley 
161e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
162339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
163e412dcbdSMatthew G. Knepley 
164e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
165e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
166e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1670c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1680c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
169e412dcbdSMatthew G. Knepley   }
170e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
171e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
172e412dcbdSMatthew G. Knepley 
173d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
174d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
175d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
176d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
177d1df6f1dSMatthew G. Knepley     IS   fis;
178d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
179d1df6f1dSMatthew G. Knepley     const char *fname;
180d1df6f1dSMatthew G. Knepley 
181d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
182d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
183d1df6f1dSMatthew G. Knepley 
184*a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
185d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
186d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
187d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
188d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
189*a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
190*a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
191d1df6f1dSMatthew G. Knepley     }
192d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
193d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
194d1df6f1dSMatthew G. Knepley 
195d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
196d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
197d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
198d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
199d1df6f1dSMatthew G. Knepley 
200d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
201d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
202339e3443SMatthew G. Knepley       if (!flg) {
203d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
204d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
205d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
206339e3443SMatthew G. Knepley       }
207e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
208339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
209d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
210e412dcbdSMatthew G. Knepley 
211d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
212e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
21399a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
214e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
215e412dcbdSMatthew G. Knepley 
216d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
217339e3443SMatthew G. Knepley         if (a) {
218d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
219339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
220339e3443SMatthew G. Knepley         } else {
221339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
222339e3443SMatthew G. Knepley           PetscInt     numVals, va;
223339e3443SMatthew G. Knepley 
224d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
225d1df6f1dSMatthew 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);
226d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
227d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
228d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
229d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
230339e3443SMatthew G. Knepley             break;
231d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
232d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
233d1df6f1dSMatthew 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]);
234d1df6f1dSMatthew G. Knepley             break;
235d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
236339e3443SMatthew G. Knepley           }
237d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
238339e3443SMatthew G. Knepley         }
239e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
240e412dcbdSMatthew G. Knepley         switch (numCoords) {
241e412dcbdSMatthew G. Knepley         case 6:
242339e3443SMatthew 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);
243e412dcbdSMatthew G. Knepley           break;
244e412dcbdSMatthew G. Knepley         case 8:
245339e3443SMatthew 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);
246339e3443SMatthew 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);
247e412dcbdSMatthew G. Knepley           break;
248e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
249e412dcbdSMatthew G. Knepley         }
250e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
251e412dcbdSMatthew G. Knepley       }
252d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
253e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
254e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
255e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
256d1df6f1dSMatthew G. Knepley     }
257d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
258d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
259d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
260d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
261d1df6f1dSMatthew G. Knepley     }
262d1df6f1dSMatthew G. Knepley   }
263e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
264e412dcbdSMatthew G. Knepley }
265e412dcbdSMatthew G. Knepley 
266552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
267552f7358SJed Brown {
268552f7358SJed Brown   DM             dm;
2698135c375SStefano Zampini   PetscBool      isvtk, ishdf5, isdraw, isseq, isglvis;
270552f7358SJed Brown   PetscErrorCode ierr;
271552f7358SJed Brown 
272552f7358SJed Brown   PetscFunctionBegin;
273552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
27482f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
275552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
276b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
277f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
2788135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
279b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
2808135c375SStefano Zampini   if (isvtk || ishdf5 || isglvis) {
281ef31f671SMatthew G. Knepley     PetscInt  numFields;
282ef31f671SMatthew G. Knepley     PetscBool fem = PETSC_FALSE;
283ef31f671SMatthew G. Knepley 
284ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
285ef31f671SMatthew G. Knepley     if (numFields) {
286ef31f671SMatthew G. Knepley       PetscObject fe;
287ef31f671SMatthew G. Knepley 
288ef31f671SMatthew G. Knepley       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
289ef31f671SMatthew G. Knepley       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
290ef31f671SMatthew G. Knepley     }
291bdd6f66aSToby Isaac     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
292ef31f671SMatthew G. Knepley   }
293552f7358SJed Brown   if (isvtk) {
294552f7358SJed Brown     PetscSection            section;
295b136c2c9SMatthew G. Knepley     PetscViewerVTKFieldType ft;
296b136c2c9SMatthew G. Knepley     PetscInt                pStart, pEnd;
297552f7358SJed Brown 
298552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2997afe7537SMatthew G. Knepley     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
300552f7358SJed Brown     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
301b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
302b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
30339d25373SMatthew G. Knepley     ierr = VecView_Plex_Local_HDF5_Internal(v, viewer);CHKERRQ(ierr);
304b136c2c9SMatthew G. Knepley #else
305b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
306b136c2c9SMatthew G. Knepley #endif
3078135c375SStefano Zampini   } else if (isglvis) {
3088135c375SStefano Zampini     ierr = VecView_GLVis(v, viewer);CHKERRQ(ierr);
309f13a32a3SMatthew G. Knepley   } else if (isdraw) {
310f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local_Draw(v, viewer);CHKERRQ(ierr);
311552f7358SJed Brown   } else {
31255f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
31355f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
314552f7358SJed Brown   }
315552f7358SJed Brown   PetscFunctionReturn(0);
316552f7358SJed Brown }
317552f7358SJed Brown 
318552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
319552f7358SJed Brown {
320552f7358SJed Brown   DM             dm;
321f13a32a3SMatthew G. Knepley   PetscReal      time = 0.0;
3228135c375SStefano Zampini   PetscBool      isvtk, ishdf5, isdraw, isseq, isglvis;
323552f7358SJed Brown   PetscErrorCode ierr;
324552f7358SJed Brown 
325552f7358SJed Brown   PetscFunctionBegin;
326552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
32782f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
328552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
32933c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
330e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3318135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3328135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
33355f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
3348135c375SStefano Zampini   if (isvtk || isglvis) {
3358135c375SStefano Zampini     PetscInt    num;
336552f7358SJed Brown     Vec         locv;
337552f7358SJed Brown     const char *name;
338552f7358SJed Brown 
33929c95491SBarry Smith     ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr);
340552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
341552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
342552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
343552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
3448135c375SStefano Zampini     ierr = DMGetOutputSequenceNumber(dm, &num, &time);CHKERRQ(ierr);
345cf9ba882SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
3468135c375SStefano Zampini     ierr = PetscViewerGLVisSetSnapId(viewer, num);CHKERRQ(ierr);
347552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
348b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
349b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
35039d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
351b136c2c9SMatthew G. Knepley #else
352b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
353b136c2c9SMatthew G. Knepley #endif
354e412dcbdSMatthew G. Knepley   } else if (isdraw) {
355f13a32a3SMatthew G. Knepley     Vec         locv;
356f13a32a3SMatthew G. Knepley     const char *name;
357f13a32a3SMatthew G. Knepley 
35829c95491SBarry Smith     ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr);
359f13a32a3SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
360f13a32a3SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
361f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
362f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
363f13a32a3SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
364f13a32a3SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
365f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
366552f7358SJed Brown   } else {
36755f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
36855f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
369552f7358SJed Brown   }
370552f7358SJed Brown   PetscFunctionReturn(0);
371552f7358SJed Brown }
372552f7358SJed Brown 
373d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
374d930f514SMatthew G. Knepley {
375d930f514SMatthew G. Knepley   DM                dm;
376d930f514SMatthew G. Knepley   MPI_Comm          comm;
377d930f514SMatthew G. Knepley   PetscViewerFormat format;
378d930f514SMatthew G. Knepley   Vec               v;
379d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
380d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
381d930f514SMatthew G. Knepley 
382d930f514SMatthew G. Knepley   PetscFunctionBegin;
383d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
384d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
3852c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
386d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
387d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
388d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
389d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
390d930f514SMatthew G. Knepley     const char *vecname;
391d930f514SMatthew G. Knepley     PetscInt    n, nroots;
392d930f514SMatthew G. Knepley 
393d930f514SMatthew G. Knepley     if (dm->sfNatural) {
394ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
395d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
396d930f514SMatthew G. Knepley       if (n == nroots) {
397d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
398d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
399d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
400d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
401d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
402d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
403d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
404d930f514SMatthew G. Knepley   } else {
405d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
406d930f514SMatthew G. Knepley     v = originalv;
407d930f514SMatthew G. Knepley   }
408d930f514SMatthew G. Knepley   if (ishdf5) {
409d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
41039d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
411d930f514SMatthew G. Knepley #else
412d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
413d930f514SMatthew G. Knepley #endif
414d930f514SMatthew G. Knepley   } else if (isvtk) {
415d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
416d930f514SMatthew G. Knepley   } else {
417d930f514SMatthew G. Knepley     PetscBool isseq;
418d930f514SMatthew G. Knepley 
419d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
420d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
421d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
422d930f514SMatthew G. Knepley   }
423d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
424d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
425d930f514SMatthew G. Knepley }
426d930f514SMatthew G. Knepley 
4272c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4282c40f234SMatthew G. Knepley {
4292c40f234SMatthew G. Knepley   DM             dm;
4302c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4312c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4322c40f234SMatthew G. Knepley 
4332c40f234SMatthew G. Knepley   PetscFunctionBegin;
4342c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4352c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4362c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4372c40f234SMatthew G. Knepley   if (ishdf5) {
4382c40f234SMatthew G. Knepley     DM          dmBC;
4392c40f234SMatthew G. Knepley     Vec         gv;
4402c40f234SMatthew G. Knepley     const char *name;
4412c40f234SMatthew G. Knepley 
4422c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4432c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4442c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4452c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4462c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4472c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4482c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4492c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4502c40f234SMatthew G. Knepley   } else {
4512c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4522c40f234SMatthew G. Knepley   }
4532c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4542c40f234SMatthew G. Knepley }
4552c40f234SMatthew G. Knepley 
4562c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4572c40f234SMatthew G. Knepley {
4582c40f234SMatthew G. Knepley   DM             dm;
4592c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4602c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4612c40f234SMatthew G. Knepley 
4622c40f234SMatthew G. Knepley   PetscFunctionBegin;
4632c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4652c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4662c40f234SMatthew G. Knepley   if (ishdf5) {
467878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
46839d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
469b136c2c9SMatthew G. Knepley #else
470b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
471878b459fSMatthew G. Knepley #endif
4722c40f234SMatthew G. Knepley   } else {
4732c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
474552f7358SJed Brown   }
475552f7358SJed Brown   PetscFunctionReturn(0);
476552f7358SJed Brown }
477552f7358SJed Brown 
478d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
479d930f514SMatthew G. Knepley {
480d930f514SMatthew G. Knepley   DM                dm;
481d930f514SMatthew G. Knepley   PetscViewerFormat format;
482d930f514SMatthew G. Knepley   PetscBool         ishdf5;
483d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
484d930f514SMatthew G. Knepley 
485d930f514SMatthew G. Knepley   PetscFunctionBegin;
486d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
487d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
488d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
489d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
490d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
491d930f514SMatthew G. Knepley     if (dm->sfNatural) {
492d930f514SMatthew G. Knepley       if (ishdf5) {
493d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
494d930f514SMatthew G. Knepley         Vec         v;
495d930f514SMatthew G. Knepley         const char *vecname;
496d930f514SMatthew G. Knepley 
497d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
498d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
499d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
50039d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
501d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
502d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
503d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
504d930f514SMatthew G. Knepley #else
505d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
506d930f514SMatthew G. Knepley #endif
507d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
508d930f514SMatthew G. Knepley     }
509d930f514SMatthew G. Knepley   }
510d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
511d930f514SMatthew G. Knepley }
512d930f514SMatthew G. Knepley 
5137cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
514731e8ddeSMatthew G. Knepley {
515731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
516731e8ddeSMatthew G. Knepley   Vec                coordinates;
517731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
518731e8ddeSMatthew G. Knepley   const char        *name[4];
519731e8ddeSMatthew G. Knepley   const PetscScalar *a;
520731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
521731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
522731e8ddeSMatthew G. Knepley 
523731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
524731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
525731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
526731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
527731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
528731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
529731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
530731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
531731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
532731e8ddeSMatthew G. Knepley   name[1]     = "edge";
533731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
534731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
535731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
536731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
537731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
538731e8ddeSMatthew G. Knepley 
539f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
540731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
541731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
542731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
543731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
544731e8ddeSMatthew G. Knepley 
545731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
546731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
547731e8ddeSMatthew G. Knepley       if (!dof) continue;
548731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
549731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
550f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
551731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
552731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
553731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
554731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
55541477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
556731e8ddeSMatthew G. Knepley         }
557731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley       }
559731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
560731e8ddeSMatthew G. Knepley     }
561731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
562731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
563731e8ddeSMatthew G. Knepley   }
564731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
565731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
566731e8ddeSMatthew G. Knepley }
567731e8ddeSMatthew G. Knepley 
5687cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
569552f7358SJed Brown {
570552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
571552f7358SJed Brown   DM                cdm;
572552f7358SJed Brown   DMLabel           markers;
573552f7358SJed Brown   PetscSection      coordSection;
574552f7358SJed Brown   Vec               coordinates;
575552f7358SJed Brown   PetscViewerFormat format;
576552f7358SJed Brown   PetscErrorCode    ierr;
577552f7358SJed Brown 
578552f7358SJed Brown   PetscFunctionBegin;
579552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
580552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
581552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
582552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
583552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
584552f7358SJed Brown     const char *name;
585f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
586552f7358SJed Brown     PetscInt    pStart, pEnd, p;
587552f7358SJed Brown     PetscMPIInt rank, size;
588552f7358SJed Brown 
58982f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
59082f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
591552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
592552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
593552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
594f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
595f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
596f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
597f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
598f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
599552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
600552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
6014d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
6024d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
603552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
604552f7358SJed Brown       PetscInt dof, off, s;
605552f7358SJed Brown 
606552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
607552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
608552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
609e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
610552f7358SJed Brown       }
611552f7358SJed Brown     }
612552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
613552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
614552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
615552f7358SJed Brown       PetscInt dof, off, c;
616552f7358SJed Brown 
617552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
618552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
619552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
620e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
621552f7358SJed Brown       }
622552f7358SJed Brown     }
623552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6244d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6250298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
626552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
627c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
628552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
629552f7358SJed Brown     if (size > 1) {
630552f7358SJed Brown       PetscSF sf;
631552f7358SJed Brown 
632552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
633552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
634552f7358SJed Brown     }
635552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
636552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6370588280cSMatthew G. Knepley     const char  *name, *color;
6380588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6390588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
640552f7358SJed Brown     PetscReal    scale         = 2.0;
6410588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6420588280cSMatthew G. Knepley     double       tcoords[3];
643552f7358SJed Brown     PetscScalar *coords;
6440588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
645552f7358SJed Brown     PetscMPIInt  rank, size;
6460588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
647552f7358SJed Brown 
6480588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
649552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
650c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6510588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6520588280cSMatthew G. Knepley     numColors  = 10;
6530588280cSMatthew G. Knepley     numLColors = 10;
6540588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
655c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
656c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
657c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6580588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
659c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6600588280cSMatthew G. Knepley     if (!useColors) {
6610588280cSMatthew G. Knepley       numColors = 3;
6620588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6630588280cSMatthew G. Knepley     }
664c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6650588280cSMatthew G. Knepley     if (!useColors) {
6660588280cSMatthew G. Knepley       numLColors = 4;
6670588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
6680588280cSMatthew G. Knepley     }
66982f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
67082f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
671552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
672770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
6730588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
674552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
675552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
676552f7358SJed Brown \\usetikzlibrary{arrows}\n\
6770588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
6780588280cSMatthew G. Knepley     if (size > 1) {
679f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
680770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
681770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
682770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
683770b213bSMatthew G Knepley         } else if (p > 0) {
684770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
685770b213bSMatthew G Knepley         }
686770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
687770b213bSMatthew G Knepley       }
6880588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
6890588280cSMatthew G. Knepley     }
6900588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
691552f7358SJed Brown     /* Plot vertices */
692552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
693552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
6944d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
695552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
696552f7358SJed Brown       PetscInt  off, dof, d;
6970588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
698552f7358SJed Brown 
699552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
700552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7010588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
702f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7030588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7040588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
705c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7060588280cSMatthew G. Knepley       }
7070588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7080588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
709552f7358SJed Brown       for (d = 0; d < dof; ++d) {
710552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7110588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
712552f7358SJed Brown       }
7130588280cSMatthew G. Knepley       color = colors[rank%numColors];
7140588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7150588280cSMatthew G. Knepley         PetscInt val;
716c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7170588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7180588280cSMatthew G. Knepley       }
7190588280cSMatthew G. Knepley       if (useNumbers) {
720e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7210588280cSMatthew G. Knepley       } else {
722e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7230588280cSMatthew G. Knepley       }
724552f7358SJed Brown     }
725552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
726552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
727552f7358SJed Brown     /* Plot edges */
7280588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
7290588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
730552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
731552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
732552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
733552f7358SJed Brown         const PetscInt *cone;
734552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
735552f7358SJed Brown 
736552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
737f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
738552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
739552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
740552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
741552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
742552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
743552f7358SJed Brown         for (d = 0; d < dof; ++d) {
7440588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
745c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
746552f7358SJed Brown         }
7470588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
7480588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
7490588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
7500588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
751f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
7520588280cSMatthew G. Knepley         }
7530588280cSMatthew G. Knepley         color = colors[rank%numColors];
7540588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7550588280cSMatthew G. Knepley           PetscInt val;
756c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7570750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7580588280cSMatthew G. Knepley         }
759e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
760552f7358SJed Brown       }
761552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
762552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
763552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
7640588280cSMatthew G. Knepley     }
765552f7358SJed Brown     /* Plot cells */
7660588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
7670588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
7680588280cSMatthew G. Knepley         const PetscInt *cone;
7690588280cSMatthew G. Knepley 
7700588280cSMatthew G. Knepley         color = colors[rank%numColors];
7710588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7720588280cSMatthew G. Knepley           PetscInt val;
773c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
7740588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7750588280cSMatthew G. Knepley         }
7760588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
777e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
7780588280cSMatthew G. Knepley       }
7790588280cSMatthew G. Knepley     } else {
780552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
781552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
7820298fd71SBarry Smith         PetscInt *closure = NULL;
783552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
784552f7358SJed Brown 
785552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
786552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
787552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
788552f7358SJed Brown           const PetscInt point = closure[p];
789552f7358SJed Brown 
790552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
791552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
792e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
793552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
794552f7358SJed Brown         }
795552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
796e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
797552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
798552f7358SJed Brown       }
7990588280cSMatthew G. Knepley     }
800552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8014d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8020588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
803770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
8040588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
8050588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
8060588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
8070588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
808552f7358SJed Brown   } else {
80982f516ccSBarry Smith     MPI_Comm    comm;
810834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
811f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
812552f7358SJed Brown     PetscInt    pStart, pEnd, p;
813a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
8141143a3c0SMatthew G. Knepley     const char *name;
815552f7358SJed Brown     PetscMPIInt size;
816552f7358SJed Brown 
81782f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
818552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
819c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
820f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8215f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
822f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
823f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
824f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
825552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
826b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
827bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
828dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
829552f7358SJed Brown     if (depth == 1) {
830552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
831552f7358SJed Brown       pEnd = pEnd - pStart;
832552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
833f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
834552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
835552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
836552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
837552f7358SJed Brown       pEnd = pEnd - pStart;
838552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
839552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
840552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
841552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
842552f7358SJed Brown     } else {
843cbb7f117SMark Adams       PetscMPIInt rank;
844cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
845552f7358SJed Brown       for (d = 0; d <= dim; d++) {
846552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
847834065abSMatthew G. Knepley         pEnd    -= pStart;
848834065abSMatthew G. Knepley         pMax[d] -= pStart;
849552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
850834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
851552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
852834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
853cbb7f117SMark Adams           if (!rank) {
854834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
855834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
856834065abSMatthew G. Knepley           }
857cbb7f117SMark Adams         }
858552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
859552f7358SJed Brown       }
860552f7358SJed Brown     }
861834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
862c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
863a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
864a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
865a57dd577SMatthew G Knepley       DMLabel         label;
866a57dd577SMatthew G Knepley       const char     *name;
867a57dd577SMatthew G Knepley       IS              valueIS;
868a57dd577SMatthew G Knepley       const PetscInt *values;
869a57dd577SMatthew G Knepley       PetscInt        numValues, v;
870a57dd577SMatthew G Knepley 
871c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
872c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
873a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
874d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
875a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
876a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
877120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
878a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
879a57dd577SMatthew G Knepley         PetscInt size;
880a57dd577SMatthew G Knepley 
881a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
882a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
883d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
884a57dd577SMatthew G Knepley       }
885a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
886120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
887a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
8884d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
889a57dd577SMatthew G Knepley     }
890a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
8918e7ff633SMatthew G. Knepley     if (cdm) {
8928e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
8938e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
8948e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
8958e7ff633SMatthew G. Knepley     }
896552f7358SJed Brown   }
897552f7358SJed Brown   PetscFunctionReturn(0);
898552f7358SJed Brown }
899552f7358SJed Brown 
9007cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
901e412dcbdSMatthew G. Knepley {
902e412dcbdSMatthew G. Knepley   PetscDraw          draw;
903e412dcbdSMatthew G. Knepley   DM                 cdm;
904e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
905e412dcbdSMatthew G. Knepley   Vec                coordinates;
906e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
90729494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
908e412dcbdSMatthew G. Knepley   PetscBool          isnull;
909e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
910e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
911e412dcbdSMatthew G. Knepley 
912e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
913e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
914e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
915e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
916e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
917e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
918e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
919e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
920e412dcbdSMatthew G. Knepley 
921e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
922e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
923e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
924e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
925e412dcbdSMatthew G. Knepley 
926e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
927e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
928e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
9290c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
9300c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
931e412dcbdSMatthew G. Knepley   }
932e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
93329494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
93429494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
93529494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
936e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
937e412dcbdSMatthew G. Knepley 
938e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
939e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
94029494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
941e412dcbdSMatthew G. Knepley 
94229494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
943e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
94429494db1SLisandro Dalcin     switch (coneSize) {
94529494db1SLisandro Dalcin     case 3:
9460c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9470c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9480c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
949e412dcbdSMatthew G. Knepley       break;
95029494db1SLisandro Dalcin     case 4:
9510c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9520c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9530c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9540c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
955e412dcbdSMatthew G. Knepley       break;
95629494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
957e412dcbdSMatthew G. Knepley     }
958e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
959e412dcbdSMatthew G. Knepley   }
960e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
961e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
962e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
963e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
964e412dcbdSMatthew G. Knepley }
965e412dcbdSMatthew G. Knepley 
966552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
967552f7358SJed Brown {
968e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
969552f7358SJed Brown   PetscErrorCode    ierr;
970552f7358SJed Brown 
971552f7358SJed Brown   PetscFunctionBegin;
972552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
973552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
974552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
975fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
976c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
977e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
9788135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
979552f7358SJed Brown   if (iascii) {
9808135c375SStefano Zampini     PetscViewerFormat format;
9818135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
9828135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
9838135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
9848135c375SStefano Zampini     } else {
985552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
9868135c375SStefano Zampini     }
987c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
988c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
9897afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
99039d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
9917afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
992c6ccd67eSMatthew G. Knepley #else
993c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
994552f7358SJed Brown #endif
995e412dcbdSMatthew G. Knepley   } else if (isvtk) {
996fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
997e412dcbdSMatthew G. Knepley   } else if (isdraw) {
998e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
9998135c375SStefano Zampini   } else if (isglvis) {
10008135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
1001fcf6c8fdSToby Isaac   }
1002cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1003cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1004cb3ba0daSMatthew G. Knepley   if (flg) {
1005cb3ba0daSMatthew G. Knepley     Vec ranks;
1006cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1007cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1008cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1009cb3ba0daSMatthew G. Knepley   }
1010552f7358SJed Brown   PetscFunctionReturn(0);
1011552f7358SJed Brown }
1012552f7358SJed Brown 
10132c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
10142c40f234SMatthew G. Knepley {
10152c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
10162c40f234SMatthew G. Knepley   PetscErrorCode ierr;
10172c40f234SMatthew G. Knepley 
10182c40f234SMatthew G. Knepley   PetscFunctionBegin;
10192c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10202c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
10212c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
10222c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
10232c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
10242c40f234SMatthew G. Knepley   else if (ishdf5) {
10252c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
102639d25373SMatthew G. Knepley     ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
10272c40f234SMatthew G. Knepley #else
10282c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1029552f7358SJed Brown #endif
1030552f7358SJed Brown   }
1031552f7358SJed Brown   PetscFunctionReturn(0);
1032552f7358SJed Brown }
1033552f7358SJed Brown 
1034552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1035552f7358SJed Brown {
1036552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1037552f7358SJed Brown   PetscErrorCode ierr;
1038552f7358SJed Brown 
1039552f7358SJed Brown   PetscFunctionBegin;
10408135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
10418135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
10420d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1043552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1044552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1045552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1046552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1047be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1048552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1049552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1050d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1051d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
105277623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1053a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1054552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1055552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1056a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1057a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1058d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1059d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1060d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1061d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1062d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1063d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1064fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1065552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1066552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1067552f7358SJed Brown   PetscFunctionReturn(0);
1068552f7358SJed Brown }
1069552f7358SJed Brown 
1070b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1071552f7358SJed Brown {
10728d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1073acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1074552f7358SJed Brown   PetscInt               localSize;
1075837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1076552f7358SJed Brown   PetscErrorCode         ierr;
1077b412c318SBarry Smith   MatType                mtype;
10781428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1079552f7358SJed Brown 
1080552f7358SJed Brown   PetscFunctionBegin;
1081607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1082b412c318SBarry Smith   mtype = dm->mattype;
1083552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1084552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1085552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
108682f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1087552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1088552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1089552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1090acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1091acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1092552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1093552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1094552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1095552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1096552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1097552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1098552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1099837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1100552f7358SJed Brown   if (!isShell) {
1101be36d101SStefano Zampini     PetscSection subSection;
1102837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
11030be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1104fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1105552f7358SJed Brown 
110600140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1107be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1108be36d101SStefano Zampini       PetscSection section;
1109be36d101SStefano Zampini       PetscInt     size;
1110be36d101SStefano Zampini 
1111be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1112be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1113be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1114be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1115be36d101SStefano Zampini     } else {
111600140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1117be36d101SStefano Zampini     }
1118552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1119be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1120a9d99c84SMatthew G. Knepley       PetscInt bdof;
1121a9d99c84SMatthew G. Knepley 
1122552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1123fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
11241d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
11251d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
11261d17a0a3SMatthew G. Knepley       if (dof) {
11271d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1128be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1129be36d101SStefano Zampini       }
1130be36d101SStefano Zampini       if (isMatIS) {
1131be36d101SStefano Zampini         PetscInt loff,c,off;
1132be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1133be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1134be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1135552f7358SJed Brown       }
11362a28c762SMatthew G Knepley     }
11372a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
11380be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11390be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11400be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11410be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
11424fa26246SMatthew G. Knepley     bs = bs < 0 ? 1 : bs;
1143be36d101SStefano Zampini     if (isMatIS) {
11444fa26246SMatthew G. Knepley       PetscInt l;
11454fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
11464fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
11474fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1148be36d101SStefano Zampini     }
1149be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1150be36d101SStefano Zampini     if (isMatIS) {
1151be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1152be36d101SStefano Zampini     }
11531795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
11548d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1155552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1156552f7358SJed Brown   }
1157b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1158552f7358SJed Brown   PetscFunctionReturn(0);
1159552f7358SJed Brown }
1160552f7358SJed Brown 
11617cd05799SMatthew G. Knepley /*@
1162a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1163be36d101SStefano Zampini 
1164be36d101SStefano Zampini   Not collective
1165be36d101SStefano Zampini 
1166be36d101SStefano Zampini   Input Parameter:
1167be36d101SStefano Zampini . mesh - The DMPlex
1168be36d101SStefano Zampini 
1169be36d101SStefano Zampini   Output Parameters:
1170be36d101SStefano Zampini . subsection - The subdomain section
1171be36d101SStefano Zampini 
1172be36d101SStefano Zampini   Level: developer
1173be36d101SStefano Zampini 
1174be36d101SStefano Zampini .seealso:
11757cd05799SMatthew G. Knepley @*/
1176be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1177be36d101SStefano Zampini {
1178be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1179be36d101SStefano Zampini   PetscErrorCode ierr;
1180be36d101SStefano Zampini 
1181be36d101SStefano Zampini   PetscFunctionBegin;
1182be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1183be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1184be36d101SStefano Zampini     PetscSection section;
1185be36d101SStefano Zampini     PetscSF      sf;
1186be36d101SStefano Zampini 
1187be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1188be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1189be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1190be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1191be36d101SStefano Zampini   }
1192be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1193be36d101SStefano Zampini   PetscFunctionReturn(0);
1194be36d101SStefano Zampini }
1195be36d101SStefano Zampini 
1196552f7358SJed Brown /*@
1197552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1198552f7358SJed Brown 
1199552f7358SJed Brown   Not collective
1200552f7358SJed Brown 
1201552f7358SJed Brown   Input Parameter:
1202552f7358SJed Brown . mesh - The DMPlex
1203552f7358SJed Brown 
1204552f7358SJed Brown   Output Parameters:
1205552f7358SJed Brown + pStart - The first mesh point
1206552f7358SJed Brown - pEnd   - The upper bound for mesh points
1207552f7358SJed Brown 
1208552f7358SJed Brown   Level: beginner
1209552f7358SJed Brown 
1210552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1211552f7358SJed Brown @*/
1212552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1213552f7358SJed Brown {
1214552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1215552f7358SJed Brown   PetscErrorCode ierr;
1216552f7358SJed Brown 
1217552f7358SJed Brown   PetscFunctionBegin;
1218552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1219552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1220552f7358SJed Brown   PetscFunctionReturn(0);
1221552f7358SJed Brown }
1222552f7358SJed Brown 
1223552f7358SJed Brown /*@
1224552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1225552f7358SJed Brown 
1226552f7358SJed Brown   Not collective
1227552f7358SJed Brown 
1228552f7358SJed Brown   Input Parameters:
1229552f7358SJed Brown + mesh - The DMPlex
1230552f7358SJed Brown . pStart - The first mesh point
1231552f7358SJed Brown - pEnd   - The upper bound for mesh points
1232552f7358SJed Brown 
1233552f7358SJed Brown   Output Parameters:
1234552f7358SJed Brown 
1235552f7358SJed Brown   Level: beginner
1236552f7358SJed Brown 
1237552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1238552f7358SJed Brown @*/
1239552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1240552f7358SJed Brown {
1241552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1242552f7358SJed Brown   PetscErrorCode ierr;
1243552f7358SJed Brown 
1244552f7358SJed Brown   PetscFunctionBegin;
1245552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1246552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1247552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1248552f7358SJed Brown   PetscFunctionReturn(0);
1249552f7358SJed Brown }
1250552f7358SJed Brown 
1251552f7358SJed Brown /*@
1252eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1253552f7358SJed Brown 
1254552f7358SJed Brown   Not collective
1255552f7358SJed Brown 
1256552f7358SJed Brown   Input Parameters:
1257552f7358SJed Brown + mesh - The DMPlex
1258eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1259552f7358SJed Brown 
1260552f7358SJed Brown   Output Parameter:
1261552f7358SJed Brown . size - The cone size for point p
1262552f7358SJed Brown 
1263552f7358SJed Brown   Level: beginner
1264552f7358SJed Brown 
1265552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1266552f7358SJed Brown @*/
1267552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1268552f7358SJed Brown {
1269552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1270552f7358SJed Brown   PetscErrorCode ierr;
1271552f7358SJed Brown 
1272552f7358SJed Brown   PetscFunctionBegin;
1273552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1274552f7358SJed Brown   PetscValidPointer(size, 3);
1275552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1276552f7358SJed Brown   PetscFunctionReturn(0);
1277552f7358SJed Brown }
1278552f7358SJed Brown 
1279552f7358SJed Brown /*@
1280eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1281552f7358SJed Brown 
1282552f7358SJed Brown   Not collective
1283552f7358SJed Brown 
1284552f7358SJed Brown   Input Parameters:
1285552f7358SJed Brown + mesh - The DMPlex
1286eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1287552f7358SJed Brown - size - The cone size for point p
1288552f7358SJed Brown 
1289552f7358SJed Brown   Output Parameter:
1290552f7358SJed Brown 
1291552f7358SJed Brown   Note:
1292552f7358SJed Brown   This should be called after DMPlexSetChart().
1293552f7358SJed Brown 
1294552f7358SJed Brown   Level: beginner
1295552f7358SJed Brown 
1296552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1297552f7358SJed Brown @*/
1298552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1299552f7358SJed Brown {
1300552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1301552f7358SJed Brown   PetscErrorCode ierr;
1302552f7358SJed Brown 
1303552f7358SJed Brown   PetscFunctionBegin;
1304552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1305552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
13060d644c17SKarl Rupp 
1307552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1308552f7358SJed Brown   PetscFunctionReturn(0);
1309552f7358SJed Brown }
1310552f7358SJed Brown 
1311f5a469b9SMatthew G. Knepley /*@
1312eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1313f5a469b9SMatthew G. Knepley 
1314f5a469b9SMatthew G. Knepley   Not collective
1315f5a469b9SMatthew G. Knepley 
1316f5a469b9SMatthew G. Knepley   Input Parameters:
1317f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1318eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1319f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1320f5a469b9SMatthew G. Knepley 
1321f5a469b9SMatthew G. Knepley   Output Parameter:
1322f5a469b9SMatthew G. Knepley 
1323f5a469b9SMatthew G. Knepley   Note:
1324f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1325f5a469b9SMatthew G. Knepley 
1326f5a469b9SMatthew G. Knepley   Level: beginner
1327f5a469b9SMatthew G. Knepley 
1328f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1329f5a469b9SMatthew G. Knepley @*/
1330f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1331f5a469b9SMatthew G. Knepley {
1332f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1333f5a469b9SMatthew G. Knepley   PetscInt       csize;
1334f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1335f5a469b9SMatthew G. Knepley 
1336f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1337f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1338f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1339f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1340f5a469b9SMatthew G. Knepley 
1341f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1342f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1343f5a469b9SMatthew G. Knepley }
1344f5a469b9SMatthew G. Knepley 
1345552f7358SJed Brown /*@C
1346eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1347552f7358SJed Brown 
1348552f7358SJed Brown   Not collective
1349552f7358SJed Brown 
1350552f7358SJed Brown   Input Parameters:
1351552f7358SJed Brown + mesh - The DMPlex
1352eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1353552f7358SJed Brown 
1354552f7358SJed Brown   Output Parameter:
1355552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1356552f7358SJed Brown 
1357552f7358SJed Brown   Level: beginner
1358552f7358SJed Brown 
13593813dfbdSMatthew G Knepley   Fortran Notes:
13603813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13613813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13623813dfbdSMatthew G Knepley 
13633813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1364552f7358SJed Brown 
1365552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1366552f7358SJed Brown @*/
1367552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1368552f7358SJed Brown {
1369552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1370552f7358SJed Brown   PetscInt       off;
1371552f7358SJed Brown   PetscErrorCode ierr;
1372552f7358SJed Brown 
1373552f7358SJed Brown   PetscFunctionBegin;
1374552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1375552f7358SJed Brown   PetscValidPointer(cone, 3);
1376552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1377552f7358SJed Brown   *cone = &mesh->cones[off];
1378552f7358SJed Brown   PetscFunctionReturn(0);
1379552f7358SJed Brown }
1380552f7358SJed Brown 
1381552f7358SJed Brown /*@
1382eaf898f9SPatrick Sanan   DMPlexSetCone - Set the points on the in-edges for this point in the DAG
1383552f7358SJed Brown 
1384552f7358SJed Brown   Not collective
1385552f7358SJed Brown 
1386552f7358SJed Brown   Input Parameters:
1387552f7358SJed Brown + mesh - The DMPlex
1388eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1389552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1390552f7358SJed Brown 
1391552f7358SJed Brown   Output Parameter:
1392552f7358SJed Brown 
1393552f7358SJed Brown   Note:
1394552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1395552f7358SJed Brown 
1396552f7358SJed Brown   Level: beginner
1397552f7358SJed Brown 
1398552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1399552f7358SJed Brown @*/
1400552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1401552f7358SJed Brown {
1402552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1403552f7358SJed Brown   PetscInt       pStart, pEnd;
1404552f7358SJed Brown   PetscInt       dof, off, c;
1405552f7358SJed Brown   PetscErrorCode ierr;
1406552f7358SJed Brown 
1407552f7358SJed Brown   PetscFunctionBegin;
1408552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1409552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1410552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1411552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1412552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
141382f516ccSBarry 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);
1414552f7358SJed Brown   for (c = 0; c < dof; ++c) {
141582f516ccSBarry 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);
1416552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1417552f7358SJed Brown   }
1418552f7358SJed Brown   PetscFunctionReturn(0);
1419552f7358SJed Brown }
1420552f7358SJed Brown 
1421552f7358SJed Brown /*@C
1422eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1423552f7358SJed Brown 
1424552f7358SJed Brown   Not collective
1425552f7358SJed Brown 
1426552f7358SJed Brown   Input Parameters:
1427552f7358SJed Brown + mesh - The DMPlex
1428eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1429552f7358SJed Brown 
1430552f7358SJed Brown   Output Parameter:
1431552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1432552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1433552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1434552f7358SJed Brown                     the index of the cone point on which to start.
1435552f7358SJed Brown 
1436552f7358SJed Brown   Level: beginner
1437552f7358SJed Brown 
14383813dfbdSMatthew G Knepley   Fortran Notes:
14393813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14403813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14413813dfbdSMatthew G Knepley 
14423813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1443552f7358SJed Brown 
1444552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1445552f7358SJed Brown @*/
1446552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1447552f7358SJed Brown {
1448552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1449552f7358SJed Brown   PetscInt       off;
1450552f7358SJed Brown   PetscErrorCode ierr;
1451552f7358SJed Brown 
1452552f7358SJed Brown   PetscFunctionBegin;
1453552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1454552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1455552f7358SJed Brown   {
1456552f7358SJed Brown     PetscInt dof;
1457552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1458552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1459552f7358SJed Brown   }
1460552f7358SJed Brown #endif
1461552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
14620d644c17SKarl Rupp 
1463552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1464552f7358SJed Brown   PetscFunctionReturn(0);
1465552f7358SJed Brown }
1466552f7358SJed Brown 
1467552f7358SJed Brown /*@
1468eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1469552f7358SJed Brown 
1470552f7358SJed Brown   Not collective
1471552f7358SJed Brown 
1472552f7358SJed Brown   Input Parameters:
1473552f7358SJed Brown + mesh - The DMPlex
1474eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1475552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1476552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1477552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1478552f7358SJed Brown                     the index of the cone point on which to start.
1479552f7358SJed Brown 
1480552f7358SJed Brown   Output Parameter:
1481552f7358SJed Brown 
1482552f7358SJed Brown   Note:
1483552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1484552f7358SJed Brown 
1485552f7358SJed Brown   Level: beginner
1486552f7358SJed Brown 
1487552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1488552f7358SJed Brown @*/
1489552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1490552f7358SJed Brown {
1491552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1492552f7358SJed Brown   PetscInt       pStart, pEnd;
1493552f7358SJed Brown   PetscInt       dof, off, c;
1494552f7358SJed Brown   PetscErrorCode ierr;
1495552f7358SJed Brown 
1496552f7358SJed Brown   PetscFunctionBegin;
1497552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1498552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1499552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1500552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1501552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
150282f516ccSBarry 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);
1503552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1504552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1505552f7358SJed Brown 
1506552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
150782f516ccSBarry 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);
1508552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1509552f7358SJed Brown   }
1510552f7358SJed Brown   PetscFunctionReturn(0);
1511552f7358SJed Brown }
1512552f7358SJed Brown 
15137cd05799SMatthew G. Knepley /*@
1514eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
15157cd05799SMatthew G. Knepley 
15167cd05799SMatthew G. Knepley   Not collective
15177cd05799SMatthew G. Knepley 
15187cd05799SMatthew G. Knepley   Input Parameters:
15197cd05799SMatthew G. Knepley + mesh - The DMPlex
1520eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15217cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15227cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
15237cd05799SMatthew G. Knepley 
15247cd05799SMatthew G. Knepley   Level: beginner
15257cd05799SMatthew G. Knepley 
15267cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15277cd05799SMatthew G. Knepley @*/
1528552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1529552f7358SJed Brown {
1530552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1531552f7358SJed Brown   PetscInt       pStart, pEnd;
1532552f7358SJed Brown   PetscInt       dof, off;
1533552f7358SJed Brown   PetscErrorCode ierr;
1534552f7358SJed Brown 
1535552f7358SJed Brown   PetscFunctionBegin;
1536552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1537552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
153882f516ccSBarry 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);
153982f516ccSBarry 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);
154077c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
154177c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
154277c88f5bSMatthew 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);
1543552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1544552f7358SJed Brown   PetscFunctionReturn(0);
1545552f7358SJed Brown }
1546552f7358SJed Brown 
15477cd05799SMatthew G. Knepley /*@
1548eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
15497cd05799SMatthew G. Knepley 
15507cd05799SMatthew G. Knepley   Not collective
15517cd05799SMatthew G. Knepley 
15527cd05799SMatthew G. Knepley   Input Parameters:
15537cd05799SMatthew G. Knepley + mesh - The DMPlex
1554eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15557cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15567cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
15577cd05799SMatthew G. Knepley 
15587cd05799SMatthew G. Knepley   Level: beginner
15597cd05799SMatthew G. Knepley 
15607cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15617cd05799SMatthew G. Knepley @*/
156277c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
156377c88f5bSMatthew G Knepley {
156477c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
156577c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
156677c88f5bSMatthew G Knepley   PetscInt       dof, off;
156777c88f5bSMatthew G Knepley   PetscErrorCode ierr;
156877c88f5bSMatthew G Knepley 
156977c88f5bSMatthew G Knepley   PetscFunctionBegin;
157077c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
157177c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
157277c88f5bSMatthew 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);
157377c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
157477c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
157577c88f5bSMatthew 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);
157677c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
157777c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
157877c88f5bSMatthew G Knepley }
157977c88f5bSMatthew G Knepley 
1580552f7358SJed Brown /*@
1581eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
1582552f7358SJed Brown 
1583552f7358SJed Brown   Not collective
1584552f7358SJed Brown 
1585552f7358SJed Brown   Input Parameters:
1586552f7358SJed Brown + mesh - The DMPlex
1587eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1588552f7358SJed Brown 
1589552f7358SJed Brown   Output Parameter:
1590552f7358SJed Brown . size - The support size for point p
1591552f7358SJed Brown 
1592552f7358SJed Brown   Level: beginner
1593552f7358SJed Brown 
1594552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1595552f7358SJed Brown @*/
1596552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1597552f7358SJed Brown {
1598552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1599552f7358SJed Brown   PetscErrorCode ierr;
1600552f7358SJed Brown 
1601552f7358SJed Brown   PetscFunctionBegin;
1602552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1603552f7358SJed Brown   PetscValidPointer(size, 3);
1604552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1605552f7358SJed Brown   PetscFunctionReturn(0);
1606552f7358SJed Brown }
1607552f7358SJed Brown 
1608552f7358SJed Brown /*@
1609eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
1610552f7358SJed Brown 
1611552f7358SJed Brown   Not collective
1612552f7358SJed Brown 
1613552f7358SJed Brown   Input Parameters:
1614552f7358SJed Brown + mesh - The DMPlex
1615eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1616552f7358SJed Brown - size - The support size for point p
1617552f7358SJed Brown 
1618552f7358SJed Brown   Output Parameter:
1619552f7358SJed Brown 
1620552f7358SJed Brown   Note:
1621552f7358SJed Brown   This should be called after DMPlexSetChart().
1622552f7358SJed Brown 
1623552f7358SJed Brown   Level: beginner
1624552f7358SJed Brown 
1625552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1626552f7358SJed Brown @*/
1627552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1628552f7358SJed Brown {
1629552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1630552f7358SJed Brown   PetscErrorCode ierr;
1631552f7358SJed Brown 
1632552f7358SJed Brown   PetscFunctionBegin;
1633552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1634552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
16350d644c17SKarl Rupp 
1636552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1637552f7358SJed Brown   PetscFunctionReturn(0);
1638552f7358SJed Brown }
1639552f7358SJed Brown 
1640552f7358SJed Brown /*@C
1641eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
1642552f7358SJed Brown 
1643552f7358SJed Brown   Not collective
1644552f7358SJed Brown 
1645552f7358SJed Brown   Input Parameters:
1646552f7358SJed Brown + mesh - The DMPlex
1647eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1648552f7358SJed Brown 
1649552f7358SJed Brown   Output Parameter:
1650552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1651552f7358SJed Brown 
1652552f7358SJed Brown   Level: beginner
1653552f7358SJed Brown 
16543813dfbdSMatthew G Knepley   Fortran Notes:
16553813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16563813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16573813dfbdSMatthew G Knepley 
16583813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
16593813dfbdSMatthew G Knepley 
1660552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1661552f7358SJed Brown @*/
1662552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1663552f7358SJed Brown {
1664552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1665552f7358SJed Brown   PetscInt       off;
1666552f7358SJed Brown   PetscErrorCode ierr;
1667552f7358SJed Brown 
1668552f7358SJed Brown   PetscFunctionBegin;
1669552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1670552f7358SJed Brown   PetscValidPointer(support, 3);
1671552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1672552f7358SJed Brown   *support = &mesh->supports[off];
1673552f7358SJed Brown   PetscFunctionReturn(0);
1674552f7358SJed Brown }
1675552f7358SJed Brown 
1676552f7358SJed Brown /*@
1677eaf898f9SPatrick Sanan   DMPlexSetSupport - Set the points on the out-edges for this point in the DAG
1678552f7358SJed Brown 
1679552f7358SJed Brown   Not collective
1680552f7358SJed Brown 
1681552f7358SJed Brown   Input Parameters:
1682552f7358SJed Brown + mesh - The DMPlex
1683eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1684552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1685552f7358SJed Brown 
1686552f7358SJed Brown   Output Parameter:
1687552f7358SJed Brown 
1688552f7358SJed Brown   Note:
1689552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1690552f7358SJed Brown 
1691552f7358SJed Brown   Level: beginner
1692552f7358SJed Brown 
1693552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1694552f7358SJed Brown @*/
1695552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1696552f7358SJed Brown {
1697552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1698552f7358SJed Brown   PetscInt       pStart, pEnd;
1699552f7358SJed Brown   PetscInt       dof, off, c;
1700552f7358SJed Brown   PetscErrorCode ierr;
1701552f7358SJed Brown 
1702552f7358SJed Brown   PetscFunctionBegin;
1703552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1704552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1705552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1706552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1707552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
170882f516ccSBarry 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);
1709552f7358SJed Brown   for (c = 0; c < dof; ++c) {
171082f516ccSBarry 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);
1711552f7358SJed Brown     mesh->supports[off+c] = support[c];
1712552f7358SJed Brown   }
1713552f7358SJed Brown   PetscFunctionReturn(0);
1714552f7358SJed Brown }
1715552f7358SJed Brown 
17167cd05799SMatthew G. Knepley /*@
1717eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
17187cd05799SMatthew G. Knepley 
17197cd05799SMatthew G. Knepley   Not collective
17207cd05799SMatthew G. Knepley 
17217cd05799SMatthew G. Knepley   Input Parameters:
17227cd05799SMatthew G. Knepley + mesh - The DMPlex
1723eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17247cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
17257cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
17267cd05799SMatthew G. Knepley 
17277cd05799SMatthew G. Knepley   Level: beginner
17287cd05799SMatthew G. Knepley 
17297cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17307cd05799SMatthew G. Knepley @*/
1731552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1732552f7358SJed Brown {
1733552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1734552f7358SJed Brown   PetscInt       pStart, pEnd;
1735552f7358SJed Brown   PetscInt       dof, off;
1736552f7358SJed Brown   PetscErrorCode ierr;
1737552f7358SJed Brown 
1738552f7358SJed Brown   PetscFunctionBegin;
1739552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1740552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1741552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1742552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
174382f516ccSBarry 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);
174482f516ccSBarry 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);
174582f516ccSBarry 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);
1746552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1747552f7358SJed Brown   PetscFunctionReturn(0);
1748552f7358SJed Brown }
1749552f7358SJed Brown 
1750552f7358SJed Brown /*@C
1751eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
1752552f7358SJed Brown 
1753552f7358SJed Brown   Not collective
1754552f7358SJed Brown 
1755552f7358SJed Brown   Input Parameters:
1756552f7358SJed Brown + mesh - The DMPlex
1757eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1758552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17590298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1760552f7358SJed Brown 
1761552f7358SJed Brown   Output Parameters:
1762552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1763552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1764552f7358SJed Brown 
1765552f7358SJed Brown   Note:
17660298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1767552f7358SJed Brown 
17683813dfbdSMatthew G Knepley   Fortran Notes:
17693813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17703813dfbdSMatthew G Knepley   include petsc.h90 in your code.
17713813dfbdSMatthew G Knepley 
17723813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17733813dfbdSMatthew G Knepley 
1774552f7358SJed Brown   Level: beginner
1775552f7358SJed Brown 
1776552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1777552f7358SJed Brown @*/
1778552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1779552f7358SJed Brown {
1780552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1781552f7358SJed Brown   PetscInt       *closure, *fifo;
17820298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1783552f7358SJed Brown   PetscInt        tmpSize, t;
1784552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1785552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1786552f7358SJed Brown   PetscErrorCode  ierr;
1787552f7358SJed Brown 
1788552f7358SJed Brown   PetscFunctionBegin;
1789552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1790552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1791552f7358SJed Brown   /* This is only 1-level */
1792552f7358SJed Brown   if (useCone) {
1793552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1794552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1795552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1796552f7358SJed Brown   } else {
1797552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1798552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1799552f7358SJed Brown   }
1800bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1801bfbcdd7aSMatthew G. Knepley     if (*points) {
1802bfbcdd7aSMatthew G. Knepley       closure = *points;
1803bfbcdd7aSMatthew G. Knepley     } else {
1804bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
180569291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1806bfbcdd7aSMatthew G. Knepley     }
1807bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1808bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1809bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1810bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1811bfbcdd7aSMatthew G. Knepley     }
1812bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1813bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1814bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1815bfbcdd7aSMatthew G. Knepley   }
181624c766afSToby Isaac   {
181724c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
181824c766afSToby Isaac 
181924c766afSToby Isaac     c = mesh->maxConeSize;
182024c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
182124c766afSToby Isaac     s = mesh->maxSupportSize;
182224c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
182324c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
182424c766afSToby Isaac   }
182569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1826bfbcdd7aSMatthew G. Knepley   if (*points) {
1827bfbcdd7aSMatthew G. Knepley     closure = *points;
1828bfbcdd7aSMatthew G. Knepley   } else {
182969291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1830bfbcdd7aSMatthew G. Knepley   }
1831bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1832552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1833552f7358SJed Brown     const PetscInt cp = tmp[t];
1834552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1835552f7358SJed Brown 
1836552f7358SJed Brown     closure[closureSize]   = cp;
1837552f7358SJed Brown     closure[closureSize+1] = co;
1838552f7358SJed Brown     fifo[fifoSize]         = cp;
1839552f7358SJed Brown     fifo[fifoSize+1]       = co;
1840552f7358SJed Brown   }
1841bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1842552f7358SJed Brown   while (fifoSize - fifoStart) {
1843552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1844552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1845552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1846552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1847552f7358SJed Brown 
1848552f7358SJed Brown     if (useCone) {
1849552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1850552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1851552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1852552f7358SJed Brown     } else {
1853552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1854552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18550298fd71SBarry Smith       tmpO = NULL;
1856552f7358SJed Brown     }
1857552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1858552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1859552f7358SJed Brown       const PetscInt cp = tmp[i];
18602e1b13c2SMatthew 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. */
18612e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18622e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18632e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1864552f7358SJed Brown       PetscInt       c;
1865552f7358SJed Brown 
18662e1b13c2SMatthew G. Knepley       if (rev) {
18672e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
18682e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18692e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18702e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18712e1b13c2SMatthew G. Knepley       }
1872552f7358SJed Brown       /* Check for duplicate */
1873552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1874552f7358SJed Brown         if (closure[c] == cp) break;
1875552f7358SJed Brown       }
1876552f7358SJed Brown       if (c == closureSize) {
1877552f7358SJed Brown         closure[closureSize]   = cp;
1878552f7358SJed Brown         closure[closureSize+1] = co;
1879552f7358SJed Brown         fifo[fifoSize]         = cp;
1880552f7358SJed Brown         fifo[fifoSize+1]       = co;
1881552f7358SJed Brown         closureSize           += 2;
1882552f7358SJed Brown         fifoSize              += 2;
1883552f7358SJed Brown       }
1884552f7358SJed Brown     }
1885552f7358SJed Brown     fifoStart += 2;
1886552f7358SJed Brown   }
1887552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1888552f7358SJed Brown   if (points)    *points    = closure;
188969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1890552f7358SJed Brown   PetscFunctionReturn(0);
1891552f7358SJed Brown }
1892552f7358SJed Brown 
18939bf0dad6SMatthew G. Knepley /*@C
1894eaf898f9SPatrick 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
18959bf0dad6SMatthew G. Knepley 
18969bf0dad6SMatthew G. Knepley   Not collective
18979bf0dad6SMatthew G. Knepley 
18989bf0dad6SMatthew G. Knepley   Input Parameters:
18999bf0dad6SMatthew G. Knepley + mesh - The DMPlex
1900eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
19019bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
19029bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
19039bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
19049bf0dad6SMatthew G. Knepley 
19059bf0dad6SMatthew G. Knepley   Output Parameters:
19069bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
19079bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
19089bf0dad6SMatthew G. Knepley 
19099bf0dad6SMatthew G. Knepley   Note:
19109bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
19119bf0dad6SMatthew G. Knepley 
19129bf0dad6SMatthew G. Knepley   Fortran Notes:
19139bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19149bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
19159bf0dad6SMatthew G. Knepley 
19169bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19179bf0dad6SMatthew G. Knepley 
19189bf0dad6SMatthew G. Knepley   Level: beginner
19199bf0dad6SMatthew G. Knepley 
19209bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
19219bf0dad6SMatthew G. Knepley @*/
19229bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
19239bf0dad6SMatthew G. Knepley {
19249bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
19259bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
19269bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
19279bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
19289bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
19299bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
19309bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
19319bf0dad6SMatthew G. Knepley 
19329bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
19339bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19349bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
19359bf0dad6SMatthew G. Knepley   /* This is only 1-level */
19369bf0dad6SMatthew G. Knepley   if (useCone) {
19379bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
19389bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
19399bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
19409bf0dad6SMatthew G. Knepley   } else {
19419bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
19429bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
19439bf0dad6SMatthew G. Knepley   }
19449bf0dad6SMatthew G. Knepley   if (depth == 1) {
19459bf0dad6SMatthew G. Knepley     if (*points) {
19469bf0dad6SMatthew G. Knepley       closure = *points;
19479bf0dad6SMatthew G. Knepley     } else {
19489bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
194969291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
19509bf0dad6SMatthew G. Knepley     }
19519bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
19529bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
19539bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19549bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
19559bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
19569bf0dad6SMatthew G. Knepley     }
19579bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
19589bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
19599bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
19609bf0dad6SMatthew G. Knepley   }
196124c766afSToby Isaac   {
196224c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
196324c766afSToby Isaac 
196424c766afSToby Isaac     c = mesh->maxConeSize;
196524c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
196624c766afSToby Isaac     s = mesh->maxSupportSize;
196724c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
196824c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
196924c766afSToby Isaac   }
197069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
19719bf0dad6SMatthew G. Knepley   if (*points) {
19729bf0dad6SMatthew G. Knepley     closure = *points;
19739bf0dad6SMatthew G. Knepley   } else {
197469291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
19759bf0dad6SMatthew G. Knepley   }
19769bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
19779bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
19789bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19799bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
19809bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
19819bf0dad6SMatthew G. Knepley 
19829bf0dad6SMatthew G. Knepley     if (ornt < 0) {
19839bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
19849bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
198586b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
19869bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
19879bf0dad6SMatthew G. Knepley     }
19889bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
19899bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
19909bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
19919bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
19929bf0dad6SMatthew G. Knepley   }
19939bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
19949bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
19959bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
19969bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
19979bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
19989bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
19999bf0dad6SMatthew G. Knepley 
20009bf0dad6SMatthew G. Knepley     if (useCone) {
20019bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
20029bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
20039bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
20049bf0dad6SMatthew G. Knepley     } else {
20059bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
20069bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
20079bf0dad6SMatthew G. Knepley       tmpO = NULL;
20089bf0dad6SMatthew G. Knepley     }
20099bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
20109bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
20119bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
20129bf0dad6SMatthew 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. */
20139bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
20149bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
20159bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
20169bf0dad6SMatthew G. Knepley       PetscInt       c;
20179bf0dad6SMatthew G. Knepley 
20189bf0dad6SMatthew G. Knepley       if (rev) {
20199bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
20209bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
20219bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
20229bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
20239bf0dad6SMatthew G. Knepley       }
20249bf0dad6SMatthew G. Knepley       /* Check for duplicate */
20259bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
20269bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
20279bf0dad6SMatthew G. Knepley       }
20289bf0dad6SMatthew G. Knepley       if (c == closureSize) {
20299bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
20309bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
20319bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
20329bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
20339bf0dad6SMatthew G. Knepley         closureSize           += 2;
20349bf0dad6SMatthew G. Knepley         fifoSize              += 2;
20359bf0dad6SMatthew G. Knepley       }
20369bf0dad6SMatthew G. Knepley     }
20379bf0dad6SMatthew G. Knepley     fifoStart += 2;
20389bf0dad6SMatthew G. Knepley   }
20399bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
20409bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
204169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
20429bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
20439bf0dad6SMatthew G. Knepley }
20449bf0dad6SMatthew G. Knepley 
2045552f7358SJed Brown /*@C
2046eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2047552f7358SJed Brown 
2048552f7358SJed Brown   Not collective
2049552f7358SJed Brown 
2050552f7358SJed Brown   Input Parameters:
2051552f7358SJed Brown + mesh - The DMPlex
2052eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2053552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2054e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2055e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2056552f7358SJed Brown 
2057552f7358SJed Brown   Note:
20580298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2059552f7358SJed Brown 
20603813dfbdSMatthew G Knepley   Fortran Notes:
20613813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20623813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20633813dfbdSMatthew G Knepley 
20643813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
20653813dfbdSMatthew G Knepley 
2066552f7358SJed Brown   Level: beginner
2067552f7358SJed Brown 
2068552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2069552f7358SJed Brown @*/
2070552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2071552f7358SJed Brown {
2072552f7358SJed Brown   PetscErrorCode ierr;
2073552f7358SJed Brown 
2074552f7358SJed Brown   PetscFunctionBegin;
2075552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2076e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2077e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
207869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
20794ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2080552f7358SJed Brown   PetscFunctionReturn(0);
2081552f7358SJed Brown }
2082552f7358SJed Brown 
2083552f7358SJed Brown /*@
2084eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2085552f7358SJed Brown 
2086552f7358SJed Brown   Not collective
2087552f7358SJed Brown 
2088552f7358SJed Brown   Input Parameter:
2089552f7358SJed Brown . mesh - The DMPlex
2090552f7358SJed Brown 
2091552f7358SJed Brown   Output Parameters:
2092552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2093552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2094552f7358SJed Brown 
2095552f7358SJed Brown   Level: beginner
2096552f7358SJed Brown 
2097552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2098552f7358SJed Brown @*/
2099552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2100552f7358SJed Brown {
2101552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2102552f7358SJed Brown 
2103552f7358SJed Brown   PetscFunctionBegin;
2104552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2105552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2106552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2107552f7358SJed Brown   PetscFunctionReturn(0);
2108552f7358SJed Brown }
2109552f7358SJed Brown 
2110552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2111552f7358SJed Brown {
2112552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2113552f7358SJed Brown   PetscInt       size;
2114552f7358SJed Brown   PetscErrorCode ierr;
2115552f7358SJed Brown 
2116552f7358SJed Brown   PetscFunctionBegin;
2117552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2118552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2119552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
21201795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
21211795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2122552f7358SJed Brown   if (mesh->maxSupportSize) {
2123552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2124552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
21251795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2126552f7358SJed Brown   }
2127552f7358SJed Brown   PetscFunctionReturn(0);
2128552f7358SJed Brown }
2129552f7358SJed Brown 
2130276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2131552f7358SJed Brown {
2132552f7358SJed Brown   PetscErrorCode ierr;
2133552f7358SJed Brown 
2134552f7358SJed Brown   PetscFunctionBegin;
21354d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
21364d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2137552f7358SJed Brown   PetscFunctionReturn(0);
2138552f7358SJed Brown }
2139552f7358SJed Brown 
21402adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
21412adcc780SMatthew G. Knepley {
21422adcc780SMatthew G. Knepley   PetscErrorCode ierr;
21432adcc780SMatthew G. Knepley 
21442adcc780SMatthew G. Knepley   PetscFunctionBegin;
21452adcc780SMatthew G. Knepley   if (superdm) {ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);}
21462adcc780SMatthew G. Knepley   ierr = DMCreateSuperDM_Section_Private(dms, len, is, superdm);CHKERRQ(ierr);
21472adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
21482adcc780SMatthew G. Knepley }
21492adcc780SMatthew G. Knepley 
2150552f7358SJed Brown /*@
2151eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2152552f7358SJed Brown 
2153552f7358SJed Brown   Not collective
2154552f7358SJed Brown 
2155552f7358SJed Brown   Input Parameter:
2156552f7358SJed Brown . mesh - The DMPlex
2157552f7358SJed Brown 
2158552f7358SJed Brown   Output Parameter:
2159552f7358SJed Brown 
2160552f7358SJed Brown   Note:
2161552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2162552f7358SJed Brown 
2163552f7358SJed Brown   Level: beginner
2164552f7358SJed Brown 
2165552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2166552f7358SJed Brown @*/
2167552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2168552f7358SJed Brown {
2169552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2170552f7358SJed Brown   PetscInt      *offsets;
2171552f7358SJed Brown   PetscInt       supportSize;
2172552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2173552f7358SJed Brown   PetscErrorCode ierr;
2174552f7358SJed Brown 
2175552f7358SJed Brown   PetscFunctionBegin;
2176552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
217782f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2178552f7358SJed Brown   /* Calculate support sizes */
2179552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2180552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2181552f7358SJed Brown     PetscInt dof, off, c;
2182552f7358SJed Brown 
2183552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2184552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2185552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2186552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2187552f7358SJed Brown     }
2188552f7358SJed Brown   }
2189552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2190552f7358SJed Brown     PetscInt dof;
2191552f7358SJed Brown 
2192552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
21930d644c17SKarl Rupp 
2194552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2195552f7358SJed Brown   }
2196552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2197552f7358SJed Brown   /* Calculate supports */
2198552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
21991795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
22001795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2201552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2202552f7358SJed Brown     PetscInt dof, off, c;
2203552f7358SJed Brown 
2204552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2205552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2206552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2207552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2208552f7358SJed Brown       PetscInt       offS;
2209552f7358SJed Brown 
2210552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
22110d644c17SKarl Rupp 
2212552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2213552f7358SJed Brown       ++offsets[q];
2214552f7358SJed Brown     }
2215552f7358SJed Brown   }
2216552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2217552f7358SJed Brown   PetscFunctionReturn(0);
2218552f7358SJed Brown }
2219552f7358SJed Brown 
2220552f7358SJed Brown /*@
2221eaf898f9SPatrick Sanan   DMPlexStratify - The DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2222eaf898f9SPatrick Sanan   can be illustrated by a Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2223552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2224552f7358SJed Brown   the DAG.
2225552f7358SJed Brown 
2226bf4602e4SToby Isaac   Collective on dm
2227552f7358SJed Brown 
2228552f7358SJed Brown   Input Parameter:
2229552f7358SJed Brown . mesh - The DMPlex
2230552f7358SJed Brown 
2231552f7358SJed Brown   Output Parameter:
2232552f7358SJed Brown 
2233552f7358SJed Brown   Notes:
2234150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2235150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2236c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2237150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2238552f7358SJed Brown 
2239150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2240552f7358SJed Brown 
2241552f7358SJed Brown   Level: beginner
2242552f7358SJed Brown 
2243552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2244552f7358SJed Brown @*/
2245552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2246552f7358SJed Brown {
2247df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2248aa50250dSMatthew G. Knepley   DMLabel        label;
2249552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2250552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2251552f7358SJed Brown   PetscErrorCode ierr;
2252552f7358SJed Brown 
2253552f7358SJed Brown   PetscFunctionBegin;
2254552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2255552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2256552f7358SJed Brown   /* Calculate depth */
2257aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2258c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2259aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2260552f7358SJed Brown   /* Initialize roots and count leaves */
2261552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2262552f7358SJed Brown     PetscInt coneSize, supportSize;
2263552f7358SJed Brown 
2264552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2265552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2266552f7358SJed Brown     if (!coneSize && supportSize) {
2267552f7358SJed Brown       ++numRoots;
2268aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2269552f7358SJed Brown     } else if (!supportSize && coneSize) {
2270552f7358SJed Brown       ++numLeaves;
2271552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2272552f7358SJed Brown       /* Isolated points */
2273aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2274552f7358SJed Brown     }
2275552f7358SJed Brown   }
2276552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2277552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2278552f7358SJed Brown       PetscInt coneSize, supportSize;
2279552f7358SJed Brown 
2280552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2281552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2282552f7358SJed Brown       if (!supportSize && coneSize) {
2283aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2284552f7358SJed Brown       }
2285552f7358SJed Brown     }
2286552f7358SJed Brown   } else {
228774ef644bSMatthew G. Knepley     IS       pointIS;
228874ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2289552f7358SJed Brown 
229074ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
229174ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
229274ef644bSMatthew G. Knepley     while (numPoints) {
229374ef644bSMatthew G. Knepley       const PetscInt *points;
229474ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
229574ef644bSMatthew G. Knepley 
229674ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
229774ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
229874ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
229974ef644bSMatthew G. Knepley         const PetscInt *support;
230074ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
230174ef644bSMatthew G. Knepley 
230274ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
230374ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
230474ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
230574ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2306552f7358SJed Brown         }
2307552f7358SJed Brown       }
23085edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
230974ef644bSMatthew G. Knepley       ++level;
231074ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
231174ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
231274ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
231374ef644bSMatthew G. Knepley       else         {numPoints = 0;}
231474ef644bSMatthew G. Knepley     }
231574ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
231674ef644bSMatthew G. Knepley   }
2317bf4602e4SToby Isaac   { /* just in case there is an empty process */
2318bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2319bf4602e4SToby Isaac 
2320bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
23214de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
23224de306b1SToby Isaac       IS pointIS;
23234de306b1SToby Isaac 
23244de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
23254de306b1SToby Isaac       if (pointIS) {
23264de306b1SToby Isaac         PetscInt  min, max, numPoints;
23274de306b1SToby Isaac         PetscInt  start;
23284de306b1SToby Isaac         PetscBool contig;
23294de306b1SToby Isaac 
23304de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
23314de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
23324de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
23334de306b1SToby Isaac         if (start == 0 && contig) {
23344de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
23354de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
23364de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
23374de306b1SToby Isaac         }
23384de306b1SToby Isaac       }
23394de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
23404de306b1SToby Isaac     }
23416d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2342bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2343bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2344bf4602e4SToby Isaac     }
2345bf4602e4SToby Isaac   }
2346bf4602e4SToby Isaac 
2347df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2348552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2349552f7358SJed Brown   PetscFunctionReturn(0);
2350552f7358SJed Brown }
2351552f7358SJed Brown 
2352552f7358SJed Brown /*@C
2353552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2354552f7358SJed Brown 
2355552f7358SJed Brown   Not Collective
2356552f7358SJed Brown 
2357552f7358SJed Brown   Input Parameters:
2358552f7358SJed Brown + dm - The DMPlex object
2359552f7358SJed Brown . numPoints - The number of input points for the join
2360552f7358SJed Brown - points - The input points
2361552f7358SJed Brown 
2362552f7358SJed Brown   Output Parameters:
2363552f7358SJed Brown + numCoveredPoints - The number of points in the join
2364552f7358SJed Brown - coveredPoints - The points in the join
2365552f7358SJed Brown 
2366552f7358SJed Brown   Level: intermediate
2367552f7358SJed Brown 
2368552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2369552f7358SJed Brown 
23703813dfbdSMatthew G Knepley   Fortran Notes:
23713813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23723813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23733813dfbdSMatthew G Knepley 
23743813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23753813dfbdSMatthew G Knepley 
2376552f7358SJed Brown .keywords: mesh
2377552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2378552f7358SJed Brown @*/
2379552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2380552f7358SJed Brown {
2381552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2382552f7358SJed Brown   PetscInt      *join[2];
2383552f7358SJed Brown   PetscInt       joinSize, i = 0;
2384552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2385552f7358SJed Brown   PetscErrorCode ierr;
2386552f7358SJed Brown 
2387552f7358SJed Brown   PetscFunctionBegin;
2388552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2389552f7358SJed Brown   PetscValidPointer(points, 2);
2390552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2391552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
239269291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
239369291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2394552f7358SJed Brown   /* Copy in support of first point */
2395552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2396552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2397552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2398552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2399552f7358SJed Brown   }
2400552f7358SJed Brown   /* Check each successive support */
2401552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2402552f7358SJed Brown     PetscInt newJoinSize = 0;
2403552f7358SJed Brown 
2404552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2405552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2406552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2407552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2408552f7358SJed Brown 
2409552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2410552f7358SJed Brown         if (point == join[i][m]) {
2411552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2412552f7358SJed Brown           break;
2413552f7358SJed Brown         }
2414552f7358SJed Brown       }
2415552f7358SJed Brown     }
2416552f7358SJed Brown     joinSize = newJoinSize;
2417552f7358SJed Brown     i        = 1-i;
2418552f7358SJed Brown   }
2419552f7358SJed Brown   *numCoveredPoints = joinSize;
2420552f7358SJed Brown   *coveredPoints    = join[i];
242169291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2422552f7358SJed Brown   PetscFunctionReturn(0);
2423552f7358SJed Brown }
2424552f7358SJed Brown 
2425552f7358SJed Brown /*@C
2426552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2427552f7358SJed Brown 
2428552f7358SJed Brown   Not Collective
2429552f7358SJed Brown 
2430552f7358SJed Brown   Input Parameters:
2431552f7358SJed Brown + dm - The DMPlex object
2432552f7358SJed Brown . numPoints - The number of input points for the join
2433552f7358SJed Brown - points - The input points
2434552f7358SJed Brown 
2435552f7358SJed Brown   Output Parameters:
2436552f7358SJed Brown + numCoveredPoints - The number of points in the join
2437552f7358SJed Brown - coveredPoints - The points in the join
2438552f7358SJed Brown 
24393813dfbdSMatthew G Knepley   Fortran Notes:
24403813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24413813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24423813dfbdSMatthew G Knepley 
24433813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24443813dfbdSMatthew G Knepley 
2445552f7358SJed Brown   Level: intermediate
2446552f7358SJed Brown 
2447552f7358SJed Brown .keywords: mesh
2448552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2449552f7358SJed Brown @*/
2450552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2451552f7358SJed Brown {
2452552f7358SJed Brown   PetscErrorCode ierr;
2453552f7358SJed Brown 
2454552f7358SJed Brown   PetscFunctionBegin;
2455552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2456d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2457d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2458d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
245969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2460d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2461552f7358SJed Brown   PetscFunctionReturn(0);
2462552f7358SJed Brown }
2463552f7358SJed Brown 
2464552f7358SJed Brown /*@C
2465552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2466552f7358SJed Brown 
2467552f7358SJed Brown   Not Collective
2468552f7358SJed Brown 
2469552f7358SJed Brown   Input Parameters:
2470552f7358SJed Brown + dm - The DMPlex object
2471552f7358SJed Brown . numPoints - The number of input points for the join
2472552f7358SJed Brown - points - The input points
2473552f7358SJed Brown 
2474552f7358SJed Brown   Output Parameters:
2475552f7358SJed Brown + numCoveredPoints - The number of points in the join
2476552f7358SJed Brown - coveredPoints - The points in the join
2477552f7358SJed Brown 
24783813dfbdSMatthew G Knepley   Fortran Notes:
24793813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24803813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24813813dfbdSMatthew G Knepley 
24823813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24833813dfbdSMatthew G Knepley 
2484552f7358SJed Brown   Level: intermediate
2485552f7358SJed Brown 
2486552f7358SJed Brown .keywords: mesh
2487552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2488552f7358SJed Brown @*/
2489552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2490552f7358SJed Brown {
2491552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2492552f7358SJed Brown   PetscInt      *offsets, **closures;
2493552f7358SJed Brown   PetscInt      *join[2];
2494552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
249524c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2496552f7358SJed Brown   PetscErrorCode ierr;
2497552f7358SJed Brown 
2498552f7358SJed Brown   PetscFunctionBegin;
2499552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2500552f7358SJed Brown   PetscValidPointer(points, 2);
2501552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2502552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2503552f7358SJed Brown 
2504552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
25051795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
250669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
250724c766afSToby Isaac   ms      = mesh->maxSupportSize;
250824c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
250969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
251069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2511552f7358SJed Brown 
2512552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2513552f7358SJed Brown     PetscInt closureSize;
2514552f7358SJed Brown 
2515552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
25160d644c17SKarl Rupp 
2517552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2518552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2519552f7358SJed Brown       PetscInt pStart, pEnd, i;
2520552f7358SJed Brown 
2521552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2522552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2523552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2524552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2525552f7358SJed Brown           break;
2526552f7358SJed Brown         }
2527552f7358SJed Brown       }
2528552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2529552f7358SJed Brown     }
253082f516ccSBarry 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);
2531552f7358SJed Brown   }
2532552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2533552f7358SJed Brown     PetscInt dof;
2534552f7358SJed Brown 
2535552f7358SJed Brown     /* Copy in support of first point */
2536552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2537552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2538552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2539552f7358SJed Brown     }
2540552f7358SJed Brown     /* Check each successive cone */
2541552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2542552f7358SJed Brown       PetscInt newJoinSize = 0;
2543552f7358SJed Brown 
2544552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2545552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2546552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2547552f7358SJed Brown 
2548552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2549552f7358SJed Brown           if (point == join[i][m]) {
2550552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2551552f7358SJed Brown             break;
2552552f7358SJed Brown           }
2553552f7358SJed Brown         }
2554552f7358SJed Brown       }
2555552f7358SJed Brown       joinSize = newJoinSize;
2556552f7358SJed Brown       i        = 1-i;
2557552f7358SJed Brown     }
2558552f7358SJed Brown     if (joinSize) break;
2559552f7358SJed Brown   }
2560552f7358SJed Brown   *numCoveredPoints = joinSize;
2561552f7358SJed Brown   *coveredPoints    = join[i];
2562552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
25630298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2564552f7358SJed Brown   }
2565552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
256669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
256769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2568552f7358SJed Brown   PetscFunctionReturn(0);
2569552f7358SJed Brown }
2570552f7358SJed Brown 
2571552f7358SJed Brown /*@C
2572552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2573552f7358SJed Brown 
2574552f7358SJed Brown   Not Collective
2575552f7358SJed Brown 
2576552f7358SJed Brown   Input Parameters:
2577552f7358SJed Brown + dm - The DMPlex object
2578552f7358SJed Brown . numPoints - The number of input points for the meet
2579552f7358SJed Brown - points - The input points
2580552f7358SJed Brown 
2581552f7358SJed Brown   Output Parameters:
2582552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2583552f7358SJed Brown - coveredPoints - The points in the meet
2584552f7358SJed Brown 
2585552f7358SJed Brown   Level: intermediate
2586552f7358SJed Brown 
2587552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2588552f7358SJed Brown 
25893813dfbdSMatthew G Knepley   Fortran Notes:
25903813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25913813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25923813dfbdSMatthew G Knepley 
25933813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25943813dfbdSMatthew G Knepley 
2595552f7358SJed Brown .keywords: mesh
2596552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2597552f7358SJed Brown @*/
2598552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2599552f7358SJed Brown {
2600552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2601552f7358SJed Brown   PetscInt      *meet[2];
2602552f7358SJed Brown   PetscInt       meetSize, i = 0;
2603552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2604552f7358SJed Brown   PetscErrorCode ierr;
2605552f7358SJed Brown 
2606552f7358SJed Brown   PetscFunctionBegin;
2607552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2608552f7358SJed Brown   PetscValidPointer(points, 2);
2609552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2610552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
261169291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
261269291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2613552f7358SJed Brown   /* Copy in cone of first point */
2614552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2615552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2616552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2617552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2618552f7358SJed Brown   }
2619552f7358SJed Brown   /* Check each successive cone */
2620552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2621552f7358SJed Brown     PetscInt newMeetSize = 0;
2622552f7358SJed Brown 
2623552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2624552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2625552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2626552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2627552f7358SJed Brown 
2628552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2629552f7358SJed Brown         if (point == meet[i][m]) {
2630552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2631552f7358SJed Brown           break;
2632552f7358SJed Brown         }
2633552f7358SJed Brown       }
2634552f7358SJed Brown     }
2635552f7358SJed Brown     meetSize = newMeetSize;
2636552f7358SJed Brown     i        = 1-i;
2637552f7358SJed Brown   }
2638552f7358SJed Brown   *numCoveringPoints = meetSize;
2639552f7358SJed Brown   *coveringPoints    = meet[i];
264069291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2641552f7358SJed Brown   PetscFunctionReturn(0);
2642552f7358SJed Brown }
2643552f7358SJed Brown 
2644552f7358SJed Brown /*@C
2645552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2646552f7358SJed Brown 
2647552f7358SJed Brown   Not Collective
2648552f7358SJed Brown 
2649552f7358SJed Brown   Input Parameters:
2650552f7358SJed Brown + dm - The DMPlex object
2651552f7358SJed Brown . numPoints - The number of input points for the meet
2652552f7358SJed Brown - points - The input points
2653552f7358SJed Brown 
2654552f7358SJed Brown   Output Parameters:
2655552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2656552f7358SJed Brown - coveredPoints - The points in the meet
2657552f7358SJed Brown 
2658552f7358SJed Brown   Level: intermediate
2659552f7358SJed Brown 
26603813dfbdSMatthew G Knepley   Fortran Notes:
26613813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26623813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26633813dfbdSMatthew G Knepley 
26643813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26653813dfbdSMatthew G Knepley 
2666552f7358SJed Brown .keywords: mesh
2667552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2668552f7358SJed Brown @*/
2669552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2670552f7358SJed Brown {
2671552f7358SJed Brown   PetscErrorCode ierr;
2672552f7358SJed Brown 
2673552f7358SJed Brown   PetscFunctionBegin;
2674552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2675d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2676d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2677d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
267869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2679d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2680552f7358SJed Brown   PetscFunctionReturn(0);
2681552f7358SJed Brown }
2682552f7358SJed Brown 
2683552f7358SJed Brown /*@C
2684552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2685552f7358SJed Brown 
2686552f7358SJed Brown   Not Collective
2687552f7358SJed Brown 
2688552f7358SJed Brown   Input Parameters:
2689552f7358SJed Brown + dm - The DMPlex object
2690552f7358SJed Brown . numPoints - The number of input points for the meet
2691552f7358SJed Brown - points - The input points
2692552f7358SJed Brown 
2693552f7358SJed Brown   Output Parameters:
2694552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2695552f7358SJed Brown - coveredPoints - The points in the meet
2696552f7358SJed Brown 
2697552f7358SJed Brown   Level: intermediate
2698552f7358SJed Brown 
26993813dfbdSMatthew G Knepley   Fortran Notes:
27003813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27013813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27023813dfbdSMatthew G Knepley 
27033813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27043813dfbdSMatthew G Knepley 
2705552f7358SJed Brown .keywords: mesh
2706552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2707552f7358SJed Brown @*/
2708552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2709552f7358SJed Brown {
2710552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2711552f7358SJed Brown   PetscInt      *offsets, **closures;
2712552f7358SJed Brown   PetscInt      *meet[2];
2713552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
271424c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2715552f7358SJed Brown   PetscErrorCode ierr;
2716552f7358SJed Brown 
2717552f7358SJed Brown   PetscFunctionBegin;
2718552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2719552f7358SJed Brown   PetscValidPointer(points, 2);
2720552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2721552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2722552f7358SJed Brown 
2723552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2724785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
272569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
272624c766afSToby Isaac   mc      = mesh->maxConeSize;
272724c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
272869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
272969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2730552f7358SJed Brown 
2731552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2732552f7358SJed Brown     PetscInt closureSize;
2733552f7358SJed Brown 
2734552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
27350d644c17SKarl Rupp 
2736552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2737552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2738552f7358SJed Brown       PetscInt pStart, pEnd, i;
2739552f7358SJed Brown 
2740552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2741552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2742552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2743552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2744552f7358SJed Brown           break;
2745552f7358SJed Brown         }
2746552f7358SJed Brown       }
2747552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2748552f7358SJed Brown     }
274982f516ccSBarry 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);
2750552f7358SJed Brown   }
2751552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2752552f7358SJed Brown     PetscInt dof;
2753552f7358SJed Brown 
2754552f7358SJed Brown     /* Copy in cone of first point */
2755552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2756552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2757552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2758552f7358SJed Brown     }
2759552f7358SJed Brown     /* Check each successive cone */
2760552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2761552f7358SJed Brown       PetscInt newMeetSize = 0;
2762552f7358SJed Brown 
2763552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2764552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2765552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2766552f7358SJed Brown 
2767552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2768552f7358SJed Brown           if (point == meet[i][m]) {
2769552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2770552f7358SJed Brown             break;
2771552f7358SJed Brown           }
2772552f7358SJed Brown         }
2773552f7358SJed Brown       }
2774552f7358SJed Brown       meetSize = newMeetSize;
2775552f7358SJed Brown       i        = 1-i;
2776552f7358SJed Brown     }
2777552f7358SJed Brown     if (meetSize) break;
2778552f7358SJed Brown   }
2779552f7358SJed Brown   *numCoveredPoints = meetSize;
2780552f7358SJed Brown   *coveredPoints    = meet[i];
2781552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
27820298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2783552f7358SJed Brown   }
2784552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
278569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
278669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2787552f7358SJed Brown   PetscFunctionReturn(0);
2788552f7358SJed Brown }
2789552f7358SJed Brown 
27904e3744c5SMatthew G. Knepley /*@C
27914e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
27924e3744c5SMatthew G. Knepley 
27934e3744c5SMatthew G. Knepley   Not Collective
27944e3744c5SMatthew G. Knepley 
27954e3744c5SMatthew G. Knepley   Input Parameters:
27964e3744c5SMatthew G. Knepley + dmA - A DMPlex object
27974e3744c5SMatthew G. Knepley - dmB - A DMPlex object
27984e3744c5SMatthew G. Knepley 
27994e3744c5SMatthew G. Knepley   Output Parameters:
28004e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
28014e3744c5SMatthew G. Knepley 
28024e3744c5SMatthew G. Knepley   Level: intermediate
28034e3744c5SMatthew G. Knepley 
28044e3744c5SMatthew G. Knepley   Notes:
28054e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
28064e3744c5SMatthew G. Knepley 
28074e3744c5SMatthew G. Knepley .keywords: mesh
28084e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
28094e3744c5SMatthew G. Knepley @*/
28104e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
28114e3744c5SMatthew G. Knepley {
28124e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
28134e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
28144e3744c5SMatthew G. Knepley 
28154e3744c5SMatthew G. Knepley   PetscFunctionBegin;
28164e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
28174e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
28184e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
28194e3744c5SMatthew G. Knepley 
28204e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
28214e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
28224e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
28234e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
28244e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
28254e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
28264e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
28274e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
28284e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
28294e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
28304e3744c5SMatthew G. Knepley 
28314e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
28324e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
28334e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
28344e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
28354e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
28364e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
28374e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
28384e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
28394e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
28404e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
28414e3744c5SMatthew G. Knepley     }
28424e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
28434e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
28444e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
28454e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
28464e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
28474e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
28484e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
28494e3744c5SMatthew G. Knepley     }
28504e3744c5SMatthew G. Knepley   }
28514e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
28524e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
28534e3744c5SMatthew G. Knepley }
28544e3744c5SMatthew G. Knepley 
28557cd05799SMatthew G. Knepley /*@C
28567cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
28577cd05799SMatthew G. Knepley 
28587cd05799SMatthew G. Knepley   Not Collective
28597cd05799SMatthew G. Knepley 
28607cd05799SMatthew G. Knepley   Input Parameters:
28617cd05799SMatthew G. Knepley + dm         - The DMPlex
28627cd05799SMatthew G. Knepley . cellDim    - The cell dimension
28637cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
28647cd05799SMatthew G. Knepley 
28657cd05799SMatthew G. Knepley   Output Parameters:
28667cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
28677cd05799SMatthew G. Knepley 
28687cd05799SMatthew G. Knepley   Level: developer
28697cd05799SMatthew G. Knepley 
28707cd05799SMatthew G. Knepley   Notes:
28717cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
28727cd05799SMatthew G. Knepley 
28737cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
28747cd05799SMatthew G. Knepley @*/
287518ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2876a6dfd86eSKarl Rupp {
287782f516ccSBarry Smith   MPI_Comm       comm;
2878552f7358SJed Brown   PetscErrorCode ierr;
2879552f7358SJed Brown 
2880552f7358SJed Brown   PetscFunctionBegin;
288182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2882552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2883552f7358SJed Brown   switch (cellDim) {
2884552f7358SJed Brown   case 0:
2885552f7358SJed Brown     *numFaceVertices = 0;
2886552f7358SJed Brown     break;
2887552f7358SJed Brown   case 1:
2888552f7358SJed Brown     *numFaceVertices = 1;
2889552f7358SJed Brown     break;
2890552f7358SJed Brown   case 2:
2891552f7358SJed Brown     switch (numCorners) {
289219436ca2SJed Brown     case 3: /* triangle */
289319436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2894552f7358SJed Brown       break;
289519436ca2SJed Brown     case 4: /* quadrilateral */
289619436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2897552f7358SJed Brown       break;
289819436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
289919436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2900552f7358SJed Brown       break;
290119436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
290219436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2903552f7358SJed Brown       break;
2904552f7358SJed Brown     default:
2905f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2906552f7358SJed Brown     }
2907552f7358SJed Brown     break;
2908552f7358SJed Brown   case 3:
2909552f7358SJed Brown     switch (numCorners) {
291019436ca2SJed Brown     case 4: /* tetradehdron */
291119436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2912552f7358SJed Brown       break;
291319436ca2SJed Brown     case 6: /* tet cohesive cells */
291419436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2915552f7358SJed Brown       break;
291619436ca2SJed Brown     case 8: /* hexahedron */
291719436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2918552f7358SJed Brown       break;
291919436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
292019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2921552f7358SJed Brown       break;
292219436ca2SJed Brown     case 10: /* quadratic tetrahedron */
292319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2924552f7358SJed Brown       break;
292519436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
292619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2927552f7358SJed Brown       break;
292819436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
292919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2930552f7358SJed Brown       break;
293119436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
293219436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2933552f7358SJed Brown       break;
2934552f7358SJed Brown     default:
2935f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2936552f7358SJed Brown     }
2937552f7358SJed Brown     break;
2938552f7358SJed Brown   default:
2939f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2940552f7358SJed Brown   }
2941552f7358SJed Brown   PetscFunctionReturn(0);
2942552f7358SJed Brown }
2943552f7358SJed Brown 
2944552f7358SJed Brown /*@
2945aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2946552f7358SJed Brown 
2947552f7358SJed Brown   Not Collective
2948552f7358SJed Brown 
2949aa50250dSMatthew G. Knepley   Input Parameter:
2950552f7358SJed Brown . dm    - The DMPlex object
2951552f7358SJed Brown 
2952aa50250dSMatthew G. Knepley   Output Parameter:
2953aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2954552f7358SJed Brown 
2955552f7358SJed Brown   Level: developer
2956552f7358SJed Brown 
2957aa50250dSMatthew G. Knepley .keywords: mesh, points
2958aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2959aa50250dSMatthew G. Knepley @*/
2960aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2961aa50250dSMatthew G. Knepley {
2962aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2963aa50250dSMatthew G. Knepley 
2964aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2965aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2966aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2967c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2968c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2969aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2970aa50250dSMatthew G. Knepley }
2971aa50250dSMatthew G. Knepley 
2972aa50250dSMatthew G. Knepley /*@
2973aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2974aa50250dSMatthew G. Knepley 
2975aa50250dSMatthew G. Knepley   Not Collective
2976aa50250dSMatthew G. Knepley 
2977aa50250dSMatthew G. Knepley   Input Parameter:
2978aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2979aa50250dSMatthew G. Knepley 
2980aa50250dSMatthew G. Knepley   Output Parameter:
2981aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2982aa50250dSMatthew G. Knepley 
2983aa50250dSMatthew G. Knepley   Level: developer
2984552f7358SJed Brown 
2985552f7358SJed Brown .keywords: mesh, points
2986aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2987552f7358SJed Brown @*/
2988552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2989552f7358SJed Brown {
2990aa50250dSMatthew G. Knepley   DMLabel        label;
2991aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2992552f7358SJed Brown   PetscErrorCode ierr;
2993552f7358SJed Brown 
2994552f7358SJed Brown   PetscFunctionBegin;
2995552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2996552f7358SJed Brown   PetscValidPointer(depth, 2);
2997aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2998aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2999552f7358SJed Brown   *depth = d-1;
3000552f7358SJed Brown   PetscFunctionReturn(0);
3001552f7358SJed Brown }
3002552f7358SJed Brown 
3003552f7358SJed Brown /*@
3004552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3005552f7358SJed Brown 
3006552f7358SJed Brown   Not Collective
3007552f7358SJed Brown 
3008552f7358SJed Brown   Input Parameters:
3009552f7358SJed Brown + dm           - The DMPlex object
3010552f7358SJed Brown - stratumValue - The requested depth
3011552f7358SJed Brown 
3012552f7358SJed Brown   Output Parameters:
3013552f7358SJed Brown + start - The first point at this depth
3014552f7358SJed Brown - end   - One beyond the last point at this depth
3015552f7358SJed Brown 
3016552f7358SJed Brown   Level: developer
3017552f7358SJed Brown 
3018552f7358SJed Brown .keywords: mesh, points
3019552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3020552f7358SJed Brown @*/
30210adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
30220adebc6cSBarry Smith {
3023aa50250dSMatthew G. Knepley   DMLabel        label;
302463d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3025552f7358SJed Brown   PetscErrorCode ierr;
3026552f7358SJed Brown 
3027552f7358SJed Brown   PetscFunctionBegin;
3028552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302963d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
303063d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3031552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
30320d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
303363d1a920SMatthew G. Knepley   if (stratumValue < 0) {
303463d1a920SMatthew G. Knepley     if (start) *start = pStart;
303563d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
303663d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3037552f7358SJed Brown   }
3038aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
303963d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
304063d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3041552f7358SJed Brown   PetscFunctionReturn(0);
3042552f7358SJed Brown }
3043552f7358SJed Brown 
3044552f7358SJed Brown /*@
3045552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3046552f7358SJed Brown 
3047552f7358SJed Brown   Not Collective
3048552f7358SJed Brown 
3049552f7358SJed Brown   Input Parameters:
3050552f7358SJed Brown + dm           - The DMPlex object
3051552f7358SJed Brown - stratumValue - The requested height
3052552f7358SJed Brown 
3053552f7358SJed Brown   Output Parameters:
3054552f7358SJed Brown + start - The first point at this height
3055552f7358SJed Brown - end   - One beyond the last point at this height
3056552f7358SJed Brown 
3057552f7358SJed Brown   Level: developer
3058552f7358SJed Brown 
3059552f7358SJed Brown .keywords: mesh, points
3060552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3061552f7358SJed Brown @*/
30620adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
30630adebc6cSBarry Smith {
3064aa50250dSMatthew G. Knepley   DMLabel        label;
306563d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3066552f7358SJed Brown   PetscErrorCode ierr;
3067552f7358SJed Brown 
3068552f7358SJed Brown   PetscFunctionBegin;
3069552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
307063d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
307163d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3072552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
30730d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
307463d1a920SMatthew G. Knepley   if (stratumValue < 0) {
307563d1a920SMatthew G. Knepley     if (start) *start = pStart;
307663d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
307763d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3078552f7358SJed Brown   }
3079aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
308013903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
308163d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
308263d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3083552f7358SJed Brown   PetscFunctionReturn(0);
3084552f7358SJed Brown }
3085552f7358SJed Brown 
3086552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
30877cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
3088a6dfd86eSKarl Rupp {
3089ee55978aSMatthew G. Knepley   PetscInt      *pMax;
3090470f9322SMatthew G. Knepley   PetscInt       depth, cellHeight, pStart = 0, pEnd = 0;
3091ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
3092fa716be8SMatthew G. Knepley   PetscBool     *isFE;
3093552f7358SJed Brown   PetscErrorCode ierr;
3094552f7358SJed Brown 
3095552f7358SJed Brown   PetscFunctionBegin;
3096fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
3097ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
3098fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
3099fa716be8SMatthew G. Knepley     PetscObject  obj;
3100fa716be8SMatthew G. Knepley     PetscClassId id;
3101fa716be8SMatthew G. Knepley 
3102ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
3103ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
3104fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
3105fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3106fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
3107fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
3108552f7358SJed Brown   }
310982f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
3110552f7358SJed Brown   if (numFields > 0) {
3111552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
3112552f7358SJed Brown     if (numComp) {
3113552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3114552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
3115d484f18aSToby Isaac         if (isFE[f]) {
3116d484f18aSToby Isaac           PetscFE           fe;
3117d484f18aSToby Isaac           PetscDualSpace    dspace;
3118d484f18aSToby Isaac           const PetscInt    ***perms;
3119d484f18aSToby Isaac           const PetscScalar ***flips;
3120d484f18aSToby Isaac           const PetscInt    *numDof;
3121d484f18aSToby Isaac 
3122d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
3123d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
3124d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
3125d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
3126d484f18aSToby Isaac           if (perms || flips) {
3127d484f18aSToby Isaac             DM               K;
3128d484f18aSToby Isaac             DMLabel          depthLabel;
3129d484f18aSToby Isaac             PetscInt         depth, h;
3130d484f18aSToby Isaac             PetscSectionSym  sym;
3131d484f18aSToby Isaac 
3132d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
3133d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
3134d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
3135d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
3136d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
3137d484f18aSToby Isaac               PetscDualSpace    hspace;
3138d484f18aSToby Isaac               PetscInt          kStart, kEnd;
3139d484f18aSToby Isaac               PetscInt          kConeSize;
3140d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3141d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3142d484f18aSToby Isaac 
3143d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3144d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3145d484f18aSToby Isaac               if (!hspace) continue;
3146d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3147d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3148d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3149d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3150d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3151d484f18aSToby Isaac               ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3152d484f18aSToby Isaac             }
3153d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3154d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3155d484f18aSToby Isaac           }
3156d484f18aSToby Isaac         }
3157552f7358SJed Brown       }
3158552f7358SJed Brown     }
3159552f7358SJed Brown   }
3160552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3161552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3162916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
31639ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
31649ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
3165470f9322SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
3166470f9322SMatthew G. Knepley   for (dep = 0; dep <= depth - cellHeight; ++dep) {
3167916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3168916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3169fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3170552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3171ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3172ee55978aSMatthew G. Knepley 
3173552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3174fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3175552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3176ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3177552f7358SJed Brown       }
3178ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3179552f7358SJed Brown     }
3180552f7358SJed Brown   }
31819ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3182fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3183552f7358SJed Brown   PetscFunctionReturn(0);
3184552f7358SJed Brown }
3185552f7358SJed Brown 
3186552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3187ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3188552f7358SJed Brown */
31897cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3190a6dfd86eSKarl Rupp {
3191552f7358SJed Brown   PetscInt       numFields;
3192552f7358SJed Brown   PetscInt       bc;
3193a68b90caSToby Isaac   PetscSection   aSec;
3194552f7358SJed Brown   PetscErrorCode ierr;
3195552f7358SJed Brown 
3196552f7358SJed Brown   PetscFunctionBegin;
3197552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3198552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3199552f7358SJed Brown     PetscInt        field = 0;
3200ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3201552f7358SJed Brown     const PetscInt *idx;
3202ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3203552f7358SJed Brown 
32040d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3205ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3206ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3207552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3208552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3209552f7358SJed Brown     for (i = 0; i < n; ++i) {
3210552f7358SJed Brown       const PetscInt p = idx[i];
321106ff1081SMatthew G. Knepley       PetscInt       numConst;
3212552f7358SJed Brown 
3213552f7358SJed Brown       if (numFields) {
3214552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3215552f7358SJed Brown       } else {
3216552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3217552f7358SJed Brown       }
321806ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
321906ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
322006ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3221552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3222552f7358SJed Brown     }
3223552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
322406ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3225552f7358SJed Brown   }
3226a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3227a68b90caSToby Isaac   if (aSec) {
3228a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3229a68b90caSToby Isaac 
3230a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3231a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3232ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3233a68b90caSToby Isaac 
3234a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3235a68b90caSToby Isaac       if (dof) {
3236a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3237a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3238a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3239a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3240a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3241a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3242a68b90caSToby Isaac         }
3243a68b90caSToby Isaac       }
3244a68b90caSToby Isaac     }
3245a68b90caSToby Isaac   }
3246552f7358SJed Brown   PetscFunctionReturn(0);
3247552f7358SJed Brown }
3248552f7358SJed Brown 
3249ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3250ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3251ab1d0545SMatthew G. Knepley */
32527cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
32530adebc6cSBarry Smith {
3254ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3255ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3256503335f2SSander Arens   PetscInt       numFields, cdof, maxDof = 0, pStart, pEnd, p, bc, f, d;
3257552f7358SJed Brown   PetscErrorCode ierr;
3258552f7358SJed Brown 
3259552f7358SJed Brown   PetscFunctionBegin;
3260552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3261ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3262ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3263552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3264503335f2SSander Arens   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr); maxDof = PetscMax(maxDof, cdof);}
3265ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3266ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3267ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3268ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3269ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3270ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3271ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3272ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3273552f7358SJed Brown 
3274ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3275ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3276ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3277ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3278ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3279ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3280ab1d0545SMatthew G. Knepley       const PetscInt *find;
3281503335f2SSander Arens       PetscInt        fdof, fcdof, c;
3282ab1d0545SMatthew G. Knepley 
3283503335f2SSander Arens       ierr = PetscSectionGetFieldDof(section, p, field, &fdof);CHKERRQ(ierr);
3284503335f2SSander Arens       if (!fdof) continue;
3285ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3286503335f2SSander Arens         for (d = 0; d < fdof; ++d) indices[d] = d;
3287503335f2SSander Arens         fcdof = fdof;
3288552f7358SJed Brown       } else {
3289503335f2SSander Arens         ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3290ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3291ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3292503335f2SSander Arens         for (c = 0; c < Nc; ++c) indices[d++] = comp[c];
3293503335f2SSander Arens         ierr = PetscSortRemoveDupsInt(&d, indices);CHKERRQ(ierr);
3294503335f2SSander Arens         for (c = d; c < fcdof; ++c) indices[c] = -1;
3295503335f2SSander Arens         fcdof = d;
3296552f7358SJed Brown       }
3297503335f2SSander Arens       ierr = PetscSectionSetFieldConstraintDof(section, p, field, fcdof);CHKERRQ(ierr);
3298ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3299552f7358SJed Brown     }
3300ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3301ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3302552f7358SJed Brown   }
3303ab1d0545SMatthew G. Knepley   /* Handle anchors */
3304ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3305ab1d0545SMatthew G. Knepley   if (aSec) {
3306ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3307552f7358SJed Brown 
3308ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3309ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3310ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3311503335f2SSander Arens       PetscInt dof, f;
3312ab1d0545SMatthew G. Knepley 
3313ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3314ab1d0545SMatthew G. Knepley       if (dof) {
3315ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3316ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3317ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3318ab1d0545SMatthew G. Knepley         }
3319ab1d0545SMatthew G. Knepley       }
3320ab1d0545SMatthew G. Knepley     }
3321ab1d0545SMatthew G. Knepley   }
3322ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3323ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3324ab1d0545SMatthew G. Knepley }
3325ab1d0545SMatthew G. Knepley 
3326ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
33277cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3328ab1d0545SMatthew G. Knepley {
3329ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3330ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3331ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3332ab1d0545SMatthew G. Knepley 
3333ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3334ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3335ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3336ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3337ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3338ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3339552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3340552f7358SJed Brown     PetscInt cdof, d;
3341552f7358SJed Brown 
3342552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3343552f7358SJed Brown     if (cdof) {
3344552f7358SJed Brown       if (numFields) {
3345552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3346552f7358SJed Brown 
3347552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3348ab1d0545SMatthew G. Knepley           const PetscInt *find;
3349ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3350552f7358SJed Brown 
3351552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3352ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3353ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3354ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3355ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3356ab1d0545SMatthew G. Knepley           numConst += fcdof;
3357552f7358SJed Brown           foff     += fdof;
3358552f7358SJed Brown         }
3359503335f2SSander Arens         if (cdof != numConst) {ierr = PetscSectionSetConstraintDof(section, p, numConst);CHKERRQ(ierr);}
3360552f7358SJed Brown       } else {
33610d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3362552f7358SJed Brown       }
3363552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3364552f7358SJed Brown     }
3365552f7358SJed Brown   }
3366552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3367552f7358SJed Brown   PetscFunctionReturn(0);
3368552f7358SJed Brown }
3369552f7358SJed Brown 
3370552f7358SJed Brown /*@C
3371552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3372552f7358SJed Brown 
3373552f7358SJed Brown   Not Collective
3374552f7358SJed Brown 
3375552f7358SJed Brown   Input Parameters:
3376552f7358SJed Brown + dm        - The DMPlex object
3377552f7358SJed Brown . dim       - The spatial dimension of the problem
3378552f7358SJed Brown . numFields - The number of fields in the problem
3379552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3380552f7358SJed Brown . numDof    - An array of size numFields*(dim+1) which holds the number of dof for each field on a mesh piece of dimension d
3381552f7358SJed Brown . numBC     - The number of boundary conditions
3382552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3383ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3384ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3385f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3386552f7358SJed Brown 
3387552f7358SJed Brown   Output Parameter:
3388552f7358SJed Brown . section - The PetscSection object
3389552f7358SJed Brown 
3390eaf898f9SPatrick Sanan   Notes: numDof[f*(dim+1)+d] gives the number of dof for field f on points of dimension d. For instance, numDof[1] is the
3391f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3392f8f126e8SMatthew G. Knepley 
3393f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3394552f7358SJed Brown 
3395552f7358SJed Brown   Level: developer
3396552f7358SJed Brown 
33973813dfbdSMatthew G Knepley   Fortran Notes:
33983813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
33993813dfbdSMatthew G Knepley 
3400552f7358SJed Brown .keywords: mesh, elements
3401f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3402552f7358SJed Brown @*/
3403ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSection(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], IS perm, PetscSection *section)
3404a6dfd86eSKarl Rupp {
3405a68b90caSToby Isaac   PetscSection   aSec;
3406552f7358SJed Brown   PetscErrorCode ierr;
3407552f7358SJed Brown 
3408552f7358SJed Brown   PetscFunctionBegin;
3409552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3410ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3411f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3412552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3413a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3414ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3415ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3416ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3417ab1d0545SMatthew G. Knepley   }
3418ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3419552f7358SJed Brown   PetscFunctionReturn(0);
3420552f7358SJed Brown }
3421552f7358SJed Brown 
34220adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
34230adebc6cSBarry Smith {
3424efe440bfSMatthew G. Knepley   PetscSection   section, s;
3425efe440bfSMatthew G. Knepley   Mat            m;
34263e922f36SToby Isaac   PetscInt       maxHeight;
3427552f7358SJed Brown   PetscErrorCode ierr;
3428552f7358SJed Brown 
3429552f7358SJed Brown   PetscFunctionBegin;
343038221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
34313e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
34323e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
343382f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3434552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
34351d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3436efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3437efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3438efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3439efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3440efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3441552f7358SJed Brown   PetscFunctionReturn(0);
3442552f7358SJed Brown }
3443552f7358SJed Brown 
34447cd05799SMatthew G. Knepley /*@C
34457cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
34467cd05799SMatthew G. Knepley 
34477cd05799SMatthew G. Knepley   Not Collective
34487cd05799SMatthew G. Knepley 
34497cd05799SMatthew G. Knepley   Input Parameters:
34507cd05799SMatthew G. Knepley . dm        - The DMPlex object
34517cd05799SMatthew G. Knepley 
34527cd05799SMatthew G. Knepley   Output Parameter:
34537cd05799SMatthew G. Knepley . section - The PetscSection object
34547cd05799SMatthew G. Knepley 
34557cd05799SMatthew G. Knepley   Level: developer
34567cd05799SMatthew G. Knepley 
34577cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
34587cd05799SMatthew G. Knepley @*/
34590adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
34600adebc6cSBarry Smith {
3461552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3462552f7358SJed Brown 
3463552f7358SJed Brown   PetscFunctionBegin;
3464552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3465552f7358SJed Brown   if (section) *section = mesh->coneSection;
3466552f7358SJed Brown   PetscFunctionReturn(0);
3467552f7358SJed Brown }
3468552f7358SJed Brown 
34697cd05799SMatthew G. Knepley /*@C
34707cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
34717cd05799SMatthew G. Knepley 
34727cd05799SMatthew G. Knepley   Not Collective
34737cd05799SMatthew G. Knepley 
34747cd05799SMatthew G. Knepley   Input Parameters:
34757cd05799SMatthew G. Knepley . dm        - The DMPlex object
34767cd05799SMatthew G. Knepley 
34777cd05799SMatthew G. Knepley   Output Parameter:
34787cd05799SMatthew G. Knepley . section - The PetscSection object
34797cd05799SMatthew G. Knepley 
34807cd05799SMatthew G. Knepley   Level: developer
34817cd05799SMatthew G. Knepley 
34827cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34837cd05799SMatthew G. Knepley @*/
34848cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
34858cb4d582SMatthew G. Knepley {
34868cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
34878cb4d582SMatthew G. Knepley 
34888cb4d582SMatthew G. Knepley   PetscFunctionBegin;
34898cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34908cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
34918cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
34928cb4d582SMatthew G. Knepley }
34938cb4d582SMatthew G. Knepley 
34947cd05799SMatthew G. Knepley /*@C
34957cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
34967cd05799SMatthew G. Knepley 
34977cd05799SMatthew G. Knepley   Not Collective
34987cd05799SMatthew G. Knepley 
34997cd05799SMatthew G. Knepley   Input Parameters:
35007cd05799SMatthew G. Knepley . dm        - The DMPlex object
35017cd05799SMatthew G. Knepley 
35027cd05799SMatthew G. Knepley   Output Parameter:
35037cd05799SMatthew G. Knepley . cones - The cone for each point
35047cd05799SMatthew G. Knepley 
35057cd05799SMatthew G. Knepley   Level: developer
35067cd05799SMatthew G. Knepley 
35077cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35087cd05799SMatthew G. Knepley @*/
3509a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3510a6dfd86eSKarl Rupp {
3511552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3512552f7358SJed Brown 
3513552f7358SJed Brown   PetscFunctionBegin;
3514552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3515552f7358SJed Brown   if (cones) *cones = mesh->cones;
3516552f7358SJed Brown   PetscFunctionReturn(0);
3517552f7358SJed Brown }
3518552f7358SJed Brown 
35197cd05799SMatthew G. Knepley /*@C
35207cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
35217cd05799SMatthew G. Knepley 
35227cd05799SMatthew G. Knepley   Not Collective
35237cd05799SMatthew G. Knepley 
35247cd05799SMatthew G. Knepley   Input Parameters:
35257cd05799SMatthew G. Knepley . dm        - The DMPlex object
35267cd05799SMatthew G. Knepley 
35277cd05799SMatthew G. Knepley   Output Parameter:
35287cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
35297cd05799SMatthew G. Knepley 
35307cd05799SMatthew G. Knepley   Level: developer
35317cd05799SMatthew G. Knepley 
35327cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35337cd05799SMatthew G. Knepley @*/
3534a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3535a6dfd86eSKarl Rupp {
3536552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3537552f7358SJed Brown 
3538552f7358SJed Brown   PetscFunctionBegin;
3539552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3540552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3541552f7358SJed Brown   PetscFunctionReturn(0);
3542552f7358SJed Brown }
3543552f7358SJed Brown 
3544552f7358SJed Brown /******************************** FEM Support **********************************/
3545552f7358SJed Brown 
35467391a63aSMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscInt point, PetscSection section)
35473194fc30SMatthew G. Knepley {
35487391a63aSMatthew G. Knepley   DMLabel        label;
35493194fc30SMatthew G. Knepley   PetscInt      *perm;
35507391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
35513194fc30SMatthew G. Knepley   PetscErrorCode ierr;
35523194fc30SMatthew G. Knepley 
35533194fc30SMatthew G. Knepley   PetscFunctionBegin;
35547391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
35553194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
35567391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
35577391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
35587391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
35597391a63aSMatthew G. Knepley   else if  (depth == dim) {
35607391a63aSMatthew G. Knepley     const PetscInt *cone;
35617391a63aSMatthew G. Knepley 
35627391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
35637391a63aSMatthew G. Knepley     eStart = cone[0];
35647391a63aSMatthew G. Knepley   } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering", point, depth);
35657391a63aSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
35663194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
356789eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
35683194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
35693194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
35703194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35713194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
35723194fc30SMatthew G. Knepley     k = k/Nc + 1;
357389eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
35743194fc30SMatthew G. Knepley   }
35753194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
35763194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
357789eabcffSMatthew G. Knepley     switch (dim) {
357889eabcffSMatthew G. Knepley     case 2:
35793194fc30SMatthew 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} */
35803194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35813194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
35823194fc30SMatthew G. Knepley       k = k/Nc + 1;
35833194fc30SMatthew G. Knepley       /* The SEM order is
35843194fc30SMatthew G. Knepley 
35853194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
358689eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
35873194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
35883194fc30SMatthew G. Knepley       */
35893194fc30SMatthew G. Knepley       {
35903194fc30SMatthew G. Knepley         const PetscInt of   = 0;
35913194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
35923194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
35933194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
35943194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
35953194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
35963194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
35973194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
35983194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
35993194fc30SMatthew G. Knepley         PetscInt       o;
36003194fc30SMatthew G. Knepley 
36013194fc30SMatthew G. Knepley         /* bottom */
36023194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
36033194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
36043194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
36053194fc30SMatthew G. Knepley         /* middle */
36063194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
36073194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
36083194fc30SMatthew 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;
36093194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
36103194fc30SMatthew G. Knepley         }
36113194fc30SMatthew G. Knepley         /* top */
36123194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
36133194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
36143194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
36153194fc30SMatthew G. Knepley         foffset = offset;
36163194fc30SMatthew G. Knepley       }
361789eabcffSMatthew G. Knepley       break;
361889eabcffSMatthew G. Knepley     case 3:
361989eabcffSMatthew G. Knepley       /* The original hex closure is
362089eabcffSMatthew G. Knepley 
362189eabcffSMatthew G. Knepley          {c,
362289eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
362389eabcffSMatthew 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,
362489eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
362589eabcffSMatthew G. Knepley       */
362689eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
362789eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
362889eabcffSMatthew G. Knepley       k = k/Nc + 1;
362989eabcffSMatthew G. Knepley       /* The SEM order is
363089eabcffSMatthew G. Knepley          Bottom Slice
363189eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
363289eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
363389eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
363489eabcffSMatthew G. Knepley 
363589eabcffSMatthew G. Knepley          Middle Slice (j)
363689eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
363789eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
363889eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
363989eabcffSMatthew G. Knepley 
364089eabcffSMatthew G. Knepley          Top Slice
364189eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
364289eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
364389eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
364489eabcffSMatthew G. Knepley       */
364589eabcffSMatthew G. Knepley       {
364689eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
364789eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
364889eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
364989eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
365089eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
365189eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
365289eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
365389eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
365489eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
365589eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
365689eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
365789eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
365889eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
365989eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
366089eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
366189eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
366289eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
366389eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
366489eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
366589eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
366689eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
366789eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
366889eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
366989eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
367089eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
367189eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
367289eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
367389eabcffSMatthew G. Knepley         PetscInt       o, n;
367489eabcffSMatthew G. Knepley 
367589eabcffSMatthew G. Knepley         /* Bottom Slice */
367689eabcffSMatthew G. Knepley         /*   bottom */
367789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
367889eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
367989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
368089eabcffSMatthew G. Knepley         /*   middle */
368189eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
368289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3683316b7f87SMax 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;}
368489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
36853194fc30SMatthew G. Knepley         }
368689eabcffSMatthew G. Knepley         /*   top */
368789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
368889eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
368989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
369089eabcffSMatthew G. Knepley 
369189eabcffSMatthew G. Knepley         /* Middle Slice */
369289eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
369389eabcffSMatthew G. Knepley           /*   bottom */
369489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
369589eabcffSMatthew 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;
369689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
369789eabcffSMatthew G. Knepley           /*   middle */
369889eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
369989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
370089eabcffSMatthew 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;
370189eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
370289eabcffSMatthew G. Knepley           }
370389eabcffSMatthew G. Knepley           /*   top */
370489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
370589eabcffSMatthew 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;
370689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
370789eabcffSMatthew G. Knepley         }
370889eabcffSMatthew G. Knepley 
370989eabcffSMatthew G. Knepley         /* Top Slice */
371089eabcffSMatthew G. Knepley         /*   bottom */
371189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
371289eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
371389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
371489eabcffSMatthew G. Knepley         /*   middle */
371589eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
371689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
371789eabcffSMatthew 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;
371889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
371989eabcffSMatthew G. Knepley         }
372089eabcffSMatthew G. Knepley         /*   top */
372189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
372289eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
372389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
372489eabcffSMatthew G. Knepley 
372589eabcffSMatthew G. Knepley         foffset = offset;
372689eabcffSMatthew G. Knepley       }
372789eabcffSMatthew G. Knepley       break;
372889eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
372989eabcffSMatthew G. Knepley     }
373089eabcffSMatthew G. Knepley   }
373189eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
37323194fc30SMatthew G. Knepley   /* Check permutation */
37333194fc30SMatthew G. Knepley   {
37343194fc30SMatthew G. Knepley     PetscInt *check;
37353194fc30SMatthew G. Knepley 
37363194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
37373194fc30SMatthew 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]);}
37383194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
37393194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
37403194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
37413194fc30SMatthew G. Knepley   }
37421fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
37433194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
37443194fc30SMatthew G. Knepley }
37453194fc30SMatthew G. Knepley 
3746e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3747e071409bSToby Isaac {
3748e071409bSToby Isaac   PetscDS        prob;
3749e071409bSToby Isaac   PetscInt       depth, Nf, h;
3750e071409bSToby Isaac   DMLabel        label;
3751e071409bSToby Isaac   PetscErrorCode ierr;
3752e071409bSToby Isaac 
3753e071409bSToby Isaac   PetscFunctionBeginHot;
3754e071409bSToby Isaac   prob    = dm->prob;
3755e071409bSToby Isaac   Nf      = prob->Nf;
3756e071409bSToby Isaac   label   = dm->depthLabel;
3757e071409bSToby Isaac   *dspace = NULL;
3758e071409bSToby Isaac   if (field < Nf) {
3759e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3760e071409bSToby Isaac 
3761e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3762e071409bSToby Isaac       PetscDualSpace dsp;
3763e071409bSToby Isaac 
3764e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3765e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3766e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3767e071409bSToby Isaac       h    = depth - 1 - h;
3768e071409bSToby Isaac       if (h) {
3769e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3770e071409bSToby Isaac       } else {
3771e071409bSToby Isaac         *dspace = dsp;
3772e071409bSToby Isaac       }
3773e071409bSToby Isaac     }
3774e071409bSToby Isaac   }
3775e071409bSToby Isaac   PetscFunctionReturn(0);
3776e071409bSToby Isaac }
3777e071409bSToby Isaac 
3778e071409bSToby Isaac 
37791a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3780a6dfd86eSKarl Rupp {
3781552f7358SJed Brown   PetscScalar    *array, *vArray;
3782d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
37831a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3784552f7358SJed Brown   PetscErrorCode  ierr;
3785552f7358SJed Brown 
37861b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
37872a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
37885a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
37895a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
37905a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
37913f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
37929df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
37939df71ca4SMatthew G. Knepley       PetscInt dof;
3794d9917b9dSMatthew G. Knepley 
37959df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37969df71ca4SMatthew G. Knepley       size += dof;
37979df71ca4SMatthew G. Knepley     }
37989df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
37999df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
38002a3aaacfSMatthew G. Knepley       PetscInt       dof;
38015a1bb5cfSMatthew G. Knepley 
38025a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
38032a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38045a1bb5cfSMatthew G. Knepley       size += dof;
38055a1bb5cfSMatthew G. Knepley     }
38063f7cbbe7SMatthew G. Knepley     if (!values) {
38073f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
38083f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
38093f7cbbe7SMatthew G. Knepley     }
381069291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
3811982e9ed1SMatthew G. Knepley   } else {
3812982e9ed1SMatthew G. Knepley     array = *values;
3813982e9ed1SMatthew G. Knepley   }
38149df71ca4SMatthew G. Knepley   size = 0;
38155a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
38169df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
38179df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
38189df71ca4SMatthew G. Knepley     PetscScalar *varr;
3819d9917b9dSMatthew G. Knepley 
38209df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38219df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
38229df71ca4SMatthew G. Knepley     varr = &vArray[off];
38231a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
38241a271a75SMatthew G. Knepley       array[offset] = varr[d];
38259df71ca4SMatthew G. Knepley     }
38269df71ca4SMatthew G. Knepley     size += dof;
38279df71ca4SMatthew G. Knepley   }
38289df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
38299df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
38309df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
38315a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
38325a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
38335a1bb5cfSMatthew G. Knepley 
383452ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
38355a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38365a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
38375a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
38385a1bb5cfSMatthew G. Knepley     if (o >= 0) {
38391a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
38401a271a75SMatthew G. Knepley         array[offset] = varr[d];
38415a1bb5cfSMatthew G. Knepley       }
38425a1bb5cfSMatthew G. Knepley     } else {
38431a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
38441a271a75SMatthew G. Knepley         array[offset] = varr[d];
38455a1bb5cfSMatthew G. Knepley       }
38465a1bb5cfSMatthew G. Knepley     }
38479df71ca4SMatthew G. Knepley     size += dof;
38485a1bb5cfSMatthew G. Knepley   }
38495a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
38509df71ca4SMatthew G. Knepley   if (!*values) {
38515a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
38525a1bb5cfSMatthew G. Knepley     *values = array;
38539df71ca4SMatthew G. Knepley   } else {
38548ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
38558c312ff3SMatthew G. Knepley     *csize = size;
38569df71ca4SMatthew G. Knepley   }
38575a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
38585a1bb5cfSMatthew G. Knepley }
3859d9917b9dSMatthew G. Knepley 
3860923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3861923c78e0SToby Isaac {
3862923c78e0SToby Isaac   const PetscInt *cla;
3863923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3864923c78e0SToby Isaac   PetscErrorCode ierr;
3865923c78e0SToby Isaac 
3866923c78e0SToby Isaac   PetscFunctionBeginHot;
3867923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3868923c78e0SToby Isaac   if (!*clPoints) {
3869923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3870923c78e0SToby Isaac 
3871923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3872923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3873923c78e0SToby Isaac     /* Compress out points not in the section */
3874923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3875923c78e0SToby Isaac       PetscInt r = pts[2*p];
3876923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3877923c78e0SToby Isaac         pts[q*2]   = r;
3878923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3879923c78e0SToby Isaac         ++q;
3880923c78e0SToby Isaac       }
3881923c78e0SToby Isaac     }
3882923c78e0SToby Isaac     np = q;
3883923c78e0SToby Isaac     cla = NULL;
3884923c78e0SToby Isaac   } else {
3885923c78e0SToby Isaac     PetscInt dof, off;
3886923c78e0SToby Isaac 
3887923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3888923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3889923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3890923c78e0SToby Isaac     np   = dof/2;
3891923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3892923c78e0SToby Isaac   }
3893923c78e0SToby Isaac   *numPoints = np;
3894923c78e0SToby Isaac   *points    = pts;
3895923c78e0SToby Isaac   *clp       = cla;
3896923c78e0SToby Isaac 
3897923c78e0SToby Isaac   PetscFunctionReturn(0);
3898923c78e0SToby Isaac }
3899923c78e0SToby Isaac 
3900923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3901923c78e0SToby Isaac {
3902923c78e0SToby Isaac   PetscErrorCode ierr;
3903923c78e0SToby Isaac 
3904923c78e0SToby Isaac   PetscFunctionBeginHot;
3905923c78e0SToby Isaac   if (!*clPoints) {
3906923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3907923c78e0SToby Isaac   } else {
3908923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3909923c78e0SToby Isaac   }
3910923c78e0SToby Isaac   *numPoints = 0;
3911923c78e0SToby Isaac   *points    = NULL;
3912923c78e0SToby Isaac   *clSec     = NULL;
3913923c78e0SToby Isaac   *clPoints  = NULL;
3914923c78e0SToby Isaac   *clp       = NULL;
3915923c78e0SToby Isaac   PetscFunctionReturn(0);
3916923c78e0SToby Isaac }
3917923c78e0SToby Isaac 
391897e99dd9SToby 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[])
39191a271a75SMatthew G. Knepley {
39201a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
392197e99dd9SToby Isaac   const PetscInt    **perms = NULL;
392297e99dd9SToby Isaac   const PetscScalar **flips = NULL;
39231a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39241a271a75SMatthew G. Knepley 
39251a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3926fe02ba77SJed Brown   *size = 0;
392797e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
392897e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
392997e99dd9SToby Isaac     const PetscInt    point = points[2*p];
393097e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
393197e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
39321a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
39331a271a75SMatthew G. Knepley     const PetscScalar *varr;
39341a271a75SMatthew G. Knepley 
39351a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39361a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
39371a271a75SMatthew G. Knepley     varr = &vArray[off];
393897e99dd9SToby Isaac     if (clperm) {
393997e99dd9SToby Isaac       if (perm) {
394097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
39411a271a75SMatthew G. Knepley       } else {
394297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
394397e99dd9SToby Isaac       }
394497e99dd9SToby Isaac       if (flip) {
394597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
394697e99dd9SToby Isaac       }
394797e99dd9SToby Isaac     } else {
394897e99dd9SToby Isaac       if (perm) {
394997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
395097e99dd9SToby Isaac       } else {
395197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
395297e99dd9SToby Isaac       }
395397e99dd9SToby Isaac       if (flip) {
395497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
39551a271a75SMatthew G. Knepley       }
39561a271a75SMatthew G. Knepley     }
395797e99dd9SToby Isaac     offset += dof;
395897e99dd9SToby Isaac   }
395997e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
39601a271a75SMatthew G. Knepley   *size = offset;
39611a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
39621a271a75SMatthew G. Knepley }
39631a271a75SMatthew G. Knepley 
396497e99dd9SToby 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[])
39651a271a75SMatthew G. Knepley {
39661a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
39671a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39681a271a75SMatthew G. Knepley 
39691a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3970fe02ba77SJed Brown   *size = 0;
39711a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
397297e99dd9SToby Isaac     PetscInt          p;
397397e99dd9SToby Isaac     const PetscInt    **perms = NULL;
397497e99dd9SToby Isaac     const PetscScalar **flips = NULL;
39751a271a75SMatthew G. Knepley 
397697e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
397797e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
397897e99dd9SToby Isaac       const PetscInt    point = points[2*p];
397997e99dd9SToby Isaac       PetscInt          fdof, foff, b;
39801a271a75SMatthew G. Knepley       const PetscScalar *varr;
398197e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
398297e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
39831a271a75SMatthew G. Knepley 
39841a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
39851a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
39861a271a75SMatthew G. Knepley       varr = &vArray[foff];
398797e99dd9SToby Isaac       if (clperm) {
398897e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
398997e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
399097e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
39911a271a75SMatthew G. Knepley       } else {
399297e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
399397e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
399497e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
39951a271a75SMatthew G. Knepley       }
399697e99dd9SToby Isaac       offset += fdof;
39971a271a75SMatthew G. Knepley     }
399897e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
39991a271a75SMatthew G. Knepley   }
40001a271a75SMatthew G. Knepley   *size = offset;
40011a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
40021a271a75SMatthew G. Knepley }
40031a271a75SMatthew G. Knepley 
4004552f7358SJed Brown /*@C
4005552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4006552f7358SJed Brown 
4007552f7358SJed Brown   Not collective
4008552f7358SJed Brown 
4009552f7358SJed Brown   Input Parameters:
4010552f7358SJed Brown + dm - The DM
4011552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4012552f7358SJed Brown . v - The local vector
401322c1ee49SMatthew G. Knepley . point - The point in the DM
401422c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
401522c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4016552f7358SJed Brown 
4017552f7358SJed Brown   Output Parameters:
401822c1ee49SMatthew G. Knepley + csize - The number of values in the closure
401922c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
402022c1ee49SMatthew G. Knepley 
402122c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
402222c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
402322c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
402422c1ee49SMatthew G. Knepley $
402522c1ee49SMatthew G. Knepley $ A typical use could be
402622c1ee49SMatthew G. Knepley $
402722c1ee49SMatthew G. Knepley $  values = NULL;
402822c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
402922c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
403022c1ee49SMatthew G. Knepley $    <Compute on closure>
403122c1ee49SMatthew G. Knepley $  }
403222c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
403322c1ee49SMatthew G. Knepley $
403422c1ee49SMatthew G. Knepley $ or
403522c1ee49SMatthew G. Knepley $
403622c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
403722c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
403822c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
403922c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
404022c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
404122c1ee49SMatthew G. Knepley $      <Compute on closure>
404222c1ee49SMatthew G. Knepley $    }
404322c1ee49SMatthew G. Knepley $  }
404422c1ee49SMatthew G. Knepley $  PetscFree(values);
4045552f7358SJed Brown 
4046552f7358SJed Brown   Fortran Notes:
4047552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4048552f7358SJed Brown   include petsc.h90 in your code.
4049552f7358SJed Brown 
4050552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4051552f7358SJed Brown 
4052552f7358SJed Brown   Level: intermediate
4053552f7358SJed Brown 
4054552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4055552f7358SJed Brown @*/
4056552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4057552f7358SJed Brown {
4058552f7358SJed Brown   PetscSection       clSection;
4059d9917b9dSMatthew G. Knepley   IS                 clPoints;
40608a84db2dSMatthew G. Knepley   PetscScalar       *array;
40618a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4062552f7358SJed Brown   PetscInt          *points = NULL;
40638f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
40641a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4065552f7358SJed Brown   PetscErrorCode     ierr;
4066552f7358SJed Brown 
4067d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4068552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4069552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
40701a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
40711a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4072552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4073552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4074552f7358SJed Brown   if (depth == 1 && numFields < 2) {
40751a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4076552f7358SJed Brown     PetscFunctionReturn(0);
4077552f7358SJed Brown   }
40781a271a75SMatthew G. Knepley   /* Get points */
4079923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40801fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
40811a271a75SMatthew G. Knepley   /* Get array */
4082bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
40831a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4084552f7358SJed Brown 
40851a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4086552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
40871a271a75SMatthew G. Knepley       asize += dof;
4088552f7358SJed Brown     }
4089bd6c0d32SMatthew G. Knepley     if (!values) {
4090923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40911a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4092bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4093bd6c0d32SMatthew G. Knepley     }
409469291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4095d0f6b257SMatthew G. Knepley   } else {
4096d0f6b257SMatthew G. Knepley     array = *values;
4097d0f6b257SMatthew G. Knepley   }
40988a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
40991a271a75SMatthew G. Knepley   /* Get values */
410097e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
410197e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
41021a271a75SMatthew G. Knepley   /* Cleanup points */
4103923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41041a271a75SMatthew G. Knepley   /* Cleanup array */
41058a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4106d0f6b257SMatthew G. Knepley   if (!*values) {
4107552f7358SJed Brown     if (csize) *csize = size;
4108552f7358SJed Brown     *values = array;
4109d0f6b257SMatthew G. Knepley   } else {
4110f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4111d0f6b257SMatthew G. Knepley     *csize = size;
4112d0f6b257SMatthew G. Knepley   }
4113552f7358SJed Brown   PetscFunctionReturn(0);
4114552f7358SJed Brown }
4115552f7358SJed Brown 
4116552f7358SJed Brown /*@C
4117552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4118552f7358SJed Brown 
4119552f7358SJed Brown   Not collective
4120552f7358SJed Brown 
4121552f7358SJed Brown   Input Parameters:
4122552f7358SJed Brown + dm - The DM
41230298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4124552f7358SJed Brown . v - The local vector
4125eaf898f9SPatrick Sanan . point - The point in the DM
41260298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4127552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4128552f7358SJed Brown 
412922c1ee49SMatthew 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()
413022c1ee49SMatthew G. Knepley 
41313813dfbdSMatthew G Knepley   Fortran Notes:
41323813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
41333813dfbdSMatthew G Knepley   include petsc.h90 in your code.
41343813dfbdSMatthew G Knepley 
41353813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
41363813dfbdSMatthew G Knepley 
4137552f7358SJed Brown   Level: intermediate
4138552f7358SJed Brown 
4139552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4140552f7358SJed Brown @*/
41417c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4142a6dfd86eSKarl Rupp {
4143552f7358SJed Brown   PetscInt       size = 0;
4144552f7358SJed Brown   PetscErrorCode ierr;
4145552f7358SJed Brown 
4146552f7358SJed Brown   PetscFunctionBegin;
4147552f7358SJed Brown   /* Should work without recalculating size */
414869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4149c9fdaa05SMatthew G. Knepley   *values = NULL;
4150552f7358SJed Brown   PetscFunctionReturn(0);
4151552f7358SJed Brown }
4152552f7358SJed Brown 
4153552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4154552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4155552f7358SJed Brown 
415697e99dd9SToby 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[])
4157552f7358SJed Brown {
4158552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4159552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4160552f7358SJed Brown   PetscScalar    *a;
4161552f7358SJed Brown   PetscInt        off, cind = 0, k;
4162552f7358SJed Brown   PetscErrorCode  ierr;
4163552f7358SJed Brown 
4164552f7358SJed Brown   PetscFunctionBegin;
4165552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4166552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4167552f7358SJed Brown   a    = &array[off];
4168552f7358SJed Brown   if (!cdof || setBC) {
416997e99dd9SToby Isaac     if (clperm) {
417097e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
417197e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4172552f7358SJed Brown     } else {
417397e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
417497e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4175552f7358SJed Brown     }
4176552f7358SJed Brown   } else {
4177552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
417897e99dd9SToby Isaac     if (clperm) {
417997e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4180552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
418197e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4182552f7358SJed Brown         }
4183552f7358SJed Brown       } else {
4184552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4185552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
418697e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
418797e99dd9SToby Isaac         }
418897e99dd9SToby Isaac       }
418997e99dd9SToby Isaac     } else {
419097e99dd9SToby Isaac       if (perm) {
419197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
419297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
419397e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
419497e99dd9SToby Isaac         }
419597e99dd9SToby Isaac       } else {
419697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
419797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
419897e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
419997e99dd9SToby Isaac         }
4200552f7358SJed Brown       }
4201552f7358SJed Brown     }
4202552f7358SJed Brown   }
4203552f7358SJed Brown   PetscFunctionReturn(0);
4204552f7358SJed Brown }
4205552f7358SJed Brown 
420697e99dd9SToby 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[])
4207a5e93ea8SMatthew G. Knepley {
4208a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4209a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4210a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4211a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4212a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4213a5e93ea8SMatthew G. Knepley 
4214a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4215a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4216a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4217a5e93ea8SMatthew G. Knepley   a    = &array[off];
4218a5e93ea8SMatthew G. Knepley   if (cdof) {
4219a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
422097e99dd9SToby Isaac     if (clperm) {
422197e99dd9SToby Isaac       if (perm) {
4222a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4223a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
422497e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
422597e99dd9SToby Isaac             cind++;
4226a5e93ea8SMatthew G. Knepley           }
4227a5e93ea8SMatthew G. Knepley         }
4228a5e93ea8SMatthew G. Knepley       } else {
4229a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4230a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
423197e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
423297e99dd9SToby Isaac             cind++;
423397e99dd9SToby Isaac           }
423497e99dd9SToby Isaac         }
423597e99dd9SToby Isaac       }
423697e99dd9SToby Isaac     } else {
423797e99dd9SToby Isaac       if (perm) {
423897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
423997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
424097e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
424197e99dd9SToby Isaac             cind++;
424297e99dd9SToby Isaac           }
424397e99dd9SToby Isaac         }
424497e99dd9SToby Isaac       } else {
424597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
424697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
424797e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
424897e99dd9SToby Isaac             cind++;
424997e99dd9SToby Isaac           }
4250a5e93ea8SMatthew G. Knepley         }
4251a5e93ea8SMatthew G. Knepley       }
4252a5e93ea8SMatthew G. Knepley     }
4253a5e93ea8SMatthew G. Knepley   }
4254a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4255a5e93ea8SMatthew G. Knepley }
4256a5e93ea8SMatthew G. Knepley 
425797e99dd9SToby 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[])
4258a6dfd86eSKarl Rupp {
4259552f7358SJed Brown   PetscScalar    *a;
42601a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
42611a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
426297e99dd9SToby Isaac   PetscInt        cind = 0, b;
4263552f7358SJed Brown   PetscErrorCode  ierr;
4264552f7358SJed Brown 
4265552f7358SJed Brown   PetscFunctionBegin;
4266552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4267552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
42681a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
42691a271a75SMatthew G. Knepley   a    = &array[foff];
4270552f7358SJed Brown   if (!fcdof || setBC) {
427197e99dd9SToby Isaac     if (clperm) {
427297e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
427397e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4274552f7358SJed Brown     } else {
427597e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
427697e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4277552f7358SJed Brown     }
4278552f7358SJed Brown   } else {
4279552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
428097e99dd9SToby Isaac     if (clperm) {
428197e99dd9SToby Isaac       if (perm) {
428297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
428397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
428497e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4285552f7358SJed Brown         }
4286552f7358SJed Brown       } else {
428797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
428897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
428997e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
429097e99dd9SToby Isaac         }
429197e99dd9SToby Isaac       }
429297e99dd9SToby Isaac     } else {
429397e99dd9SToby Isaac       if (perm) {
429497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
429597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
429697e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
429797e99dd9SToby Isaac         }
429897e99dd9SToby Isaac       } else {
429997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
430097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
430197e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4302552f7358SJed Brown         }
4303552f7358SJed Brown       }
4304552f7358SJed Brown     }
4305552f7358SJed Brown   }
43061a271a75SMatthew G. Knepley   *offset += fdof;
4307552f7358SJed Brown   PetscFunctionReturn(0);
4308552f7358SJed Brown }
4309552f7358SJed Brown 
4310ba322698SMatthew 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[])
4311a5e93ea8SMatthew G. Knepley {
4312a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
43131a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
43141a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4315ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4316ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4317a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4318a5e93ea8SMatthew G. Knepley 
4319a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4320a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4321a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
43221a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43231a271a75SMatthew G. Knepley   a    = &array[foff];
4324a5e93ea8SMatthew G. Knepley   if (fcdof) {
4325ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4326a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
432797e99dd9SToby Isaac     if (clperm) {
432897e99dd9SToby Isaac       if (perm) {
4329ba322698SMatthew G. Knepley         if (comps) {
4330ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4331ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4332ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4333ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4334ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4335ba322698SMatthew G. Knepley           }
4336ba322698SMatthew G. Knepley         } else {
433797e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
433897e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
433997e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4340a5e93ea8SMatthew G. Knepley               ++cind;
4341a5e93ea8SMatthew G. Knepley             }
4342a5e93ea8SMatthew G. Knepley           }
4343ba322698SMatthew G. Knepley         }
4344ba322698SMatthew G. Knepley       } else {
4345ba322698SMatthew G. Knepley         if (comps) {
4346ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4347ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4348ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4349ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4350ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4351ba322698SMatthew G. Knepley           }
4352a5e93ea8SMatthew G. Knepley         } else {
435397e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
435497e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
435597e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
435697e99dd9SToby Isaac               ++cind;
435797e99dd9SToby Isaac             }
435897e99dd9SToby Isaac           }
435997e99dd9SToby Isaac         }
4360ba322698SMatthew G. Knepley       }
436197e99dd9SToby Isaac     } else {
436297e99dd9SToby Isaac       if (perm) {
4363ba322698SMatthew G. Knepley         if (comps) {
4364ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4365ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4366ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4367ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4368ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4369ba322698SMatthew G. Knepley           }
4370ba322698SMatthew G. Knepley         } else {
437197e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
437297e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
437397e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
437497e99dd9SToby Isaac               ++cind;
437597e99dd9SToby Isaac             }
437697e99dd9SToby Isaac           }
4377ba322698SMatthew G. Knepley         }
4378ba322698SMatthew G. Knepley       } else {
4379ba322698SMatthew G. Knepley         if (comps) {
4380ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4381ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4382ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4383ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4384ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4385ba322698SMatthew G. Knepley           }
438697e99dd9SToby Isaac         } else {
438797e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
438897e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
438997e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4390a5e93ea8SMatthew G. Knepley               ++cind;
4391a5e93ea8SMatthew G. Knepley             }
4392a5e93ea8SMatthew G. Knepley           }
4393a5e93ea8SMatthew G. Knepley         }
4394a5e93ea8SMatthew G. Knepley       }
4395a5e93ea8SMatthew G. Knepley     }
4396ba322698SMatthew G. Knepley   }
43971a271a75SMatthew G. Knepley   *offset += fdof;
4398a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4399a5e93ea8SMatthew G. Knepley }
4400a5e93ea8SMatthew G. Knepley 
44018f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4402a6dfd86eSKarl Rupp {
4403552f7358SJed Brown   PetscScalar    *array;
44041b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
44051b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4406552f7358SJed Brown   PetscErrorCode  ierr;
4407552f7358SJed Brown 
44081b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4409b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4410b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4411b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4412b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4413b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4414b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4415b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4416b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4417b6ebb6e6SMatthew G. Knepley 
4418b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4419b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4420b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4421b6ebb6e6SMatthew G. Knepley     {
4422b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4423b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4424b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4425b6ebb6e6SMatthew G. Knepley 
4426b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4427b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4428b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4429b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4430b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4431b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4432b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4433b6ebb6e6SMatthew G. Knepley           }
4434b6ebb6e6SMatthew G. Knepley         } else {
4435b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4436b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4437b6ebb6e6SMatthew G. Knepley           }
4438b6ebb6e6SMatthew G. Knepley         }
4439b6ebb6e6SMatthew G. Knepley       } else {
4440b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4441b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4442b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4443b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4444b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4445b6ebb6e6SMatthew G. Knepley           }
4446b6ebb6e6SMatthew G. Knepley         } else {
4447b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4448b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4449b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4450b6ebb6e6SMatthew G. Knepley           }
4451b6ebb6e6SMatthew G. Knepley         }
4452b6ebb6e6SMatthew G. Knepley       }
4453b6ebb6e6SMatthew G. Knepley     }
4454b6ebb6e6SMatthew G. Knepley   }
4455b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4456b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4457b6ebb6e6SMatthew G. Knepley }
44581b406b76SMatthew G. Knepley 
44591b406b76SMatthew G. Knepley /*@C
44601b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
44611b406b76SMatthew G. Knepley 
44621b406b76SMatthew G. Knepley   Not collective
44631b406b76SMatthew G. Knepley 
44641b406b76SMatthew G. Knepley   Input Parameters:
44651b406b76SMatthew G. Knepley + dm - The DM
44661b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
44671b406b76SMatthew G. Knepley . v - The local vector
4468eaf898f9SPatrick Sanan . point - The point in the DM
44691b406b76SMatthew G. Knepley . values - The array of values
447022c1ee49SMatthew 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,
447122c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
44721b406b76SMatthew G. Knepley 
44731b406b76SMatthew G. Knepley   Fortran Notes:
44741b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
44751b406b76SMatthew G. Knepley 
44761b406b76SMatthew G. Knepley   Level: intermediate
44771b406b76SMatthew G. Knepley 
44781b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
44791b406b76SMatthew G. Knepley @*/
44801b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
44811b406b76SMatthew G. Knepley {
44821b406b76SMatthew G. Knepley   PetscSection    clSection;
44831b406b76SMatthew G. Knepley   IS              clPoints;
44841b406b76SMatthew G. Knepley   PetscScalar    *array;
44851b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
448697e99dd9SToby Isaac   const PetscInt *clp, *clperm;
44871a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
44881b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
44891b406b76SMatthew G. Knepley 
44901a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
44911b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44921b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
44931a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44941a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
44951b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
44961b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
44971b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
44988f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
44991b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
45001b406b76SMatthew G. Knepley   }
45011a271a75SMatthew G. Knepley   /* Get points */
450297e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4503923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
45041a271a75SMatthew G. Knepley   /* Get array */
4505552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
45061a271a75SMatthew G. Knepley   /* Get values */
4507ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
450897e99dd9SToby Isaac     PetscInt offset = 0, f;
4509552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
451097e99dd9SToby Isaac       const PetscInt    **perms = NULL;
451197e99dd9SToby Isaac       const PetscScalar **flips = NULL;
451297e99dd9SToby Isaac 
451397e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4514552f7358SJed Brown       switch (mode) {
4515552f7358SJed Brown       case INSERT_VALUES:
451697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
451797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
451897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
451997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
452097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4521552f7358SJed Brown         } break;
4522552f7358SJed Brown       case INSERT_ALL_VALUES:
452397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
452497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
452597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
452697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
452797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4528552f7358SJed Brown         } break;
4529a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
453097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
453197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
453297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
453397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4534ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4535a5e93ea8SMatthew G. Knepley         } break;
4536552f7358SJed Brown       case ADD_VALUES:
453797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
453897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
453997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
454097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
454197e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4542552f7358SJed Brown         } break;
4543552f7358SJed Brown       case ADD_ALL_VALUES:
454497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
454597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
454697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
454797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
454897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4549552f7358SJed Brown         } break;
4550304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
455197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
455297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
455397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
455497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4555ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4556304ab55fSMatthew G. Knepley         } break;
4557552f7358SJed Brown       default:
4558f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4559552f7358SJed Brown       }
456097e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
45611a271a75SMatthew G. Knepley     }
4562552f7358SJed Brown   } else {
45631a271a75SMatthew G. Knepley     PetscInt dof, off;
456497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
456597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
45661a271a75SMatthew G. Knepley 
456797e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4568552f7358SJed Brown     switch (mode) {
4569552f7358SJed Brown     case INSERT_VALUES:
457097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
457197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
457297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
457397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
457497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
457597e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4576552f7358SJed Brown       } break;
4577552f7358SJed Brown     case INSERT_ALL_VALUES:
457897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
457997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
458097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
458197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
458297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
458397e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4584552f7358SJed Brown       } break;
4585a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
458697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
458797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
458897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
458997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
459097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
459197e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4592a5e93ea8SMatthew G. Knepley       } break;
4593552f7358SJed Brown     case ADD_VALUES:
459497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
459597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
459697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
459797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
459897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
459997e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4600552f7358SJed Brown       } break;
4601552f7358SJed Brown     case ADD_ALL_VALUES:
460297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
460397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
460497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
460597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
460697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
460797e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4608552f7358SJed Brown       } break;
4609304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
461097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
461197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
461297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
461397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
461497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
461597e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4616304ab55fSMatthew G. Knepley       } break;
4617552f7358SJed Brown     default:
4618f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4619552f7358SJed Brown     }
462097e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4621552f7358SJed Brown   }
46221a271a75SMatthew G. Knepley   /* Cleanup points */
4623923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
46241a271a75SMatthew G. Knepley   /* Cleanup array */
4625552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4626552f7358SJed Brown   PetscFunctionReturn(0);
4627552f7358SJed Brown }
4628552f7358SJed Brown 
4629ba322698SMatthew 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)
4630e07394fbSMatthew G. Knepley {
4631e07394fbSMatthew G. Knepley   PetscSection      clSection;
4632e07394fbSMatthew G. Knepley   IS                clPoints;
4633e07394fbSMatthew G. Knepley   PetscScalar       *array;
4634e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4635b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4636e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
463797e99dd9SToby Isaac   PetscInt          offset = 0, f;
4638e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4639e07394fbSMatthew G. Knepley 
4640e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4641e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4642e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4643e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4644e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4645e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4646e07394fbSMatthew G. Knepley   /* Get points */
4647b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4648923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4649e07394fbSMatthew G. Knepley   /* Get array */
4650e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4651e07394fbSMatthew G. Knepley   /* Get values */
4652e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
465397e99dd9SToby Isaac     const PetscInt    **perms = NULL;
465497e99dd9SToby Isaac     const PetscScalar **flips = NULL;
465597e99dd9SToby Isaac 
4656e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4657e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4658e07394fbSMatthew G. Knepley         PetscInt fdof;
4659e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4660e07394fbSMatthew G. Knepley         offset += fdof;
4661e07394fbSMatthew G. Knepley       }
4662e07394fbSMatthew G. Knepley       continue;
4663e07394fbSMatthew G. Knepley     }
466497e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4665e07394fbSMatthew G. Knepley     switch (mode) {
4666e07394fbSMatthew G. Knepley     case INSERT_VALUES:
466797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
466897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
466997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
467097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4671b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4672e07394fbSMatthew G. Knepley       } break;
4673e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
467497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
467597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
467697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
467797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4678b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4679e07394fbSMatthew G. Knepley         } break;
4680e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
468197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
468297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
468397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
468497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4685ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
4686e07394fbSMatthew G. Knepley       } break;
4687e07394fbSMatthew G. Knepley     case ADD_VALUES:
468897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
468997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
469097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
469197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4692b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4693e07394fbSMatthew G. Knepley       } break;
4694e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
469597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
469697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
469797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
469897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4699b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4700e07394fbSMatthew G. Knepley       } break;
4701e07394fbSMatthew G. Knepley     default:
4702f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4703e07394fbSMatthew G. Knepley     }
470497e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4705e07394fbSMatthew G. Knepley   }
4706e07394fbSMatthew G. Knepley   /* Cleanup points */
4707923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4708e07394fbSMatthew G. Knepley   /* Cleanup array */
4709e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4710e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4711e07394fbSMatthew G. Knepley }
4712e07394fbSMatthew G. Knepley 
47137cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4714552f7358SJed Brown {
4715552f7358SJed Brown   PetscMPIInt    rank;
4716552f7358SJed Brown   PetscInt       i, j;
4717552f7358SJed Brown   PetscErrorCode ierr;
4718552f7358SJed Brown 
4719552f7358SJed Brown   PetscFunctionBegin;
472082f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4721eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
4722e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4723e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4724b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4725b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4726e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4727b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4728519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
47297eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4730552f7358SJed Brown #else
4731b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4732552f7358SJed Brown #endif
4733552f7358SJed Brown     }
473477a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4735552f7358SJed Brown   }
4736552f7358SJed Brown   PetscFunctionReturn(0);
4737552f7358SJed Brown }
4738552f7358SJed Brown 
4739552f7358SJed Brown /* . off - The global offset of this point */
47404acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4741a6dfd86eSKarl Rupp {
4742e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4743552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4744552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4745552f7358SJed Brown   PetscInt        cind = 0, k;
4746552f7358SJed Brown   PetscErrorCode  ierr;
4747552f7358SJed Brown 
4748552f7358SJed Brown   PetscFunctionBegin;
4749552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4750552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4751552f7358SJed Brown   if (!cdof || setBC) {
47524acb8e1eSToby Isaac     if (perm) {
47534acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4754552f7358SJed Brown     } else {
47554acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4756552f7358SJed Brown     }
4757552f7358SJed Brown   } else {
4758552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
47594acb8e1eSToby Isaac     if (perm) {
47604acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
47614acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
47624acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
47634acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
47644acb8e1eSToby Isaac           ++cind;
47654acb8e1eSToby Isaac         } else {
47664acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
47674acb8e1eSToby Isaac         }
47684acb8e1eSToby Isaac       }
47694acb8e1eSToby Isaac     } else {
4770552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4771552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4772552f7358SJed Brown           /* Insert check for returning constrained indices */
4773e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4774552f7358SJed Brown           ++cind;
4775552f7358SJed Brown         } else {
4776e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4777552f7358SJed Brown         }
4778552f7358SJed Brown       }
4779552f7358SJed Brown     }
4780552f7358SJed Brown   }
4781e6ccafaeSMatthew G Knepley   *loff += dof;
4782552f7358SJed Brown   PetscFunctionReturn(0);
4783552f7358SJed Brown }
4784552f7358SJed Brown 
4785552f7358SJed Brown /* . off - The global offset of this point */
47864acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4787a6dfd86eSKarl Rupp {
4788552f7358SJed Brown   PetscInt       numFields, foff, f;
4789552f7358SJed Brown   PetscErrorCode ierr;
4790552f7358SJed Brown 
4791552f7358SJed Brown   PetscFunctionBegin;
4792552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4793552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
47944acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4795552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
47964acb8e1eSToby Isaac     PetscInt        cind = 0, b;
47974acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4798552f7358SJed Brown 
4799552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4800552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4801552f7358SJed Brown     if (!cfdof || setBC) {
48024acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
48034acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4804552f7358SJed Brown     } else {
4805552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
48064acb8e1eSToby Isaac       if (perm) {
48074acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
48084acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
48094acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4810552f7358SJed Brown             ++cind;
4811552f7358SJed Brown           } else {
48124acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4813552f7358SJed Brown           }
4814552f7358SJed Brown         }
4815552f7358SJed Brown       } else {
48164acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
48174acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
48184acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4819552f7358SJed Brown             ++cind;
4820552f7358SJed Brown           } else {
48214acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4822552f7358SJed Brown           }
4823552f7358SJed Brown         }
4824552f7358SJed Brown       }
4825552f7358SJed Brown     }
4826a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4827552f7358SJed Brown     foffs[f] += fdof;
4828552f7358SJed Brown   }
4829552f7358SJed Brown   PetscFunctionReturn(0);
4830552f7358SJed Brown }
4831552f7358SJed Brown 
48324acb8e1eSToby 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)
4833d3d1a6afSToby Isaac {
4834d3d1a6afSToby Isaac   Mat             cMat;
4835d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4836d3d1a6afSToby Isaac   IS              aIS;
4837d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4838d3d1a6afSToby Isaac   const PetscInt  *anchors;
4839e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4840d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4841d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4842d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4843d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4844d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4845d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4846d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
48476ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4848d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4849d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4850d3d1a6afSToby Isaac 
4851d3d1a6afSToby Isaac   PetscFunctionBegin;
4852d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4853d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4854d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4855d3d1a6afSToby Isaac 
4856a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4857d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4858d3d1a6afSToby Isaac   if (aSec) {
4859d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4860d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4861d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4862d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4863d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4864d3d1a6afSToby Isaac      * into the global matrix anyway) */
4865d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4866d3d1a6afSToby Isaac       PetscInt b    = points[p];
48674b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4868d3d1a6afSToby Isaac 
48694b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
48704b2f2278SToby Isaac       if (!bSecDof) {
48714b2f2278SToby Isaac         continue;
48724b2f2278SToby Isaac       }
4873d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4874d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4875d3d1a6afSToby Isaac       }
4876d3d1a6afSToby Isaac       if (bDof) {
4877d3d1a6afSToby Isaac         /* this point is constrained */
4878d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4879d3d1a6afSToby Isaac         PetscInt bOff, q;
4880d3d1a6afSToby Isaac 
4881d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4882d3d1a6afSToby Isaac         newNumPoints  += bDof;
4883d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4884d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4885d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4886d3d1a6afSToby Isaac           PetscInt aDof;
4887d3d1a6afSToby Isaac 
4888d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4889d3d1a6afSToby Isaac           newNumIndices += aDof;
4890d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4891d3d1a6afSToby Isaac             PetscInt fDof;
4892d3d1a6afSToby Isaac 
4893d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4894d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4895d3d1a6afSToby Isaac           }
4896d3d1a6afSToby Isaac         }
4897d3d1a6afSToby Isaac       }
4898d3d1a6afSToby Isaac       else {
4899d3d1a6afSToby Isaac         /* this point is not constrained */
4900d3d1a6afSToby Isaac         newNumPoints++;
49014b2f2278SToby Isaac         newNumIndices += bSecDof;
4902d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4903d3d1a6afSToby Isaac           PetscInt fDof;
4904d3d1a6afSToby Isaac 
4905d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4906d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4907d3d1a6afSToby Isaac         }
4908d3d1a6afSToby Isaac       }
4909d3d1a6afSToby Isaac     }
4910d3d1a6afSToby Isaac   }
4911d3d1a6afSToby Isaac   if (!anyConstrained) {
491272b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
491372b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
491472b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
491572b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
491672b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4917d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4918d3d1a6afSToby Isaac   }
4919d3d1a6afSToby Isaac 
49206ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
49216ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
49226ecaa68aSToby Isaac 
4923f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4924d3d1a6afSToby Isaac 
49256ecaa68aSToby Isaac   if (!outPoints && !outValues) {
49266ecaa68aSToby Isaac     if (offsets) {
49276ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
49286ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
49296ecaa68aSToby Isaac       }
49306ecaa68aSToby Isaac     }
49316ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
49326ecaa68aSToby Isaac     PetscFunctionReturn(0);
49336ecaa68aSToby Isaac   }
49346ecaa68aSToby Isaac 
4935f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4936d3d1a6afSToby Isaac 
4937f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4938d3d1a6afSToby Isaac 
4939d3d1a6afSToby Isaac   /* workspaces */
4940d3d1a6afSToby Isaac   if (numFields) {
4941d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
494269291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
494369291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4944d3d1a6afSToby Isaac     }
4945d3d1a6afSToby Isaac   }
4946d3d1a6afSToby Isaac   else {
494769291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
494869291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4949d3d1a6afSToby Isaac   }
4950d3d1a6afSToby Isaac 
4951d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4952d3d1a6afSToby Isaac   if (numFields) {
49534b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
49544b2f2278SToby Isaac 
4955d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4956d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
49574b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4958d3d1a6afSToby Isaac 
49594b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
49604b2f2278SToby Isaac       if (!bSecDof) {
49614b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
49624b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
49634b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
49644b2f2278SToby Isaac         }
49654b2f2278SToby Isaac         continue;
49664b2f2278SToby Isaac       }
4967d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4968d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4969d3d1a6afSToby Isaac       }
4970d3d1a6afSToby Isaac       if (bDof) {
4971d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4972d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4973d3d1a6afSToby Isaac 
4974d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4975d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4976d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4977d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4978d3d1a6afSToby Isaac             PetscInt aFDof;
4979d3d1a6afSToby Isaac 
4980d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4981d3d1a6afSToby Isaac             allFDof += aFDof;
4982d3d1a6afSToby Isaac           }
4983d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4984d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4985d3d1a6afSToby Isaac         }
4986d3d1a6afSToby Isaac       }
4987d3d1a6afSToby Isaac       else {
4988d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4989d3d1a6afSToby Isaac           PetscInt fDof;
4990d3d1a6afSToby Isaac 
4991d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4992d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4993d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4994d3d1a6afSToby Isaac         }
4995d3d1a6afSToby Isaac       }
4996d3d1a6afSToby Isaac     }
49974b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
49984b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
49994b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5000d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5001d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5002d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5003d3d1a6afSToby Isaac       }
500419f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
500519f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
500669291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5007d3d1a6afSToby Isaac     }
5008d3d1a6afSToby Isaac   }
5009d3d1a6afSToby Isaac   else {
5010d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5011d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
50124b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5013d3d1a6afSToby Isaac 
50144b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
50154b2f2278SToby Isaac       if (!bSecDof) {
50164b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
50174b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
50184b2f2278SToby Isaac         continue;
50194b2f2278SToby Isaac       }
5020d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5021d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5022d3d1a6afSToby Isaac       }
5023d3d1a6afSToby Isaac       if (bDof) {
50244b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5025d3d1a6afSToby Isaac 
5026d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5027d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5028d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5029d3d1a6afSToby Isaac 
5030d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5031d3d1a6afSToby Isaac           allDof += aDof;
5032d3d1a6afSToby Isaac         }
5033d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
50344b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5035d3d1a6afSToby Isaac       }
5036d3d1a6afSToby Isaac       else {
50374b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5038d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5039d3d1a6afSToby Isaac       }
5040d3d1a6afSToby Isaac     }
5041d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5042d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5043d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5044d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5045d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5046d3d1a6afSToby Isaac     }
504769291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5048d3d1a6afSToby Isaac   }
5049d3d1a6afSToby Isaac 
50506ecaa68aSToby Isaac   /* output arrays */
505169291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
50526ecaa68aSToby Isaac 
5053d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5054d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5055d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
505669291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
505769291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5058d3d1a6afSToby Isaac   if (numFields) {
5059d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5060d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5061d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
50624b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5063d3d1a6afSToby Isaac 
50644b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
50654b2f2278SToby Isaac       if (!bSecDof) {
50664b2f2278SToby Isaac         continue;
50674b2f2278SToby Isaac       }
5068d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5069d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5070d3d1a6afSToby Isaac       }
5071d3d1a6afSToby Isaac       if (bDof) {
5072d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5073d3d1a6afSToby Isaac 
5074d3d1a6afSToby Isaac         fStart[0] = 0;
5075d3d1a6afSToby Isaac         fEnd[0]   = 0;
5076d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5077d3d1a6afSToby Isaac           PetscInt fDof;
5078d3d1a6afSToby Isaac 
5079d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5080d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5081d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5082d3d1a6afSToby Isaac         }
5083d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
50844acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
5085d3d1a6afSToby Isaac 
5086d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5087d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5088d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5089d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5090d3d1a6afSToby Isaac 
5091d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5092d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5093d3d1a6afSToby Isaac         }
5094d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5095d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5096d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5097d3d1a6afSToby Isaac 
5098d3d1a6afSToby 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 */
5099d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5100d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5101302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
51024acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
5103d3d1a6afSToby Isaac         }
5104d3d1a6afSToby Isaac         newP += bDof;
5105d3d1a6afSToby Isaac 
51066ecaa68aSToby Isaac         if (outValues) {
5107d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5108d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5109d3d1a6afSToby 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);
5110d3d1a6afSToby Isaac           }
5111d3d1a6afSToby Isaac         }
51126ecaa68aSToby Isaac       }
5113d3d1a6afSToby Isaac       else {
5114d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5115d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5116d3d1a6afSToby Isaac         newP++;
5117d3d1a6afSToby Isaac       }
5118d3d1a6afSToby Isaac     }
5119d3d1a6afSToby Isaac   } else {
5120d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5121d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5122d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
51234b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5124d3d1a6afSToby Isaac 
51254b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
51264b2f2278SToby Isaac       if (!bSecDof) {
51274b2f2278SToby Isaac         continue;
51284b2f2278SToby Isaac       }
5129d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5130d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5131d3d1a6afSToby Isaac       }
5132d3d1a6afSToby Isaac       if (bDof) {
5133d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5134d3d1a6afSToby Isaac 
5135d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
51364acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
5137d3d1a6afSToby Isaac 
5138d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5139d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5140d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5141d3d1a6afSToby Isaac 
5142d3d1a6afSToby 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 */
5143d3d1a6afSToby Isaac 
5144d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5145d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5146302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
51474acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
5148d3d1a6afSToby Isaac         }
5149d3d1a6afSToby Isaac         newP += bDof;
5150d3d1a6afSToby Isaac 
5151d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
51526ecaa68aSToby Isaac         if (outValues) {
5153d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5154d3d1a6afSToby Isaac         }
51556ecaa68aSToby Isaac       }
5156d3d1a6afSToby Isaac       else {
5157d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5158d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5159d3d1a6afSToby Isaac         newP++;
5160d3d1a6afSToby Isaac       }
5161d3d1a6afSToby Isaac     }
5162d3d1a6afSToby Isaac   }
5163d3d1a6afSToby Isaac 
51646ecaa68aSToby Isaac   if (outValues) {
516569291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5166d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5167d3d1a6afSToby Isaac     /* multiply constraints on the right */
5168d3d1a6afSToby Isaac     if (numFields) {
5169d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5170d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5171d3d1a6afSToby Isaac 
5172d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5173d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5174d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5175d3d1a6afSToby Isaac           PetscInt c, r, k;
5176d3d1a6afSToby Isaac           PetscInt dof;
5177d3d1a6afSToby Isaac 
5178d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
51794b2f2278SToby Isaac           if (!dof) {
51804b2f2278SToby Isaac             continue;
51814b2f2278SToby Isaac           }
5182d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5183d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5184d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5185d3d1a6afSToby Isaac 
5186d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5187d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5188d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
51894acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5190d3d1a6afSToby Isaac                 }
5191d3d1a6afSToby Isaac               }
5192d3d1a6afSToby Isaac             }
5193d3d1a6afSToby Isaac           }
5194d3d1a6afSToby Isaac           else {
5195d3d1a6afSToby Isaac             /* copy this column as is */
5196d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5197d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5198d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5199d3d1a6afSToby Isaac               }
5200d3d1a6afSToby Isaac             }
5201d3d1a6afSToby Isaac           }
5202d3d1a6afSToby Isaac           oldOff += dof;
5203d3d1a6afSToby Isaac         }
5204d3d1a6afSToby Isaac       }
5205d3d1a6afSToby Isaac     }
5206d3d1a6afSToby Isaac     else {
5207d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5208d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5209d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5210d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5211d3d1a6afSToby Isaac         PetscInt c, r, k;
5212d3d1a6afSToby Isaac         PetscInt dof;
5213d3d1a6afSToby Isaac 
5214d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
52154b2f2278SToby Isaac         if (!dof) {
52164b2f2278SToby Isaac           continue;
52174b2f2278SToby Isaac         }
5218d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5219d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5220d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5221d3d1a6afSToby Isaac 
5222d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5223d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5224d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5225d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5226d3d1a6afSToby Isaac               }
5227d3d1a6afSToby Isaac             }
5228d3d1a6afSToby Isaac           }
5229d3d1a6afSToby Isaac         }
5230d3d1a6afSToby Isaac         else {
5231d3d1a6afSToby Isaac           /* copy this column as is */
5232d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5233d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5234d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5235d3d1a6afSToby Isaac             }
5236d3d1a6afSToby Isaac           }
5237d3d1a6afSToby Isaac         }
5238d3d1a6afSToby Isaac         oldOff += dof;
5239d3d1a6afSToby Isaac       }
5240d3d1a6afSToby Isaac     }
5241d3d1a6afSToby Isaac 
52426ecaa68aSToby Isaac     if (multiplyLeft) {
524369291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5244d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5245d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5246d3d1a6afSToby Isaac       if (numFields) {
5247d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5248d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5249d3d1a6afSToby Isaac 
5250d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5251d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5252d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5253d3d1a6afSToby Isaac             PetscInt c, r, k;
5254d3d1a6afSToby Isaac             PetscInt dof;
5255d3d1a6afSToby Isaac 
5256d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5257d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5258d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5259d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5260d3d1a6afSToby Isaac 
5261d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5262d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5263d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5264d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5265d3d1a6afSToby Isaac                   }
5266d3d1a6afSToby Isaac                 }
5267d3d1a6afSToby Isaac               }
5268d3d1a6afSToby Isaac             }
5269d3d1a6afSToby Isaac             else {
5270d3d1a6afSToby Isaac               /* copy this row as is */
5271d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5272d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5273d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5274d3d1a6afSToby Isaac                 }
5275d3d1a6afSToby Isaac               }
5276d3d1a6afSToby Isaac             }
5277d3d1a6afSToby Isaac             oldOff += dof;
5278d3d1a6afSToby Isaac           }
5279d3d1a6afSToby Isaac         }
5280d3d1a6afSToby Isaac       }
5281d3d1a6afSToby Isaac       else {
5282d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5283d3d1a6afSToby Isaac 
5284d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5285d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5286d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5287d3d1a6afSToby Isaac           PetscInt c, r, k;
5288d3d1a6afSToby Isaac           PetscInt dof;
5289d3d1a6afSToby Isaac 
5290d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5291d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5292d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5293d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5294d3d1a6afSToby Isaac 
5295d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5296d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5297d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5298d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5299d3d1a6afSToby Isaac                 }
5300d3d1a6afSToby Isaac               }
5301d3d1a6afSToby Isaac             }
5302d3d1a6afSToby Isaac           }
5303d3d1a6afSToby Isaac           else {
5304d3d1a6afSToby Isaac             /* copy this row as is */
53059fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5306d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5307d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5308d3d1a6afSToby Isaac               }
5309d3d1a6afSToby Isaac             }
5310d3d1a6afSToby Isaac           }
5311d3d1a6afSToby Isaac           oldOff += dof;
5312d3d1a6afSToby Isaac         }
5313d3d1a6afSToby Isaac       }
5314d3d1a6afSToby Isaac 
531569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
53166ecaa68aSToby Isaac     }
53176ecaa68aSToby Isaac     else {
53186ecaa68aSToby Isaac       newValues = tmpValues;
53196ecaa68aSToby Isaac     }
53206ecaa68aSToby Isaac   }
53216ecaa68aSToby Isaac 
5322d3d1a6afSToby Isaac   /* clean up */
532369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
532469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
53256ecaa68aSToby Isaac 
5326d3d1a6afSToby Isaac   if (numFields) {
5327d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
532869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
532969291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
533069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5331d3d1a6afSToby Isaac     }
5332d3d1a6afSToby Isaac   }
5333d3d1a6afSToby Isaac   else {
533469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
533569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
533669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5337d3d1a6afSToby Isaac   }
5338d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5339d3d1a6afSToby Isaac 
5340d3d1a6afSToby Isaac   /* output */
53416ecaa68aSToby Isaac   if (outPoints) {
5342d3d1a6afSToby Isaac     *outPoints = newPoints;
53436ecaa68aSToby Isaac   }
53446ecaa68aSToby Isaac   else {
534569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
53466ecaa68aSToby Isaac   }
534731620726SToby Isaac   if (outValues) {
5348d3d1a6afSToby Isaac     *outValues = newValues;
53496ecaa68aSToby Isaac   }
53506ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5351d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5352d3d1a6afSToby Isaac   }
5353d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5354d3d1a6afSToby Isaac }
5355d3d1a6afSToby Isaac 
53567cd05799SMatthew G. Knepley /*@C
5357a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
53587cd05799SMatthew G. Knepley 
53597cd05799SMatthew G. Knepley   Not collective
53607cd05799SMatthew G. Knepley 
53617cd05799SMatthew G. Knepley   Input Parameters:
53627cd05799SMatthew G. Knepley + dm - The DM
53637cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
53647cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
53657cd05799SMatthew G. Knepley - point - The mesh point
53667cd05799SMatthew G. Knepley 
53677cd05799SMatthew G. Knepley   Output parameters:
53687cd05799SMatthew G. Knepley + numIndices - The number of indices
53697cd05799SMatthew G. Knepley . indices - The indices
53707cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
53717cd05799SMatthew G. Knepley 
53727cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
53737cd05799SMatthew G. Knepley 
53747cd05799SMatthew G. Knepley   Level: advanced
53757cd05799SMatthew G. Knepley 
53767cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
53777cd05799SMatthew G. Knepley @*/
53786ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
53797773e69fSMatthew G. Knepley {
53807773e69fSMatthew G. Knepley   PetscSection    clSection;
53817773e69fSMatthew G. Knepley   IS              clPoints;
53827773e69fSMatthew G. Knepley   const PetscInt *clp;
53834acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
53847773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
53857773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
53867773e69fSMatthew G. Knepley   PetscInt        offsets[32];
53877773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
53887773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
53897773e69fSMatthew G. Knepley 
53907773e69fSMatthew G. Knepley   PetscFunctionBegin;
53917773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53927773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
53937773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
53947773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
53957773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
53967773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
53977773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
53987773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
53997773e69fSMatthew G. Knepley   /* Get points in closure */
5400923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54017773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
54027773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
54037773e69fSMatthew G. Knepley     PetscInt dof, fdof;
54047773e69fSMatthew G. Knepley 
54057773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
54067773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
54077773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
54087773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
54097773e69fSMatthew G. Knepley     }
54107773e69fSMatthew G. Knepley     Nind += dof;
54117773e69fSMatthew G. Knepley   }
54127773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
54138ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
54144acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
54154acb8e1eSToby Isaac   /* Get dual space symmetries */
54164acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
54174acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54184acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54194acb8e1eSToby Isaac   }
54207773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
54217773e69fSMatthew G. Knepley   {
54224acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
54237773e69fSMatthew G. Knepley     if (numPointsNew) {
54244acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
54254acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54264acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54274acb8e1eSToby Isaac       }
54284acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
54294acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
54304acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
54314acb8e1eSToby Isaac       }
5432923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54337773e69fSMatthew G. Knepley       numPoints = numPointsNew;
54347773e69fSMatthew G. Knepley       Nind      = NindNew;
54357773e69fSMatthew G. Knepley       points    = pointsNew;
54367773e69fSMatthew G. Knepley     }
54377773e69fSMatthew G. Knepley   }
54387773e69fSMatthew G. Knepley   /* Calculate indices */
543969291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
54407773e69fSMatthew G. Knepley   if (Nf) {
54416ecaa68aSToby Isaac     if (outOffsets) {
54426ecaa68aSToby Isaac       PetscInt f;
54436ecaa68aSToby Isaac 
544446bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
54456ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
54466ecaa68aSToby Isaac       }
54476ecaa68aSToby Isaac     }
54484acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
54494acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54504acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
54517773e69fSMatthew G. Knepley     }
54527773e69fSMatthew G. Knepley   } else {
54534acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
54544acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
54554acb8e1eSToby Isaac 
54564acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54574acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
54587773e69fSMatthew G. Knepley     }
54597773e69fSMatthew G. Knepley   }
54607773e69fSMatthew G. Knepley   /* Cleanup points */
54614acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
54624acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54634acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54644acb8e1eSToby Isaac   }
54657773e69fSMatthew G. Knepley   if (numPointsNew) {
546669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
54677773e69fSMatthew G. Knepley   } else {
5468923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54697773e69fSMatthew G. Knepley   }
54707773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
54717773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
54727773e69fSMatthew G. Knepley }
54737773e69fSMatthew G. Knepley 
54747cd05799SMatthew G. Knepley /*@C
54757cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
54767cd05799SMatthew G. Knepley 
54777cd05799SMatthew G. Knepley   Not collective
54787cd05799SMatthew G. Knepley 
54797cd05799SMatthew G. Knepley   Input Parameters:
54807cd05799SMatthew G. Knepley + dm - The DM
54817cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
54827cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
54837cd05799SMatthew G. Knepley . point - The mesh point
54847cd05799SMatthew G. Knepley . numIndices - The number of indices
54857cd05799SMatthew G. Knepley . indices - The indices
54867cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
54877cd05799SMatthew G. Knepley 
54887cd05799SMatthew G. Knepley   Level: advanced
54897cd05799SMatthew G. Knepley 
54907cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
54917cd05799SMatthew G. Knepley @*/
549246bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
54937773e69fSMatthew G. Knepley {
54947773e69fSMatthew G. Knepley   PetscErrorCode ierr;
54957773e69fSMatthew G. Knepley 
54967773e69fSMatthew G. Knepley   PetscFunctionBegin;
54977773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54987773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
549969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
55007773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55017773e69fSMatthew G. Knepley }
55027773e69fSMatthew G. Knepley 
55037f5d1fdeSMatthew G. Knepley /*@C
55047f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
55057f5d1fdeSMatthew G. Knepley 
55067f5d1fdeSMatthew G. Knepley   Not collective
55077f5d1fdeSMatthew G. Knepley 
55087f5d1fdeSMatthew G. Knepley   Input Parameters:
55097f5d1fdeSMatthew G. Knepley + dm - The DM
5510ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5511ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
55127f5d1fdeSMatthew G. Knepley . A - The matrix
5513eaf898f9SPatrick Sanan . point - The point in the DM
55147f5d1fdeSMatthew G. Knepley . values - The array of values
55157f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
55167f5d1fdeSMatthew G. Knepley 
55177f5d1fdeSMatthew G. Knepley   Fortran Notes:
55187f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
55197f5d1fdeSMatthew G. Knepley 
55207f5d1fdeSMatthew G. Knepley   Level: intermediate
55217f5d1fdeSMatthew G. Knepley 
55227f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
55237f5d1fdeSMatthew G. Knepley @*/
55247c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5525552f7358SJed Brown {
5526552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
55271b406b76SMatthew G. Knepley   PetscSection        clSection;
55281b406b76SMatthew G. Knepley   IS                  clPoints;
5529d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
55301b406b76SMatthew G. Knepley   const PetscInt     *clp;
5531552f7358SJed Brown   PetscInt           *indices;
5532552f7358SJed Brown   PetscInt            offsets[32];
55334acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
55344acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5535923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
55364acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5537d3d1a6afSToby Isaac   PetscScalar        *newValues;
5538552f7358SJed Brown   PetscErrorCode      ierr;
5539552f7358SJed Brown 
5540552f7358SJed Brown   PetscFunctionBegin;
5541552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5542ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
55433dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5544ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
55453dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
55463dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5547552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
554882f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5549552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5550923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5551552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5552552f7358SJed Brown     PetscInt fdof;
5553552f7358SJed Brown 
5554552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5555552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5556552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5557552f7358SJed Brown       offsets[f+1] += fdof;
5558552f7358SJed Brown     }
5559552f7358SJed Brown     numIndices += dof;
5560552f7358SJed Brown   }
55610d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
55620d644c17SKarl Rupp 
55638ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
55644acb8e1eSToby Isaac   /* Get symmetries */
55654acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
55664acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
55674acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
55684acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
55694acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
55704acb8e1eSToby Isaac 
55714acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
55724acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
55734acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
55744acb8e1eSToby Isaac 
55754acb8e1eSToby Isaac         if (!numFields) {
55764acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
55774acb8e1eSToby Isaac         } else {
55784acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
55794acb8e1eSToby Isaac         }
55804acb8e1eSToby Isaac         if (flip) {
55814acb8e1eSToby Isaac           PetscInt i, j, k;
55824acb8e1eSToby Isaac 
55834acb8e1eSToby Isaac           if (!valCopy) {
558469291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
55854acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
55864acb8e1eSToby Isaac             values = valCopy;
55874acb8e1eSToby Isaac           }
55884acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5589775f7be2SToby Isaac             PetscScalar fval = flip[i];
55904acb8e1eSToby Isaac 
55914acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
55924acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
55934acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
55944acb8e1eSToby Isaac             }
55954acb8e1eSToby Isaac           }
55964acb8e1eSToby Isaac         }
55974acb8e1eSToby Isaac         foffset += fdof;
55984acb8e1eSToby Isaac       }
55994acb8e1eSToby Isaac     }
56004acb8e1eSToby Isaac   }
56014acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5602d3d1a6afSToby Isaac   if (newNumPoints) {
56034acb8e1eSToby Isaac     if (valCopy) {
560469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56054acb8e1eSToby Isaac     }
56064acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56074acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56084acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56094acb8e1eSToby Isaac     }
56104acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56114acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56124acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56134acb8e1eSToby Isaac     }
5614923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5615d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5616d3d1a6afSToby Isaac     numIndices = newNumIndices;
5617d3d1a6afSToby Isaac     points     = newPoints;
5618d3d1a6afSToby Isaac     values     = newValues;
5619d3d1a6afSToby Isaac   }
562069291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5621552f7358SJed Brown   if (numFields) {
56224acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
56234acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
56244acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5625552f7358SJed Brown     }
5626552f7358SJed Brown   } else {
56274acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
56284acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
56294acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
56304acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5631552f7358SJed Brown     }
5632552f7358SJed Brown   }
5633b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5634552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5635ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5636ab1d0545SMatthew G. Knepley     PetscInt i;
5637ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
56388ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5639ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5640ab1d0545SMatthew G. Knepley   }
5641552f7358SJed Brown   if (ierr) {
5642552f7358SJed Brown     PetscMPIInt    rank;
5643552f7358SJed Brown     PetscErrorCode ierr2;
5644552f7358SJed Brown 
564582f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5646e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5647b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
564869291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
5649552f7358SJed Brown     CHKERRQ(ierr);
5650552f7358SJed Brown   }
56514acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
56524acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56534acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56544acb8e1eSToby Isaac   }
5655d3d1a6afSToby Isaac   if (newNumPoints) {
565669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
565769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
5658d3d1a6afSToby Isaac   }
5659d3d1a6afSToby Isaac   else {
56604acb8e1eSToby Isaac     if (valCopy) {
566169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56624acb8e1eSToby Isaac     }
5663923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5664d3d1a6afSToby Isaac   }
566569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5666552f7358SJed Brown   PetscFunctionReturn(0);
5667552f7358SJed Brown }
5668552f7358SJed Brown 
5669de41b84cSMatthew 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)
5670de41b84cSMatthew G. Knepley {
5671de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5672de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5673de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5674de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5675de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5676de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
56774ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5678de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5679de41b84cSMatthew G. Knepley 
5680de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5681de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5682de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5683de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5684de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5685de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5686de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5687de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5688de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5689de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5690de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5691de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5692de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5693de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5694de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5695de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5696de41b84cSMatthew G. Knepley   /* Column indices */
5697de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
56984ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5699de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5700de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5701de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5702de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5703de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5704de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5705de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5706de41b84cSMatthew G. Knepley       ++q;
5707de41b84cSMatthew G. Knepley     }
5708de41b84cSMatthew G. Knepley   }
5709de41b84cSMatthew G. Knepley   numCPoints = q;
5710de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5711de41b84cSMatthew G. Knepley     PetscInt fdof;
5712de41b84cSMatthew G. Knepley 
5713de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
57144ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5715de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5716de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5717de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5718de41b84cSMatthew G. Knepley     }
5719de41b84cSMatthew G. Knepley     numCIndices += dof;
5720de41b84cSMatthew G. Knepley   }
5721de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5722de41b84cSMatthew G. Knepley   /* Row indices */
5723de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5724de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
572569291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5726de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5727de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5728de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5729de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5730de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5731de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5732de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
57334ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
57344ca5e9f5SMatthew G. Knepley         if (!dof) continue;
57354ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
57364ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5737de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5738de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5739de41b84cSMatthew G. Knepley         ++q;
5740de41b84cSMatthew G. Knepley       }
5741de41b84cSMatthew G. Knepley     }
5742de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5743de41b84cSMatthew G. Knepley   }
5744de41b84cSMatthew G. Knepley   numFPoints = q;
5745de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5746de41b84cSMatthew G. Knepley     PetscInt fdof;
5747de41b84cSMatthew G. Knepley 
5748de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
57494ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5750de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5751de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5752de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5753de41b84cSMatthew G. Knepley     }
5754de41b84cSMatthew G. Knepley     numFIndices += dof;
5755de41b84cSMatthew G. Knepley   }
5756de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5757de41b84cSMatthew G. Knepley 
57588ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
57598ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
576069291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
576169291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5762de41b84cSMatthew G. Knepley   if (numFields) {
57634acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
57644acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
57654acb8e1eSToby Isaac 
57664acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57674acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57684acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5769de41b84cSMatthew G. Knepley     }
57704acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
57714acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57724acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
57734acb8e1eSToby Isaac     }
57744acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
57754acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57764acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
57774acb8e1eSToby Isaac     }
57784acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57794acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57804acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5781de41b84cSMatthew G. Knepley     }
5782de41b84cSMatthew G. Knepley   } else {
57834acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
57844acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
57854acb8e1eSToby Isaac 
57864acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57874acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57884acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
57894acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
57904acb8e1eSToby Isaac 
57914acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57924acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5793de41b84cSMatthew G. Knepley     }
57944acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
57954acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
57964acb8e1eSToby Isaac 
57974acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57984acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5799de41b84cSMatthew G. Knepley     }
58004acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58014acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5802de41b84cSMatthew G. Knepley   }
5803de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
58044acb8e1eSToby Isaac   /* TODO: flips */
5805de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5806de41b84cSMatthew G. Knepley   if (ierr) {
5807de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5808de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5809de41b84cSMatthew G. Knepley 
5810de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5811e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5812de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
581369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
581469291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
5815de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5816de41b84cSMatthew G. Knepley   }
581769291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5818de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
581969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
582069291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5821de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5822de41b84cSMatthew G. Knepley }
5823de41b84cSMatthew G. Knepley 
58247c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
58257c927364SMatthew G. Knepley {
58267c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
58277c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
58287c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
58297c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
58307c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
58317c927364SMatthew G. Knepley   PetscErrorCode ierr;
58327c927364SMatthew G. Knepley 
58337c927364SMatthew G. Knepley   PetscFunctionBegin;
58347c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
58357c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
58367c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
58377c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
58387c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
58397c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
58407c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
58417c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
58427c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
58437c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
58447c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
58457c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
58467c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
58477c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
58487c927364SMatthew G. Knepley   /* Column indices */
58497c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
58507c927364SMatthew G. Knepley   maxFPoints = numCPoints;
58517c927364SMatthew G. Knepley   /* Compress out points not in the section */
58527c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
58537c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
58547c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
58557c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
58567c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
58577c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
58587c927364SMatthew G. Knepley       ++q;
58597c927364SMatthew G. Knepley     }
58607c927364SMatthew G. Knepley   }
58617c927364SMatthew G. Knepley   numCPoints = q;
58627c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
58637c927364SMatthew G. Knepley     PetscInt fdof;
58647c927364SMatthew G. Knepley 
58657c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
58667c927364SMatthew G. Knepley     if (!dof) continue;
58677c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
58687c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
58697c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
58707c927364SMatthew G. Knepley     }
58717c927364SMatthew G. Knepley     numCIndices += dof;
58727c927364SMatthew G. Knepley   }
58737c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
58747c927364SMatthew G. Knepley   /* Row indices */
58757c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
58767c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
587769291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
58787c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
58797c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
58807c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
58817c927364SMatthew G. Knepley     /* Compress out points not in the section */
58827c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
58837c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
58847c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
58857c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
58867c927364SMatthew G. Knepley         if (!dof) continue;
58877c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
58887c927364SMatthew G. Knepley         if (s < q) continue;
58897c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
58907c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
58917c927364SMatthew G. Knepley         ++q;
58927c927364SMatthew G. Knepley       }
58937c927364SMatthew G. Knepley     }
58947c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
58957c927364SMatthew G. Knepley   }
58967c927364SMatthew G. Knepley   numFPoints = q;
58977c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
58987c927364SMatthew G. Knepley     PetscInt fdof;
58997c927364SMatthew G. Knepley 
59007c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
59017c927364SMatthew G. Knepley     if (!dof) continue;
59027c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59037c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
59047c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
59057c927364SMatthew G. Knepley     }
59067c927364SMatthew G. Knepley     numFIndices += dof;
59077c927364SMatthew G. Knepley   }
59087c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
59097c927364SMatthew G. Knepley 
59108ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
59118ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
59127c927364SMatthew G. Knepley   if (numFields) {
59134acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
59144acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
59154acb8e1eSToby Isaac 
59164acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
59174acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
59184acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
59197c927364SMatthew G. Knepley     }
59204acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
59214acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
59224acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
59234acb8e1eSToby Isaac     }
59244acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
59254acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
59264acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
59274acb8e1eSToby Isaac     }
59284acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
59294acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
59304acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
59317c927364SMatthew G. Knepley     }
59327c927364SMatthew G. Knepley   } else {
59334acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
59344acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
59354acb8e1eSToby Isaac 
59364acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59374acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
59384acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
59394acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
59404acb8e1eSToby Isaac 
59414acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
59424acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
59437c927364SMatthew G. Knepley     }
59444acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
59454acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
59464acb8e1eSToby Isaac 
59474acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
59484acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
59497c927364SMatthew G. Knepley     }
59504acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59514acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
59527c927364SMatthew G. Knepley   }
595369291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
59547c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
59557c927364SMatthew G. Knepley   PetscFunctionReturn(0);
59567c927364SMatthew G. Knepley }
59577c927364SMatthew G. Knepley 
5958f96281f8SMatthew G. Knepley /*@
5959f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5960f96281f8SMatthew G. Knepley 
5961f96281f8SMatthew G. Knepley   Input Parameter:
5962f96281f8SMatthew G. Knepley . dm - The DMPlex object
5963f96281f8SMatthew G. Knepley 
5964f96281f8SMatthew G. Knepley   Output Parameters:
5965f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5966dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5967dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5968dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5969f96281f8SMatthew G. Knepley 
5970f96281f8SMatthew G. Knepley   Level: developer
5971f96281f8SMatthew G. Knepley 
5972dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5973f96281f8SMatthew G. Knepley @*/
5974770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5975552f7358SJed Brown {
5976552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5977770b213bSMatthew G Knepley   PetscInt       dim;
5978770b213bSMatthew G Knepley   PetscErrorCode ierr;
5979552f7358SJed Brown 
5980552f7358SJed Brown   PetscFunctionBegin;
5981552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5982c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5983770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5984770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5985770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5986770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5987552f7358SJed Brown   PetscFunctionReturn(0);
5988552f7358SJed Brown }
5989552f7358SJed Brown 
5990dc5a3409SMatthew G. Knepley /*@
5991dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5992dc5a3409SMatthew G. Knepley 
5993dc5a3409SMatthew G. Knepley   Input Parameters:
5994dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5995dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5996dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5997dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5998dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5999dc5a3409SMatthew G. Knepley 
6000dc5a3409SMatthew G. Knepley   Level: developer
6001dc5a3409SMatthew G. Knepley 
6002dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6003dc5a3409SMatthew G. Knepley @*/
6004770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6005552f7358SJed Brown {
6006552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6007770b213bSMatthew G Knepley   PetscInt       dim;
6008770b213bSMatthew G Knepley   PetscErrorCode ierr;
6009552f7358SJed Brown 
6010552f7358SJed Brown   PetscFunctionBegin;
6011552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6012c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6013770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
6014770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
6015770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
6016770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
6017552f7358SJed Brown   PetscFunctionReturn(0);
6018552f7358SJed Brown }
6019552f7358SJed Brown 
60207cd05799SMatthew G. Knepley /*@C
60217cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
60227cd05799SMatthew G. Knepley 
60237cd05799SMatthew G. Knepley   Input Parameter:
60247cd05799SMatthew G. Knepley . dm   - The DMPlex object
60257cd05799SMatthew G. Knepley 
60267cd05799SMatthew G. Knepley   Output Parameter:
60277cd05799SMatthew G. Knepley . cellHeight - The height of a cell
60287cd05799SMatthew G. Knepley 
60297cd05799SMatthew G. Knepley   Level: developer
60307cd05799SMatthew G. Knepley 
60317cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
60327cd05799SMatthew G. Knepley @*/
6033552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6034552f7358SJed Brown {
6035552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6036552f7358SJed Brown 
6037552f7358SJed Brown   PetscFunctionBegin;
6038552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6039552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6040552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6041552f7358SJed Brown   PetscFunctionReturn(0);
6042552f7358SJed Brown }
6043552f7358SJed Brown 
60447cd05799SMatthew G. Knepley /*@C
60457cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
60467cd05799SMatthew G. Knepley 
60477cd05799SMatthew G. Knepley   Input Parameters:
60487cd05799SMatthew G. Knepley + dm   - The DMPlex object
60497cd05799SMatthew G. Knepley - cellHeight - The height of a cell
60507cd05799SMatthew G. Knepley 
60517cd05799SMatthew G. Knepley   Level: developer
60527cd05799SMatthew G. Knepley 
60537cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
60547cd05799SMatthew G. Knepley @*/
6055552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6056552f7358SJed Brown {
6057552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6058552f7358SJed Brown 
6059552f7358SJed Brown   PetscFunctionBegin;
6060552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6061552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6062552f7358SJed Brown   PetscFunctionReturn(0);
6063552f7358SJed Brown }
6064552f7358SJed Brown 
6065552f7358SJed Brown /* We can easily have a form that takes an IS instead */
6066ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6067552f7358SJed Brown {
6068552f7358SJed Brown   PetscSection   section, globalSection;
6069552f7358SJed Brown   PetscInt      *numbers, p;
6070552f7358SJed Brown   PetscErrorCode ierr;
6071552f7358SJed Brown 
6072552f7358SJed Brown   PetscFunctionBegin;
607382f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6074552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6075552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6076552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6077552f7358SJed Brown   }
6078552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
607915b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6080854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6081552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6082552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6083ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6084ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6085552f7358SJed Brown   }
608682f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6087ef48cebcSMatthew G. Knepley   if (globalSize) {
6088ef48cebcSMatthew G. Knepley     PetscLayout layout;
6089ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6090ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6091ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6092ef48cebcSMatthew G. Knepley   }
6093552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6094552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6095552f7358SJed Brown   PetscFunctionReturn(0);
6096552f7358SJed Brown }
6097552f7358SJed Brown 
609881ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6099552f7358SJed Brown {
6100552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6101552f7358SJed Brown   PetscErrorCode ierr;
6102552f7358SJed Brown 
6103552f7358SJed Brown   PetscFunctionBegin;
6104552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6105552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
61060298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
610781ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
610881ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
610981ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6110552f7358SJed Brown }
611181ed3555SMatthew G. Knepley 
61127cd05799SMatthew G. Knepley /*@C
61137cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
61147cd05799SMatthew G. Knepley 
61157cd05799SMatthew G. Knepley   Input Parameter:
61167cd05799SMatthew G. Knepley . dm   - The DMPlex object
61177cd05799SMatthew G. Knepley 
61187cd05799SMatthew G. Knepley   Output Parameter:
61197cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
61207cd05799SMatthew G. Knepley 
61217cd05799SMatthew G. Knepley   Level: developer
61227cd05799SMatthew G. Knepley 
61237cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
61247cd05799SMatthew G. Knepley @*/
612581ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
612681ed3555SMatthew G. Knepley {
612781ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
612881ed3555SMatthew G. Knepley   PetscErrorCode ierr;
612981ed3555SMatthew G. Knepley 
613081ed3555SMatthew G. Knepley   PetscFunctionBegin;
613181ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
613281ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6133552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6134552f7358SJed Brown   PetscFunctionReturn(0);
6135552f7358SJed Brown }
6136552f7358SJed Brown 
613781ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
613881ed3555SMatthew G. Knepley {
613981ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
614081ed3555SMatthew G. Knepley   PetscErrorCode ierr;
614181ed3555SMatthew G. Knepley 
614281ed3555SMatthew G. Knepley   PetscFunctionBegin;
614381ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
614481ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
614581ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
614681ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
614781ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
614881ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
614981ed3555SMatthew G. Knepley }
615081ed3555SMatthew G. Knepley 
61517cd05799SMatthew G. Knepley /*@C
61527cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
61537cd05799SMatthew G. Knepley 
61547cd05799SMatthew G. Knepley   Input Parameter:
61557cd05799SMatthew G. Knepley . dm   - The DMPlex object
61567cd05799SMatthew G. Knepley 
61577cd05799SMatthew G. Knepley   Output Parameter:
61587cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
61597cd05799SMatthew G. Knepley 
61607cd05799SMatthew G. Knepley   Level: developer
61617cd05799SMatthew G. Knepley 
61627cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
61637cd05799SMatthew G. Knepley @*/
6164552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6165552f7358SJed Brown {
6166552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6167552f7358SJed Brown   PetscErrorCode ierr;
6168552f7358SJed Brown 
6169552f7358SJed Brown   PetscFunctionBegin;
6170552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
617181ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6172552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6173552f7358SJed Brown   PetscFunctionReturn(0);
6174552f7358SJed Brown }
6175552f7358SJed Brown 
61767cd05799SMatthew G. Knepley /*@C
61777cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
61787cd05799SMatthew G. Knepley 
61797cd05799SMatthew G. Knepley   Input Parameter:
61807cd05799SMatthew G. Knepley . dm   - The DMPlex object
61817cd05799SMatthew G. Knepley 
61827cd05799SMatthew G. Knepley   Output Parameter:
61837cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
61847cd05799SMatthew G. Knepley 
61857cd05799SMatthew G. Knepley   Level: developer
61867cd05799SMatthew G. Knepley 
61877cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
61887cd05799SMatthew G. Knepley @*/
6189ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6190ef48cebcSMatthew G. Knepley {
6191ef48cebcSMatthew G. Knepley   IS             nums[4];
6192ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
6193ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6194ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6195ef48cebcSMatthew G. Knepley 
6196ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6197ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6198ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
61998abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
62008abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6201ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
6202ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
6203ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6204ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6205ef48cebcSMatthew G. Knepley 
6206ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
6207ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6208ef48cebcSMatthew G. Knepley     shift += gsize;
6209ef48cebcSMatthew G. Knepley   }
6210302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6211ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6212ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6213ef48cebcSMatthew G. Knepley }
6214ef48cebcSMatthew G. Knepley 
621508a22f4bSMatthew G. Knepley 
621608a22f4bSMatthew G. Knepley /*@
621708a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
621808a22f4bSMatthew G. Knepley 
621908a22f4bSMatthew G. Knepley   Input Parameter:
622008a22f4bSMatthew G. Knepley . dm - The DMPlex object
622108a22f4bSMatthew G. Knepley 
622208a22f4bSMatthew G. Knepley   Output Parameter:
622308a22f4bSMatthew G. Knepley . ranks - The rank field
622408a22f4bSMatthew G. Knepley 
622508a22f4bSMatthew G. Knepley   Options Database Keys:
622608a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
622708a22f4bSMatthew G. Knepley 
622808a22f4bSMatthew G. Knepley   Level: intermediate
622908a22f4bSMatthew G. Knepley 
623008a22f4bSMatthew G. Knepley .seealso: DMView()
623108a22f4bSMatthew G. Knepley @*/
623208a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
623308a22f4bSMatthew G. Knepley {
623408a22f4bSMatthew G. Knepley   DM             rdm;
623508a22f4bSMatthew G. Knepley   PetscDS        prob;
623608a22f4bSMatthew G. Knepley   PetscFE        fe;
623708a22f4bSMatthew G. Knepley   PetscScalar   *r;
623808a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
623908a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
624008a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
624108a22f4bSMatthew G. Knepley 
624208a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
624308a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
624408a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
624508a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
624608a22f4bSMatthew G. Knepley   ierr = PetscFECreateDefault(rdm, dim, 1, PETSC_TRUE, NULL, -1, &fe);CHKERRQ(ierr);
624708a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
624808a22f4bSMatthew G. Knepley   ierr = DMGetDS(rdm, &prob);CHKERRQ(ierr);
624908a22f4bSMatthew G. Knepley   ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe);CHKERRQ(ierr);
625008a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
625108a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
625208a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
625308a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
625408a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
625508a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
625608a22f4bSMatthew G. Knepley     PetscScalar *lr;
625708a22f4bSMatthew G. Knepley 
625808a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
625908a22f4bSMatthew G. Knepley     *lr = rank;
626008a22f4bSMatthew G. Knepley   }
626108a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
626208a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
626308a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
626408a22f4bSMatthew G. Knepley }
626508a22f4bSMatthew G. Knepley 
6266ca8062c8SMatthew G. Knepley /*@
6267ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6268ca8062c8SMatthew G. Knepley 
626969916449SMatthew G. Knepley   Input Parameter:
627069916449SMatthew G. Knepley . dm - The DMPlex object
6271ca8062c8SMatthew G. Knepley 
6272ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6273ca8062c8SMatthew G. Knepley 
6274ca8062c8SMatthew G. Knepley   Level: developer
6275ca8062c8SMatthew G. Knepley 
62769bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
6277ca8062c8SMatthew G. Knepley @*/
6278ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6279ca8062c8SMatthew G. Knepley {
6280ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6281ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6282ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
6283ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
6284ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6285ca8062c8SMatthew G. Knepley 
6286ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6287ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6288ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6289ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6290ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6291ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6292ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6293ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6294ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6295ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
629642e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
629742e66dfaSMatthew G. Knepley       PetscInt  d;
629842e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
629942e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
630042e66dfaSMatthew G. Knepley       }
6301ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6302ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6303ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6304ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6305ca8062c8SMatthew G. Knepley       }
630642e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
63078ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6308ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
63098ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6310ca8062c8SMatthew G. Knepley         }
6311302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63128ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6313ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
63148ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6315ca8062c8SMatthew G. Knepley         }
6316302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63178ccfff9cSToby 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]);
63188ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6319ca8062c8SMatthew G. Knepley       }
632042e66dfaSMatthew G. Knepley     }
6321ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6322ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6323ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6324ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6325ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6326ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6327ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6328ca8062c8SMatthew G. Knepley       }
6329ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
63308ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6331ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
63328ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6333ca8062c8SMatthew G. Knepley         }
6334302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63358ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6336ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
63378ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6338ca8062c8SMatthew G. Knepley         }
6339302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63408ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6341ca8062c8SMatthew G. Knepley       }
6342ca8062c8SMatthew G. Knepley     }
6343ca8062c8SMatthew G. Knepley   }
6344ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6345ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
63468ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6347ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6348ca8062c8SMatthew G. Knepley }
6349ca8062c8SMatthew G. Knepley 
6350ca8062c8SMatthew G. Knepley /*@
6351ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6352ca8062c8SMatthew G. Knepley 
6353ca8062c8SMatthew G. Knepley   Input Parameters:
6354ca8062c8SMatthew G. Knepley + dm - The DMPlex object
635558723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
635658723a97SMatthew G. Knepley - cellHeight - Normally 0
6357ca8062c8SMatthew G. Knepley 
6358ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6359ca8062c8SMatthew G. Knepley 
6360ca8062c8SMatthew G. Knepley   Level: developer
6361ca8062c8SMatthew G. Knepley 
63629bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
6363ca8062c8SMatthew G. Knepley @*/
636458723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6365ca8062c8SMatthew G. Knepley {
636642363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6367ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6368ca8062c8SMatthew G. Knepley 
6369ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6370ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6371c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6372ca8062c8SMatthew G. Knepley   switch (dim) {
637342363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
637442363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
637542363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6376ca8062c8SMatthew G. Knepley   default:
63778ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6378ca8062c8SMatthew G. Knepley   }
637958723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
638058723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6381ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6382ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6383ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
638458723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
638558723a97SMatthew G. Knepley 
638658723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
638758723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
638858723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
638958723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
639058723a97SMatthew G. Knepley     }
639158723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
63928ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6393ca8062c8SMatthew G. Knepley   }
639442363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
639542363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
639642363296SMatthew G. Knepley 
639742363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
639842363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
639942363296SMatthew G. Knepley       const PetscInt p = closure[cl];
640042363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
640142363296SMatthew G. Knepley     }
640242363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
64038ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
640442363296SMatthew G. Knepley   }
6405ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6406ca8062c8SMatthew G. Knepley }
64079bf0dad6SMatthew G. Knepley 
64089bf0dad6SMatthew G. Knepley /*@
64099bf0dad6SMatthew 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
64109bf0dad6SMatthew G. Knepley 
64119bf0dad6SMatthew G. Knepley   Input Parameters:
64129bf0dad6SMatthew G. Knepley + dm - The DMPlex object
64139bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
64149bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
64159bf0dad6SMatthew G. Knepley 
64169bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
64179bf0dad6SMatthew G. Knepley 
64189bf0dad6SMatthew G. Knepley   Level: developer
64199bf0dad6SMatthew G. Knepley 
64209bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
64219bf0dad6SMatthew G. Knepley @*/
64229bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
64239bf0dad6SMatthew G. Knepley {
64243554e41dSMatthew G. Knepley   PetscInt       pMax[4];
64253554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
64269bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
64279bf0dad6SMatthew G. Knepley 
64289bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
64299bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6430c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
64319bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
643225abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
64333554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
64343554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
64353554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
64369bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
64379bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
64389bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
64399bf0dad6SMatthew G. Knepley 
644025abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
64419bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
64429bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
64439bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
64449bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
64459bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
64469bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
64479bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
64489bf0dad6SMatthew G. Knepley       }
64493554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
64508ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
64519bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
64529bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
64539bf0dad6SMatthew G. Knepley 
64549bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
64559bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
64569bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
64579bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
64589bf0dad6SMatthew G. Knepley         }
64598ccfff9cSToby 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);
64609bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
64618ccfff9cSToby 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]);
64629bf0dad6SMatthew G. Knepley         }
64639bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
64649bf0dad6SMatthew G. Knepley       }
64659bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
64669bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
64679bf0dad6SMatthew G. Knepley     }
64683554e41dSMatthew G. Knepley   }
6469552f7358SJed Brown   PetscFunctionReturn(0);
6470552f7358SJed Brown }
64713913d7c8SMatthew G. Knepley 
6472bceba477SMatthew G. Knepley /* Pointwise interpolation
6473bceba477SMatthew G. Knepley      Just code FEM for now
6474bceba477SMatthew G. Knepley      u^f = I u^c
64754ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
64764ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
64774ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6478bceba477SMatthew G. Knepley */
6479bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6480bceba477SMatthew G. Knepley {
6481bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6482bceba477SMatthew G. Knepley   PetscInt       m, n;
6483a063dac3SMatthew G. Knepley   void          *ctx;
648468132eb9SMatthew G. Knepley   DM             cdm;
648568132eb9SMatthew G. Knepley   PetscBool      regular;
6486bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6487bceba477SMatthew G. Knepley 
6488bceba477SMatthew G. Knepley   PetscFunctionBegin;
6489bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6490bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6491bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6492bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
649368132eb9SMatthew G. Knepley 
6494bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6495bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6496bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6497a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
649868132eb9SMatthew G. Knepley 
6499a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
650068132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
650168132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
650268132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
650368132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
65045d1c2e58SMatthew G. Knepley   /* Use naive scaling */
65055d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6506a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6507a063dac3SMatthew G. Knepley }
6508bceba477SMatthew G. Knepley 
65096dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6510a063dac3SMatthew G. Knepley {
651190748bafSMatthew G. Knepley   PetscErrorCode ierr;
65126dbf9973SLawrence Mitchell   VecScatter     ctx;
651390748bafSMatthew G. Knepley 
6514a063dac3SMatthew G. Knepley   PetscFunctionBegin;
65156dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
65166dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
65176dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6518bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6519bceba477SMatthew G. Knepley }
6520bceba477SMatthew G. Knepley 
6521bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
6522bd041c0cSMatthew G. Knepley {
6523bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
6524bd041c0cSMatthew G. Knepley   PetscInt       m, n;
6525bd041c0cSMatthew G. Knepley   void          *ctx;
6526bd041c0cSMatthew G. Knepley   DM             cdm;
6527bd041c0cSMatthew G. Knepley   PetscBool      regular;
6528bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
6529bd041c0cSMatthew G. Knepley 
6530bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
6531bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6532bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6533bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6534bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
6535bd041c0cSMatthew G. Knepley 
6536bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
6537bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6538bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
6539bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
6540bd041c0cSMatthew G. Knepley 
6541bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
6542bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
6543bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6544bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6545bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
6546bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
6547bd041c0cSMatthew G. Knepley }
6548bd041c0cSMatthew G. Knepley 
6549fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6550fd59a867SMatthew G. Knepley {
6551fd59a867SMatthew G. Knepley   PetscSection   section;
6552ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6553ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6554286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6555ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6556ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6557fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6558fd59a867SMatthew G. Knepley 
6559fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6560ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6561ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6562ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6563ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6564ebb3236fSMatthew G. Knepley     PetscObject  obj;
6565ebb3236fSMatthew G. Knepley     PetscClassId id;
6566ebb3236fSMatthew G. Knepley 
6567ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6568ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6569ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6570ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
65718ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6572ebb3236fSMatthew G. Knepley   }
65732f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6574286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6575c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6576ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6577ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6578dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6579fdafae62SVaclav Hapla   if (!numFields && numBd) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "number of fields is zero and number of boundary conditions is nonzero (this should never happen)");
6580fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
65812f900235SMatthew G. Knepley     PetscInt                field;
6582f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6583385532a5SToby Isaac     const char             *labelName;
6584385532a5SToby Isaac     DMLabel                 label;
65852f900235SMatthew G. Knepley 
6586f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6587385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6588f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6589fd59a867SMatthew G. Knepley   }
65902f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6591ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
65926c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6593ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6594ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6595ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6596ebb3236fSMatthew G. Knepley 
6597ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6598ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6599ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6600ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6601ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6602ebb3236fSMatthew G. Knepley   }
6603ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6604ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6605fd59a867SMatthew G. Knepley     const char             *bdLabel;
6606fd59a867SMatthew G. Knepley     DMLabel                 label;
66070e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6608fd59a867SMatthew G. Knepley     const PetscInt         *values;
66090e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6610f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6611f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6612fd59a867SMatthew G. Knepley 
6613f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6614c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6615385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6616ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
66177391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
66187391c1cbSMatthew G. Knepley       const char *bdname;
6619dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
66207391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
66217391c1cbSMatthew G. Knepley       if (duplicate) break;
66227391c1cbSMatthew G. Knepley     }
6623ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6624b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6625092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
66267391c1cbSMatthew G. Knepley     }
6627ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6628f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
662993a5981aSMatthew G. Knepley       PetscInt       *newidx;
6630ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
663193a5981aSMatthew G. Knepley 
6632fd59a867SMatthew G. Knepley       bcFields[bc] = field;
66330e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6634ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6635ebb3236fSMatthew G. Knepley         IS              tmp;
6636ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6637ebb3236fSMatthew G. Knepley 
6638c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6639ebb3236fSMatthew G. Knepley         if (!tmp) continue;
664093a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
664193a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6642ebb3236fSMatthew G. Knepley         if (isFE[field]) {
664393a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6644ebb3236fSMatthew G. Knepley         } else {
6645ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6646ebb3236fSMatthew G. Knepley         }
664793a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
664893a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6649fd59a867SMatthew G. Knepley       }
6650ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6651ebb3236fSMatthew G. Knepley       newn = 0;
6652ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6653ebb3236fSMatthew G. Knepley         IS              tmp;
6654ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6655ebb3236fSMatthew G. Knepley 
6656c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6657ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6658ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6659ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6660ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6661ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6662ebb3236fSMatthew G. Knepley         } else {
6663ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6664ebb3236fSMatthew G. Knepley         }
6665ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6666ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6667ebb3236fSMatthew G. Knepley       }
6668ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6669ebb3236fSMatthew G. Knepley     }
6670fd59a867SMatthew G. Knepley   }
6671fd59a867SMatthew G. Knepley   /* Handle discretization */
6672ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6673fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
66740bacfa0aSMatthew G. Knepley     PetscObject obj;
66750bacfa0aSMatthew G. Knepley 
66760bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6677ebb3236fSMatthew G. Knepley     if (isFE[f]) {
66780bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6679286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6680fd59a867SMatthew G. Knepley       PetscInt        d;
6681fd59a867SMatthew G. Knepley 
6682fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6683286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6684286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6685ebb3236fSMatthew G. Knepley     } else {
66860bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
66870bacfa0aSMatthew G. Knepley 
66880bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
66890bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6690ebb3236fSMatthew G. Knepley     }
6691fd59a867SMatthew G. Knepley   }
6692286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6693286d8613SMatthew G. Knepley     PetscInt d;
6694286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6695286d8613SMatthew G. Knepley       if ((numDof[f*(dim+1)+d] > 0) && (depth < dim)) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mesh must be interpolated when unknowns are specified on edges or faces.");
6696286d8613SMatthew G. Knepley     }
6697286d8613SMatthew G. Knepley   }
6698ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6699fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6700fd59a867SMatthew G. Knepley     PetscFE     fe;
6701fd59a867SMatthew G. Knepley     const char *name;
670218abd763SMatthew G. Knepley 
670318abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6704fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6705fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6706fd59a867SMatthew G. Knepley   }
6707fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6708fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
67090e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
67100e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6711286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6712ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6713bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6714bceba477SMatthew G. Knepley }
6715bceba477SMatthew G. Knepley 
67160aef6b92SMatthew G. Knepley /*@
67170aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
67180aef6b92SMatthew G. Knepley 
67190aef6b92SMatthew G. Knepley   Input Parameter:
67200aef6b92SMatthew G. Knepley . dm - The DMPlex object
67210aef6b92SMatthew G. Knepley 
67220aef6b92SMatthew G. Knepley   Output Parameter:
67230aef6b92SMatthew G. Knepley . regular - The flag
67240aef6b92SMatthew G. Knepley 
67250aef6b92SMatthew G. Knepley   Level: intermediate
67260aef6b92SMatthew G. Knepley 
67270aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
67280aef6b92SMatthew G. Knepley @*/
67290aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
67300aef6b92SMatthew G. Knepley {
67310aef6b92SMatthew G. Knepley   PetscFunctionBegin;
67320aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67330aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
67340aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
67350aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
67360aef6b92SMatthew G. Knepley }
67370aef6b92SMatthew G. Knepley 
67380aef6b92SMatthew G. Knepley /*@
67390aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
67400aef6b92SMatthew G. Knepley 
67410aef6b92SMatthew G. Knepley   Input Parameters:
67420aef6b92SMatthew G. Knepley + dm - The DMPlex object
67430aef6b92SMatthew G. Knepley - regular - The flag
67440aef6b92SMatthew G. Knepley 
67450aef6b92SMatthew G. Knepley   Level: intermediate
67460aef6b92SMatthew G. Knepley 
67470aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
67480aef6b92SMatthew G. Knepley @*/
67490aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
67500aef6b92SMatthew G. Knepley {
67510aef6b92SMatthew G. Knepley   PetscFunctionBegin;
67520aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67530aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
67540aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
67550aef6b92SMatthew G. Knepley }
67560aef6b92SMatthew G. Knepley 
6757f7c74593SToby Isaac /* anchors */
6758a68b90caSToby Isaac /*@
6759f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6760f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6761a68b90caSToby Isaac 
6762e228b242SToby Isaac   not collective
6763a68b90caSToby Isaac 
6764a68b90caSToby Isaac   Input Parameters:
6765a68b90caSToby Isaac . dm - The DMPlex object
6766a68b90caSToby Isaac 
6767a68b90caSToby Isaac   Output Parameters:
6768a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6769a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6770a68b90caSToby Isaac 
6771a68b90caSToby Isaac 
6772a68b90caSToby Isaac   Level: intermediate
6773a68b90caSToby Isaac 
6774f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6775a68b90caSToby Isaac @*/
6776a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6777a68b90caSToby Isaac {
6778a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
677941e6d900SToby Isaac   PetscErrorCode ierr;
6780a68b90caSToby Isaac 
6781a68b90caSToby Isaac   PetscFunctionBegin;
6782a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
678341e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6784a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6785a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6786a68b90caSToby Isaac   PetscFunctionReturn(0);
6787a68b90caSToby Isaac }
6788a68b90caSToby Isaac 
6789a68b90caSToby Isaac /*@
6790f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6791f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6792a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6793a68b90caSToby Isaac 
6794a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6795f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6796a68b90caSToby Isaac 
6797e228b242SToby Isaac   collective on dm
6798a68b90caSToby Isaac 
6799a68b90caSToby Isaac   Input Parameters:
6800a68b90caSToby Isaac + dm - The DMPlex object
6801e228b242SToby 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).
6802e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6803a68b90caSToby Isaac 
6804a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6805a68b90caSToby Isaac 
6806a68b90caSToby Isaac   Level: intermediate
6807a68b90caSToby Isaac 
6808f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6809a68b90caSToby Isaac @*/
6810a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6811a68b90caSToby Isaac {
6812a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6813e228b242SToby Isaac   PetscMPIInt    result;
6814a68b90caSToby Isaac   PetscErrorCode ierr;
6815a68b90caSToby Isaac 
6816a68b90caSToby Isaac   PetscFunctionBegin;
6817a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6818e228b242SToby Isaac   if (anchorSection) {
6819e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6820e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6821f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6822e228b242SToby Isaac   }
6823e228b242SToby Isaac   if (anchorIS) {
6824e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6825e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6826f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6827e228b242SToby Isaac   }
6828a68b90caSToby Isaac 
6829a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6830a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6831a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6832a68b90caSToby Isaac 
6833a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6834a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6835a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6836a68b90caSToby Isaac 
6837a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6838a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6839a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6840a68b90caSToby Isaac     const PetscInt *anchors;
6841a68b90caSToby Isaac 
6842a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6843a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6844a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6845a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6846a68b90caSToby Isaac       PetscInt p;
6847a68b90caSToby Isaac 
6848a68b90caSToby Isaac       p = anchors[a];
6849a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6850a68b90caSToby Isaac         PetscInt dof;
6851a68b90caSToby Isaac 
6852a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6853a68b90caSToby Isaac         if (dof) {
6854a68b90caSToby Isaac           PetscErrorCode ierr2;
6855a68b90caSToby Isaac 
6856a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
68578ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6858a68b90caSToby Isaac         }
6859a68b90caSToby Isaac       }
6860a68b90caSToby Isaac     }
6861a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6862a68b90caSToby Isaac   }
6863a68b90caSToby Isaac #endif
6864f7c74593SToby Isaac   /* reset the generic constraints */
6865f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6866a68b90caSToby Isaac   PetscFunctionReturn(0);
6867a68b90caSToby Isaac }
6868a68b90caSToby Isaac 
6869f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6870a68b90caSToby Isaac {
6871f7c74593SToby Isaac   PetscSection anchorSection;
68726995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6873a68b90caSToby Isaac   PetscErrorCode ierr;
6874a68b90caSToby Isaac 
6875a68b90caSToby Isaac   PetscFunctionBegin;
6876a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6877a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6878e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6879a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
68806995de1eSToby Isaac   if (numFields) {
6881719ab38cSToby Isaac     PetscInt f;
6882a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6883719ab38cSToby Isaac 
6884719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6885719ab38cSToby Isaac       PetscInt numComp;
6886719ab38cSToby Isaac 
6887719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6888719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6889719ab38cSToby Isaac     }
68906995de1eSToby Isaac   }
6891a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
68926995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
68936995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
68946995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
68956995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6896a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6897a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6898a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6899a68b90caSToby Isaac     if (dof) {
6900a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6901a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6902a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6903a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6904a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6905a68b90caSToby Isaac       }
6906a68b90caSToby Isaac     }
6907a68b90caSToby Isaac   }
6908a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6909a68b90caSToby Isaac   PetscFunctionReturn(0);
6910a68b90caSToby Isaac }
6911a68b90caSToby Isaac 
6912f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6913a68b90caSToby Isaac {
6914f7c74593SToby Isaac   PetscSection aSec;
69150ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
69160ac89760SToby Isaac   const PetscInt *anchors;
69170ac89760SToby Isaac   PetscInt numFields, f;
691866ad2231SToby Isaac   IS aIS;
69190ac89760SToby Isaac   PetscErrorCode ierr;
69200ac89760SToby Isaac 
69210ac89760SToby Isaac   PetscFunctionBegin;
69220ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69230ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
69240ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
69250ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
69260ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6927302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6928a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
692966ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
69306995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
69316995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
69320ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
69330ac89760SToby Isaac   i[0] = 0;
69340ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
69350ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6936f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6937f19733c5SToby Isaac 
6938f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6939f19733c5SToby Isaac     if (!rDof) continue;
6940f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
69410ac89760SToby Isaac     if (numFields) {
69420ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
69430ac89760SToby Isaac         annz = 0;
6944f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6945f19733c5SToby Isaac           a = anchors[rOff + r];
69460ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
69470ac89760SToby Isaac           annz += aDof;
69480ac89760SToby Isaac         }
69490ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
69500ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
69510ac89760SToby Isaac         for (q = 0; q < dof; q++) {
69520ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
69530ac89760SToby Isaac         }
69540ac89760SToby Isaac       }
69550ac89760SToby Isaac     }
69560ac89760SToby Isaac     else {
69570ac89760SToby Isaac       annz = 0;
69580ac89760SToby Isaac       for (q = 0; q < dof; q++) {
69590ac89760SToby Isaac         a = anchors[off + q];
69600ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
69610ac89760SToby Isaac         annz += aDof;
69620ac89760SToby Isaac       }
69630ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
69640ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
69650ac89760SToby Isaac       for (q = 0; q < dof; q++) {
69660ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
69670ac89760SToby Isaac       }
69680ac89760SToby Isaac     }
69690ac89760SToby Isaac   }
69700ac89760SToby Isaac   nnz = i[m];
69710ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
69720ac89760SToby Isaac   offset = 0;
69730ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
69740ac89760SToby Isaac     if (numFields) {
69750ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
69760ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
69770ac89760SToby Isaac         for (q = 0; q < dof; q++) {
69780ac89760SToby Isaac           PetscInt rDof, rOff, r;
69790ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
69800ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
69810ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
69820ac89760SToby Isaac             PetscInt s;
69830ac89760SToby Isaac 
69840ac89760SToby Isaac             a = anchors[rOff + r];
69850ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
69860ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
69870ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
69880ac89760SToby Isaac               j[offset++] = aOff + s;
69890ac89760SToby Isaac             }
69900ac89760SToby Isaac           }
69910ac89760SToby Isaac         }
69920ac89760SToby Isaac       }
69930ac89760SToby Isaac     }
69940ac89760SToby Isaac     else {
69950ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
69960ac89760SToby Isaac       for (q = 0; q < dof; q++) {
69970ac89760SToby Isaac         PetscInt rDof, rOff, r;
69980ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
69990ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
70000ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
70010ac89760SToby Isaac           PetscInt s;
70020ac89760SToby Isaac 
70030ac89760SToby Isaac           a = anchors[rOff + r];
70040ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
70050ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
70060ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
70070ac89760SToby Isaac             j[offset++] = aOff + s;
70080ac89760SToby Isaac           }
70090ac89760SToby Isaac         }
70100ac89760SToby Isaac       }
70110ac89760SToby Isaac     }
70120ac89760SToby Isaac   }
70130ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
701425570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
701525570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
701666ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
70170ac89760SToby Isaac   PetscFunctionReturn(0);
70180ac89760SToby Isaac }
70190ac89760SToby Isaac 
702066ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
702166ad2231SToby Isaac {
7022f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7023f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
702466ad2231SToby Isaac   Mat            cMat;
702566ad2231SToby Isaac   PetscErrorCode ierr;
702666ad2231SToby Isaac 
702766ad2231SToby Isaac   PetscFunctionBegin;
702866ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7029a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
703066ad2231SToby Isaac   if (anchorSection) {
7031e228b242SToby Isaac     PetscDS  ds;
7032e228b242SToby Isaac     PetscInt nf;
7033e228b242SToby Isaac 
7034f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
7035f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7036f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
7037e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
7038302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
7039e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
704066ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
704166ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
704266ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
704366ad2231SToby Isaac   }
704466ad2231SToby Isaac   PetscFunctionReturn(0);
704566ad2231SToby Isaac }
7046