xref: /petsc/src/dm/impls/plex/plex.c (revision 5483465ca82363d55d0af0b3fb6a7f863d14bf6d)
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 {
95a99a26bcSAdrian Croucher   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior;
96a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
977e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
987e42fee7SMatthew G. Knepley 
997e42fee7SMatthew G. Knepley   PetscFunctionBegin;
1007e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
101c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1027e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1037e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10480d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
10580d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1067e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1077e42fee7SMatthew G. Knepley   if (field >= 0) {
108a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
109a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
1107e42fee7SMatthew G. Knepley   } else {
111a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
112a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
1137e42fee7SMatthew G. Knepley   }
114*5483465cSMatthew G. Knepley   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
115a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
1167e42fee7SMatthew G. Knepley     *sStart = vStart;
1177e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
118a99a26bcSAdrian Croucher     if (globalvcdof[0] == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1197e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_POINT_FIELD;
120a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
1217e42fee7SMatthew G. Knepley     *sStart = cStart;
1227e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
123a99a26bcSAdrian Croucher     if (globalvcdof[1] == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1247e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_CELL_FIELD;
1257e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
1267e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1277e42fee7SMatthew G. Knepley }
1287e42fee7SMatthew G. Knepley 
1297cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
130e412dcbdSMatthew G. Knepley {
131e412dcbdSMatthew G. Knepley   DM                 dm;
132d1df6f1dSMatthew G. Knepley   PetscSection       s;
133e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
134e412dcbdSMatthew G. Knepley   DM                 cdm;
135e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
136e412dcbdSMatthew G. Knepley   Vec                coordinates;
137e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
138e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
139339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
140339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
141d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
142e412dcbdSMatthew G. Knepley   const char        *name;
143339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
144e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
145e412dcbdSMatthew G. Knepley 
146e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
147d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
148d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
149d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
150d1df6f1dSMatthew G. Knepley 
151e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
152e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1538135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
154d1df6f1dSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
155d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
156e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
157e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
158e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
159e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
160e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
161e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
162e412dcbdSMatthew G. Knepley 
163e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
164339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
165e412dcbdSMatthew G. Knepley 
166e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
167e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
168e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1690c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1700c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
171e412dcbdSMatthew G. Knepley   }
172e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
173e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley 
175d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
176d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
177d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
178d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
179d1df6f1dSMatthew G. Knepley     IS   fis;
180d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
181d1df6f1dSMatthew G. Knepley     const char *fname;
182d1df6f1dSMatthew G. Knepley 
183d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
184d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
185d1df6f1dSMatthew G. Knepley 
186a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
187d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
188d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
189d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
190d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
191a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
192a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
193d1df6f1dSMatthew G. Knepley     }
194d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
195d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
196d1df6f1dSMatthew G. Knepley 
197d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
198d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
199d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
200d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
201d1df6f1dSMatthew G. Knepley 
202d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
203d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
204339e3443SMatthew G. Knepley       if (!flg) {
205d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
206d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
207d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
208339e3443SMatthew G. Knepley       }
209e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
210339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
211d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
212e412dcbdSMatthew G. Knepley 
213d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
214e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
21599a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
216e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
217e412dcbdSMatthew G. Knepley 
218d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
219339e3443SMatthew G. Knepley         if (a) {
220d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
221339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
222339e3443SMatthew G. Knepley         } else {
223339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
224339e3443SMatthew G. Knepley           PetscInt     numVals, va;
225339e3443SMatthew G. Knepley 
226d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
227d1df6f1dSMatthew 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);
228d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
229d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
230d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
231d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
232339e3443SMatthew G. Knepley             break;
233d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
234d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
235d1df6f1dSMatthew 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]);
236d1df6f1dSMatthew G. Knepley             break;
237d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
238339e3443SMatthew G. Knepley           }
239d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
240339e3443SMatthew G. Knepley         }
241e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
242e412dcbdSMatthew G. Knepley         switch (numCoords) {
243e412dcbdSMatthew G. Knepley         case 6:
244339e3443SMatthew 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);
245e412dcbdSMatthew G. Knepley           break;
246e412dcbdSMatthew G. Knepley         case 8:
247339e3443SMatthew 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);
248339e3443SMatthew 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);
249e412dcbdSMatthew G. Knepley           break;
250e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
251e412dcbdSMatthew G. Knepley         }
252e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
253e412dcbdSMatthew G. Knepley       }
254d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
255e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
256e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
257e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
258d1df6f1dSMatthew G. Knepley     }
259d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
260d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
261d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
262d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
263d1df6f1dSMatthew G. Knepley     }
264d1df6f1dSMatthew G. Knepley   }
265e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
266e412dcbdSMatthew G. Knepley }
267e412dcbdSMatthew G. Knepley 
268684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
269684b87d9SLisandro Dalcin {
270684b87d9SLisandro Dalcin   DM                      dm;
271684b87d9SLisandro Dalcin   Vec                     locv;
272684b87d9SLisandro Dalcin   const char              *name;
273684b87d9SLisandro Dalcin   PetscSection            section;
274684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
275684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
276684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
277684b87d9SLisandro Dalcin 
278684b87d9SLisandro Dalcin   PetscFunctionBegin;
279684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
280684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
281684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
282684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
283684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
284684b87d9SLisandro Dalcin   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
285684b87d9SLisandro Dalcin   ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
286684b87d9SLisandro Dalcin   ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) locv);CHKERRQ(ierr);
287684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
288684b87d9SLisandro Dalcin }
289684b87d9SLisandro Dalcin 
290552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
291552f7358SJed Brown {
292552f7358SJed Brown   DM             dm;
293684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
294552f7358SJed Brown   PetscErrorCode ierr;
295552f7358SJed Brown 
296552f7358SJed Brown   PetscFunctionBegin;
297552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
29882f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
299552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
300b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
301f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3028135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
303684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
304684b87d9SLisandro Dalcin     PetscInt    i,numFields;
305684b87d9SLisandro Dalcin     PetscObject fe;
306ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
307684b87d9SLisandro Dalcin     Vec         locv = v;
308684b87d9SLisandro Dalcin     const char  *name;
309684b87d9SLisandro Dalcin     PetscInt    step;
310684b87d9SLisandro Dalcin     PetscReal   time;
311ef31f671SMatthew G. Knepley 
312ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
313684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
314684b87d9SLisandro Dalcin       ierr = DMGetField(dm, i, &fe);CHKERRQ(ierr);
315684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
316ef31f671SMatthew G. Knepley     }
317684b87d9SLisandro Dalcin     if (fem) {
318684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
319684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
320684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
321684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
322684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
323684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
324ef31f671SMatthew G. Knepley     }
325552f7358SJed Brown     if (isvtk) {
326684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
327b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
328b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
329684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
330b136c2c9SMatthew G. Knepley #else
331b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
332b136c2c9SMatthew G. Knepley #endif
333f13a32a3SMatthew G. Knepley     } else if (isdraw) {
334684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
335684b87d9SLisandro Dalcin     } else if (isglvis) {
336684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
337684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
338684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
339684b87d9SLisandro Dalcin     }
340684b87d9SLisandro Dalcin     if (fem) {ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);}
341552f7358SJed Brown   } else {
342684b87d9SLisandro Dalcin     PetscBool isseq;
343684b87d9SLisandro Dalcin 
344684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
34555f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
34655f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
347552f7358SJed Brown   }
348552f7358SJed Brown   PetscFunctionReturn(0);
349552f7358SJed Brown }
350552f7358SJed Brown 
351552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
352552f7358SJed Brown {
353552f7358SJed Brown   DM             dm;
354684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
355552f7358SJed Brown   PetscErrorCode ierr;
356552f7358SJed Brown 
357552f7358SJed Brown   PetscFunctionBegin;
358552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
35982f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
360552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
36133c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
362e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3638135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
364684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
365552f7358SJed Brown     Vec         locv;
366552f7358SJed Brown     const char *name;
367552f7358SJed Brown 
368c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
369552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
370552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
371552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
372552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
373552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
374c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
375b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
376b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
37739d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
378b136c2c9SMatthew G. Knepley #else
379b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
380b136c2c9SMatthew G. Knepley #endif
381552f7358SJed Brown   } else {
382684b87d9SLisandro Dalcin     PetscBool isseq;
383684b87d9SLisandro Dalcin 
384684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
38555f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
38655f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
387552f7358SJed Brown   }
388552f7358SJed Brown   PetscFunctionReturn(0);
389552f7358SJed Brown }
390552f7358SJed Brown 
391d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
392d930f514SMatthew G. Knepley {
393d930f514SMatthew G. Knepley   DM                dm;
394d930f514SMatthew G. Knepley   MPI_Comm          comm;
395d930f514SMatthew G. Knepley   PetscViewerFormat format;
396d930f514SMatthew G. Knepley   Vec               v;
397d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
398d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
399d930f514SMatthew G. Knepley 
400d930f514SMatthew G. Knepley   PetscFunctionBegin;
401d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
402d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4032c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
404d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
405d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
406d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
407d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
408d930f514SMatthew G. Knepley     const char *vecname;
409d930f514SMatthew G. Knepley     PetscInt    n, nroots;
410d930f514SMatthew G. Knepley 
411d930f514SMatthew G. Knepley     if (dm->sfNatural) {
412ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
413d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
414d930f514SMatthew G. Knepley       if (n == nroots) {
415d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
416d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
417d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
418d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
419d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
420d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
421d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
422d930f514SMatthew G. Knepley   } else {
423d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
424d930f514SMatthew G. Knepley     v = originalv;
425d930f514SMatthew G. Knepley   }
426d930f514SMatthew G. Knepley   if (ishdf5) {
427d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
42839d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
429d930f514SMatthew G. Knepley #else
430d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
431d930f514SMatthew G. Knepley #endif
432d930f514SMatthew G. Knepley   } else if (isvtk) {
433d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
434d930f514SMatthew G. Knepley   } else {
435d930f514SMatthew G. Knepley     PetscBool isseq;
436d930f514SMatthew G. Knepley 
437d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
438d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
439d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
440d930f514SMatthew G. Knepley   }
441d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
442d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
443d930f514SMatthew G. Knepley }
444d930f514SMatthew G. Knepley 
4452c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4462c40f234SMatthew G. Knepley {
4472c40f234SMatthew G. Knepley   DM             dm;
4482c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4492c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4502c40f234SMatthew G. Knepley 
4512c40f234SMatthew G. Knepley   PetscFunctionBegin;
4522c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4532c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4542c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4552c40f234SMatthew G. Knepley   if (ishdf5) {
4562c40f234SMatthew G. Knepley     DM          dmBC;
4572c40f234SMatthew G. Knepley     Vec         gv;
4582c40f234SMatthew G. Knepley     const char *name;
4592c40f234SMatthew G. Knepley 
4602c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4612c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4622c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4632c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4652c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4662c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4672c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4682c40f234SMatthew G. Knepley   } else {
4692c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4702c40f234SMatthew G. Knepley   }
4712c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4722c40f234SMatthew G. Knepley }
4732c40f234SMatthew G. Knepley 
4742c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4752c40f234SMatthew G. Knepley {
4762c40f234SMatthew G. Knepley   DM             dm;
4772c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4782c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4792c40f234SMatthew G. Knepley 
4802c40f234SMatthew G. Knepley   PetscFunctionBegin;
4812c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4822c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4832c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4842c40f234SMatthew G. Knepley   if (ishdf5) {
485878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
48639d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
487b136c2c9SMatthew G. Knepley #else
488b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
489878b459fSMatthew G. Knepley #endif
4902c40f234SMatthew G. Knepley   } else {
4912c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
492552f7358SJed Brown   }
493552f7358SJed Brown   PetscFunctionReturn(0);
494552f7358SJed Brown }
495552f7358SJed Brown 
496d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
497d930f514SMatthew G. Knepley {
498d930f514SMatthew G. Knepley   DM                dm;
499d930f514SMatthew G. Knepley   PetscViewerFormat format;
500d930f514SMatthew G. Knepley   PetscBool         ishdf5;
501d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
502d930f514SMatthew G. Knepley 
503d930f514SMatthew G. Knepley   PetscFunctionBegin;
504d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
505d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
506d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
507d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
508d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
509d930f514SMatthew G. Knepley     if (dm->sfNatural) {
510d930f514SMatthew G. Knepley       if (ishdf5) {
511d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
512d930f514SMatthew G. Knepley         Vec         v;
513d930f514SMatthew G. Knepley         const char *vecname;
514d930f514SMatthew G. Knepley 
515d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
516d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
517d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
51839d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
519d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
520d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
521d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
522d930f514SMatthew G. Knepley #else
523d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
524d930f514SMatthew G. Knepley #endif
525d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
526d930f514SMatthew G. Knepley     }
527d930f514SMatthew G. Knepley   }
528d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
529d930f514SMatthew G. Knepley }
530d930f514SMatthew G. Knepley 
5317cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
532731e8ddeSMatthew G. Knepley {
533731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
534731e8ddeSMatthew G. Knepley   Vec                coordinates;
535731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
536731e8ddeSMatthew G. Knepley   const char        *name[4];
537731e8ddeSMatthew G. Knepley   const PetscScalar *a;
538731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
539731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
540731e8ddeSMatthew G. Knepley 
541731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
542731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
543731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
544731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
545731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
546731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
547731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
548731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
549731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
550731e8ddeSMatthew G. Knepley   name[1]     = "edge";
551731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
552731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
553731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
554731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
555731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
556731e8ddeSMatthew G. Knepley 
557f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
559731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
560731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
561731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
562731e8ddeSMatthew G. Knepley 
563731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
564731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
565731e8ddeSMatthew G. Knepley       if (!dof) continue;
566731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
567731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
568f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
569731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
570731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
571731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
572731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
57341477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley         }
575731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
576731e8ddeSMatthew G. Knepley       }
577731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
578731e8ddeSMatthew G. Knepley     }
579731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
580731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
581731e8ddeSMatthew G. Knepley   }
582731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
583731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
584731e8ddeSMatthew G. Knepley }
585731e8ddeSMatthew G. Knepley 
5867cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
587552f7358SJed Brown {
588552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
589552f7358SJed Brown   DM                cdm;
590552f7358SJed Brown   DMLabel           markers;
591552f7358SJed Brown   PetscSection      coordSection;
592552f7358SJed Brown   Vec               coordinates;
593552f7358SJed Brown   PetscViewerFormat format;
594552f7358SJed Brown   PetscErrorCode    ierr;
595552f7358SJed Brown 
596552f7358SJed Brown   PetscFunctionBegin;
597552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
598552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
599552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
600552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
601552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
602552f7358SJed Brown     const char *name;
603f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
604552f7358SJed Brown     PetscInt    pStart, pEnd, p;
605552f7358SJed Brown     PetscMPIInt rank, size;
606552f7358SJed Brown 
60782f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
60882f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
609552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
610552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
611552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
612f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
613f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
614f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
615f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
616f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
617552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
618552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
6194d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
6204d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
621552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
622552f7358SJed Brown       PetscInt dof, off, s;
623552f7358SJed Brown 
624552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
625552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
626552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
627e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
628552f7358SJed Brown       }
629552f7358SJed Brown     }
630552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
631552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
632552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
633552f7358SJed Brown       PetscInt dof, off, c;
634552f7358SJed Brown 
635552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
636552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
637552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
638e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
639552f7358SJed Brown       }
640552f7358SJed Brown     }
641552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6424d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6430298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
644552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
645c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
646552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
647552f7358SJed Brown     if (size > 1) {
648552f7358SJed Brown       PetscSF sf;
649552f7358SJed Brown 
650552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
651552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
652552f7358SJed Brown     }
653552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
654552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6550588280cSMatthew G. Knepley     const char  *name, *color;
6560588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6570588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
658552f7358SJed Brown     PetscReal    scale         = 2.0;
6590588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6600588280cSMatthew G. Knepley     double       tcoords[3];
661552f7358SJed Brown     PetscScalar *coords;
6620588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
663552f7358SJed Brown     PetscMPIInt  rank, size;
6640588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
665552f7358SJed Brown 
6660588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
667552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
668c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6690588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6700588280cSMatthew G. Knepley     numColors  = 10;
6710588280cSMatthew G. Knepley     numLColors = 10;
6720588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
673c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
674c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
675c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6760588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
677c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6780588280cSMatthew G. Knepley     if (!useColors) {
6790588280cSMatthew G. Knepley       numColors = 3;
6800588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6810588280cSMatthew G. Knepley     }
682c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6830588280cSMatthew G. Knepley     if (!useColors) {
6840588280cSMatthew G. Knepley       numLColors = 4;
6850588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
6860588280cSMatthew G. Knepley     }
68782f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
68882f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
689552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
690770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
6910588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
692552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
693552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
694552f7358SJed Brown \\usetikzlibrary{arrows}\n\
6950588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
6960588280cSMatthew G. Knepley     if (size > 1) {
697f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
698770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
699770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
700770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
701770b213bSMatthew G Knepley         } else if (p > 0) {
702770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
703770b213bSMatthew G Knepley         }
704770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
705770b213bSMatthew G Knepley       }
7060588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7070588280cSMatthew G. Knepley     }
7080588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
709552f7358SJed Brown     /* Plot vertices */
710552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
711552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7124d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
713552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
714552f7358SJed Brown       PetscInt  off, dof, d;
7150588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
716552f7358SJed Brown 
717552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
718552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7190588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
720f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7210588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7220588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
723c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7240588280cSMatthew G. Knepley       }
7250588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7260588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
727552f7358SJed Brown       for (d = 0; d < dof; ++d) {
728552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7290588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
730552f7358SJed Brown       }
7310588280cSMatthew G. Knepley       color = colors[rank%numColors];
7320588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7330588280cSMatthew G. Knepley         PetscInt val;
734c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7350588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7360588280cSMatthew G. Knepley       }
7370588280cSMatthew G. Knepley       if (useNumbers) {
738e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7390588280cSMatthew G. Knepley       } else {
740e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7410588280cSMatthew G. Knepley       }
742552f7358SJed Brown     }
743552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
744552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
745552f7358SJed Brown     /* Plot edges */
7460588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
7470588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
748552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
749552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
750552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
751552f7358SJed Brown         const PetscInt *cone;
752552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
753552f7358SJed Brown 
754552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
755f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
756552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
757552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
758552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
759552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
760552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
761552f7358SJed Brown         for (d = 0; d < dof; ++d) {
7620588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
763c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
764552f7358SJed Brown         }
7650588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
7660588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
7670588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
7680588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
769f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
7700588280cSMatthew G. Knepley         }
7710588280cSMatthew G. Knepley         color = colors[rank%numColors];
7720588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7730588280cSMatthew G. Knepley           PetscInt val;
774c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7750750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7760588280cSMatthew G. Knepley         }
777e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
778552f7358SJed Brown       }
779552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
780552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
781552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
7820588280cSMatthew G. Knepley     }
783552f7358SJed Brown     /* Plot cells */
7840588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
7850588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
7860588280cSMatthew G. Knepley         const PetscInt *cone;
7870588280cSMatthew G. Knepley 
7880588280cSMatthew G. Knepley         color = colors[rank%numColors];
7890588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7900588280cSMatthew G. Knepley           PetscInt val;
791c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
7920588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7930588280cSMatthew G. Knepley         }
7940588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
795e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
7960588280cSMatthew G. Knepley       }
7970588280cSMatthew G. Knepley     } else {
798552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
799552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
8000298fd71SBarry Smith         PetscInt *closure = NULL;
801552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
802552f7358SJed Brown 
803552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
804552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
805552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
806552f7358SJed Brown           const PetscInt point = closure[p];
807552f7358SJed Brown 
808552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
809552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
810e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
811552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
812552f7358SJed Brown         }
813552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
814e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
815552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
816552f7358SJed Brown       }
8170588280cSMatthew G. Knepley     }
818552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8194d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8200588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
821770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
8220588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
8230588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
8240588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
8250588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
826552f7358SJed Brown   } else {
82782f516ccSBarry Smith     MPI_Comm    comm;
828834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
829f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
830552f7358SJed Brown     PetscInt    pStart, pEnd, p;
831a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
8321143a3c0SMatthew G. Knepley     const char *name;
833552f7358SJed Brown     PetscMPIInt size;
834552f7358SJed Brown 
83582f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
836552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
837c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
838f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8395f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
840f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
841f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
842f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
843552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
844b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
845bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
846dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
847552f7358SJed Brown     if (depth == 1) {
848552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
849552f7358SJed Brown       pEnd = pEnd - pStart;
850552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
851f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
852552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
853552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
854552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
855552f7358SJed Brown       pEnd = pEnd - pStart;
856552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
857552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
858552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
859552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
860552f7358SJed Brown     } else {
861cbb7f117SMark Adams       PetscMPIInt rank;
862cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
863552f7358SJed Brown       for (d = 0; d <= dim; d++) {
864552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
865834065abSMatthew G. Knepley         pEnd    -= pStart;
866834065abSMatthew G. Knepley         pMax[d] -= pStart;
867552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
868834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
869552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
870834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
871cbb7f117SMark Adams           if (!rank) {
872834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
873834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
874834065abSMatthew G. Knepley           }
875cbb7f117SMark Adams         }
876552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
877552f7358SJed Brown       }
878552f7358SJed Brown     }
879834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
880c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
881a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
882a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
883a57dd577SMatthew G Knepley       DMLabel         label;
884a57dd577SMatthew G Knepley       const char     *name;
885a57dd577SMatthew G Knepley       IS              valueIS;
886a57dd577SMatthew G Knepley       const PetscInt *values;
887a57dd577SMatthew G Knepley       PetscInt        numValues, v;
888a57dd577SMatthew G Knepley 
889c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
890c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
891a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
892d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
893a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
894a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
895120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
896a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
897a57dd577SMatthew G Knepley         PetscInt size;
898a57dd577SMatthew G Knepley 
899a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
900a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
901d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
902a57dd577SMatthew G Knepley       }
903a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
904120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
905a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
9064d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
907a57dd577SMatthew G Knepley     }
908a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
9098e7ff633SMatthew G. Knepley     if (cdm) {
9108e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
9118e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
9128e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
9138e7ff633SMatthew G. Knepley     }
914552f7358SJed Brown   }
915552f7358SJed Brown   PetscFunctionReturn(0);
916552f7358SJed Brown }
917552f7358SJed Brown 
9187cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
919e412dcbdSMatthew G. Knepley {
920e412dcbdSMatthew G. Knepley   PetscDraw          draw;
921e412dcbdSMatthew G. Knepley   DM                 cdm;
922e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
923e412dcbdSMatthew G. Knepley   Vec                coordinates;
924e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
92529494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
926e412dcbdSMatthew G. Knepley   PetscBool          isnull;
927e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
928e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
929e412dcbdSMatthew G. Knepley 
930e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
931e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
932e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
933e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
934e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
935e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
936e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
937e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
938e412dcbdSMatthew G. Knepley 
939e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
940e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
941e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
942e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
943e412dcbdSMatthew G. Knepley 
944e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
945e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
946e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
9470c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
9480c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
949e412dcbdSMatthew G. Knepley   }
950e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
95129494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
95229494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
95329494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
954e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
955e412dcbdSMatthew G. Knepley 
956e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
957e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
95829494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
959e412dcbdSMatthew G. Knepley 
96029494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
961e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
96229494db1SLisandro Dalcin     switch (coneSize) {
96329494db1SLisandro Dalcin     case 3:
9640c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9650c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9660c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
967e412dcbdSMatthew G. Knepley       break;
96829494db1SLisandro Dalcin     case 4:
9690c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9700c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9710c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9720c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
973e412dcbdSMatthew G. Knepley       break;
97429494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
975e412dcbdSMatthew G. Knepley     }
976e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
977e412dcbdSMatthew G. Knepley   }
978e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
979e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
980e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
981e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
982e412dcbdSMatthew G. Knepley }
983e412dcbdSMatthew G. Knepley 
984552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
985552f7358SJed Brown {
986e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
987552f7358SJed Brown   PetscErrorCode    ierr;
988552f7358SJed Brown 
989552f7358SJed Brown   PetscFunctionBegin;
990552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
991552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
992552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
993fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
994c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
995e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
9968135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
997552f7358SJed Brown   if (iascii) {
9988135c375SStefano Zampini     PetscViewerFormat format;
9998135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
10008135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
10018135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
10028135c375SStefano Zampini     } else {
1003552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
10048135c375SStefano Zampini     }
1005c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1006c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
10077afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
100839d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
10097afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
1010c6ccd67eSMatthew G. Knepley #else
1011c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1012552f7358SJed Brown #endif
1013e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1014fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1015e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1016e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
10178135c375SStefano Zampini   } else if (isglvis) {
10188135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
1019fcf6c8fdSToby Isaac   }
1020cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1021cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1022cb3ba0daSMatthew G. Knepley   if (flg) {
1023cb3ba0daSMatthew G. Knepley     Vec ranks;
1024cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1025cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1026cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1027cb3ba0daSMatthew G. Knepley   }
1028552f7358SJed Brown   PetscFunctionReturn(0);
1029552f7358SJed Brown }
1030552f7358SJed Brown 
10312c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
10322c40f234SMatthew G. Knepley {
10332c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
10342c40f234SMatthew G. Knepley   PetscErrorCode ierr;
10352c40f234SMatthew G. Knepley 
10362c40f234SMatthew G. Knepley   PetscFunctionBegin;
10372c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10382c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
10392c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
10402c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
10412c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
10422c40f234SMatthew G. Knepley   else if (ishdf5) {
10432c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
104439d25373SMatthew G. Knepley     ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
10452c40f234SMatthew G. Knepley #else
10462c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1047552f7358SJed Brown #endif
1048552f7358SJed Brown   }
1049552f7358SJed Brown   PetscFunctionReturn(0);
1050552f7358SJed Brown }
1051552f7358SJed Brown 
1052552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1053552f7358SJed Brown {
1054552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1055552f7358SJed Brown   PetscErrorCode ierr;
1056552f7358SJed Brown 
1057552f7358SJed Brown   PetscFunctionBegin;
10588135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
10598135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
10600d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1061552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1062552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1063552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1064552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1065be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1066552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1067552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1068d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1069d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
107077623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1071a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1072552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1073552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1074a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1075a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1076d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1077d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1078d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1079d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1080d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1081d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1082fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1083552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1084552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1085552f7358SJed Brown   PetscFunctionReturn(0);
1086552f7358SJed Brown }
1087552f7358SJed Brown 
1088b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1089552f7358SJed Brown {
10908d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1091acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1092552f7358SJed Brown   PetscInt               localSize;
1093837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1094552f7358SJed Brown   PetscErrorCode         ierr;
1095b412c318SBarry Smith   MatType                mtype;
10961428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1097552f7358SJed Brown 
1098552f7358SJed Brown   PetscFunctionBegin;
1099607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1100b412c318SBarry Smith   mtype = dm->mattype;
1101552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1102552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1103552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
110482f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1105552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1106552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1107552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1108acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1109acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1110552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1111552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1112552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1113552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1114552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1115552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1116552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1117837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1118552f7358SJed Brown   if (!isShell) {
1119be36d101SStefano Zampini     PetscSection subSection;
1120837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
11210be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1122fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1123552f7358SJed Brown 
112400140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1125be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1126be36d101SStefano Zampini       PetscSection section;
1127be36d101SStefano Zampini       PetscInt     size;
1128be36d101SStefano Zampini 
1129be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1130be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1131be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1132be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1133be36d101SStefano Zampini     } else {
113400140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1135be36d101SStefano Zampini     }
1136552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1137be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1138a9d99c84SMatthew G. Knepley       PetscInt bdof;
1139a9d99c84SMatthew G. Knepley 
1140552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1141fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
11421d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
11431d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
11441d17a0a3SMatthew G. Knepley       if (dof) {
11451d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1146be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1147be36d101SStefano Zampini       }
1148be36d101SStefano Zampini       if (isMatIS) {
1149be36d101SStefano Zampini         PetscInt loff,c,off;
1150be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1151be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1152be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1153552f7358SJed Brown       }
11542a28c762SMatthew G Knepley     }
11552a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
11560be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11570be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11580be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11590be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
11604fa26246SMatthew G. Knepley     bs = bs < 0 ? 1 : bs;
1161be36d101SStefano Zampini     if (isMatIS) {
11624fa26246SMatthew G. Knepley       PetscInt l;
11634fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
11644fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
11654fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1166be36d101SStefano Zampini     }
1167be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1168be36d101SStefano Zampini     if (isMatIS) {
1169be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1170be36d101SStefano Zampini     }
11711795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
11728d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1173552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1174552f7358SJed Brown   }
1175b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1176552f7358SJed Brown   PetscFunctionReturn(0);
1177552f7358SJed Brown }
1178552f7358SJed Brown 
11797cd05799SMatthew G. Knepley /*@
1180a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1181be36d101SStefano Zampini 
1182be36d101SStefano Zampini   Not collective
1183be36d101SStefano Zampini 
1184be36d101SStefano Zampini   Input Parameter:
1185be36d101SStefano Zampini . mesh - The DMPlex
1186be36d101SStefano Zampini 
1187be36d101SStefano Zampini   Output Parameters:
1188be36d101SStefano Zampini . subsection - The subdomain section
1189be36d101SStefano Zampini 
1190be36d101SStefano Zampini   Level: developer
1191be36d101SStefano Zampini 
1192be36d101SStefano Zampini .seealso:
11937cd05799SMatthew G. Knepley @*/
1194be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1195be36d101SStefano Zampini {
1196be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1197be36d101SStefano Zampini   PetscErrorCode ierr;
1198be36d101SStefano Zampini 
1199be36d101SStefano Zampini   PetscFunctionBegin;
1200be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1201be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1202be36d101SStefano Zampini     PetscSection section;
1203be36d101SStefano Zampini     PetscSF      sf;
1204be36d101SStefano Zampini 
1205be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1206be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1207be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1208be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1209be36d101SStefano Zampini   }
1210be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1211be36d101SStefano Zampini   PetscFunctionReturn(0);
1212be36d101SStefano Zampini }
1213be36d101SStefano Zampini 
1214552f7358SJed Brown /*@
1215552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1216552f7358SJed Brown 
1217552f7358SJed Brown   Not collective
1218552f7358SJed Brown 
1219552f7358SJed Brown   Input Parameter:
1220552f7358SJed Brown . mesh - The DMPlex
1221552f7358SJed Brown 
1222552f7358SJed Brown   Output Parameters:
1223552f7358SJed Brown + pStart - The first mesh point
1224552f7358SJed Brown - pEnd   - The upper bound for mesh points
1225552f7358SJed Brown 
1226552f7358SJed Brown   Level: beginner
1227552f7358SJed Brown 
1228552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1229552f7358SJed Brown @*/
1230552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1231552f7358SJed Brown {
1232552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1233552f7358SJed Brown   PetscErrorCode ierr;
1234552f7358SJed Brown 
1235552f7358SJed Brown   PetscFunctionBegin;
1236552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1237552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1238552f7358SJed Brown   PetscFunctionReturn(0);
1239552f7358SJed Brown }
1240552f7358SJed Brown 
1241552f7358SJed Brown /*@
1242552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1243552f7358SJed Brown 
1244552f7358SJed Brown   Not collective
1245552f7358SJed Brown 
1246552f7358SJed Brown   Input Parameters:
1247552f7358SJed Brown + mesh - The DMPlex
1248552f7358SJed Brown . pStart - The first mesh point
1249552f7358SJed Brown - pEnd   - The upper bound for mesh points
1250552f7358SJed Brown 
1251552f7358SJed Brown   Output Parameters:
1252552f7358SJed Brown 
1253552f7358SJed Brown   Level: beginner
1254552f7358SJed Brown 
1255552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1256552f7358SJed Brown @*/
1257552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1258552f7358SJed Brown {
1259552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1260552f7358SJed Brown   PetscErrorCode ierr;
1261552f7358SJed Brown 
1262552f7358SJed Brown   PetscFunctionBegin;
1263552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1264552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1265552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1266552f7358SJed Brown   PetscFunctionReturn(0);
1267552f7358SJed Brown }
1268552f7358SJed Brown 
1269552f7358SJed Brown /*@
1270eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1271552f7358SJed Brown 
1272552f7358SJed Brown   Not collective
1273552f7358SJed Brown 
1274552f7358SJed Brown   Input Parameters:
1275552f7358SJed Brown + mesh - The DMPlex
1276eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1277552f7358SJed Brown 
1278552f7358SJed Brown   Output Parameter:
1279552f7358SJed Brown . size - The cone size for point p
1280552f7358SJed Brown 
1281552f7358SJed Brown   Level: beginner
1282552f7358SJed Brown 
1283552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1284552f7358SJed Brown @*/
1285552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1286552f7358SJed Brown {
1287552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1288552f7358SJed Brown   PetscErrorCode ierr;
1289552f7358SJed Brown 
1290552f7358SJed Brown   PetscFunctionBegin;
1291552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1292552f7358SJed Brown   PetscValidPointer(size, 3);
1293552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1294552f7358SJed Brown   PetscFunctionReturn(0);
1295552f7358SJed Brown }
1296552f7358SJed Brown 
1297552f7358SJed Brown /*@
1298eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1299552f7358SJed Brown 
1300552f7358SJed Brown   Not collective
1301552f7358SJed Brown 
1302552f7358SJed Brown   Input Parameters:
1303552f7358SJed Brown + mesh - The DMPlex
1304eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1305552f7358SJed Brown - size - The cone size for point p
1306552f7358SJed Brown 
1307552f7358SJed Brown   Output Parameter:
1308552f7358SJed Brown 
1309552f7358SJed Brown   Note:
1310552f7358SJed Brown   This should be called after DMPlexSetChart().
1311552f7358SJed Brown 
1312552f7358SJed Brown   Level: beginner
1313552f7358SJed Brown 
1314552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1315552f7358SJed Brown @*/
1316552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1317552f7358SJed Brown {
1318552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1319552f7358SJed Brown   PetscErrorCode ierr;
1320552f7358SJed Brown 
1321552f7358SJed Brown   PetscFunctionBegin;
1322552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1323552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
13240d644c17SKarl Rupp 
1325552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1326552f7358SJed Brown   PetscFunctionReturn(0);
1327552f7358SJed Brown }
1328552f7358SJed Brown 
1329f5a469b9SMatthew G. Knepley /*@
1330eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1331f5a469b9SMatthew G. Knepley 
1332f5a469b9SMatthew G. Knepley   Not collective
1333f5a469b9SMatthew G. Knepley 
1334f5a469b9SMatthew G. Knepley   Input Parameters:
1335f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1336eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1337f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1338f5a469b9SMatthew G. Knepley 
1339f5a469b9SMatthew G. Knepley   Output Parameter:
1340f5a469b9SMatthew G. Knepley 
1341f5a469b9SMatthew G. Knepley   Note:
1342f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1343f5a469b9SMatthew G. Knepley 
1344f5a469b9SMatthew G. Knepley   Level: beginner
1345f5a469b9SMatthew G. Knepley 
1346f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1347f5a469b9SMatthew G. Knepley @*/
1348f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1349f5a469b9SMatthew G. Knepley {
1350f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1351f5a469b9SMatthew G. Knepley   PetscInt       csize;
1352f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1353f5a469b9SMatthew G. Knepley 
1354f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1355f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1356f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1357f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1358f5a469b9SMatthew G. Knepley 
1359f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1360f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1361f5a469b9SMatthew G. Knepley }
1362f5a469b9SMatthew G. Knepley 
1363552f7358SJed Brown /*@C
1364eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1365552f7358SJed Brown 
1366552f7358SJed Brown   Not collective
1367552f7358SJed Brown 
1368552f7358SJed Brown   Input Parameters:
1369552f7358SJed Brown + mesh - The DMPlex
1370eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1371552f7358SJed Brown 
1372552f7358SJed Brown   Output Parameter:
1373552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1374552f7358SJed Brown 
1375552f7358SJed Brown   Level: beginner
1376552f7358SJed Brown 
13773813dfbdSMatthew G Knepley   Fortran Notes:
13783813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13793813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13803813dfbdSMatthew G Knepley 
13813813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1382552f7358SJed Brown 
1383552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1384552f7358SJed Brown @*/
1385552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1386552f7358SJed Brown {
1387552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1388552f7358SJed Brown   PetscInt       off;
1389552f7358SJed Brown   PetscErrorCode ierr;
1390552f7358SJed Brown 
1391552f7358SJed Brown   PetscFunctionBegin;
1392552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1393552f7358SJed Brown   PetscValidPointer(cone, 3);
1394552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1395552f7358SJed Brown   *cone = &mesh->cones[off];
1396552f7358SJed Brown   PetscFunctionReturn(0);
1397552f7358SJed Brown }
1398552f7358SJed Brown 
1399552f7358SJed Brown /*@
1400eaf898f9SPatrick Sanan   DMPlexSetCone - Set the points on the in-edges for this point in the DAG
1401552f7358SJed Brown 
1402552f7358SJed Brown   Not collective
1403552f7358SJed Brown 
1404552f7358SJed Brown   Input Parameters:
1405552f7358SJed Brown + mesh - The DMPlex
1406eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1407552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1408552f7358SJed Brown 
1409552f7358SJed Brown   Output Parameter:
1410552f7358SJed Brown 
1411552f7358SJed Brown   Note:
1412552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1413552f7358SJed Brown 
1414552f7358SJed Brown   Level: beginner
1415552f7358SJed Brown 
1416552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1417552f7358SJed Brown @*/
1418552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1419552f7358SJed Brown {
1420552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1421552f7358SJed Brown   PetscInt       pStart, pEnd;
1422552f7358SJed Brown   PetscInt       dof, off, c;
1423552f7358SJed Brown   PetscErrorCode ierr;
1424552f7358SJed Brown 
1425552f7358SJed Brown   PetscFunctionBegin;
1426552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1427552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1428552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1429552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1430552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
143182f516ccSBarry 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);
1432552f7358SJed Brown   for (c = 0; c < dof; ++c) {
143382f516ccSBarry 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);
1434552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1435552f7358SJed Brown   }
1436552f7358SJed Brown   PetscFunctionReturn(0);
1437552f7358SJed Brown }
1438552f7358SJed Brown 
1439552f7358SJed Brown /*@C
1440eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1441552f7358SJed Brown 
1442552f7358SJed Brown   Not collective
1443552f7358SJed Brown 
1444552f7358SJed Brown   Input Parameters:
1445552f7358SJed Brown + mesh - The DMPlex
1446eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1447552f7358SJed Brown 
1448552f7358SJed Brown   Output Parameter:
1449552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1450552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1451552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1452552f7358SJed Brown                     the index of the cone point on which to start.
1453552f7358SJed Brown 
1454552f7358SJed Brown   Level: beginner
1455552f7358SJed Brown 
14563813dfbdSMatthew G Knepley   Fortran Notes:
14573813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14583813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14593813dfbdSMatthew G Knepley 
14603813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1461552f7358SJed Brown 
1462552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1463552f7358SJed Brown @*/
1464552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1465552f7358SJed Brown {
1466552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1467552f7358SJed Brown   PetscInt       off;
1468552f7358SJed Brown   PetscErrorCode ierr;
1469552f7358SJed Brown 
1470552f7358SJed Brown   PetscFunctionBegin;
1471552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1472552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1473552f7358SJed Brown   {
1474552f7358SJed Brown     PetscInt dof;
1475552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1476552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1477552f7358SJed Brown   }
1478552f7358SJed Brown #endif
1479552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
14800d644c17SKarl Rupp 
1481552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1482552f7358SJed Brown   PetscFunctionReturn(0);
1483552f7358SJed Brown }
1484552f7358SJed Brown 
1485552f7358SJed Brown /*@
1486eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1487552f7358SJed Brown 
1488552f7358SJed Brown   Not collective
1489552f7358SJed Brown 
1490552f7358SJed Brown   Input Parameters:
1491552f7358SJed Brown + mesh - The DMPlex
1492eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1493552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1494552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1495552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1496552f7358SJed Brown                     the index of the cone point on which to start.
1497552f7358SJed Brown 
1498552f7358SJed Brown   Output Parameter:
1499552f7358SJed Brown 
1500552f7358SJed Brown   Note:
1501552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1502552f7358SJed Brown 
1503552f7358SJed Brown   Level: beginner
1504552f7358SJed Brown 
1505552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1506552f7358SJed Brown @*/
1507552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1508552f7358SJed Brown {
1509552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1510552f7358SJed Brown   PetscInt       pStart, pEnd;
1511552f7358SJed Brown   PetscInt       dof, off, c;
1512552f7358SJed Brown   PetscErrorCode ierr;
1513552f7358SJed Brown 
1514552f7358SJed Brown   PetscFunctionBegin;
1515552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1516552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1517552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1518552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1519552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
152082f516ccSBarry 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);
1521552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1522552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1523552f7358SJed Brown 
1524552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
152582f516ccSBarry 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);
1526552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1527552f7358SJed Brown   }
1528552f7358SJed Brown   PetscFunctionReturn(0);
1529552f7358SJed Brown }
1530552f7358SJed Brown 
15317cd05799SMatthew G. Knepley /*@
1532eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
15337cd05799SMatthew G. Knepley 
15347cd05799SMatthew G. Knepley   Not collective
15357cd05799SMatthew G. Knepley 
15367cd05799SMatthew G. Knepley   Input Parameters:
15377cd05799SMatthew G. Knepley + mesh - The DMPlex
1538eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15397cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15407cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
15417cd05799SMatthew G. Knepley 
15427cd05799SMatthew G. Knepley   Level: beginner
15437cd05799SMatthew G. Knepley 
15447cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15457cd05799SMatthew G. Knepley @*/
1546552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1547552f7358SJed Brown {
1548552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1549552f7358SJed Brown   PetscInt       pStart, pEnd;
1550552f7358SJed Brown   PetscInt       dof, off;
1551552f7358SJed Brown   PetscErrorCode ierr;
1552552f7358SJed Brown 
1553552f7358SJed Brown   PetscFunctionBegin;
1554552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1555552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
155682f516ccSBarry 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);
155782f516ccSBarry 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);
155877c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
155977c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
156077c88f5bSMatthew 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);
1561552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1562552f7358SJed Brown   PetscFunctionReturn(0);
1563552f7358SJed Brown }
1564552f7358SJed Brown 
15657cd05799SMatthew G. Knepley /*@
1566eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
15677cd05799SMatthew G. Knepley 
15687cd05799SMatthew G. Knepley   Not collective
15697cd05799SMatthew G. Knepley 
15707cd05799SMatthew G. Knepley   Input Parameters:
15717cd05799SMatthew G. Knepley + mesh - The DMPlex
1572eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15737cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15747cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
15757cd05799SMatthew G. Knepley 
15767cd05799SMatthew G. Knepley   Level: beginner
15777cd05799SMatthew G. Knepley 
15787cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15797cd05799SMatthew G. Knepley @*/
158077c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
158177c88f5bSMatthew G Knepley {
158277c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
158377c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
158477c88f5bSMatthew G Knepley   PetscInt       dof, off;
158577c88f5bSMatthew G Knepley   PetscErrorCode ierr;
158677c88f5bSMatthew G Knepley 
158777c88f5bSMatthew G Knepley   PetscFunctionBegin;
158877c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
158977c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
159077c88f5bSMatthew 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);
159177c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
159277c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
159377c88f5bSMatthew 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);
159477c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
159577c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
159677c88f5bSMatthew G Knepley }
159777c88f5bSMatthew G Knepley 
1598552f7358SJed Brown /*@
1599eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
1600552f7358SJed Brown 
1601552f7358SJed Brown   Not collective
1602552f7358SJed Brown 
1603552f7358SJed Brown   Input Parameters:
1604552f7358SJed Brown + mesh - The DMPlex
1605eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1606552f7358SJed Brown 
1607552f7358SJed Brown   Output Parameter:
1608552f7358SJed Brown . size - The support size for point p
1609552f7358SJed Brown 
1610552f7358SJed Brown   Level: beginner
1611552f7358SJed Brown 
1612552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1613552f7358SJed Brown @*/
1614552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1615552f7358SJed Brown {
1616552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1617552f7358SJed Brown   PetscErrorCode ierr;
1618552f7358SJed Brown 
1619552f7358SJed Brown   PetscFunctionBegin;
1620552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1621552f7358SJed Brown   PetscValidPointer(size, 3);
1622552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1623552f7358SJed Brown   PetscFunctionReturn(0);
1624552f7358SJed Brown }
1625552f7358SJed Brown 
1626552f7358SJed Brown /*@
1627eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
1628552f7358SJed Brown 
1629552f7358SJed Brown   Not collective
1630552f7358SJed Brown 
1631552f7358SJed Brown   Input Parameters:
1632552f7358SJed Brown + mesh - The DMPlex
1633eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1634552f7358SJed Brown - size - The support size for point p
1635552f7358SJed Brown 
1636552f7358SJed Brown   Output Parameter:
1637552f7358SJed Brown 
1638552f7358SJed Brown   Note:
1639552f7358SJed Brown   This should be called after DMPlexSetChart().
1640552f7358SJed Brown 
1641552f7358SJed Brown   Level: beginner
1642552f7358SJed Brown 
1643552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1644552f7358SJed Brown @*/
1645552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1646552f7358SJed Brown {
1647552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1648552f7358SJed Brown   PetscErrorCode ierr;
1649552f7358SJed Brown 
1650552f7358SJed Brown   PetscFunctionBegin;
1651552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1652552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
16530d644c17SKarl Rupp 
1654552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1655552f7358SJed Brown   PetscFunctionReturn(0);
1656552f7358SJed Brown }
1657552f7358SJed Brown 
1658552f7358SJed Brown /*@C
1659eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
1660552f7358SJed Brown 
1661552f7358SJed Brown   Not collective
1662552f7358SJed Brown 
1663552f7358SJed Brown   Input Parameters:
1664552f7358SJed Brown + mesh - The DMPlex
1665eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1666552f7358SJed Brown 
1667552f7358SJed Brown   Output Parameter:
1668552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1669552f7358SJed Brown 
1670552f7358SJed Brown   Level: beginner
1671552f7358SJed Brown 
16723813dfbdSMatthew G Knepley   Fortran Notes:
16733813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16743813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16753813dfbdSMatthew G Knepley 
16763813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
16773813dfbdSMatthew G Knepley 
1678552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1679552f7358SJed Brown @*/
1680552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1681552f7358SJed Brown {
1682552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1683552f7358SJed Brown   PetscInt       off;
1684552f7358SJed Brown   PetscErrorCode ierr;
1685552f7358SJed Brown 
1686552f7358SJed Brown   PetscFunctionBegin;
1687552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1688552f7358SJed Brown   PetscValidPointer(support, 3);
1689552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1690552f7358SJed Brown   *support = &mesh->supports[off];
1691552f7358SJed Brown   PetscFunctionReturn(0);
1692552f7358SJed Brown }
1693552f7358SJed Brown 
1694552f7358SJed Brown /*@
1695eaf898f9SPatrick Sanan   DMPlexSetSupport - Set the points on the out-edges for this point in the DAG
1696552f7358SJed Brown 
1697552f7358SJed Brown   Not collective
1698552f7358SJed Brown 
1699552f7358SJed Brown   Input Parameters:
1700552f7358SJed Brown + mesh - The DMPlex
1701eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1702552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1703552f7358SJed Brown 
1704552f7358SJed Brown   Output Parameter:
1705552f7358SJed Brown 
1706552f7358SJed Brown   Note:
1707552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1708552f7358SJed Brown 
1709552f7358SJed Brown   Level: beginner
1710552f7358SJed Brown 
1711552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1712552f7358SJed Brown @*/
1713552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1714552f7358SJed Brown {
1715552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1716552f7358SJed Brown   PetscInt       pStart, pEnd;
1717552f7358SJed Brown   PetscInt       dof, off, c;
1718552f7358SJed Brown   PetscErrorCode ierr;
1719552f7358SJed Brown 
1720552f7358SJed Brown   PetscFunctionBegin;
1721552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1722552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1723552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1724552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1725552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
172682f516ccSBarry 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);
1727552f7358SJed Brown   for (c = 0; c < dof; ++c) {
172882f516ccSBarry 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);
1729552f7358SJed Brown     mesh->supports[off+c] = support[c];
1730552f7358SJed Brown   }
1731552f7358SJed Brown   PetscFunctionReturn(0);
1732552f7358SJed Brown }
1733552f7358SJed Brown 
17347cd05799SMatthew G. Knepley /*@
1735eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
17367cd05799SMatthew G. Knepley 
17377cd05799SMatthew G. Knepley   Not collective
17387cd05799SMatthew G. Knepley 
17397cd05799SMatthew G. Knepley   Input Parameters:
17407cd05799SMatthew G. Knepley + mesh - The DMPlex
1741eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17427cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
17437cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
17447cd05799SMatthew G. Knepley 
17457cd05799SMatthew G. Knepley   Level: beginner
17467cd05799SMatthew G. Knepley 
17477cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17487cd05799SMatthew G. Knepley @*/
1749552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1750552f7358SJed Brown {
1751552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1752552f7358SJed Brown   PetscInt       pStart, pEnd;
1753552f7358SJed Brown   PetscInt       dof, off;
1754552f7358SJed Brown   PetscErrorCode ierr;
1755552f7358SJed Brown 
1756552f7358SJed Brown   PetscFunctionBegin;
1757552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1758552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1759552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1760552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
176182f516ccSBarry 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);
176282f516ccSBarry 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);
176382f516ccSBarry 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);
1764552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1765552f7358SJed Brown   PetscFunctionReturn(0);
1766552f7358SJed Brown }
1767552f7358SJed Brown 
1768552f7358SJed Brown /*@C
1769eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
1770552f7358SJed Brown 
1771552f7358SJed Brown   Not collective
1772552f7358SJed Brown 
1773552f7358SJed Brown   Input Parameters:
1774552f7358SJed Brown + mesh - The DMPlex
1775eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1776552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17770298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1778552f7358SJed Brown 
1779552f7358SJed Brown   Output Parameters:
1780552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1781552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1782552f7358SJed Brown 
1783552f7358SJed Brown   Note:
17840298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1785552f7358SJed Brown 
17863813dfbdSMatthew G Knepley   Fortran Notes:
17873813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17883813dfbdSMatthew G Knepley   include petsc.h90 in your code.
17893813dfbdSMatthew G Knepley 
17903813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17913813dfbdSMatthew G Knepley 
1792552f7358SJed Brown   Level: beginner
1793552f7358SJed Brown 
1794552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1795552f7358SJed Brown @*/
1796552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1797552f7358SJed Brown {
1798552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1799552f7358SJed Brown   PetscInt       *closure, *fifo;
18000298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1801552f7358SJed Brown   PetscInt        tmpSize, t;
1802552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1803552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1804552f7358SJed Brown   PetscErrorCode  ierr;
1805552f7358SJed Brown 
1806552f7358SJed Brown   PetscFunctionBegin;
1807552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1808552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1809552f7358SJed Brown   /* This is only 1-level */
1810552f7358SJed Brown   if (useCone) {
1811552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1812552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1813552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1814552f7358SJed Brown   } else {
1815552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1816552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1817552f7358SJed Brown   }
1818bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1819bfbcdd7aSMatthew G. Knepley     if (*points) {
1820bfbcdd7aSMatthew G. Knepley       closure = *points;
1821bfbcdd7aSMatthew G. Knepley     } else {
1822bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
182369291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1824bfbcdd7aSMatthew G. Knepley     }
1825bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1826bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1827bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1828bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1829bfbcdd7aSMatthew G. Knepley     }
1830bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1831bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1832bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1833bfbcdd7aSMatthew G. Knepley   }
183424c766afSToby Isaac   {
183524c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
183624c766afSToby Isaac 
183724c766afSToby Isaac     c = mesh->maxConeSize;
183824c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
183924c766afSToby Isaac     s = mesh->maxSupportSize;
184024c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
184124c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
184224c766afSToby Isaac   }
184369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1844bfbcdd7aSMatthew G. Knepley   if (*points) {
1845bfbcdd7aSMatthew G. Knepley     closure = *points;
1846bfbcdd7aSMatthew G. Knepley   } else {
184769291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1848bfbcdd7aSMatthew G. Knepley   }
1849bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1850552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1851552f7358SJed Brown     const PetscInt cp = tmp[t];
1852552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1853552f7358SJed Brown 
1854552f7358SJed Brown     closure[closureSize]   = cp;
1855552f7358SJed Brown     closure[closureSize+1] = co;
1856552f7358SJed Brown     fifo[fifoSize]         = cp;
1857552f7358SJed Brown     fifo[fifoSize+1]       = co;
1858552f7358SJed Brown   }
1859bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1860552f7358SJed Brown   while (fifoSize - fifoStart) {
1861552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1862552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1863552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1864552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1865552f7358SJed Brown 
1866552f7358SJed Brown     if (useCone) {
1867552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1868552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1869552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1870552f7358SJed Brown     } else {
1871552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1872552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18730298fd71SBarry Smith       tmpO = NULL;
1874552f7358SJed Brown     }
1875552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1876552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1877552f7358SJed Brown       const PetscInt cp = tmp[i];
18782e1b13c2SMatthew 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. */
18792e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18802e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18812e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1882552f7358SJed Brown       PetscInt       c;
1883552f7358SJed Brown 
18842e1b13c2SMatthew G. Knepley       if (rev) {
18852e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
18862e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18872e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18882e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18892e1b13c2SMatthew G. Knepley       }
1890552f7358SJed Brown       /* Check for duplicate */
1891552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1892552f7358SJed Brown         if (closure[c] == cp) break;
1893552f7358SJed Brown       }
1894552f7358SJed Brown       if (c == closureSize) {
1895552f7358SJed Brown         closure[closureSize]   = cp;
1896552f7358SJed Brown         closure[closureSize+1] = co;
1897552f7358SJed Brown         fifo[fifoSize]         = cp;
1898552f7358SJed Brown         fifo[fifoSize+1]       = co;
1899552f7358SJed Brown         closureSize           += 2;
1900552f7358SJed Brown         fifoSize              += 2;
1901552f7358SJed Brown       }
1902552f7358SJed Brown     }
1903552f7358SJed Brown     fifoStart += 2;
1904552f7358SJed Brown   }
1905552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1906552f7358SJed Brown   if (points)    *points    = closure;
190769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1908552f7358SJed Brown   PetscFunctionReturn(0);
1909552f7358SJed Brown }
1910552f7358SJed Brown 
19119bf0dad6SMatthew G. Knepley /*@C
1912eaf898f9SPatrick 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
19139bf0dad6SMatthew G. Knepley 
19149bf0dad6SMatthew G. Knepley   Not collective
19159bf0dad6SMatthew G. Knepley 
19169bf0dad6SMatthew G. Knepley   Input Parameters:
19179bf0dad6SMatthew G. Knepley + mesh - The DMPlex
1918eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
19199bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
19209bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
19219bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
19229bf0dad6SMatthew G. Knepley 
19239bf0dad6SMatthew G. Knepley   Output Parameters:
19249bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
19259bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
19269bf0dad6SMatthew G. Knepley 
19279bf0dad6SMatthew G. Knepley   Note:
19289bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
19299bf0dad6SMatthew G. Knepley 
19309bf0dad6SMatthew G. Knepley   Fortran Notes:
19319bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19329bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
19339bf0dad6SMatthew G. Knepley 
19349bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19359bf0dad6SMatthew G. Knepley 
19369bf0dad6SMatthew G. Knepley   Level: beginner
19379bf0dad6SMatthew G. Knepley 
19389bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
19399bf0dad6SMatthew G. Knepley @*/
19409bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
19419bf0dad6SMatthew G. Knepley {
19429bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
19439bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
19449bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
19459bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
19469bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
19479bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
19489bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
19499bf0dad6SMatthew G. Knepley 
19509bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
19519bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19529bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
19539bf0dad6SMatthew G. Knepley   /* This is only 1-level */
19549bf0dad6SMatthew G. Knepley   if (useCone) {
19559bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
19569bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
19579bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
19589bf0dad6SMatthew G. Knepley   } else {
19599bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
19609bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
19619bf0dad6SMatthew G. Knepley   }
19629bf0dad6SMatthew G. Knepley   if (depth == 1) {
19639bf0dad6SMatthew G. Knepley     if (*points) {
19649bf0dad6SMatthew G. Knepley       closure = *points;
19659bf0dad6SMatthew G. Knepley     } else {
19669bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
196769291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
19689bf0dad6SMatthew G. Knepley     }
19699bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
19709bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
19719bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19729bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
19739bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
19749bf0dad6SMatthew G. Knepley     }
19759bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
19769bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
19779bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
19789bf0dad6SMatthew G. Knepley   }
197924c766afSToby Isaac   {
198024c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
198124c766afSToby Isaac 
198224c766afSToby Isaac     c = mesh->maxConeSize;
198324c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
198424c766afSToby Isaac     s = mesh->maxSupportSize;
198524c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
198624c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
198724c766afSToby Isaac   }
198869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
19899bf0dad6SMatthew G. Knepley   if (*points) {
19909bf0dad6SMatthew G. Knepley     closure = *points;
19919bf0dad6SMatthew G. Knepley   } else {
199269291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
19939bf0dad6SMatthew G. Knepley   }
19949bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
19959bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
19969bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19979bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
19989bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
19999bf0dad6SMatthew G. Knepley 
20009bf0dad6SMatthew G. Knepley     if (ornt < 0) {
20019bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
20029bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
200386b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
20049bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
20059bf0dad6SMatthew G. Knepley     }
20069bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
20079bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
20089bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
20099bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
20109bf0dad6SMatthew G. Knepley   }
20119bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
20129bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
20139bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
20149bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
20159bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
20169bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
20179bf0dad6SMatthew G. Knepley 
20189bf0dad6SMatthew G. Knepley     if (useCone) {
20199bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
20209bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
20219bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
20229bf0dad6SMatthew G. Knepley     } else {
20239bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
20249bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
20259bf0dad6SMatthew G. Knepley       tmpO = NULL;
20269bf0dad6SMatthew G. Knepley     }
20279bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
20289bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
20299bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
20309bf0dad6SMatthew 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. */
20319bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
20329bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
20339bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
20349bf0dad6SMatthew G. Knepley       PetscInt       c;
20359bf0dad6SMatthew G. Knepley 
20369bf0dad6SMatthew G. Knepley       if (rev) {
20379bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
20389bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
20399bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
20409bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
20419bf0dad6SMatthew G. Knepley       }
20429bf0dad6SMatthew G. Knepley       /* Check for duplicate */
20439bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
20449bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
20459bf0dad6SMatthew G. Knepley       }
20469bf0dad6SMatthew G. Knepley       if (c == closureSize) {
20479bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
20489bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
20499bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
20509bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
20519bf0dad6SMatthew G. Knepley         closureSize           += 2;
20529bf0dad6SMatthew G. Knepley         fifoSize              += 2;
20539bf0dad6SMatthew G. Knepley       }
20549bf0dad6SMatthew G. Knepley     }
20559bf0dad6SMatthew G. Knepley     fifoStart += 2;
20569bf0dad6SMatthew G. Knepley   }
20579bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
20589bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
205969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
20609bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
20619bf0dad6SMatthew G. Knepley }
20629bf0dad6SMatthew G. Knepley 
2063552f7358SJed Brown /*@C
2064eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2065552f7358SJed Brown 
2066552f7358SJed Brown   Not collective
2067552f7358SJed Brown 
2068552f7358SJed Brown   Input Parameters:
2069552f7358SJed Brown + mesh - The DMPlex
2070eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2071552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2072e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2073e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2074552f7358SJed Brown 
2075552f7358SJed Brown   Note:
20760298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2077552f7358SJed Brown 
20783813dfbdSMatthew G Knepley   Fortran Notes:
20793813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20803813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20813813dfbdSMatthew G Knepley 
20823813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
20833813dfbdSMatthew G Knepley 
2084552f7358SJed Brown   Level: beginner
2085552f7358SJed Brown 
2086552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2087552f7358SJed Brown @*/
2088552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2089552f7358SJed Brown {
2090552f7358SJed Brown   PetscErrorCode ierr;
2091552f7358SJed Brown 
2092552f7358SJed Brown   PetscFunctionBegin;
2093552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2094e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2095e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
209669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
20974ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2098552f7358SJed Brown   PetscFunctionReturn(0);
2099552f7358SJed Brown }
2100552f7358SJed Brown 
2101552f7358SJed Brown /*@
2102eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2103552f7358SJed Brown 
2104552f7358SJed Brown   Not collective
2105552f7358SJed Brown 
2106552f7358SJed Brown   Input Parameter:
2107552f7358SJed Brown . mesh - The DMPlex
2108552f7358SJed Brown 
2109552f7358SJed Brown   Output Parameters:
2110552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2111552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2112552f7358SJed Brown 
2113552f7358SJed Brown   Level: beginner
2114552f7358SJed Brown 
2115552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2116552f7358SJed Brown @*/
2117552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2118552f7358SJed Brown {
2119552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2120552f7358SJed Brown 
2121552f7358SJed Brown   PetscFunctionBegin;
2122552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2123552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2124552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2125552f7358SJed Brown   PetscFunctionReturn(0);
2126552f7358SJed Brown }
2127552f7358SJed Brown 
2128552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2129552f7358SJed Brown {
2130552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2131552f7358SJed Brown   PetscInt       size;
2132552f7358SJed Brown   PetscErrorCode ierr;
2133552f7358SJed Brown 
2134552f7358SJed Brown   PetscFunctionBegin;
2135552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2136552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2137552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
21381795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
21391795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2140552f7358SJed Brown   if (mesh->maxSupportSize) {
2141552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2142552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
21431795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2144552f7358SJed Brown   }
2145552f7358SJed Brown   PetscFunctionReturn(0);
2146552f7358SJed Brown }
2147552f7358SJed Brown 
2148276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2149552f7358SJed Brown {
2150552f7358SJed Brown   PetscErrorCode ierr;
2151552f7358SJed Brown 
2152552f7358SJed Brown   PetscFunctionBegin;
21534d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
21544d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2155c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2156736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2157f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2158f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2159f94b4a02SBlaise Bourdin 
21603dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
21613dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2162f94b4a02SBlaise Bourdin     ierr = DMGetDefaultSection((*subdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
2163f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2164f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2165f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2166f94b4a02SBlaise Bourdin 
2167f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2168c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2169f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2170f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2171f94b4a02SBlaise Bourdin   }
2172552f7358SJed Brown   PetscFunctionReturn(0);
2173552f7358SJed Brown }
2174552f7358SJed Brown 
21752adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
21762adcc780SMatthew G. Knepley {
21772adcc780SMatthew G. Knepley   PetscErrorCode ierr;
21783dcd263cSBlaise Bourdin   PetscInt       i = 0;
21792adcc780SMatthew G. Knepley 
21802adcc780SMatthew G. Knepley   PetscFunctionBegin;
21812adcc780SMatthew G. Knepley   if (superdm) {ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);}
21822adcc780SMatthew G. Knepley   ierr = DMCreateSuperDM_Section_Private(dms, len, is, superdm);CHKERRQ(ierr);
2183c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
21843dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
21853dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
21863dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
21873dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
21883dcd263cSBlaise Bourdin 
21893dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
21903dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2191c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
21923dcd263cSBlaise Bourdin       ierr = DMGetDefaultSection((*superdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
21933dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
21943dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
21953dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
21963dcd263cSBlaise Bourdin 
21973dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2198c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
21993dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
22003dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
22013dcd263cSBlaise Bourdin       break;
22023dcd263cSBlaise Bourdin     }
22033dcd263cSBlaise Bourdin   }
22042adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
22052adcc780SMatthew G. Knepley }
22062adcc780SMatthew G. Knepley 
2207552f7358SJed Brown /*@
2208eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2209552f7358SJed Brown 
2210552f7358SJed Brown   Not collective
2211552f7358SJed Brown 
2212552f7358SJed Brown   Input Parameter:
2213552f7358SJed Brown . mesh - The DMPlex
2214552f7358SJed Brown 
2215552f7358SJed Brown   Output Parameter:
2216552f7358SJed Brown 
2217552f7358SJed Brown   Note:
2218552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2219552f7358SJed Brown 
2220552f7358SJed Brown   Level: beginner
2221552f7358SJed Brown 
2222552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2223552f7358SJed Brown @*/
2224552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2225552f7358SJed Brown {
2226552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2227552f7358SJed Brown   PetscInt      *offsets;
2228552f7358SJed Brown   PetscInt       supportSize;
2229552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2230552f7358SJed Brown   PetscErrorCode ierr;
2231552f7358SJed Brown 
2232552f7358SJed Brown   PetscFunctionBegin;
2233552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
223482f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2235552f7358SJed Brown   /* Calculate support sizes */
2236552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2237552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2238552f7358SJed Brown     PetscInt dof, off, c;
2239552f7358SJed Brown 
2240552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2241552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2242552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2243552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2244552f7358SJed Brown     }
2245552f7358SJed Brown   }
2246552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2247552f7358SJed Brown     PetscInt dof;
2248552f7358SJed Brown 
2249552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
22500d644c17SKarl Rupp 
2251552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2252552f7358SJed Brown   }
2253552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2254552f7358SJed Brown   /* Calculate supports */
2255552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
22561795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
22571795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2258552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2259552f7358SJed Brown     PetscInt dof, off, c;
2260552f7358SJed Brown 
2261552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2262552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2263552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2264552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2265552f7358SJed Brown       PetscInt       offS;
2266552f7358SJed Brown 
2267552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
22680d644c17SKarl Rupp 
2269552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2270552f7358SJed Brown       ++offsets[q];
2271552f7358SJed Brown     }
2272552f7358SJed Brown   }
2273552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2274552f7358SJed Brown   PetscFunctionReturn(0);
2275552f7358SJed Brown }
2276552f7358SJed Brown 
2277552f7358SJed Brown /*@
2278eaf898f9SPatrick Sanan   DMPlexStratify - The DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2279eaf898f9SPatrick Sanan   can be illustrated by a Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2280552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2281552f7358SJed Brown   the DAG.
2282552f7358SJed Brown 
2283bf4602e4SToby Isaac   Collective on dm
2284552f7358SJed Brown 
2285552f7358SJed Brown   Input Parameter:
2286552f7358SJed Brown . mesh - The DMPlex
2287552f7358SJed Brown 
2288552f7358SJed Brown   Output Parameter:
2289552f7358SJed Brown 
2290552f7358SJed Brown   Notes:
2291150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2292150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2293c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2294150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2295552f7358SJed Brown 
2296150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2297552f7358SJed Brown 
2298552f7358SJed Brown   Level: beginner
2299552f7358SJed Brown 
2300552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2301552f7358SJed Brown @*/
2302552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2303552f7358SJed Brown {
2304df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2305aa50250dSMatthew G. Knepley   DMLabel        label;
2306552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2307552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2308552f7358SJed Brown   PetscErrorCode ierr;
2309552f7358SJed Brown 
2310552f7358SJed Brown   PetscFunctionBegin;
2311552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2312552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2313552f7358SJed Brown   /* Calculate depth */
2314aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2315c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2316aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2317552f7358SJed Brown   /* Initialize roots and count leaves */
2318552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2319552f7358SJed Brown     PetscInt coneSize, supportSize;
2320552f7358SJed Brown 
2321552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2322552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2323552f7358SJed Brown     if (!coneSize && supportSize) {
2324552f7358SJed Brown       ++numRoots;
2325aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2326552f7358SJed Brown     } else if (!supportSize && coneSize) {
2327552f7358SJed Brown       ++numLeaves;
2328552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2329552f7358SJed Brown       /* Isolated points */
2330aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2331552f7358SJed Brown     }
2332552f7358SJed Brown   }
2333552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2334552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2335552f7358SJed Brown       PetscInt coneSize, supportSize;
2336552f7358SJed Brown 
2337552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2338552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2339552f7358SJed Brown       if (!supportSize && coneSize) {
2340aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2341552f7358SJed Brown       }
2342552f7358SJed Brown     }
2343552f7358SJed Brown   } else {
234474ef644bSMatthew G. Knepley     IS       pointIS;
234574ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2346552f7358SJed Brown 
234774ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
234874ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
234974ef644bSMatthew G. Knepley     while (numPoints) {
235074ef644bSMatthew G. Knepley       const PetscInt *points;
235174ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
235274ef644bSMatthew G. Knepley 
235374ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
235474ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
235574ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
235674ef644bSMatthew G. Knepley         const PetscInt *support;
235774ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
235874ef644bSMatthew G. Knepley 
235974ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
236074ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
236174ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
236274ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2363552f7358SJed Brown         }
2364552f7358SJed Brown       }
23655edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
236674ef644bSMatthew G. Knepley       ++level;
236774ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
236874ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
236974ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
237074ef644bSMatthew G. Knepley       else         {numPoints = 0;}
237174ef644bSMatthew G. Knepley     }
237274ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
237374ef644bSMatthew G. Knepley   }
2374bf4602e4SToby Isaac   { /* just in case there is an empty process */
2375bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2376bf4602e4SToby Isaac 
2377bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
23784de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
23794de306b1SToby Isaac       IS pointIS;
23804de306b1SToby Isaac 
23814de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
23824de306b1SToby Isaac       if (pointIS) {
23834de306b1SToby Isaac         PetscInt  min, max, numPoints;
23844de306b1SToby Isaac         PetscInt  start;
23854de306b1SToby Isaac         PetscBool contig;
23864de306b1SToby Isaac 
23874de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
23884de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
23894de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
23904de306b1SToby Isaac         if (start == 0 && contig) {
23914de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
23924de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
23934de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
23944de306b1SToby Isaac         }
23954de306b1SToby Isaac       }
23964de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
23974de306b1SToby Isaac     }
23986d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2399bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2400bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2401bf4602e4SToby Isaac     }
2402bf4602e4SToby Isaac   }
2403bf4602e4SToby Isaac 
2404df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2405552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2406552f7358SJed Brown   PetscFunctionReturn(0);
2407552f7358SJed Brown }
2408552f7358SJed Brown 
2409552f7358SJed Brown /*@C
2410552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2411552f7358SJed Brown 
2412552f7358SJed Brown   Not Collective
2413552f7358SJed Brown 
2414552f7358SJed Brown   Input Parameters:
2415552f7358SJed Brown + dm - The DMPlex object
2416552f7358SJed Brown . numPoints - The number of input points for the join
2417552f7358SJed Brown - points - The input points
2418552f7358SJed Brown 
2419552f7358SJed Brown   Output Parameters:
2420552f7358SJed Brown + numCoveredPoints - The number of points in the join
2421552f7358SJed Brown - coveredPoints - The points in the join
2422552f7358SJed Brown 
2423552f7358SJed Brown   Level: intermediate
2424552f7358SJed Brown 
2425552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2426552f7358SJed Brown 
24273813dfbdSMatthew G Knepley   Fortran Notes:
24283813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24293813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24303813dfbdSMatthew G Knepley 
24313813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24323813dfbdSMatthew G Knepley 
2433552f7358SJed Brown .keywords: mesh
2434552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2435552f7358SJed Brown @*/
2436552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2437552f7358SJed Brown {
2438552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2439552f7358SJed Brown   PetscInt      *join[2];
2440552f7358SJed Brown   PetscInt       joinSize, i = 0;
2441552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2442552f7358SJed Brown   PetscErrorCode ierr;
2443552f7358SJed Brown 
2444552f7358SJed Brown   PetscFunctionBegin;
2445552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2446552f7358SJed Brown   PetscValidPointer(points, 2);
2447552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2448552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
244969291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
245069291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2451552f7358SJed Brown   /* Copy in support of first point */
2452552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2453552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2454552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2455552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2456552f7358SJed Brown   }
2457552f7358SJed Brown   /* Check each successive support */
2458552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2459552f7358SJed Brown     PetscInt newJoinSize = 0;
2460552f7358SJed Brown 
2461552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2462552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2463552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2464552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2465552f7358SJed Brown 
2466552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2467552f7358SJed Brown         if (point == join[i][m]) {
2468552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2469552f7358SJed Brown           break;
2470552f7358SJed Brown         }
2471552f7358SJed Brown       }
2472552f7358SJed Brown     }
2473552f7358SJed Brown     joinSize = newJoinSize;
2474552f7358SJed Brown     i        = 1-i;
2475552f7358SJed Brown   }
2476552f7358SJed Brown   *numCoveredPoints = joinSize;
2477552f7358SJed Brown   *coveredPoints    = join[i];
247869291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2479552f7358SJed Brown   PetscFunctionReturn(0);
2480552f7358SJed Brown }
2481552f7358SJed Brown 
2482552f7358SJed Brown /*@C
2483552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2484552f7358SJed Brown 
2485552f7358SJed Brown   Not Collective
2486552f7358SJed Brown 
2487552f7358SJed Brown   Input Parameters:
2488552f7358SJed Brown + dm - The DMPlex object
2489552f7358SJed Brown . numPoints - The number of input points for the join
2490552f7358SJed Brown - points - The input points
2491552f7358SJed Brown 
2492552f7358SJed Brown   Output Parameters:
2493552f7358SJed Brown + numCoveredPoints - The number of points in the join
2494552f7358SJed Brown - coveredPoints - The points in the join
2495552f7358SJed Brown 
24963813dfbdSMatthew G Knepley   Fortran Notes:
24973813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24983813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24993813dfbdSMatthew G Knepley 
25003813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25013813dfbdSMatthew G Knepley 
2502552f7358SJed Brown   Level: intermediate
2503552f7358SJed Brown 
2504552f7358SJed Brown .keywords: mesh
2505552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2506552f7358SJed Brown @*/
2507552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2508552f7358SJed Brown {
2509552f7358SJed Brown   PetscErrorCode ierr;
2510552f7358SJed Brown 
2511552f7358SJed Brown   PetscFunctionBegin;
2512552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2513d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2514d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2515d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
251669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2517d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2518552f7358SJed Brown   PetscFunctionReturn(0);
2519552f7358SJed Brown }
2520552f7358SJed Brown 
2521552f7358SJed Brown /*@C
2522552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2523552f7358SJed Brown 
2524552f7358SJed Brown   Not Collective
2525552f7358SJed Brown 
2526552f7358SJed Brown   Input Parameters:
2527552f7358SJed Brown + dm - The DMPlex object
2528552f7358SJed Brown . numPoints - The number of input points for the join
2529552f7358SJed Brown - points - The input points
2530552f7358SJed Brown 
2531552f7358SJed Brown   Output Parameters:
2532552f7358SJed Brown + numCoveredPoints - The number of points in the join
2533552f7358SJed Brown - coveredPoints - The points in the join
2534552f7358SJed Brown 
25353813dfbdSMatthew G Knepley   Fortran Notes:
25363813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25373813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25383813dfbdSMatthew G Knepley 
25393813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25403813dfbdSMatthew G Knepley 
2541552f7358SJed Brown   Level: intermediate
2542552f7358SJed Brown 
2543552f7358SJed Brown .keywords: mesh
2544552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2545552f7358SJed Brown @*/
2546552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2547552f7358SJed Brown {
2548552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2549552f7358SJed Brown   PetscInt      *offsets, **closures;
2550552f7358SJed Brown   PetscInt      *join[2];
2551552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
255224c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2553552f7358SJed Brown   PetscErrorCode ierr;
2554552f7358SJed Brown 
2555552f7358SJed Brown   PetscFunctionBegin;
2556552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2557552f7358SJed Brown   PetscValidPointer(points, 2);
2558552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2559552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2560552f7358SJed Brown 
2561552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
25621795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
256369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
256424c766afSToby Isaac   ms      = mesh->maxSupportSize;
256524c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
256669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
256769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2568552f7358SJed Brown 
2569552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2570552f7358SJed Brown     PetscInt closureSize;
2571552f7358SJed Brown 
2572552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
25730d644c17SKarl Rupp 
2574552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2575552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2576552f7358SJed Brown       PetscInt pStart, pEnd, i;
2577552f7358SJed Brown 
2578552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2579552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2580552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2581552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2582552f7358SJed Brown           break;
2583552f7358SJed Brown         }
2584552f7358SJed Brown       }
2585552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2586552f7358SJed Brown     }
258782f516ccSBarry 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);
2588552f7358SJed Brown   }
2589552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2590552f7358SJed Brown     PetscInt dof;
2591552f7358SJed Brown 
2592552f7358SJed Brown     /* Copy in support of first point */
2593552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2594552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2595552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2596552f7358SJed Brown     }
2597552f7358SJed Brown     /* Check each successive cone */
2598552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2599552f7358SJed Brown       PetscInt newJoinSize = 0;
2600552f7358SJed Brown 
2601552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2602552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2603552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2604552f7358SJed Brown 
2605552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2606552f7358SJed Brown           if (point == join[i][m]) {
2607552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2608552f7358SJed Brown             break;
2609552f7358SJed Brown           }
2610552f7358SJed Brown         }
2611552f7358SJed Brown       }
2612552f7358SJed Brown       joinSize = newJoinSize;
2613552f7358SJed Brown       i        = 1-i;
2614552f7358SJed Brown     }
2615552f7358SJed Brown     if (joinSize) break;
2616552f7358SJed Brown   }
2617552f7358SJed Brown   *numCoveredPoints = joinSize;
2618552f7358SJed Brown   *coveredPoints    = join[i];
2619552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26200298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2621552f7358SJed Brown   }
2622552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
262369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
262469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2625552f7358SJed Brown   PetscFunctionReturn(0);
2626552f7358SJed Brown }
2627552f7358SJed Brown 
2628552f7358SJed Brown /*@C
2629552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2630552f7358SJed Brown 
2631552f7358SJed Brown   Not Collective
2632552f7358SJed Brown 
2633552f7358SJed Brown   Input Parameters:
2634552f7358SJed Brown + dm - The DMPlex object
2635552f7358SJed Brown . numPoints - The number of input points for the meet
2636552f7358SJed Brown - points - The input points
2637552f7358SJed Brown 
2638552f7358SJed Brown   Output Parameters:
2639552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2640552f7358SJed Brown - coveredPoints - The points in the meet
2641552f7358SJed Brown 
2642552f7358SJed Brown   Level: intermediate
2643552f7358SJed Brown 
2644552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2645552f7358SJed Brown 
26463813dfbdSMatthew G Knepley   Fortran Notes:
26473813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26483813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26493813dfbdSMatthew G Knepley 
26503813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26513813dfbdSMatthew G Knepley 
2652552f7358SJed Brown .keywords: mesh
2653552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2654552f7358SJed Brown @*/
2655552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2656552f7358SJed Brown {
2657552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2658552f7358SJed Brown   PetscInt      *meet[2];
2659552f7358SJed Brown   PetscInt       meetSize, i = 0;
2660552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2661552f7358SJed Brown   PetscErrorCode ierr;
2662552f7358SJed Brown 
2663552f7358SJed Brown   PetscFunctionBegin;
2664552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2665552f7358SJed Brown   PetscValidPointer(points, 2);
2666552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2667552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
266869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
266969291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2670552f7358SJed Brown   /* Copy in cone of first point */
2671552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2672552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2673552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2674552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2675552f7358SJed Brown   }
2676552f7358SJed Brown   /* Check each successive cone */
2677552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2678552f7358SJed Brown     PetscInt newMeetSize = 0;
2679552f7358SJed Brown 
2680552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2681552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2682552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2683552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2684552f7358SJed Brown 
2685552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2686552f7358SJed Brown         if (point == meet[i][m]) {
2687552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2688552f7358SJed Brown           break;
2689552f7358SJed Brown         }
2690552f7358SJed Brown       }
2691552f7358SJed Brown     }
2692552f7358SJed Brown     meetSize = newMeetSize;
2693552f7358SJed Brown     i        = 1-i;
2694552f7358SJed Brown   }
2695552f7358SJed Brown   *numCoveringPoints = meetSize;
2696552f7358SJed Brown   *coveringPoints    = meet[i];
269769291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2698552f7358SJed Brown   PetscFunctionReturn(0);
2699552f7358SJed Brown }
2700552f7358SJed Brown 
2701552f7358SJed Brown /*@C
2702552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2703552f7358SJed Brown 
2704552f7358SJed Brown   Not Collective
2705552f7358SJed Brown 
2706552f7358SJed Brown   Input Parameters:
2707552f7358SJed Brown + dm - The DMPlex object
2708552f7358SJed Brown . numPoints - The number of input points for the meet
2709552f7358SJed Brown - points - The input points
2710552f7358SJed Brown 
2711552f7358SJed Brown   Output Parameters:
2712552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2713552f7358SJed Brown - coveredPoints - The points in the meet
2714552f7358SJed Brown 
2715552f7358SJed Brown   Level: intermediate
2716552f7358SJed Brown 
27173813dfbdSMatthew G Knepley   Fortran Notes:
27183813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27193813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27203813dfbdSMatthew G Knepley 
27213813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27223813dfbdSMatthew G Knepley 
2723552f7358SJed Brown .keywords: mesh
2724552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2725552f7358SJed Brown @*/
2726552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2727552f7358SJed Brown {
2728552f7358SJed Brown   PetscErrorCode ierr;
2729552f7358SJed Brown 
2730552f7358SJed Brown   PetscFunctionBegin;
2731552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2732d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2733d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2734d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
273569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2736d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2737552f7358SJed Brown   PetscFunctionReturn(0);
2738552f7358SJed Brown }
2739552f7358SJed Brown 
2740552f7358SJed Brown /*@C
2741552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2742552f7358SJed Brown 
2743552f7358SJed Brown   Not Collective
2744552f7358SJed Brown 
2745552f7358SJed Brown   Input Parameters:
2746552f7358SJed Brown + dm - The DMPlex object
2747552f7358SJed Brown . numPoints - The number of input points for the meet
2748552f7358SJed Brown - points - The input points
2749552f7358SJed Brown 
2750552f7358SJed Brown   Output Parameters:
2751552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2752552f7358SJed Brown - coveredPoints - The points in the meet
2753552f7358SJed Brown 
2754552f7358SJed Brown   Level: intermediate
2755552f7358SJed Brown 
27563813dfbdSMatthew G Knepley   Fortran Notes:
27573813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27583813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27593813dfbdSMatthew G Knepley 
27603813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27613813dfbdSMatthew G Knepley 
2762552f7358SJed Brown .keywords: mesh
2763552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2764552f7358SJed Brown @*/
2765552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2766552f7358SJed Brown {
2767552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2768552f7358SJed Brown   PetscInt      *offsets, **closures;
2769552f7358SJed Brown   PetscInt      *meet[2];
2770552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
277124c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2772552f7358SJed Brown   PetscErrorCode ierr;
2773552f7358SJed Brown 
2774552f7358SJed Brown   PetscFunctionBegin;
2775552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2776552f7358SJed Brown   PetscValidPointer(points, 2);
2777552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2778552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2779552f7358SJed Brown 
2780552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2781785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
278269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
278324c766afSToby Isaac   mc      = mesh->maxConeSize;
278424c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
278569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
278669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2787552f7358SJed Brown 
2788552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2789552f7358SJed Brown     PetscInt closureSize;
2790552f7358SJed Brown 
2791552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
27920d644c17SKarl Rupp 
2793552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2794552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2795552f7358SJed Brown       PetscInt pStart, pEnd, i;
2796552f7358SJed Brown 
2797552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2798552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2799552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2800552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2801552f7358SJed Brown           break;
2802552f7358SJed Brown         }
2803552f7358SJed Brown       }
2804552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2805552f7358SJed Brown     }
280682f516ccSBarry 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);
2807552f7358SJed Brown   }
2808552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2809552f7358SJed Brown     PetscInt dof;
2810552f7358SJed Brown 
2811552f7358SJed Brown     /* Copy in cone of first point */
2812552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2813552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2814552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2815552f7358SJed Brown     }
2816552f7358SJed Brown     /* Check each successive cone */
2817552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2818552f7358SJed Brown       PetscInt newMeetSize = 0;
2819552f7358SJed Brown 
2820552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2821552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2822552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2823552f7358SJed Brown 
2824552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2825552f7358SJed Brown           if (point == meet[i][m]) {
2826552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2827552f7358SJed Brown             break;
2828552f7358SJed Brown           }
2829552f7358SJed Brown         }
2830552f7358SJed Brown       }
2831552f7358SJed Brown       meetSize = newMeetSize;
2832552f7358SJed Brown       i        = 1-i;
2833552f7358SJed Brown     }
2834552f7358SJed Brown     if (meetSize) break;
2835552f7358SJed Brown   }
2836552f7358SJed Brown   *numCoveredPoints = meetSize;
2837552f7358SJed Brown   *coveredPoints    = meet[i];
2838552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
28390298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2840552f7358SJed Brown   }
2841552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
284269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
284369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2844552f7358SJed Brown   PetscFunctionReturn(0);
2845552f7358SJed Brown }
2846552f7358SJed Brown 
28474e3744c5SMatthew G. Knepley /*@C
28484e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
28494e3744c5SMatthew G. Knepley 
28504e3744c5SMatthew G. Knepley   Not Collective
28514e3744c5SMatthew G. Knepley 
28524e3744c5SMatthew G. Knepley   Input Parameters:
28534e3744c5SMatthew G. Knepley + dmA - A DMPlex object
28544e3744c5SMatthew G. Knepley - dmB - A DMPlex object
28554e3744c5SMatthew G. Knepley 
28564e3744c5SMatthew G. Knepley   Output Parameters:
28574e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
28584e3744c5SMatthew G. Knepley 
28594e3744c5SMatthew G. Knepley   Level: intermediate
28604e3744c5SMatthew G. Knepley 
28614e3744c5SMatthew G. Knepley   Notes:
28624e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
28634e3744c5SMatthew G. Knepley 
28644e3744c5SMatthew G. Knepley .keywords: mesh
28654e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
28664e3744c5SMatthew G. Knepley @*/
28674e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
28684e3744c5SMatthew G. Knepley {
28694e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
28704e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
28714e3744c5SMatthew G. Knepley 
28724e3744c5SMatthew G. Knepley   PetscFunctionBegin;
28734e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
28744e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
28754e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
28764e3744c5SMatthew G. Knepley 
28774e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
28784e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
28794e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
28804e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
28814e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
28824e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
28834e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
28844e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
28854e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
28864e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
28874e3744c5SMatthew G. Knepley 
28884e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
28894e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
28904e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
28914e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
28924e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
28934e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
28944e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
28954e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
28964e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
28974e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
28984e3744c5SMatthew G. Knepley     }
28994e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
29004e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
29014e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
29024e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
29034e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
29044e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
29054e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
29064e3744c5SMatthew G. Knepley     }
29074e3744c5SMatthew G. Knepley   }
29084e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
29094e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
29104e3744c5SMatthew G. Knepley }
29114e3744c5SMatthew G. Knepley 
29127cd05799SMatthew G. Knepley /*@C
29137cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
29147cd05799SMatthew G. Knepley 
29157cd05799SMatthew G. Knepley   Not Collective
29167cd05799SMatthew G. Knepley 
29177cd05799SMatthew G. Knepley   Input Parameters:
29187cd05799SMatthew G. Knepley + dm         - The DMPlex
29197cd05799SMatthew G. Knepley . cellDim    - The cell dimension
29207cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
29217cd05799SMatthew G. Knepley 
29227cd05799SMatthew G. Knepley   Output Parameters:
29237cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
29247cd05799SMatthew G. Knepley 
29257cd05799SMatthew G. Knepley   Level: developer
29267cd05799SMatthew G. Knepley 
29277cd05799SMatthew G. Knepley   Notes:
29287cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
29297cd05799SMatthew G. Knepley 
29307cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
29317cd05799SMatthew G. Knepley @*/
293218ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2933a6dfd86eSKarl Rupp {
293482f516ccSBarry Smith   MPI_Comm       comm;
2935552f7358SJed Brown   PetscErrorCode ierr;
2936552f7358SJed Brown 
2937552f7358SJed Brown   PetscFunctionBegin;
293882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2939552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2940552f7358SJed Brown   switch (cellDim) {
2941552f7358SJed Brown   case 0:
2942552f7358SJed Brown     *numFaceVertices = 0;
2943552f7358SJed Brown     break;
2944552f7358SJed Brown   case 1:
2945552f7358SJed Brown     *numFaceVertices = 1;
2946552f7358SJed Brown     break;
2947552f7358SJed Brown   case 2:
2948552f7358SJed Brown     switch (numCorners) {
294919436ca2SJed Brown     case 3: /* triangle */
295019436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2951552f7358SJed Brown       break;
295219436ca2SJed Brown     case 4: /* quadrilateral */
295319436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2954552f7358SJed Brown       break;
295519436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
295619436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2957552f7358SJed Brown       break;
295819436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
295919436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2960552f7358SJed Brown       break;
2961552f7358SJed Brown     default:
2962f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2963552f7358SJed Brown     }
2964552f7358SJed Brown     break;
2965552f7358SJed Brown   case 3:
2966552f7358SJed Brown     switch (numCorners) {
296719436ca2SJed Brown     case 4: /* tetradehdron */
296819436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2969552f7358SJed Brown       break;
297019436ca2SJed Brown     case 6: /* tet cohesive cells */
297119436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2972552f7358SJed Brown       break;
297319436ca2SJed Brown     case 8: /* hexahedron */
297419436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2975552f7358SJed Brown       break;
297619436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
297719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2978552f7358SJed Brown       break;
297919436ca2SJed Brown     case 10: /* quadratic tetrahedron */
298019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2981552f7358SJed Brown       break;
298219436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
298319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2984552f7358SJed Brown       break;
298519436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
298619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2987552f7358SJed Brown       break;
298819436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
298919436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2990552f7358SJed Brown       break;
2991552f7358SJed Brown     default:
2992f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2993552f7358SJed Brown     }
2994552f7358SJed Brown     break;
2995552f7358SJed Brown   default:
2996f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2997552f7358SJed Brown   }
2998552f7358SJed Brown   PetscFunctionReturn(0);
2999552f7358SJed Brown }
3000552f7358SJed Brown 
3001552f7358SJed Brown /*@
3002aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3003552f7358SJed Brown 
3004552f7358SJed Brown   Not Collective
3005552f7358SJed Brown 
3006aa50250dSMatthew G. Knepley   Input Parameter:
3007552f7358SJed Brown . dm    - The DMPlex object
3008552f7358SJed Brown 
3009aa50250dSMatthew G. Knepley   Output Parameter:
3010aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3011552f7358SJed Brown 
3012552f7358SJed Brown   Level: developer
3013552f7358SJed Brown 
3014aa50250dSMatthew G. Knepley .keywords: mesh, points
3015aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3016aa50250dSMatthew G. Knepley @*/
3017aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3018aa50250dSMatthew G. Knepley {
3019aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
3020aa50250dSMatthew G. Knepley 
3021aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3022aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3023aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3024c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
3025c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3026aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3027aa50250dSMatthew G. Knepley }
3028aa50250dSMatthew G. Knepley 
3029aa50250dSMatthew G. Knepley /*@
3030aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3031aa50250dSMatthew G. Knepley 
3032aa50250dSMatthew G. Knepley   Not Collective
3033aa50250dSMatthew G. Knepley 
3034aa50250dSMatthew G. Knepley   Input Parameter:
3035aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3036aa50250dSMatthew G. Knepley 
3037aa50250dSMatthew G. Knepley   Output Parameter:
3038aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3039aa50250dSMatthew G. Knepley 
3040aa50250dSMatthew G. Knepley   Level: developer
3041552f7358SJed Brown 
3042552f7358SJed Brown .keywords: mesh, points
3043aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3044552f7358SJed Brown @*/
3045552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3046552f7358SJed Brown {
3047aa50250dSMatthew G. Knepley   DMLabel        label;
3048aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3049552f7358SJed Brown   PetscErrorCode ierr;
3050552f7358SJed Brown 
3051552f7358SJed Brown   PetscFunctionBegin;
3052552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3053552f7358SJed Brown   PetscValidPointer(depth, 2);
3054aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3055aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3056552f7358SJed Brown   *depth = d-1;
3057552f7358SJed Brown   PetscFunctionReturn(0);
3058552f7358SJed Brown }
3059552f7358SJed Brown 
3060552f7358SJed Brown /*@
3061552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3062552f7358SJed Brown 
3063552f7358SJed Brown   Not Collective
3064552f7358SJed Brown 
3065552f7358SJed Brown   Input Parameters:
3066552f7358SJed Brown + dm           - The DMPlex object
3067552f7358SJed Brown - stratumValue - The requested depth
3068552f7358SJed Brown 
3069552f7358SJed Brown   Output Parameters:
3070552f7358SJed Brown + start - The first point at this depth
3071552f7358SJed Brown - end   - One beyond the last point at this depth
3072552f7358SJed Brown 
3073552f7358SJed Brown   Level: developer
3074552f7358SJed Brown 
3075552f7358SJed Brown .keywords: mesh, points
3076552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3077552f7358SJed Brown @*/
30780adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
30790adebc6cSBarry Smith {
3080aa50250dSMatthew G. Knepley   DMLabel        label;
308163d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3082552f7358SJed Brown   PetscErrorCode ierr;
3083552f7358SJed Brown 
3084552f7358SJed Brown   PetscFunctionBegin;
3085552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
308663d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
308763d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3088552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
30890d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
309063d1a920SMatthew G. Knepley   if (stratumValue < 0) {
309163d1a920SMatthew G. Knepley     if (start) *start = pStart;
309263d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
309363d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3094552f7358SJed Brown   }
3095aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
309663d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
309763d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3098552f7358SJed Brown   PetscFunctionReturn(0);
3099552f7358SJed Brown }
3100552f7358SJed Brown 
3101552f7358SJed Brown /*@
3102552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3103552f7358SJed Brown 
3104552f7358SJed Brown   Not Collective
3105552f7358SJed Brown 
3106552f7358SJed Brown   Input Parameters:
3107552f7358SJed Brown + dm           - The DMPlex object
3108552f7358SJed Brown - stratumValue - The requested height
3109552f7358SJed Brown 
3110552f7358SJed Brown   Output Parameters:
3111552f7358SJed Brown + start - The first point at this height
3112552f7358SJed Brown - end   - One beyond the last point at this height
3113552f7358SJed Brown 
3114552f7358SJed Brown   Level: developer
3115552f7358SJed Brown 
3116552f7358SJed Brown .keywords: mesh, points
3117552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3118552f7358SJed Brown @*/
31190adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
31200adebc6cSBarry Smith {
3121aa50250dSMatthew G. Knepley   DMLabel        label;
312263d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3123552f7358SJed Brown   PetscErrorCode ierr;
3124552f7358SJed Brown 
3125552f7358SJed Brown   PetscFunctionBegin;
3126552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
312763d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
312863d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3129552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
31300d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
313163d1a920SMatthew G. Knepley   if (stratumValue < 0) {
313263d1a920SMatthew G. Knepley     if (start) *start = pStart;
313363d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
313463d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3135552f7358SJed Brown   }
3136aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
313713903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
313863d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
313963d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3140552f7358SJed Brown   PetscFunctionReturn(0);
3141552f7358SJed Brown }
3142552f7358SJed Brown 
3143552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
31447cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
3145a6dfd86eSKarl Rupp {
3146ee55978aSMatthew G. Knepley   PetscInt      *pMax;
3147470f9322SMatthew G. Knepley   PetscInt       depth, cellHeight, pStart = 0, pEnd = 0;
3148ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
3149fa716be8SMatthew G. Knepley   PetscBool     *isFE;
3150552f7358SJed Brown   PetscErrorCode ierr;
3151552f7358SJed Brown 
3152552f7358SJed Brown   PetscFunctionBegin;
3153fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
3154ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
3155fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
3156fa716be8SMatthew G. Knepley     PetscObject  obj;
3157fa716be8SMatthew G. Knepley     PetscClassId id;
3158fa716be8SMatthew G. Knepley 
3159ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
3160ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
3161fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
3162fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3163fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
3164fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
3165552f7358SJed Brown   }
316682f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
3167552f7358SJed Brown   if (numFields > 0) {
3168552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
3169552f7358SJed Brown     if (numComp) {
3170552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3171552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
3172d484f18aSToby Isaac         if (isFE[f]) {
3173d484f18aSToby Isaac           PetscFE           fe;
3174d484f18aSToby Isaac           PetscDualSpace    dspace;
3175d484f18aSToby Isaac           const PetscInt    ***perms;
3176d484f18aSToby Isaac           const PetscScalar ***flips;
3177d484f18aSToby Isaac           const PetscInt    *numDof;
3178d484f18aSToby Isaac 
3179d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
3180d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
3181d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
3182d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
3183d484f18aSToby Isaac           if (perms || flips) {
3184d484f18aSToby Isaac             DM               K;
3185d484f18aSToby Isaac             DMLabel          depthLabel;
3186d484f18aSToby Isaac             PetscInt         depth, h;
3187d484f18aSToby Isaac             PetscSectionSym  sym;
3188d484f18aSToby Isaac 
3189d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
3190d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
3191d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
3192d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
3193d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
3194d484f18aSToby Isaac               PetscDualSpace    hspace;
3195d484f18aSToby Isaac               PetscInt          kStart, kEnd;
3196d484f18aSToby Isaac               PetscInt          kConeSize;
3197d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3198d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3199d484f18aSToby Isaac 
3200d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3201d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3202d484f18aSToby Isaac               if (!hspace) continue;
3203d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3204d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3205d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3206d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3207d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3208d484f18aSToby Isaac               ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3209d484f18aSToby Isaac             }
3210d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3211d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3212d484f18aSToby Isaac           }
3213d484f18aSToby Isaac         }
3214552f7358SJed Brown       }
3215552f7358SJed Brown     }
3216552f7358SJed Brown   }
3217552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3218552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3219916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
32209ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
32219ac3fadcSMatthew 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);
3222470f9322SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
3223470f9322SMatthew G. Knepley   for (dep = 0; dep <= depth - cellHeight; ++dep) {
3224916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3225916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3226fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3227552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3228ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3229ee55978aSMatthew G. Knepley 
3230552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3231fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3232552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3233ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3234552f7358SJed Brown       }
3235ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3236552f7358SJed Brown     }
3237552f7358SJed Brown   }
32389ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3239fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3240552f7358SJed Brown   PetscFunctionReturn(0);
3241552f7358SJed Brown }
3242552f7358SJed Brown 
3243552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3244ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3245552f7358SJed Brown */
32467cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3247a6dfd86eSKarl Rupp {
3248552f7358SJed Brown   PetscInt       numFields;
3249552f7358SJed Brown   PetscInt       bc;
3250a68b90caSToby Isaac   PetscSection   aSec;
3251552f7358SJed Brown   PetscErrorCode ierr;
3252552f7358SJed Brown 
3253552f7358SJed Brown   PetscFunctionBegin;
3254552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3255552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3256552f7358SJed Brown     PetscInt        field = 0;
3257ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3258552f7358SJed Brown     const PetscInt *idx;
3259ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3260552f7358SJed Brown 
32610d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3262ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3263ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3264552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3265552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3266552f7358SJed Brown     for (i = 0; i < n; ++i) {
3267552f7358SJed Brown       const PetscInt p = idx[i];
326806ff1081SMatthew G. Knepley       PetscInt       numConst;
3269552f7358SJed Brown 
3270552f7358SJed Brown       if (numFields) {
3271552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3272552f7358SJed Brown       } else {
3273552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3274552f7358SJed Brown       }
327506ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
327606ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
327706ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3278552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3279552f7358SJed Brown     }
3280552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
328106ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3282552f7358SJed Brown   }
3283a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3284a68b90caSToby Isaac   if (aSec) {
3285a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3286a68b90caSToby Isaac 
3287a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3288a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3289ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3290a68b90caSToby Isaac 
3291a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3292a68b90caSToby Isaac       if (dof) {
3293a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3294a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3295a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3296a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3297a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3298a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3299a68b90caSToby Isaac         }
3300a68b90caSToby Isaac       }
3301a68b90caSToby Isaac     }
3302a68b90caSToby Isaac   }
3303552f7358SJed Brown   PetscFunctionReturn(0);
3304552f7358SJed Brown }
3305552f7358SJed Brown 
3306ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3307ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3308ab1d0545SMatthew G. Knepley */
33097cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
33100adebc6cSBarry Smith {
3311ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3312ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3313503335f2SSander Arens   PetscInt       numFields, cdof, maxDof = 0, pStart, pEnd, p, bc, f, d;
3314552f7358SJed Brown   PetscErrorCode ierr;
3315552f7358SJed Brown 
3316552f7358SJed Brown   PetscFunctionBegin;
3317552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3318ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3319ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3320552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3321503335f2SSander Arens   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr); maxDof = PetscMax(maxDof, cdof);}
3322ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3323ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3324ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3325ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3326ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3327ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3328ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3329ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3330552f7358SJed Brown 
3331ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3332ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3333ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3334ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3335ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3336ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3337ab1d0545SMatthew G. Knepley       const PetscInt *find;
3338503335f2SSander Arens       PetscInt        fdof, fcdof, c;
3339ab1d0545SMatthew G. Knepley 
3340503335f2SSander Arens       ierr = PetscSectionGetFieldDof(section, p, field, &fdof);CHKERRQ(ierr);
3341503335f2SSander Arens       if (!fdof) continue;
3342ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3343503335f2SSander Arens         for (d = 0; d < fdof; ++d) indices[d] = d;
3344503335f2SSander Arens         fcdof = fdof;
3345552f7358SJed Brown       } else {
3346503335f2SSander Arens         ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3347ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3348ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3349503335f2SSander Arens         for (c = 0; c < Nc; ++c) indices[d++] = comp[c];
3350503335f2SSander Arens         ierr = PetscSortRemoveDupsInt(&d, indices);CHKERRQ(ierr);
3351503335f2SSander Arens         for (c = d; c < fcdof; ++c) indices[c] = -1;
3352503335f2SSander Arens         fcdof = d;
3353552f7358SJed Brown       }
3354503335f2SSander Arens       ierr = PetscSectionSetFieldConstraintDof(section, p, field, fcdof);CHKERRQ(ierr);
3355ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3356552f7358SJed Brown     }
3357ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3358ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3359552f7358SJed Brown   }
3360ab1d0545SMatthew G. Knepley   /* Handle anchors */
3361ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3362ab1d0545SMatthew G. Knepley   if (aSec) {
3363ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3364552f7358SJed Brown 
3365ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3366ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3367ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3368503335f2SSander Arens       PetscInt dof, f;
3369ab1d0545SMatthew G. Knepley 
3370ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3371ab1d0545SMatthew G. Knepley       if (dof) {
3372ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3373ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3374ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3375ab1d0545SMatthew G. Knepley         }
3376ab1d0545SMatthew G. Knepley       }
3377ab1d0545SMatthew G. Knepley     }
3378ab1d0545SMatthew G. Knepley   }
3379ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3380ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3381ab1d0545SMatthew G. Knepley }
3382ab1d0545SMatthew G. Knepley 
3383ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
33847cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3385ab1d0545SMatthew G. Knepley {
3386ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3387ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3388ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3389ab1d0545SMatthew G. Knepley 
3390ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3391ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3392ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3393ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3394ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3395ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3396552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3397552f7358SJed Brown     PetscInt cdof, d;
3398552f7358SJed Brown 
3399552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3400552f7358SJed Brown     if (cdof) {
3401552f7358SJed Brown       if (numFields) {
3402552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3403552f7358SJed Brown 
3404552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3405ab1d0545SMatthew G. Knepley           const PetscInt *find;
3406ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3407552f7358SJed Brown 
3408552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3409ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3410ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3411ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3412ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3413ab1d0545SMatthew G. Knepley           numConst += fcdof;
3414552f7358SJed Brown           foff     += fdof;
3415552f7358SJed Brown         }
3416503335f2SSander Arens         if (cdof != numConst) {ierr = PetscSectionSetConstraintDof(section, p, numConst);CHKERRQ(ierr);}
3417552f7358SJed Brown       } else {
34180d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3419552f7358SJed Brown       }
3420552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3421552f7358SJed Brown     }
3422552f7358SJed Brown   }
3423552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3424552f7358SJed Brown   PetscFunctionReturn(0);
3425552f7358SJed Brown }
3426552f7358SJed Brown 
3427552f7358SJed Brown /*@C
3428552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3429552f7358SJed Brown 
3430552f7358SJed Brown   Not Collective
3431552f7358SJed Brown 
3432552f7358SJed Brown   Input Parameters:
3433552f7358SJed Brown + dm        - The DMPlex object
3434552f7358SJed Brown . dim       - The spatial dimension of the problem
3435552f7358SJed Brown . numFields - The number of fields in the problem
3436552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3437552f7358SJed 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
3438552f7358SJed Brown . numBC     - The number of boundary conditions
3439552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3440ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3441ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3442f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3443552f7358SJed Brown 
3444552f7358SJed Brown   Output Parameter:
3445552f7358SJed Brown . section - The PetscSection object
3446552f7358SJed Brown 
3447eaf898f9SPatrick 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
3448f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3449f8f126e8SMatthew G. Knepley 
3450f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3451552f7358SJed Brown 
3452552f7358SJed Brown   Level: developer
3453552f7358SJed Brown 
34543813dfbdSMatthew G Knepley   Fortran Notes:
34553813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
34563813dfbdSMatthew G Knepley 
3457552f7358SJed Brown .keywords: mesh, elements
3458f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3459552f7358SJed Brown @*/
3460ab1d0545SMatthew 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)
3461a6dfd86eSKarl Rupp {
3462a68b90caSToby Isaac   PetscSection   aSec;
3463552f7358SJed Brown   PetscErrorCode ierr;
3464552f7358SJed Brown 
3465552f7358SJed Brown   PetscFunctionBegin;
3466552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3467ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3468f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3469552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3470a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3471ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3472ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3473ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3474ab1d0545SMatthew G. Knepley   }
3475ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3476552f7358SJed Brown   PetscFunctionReturn(0);
3477552f7358SJed Brown }
3478552f7358SJed Brown 
34790adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
34800adebc6cSBarry Smith {
3481efe440bfSMatthew G. Knepley   PetscSection   section, s;
3482efe440bfSMatthew G. Knepley   Mat            m;
34833e922f36SToby Isaac   PetscInt       maxHeight;
3484552f7358SJed Brown   PetscErrorCode ierr;
3485552f7358SJed Brown 
3486552f7358SJed Brown   PetscFunctionBegin;
348738221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
34883e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
34893e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
349082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3491552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
34921d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3493efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3494efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3495efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3496efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3497efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3498552f7358SJed Brown   PetscFunctionReturn(0);
3499552f7358SJed Brown }
3500552f7358SJed Brown 
35017cd05799SMatthew G. Knepley /*@C
35027cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
35037cd05799SMatthew G. Knepley 
35047cd05799SMatthew G. Knepley   Not Collective
35057cd05799SMatthew G. Knepley 
35067cd05799SMatthew G. Knepley   Input Parameters:
35077cd05799SMatthew G. Knepley . dm        - The DMPlex object
35087cd05799SMatthew G. Knepley 
35097cd05799SMatthew G. Knepley   Output Parameter:
35107cd05799SMatthew G. Knepley . section - The PetscSection object
35117cd05799SMatthew G. Knepley 
35127cd05799SMatthew G. Knepley   Level: developer
35137cd05799SMatthew G. Knepley 
35147cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
35157cd05799SMatthew G. Knepley @*/
35160adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
35170adebc6cSBarry Smith {
3518552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3519552f7358SJed Brown 
3520552f7358SJed Brown   PetscFunctionBegin;
3521552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3522552f7358SJed Brown   if (section) *section = mesh->coneSection;
3523552f7358SJed Brown   PetscFunctionReturn(0);
3524552f7358SJed Brown }
3525552f7358SJed Brown 
35267cd05799SMatthew G. Knepley /*@C
35277cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
35287cd05799SMatthew G. Knepley 
35297cd05799SMatthew G. Knepley   Not Collective
35307cd05799SMatthew G. Knepley 
35317cd05799SMatthew G. Knepley   Input Parameters:
35327cd05799SMatthew G. Knepley . dm        - The DMPlex object
35337cd05799SMatthew G. Knepley 
35347cd05799SMatthew G. Knepley   Output Parameter:
35357cd05799SMatthew G. Knepley . section - The PetscSection object
35367cd05799SMatthew G. Knepley 
35377cd05799SMatthew G. Knepley   Level: developer
35387cd05799SMatthew G. Knepley 
35397cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35407cd05799SMatthew G. Knepley @*/
35418cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
35428cb4d582SMatthew G. Knepley {
35438cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
35448cb4d582SMatthew G. Knepley 
35458cb4d582SMatthew G. Knepley   PetscFunctionBegin;
35468cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35478cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
35488cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
35498cb4d582SMatthew G. Knepley }
35508cb4d582SMatthew G. Knepley 
35517cd05799SMatthew G. Knepley /*@C
35527cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
35537cd05799SMatthew G. Knepley 
35547cd05799SMatthew G. Knepley   Not Collective
35557cd05799SMatthew G. Knepley 
35567cd05799SMatthew G. Knepley   Input Parameters:
35577cd05799SMatthew G. Knepley . dm        - The DMPlex object
35587cd05799SMatthew G. Knepley 
35597cd05799SMatthew G. Knepley   Output Parameter:
35607cd05799SMatthew G. Knepley . cones - The cone for each point
35617cd05799SMatthew G. Knepley 
35627cd05799SMatthew G. Knepley   Level: developer
35637cd05799SMatthew G. Knepley 
35647cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35657cd05799SMatthew G. Knepley @*/
3566a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3567a6dfd86eSKarl Rupp {
3568552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3569552f7358SJed Brown 
3570552f7358SJed Brown   PetscFunctionBegin;
3571552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3572552f7358SJed Brown   if (cones) *cones = mesh->cones;
3573552f7358SJed Brown   PetscFunctionReturn(0);
3574552f7358SJed Brown }
3575552f7358SJed Brown 
35767cd05799SMatthew G. Knepley /*@C
35777cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
35787cd05799SMatthew G. Knepley 
35797cd05799SMatthew G. Knepley   Not Collective
35807cd05799SMatthew G. Knepley 
35817cd05799SMatthew G. Knepley   Input Parameters:
35827cd05799SMatthew G. Knepley . dm        - The DMPlex object
35837cd05799SMatthew G. Knepley 
35847cd05799SMatthew G. Knepley   Output Parameter:
35857cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
35867cd05799SMatthew G. Knepley 
35877cd05799SMatthew G. Knepley   Level: developer
35887cd05799SMatthew G. Knepley 
35897cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35907cd05799SMatthew G. Knepley @*/
3591a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3592a6dfd86eSKarl Rupp {
3593552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3594552f7358SJed Brown 
3595552f7358SJed Brown   PetscFunctionBegin;
3596552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3597552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3598552f7358SJed Brown   PetscFunctionReturn(0);
3599552f7358SJed Brown }
3600552f7358SJed Brown 
3601552f7358SJed Brown /******************************** FEM Support **********************************/
3602552f7358SJed Brown 
36037391a63aSMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscInt point, PetscSection section)
36043194fc30SMatthew G. Knepley {
36057391a63aSMatthew G. Knepley   DMLabel        label;
36063194fc30SMatthew G. Knepley   PetscInt      *perm;
36077391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
36083194fc30SMatthew G. Knepley   PetscErrorCode ierr;
36093194fc30SMatthew G. Knepley 
36103194fc30SMatthew G. Knepley   PetscFunctionBegin;
36117391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
36123194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
36137391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
36147391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
36157391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
36167391a63aSMatthew G. Knepley   else if  (depth == dim) {
36177391a63aSMatthew G. Knepley     const PetscInt *cone;
36187391a63aSMatthew G. Knepley 
36197391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
36207391a63aSMatthew G. Knepley     eStart = cone[0];
36217391a63aSMatthew 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);
36227391a63aSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
36233194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
362489eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
36253194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
36263194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
36273194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
36283194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
36293194fc30SMatthew G. Knepley     k = k/Nc + 1;
363089eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
36313194fc30SMatthew G. Knepley   }
36323194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
36333194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
363489eabcffSMatthew G. Knepley     switch (dim) {
363589eabcffSMatthew G. Knepley     case 2:
36363194fc30SMatthew 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} */
36373194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
36383194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
36393194fc30SMatthew G. Knepley       k = k/Nc + 1;
36403194fc30SMatthew G. Knepley       /* The SEM order is
36413194fc30SMatthew G. Knepley 
36423194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
364389eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
36443194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
36453194fc30SMatthew G. Knepley       */
36463194fc30SMatthew G. Knepley       {
36473194fc30SMatthew G. Knepley         const PetscInt of   = 0;
36483194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
36493194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
36503194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
36513194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
36523194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
36533194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
36543194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
36553194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
36563194fc30SMatthew G. Knepley         PetscInt       o;
36573194fc30SMatthew G. Knepley 
36583194fc30SMatthew G. Knepley         /* bottom */
36593194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
36603194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
36613194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
36623194fc30SMatthew G. Knepley         /* middle */
36633194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
36643194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
36653194fc30SMatthew 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;
36663194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
36673194fc30SMatthew G. Knepley         }
36683194fc30SMatthew G. Knepley         /* top */
36693194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
36703194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
36713194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
36723194fc30SMatthew G. Knepley         foffset = offset;
36733194fc30SMatthew G. Knepley       }
367489eabcffSMatthew G. Knepley       break;
367589eabcffSMatthew G. Knepley     case 3:
367689eabcffSMatthew G. Knepley       /* The original hex closure is
367789eabcffSMatthew G. Knepley 
367889eabcffSMatthew G. Knepley          {c,
367989eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
368089eabcffSMatthew 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,
368189eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
368289eabcffSMatthew G. Knepley       */
368389eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
368489eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
368589eabcffSMatthew G. Knepley       k = k/Nc + 1;
368689eabcffSMatthew G. Knepley       /* The SEM order is
368789eabcffSMatthew G. Knepley          Bottom Slice
368889eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
368989eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
369089eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
369189eabcffSMatthew G. Knepley 
369289eabcffSMatthew G. Knepley          Middle Slice (j)
369389eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
369489eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
369589eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
369689eabcffSMatthew G. Knepley 
369789eabcffSMatthew G. Knepley          Top Slice
369889eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
369989eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
370089eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
370189eabcffSMatthew G. Knepley       */
370289eabcffSMatthew G. Knepley       {
370389eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
370489eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
370589eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
370689eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
370789eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
370889eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
370989eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
371089eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
371189eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
371289eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
371389eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
371489eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
371589eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
371689eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
371789eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
371889eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
371989eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
372089eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
372189eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
372289eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
372389eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
372489eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
372589eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
372689eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
372789eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
372889eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
372989eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
373089eabcffSMatthew G. Knepley         PetscInt       o, n;
373189eabcffSMatthew G. Knepley 
373289eabcffSMatthew G. Knepley         /* Bottom Slice */
373389eabcffSMatthew G. Knepley         /*   bottom */
373489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
373589eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
373689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
373789eabcffSMatthew G. Knepley         /*   middle */
373889eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
373989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3740316b7f87SMax 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;}
374189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
37423194fc30SMatthew G. Knepley         }
374389eabcffSMatthew G. Knepley         /*   top */
374489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
374589eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
374689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
374789eabcffSMatthew G. Knepley 
374889eabcffSMatthew G. Knepley         /* Middle Slice */
374989eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
375089eabcffSMatthew G. Knepley           /*   bottom */
375189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
375289eabcffSMatthew 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;
375389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
375489eabcffSMatthew G. Knepley           /*   middle */
375589eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
375689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
375789eabcffSMatthew 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;
375889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
375989eabcffSMatthew G. Knepley           }
376089eabcffSMatthew G. Knepley           /*   top */
376189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
376289eabcffSMatthew 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;
376389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
376489eabcffSMatthew G. Knepley         }
376589eabcffSMatthew G. Knepley 
376689eabcffSMatthew G. Knepley         /* Top Slice */
376789eabcffSMatthew G. Knepley         /*   bottom */
376889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
376989eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
377089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
377189eabcffSMatthew G. Knepley         /*   middle */
377289eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
377389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
377489eabcffSMatthew 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;
377589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
377689eabcffSMatthew G. Knepley         }
377789eabcffSMatthew G. Knepley         /*   top */
377889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
377989eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
378089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
378189eabcffSMatthew G. Knepley 
378289eabcffSMatthew G. Knepley         foffset = offset;
378389eabcffSMatthew G. Knepley       }
378489eabcffSMatthew G. Knepley       break;
378589eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
378689eabcffSMatthew G. Knepley     }
378789eabcffSMatthew G. Knepley   }
378889eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
37893194fc30SMatthew G. Knepley   /* Check permutation */
37903194fc30SMatthew G. Knepley   {
37913194fc30SMatthew G. Knepley     PetscInt *check;
37923194fc30SMatthew G. Knepley 
37933194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
37943194fc30SMatthew 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]);}
37953194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
37963194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
37973194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
37983194fc30SMatthew G. Knepley   }
37991fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
38003194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
38013194fc30SMatthew G. Knepley }
38023194fc30SMatthew G. Knepley 
3803e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3804e071409bSToby Isaac {
3805e071409bSToby Isaac   PetscDS        prob;
3806e071409bSToby Isaac   PetscInt       depth, Nf, h;
3807e071409bSToby Isaac   DMLabel        label;
3808e071409bSToby Isaac   PetscErrorCode ierr;
3809e071409bSToby Isaac 
3810e071409bSToby Isaac   PetscFunctionBeginHot;
3811e071409bSToby Isaac   prob    = dm->prob;
3812e071409bSToby Isaac   Nf      = prob->Nf;
3813e071409bSToby Isaac   label   = dm->depthLabel;
3814e071409bSToby Isaac   *dspace = NULL;
3815e071409bSToby Isaac   if (field < Nf) {
3816e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3817e071409bSToby Isaac 
3818e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3819e071409bSToby Isaac       PetscDualSpace dsp;
3820e071409bSToby Isaac 
3821e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3822e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3823e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3824e071409bSToby Isaac       h    = depth - 1 - h;
3825e071409bSToby Isaac       if (h) {
3826e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3827e071409bSToby Isaac       } else {
3828e071409bSToby Isaac         *dspace = dsp;
3829e071409bSToby Isaac       }
3830e071409bSToby Isaac     }
3831e071409bSToby Isaac   }
3832e071409bSToby Isaac   PetscFunctionReturn(0);
3833e071409bSToby Isaac }
3834e071409bSToby Isaac 
3835e071409bSToby Isaac 
38361a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3837a6dfd86eSKarl Rupp {
3838552f7358SJed Brown   PetscScalar    *array, *vArray;
3839d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
38401a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3841552f7358SJed Brown   PetscErrorCode  ierr;
3842552f7358SJed Brown 
38431b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
38442a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
38455a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
38465a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
38475a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
38483f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
38499df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
38509df71ca4SMatthew G. Knepley       PetscInt dof;
3851d9917b9dSMatthew G. Knepley 
38529df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38539df71ca4SMatthew G. Knepley       size += dof;
38549df71ca4SMatthew G. Knepley     }
38559df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
38569df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
38572a3aaacfSMatthew G. Knepley       PetscInt       dof;
38585a1bb5cfSMatthew G. Knepley 
38595a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
38602a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38615a1bb5cfSMatthew G. Knepley       size += dof;
38625a1bb5cfSMatthew G. Knepley     }
38633f7cbbe7SMatthew G. Knepley     if (!values) {
38643f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
38653f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
38663f7cbbe7SMatthew G. Knepley     }
386769291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
3868982e9ed1SMatthew G. Knepley   } else {
3869982e9ed1SMatthew G. Knepley     array = *values;
3870982e9ed1SMatthew G. Knepley   }
38719df71ca4SMatthew G. Knepley   size = 0;
38725a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
38739df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
38749df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
38759df71ca4SMatthew G. Knepley     PetscScalar *varr;
3876d9917b9dSMatthew G. Knepley 
38779df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38789df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
38799df71ca4SMatthew G. Knepley     varr = &vArray[off];
38801a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
38811a271a75SMatthew G. Knepley       array[offset] = varr[d];
38829df71ca4SMatthew G. Knepley     }
38839df71ca4SMatthew G. Knepley     size += dof;
38849df71ca4SMatthew G. Knepley   }
38859df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
38869df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
38879df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
38885a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
38895a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
38905a1bb5cfSMatthew G. Knepley 
389152ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
38925a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38935a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
38945a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
38955a1bb5cfSMatthew G. Knepley     if (o >= 0) {
38961a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
38971a271a75SMatthew G. Knepley         array[offset] = varr[d];
38985a1bb5cfSMatthew G. Knepley       }
38995a1bb5cfSMatthew G. Knepley     } else {
39001a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
39011a271a75SMatthew G. Knepley         array[offset] = varr[d];
39025a1bb5cfSMatthew G. Knepley       }
39035a1bb5cfSMatthew G. Knepley     }
39049df71ca4SMatthew G. Knepley     size += dof;
39055a1bb5cfSMatthew G. Knepley   }
39065a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
39079df71ca4SMatthew G. Knepley   if (!*values) {
39085a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
39095a1bb5cfSMatthew G. Knepley     *values = array;
39109df71ca4SMatthew G. Knepley   } else {
39118ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
39128c312ff3SMatthew G. Knepley     *csize = size;
39139df71ca4SMatthew G. Knepley   }
39145a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
39155a1bb5cfSMatthew G. Knepley }
3916d9917b9dSMatthew G. Knepley 
3917923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3918923c78e0SToby Isaac {
3919923c78e0SToby Isaac   const PetscInt *cla;
3920923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3921923c78e0SToby Isaac   PetscErrorCode ierr;
3922923c78e0SToby Isaac 
3923923c78e0SToby Isaac   PetscFunctionBeginHot;
3924923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3925923c78e0SToby Isaac   if (!*clPoints) {
3926923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3927923c78e0SToby Isaac 
3928923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3929923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3930923c78e0SToby Isaac     /* Compress out points not in the section */
3931923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3932923c78e0SToby Isaac       PetscInt r = pts[2*p];
3933923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3934923c78e0SToby Isaac         pts[q*2]   = r;
3935923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3936923c78e0SToby Isaac         ++q;
3937923c78e0SToby Isaac       }
3938923c78e0SToby Isaac     }
3939923c78e0SToby Isaac     np = q;
3940923c78e0SToby Isaac     cla = NULL;
3941923c78e0SToby Isaac   } else {
3942923c78e0SToby Isaac     PetscInt dof, off;
3943923c78e0SToby Isaac 
3944923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3945923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3946923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3947923c78e0SToby Isaac     np   = dof/2;
3948923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3949923c78e0SToby Isaac   }
3950923c78e0SToby Isaac   *numPoints = np;
3951923c78e0SToby Isaac   *points    = pts;
3952923c78e0SToby Isaac   *clp       = cla;
3953923c78e0SToby Isaac 
3954923c78e0SToby Isaac   PetscFunctionReturn(0);
3955923c78e0SToby Isaac }
3956923c78e0SToby Isaac 
3957923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3958923c78e0SToby Isaac {
3959923c78e0SToby Isaac   PetscErrorCode ierr;
3960923c78e0SToby Isaac 
3961923c78e0SToby Isaac   PetscFunctionBeginHot;
3962923c78e0SToby Isaac   if (!*clPoints) {
3963923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3964923c78e0SToby Isaac   } else {
3965923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3966923c78e0SToby Isaac   }
3967923c78e0SToby Isaac   *numPoints = 0;
3968923c78e0SToby Isaac   *points    = NULL;
3969923c78e0SToby Isaac   *clSec     = NULL;
3970923c78e0SToby Isaac   *clPoints  = NULL;
3971923c78e0SToby Isaac   *clp       = NULL;
3972923c78e0SToby Isaac   PetscFunctionReturn(0);
3973923c78e0SToby Isaac }
3974923c78e0SToby Isaac 
397597e99dd9SToby 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[])
39761a271a75SMatthew G. Knepley {
39771a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
397897e99dd9SToby Isaac   const PetscInt    **perms = NULL;
397997e99dd9SToby Isaac   const PetscScalar **flips = NULL;
39801a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39811a271a75SMatthew G. Knepley 
39821a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3983fe02ba77SJed Brown   *size = 0;
398497e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
398597e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
398697e99dd9SToby Isaac     const PetscInt    point = points[2*p];
398797e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
398897e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
39891a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
39901a271a75SMatthew G. Knepley     const PetscScalar *varr;
39911a271a75SMatthew G. Knepley 
39921a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39931a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
39941a271a75SMatthew G. Knepley     varr = &vArray[off];
399597e99dd9SToby Isaac     if (clperm) {
399697e99dd9SToby Isaac       if (perm) {
399797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
39981a271a75SMatthew G. Knepley       } else {
399997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
400097e99dd9SToby Isaac       }
400197e99dd9SToby Isaac       if (flip) {
400297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
400397e99dd9SToby Isaac       }
400497e99dd9SToby Isaac     } else {
400597e99dd9SToby Isaac       if (perm) {
400697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
400797e99dd9SToby Isaac       } else {
400897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
400997e99dd9SToby Isaac       }
401097e99dd9SToby Isaac       if (flip) {
401197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
40121a271a75SMatthew G. Knepley       }
40131a271a75SMatthew G. Knepley     }
401497e99dd9SToby Isaac     offset += dof;
401597e99dd9SToby Isaac   }
401697e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
40171a271a75SMatthew G. Knepley   *size = offset;
40181a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
40191a271a75SMatthew G. Knepley }
40201a271a75SMatthew G. Knepley 
402197e99dd9SToby 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[])
40221a271a75SMatthew G. Knepley {
40231a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
40241a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
40251a271a75SMatthew G. Knepley 
40261a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4027fe02ba77SJed Brown   *size = 0;
40281a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
402997e99dd9SToby Isaac     PetscInt          p;
403097e99dd9SToby Isaac     const PetscInt    **perms = NULL;
403197e99dd9SToby Isaac     const PetscScalar **flips = NULL;
40321a271a75SMatthew G. Knepley 
403397e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
403497e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
403597e99dd9SToby Isaac       const PetscInt    point = points[2*p];
403697e99dd9SToby Isaac       PetscInt          fdof, foff, b;
40371a271a75SMatthew G. Knepley       const PetscScalar *varr;
403897e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
403997e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
40401a271a75SMatthew G. Knepley 
40411a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
40421a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
40431a271a75SMatthew G. Knepley       varr = &vArray[foff];
404497e99dd9SToby Isaac       if (clperm) {
404597e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
404697e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
404797e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
40481a271a75SMatthew G. Knepley       } else {
404997e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
405097e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
405197e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
40521a271a75SMatthew G. Knepley       }
405397e99dd9SToby Isaac       offset += fdof;
40541a271a75SMatthew G. Knepley     }
405597e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
40561a271a75SMatthew G. Knepley   }
40571a271a75SMatthew G. Knepley   *size = offset;
40581a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
40591a271a75SMatthew G. Knepley }
40601a271a75SMatthew G. Knepley 
4061552f7358SJed Brown /*@C
4062552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4063552f7358SJed Brown 
4064552f7358SJed Brown   Not collective
4065552f7358SJed Brown 
4066552f7358SJed Brown   Input Parameters:
4067552f7358SJed Brown + dm - The DM
4068552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4069552f7358SJed Brown . v - The local vector
407022c1ee49SMatthew G. Knepley . point - The point in the DM
407122c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
407222c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4073552f7358SJed Brown 
4074552f7358SJed Brown   Output Parameters:
407522c1ee49SMatthew G. Knepley + csize - The number of values in the closure
407622c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
407722c1ee49SMatthew G. Knepley 
407822c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
407922c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
408022c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
408122c1ee49SMatthew G. Knepley $
408222c1ee49SMatthew G. Knepley $ A typical use could be
408322c1ee49SMatthew G. Knepley $
408422c1ee49SMatthew G. Knepley $  values = NULL;
408522c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
408622c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
408722c1ee49SMatthew G. Knepley $    <Compute on closure>
408822c1ee49SMatthew G. Knepley $  }
408922c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
409022c1ee49SMatthew G. Knepley $
409122c1ee49SMatthew G. Knepley $ or
409222c1ee49SMatthew G. Knepley $
409322c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
409422c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
409522c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
409622c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
409722c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
409822c1ee49SMatthew G. Knepley $      <Compute on closure>
409922c1ee49SMatthew G. Knepley $    }
410022c1ee49SMatthew G. Knepley $  }
410122c1ee49SMatthew G. Knepley $  PetscFree(values);
4102552f7358SJed Brown 
4103552f7358SJed Brown   Fortran Notes:
4104552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4105552f7358SJed Brown   include petsc.h90 in your code.
4106552f7358SJed Brown 
4107552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4108552f7358SJed Brown 
4109552f7358SJed Brown   Level: intermediate
4110552f7358SJed Brown 
4111552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4112552f7358SJed Brown @*/
4113552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4114552f7358SJed Brown {
4115552f7358SJed Brown   PetscSection       clSection;
4116d9917b9dSMatthew G. Knepley   IS                 clPoints;
41178a84db2dSMatthew G. Knepley   PetscScalar       *array;
41188a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4119552f7358SJed Brown   PetscInt          *points = NULL;
41208f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
41211a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4122552f7358SJed Brown   PetscErrorCode     ierr;
4123552f7358SJed Brown 
4124d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4125552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4126552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
41271a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
41281a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4129552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4130552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4131552f7358SJed Brown   if (depth == 1 && numFields < 2) {
41321a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4133552f7358SJed Brown     PetscFunctionReturn(0);
4134552f7358SJed Brown   }
41351a271a75SMatthew G. Knepley   /* Get points */
4136923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41371fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
41381a271a75SMatthew G. Knepley   /* Get array */
4139bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
41401a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4141552f7358SJed Brown 
41421a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4143552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
41441a271a75SMatthew G. Knepley       asize += dof;
4145552f7358SJed Brown     }
4146bd6c0d32SMatthew G. Knepley     if (!values) {
4147923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41481a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4149bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4150bd6c0d32SMatthew G. Knepley     }
415169291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4152d0f6b257SMatthew G. Knepley   } else {
4153d0f6b257SMatthew G. Knepley     array = *values;
4154d0f6b257SMatthew G. Knepley   }
41558a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
41561a271a75SMatthew G. Knepley   /* Get values */
415797e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
415897e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
41591a271a75SMatthew G. Knepley   /* Cleanup points */
4160923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41611a271a75SMatthew G. Knepley   /* Cleanup array */
41628a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4163d0f6b257SMatthew G. Knepley   if (!*values) {
4164552f7358SJed Brown     if (csize) *csize = size;
4165552f7358SJed Brown     *values = array;
4166d0f6b257SMatthew G. Knepley   } else {
4167f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4168d0f6b257SMatthew G. Knepley     *csize = size;
4169d0f6b257SMatthew G. Knepley   }
4170552f7358SJed Brown   PetscFunctionReturn(0);
4171552f7358SJed Brown }
4172552f7358SJed Brown 
4173552f7358SJed Brown /*@C
4174552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4175552f7358SJed Brown 
4176552f7358SJed Brown   Not collective
4177552f7358SJed Brown 
4178552f7358SJed Brown   Input Parameters:
4179552f7358SJed Brown + dm - The DM
41800298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4181552f7358SJed Brown . v - The local vector
4182eaf898f9SPatrick Sanan . point - The point in the DM
41830298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4184552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4185552f7358SJed Brown 
418622c1ee49SMatthew 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()
418722c1ee49SMatthew G. Knepley 
41883813dfbdSMatthew G Knepley   Fortran Notes:
41893813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
41903813dfbdSMatthew G Knepley   include petsc.h90 in your code.
41913813dfbdSMatthew G Knepley 
41923813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
41933813dfbdSMatthew G Knepley 
4194552f7358SJed Brown   Level: intermediate
4195552f7358SJed Brown 
4196552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4197552f7358SJed Brown @*/
41987c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4199a6dfd86eSKarl Rupp {
4200552f7358SJed Brown   PetscInt       size = 0;
4201552f7358SJed Brown   PetscErrorCode ierr;
4202552f7358SJed Brown 
4203552f7358SJed Brown   PetscFunctionBegin;
4204552f7358SJed Brown   /* Should work without recalculating size */
420569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4206c9fdaa05SMatthew G. Knepley   *values = NULL;
4207552f7358SJed Brown   PetscFunctionReturn(0);
4208552f7358SJed Brown }
4209552f7358SJed Brown 
4210552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4211552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4212552f7358SJed Brown 
421397e99dd9SToby 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[])
4214552f7358SJed Brown {
4215552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4216552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4217552f7358SJed Brown   PetscScalar    *a;
4218552f7358SJed Brown   PetscInt        off, cind = 0, k;
4219552f7358SJed Brown   PetscErrorCode  ierr;
4220552f7358SJed Brown 
4221552f7358SJed Brown   PetscFunctionBegin;
4222552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4223552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4224552f7358SJed Brown   a    = &array[off];
4225552f7358SJed Brown   if (!cdof || setBC) {
422697e99dd9SToby Isaac     if (clperm) {
422797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
422897e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4229552f7358SJed Brown     } else {
423097e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
423197e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4232552f7358SJed Brown     }
4233552f7358SJed Brown   } else {
4234552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
423597e99dd9SToby Isaac     if (clperm) {
423697e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4237552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
423897e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4239552f7358SJed Brown         }
4240552f7358SJed Brown       } else {
4241552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4242552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
424397e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
424497e99dd9SToby Isaac         }
424597e99dd9SToby Isaac       }
424697e99dd9SToby Isaac     } else {
424797e99dd9SToby Isaac       if (perm) {
424897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
424997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
425097e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
425197e99dd9SToby Isaac         }
425297e99dd9SToby Isaac       } else {
425397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
425497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
425597e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
425697e99dd9SToby Isaac         }
4257552f7358SJed Brown       }
4258552f7358SJed Brown     }
4259552f7358SJed Brown   }
4260552f7358SJed Brown   PetscFunctionReturn(0);
4261552f7358SJed Brown }
4262552f7358SJed Brown 
426397e99dd9SToby 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[])
4264a5e93ea8SMatthew G. Knepley {
4265a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4266a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4267a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4268a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4269a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4270a5e93ea8SMatthew G. Knepley 
4271a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4272a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4273a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4274a5e93ea8SMatthew G. Knepley   a    = &array[off];
4275a5e93ea8SMatthew G. Knepley   if (cdof) {
4276a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
427797e99dd9SToby Isaac     if (clperm) {
427897e99dd9SToby Isaac       if (perm) {
4279a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4280a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
428197e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
428297e99dd9SToby Isaac             cind++;
4283a5e93ea8SMatthew G. Knepley           }
4284a5e93ea8SMatthew G. Knepley         }
4285a5e93ea8SMatthew G. Knepley       } else {
4286a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4287a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
428897e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
428997e99dd9SToby Isaac             cind++;
429097e99dd9SToby Isaac           }
429197e99dd9SToby Isaac         }
429297e99dd9SToby Isaac       }
429397e99dd9SToby Isaac     } else {
429497e99dd9SToby Isaac       if (perm) {
429597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
429697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
429797e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
429897e99dd9SToby Isaac             cind++;
429997e99dd9SToby Isaac           }
430097e99dd9SToby Isaac         }
430197e99dd9SToby Isaac       } else {
430297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
430397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
430497e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
430597e99dd9SToby Isaac             cind++;
430697e99dd9SToby Isaac           }
4307a5e93ea8SMatthew G. Knepley         }
4308a5e93ea8SMatthew G. Knepley       }
4309a5e93ea8SMatthew G. Knepley     }
4310a5e93ea8SMatthew G. Knepley   }
4311a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4312a5e93ea8SMatthew G. Knepley }
4313a5e93ea8SMatthew G. Knepley 
431497e99dd9SToby 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[])
4315a6dfd86eSKarl Rupp {
4316552f7358SJed Brown   PetscScalar    *a;
43171a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
43181a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
431997e99dd9SToby Isaac   PetscInt        cind = 0, b;
4320552f7358SJed Brown   PetscErrorCode  ierr;
4321552f7358SJed Brown 
4322552f7358SJed Brown   PetscFunctionBegin;
4323552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4324552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
43251a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43261a271a75SMatthew G. Knepley   a    = &array[foff];
4327552f7358SJed Brown   if (!fcdof || setBC) {
432897e99dd9SToby Isaac     if (clperm) {
432997e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
433097e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4331552f7358SJed Brown     } else {
433297e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
433397e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4334552f7358SJed Brown     }
4335552f7358SJed Brown   } else {
4336552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
433797e99dd9SToby Isaac     if (clperm) {
433897e99dd9SToby Isaac       if (perm) {
433997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
434097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
434197e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4342552f7358SJed Brown         }
4343552f7358SJed Brown       } else {
434497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
434597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
434697e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
434797e99dd9SToby Isaac         }
434897e99dd9SToby Isaac       }
434997e99dd9SToby Isaac     } else {
435097e99dd9SToby Isaac       if (perm) {
435197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
435297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
435397e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
435497e99dd9SToby Isaac         }
435597e99dd9SToby Isaac       } else {
435697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
435797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
435897e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4359552f7358SJed Brown         }
4360552f7358SJed Brown       }
4361552f7358SJed Brown     }
4362552f7358SJed Brown   }
43631a271a75SMatthew G. Knepley   *offset += fdof;
4364552f7358SJed Brown   PetscFunctionReturn(0);
4365552f7358SJed Brown }
4366552f7358SJed Brown 
4367ba322698SMatthew 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[])
4368a5e93ea8SMatthew G. Knepley {
4369a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
43701a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
43711a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4372ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4373ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4374a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4375a5e93ea8SMatthew G. Knepley 
4376a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4377a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4378a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
43791a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43801a271a75SMatthew G. Knepley   a    = &array[foff];
4381a5e93ea8SMatthew G. Knepley   if (fcdof) {
4382ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4383a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
438497e99dd9SToby Isaac     if (clperm) {
438597e99dd9SToby Isaac       if (perm) {
4386ba322698SMatthew G. Knepley         if (comps) {
4387ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4388ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4389ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4390ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4391ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4392ba322698SMatthew G. Knepley           }
4393ba322698SMatthew G. Knepley         } else {
439497e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
439597e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
439697e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4397a5e93ea8SMatthew G. Knepley               ++cind;
4398a5e93ea8SMatthew G. Knepley             }
4399a5e93ea8SMatthew G. Knepley           }
4400ba322698SMatthew G. Knepley         }
4401ba322698SMatthew G. Knepley       } else {
4402ba322698SMatthew G. Knepley         if (comps) {
4403ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4404ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4405ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4406ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4407ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4408ba322698SMatthew G. Knepley           }
4409a5e93ea8SMatthew G. Knepley         } else {
441097e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
441197e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
441297e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
441397e99dd9SToby Isaac               ++cind;
441497e99dd9SToby Isaac             }
441597e99dd9SToby Isaac           }
441697e99dd9SToby Isaac         }
4417ba322698SMatthew G. Knepley       }
441897e99dd9SToby Isaac     } else {
441997e99dd9SToby Isaac       if (perm) {
4420ba322698SMatthew G. Knepley         if (comps) {
4421ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4422ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4423ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4424ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4425ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4426ba322698SMatthew G. Knepley           }
4427ba322698SMatthew G. Knepley         } else {
442897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
442997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
443097e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
443197e99dd9SToby Isaac               ++cind;
443297e99dd9SToby Isaac             }
443397e99dd9SToby Isaac           }
4434ba322698SMatthew G. Knepley         }
4435ba322698SMatthew G. Knepley       } else {
4436ba322698SMatthew G. Knepley         if (comps) {
4437ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4438ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4439ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4440ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4441ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4442ba322698SMatthew G. Knepley           }
444397e99dd9SToby Isaac         } else {
444497e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
444597e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
444697e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4447a5e93ea8SMatthew G. Knepley               ++cind;
4448a5e93ea8SMatthew G. Knepley             }
4449a5e93ea8SMatthew G. Knepley           }
4450a5e93ea8SMatthew G. Knepley         }
4451a5e93ea8SMatthew G. Knepley       }
4452a5e93ea8SMatthew G. Knepley     }
4453ba322698SMatthew G. Knepley   }
44541a271a75SMatthew G. Knepley   *offset += fdof;
4455a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4456a5e93ea8SMatthew G. Knepley }
4457a5e93ea8SMatthew G. Knepley 
44588f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4459a6dfd86eSKarl Rupp {
4460552f7358SJed Brown   PetscScalar    *array;
44611b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
44621b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4463552f7358SJed Brown   PetscErrorCode  ierr;
4464552f7358SJed Brown 
44651b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4466b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4467b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4468b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4469b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4470b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4471b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4472b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4473b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4474b6ebb6e6SMatthew G. Knepley 
4475b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4476b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4477b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4478b6ebb6e6SMatthew G. Knepley     {
4479b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4480b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4481b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4482b6ebb6e6SMatthew G. Knepley 
4483b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4484b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4485b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4486b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4487b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4488b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4489b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4490b6ebb6e6SMatthew G. Knepley           }
4491b6ebb6e6SMatthew G. Knepley         } else {
4492b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4493b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4494b6ebb6e6SMatthew G. Knepley           }
4495b6ebb6e6SMatthew G. Knepley         }
4496b6ebb6e6SMatthew G. Knepley       } else {
4497b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4498b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4499b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4500b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4501b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4502b6ebb6e6SMatthew G. Knepley           }
4503b6ebb6e6SMatthew G. Knepley         } else {
4504b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4505b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4506b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4507b6ebb6e6SMatthew G. Knepley           }
4508b6ebb6e6SMatthew G. Knepley         }
4509b6ebb6e6SMatthew G. Knepley       }
4510b6ebb6e6SMatthew G. Knepley     }
4511b6ebb6e6SMatthew G. Knepley   }
4512b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4513b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4514b6ebb6e6SMatthew G. Knepley }
45151b406b76SMatthew G. Knepley 
45161b406b76SMatthew G. Knepley /*@C
45171b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
45181b406b76SMatthew G. Knepley 
45191b406b76SMatthew G. Knepley   Not collective
45201b406b76SMatthew G. Knepley 
45211b406b76SMatthew G. Knepley   Input Parameters:
45221b406b76SMatthew G. Knepley + dm - The DM
45231b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
45241b406b76SMatthew G. Knepley . v - The local vector
4525eaf898f9SPatrick Sanan . point - The point in the DM
45261b406b76SMatthew G. Knepley . values - The array of values
452722c1ee49SMatthew 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,
452822c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
45291b406b76SMatthew G. Knepley 
45301b406b76SMatthew G. Knepley   Fortran Notes:
45311b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
45321b406b76SMatthew G. Knepley 
45331b406b76SMatthew G. Knepley   Level: intermediate
45341b406b76SMatthew G. Knepley 
45351b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
45361b406b76SMatthew G. Knepley @*/
45371b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
45381b406b76SMatthew G. Knepley {
45391b406b76SMatthew G. Knepley   PetscSection    clSection;
45401b406b76SMatthew G. Knepley   IS              clPoints;
45411b406b76SMatthew G. Knepley   PetscScalar    *array;
45421b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
454397e99dd9SToby Isaac   const PetscInt *clp, *clperm;
45441a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
45451b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
45461b406b76SMatthew G. Knepley 
45471a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
45481b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45491b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
45501a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45511a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
45521b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
45531b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
45541b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
45558f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
45561b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
45571b406b76SMatthew G. Knepley   }
45581a271a75SMatthew G. Knepley   /* Get points */
455997e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4560923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
45611a271a75SMatthew G. Knepley   /* Get array */
4562552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
45631a271a75SMatthew G. Knepley   /* Get values */
4564ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
456597e99dd9SToby Isaac     PetscInt offset = 0, f;
4566552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
456797e99dd9SToby Isaac       const PetscInt    **perms = NULL;
456897e99dd9SToby Isaac       const PetscScalar **flips = NULL;
456997e99dd9SToby Isaac 
457097e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4571552f7358SJed Brown       switch (mode) {
4572552f7358SJed Brown       case INSERT_VALUES:
457397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
457497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
457597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
457697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
457797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4578552f7358SJed Brown         } break;
4579552f7358SJed Brown       case INSERT_ALL_VALUES:
458097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
458197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
458297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
458397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
458497e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4585552f7358SJed Brown         } break;
4586a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
458797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
458897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
458997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
459097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4591ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4592a5e93ea8SMatthew G. Knepley         } break;
4593552f7358SJed Brown       case ADD_VALUES:
459497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
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           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4599552f7358SJed Brown         } break;
4600552f7358SJed Brown       case ADD_ALL_VALUES:
460197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
460297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
460397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
460497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
460597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4606552f7358SJed Brown         } break;
4607304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
460897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
460997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
461097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
461197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4612ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4613304ab55fSMatthew G. Knepley         } break;
4614552f7358SJed Brown       default:
4615f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4616552f7358SJed Brown       }
461797e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
46181a271a75SMatthew G. Knepley     }
4619552f7358SJed Brown   } else {
46201a271a75SMatthew G. Knepley     PetscInt dof, off;
462197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
462297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
46231a271a75SMatthew G. Knepley 
462497e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4625552f7358SJed Brown     switch (mode) {
4626552f7358SJed Brown     case INSERT_VALUES:
462797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
462897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
462997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
463097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
463197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
463297e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4633552f7358SJed Brown       } break;
4634552f7358SJed Brown     case INSERT_ALL_VALUES:
463597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
463697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
463797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
463897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
463997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
464097e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4641552f7358SJed Brown       } break;
4642a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
464397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
464497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
464597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
464697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
464797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
464897e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4649a5e93ea8SMatthew G. Knepley       } break;
4650552f7358SJed Brown     case ADD_VALUES:
465197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
465297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
465397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
465497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
465597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
465697e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4657552f7358SJed Brown       } break;
4658552f7358SJed Brown     case ADD_ALL_VALUES:
465997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
466097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
466197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
466297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
466397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
466497e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4665552f7358SJed Brown       } break;
4666304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
466797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
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;
467197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
467297e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4673304ab55fSMatthew G. Knepley       } break;
4674552f7358SJed Brown     default:
4675f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4676552f7358SJed Brown     }
467797e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4678552f7358SJed Brown   }
46791a271a75SMatthew G. Knepley   /* Cleanup points */
4680923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
46811a271a75SMatthew G. Knepley   /* Cleanup array */
4682552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4683552f7358SJed Brown   PetscFunctionReturn(0);
4684552f7358SJed Brown }
4685552f7358SJed Brown 
4686ba322698SMatthew 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)
4687e07394fbSMatthew G. Knepley {
4688e07394fbSMatthew G. Knepley   PetscSection      clSection;
4689e07394fbSMatthew G. Knepley   IS                clPoints;
4690e07394fbSMatthew G. Knepley   PetscScalar       *array;
4691e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4692b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4693e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
469497e99dd9SToby Isaac   PetscInt          offset = 0, f;
4695e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4696e07394fbSMatthew G. Knepley 
4697e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4698e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4699e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4700e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4701e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4702e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4703e07394fbSMatthew G. Knepley   /* Get points */
4704b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4705923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4706e07394fbSMatthew G. Knepley   /* Get array */
4707e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4708e07394fbSMatthew G. Knepley   /* Get values */
4709e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
471097e99dd9SToby Isaac     const PetscInt    **perms = NULL;
471197e99dd9SToby Isaac     const PetscScalar **flips = NULL;
471297e99dd9SToby Isaac 
4713e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4714e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4715e07394fbSMatthew G. Knepley         PetscInt fdof;
4716e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4717e07394fbSMatthew G. Knepley         offset += fdof;
4718e07394fbSMatthew G. Knepley       }
4719e07394fbSMatthew G. Knepley       continue;
4720e07394fbSMatthew G. Knepley     }
472197e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4722e07394fbSMatthew G. Knepley     switch (mode) {
4723e07394fbSMatthew G. Knepley     case INSERT_VALUES:
472497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
472597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
472697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
472797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4728b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4729e07394fbSMatthew G. Knepley       } break;
4730e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
473197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
473297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
473397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
473497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4735b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4736e07394fbSMatthew G. Knepley         } break;
4737e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
473897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
473997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
474097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
474197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4742ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
4743e07394fbSMatthew G. Knepley       } break;
4744e07394fbSMatthew G. Knepley     case ADD_VALUES:
474597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
474697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
474797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
474897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4749b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4750e07394fbSMatthew G. Knepley       } break;
4751e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
475297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
475397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
475497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
475597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4756b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4757e07394fbSMatthew G. Knepley       } break;
4758e07394fbSMatthew G. Knepley     default:
4759f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4760e07394fbSMatthew G. Knepley     }
476197e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4762e07394fbSMatthew G. Knepley   }
4763e07394fbSMatthew G. Knepley   /* Cleanup points */
4764923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4765e07394fbSMatthew G. Knepley   /* Cleanup array */
4766e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4767e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4768e07394fbSMatthew G. Knepley }
4769e07394fbSMatthew G. Knepley 
47707cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4771552f7358SJed Brown {
4772552f7358SJed Brown   PetscMPIInt    rank;
4773552f7358SJed Brown   PetscInt       i, j;
4774552f7358SJed Brown   PetscErrorCode ierr;
4775552f7358SJed Brown 
4776552f7358SJed Brown   PetscFunctionBegin;
477782f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4778eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
4779e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4780e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4781b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4782b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4783e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4784b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4785519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
47867eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4787552f7358SJed Brown #else
4788b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4789552f7358SJed Brown #endif
4790552f7358SJed Brown     }
479177a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4792552f7358SJed Brown   }
4793552f7358SJed Brown   PetscFunctionReturn(0);
4794552f7358SJed Brown }
4795552f7358SJed Brown 
4796552f7358SJed Brown /* . off - The global offset of this point */
47974acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4798a6dfd86eSKarl Rupp {
4799e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4800552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4801552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4802552f7358SJed Brown   PetscInt        cind = 0, k;
4803552f7358SJed Brown   PetscErrorCode  ierr;
4804552f7358SJed Brown 
4805552f7358SJed Brown   PetscFunctionBegin;
4806552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4807552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4808552f7358SJed Brown   if (!cdof || setBC) {
48094acb8e1eSToby Isaac     if (perm) {
48104acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4811552f7358SJed Brown     } else {
48124acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4813552f7358SJed Brown     }
4814552f7358SJed Brown   } else {
4815552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
48164acb8e1eSToby Isaac     if (perm) {
48174acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
48184acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
48194acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
48204acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
48214acb8e1eSToby Isaac           ++cind;
48224acb8e1eSToby Isaac         } else {
48234acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
48244acb8e1eSToby Isaac         }
48254acb8e1eSToby Isaac       }
48264acb8e1eSToby Isaac     } else {
4827552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4828552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4829552f7358SJed Brown           /* Insert check for returning constrained indices */
4830e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4831552f7358SJed Brown           ++cind;
4832552f7358SJed Brown         } else {
4833e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4834552f7358SJed Brown         }
4835552f7358SJed Brown       }
4836552f7358SJed Brown     }
4837552f7358SJed Brown   }
4838e6ccafaeSMatthew G Knepley   *loff += dof;
4839552f7358SJed Brown   PetscFunctionReturn(0);
4840552f7358SJed Brown }
4841552f7358SJed Brown 
4842552f7358SJed Brown /* . off - The global offset of this point */
48434acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4844a6dfd86eSKarl Rupp {
4845552f7358SJed Brown   PetscInt       numFields, foff, f;
4846552f7358SJed Brown   PetscErrorCode ierr;
4847552f7358SJed Brown 
4848552f7358SJed Brown   PetscFunctionBegin;
4849552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4850552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
48514acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4852552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
48534acb8e1eSToby Isaac     PetscInt        cind = 0, b;
48544acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4855552f7358SJed Brown 
4856552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4857552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4858552f7358SJed Brown     if (!cfdof || setBC) {
48594acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
48604acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4861552f7358SJed Brown     } else {
4862552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
48634acb8e1eSToby Isaac       if (perm) {
48644acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
48654acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
48664acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4867552f7358SJed Brown             ++cind;
4868552f7358SJed Brown           } else {
48694acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4870552f7358SJed Brown           }
4871552f7358SJed Brown         }
4872552f7358SJed Brown       } else {
48734acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
48744acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
48754acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4876552f7358SJed Brown             ++cind;
4877552f7358SJed Brown           } else {
48784acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4879552f7358SJed Brown           }
4880552f7358SJed Brown         }
4881552f7358SJed Brown       }
4882552f7358SJed Brown     }
4883a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4884552f7358SJed Brown     foffs[f] += fdof;
4885552f7358SJed Brown   }
4886552f7358SJed Brown   PetscFunctionReturn(0);
4887552f7358SJed Brown }
4888552f7358SJed Brown 
48894acb8e1eSToby 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)
4890d3d1a6afSToby Isaac {
4891d3d1a6afSToby Isaac   Mat             cMat;
4892d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4893d3d1a6afSToby Isaac   IS              aIS;
4894d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4895d3d1a6afSToby Isaac   const PetscInt  *anchors;
4896e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4897d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4898d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4899d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4900d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4901d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4902d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4903d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
49046ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4905d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4906d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4907d3d1a6afSToby Isaac 
4908d3d1a6afSToby Isaac   PetscFunctionBegin;
4909d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4910d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4911d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4912d3d1a6afSToby Isaac 
4913a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4914d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4915d3d1a6afSToby Isaac   if (aSec) {
4916d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4917d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4918d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4919d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4920d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4921d3d1a6afSToby Isaac      * into the global matrix anyway) */
4922d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4923d3d1a6afSToby Isaac       PetscInt b    = points[p];
49244b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4925d3d1a6afSToby Isaac 
49264b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
49274b2f2278SToby Isaac       if (!bSecDof) {
49284b2f2278SToby Isaac         continue;
49294b2f2278SToby Isaac       }
4930d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4931d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4932d3d1a6afSToby Isaac       }
4933d3d1a6afSToby Isaac       if (bDof) {
4934d3d1a6afSToby Isaac         /* this point is constrained */
4935d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4936d3d1a6afSToby Isaac         PetscInt bOff, q;
4937d3d1a6afSToby Isaac 
4938d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4939d3d1a6afSToby Isaac         newNumPoints  += bDof;
4940d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4941d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4942d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4943d3d1a6afSToby Isaac           PetscInt aDof;
4944d3d1a6afSToby Isaac 
4945d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4946d3d1a6afSToby Isaac           newNumIndices += aDof;
4947d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4948d3d1a6afSToby Isaac             PetscInt fDof;
4949d3d1a6afSToby Isaac 
4950d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4951d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4952d3d1a6afSToby Isaac           }
4953d3d1a6afSToby Isaac         }
4954d3d1a6afSToby Isaac       }
4955d3d1a6afSToby Isaac       else {
4956d3d1a6afSToby Isaac         /* this point is not constrained */
4957d3d1a6afSToby Isaac         newNumPoints++;
49584b2f2278SToby Isaac         newNumIndices += bSecDof;
4959d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4960d3d1a6afSToby Isaac           PetscInt fDof;
4961d3d1a6afSToby Isaac 
4962d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4963d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4964d3d1a6afSToby Isaac         }
4965d3d1a6afSToby Isaac       }
4966d3d1a6afSToby Isaac     }
4967d3d1a6afSToby Isaac   }
4968d3d1a6afSToby Isaac   if (!anyConstrained) {
496972b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
497072b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
497172b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
497272b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
497372b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4974d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4975d3d1a6afSToby Isaac   }
4976d3d1a6afSToby Isaac 
49776ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
49786ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
49796ecaa68aSToby Isaac 
4980f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4981d3d1a6afSToby Isaac 
49826ecaa68aSToby Isaac   if (!outPoints && !outValues) {
49836ecaa68aSToby Isaac     if (offsets) {
49846ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
49856ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
49866ecaa68aSToby Isaac       }
49876ecaa68aSToby Isaac     }
49886ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
49896ecaa68aSToby Isaac     PetscFunctionReturn(0);
49906ecaa68aSToby Isaac   }
49916ecaa68aSToby Isaac 
4992f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4993d3d1a6afSToby Isaac 
4994f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4995d3d1a6afSToby Isaac 
4996d3d1a6afSToby Isaac   /* workspaces */
4997d3d1a6afSToby Isaac   if (numFields) {
4998d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
499969291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
500069291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5001d3d1a6afSToby Isaac     }
5002d3d1a6afSToby Isaac   }
5003d3d1a6afSToby Isaac   else {
500469291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
500569291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5006d3d1a6afSToby Isaac   }
5007d3d1a6afSToby Isaac 
5008d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5009d3d1a6afSToby Isaac   if (numFields) {
50104b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
50114b2f2278SToby Isaac 
5012d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5013d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
50144b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5015d3d1a6afSToby Isaac 
50164b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
50174b2f2278SToby Isaac       if (!bSecDof) {
50184b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
50194b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
50204b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
50214b2f2278SToby Isaac         }
50224b2f2278SToby Isaac         continue;
50234b2f2278SToby Isaac       }
5024d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5025d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5026d3d1a6afSToby Isaac       }
5027d3d1a6afSToby Isaac       if (bDof) {
5028d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5029d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5030d3d1a6afSToby Isaac 
5031d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5032d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5033d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5034d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5035d3d1a6afSToby Isaac             PetscInt aFDof;
5036d3d1a6afSToby Isaac 
5037d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5038d3d1a6afSToby Isaac             allFDof += aFDof;
5039d3d1a6afSToby Isaac           }
5040d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5041d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5042d3d1a6afSToby Isaac         }
5043d3d1a6afSToby Isaac       }
5044d3d1a6afSToby Isaac       else {
5045d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5046d3d1a6afSToby Isaac           PetscInt fDof;
5047d3d1a6afSToby Isaac 
5048d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5049d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5050d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5051d3d1a6afSToby Isaac         }
5052d3d1a6afSToby Isaac       }
5053d3d1a6afSToby Isaac     }
50544b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
50554b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
50564b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5057d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5058d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5059d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5060d3d1a6afSToby Isaac       }
506119f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
506219f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
506369291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5064d3d1a6afSToby Isaac     }
5065d3d1a6afSToby Isaac   }
5066d3d1a6afSToby Isaac   else {
5067d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5068d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
50694b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5070d3d1a6afSToby Isaac 
50714b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
50724b2f2278SToby Isaac       if (!bSecDof) {
50734b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
50744b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
50754b2f2278SToby Isaac         continue;
50764b2f2278SToby Isaac       }
5077d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5078d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5079d3d1a6afSToby Isaac       }
5080d3d1a6afSToby Isaac       if (bDof) {
50814b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5082d3d1a6afSToby Isaac 
5083d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5084d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5085d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5086d3d1a6afSToby Isaac 
5087d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5088d3d1a6afSToby Isaac           allDof += aDof;
5089d3d1a6afSToby Isaac         }
5090d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
50914b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5092d3d1a6afSToby Isaac       }
5093d3d1a6afSToby Isaac       else {
50944b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5095d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5096d3d1a6afSToby Isaac       }
5097d3d1a6afSToby Isaac     }
5098d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5099d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5100d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5101d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5102d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5103d3d1a6afSToby Isaac     }
510469291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5105d3d1a6afSToby Isaac   }
5106d3d1a6afSToby Isaac 
51076ecaa68aSToby Isaac   /* output arrays */
510869291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
51096ecaa68aSToby Isaac 
5110d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5111d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5112d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
511369291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
511469291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5115d3d1a6afSToby Isaac   if (numFields) {
5116d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5117d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5118d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
51194b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5120d3d1a6afSToby Isaac 
51214b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
51224b2f2278SToby Isaac       if (!bSecDof) {
51234b2f2278SToby Isaac         continue;
51244b2f2278SToby Isaac       }
5125d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5126d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5127d3d1a6afSToby Isaac       }
5128d3d1a6afSToby Isaac       if (bDof) {
5129d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5130d3d1a6afSToby Isaac 
5131d3d1a6afSToby Isaac         fStart[0] = 0;
5132d3d1a6afSToby Isaac         fEnd[0]   = 0;
5133d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5134d3d1a6afSToby Isaac           PetscInt fDof;
5135d3d1a6afSToby Isaac 
5136d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5137d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5138d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5139d3d1a6afSToby Isaac         }
5140d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
51414acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
5142d3d1a6afSToby Isaac 
5143d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5144d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5145d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5146d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5147d3d1a6afSToby Isaac 
5148d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5149d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5150d3d1a6afSToby Isaac         }
5151d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5152d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5153d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5154d3d1a6afSToby Isaac 
5155d3d1a6afSToby 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 */
5156d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5157d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5158302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
51594acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
5160d3d1a6afSToby Isaac         }
5161d3d1a6afSToby Isaac         newP += bDof;
5162d3d1a6afSToby Isaac 
51636ecaa68aSToby Isaac         if (outValues) {
5164d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5165d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5166d3d1a6afSToby 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);
5167d3d1a6afSToby Isaac           }
5168d3d1a6afSToby Isaac         }
51696ecaa68aSToby Isaac       }
5170d3d1a6afSToby Isaac       else {
5171d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5172d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5173d3d1a6afSToby Isaac         newP++;
5174d3d1a6afSToby Isaac       }
5175d3d1a6afSToby Isaac     }
5176d3d1a6afSToby Isaac   } else {
5177d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5178d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5179d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
51804b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5181d3d1a6afSToby Isaac 
51824b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
51834b2f2278SToby Isaac       if (!bSecDof) {
51844b2f2278SToby Isaac         continue;
51854b2f2278SToby Isaac       }
5186d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5187d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5188d3d1a6afSToby Isaac       }
5189d3d1a6afSToby Isaac       if (bDof) {
5190d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5191d3d1a6afSToby Isaac 
5192d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
51934acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
5194d3d1a6afSToby Isaac 
5195d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5196d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5197d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5198d3d1a6afSToby Isaac 
5199d3d1a6afSToby 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 */
5200d3d1a6afSToby Isaac 
5201d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5202d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5203302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
52044acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
5205d3d1a6afSToby Isaac         }
5206d3d1a6afSToby Isaac         newP += bDof;
5207d3d1a6afSToby Isaac 
5208d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
52096ecaa68aSToby Isaac         if (outValues) {
5210d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5211d3d1a6afSToby Isaac         }
52126ecaa68aSToby Isaac       }
5213d3d1a6afSToby Isaac       else {
5214d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5215d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5216d3d1a6afSToby Isaac         newP++;
5217d3d1a6afSToby Isaac       }
5218d3d1a6afSToby Isaac     }
5219d3d1a6afSToby Isaac   }
5220d3d1a6afSToby Isaac 
52216ecaa68aSToby Isaac   if (outValues) {
522269291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5223d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5224d3d1a6afSToby Isaac     /* multiply constraints on the right */
5225d3d1a6afSToby Isaac     if (numFields) {
5226d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5227d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5228d3d1a6afSToby Isaac 
5229d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5230d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5231d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5232d3d1a6afSToby Isaac           PetscInt c, r, k;
5233d3d1a6afSToby Isaac           PetscInt dof;
5234d3d1a6afSToby Isaac 
5235d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
52364b2f2278SToby Isaac           if (!dof) {
52374b2f2278SToby Isaac             continue;
52384b2f2278SToby Isaac           }
5239d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5240d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5241d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5242d3d1a6afSToby Isaac 
5243d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5244d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5245d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
52464acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5247d3d1a6afSToby Isaac                 }
5248d3d1a6afSToby Isaac               }
5249d3d1a6afSToby Isaac             }
5250d3d1a6afSToby Isaac           }
5251d3d1a6afSToby Isaac           else {
5252d3d1a6afSToby Isaac             /* copy this column as is */
5253d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5254d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5255d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5256d3d1a6afSToby Isaac               }
5257d3d1a6afSToby Isaac             }
5258d3d1a6afSToby Isaac           }
5259d3d1a6afSToby Isaac           oldOff += dof;
5260d3d1a6afSToby Isaac         }
5261d3d1a6afSToby Isaac       }
5262d3d1a6afSToby Isaac     }
5263d3d1a6afSToby Isaac     else {
5264d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5265d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5266d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5267d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5268d3d1a6afSToby Isaac         PetscInt c, r, k;
5269d3d1a6afSToby Isaac         PetscInt dof;
5270d3d1a6afSToby Isaac 
5271d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
52724b2f2278SToby Isaac         if (!dof) {
52734b2f2278SToby Isaac           continue;
52744b2f2278SToby Isaac         }
5275d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5276d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5277d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5278d3d1a6afSToby Isaac 
5279d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5280d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5281d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5282d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5283d3d1a6afSToby Isaac               }
5284d3d1a6afSToby Isaac             }
5285d3d1a6afSToby Isaac           }
5286d3d1a6afSToby Isaac         }
5287d3d1a6afSToby Isaac         else {
5288d3d1a6afSToby Isaac           /* copy this column as is */
5289d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5290d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5291d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5292d3d1a6afSToby Isaac             }
5293d3d1a6afSToby Isaac           }
5294d3d1a6afSToby Isaac         }
5295d3d1a6afSToby Isaac         oldOff += dof;
5296d3d1a6afSToby Isaac       }
5297d3d1a6afSToby Isaac     }
5298d3d1a6afSToby Isaac 
52996ecaa68aSToby Isaac     if (multiplyLeft) {
530069291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5301d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5302d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5303d3d1a6afSToby Isaac       if (numFields) {
5304d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5305d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5306d3d1a6afSToby Isaac 
5307d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5308d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5309d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5310d3d1a6afSToby Isaac             PetscInt c, r, k;
5311d3d1a6afSToby Isaac             PetscInt dof;
5312d3d1a6afSToby Isaac 
5313d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5314d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5315d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5316d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5317d3d1a6afSToby Isaac 
5318d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5319d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5320d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5321d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5322d3d1a6afSToby Isaac                   }
5323d3d1a6afSToby Isaac                 }
5324d3d1a6afSToby Isaac               }
5325d3d1a6afSToby Isaac             }
5326d3d1a6afSToby Isaac             else {
5327d3d1a6afSToby Isaac               /* copy this row as is */
5328d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5329d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5330d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5331d3d1a6afSToby Isaac                 }
5332d3d1a6afSToby Isaac               }
5333d3d1a6afSToby Isaac             }
5334d3d1a6afSToby Isaac             oldOff += dof;
5335d3d1a6afSToby Isaac           }
5336d3d1a6afSToby Isaac         }
5337d3d1a6afSToby Isaac       }
5338d3d1a6afSToby Isaac       else {
5339d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5340d3d1a6afSToby Isaac 
5341d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5342d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5343d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5344d3d1a6afSToby Isaac           PetscInt c, r, k;
5345d3d1a6afSToby Isaac           PetscInt dof;
5346d3d1a6afSToby Isaac 
5347d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5348d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5349d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5350d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5351d3d1a6afSToby Isaac 
5352d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5353d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5354d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5355d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5356d3d1a6afSToby Isaac                 }
5357d3d1a6afSToby Isaac               }
5358d3d1a6afSToby Isaac             }
5359d3d1a6afSToby Isaac           }
5360d3d1a6afSToby Isaac           else {
5361d3d1a6afSToby Isaac             /* copy this row as is */
53629fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5363d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5364d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5365d3d1a6afSToby Isaac               }
5366d3d1a6afSToby Isaac             }
5367d3d1a6afSToby Isaac           }
5368d3d1a6afSToby Isaac           oldOff += dof;
5369d3d1a6afSToby Isaac         }
5370d3d1a6afSToby Isaac       }
5371d3d1a6afSToby Isaac 
537269291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
53736ecaa68aSToby Isaac     }
53746ecaa68aSToby Isaac     else {
53756ecaa68aSToby Isaac       newValues = tmpValues;
53766ecaa68aSToby Isaac     }
53776ecaa68aSToby Isaac   }
53786ecaa68aSToby Isaac 
5379d3d1a6afSToby Isaac   /* clean up */
538069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
538169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
53826ecaa68aSToby Isaac 
5383d3d1a6afSToby Isaac   if (numFields) {
5384d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
538569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
538669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
538769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5388d3d1a6afSToby Isaac     }
5389d3d1a6afSToby Isaac   }
5390d3d1a6afSToby Isaac   else {
539169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
539269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
539369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5394d3d1a6afSToby Isaac   }
5395d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5396d3d1a6afSToby Isaac 
5397d3d1a6afSToby Isaac   /* output */
53986ecaa68aSToby Isaac   if (outPoints) {
5399d3d1a6afSToby Isaac     *outPoints = newPoints;
54006ecaa68aSToby Isaac   }
54016ecaa68aSToby Isaac   else {
540269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
54036ecaa68aSToby Isaac   }
540431620726SToby Isaac   if (outValues) {
5405d3d1a6afSToby Isaac     *outValues = newValues;
54066ecaa68aSToby Isaac   }
54076ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5408d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5409d3d1a6afSToby Isaac   }
5410d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5411d3d1a6afSToby Isaac }
5412d3d1a6afSToby Isaac 
54137cd05799SMatthew G. Knepley /*@C
5414a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
54157cd05799SMatthew G. Knepley 
54167cd05799SMatthew G. Knepley   Not collective
54177cd05799SMatthew G. Knepley 
54187cd05799SMatthew G. Knepley   Input Parameters:
54197cd05799SMatthew G. Knepley + dm - The DM
54207cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
54217cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
54227cd05799SMatthew G. Knepley - point - The mesh point
54237cd05799SMatthew G. Knepley 
54247cd05799SMatthew G. Knepley   Output parameters:
54257cd05799SMatthew G. Knepley + numIndices - The number of indices
54267cd05799SMatthew G. Knepley . indices - The indices
54277cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
54287cd05799SMatthew G. Knepley 
54297cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
54307cd05799SMatthew G. Knepley 
54317cd05799SMatthew G. Knepley   Level: advanced
54327cd05799SMatthew G. Knepley 
54337cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
54347cd05799SMatthew G. Knepley @*/
54356ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
54367773e69fSMatthew G. Knepley {
54377773e69fSMatthew G. Knepley   PetscSection    clSection;
54387773e69fSMatthew G. Knepley   IS              clPoints;
54397773e69fSMatthew G. Knepley   const PetscInt *clp;
54404acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
54417773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
54427773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
54437773e69fSMatthew G. Knepley   PetscInt        offsets[32];
54447773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
54457773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
54467773e69fSMatthew G. Knepley 
54477773e69fSMatthew G. Knepley   PetscFunctionBegin;
54487773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54497773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
54507773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
54517773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
54527773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
54537773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
54547773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
54557773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
54567773e69fSMatthew G. Knepley   /* Get points in closure */
5457923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54587773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
54597773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
54607773e69fSMatthew G. Knepley     PetscInt dof, fdof;
54617773e69fSMatthew G. Knepley 
54627773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
54637773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
54647773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
54657773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
54667773e69fSMatthew G. Knepley     }
54677773e69fSMatthew G. Knepley     Nind += dof;
54687773e69fSMatthew G. Knepley   }
54697773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
54708ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
54714acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
54724acb8e1eSToby Isaac   /* Get dual space symmetries */
54734acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
54744acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54754acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54764acb8e1eSToby Isaac   }
54777773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
54787773e69fSMatthew G. Knepley   {
54794acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
54807773e69fSMatthew G. Knepley     if (numPointsNew) {
54814acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
54824acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54834acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54844acb8e1eSToby Isaac       }
54854acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
54864acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
54874acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
54884acb8e1eSToby Isaac       }
5489923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54907773e69fSMatthew G. Knepley       numPoints = numPointsNew;
54917773e69fSMatthew G. Knepley       Nind      = NindNew;
54927773e69fSMatthew G. Knepley       points    = pointsNew;
54937773e69fSMatthew G. Knepley     }
54947773e69fSMatthew G. Knepley   }
54957773e69fSMatthew G. Knepley   /* Calculate indices */
549669291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
54977773e69fSMatthew G. Knepley   if (Nf) {
54986ecaa68aSToby Isaac     if (outOffsets) {
54996ecaa68aSToby Isaac       PetscInt f;
55006ecaa68aSToby Isaac 
550146bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
55026ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
55036ecaa68aSToby Isaac       }
55046ecaa68aSToby Isaac     }
55054acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
55064acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55074acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
55087773e69fSMatthew G. Knepley     }
55097773e69fSMatthew G. Knepley   } else {
55104acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
55114acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
55124acb8e1eSToby Isaac 
55134acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55144acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
55157773e69fSMatthew G. Knepley     }
55167773e69fSMatthew G. Knepley   }
55177773e69fSMatthew G. Knepley   /* Cleanup points */
55184acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
55194acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55204acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55214acb8e1eSToby Isaac   }
55227773e69fSMatthew G. Knepley   if (numPointsNew) {
552369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
55247773e69fSMatthew G. Knepley   } else {
5525923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55267773e69fSMatthew G. Knepley   }
55277773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
55287773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55297773e69fSMatthew G. Knepley }
55307773e69fSMatthew G. Knepley 
55317cd05799SMatthew G. Knepley /*@C
55327cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
55337cd05799SMatthew G. Knepley 
55347cd05799SMatthew G. Knepley   Not collective
55357cd05799SMatthew G. Knepley 
55367cd05799SMatthew G. Knepley   Input Parameters:
55377cd05799SMatthew G. Knepley + dm - The DM
55387cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
55397cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
55407cd05799SMatthew G. Knepley . point - The mesh point
55417cd05799SMatthew G. Knepley . numIndices - The number of indices
55427cd05799SMatthew G. Knepley . indices - The indices
55437cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
55447cd05799SMatthew G. Knepley 
55457cd05799SMatthew G. Knepley   Level: advanced
55467cd05799SMatthew G. Knepley 
55477cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
55487cd05799SMatthew G. Knepley @*/
554946bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
55507773e69fSMatthew G. Knepley {
55517773e69fSMatthew G. Knepley   PetscErrorCode ierr;
55527773e69fSMatthew G. Knepley 
55537773e69fSMatthew G. Knepley   PetscFunctionBegin;
55547773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55557773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
555669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
55577773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55587773e69fSMatthew G. Knepley }
55597773e69fSMatthew G. Knepley 
55607f5d1fdeSMatthew G. Knepley /*@C
55617f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
55627f5d1fdeSMatthew G. Knepley 
55637f5d1fdeSMatthew G. Knepley   Not collective
55647f5d1fdeSMatthew G. Knepley 
55657f5d1fdeSMatthew G. Knepley   Input Parameters:
55667f5d1fdeSMatthew G. Knepley + dm - The DM
5567ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5568ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
55697f5d1fdeSMatthew G. Knepley . A - The matrix
5570eaf898f9SPatrick Sanan . point - The point in the DM
55717f5d1fdeSMatthew G. Knepley . values - The array of values
55727f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
55737f5d1fdeSMatthew G. Knepley 
55747f5d1fdeSMatthew G. Knepley   Fortran Notes:
55757f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
55767f5d1fdeSMatthew G. Knepley 
55777f5d1fdeSMatthew G. Knepley   Level: intermediate
55787f5d1fdeSMatthew G. Knepley 
55797f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
55807f5d1fdeSMatthew G. Knepley @*/
55817c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5582552f7358SJed Brown {
5583552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
55841b406b76SMatthew G. Knepley   PetscSection        clSection;
55851b406b76SMatthew G. Knepley   IS                  clPoints;
5586d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
55871b406b76SMatthew G. Knepley   const PetscInt     *clp;
5588552f7358SJed Brown   PetscInt           *indices;
5589552f7358SJed Brown   PetscInt            offsets[32];
55904acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
55914acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5592923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
55934acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5594d3d1a6afSToby Isaac   PetscScalar        *newValues;
5595552f7358SJed Brown   PetscErrorCode      ierr;
5596552f7358SJed Brown 
5597552f7358SJed Brown   PetscFunctionBegin;
5598552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5599ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
56003dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5601ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
56023dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
56033dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5604552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
560582f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5606552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5607923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5608552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5609552f7358SJed Brown     PetscInt fdof;
5610552f7358SJed Brown 
5611552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5612552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5613552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5614552f7358SJed Brown       offsets[f+1] += fdof;
5615552f7358SJed Brown     }
5616552f7358SJed Brown     numIndices += dof;
5617552f7358SJed Brown   }
56180d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
56190d644c17SKarl Rupp 
56208ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
56214acb8e1eSToby Isaac   /* Get symmetries */
56224acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
56234acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56244acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56254acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
56264acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
56274acb8e1eSToby Isaac 
56284acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
56294acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
56304acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
56314acb8e1eSToby Isaac 
56324acb8e1eSToby Isaac         if (!numFields) {
56334acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
56344acb8e1eSToby Isaac         } else {
56354acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
56364acb8e1eSToby Isaac         }
56374acb8e1eSToby Isaac         if (flip) {
56384acb8e1eSToby Isaac           PetscInt i, j, k;
56394acb8e1eSToby Isaac 
56404acb8e1eSToby Isaac           if (!valCopy) {
564169291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56424acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
56434acb8e1eSToby Isaac             values = valCopy;
56444acb8e1eSToby Isaac           }
56454acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5646775f7be2SToby Isaac             PetscScalar fval = flip[i];
56474acb8e1eSToby Isaac 
56484acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
56494acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
56504acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
56514acb8e1eSToby Isaac             }
56524acb8e1eSToby Isaac           }
56534acb8e1eSToby Isaac         }
56544acb8e1eSToby Isaac         foffset += fdof;
56554acb8e1eSToby Isaac       }
56564acb8e1eSToby Isaac     }
56574acb8e1eSToby Isaac   }
56584acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5659d3d1a6afSToby Isaac   if (newNumPoints) {
56604acb8e1eSToby Isaac     if (valCopy) {
566169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56624acb8e1eSToby Isaac     }
56634acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56644acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56654acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56664acb8e1eSToby Isaac     }
56674acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56684acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56694acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56704acb8e1eSToby Isaac     }
5671923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5672d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5673d3d1a6afSToby Isaac     numIndices = newNumIndices;
5674d3d1a6afSToby Isaac     points     = newPoints;
5675d3d1a6afSToby Isaac     values     = newValues;
5676d3d1a6afSToby Isaac   }
567769291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5678552f7358SJed Brown   if (numFields) {
56794acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
56804acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
56814acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5682552f7358SJed Brown     }
5683552f7358SJed Brown   } else {
56844acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
56854acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
56864acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
56874acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5688552f7358SJed Brown     }
5689552f7358SJed Brown   }
5690b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5691552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5692ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5693ab1d0545SMatthew G. Knepley     PetscInt i;
5694ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
56958ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5696ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5697ab1d0545SMatthew G. Knepley   }
5698552f7358SJed Brown   if (ierr) {
5699552f7358SJed Brown     PetscMPIInt    rank;
5700552f7358SJed Brown     PetscErrorCode ierr2;
5701552f7358SJed Brown 
570282f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5703e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5704b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
570569291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
5706552f7358SJed Brown     CHKERRQ(ierr);
5707552f7358SJed Brown   }
57084acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
57094acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57104acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57114acb8e1eSToby Isaac   }
5712d3d1a6afSToby Isaac   if (newNumPoints) {
571369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
571469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
5715d3d1a6afSToby Isaac   }
5716d3d1a6afSToby Isaac   else {
57174acb8e1eSToby Isaac     if (valCopy) {
571869291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
57194acb8e1eSToby Isaac     }
5720923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5721d3d1a6afSToby Isaac   }
572269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5723552f7358SJed Brown   PetscFunctionReturn(0);
5724552f7358SJed Brown }
5725552f7358SJed Brown 
5726de41b84cSMatthew 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)
5727de41b84cSMatthew G. Knepley {
5728de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5729de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5730de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5731de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5732de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5733de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
57344ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5735de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5736de41b84cSMatthew G. Knepley 
5737de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5738de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5739de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5740de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5741de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5742de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5743de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5744de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5745de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5746de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5747de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5748de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5749de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5750de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5751de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5752de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5753de41b84cSMatthew G. Knepley   /* Column indices */
5754de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57554ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5756de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5757de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5758de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5759de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5760de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5761de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5762de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5763de41b84cSMatthew G. Knepley       ++q;
5764de41b84cSMatthew G. Knepley     }
5765de41b84cSMatthew G. Knepley   }
5766de41b84cSMatthew G. Knepley   numCPoints = q;
5767de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5768de41b84cSMatthew G. Knepley     PetscInt fdof;
5769de41b84cSMatthew G. Knepley 
5770de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
57714ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5772de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5773de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5774de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5775de41b84cSMatthew G. Knepley     }
5776de41b84cSMatthew G. Knepley     numCIndices += dof;
5777de41b84cSMatthew G. Knepley   }
5778de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5779de41b84cSMatthew G. Knepley   /* Row indices */
5780de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5781de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
578269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5783de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5784de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5785de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5786de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5787de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5788de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5789de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
57904ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
57914ca5e9f5SMatthew G. Knepley         if (!dof) continue;
57924ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
57934ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5794de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5795de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5796de41b84cSMatthew G. Knepley         ++q;
5797de41b84cSMatthew G. Knepley       }
5798de41b84cSMatthew G. Knepley     }
5799de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5800de41b84cSMatthew G. Knepley   }
5801de41b84cSMatthew G. Knepley   numFPoints = q;
5802de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5803de41b84cSMatthew G. Knepley     PetscInt fdof;
5804de41b84cSMatthew G. Knepley 
5805de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
58064ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5807de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5808de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5809de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5810de41b84cSMatthew G. Knepley     }
5811de41b84cSMatthew G. Knepley     numFIndices += dof;
5812de41b84cSMatthew G. Knepley   }
5813de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5814de41b84cSMatthew G. Knepley 
58158ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
58168ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
581769291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
581869291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5819de41b84cSMatthew G. Knepley   if (numFields) {
58204acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
58214acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
58224acb8e1eSToby Isaac 
58234acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58244acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58254acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5826de41b84cSMatthew G. Knepley     }
58274acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
58284acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
58294acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
58304acb8e1eSToby Isaac     }
58314acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
58324acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
58334acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
58344acb8e1eSToby Isaac     }
58354acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58364acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58374acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5838de41b84cSMatthew G. Knepley     }
5839de41b84cSMatthew G. Knepley   } else {
58404acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
58414acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
58424acb8e1eSToby Isaac 
58434acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58444acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
58454acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
58464acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
58474acb8e1eSToby Isaac 
58484acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
58494acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5850de41b84cSMatthew G. Knepley     }
58514acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
58524acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
58534acb8e1eSToby Isaac 
58544acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
58554acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5856de41b84cSMatthew G. Knepley     }
58574acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58584acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5859de41b84cSMatthew G. Knepley   }
5860de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
58614acb8e1eSToby Isaac   /* TODO: flips */
5862de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5863de41b84cSMatthew G. Knepley   if (ierr) {
5864de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5865de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5866de41b84cSMatthew G. Knepley 
5867de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5868e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5869de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
587069291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
587169291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
5872de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5873de41b84cSMatthew G. Knepley   }
587469291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5875de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
587669291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
587769291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5878de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5879de41b84cSMatthew G. Knepley }
5880de41b84cSMatthew G. Knepley 
58817c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
58827c927364SMatthew G. Knepley {
58837c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
58847c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
58857c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
58867c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
58877c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
58887c927364SMatthew G. Knepley   PetscErrorCode ierr;
58897c927364SMatthew G. Knepley 
58907c927364SMatthew G. Knepley   PetscFunctionBegin;
58917c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
58927c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
58937c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
58947c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
58957c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
58967c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
58977c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
58987c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
58997c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
59007c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
59017c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
59027c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
59037c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
59047c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
59057c927364SMatthew G. Knepley   /* Column indices */
59067c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
59077c927364SMatthew G. Knepley   maxFPoints = numCPoints;
59087c927364SMatthew G. Knepley   /* Compress out points not in the section */
59097c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
59107c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
59117c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
59127c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
59137c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
59147c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
59157c927364SMatthew G. Knepley       ++q;
59167c927364SMatthew G. Knepley     }
59177c927364SMatthew G. Knepley   }
59187c927364SMatthew G. Knepley   numCPoints = q;
59197c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
59207c927364SMatthew G. Knepley     PetscInt fdof;
59217c927364SMatthew G. Knepley 
59227c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
59237c927364SMatthew G. Knepley     if (!dof) continue;
59247c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59257c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
59267c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
59277c927364SMatthew G. Knepley     }
59287c927364SMatthew G. Knepley     numCIndices += dof;
59297c927364SMatthew G. Knepley   }
59307c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
59317c927364SMatthew G. Knepley   /* Row indices */
59327c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
59337c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
593469291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
59357c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
59367c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
59377c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59387c927364SMatthew G. Knepley     /* Compress out points not in the section */
59397c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
59407c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
59417c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
59427c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
59437c927364SMatthew G. Knepley         if (!dof) continue;
59447c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
59457c927364SMatthew G. Knepley         if (s < q) continue;
59467c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
59477c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
59487c927364SMatthew G. Knepley         ++q;
59497c927364SMatthew G. Knepley       }
59507c927364SMatthew G. Knepley     }
59517c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59527c927364SMatthew G. Knepley   }
59537c927364SMatthew G. Knepley   numFPoints = q;
59547c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
59557c927364SMatthew G. Knepley     PetscInt fdof;
59567c927364SMatthew G. Knepley 
59577c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
59587c927364SMatthew G. Knepley     if (!dof) continue;
59597c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59607c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
59617c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
59627c927364SMatthew G. Knepley     }
59637c927364SMatthew G. Knepley     numFIndices += dof;
59647c927364SMatthew G. Knepley   }
59657c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
59667c927364SMatthew G. Knepley 
59678ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
59688ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
59697c927364SMatthew G. Knepley   if (numFields) {
59704acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
59714acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
59724acb8e1eSToby Isaac 
59734acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
59744acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
59754acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
59767c927364SMatthew G. Knepley     }
59774acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
59784acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
59794acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
59804acb8e1eSToby Isaac     }
59814acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
59824acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
59834acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
59844acb8e1eSToby Isaac     }
59854acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
59864acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
59874acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
59887c927364SMatthew G. Knepley     }
59897c927364SMatthew G. Knepley   } else {
59904acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
59914acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
59924acb8e1eSToby Isaac 
59934acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59944acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
59954acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
59964acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
59974acb8e1eSToby Isaac 
59984acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
59994acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
60007c927364SMatthew G. Knepley     }
60014acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
60024acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
60034acb8e1eSToby Isaac 
60044acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
60054acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
60067c927364SMatthew G. Knepley     }
60074acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
60084acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
60097c927364SMatthew G. Knepley   }
601069291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
60117c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
60127c927364SMatthew G. Knepley   PetscFunctionReturn(0);
60137c927364SMatthew G. Knepley }
60147c927364SMatthew G. Knepley 
6015f96281f8SMatthew G. Knepley /*@
6016f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6017f96281f8SMatthew G. Knepley 
6018f96281f8SMatthew G. Knepley   Input Parameter:
6019f96281f8SMatthew G. Knepley . dm - The DMPlex object
6020f96281f8SMatthew G. Knepley 
6021f96281f8SMatthew G. Knepley   Output Parameters:
6022f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6023dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6024dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6025dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6026f96281f8SMatthew G. Knepley 
6027f96281f8SMatthew G. Knepley   Level: developer
6028f96281f8SMatthew G. Knepley 
6029dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6030f96281f8SMatthew G. Knepley @*/
6031770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6032552f7358SJed Brown {
6033552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6034770b213bSMatthew G Knepley   PetscInt       dim;
6035770b213bSMatthew G Knepley   PetscErrorCode ierr;
6036552f7358SJed Brown 
6037552f7358SJed Brown   PetscFunctionBegin;
6038552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6039c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6040770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
6041770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
6042770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6043770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6044552f7358SJed Brown   PetscFunctionReturn(0);
6045552f7358SJed Brown }
6046552f7358SJed Brown 
6047dc5a3409SMatthew G. Knepley /*@
6048dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6049dc5a3409SMatthew G. Knepley 
6050dc5a3409SMatthew G. Knepley   Input Parameters:
6051dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
6052dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6053dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6054dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6055dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6056dc5a3409SMatthew G. Knepley 
6057dc5a3409SMatthew G. Knepley   Level: developer
6058dc5a3409SMatthew G. Knepley 
6059dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6060dc5a3409SMatthew G. Knepley @*/
6061770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6062552f7358SJed Brown {
6063552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6064770b213bSMatthew G Knepley   PetscInt       dim;
6065770b213bSMatthew G Knepley   PetscErrorCode ierr;
6066552f7358SJed Brown 
6067552f7358SJed Brown   PetscFunctionBegin;
6068552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6069c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6070770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
6071770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
6072770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
6073770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
6074552f7358SJed Brown   PetscFunctionReturn(0);
6075552f7358SJed Brown }
6076552f7358SJed Brown 
60777cd05799SMatthew G. Knepley /*@C
60787cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
60797cd05799SMatthew G. Knepley 
60807cd05799SMatthew G. Knepley   Input Parameter:
60817cd05799SMatthew G. Knepley . dm   - The DMPlex object
60827cd05799SMatthew G. Knepley 
60837cd05799SMatthew G. Knepley   Output Parameter:
60847cd05799SMatthew G. Knepley . cellHeight - The height of a cell
60857cd05799SMatthew G. Knepley 
60867cd05799SMatthew G. Knepley   Level: developer
60877cd05799SMatthew G. Knepley 
60887cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
60897cd05799SMatthew G. Knepley @*/
6090552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6091552f7358SJed Brown {
6092552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6093552f7358SJed Brown 
6094552f7358SJed Brown   PetscFunctionBegin;
6095552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6096552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6097552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6098552f7358SJed Brown   PetscFunctionReturn(0);
6099552f7358SJed Brown }
6100552f7358SJed Brown 
61017cd05799SMatthew G. Knepley /*@C
61027cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
61037cd05799SMatthew G. Knepley 
61047cd05799SMatthew G. Knepley   Input Parameters:
61057cd05799SMatthew G. Knepley + dm   - The DMPlex object
61067cd05799SMatthew G. Knepley - cellHeight - The height of a cell
61077cd05799SMatthew G. Knepley 
61087cd05799SMatthew G. Knepley   Level: developer
61097cd05799SMatthew G. Knepley 
61107cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
61117cd05799SMatthew G. Knepley @*/
6112552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6113552f7358SJed Brown {
6114552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6115552f7358SJed Brown 
6116552f7358SJed Brown   PetscFunctionBegin;
6117552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6118552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6119552f7358SJed Brown   PetscFunctionReturn(0);
6120552f7358SJed Brown }
6121552f7358SJed Brown 
6122552f7358SJed Brown /* We can easily have a form that takes an IS instead */
6123ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6124552f7358SJed Brown {
6125552f7358SJed Brown   PetscSection   section, globalSection;
6126552f7358SJed Brown   PetscInt      *numbers, p;
6127552f7358SJed Brown   PetscErrorCode ierr;
6128552f7358SJed Brown 
6129552f7358SJed Brown   PetscFunctionBegin;
613082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6131552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6132552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6133552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6134552f7358SJed Brown   }
6135552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
613615b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6137854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6138552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6139552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6140ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6141ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6142552f7358SJed Brown   }
614382f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6144ef48cebcSMatthew G. Knepley   if (globalSize) {
6145ef48cebcSMatthew G. Knepley     PetscLayout layout;
6146ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6147ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6148ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6149ef48cebcSMatthew G. Knepley   }
6150552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6151552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6152552f7358SJed Brown   PetscFunctionReturn(0);
6153552f7358SJed Brown }
6154552f7358SJed Brown 
615581ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6156552f7358SJed Brown {
6157552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6158552f7358SJed Brown   PetscErrorCode ierr;
6159552f7358SJed Brown 
6160552f7358SJed Brown   PetscFunctionBegin;
6161552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6162552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
61630298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
616481ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
616581ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
616681ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6167552f7358SJed Brown }
616881ed3555SMatthew G. Knepley 
61697cd05799SMatthew G. Knepley /*@C
61707cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
61717cd05799SMatthew G. Knepley 
61727cd05799SMatthew G. Knepley   Input Parameter:
61737cd05799SMatthew G. Knepley . dm   - The DMPlex object
61747cd05799SMatthew G. Knepley 
61757cd05799SMatthew G. Knepley   Output Parameter:
61767cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
61777cd05799SMatthew G. Knepley 
61787cd05799SMatthew G. Knepley   Level: developer
61797cd05799SMatthew G. Knepley 
61807cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
61817cd05799SMatthew G. Knepley @*/
618281ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
618381ed3555SMatthew G. Knepley {
618481ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
618581ed3555SMatthew G. Knepley   PetscErrorCode ierr;
618681ed3555SMatthew G. Knepley 
618781ed3555SMatthew G. Knepley   PetscFunctionBegin;
618881ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
618981ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6190552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6191552f7358SJed Brown   PetscFunctionReturn(0);
6192552f7358SJed Brown }
6193552f7358SJed Brown 
619481ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
619581ed3555SMatthew G. Knepley {
619681ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
619781ed3555SMatthew G. Knepley   PetscErrorCode ierr;
619881ed3555SMatthew G. Knepley 
619981ed3555SMatthew G. Knepley   PetscFunctionBegin;
620081ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
620181ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
620281ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
620381ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
620481ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
620581ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
620681ed3555SMatthew G. Knepley }
620781ed3555SMatthew G. Knepley 
62087cd05799SMatthew G. Knepley /*@C
62097cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
62107cd05799SMatthew G. Knepley 
62117cd05799SMatthew G. Knepley   Input Parameter:
62127cd05799SMatthew G. Knepley . dm   - The DMPlex object
62137cd05799SMatthew G. Knepley 
62147cd05799SMatthew G. Knepley   Output Parameter:
62157cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
62167cd05799SMatthew G. Knepley 
62177cd05799SMatthew G. Knepley   Level: developer
62187cd05799SMatthew G. Knepley 
62197cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
62207cd05799SMatthew G. Knepley @*/
6221552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6222552f7358SJed Brown {
6223552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6224552f7358SJed Brown   PetscErrorCode ierr;
6225552f7358SJed Brown 
6226552f7358SJed Brown   PetscFunctionBegin;
6227552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
622881ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6229552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6230552f7358SJed Brown   PetscFunctionReturn(0);
6231552f7358SJed Brown }
6232552f7358SJed Brown 
62337cd05799SMatthew G. Knepley /*@C
62347cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
62357cd05799SMatthew G. Knepley 
62367cd05799SMatthew G. Knepley   Input Parameter:
62377cd05799SMatthew G. Knepley . dm   - The DMPlex object
62387cd05799SMatthew G. Knepley 
62397cd05799SMatthew G. Knepley   Output Parameter:
62407cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
62417cd05799SMatthew G. Knepley 
62427cd05799SMatthew G. Knepley   Level: developer
62437cd05799SMatthew G. Knepley 
62447cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
62457cd05799SMatthew G. Knepley @*/
6246ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6247ef48cebcSMatthew G. Knepley {
6248ef48cebcSMatthew G. Knepley   IS             nums[4];
6249ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
6250ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6251ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6252ef48cebcSMatthew G. Knepley 
6253ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6254ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6255ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
62568abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
62578abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6258ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
6259ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
6260ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6261ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6262ef48cebcSMatthew G. Knepley 
6263ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
6264ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6265ef48cebcSMatthew G. Knepley     shift += gsize;
6266ef48cebcSMatthew G. Knepley   }
6267302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6268ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6269ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6270ef48cebcSMatthew G. Knepley }
6271ef48cebcSMatthew G. Knepley 
627208a22f4bSMatthew G. Knepley 
627308a22f4bSMatthew G. Knepley /*@
627408a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
627508a22f4bSMatthew G. Knepley 
627608a22f4bSMatthew G. Knepley   Input Parameter:
627708a22f4bSMatthew G. Knepley . dm - The DMPlex object
627808a22f4bSMatthew G. Knepley 
627908a22f4bSMatthew G. Knepley   Output Parameter:
628008a22f4bSMatthew G. Knepley . ranks - The rank field
628108a22f4bSMatthew G. Knepley 
628208a22f4bSMatthew G. Knepley   Options Database Keys:
628308a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
628408a22f4bSMatthew G. Knepley 
628508a22f4bSMatthew G. Knepley   Level: intermediate
628608a22f4bSMatthew G. Knepley 
628708a22f4bSMatthew G. Knepley .seealso: DMView()
628808a22f4bSMatthew G. Knepley @*/
628908a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
629008a22f4bSMatthew G. Knepley {
629108a22f4bSMatthew G. Knepley   DM             rdm;
629208a22f4bSMatthew G. Knepley   PetscDS        prob;
629308a22f4bSMatthew G. Knepley   PetscFE        fe;
629408a22f4bSMatthew G. Knepley   PetscScalar   *r;
629508a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
629608a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
629708a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
629808a22f4bSMatthew G. Knepley 
629908a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
630008a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
630108a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
630208a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
630308a22f4bSMatthew G. Knepley   ierr = PetscFECreateDefault(rdm, dim, 1, PETSC_TRUE, NULL, -1, &fe);CHKERRQ(ierr);
630408a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
630508a22f4bSMatthew G. Knepley   ierr = DMGetDS(rdm, &prob);CHKERRQ(ierr);
630608a22f4bSMatthew G. Knepley   ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe);CHKERRQ(ierr);
630708a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
630808a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
630908a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
631008a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
631108a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
631208a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
631308a22f4bSMatthew G. Knepley     PetscScalar *lr;
631408a22f4bSMatthew G. Knepley 
631508a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
631608a22f4bSMatthew G. Knepley     *lr = rank;
631708a22f4bSMatthew G. Knepley   }
631808a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
631908a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
632008a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
632108a22f4bSMatthew G. Knepley }
632208a22f4bSMatthew G. Knepley 
6323ca8062c8SMatthew G. Knepley /*@
6324ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6325ca8062c8SMatthew G. Knepley 
632669916449SMatthew G. Knepley   Input Parameter:
632769916449SMatthew G. Knepley . dm - The DMPlex object
6328ca8062c8SMatthew G. Knepley 
6329ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6330ca8062c8SMatthew G. Knepley 
6331ca8062c8SMatthew G. Knepley   Level: developer
6332ca8062c8SMatthew G. Knepley 
63339bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
6334ca8062c8SMatthew G. Knepley @*/
6335ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6336ca8062c8SMatthew G. Knepley {
6337ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6338ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6339ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
6340ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
6341ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6342ca8062c8SMatthew G. Knepley 
6343ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6344ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6345ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6346ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6347ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6348ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6349ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6350ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6351ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6352ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
635342e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
635442e66dfaSMatthew G. Knepley       PetscInt  d;
635542e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
635642e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
635742e66dfaSMatthew G. Knepley       }
6358ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6359ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6360ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6361ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6362ca8062c8SMatthew G. Knepley       }
636342e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
63648ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6365ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
63668ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6367ca8062c8SMatthew G. Knepley         }
6368302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63698ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6370ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
63718ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6372ca8062c8SMatthew G. Knepley         }
6373302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63748ccfff9cSToby 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]);
63758ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6376ca8062c8SMatthew G. Knepley       }
637742e66dfaSMatthew G. Knepley     }
6378ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6379ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6380ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6381ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6382ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6383ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6384ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6385ca8062c8SMatthew G. Knepley       }
6386ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
63878ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6388ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
63898ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6390ca8062c8SMatthew G. Knepley         }
6391302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63928ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6393ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
63948ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6395ca8062c8SMatthew G. Knepley         }
6396302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63978ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6398ca8062c8SMatthew G. Knepley       }
6399ca8062c8SMatthew G. Knepley     }
6400ca8062c8SMatthew G. Knepley   }
6401ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6402ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
64038ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6404ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6405ca8062c8SMatthew G. Knepley }
6406ca8062c8SMatthew G. Knepley 
6407ca8062c8SMatthew G. Knepley /*@
6408ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6409ca8062c8SMatthew G. Knepley 
6410ca8062c8SMatthew G. Knepley   Input Parameters:
6411ca8062c8SMatthew G. Knepley + dm - The DMPlex object
641258723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
641358723a97SMatthew G. Knepley - cellHeight - Normally 0
6414ca8062c8SMatthew G. Knepley 
6415ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6416ca8062c8SMatthew G. Knepley 
6417ca8062c8SMatthew G. Knepley   Level: developer
6418ca8062c8SMatthew G. Knepley 
64199bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
6420ca8062c8SMatthew G. Knepley @*/
642158723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6422ca8062c8SMatthew G. Knepley {
642342363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6424ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6425ca8062c8SMatthew G. Knepley 
6426ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6427ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6428c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6429ca8062c8SMatthew G. Knepley   switch (dim) {
643042363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
643142363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
643242363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6433ca8062c8SMatthew G. Knepley   default:
64348ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6435ca8062c8SMatthew G. Knepley   }
643658723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
643758723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6438ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6439ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6440ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
644158723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
644258723a97SMatthew G. Knepley 
644358723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
644458723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
644558723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
644658723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
644758723a97SMatthew G. Knepley     }
644858723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
64498ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6450ca8062c8SMatthew G. Knepley   }
645142363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
645242363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
645342363296SMatthew G. Knepley 
645442363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
645542363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
645642363296SMatthew G. Knepley       const PetscInt p = closure[cl];
645742363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
645842363296SMatthew G. Knepley     }
645942363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
64608ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
646142363296SMatthew G. Knepley   }
6462ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6463ca8062c8SMatthew G. Knepley }
64649bf0dad6SMatthew G. Knepley 
64659bf0dad6SMatthew G. Knepley /*@
64669bf0dad6SMatthew 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
64679bf0dad6SMatthew G. Knepley 
64689bf0dad6SMatthew G. Knepley   Input Parameters:
64699bf0dad6SMatthew G. Knepley + dm - The DMPlex object
64709bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
64719bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
64729bf0dad6SMatthew G. Knepley 
64739bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
64749bf0dad6SMatthew G. Knepley 
64759bf0dad6SMatthew G. Knepley   Level: developer
64769bf0dad6SMatthew G. Knepley 
64779bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
64789bf0dad6SMatthew G. Knepley @*/
64799bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
64809bf0dad6SMatthew G. Knepley {
64813554e41dSMatthew G. Knepley   PetscInt       pMax[4];
64823554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
64839bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
64849bf0dad6SMatthew G. Knepley 
64859bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
64869bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6487c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
64889bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
648925abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
64903554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
64913554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
64923554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
64939bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
64949bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
64959bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
64969bf0dad6SMatthew G. Knepley 
649725abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
64989bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
64999bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
65009bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
65019bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65029bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
65039bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
65049bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
65059bf0dad6SMatthew G. Knepley       }
65063554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
65078ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
65089bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
65099bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
65109bf0dad6SMatthew G. Knepley 
65119bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
65129bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
65139bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
65149bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
65159bf0dad6SMatthew G. Knepley         }
65168ccfff9cSToby 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);
65179bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
65188ccfff9cSToby 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]);
65199bf0dad6SMatthew G. Knepley         }
65209bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
65219bf0dad6SMatthew G. Knepley       }
65229bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
65239bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65249bf0dad6SMatthew G. Knepley     }
65253554e41dSMatthew G. Knepley   }
6526552f7358SJed Brown   PetscFunctionReturn(0);
6527552f7358SJed Brown }
65283913d7c8SMatthew G. Knepley 
6529bceba477SMatthew G. Knepley /* Pointwise interpolation
6530bceba477SMatthew G. Knepley      Just code FEM for now
6531bceba477SMatthew G. Knepley      u^f = I u^c
65324ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
65334ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
65344ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6535bceba477SMatthew G. Knepley */
6536bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6537bceba477SMatthew G. Knepley {
6538bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6539bceba477SMatthew G. Knepley   PetscInt       m, n;
6540a063dac3SMatthew G. Knepley   void          *ctx;
654168132eb9SMatthew G. Knepley   DM             cdm;
654268132eb9SMatthew G. Knepley   PetscBool      regular;
6543bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6544bceba477SMatthew G. Knepley 
6545bceba477SMatthew G. Knepley   PetscFunctionBegin;
6546bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6547bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6548bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6549bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
655068132eb9SMatthew G. Knepley 
6551bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6552bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6553bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6554a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
655568132eb9SMatthew G. Knepley 
6556a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
655768132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
655868132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
655968132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
656068132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
65615d1c2e58SMatthew G. Knepley   /* Use naive scaling */
65625d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6563a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6564a063dac3SMatthew G. Knepley }
6565bceba477SMatthew G. Knepley 
65666dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6567a063dac3SMatthew G. Knepley {
656890748bafSMatthew G. Knepley   PetscErrorCode ierr;
65696dbf9973SLawrence Mitchell   VecScatter     ctx;
657090748bafSMatthew G. Knepley 
6571a063dac3SMatthew G. Knepley   PetscFunctionBegin;
65726dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
65736dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
65746dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6575bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6576bceba477SMatthew G. Knepley }
6577bceba477SMatthew G. Knepley 
6578bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
6579bd041c0cSMatthew G. Knepley {
6580bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
6581bd041c0cSMatthew G. Knepley   PetscInt       m, n;
6582bd041c0cSMatthew G. Knepley   void          *ctx;
6583bd041c0cSMatthew G. Knepley   DM             cdm;
6584bd041c0cSMatthew G. Knepley   PetscBool      regular;
6585bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
6586bd041c0cSMatthew G. Knepley 
6587bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
6588bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6589bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6590bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6591bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
6592bd041c0cSMatthew G. Knepley 
6593bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
6594bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6595bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
6596bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
6597bd041c0cSMatthew G. Knepley 
6598bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
6599bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
6600bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6601bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6602bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
6603bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
6604bd041c0cSMatthew G. Knepley }
6605bd041c0cSMatthew G. Knepley 
6606fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6607fd59a867SMatthew G. Knepley {
6608fd59a867SMatthew G. Knepley   PetscSection   section;
6609ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6610ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6611286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6612ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6613ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6614fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6615fd59a867SMatthew G. Knepley 
6616fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6617ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6618ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6619ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6620ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6621ebb3236fSMatthew G. Knepley     PetscObject  obj;
6622ebb3236fSMatthew G. Knepley     PetscClassId id;
6623ebb3236fSMatthew G. Knepley 
6624ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6625ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6626ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6627ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
66288ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6629ebb3236fSMatthew G. Knepley   }
66302f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6631286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6632c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6633ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6634ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6635dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6636fdafae62SVaclav 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)");
6637fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
66382f900235SMatthew G. Knepley     PetscInt                field;
6639f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6640385532a5SToby Isaac     const char             *labelName;
6641385532a5SToby Isaac     DMLabel                 label;
66422f900235SMatthew G. Knepley 
6643f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6644385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6645f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6646fd59a867SMatthew G. Knepley   }
66472f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6648ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
66496c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6650ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6651ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6652ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6653ebb3236fSMatthew G. Knepley 
6654ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6655ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6656ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6657ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6658ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6659ebb3236fSMatthew G. Knepley   }
6660ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6661ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6662fd59a867SMatthew G. Knepley     const char             *bdLabel;
6663fd59a867SMatthew G. Knepley     DMLabel                 label;
66640e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6665fd59a867SMatthew G. Knepley     const PetscInt         *values;
66660e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6667f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6668f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6669fd59a867SMatthew G. Knepley 
6670f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6671c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6672385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6673ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
66747391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
66757391c1cbSMatthew G. Knepley       const char *bdname;
6676dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
66777391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
66787391c1cbSMatthew G. Knepley       if (duplicate) break;
66797391c1cbSMatthew G. Knepley     }
6680ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6681b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6682092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
66837391c1cbSMatthew G. Knepley     }
6684ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6685f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
668693a5981aSMatthew G. Knepley       PetscInt       *newidx;
6687ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
668893a5981aSMatthew G. Knepley 
6689fd59a867SMatthew G. Knepley       bcFields[bc] = field;
66900e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6691ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6692ebb3236fSMatthew G. Knepley         IS              tmp;
6693ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6694ebb3236fSMatthew G. Knepley 
6695c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6696ebb3236fSMatthew G. Knepley         if (!tmp) continue;
669793a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
669893a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6699ebb3236fSMatthew G. Knepley         if (isFE[field]) {
670093a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6701ebb3236fSMatthew G. Knepley         } else {
6702ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6703ebb3236fSMatthew G. Knepley         }
670493a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
670593a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6706fd59a867SMatthew G. Knepley       }
6707ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6708ebb3236fSMatthew G. Knepley       newn = 0;
6709ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6710ebb3236fSMatthew G. Knepley         IS              tmp;
6711ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6712ebb3236fSMatthew G. Knepley 
6713c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6714ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6715ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6716ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6717ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6718ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6719ebb3236fSMatthew G. Knepley         } else {
6720ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6721ebb3236fSMatthew G. Knepley         }
6722ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6723ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6724ebb3236fSMatthew G. Knepley       }
6725ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6726ebb3236fSMatthew G. Knepley     }
6727fd59a867SMatthew G. Knepley   }
6728fd59a867SMatthew G. Knepley   /* Handle discretization */
6729ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6730fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
67310bacfa0aSMatthew G. Knepley     PetscObject obj;
67320bacfa0aSMatthew G. Knepley 
67330bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6734ebb3236fSMatthew G. Knepley     if (isFE[f]) {
67350bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6736286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6737fd59a867SMatthew G. Knepley       PetscInt        d;
6738fd59a867SMatthew G. Knepley 
6739fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6740286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6741286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6742ebb3236fSMatthew G. Knepley     } else {
67430bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
67440bacfa0aSMatthew G. Knepley 
67450bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
67460bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6747ebb3236fSMatthew G. Knepley     }
6748fd59a867SMatthew G. Knepley   }
6749286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6750286d8613SMatthew G. Knepley     PetscInt d;
6751286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6752286d8613SMatthew 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.");
6753286d8613SMatthew G. Knepley     }
6754286d8613SMatthew G. Knepley   }
6755ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6756fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6757fd59a867SMatthew G. Knepley     PetscFE     fe;
6758fd59a867SMatthew G. Knepley     const char *name;
675918abd763SMatthew G. Knepley 
676018abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6761fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6762fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6763fd59a867SMatthew G. Knepley   }
6764fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6765fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
67660e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
67670e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6768286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6769ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6770bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6771bceba477SMatthew G. Knepley }
6772bceba477SMatthew G. Knepley 
67730aef6b92SMatthew G. Knepley /*@
67740aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
67750aef6b92SMatthew G. Knepley 
67760aef6b92SMatthew G. Knepley   Input Parameter:
67770aef6b92SMatthew G. Knepley . dm - The DMPlex object
67780aef6b92SMatthew G. Knepley 
67790aef6b92SMatthew G. Knepley   Output Parameter:
67800aef6b92SMatthew G. Knepley . regular - The flag
67810aef6b92SMatthew G. Knepley 
67820aef6b92SMatthew G. Knepley   Level: intermediate
67830aef6b92SMatthew G. Knepley 
67840aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
67850aef6b92SMatthew G. Knepley @*/
67860aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
67870aef6b92SMatthew G. Knepley {
67880aef6b92SMatthew G. Knepley   PetscFunctionBegin;
67890aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67900aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
67910aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
67920aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
67930aef6b92SMatthew G. Knepley }
67940aef6b92SMatthew G. Knepley 
67950aef6b92SMatthew G. Knepley /*@
67960aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
67970aef6b92SMatthew G. Knepley 
67980aef6b92SMatthew G. Knepley   Input Parameters:
67990aef6b92SMatthew G. Knepley + dm - The DMPlex object
68000aef6b92SMatthew G. Knepley - regular - The flag
68010aef6b92SMatthew G. Knepley 
68020aef6b92SMatthew G. Knepley   Level: intermediate
68030aef6b92SMatthew G. Knepley 
68040aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
68050aef6b92SMatthew G. Knepley @*/
68060aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
68070aef6b92SMatthew G. Knepley {
68080aef6b92SMatthew G. Knepley   PetscFunctionBegin;
68090aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68100aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
68110aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
68120aef6b92SMatthew G. Knepley }
68130aef6b92SMatthew G. Knepley 
6814f7c74593SToby Isaac /* anchors */
6815a68b90caSToby Isaac /*@
6816f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6817f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6818a68b90caSToby Isaac 
6819e228b242SToby Isaac   not collective
6820a68b90caSToby Isaac 
6821a68b90caSToby Isaac   Input Parameters:
6822a68b90caSToby Isaac . dm - The DMPlex object
6823a68b90caSToby Isaac 
6824a68b90caSToby Isaac   Output Parameters:
6825a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6826a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6827a68b90caSToby Isaac 
6828a68b90caSToby Isaac 
6829a68b90caSToby Isaac   Level: intermediate
6830a68b90caSToby Isaac 
6831f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6832a68b90caSToby Isaac @*/
6833a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6834a68b90caSToby Isaac {
6835a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
683641e6d900SToby Isaac   PetscErrorCode ierr;
6837a68b90caSToby Isaac 
6838a68b90caSToby Isaac   PetscFunctionBegin;
6839a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
684041e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6841a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6842a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6843a68b90caSToby Isaac   PetscFunctionReturn(0);
6844a68b90caSToby Isaac }
6845a68b90caSToby Isaac 
6846a68b90caSToby Isaac /*@
6847f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6848f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6849a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6850a68b90caSToby Isaac 
6851a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6852f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6853a68b90caSToby Isaac 
6854e228b242SToby Isaac   collective on dm
6855a68b90caSToby Isaac 
6856a68b90caSToby Isaac   Input Parameters:
6857a68b90caSToby Isaac + dm - The DMPlex object
6858e228b242SToby 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).
6859e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6860a68b90caSToby Isaac 
6861a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6862a68b90caSToby Isaac 
6863a68b90caSToby Isaac   Level: intermediate
6864a68b90caSToby Isaac 
6865f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6866a68b90caSToby Isaac @*/
6867a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6868a68b90caSToby Isaac {
6869a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6870e228b242SToby Isaac   PetscMPIInt    result;
6871a68b90caSToby Isaac   PetscErrorCode ierr;
6872a68b90caSToby Isaac 
6873a68b90caSToby Isaac   PetscFunctionBegin;
6874a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6875e228b242SToby Isaac   if (anchorSection) {
6876e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6877e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6878f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6879e228b242SToby Isaac   }
6880e228b242SToby Isaac   if (anchorIS) {
6881e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6882e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6883f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6884e228b242SToby Isaac   }
6885a68b90caSToby Isaac 
6886a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6887a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6888a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6889a68b90caSToby Isaac 
6890a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6891a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6892a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6893a68b90caSToby Isaac 
6894a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6895a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6896a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6897a68b90caSToby Isaac     const PetscInt *anchors;
6898a68b90caSToby Isaac 
6899a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6900a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6901a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6902a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6903a68b90caSToby Isaac       PetscInt p;
6904a68b90caSToby Isaac 
6905a68b90caSToby Isaac       p = anchors[a];
6906a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6907a68b90caSToby Isaac         PetscInt dof;
6908a68b90caSToby Isaac 
6909a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6910a68b90caSToby Isaac         if (dof) {
6911a68b90caSToby Isaac           PetscErrorCode ierr2;
6912a68b90caSToby Isaac 
6913a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
69148ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6915a68b90caSToby Isaac         }
6916a68b90caSToby Isaac       }
6917a68b90caSToby Isaac     }
6918a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6919a68b90caSToby Isaac   }
6920a68b90caSToby Isaac #endif
6921f7c74593SToby Isaac   /* reset the generic constraints */
6922f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6923a68b90caSToby Isaac   PetscFunctionReturn(0);
6924a68b90caSToby Isaac }
6925a68b90caSToby Isaac 
6926f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6927a68b90caSToby Isaac {
6928f7c74593SToby Isaac   PetscSection anchorSection;
69296995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6930a68b90caSToby Isaac   PetscErrorCode ierr;
6931a68b90caSToby Isaac 
6932a68b90caSToby Isaac   PetscFunctionBegin;
6933a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6934a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6935e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6936a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
69376995de1eSToby Isaac   if (numFields) {
6938719ab38cSToby Isaac     PetscInt f;
6939a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6940719ab38cSToby Isaac 
6941719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6942719ab38cSToby Isaac       PetscInt numComp;
6943719ab38cSToby Isaac 
6944719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6945719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6946719ab38cSToby Isaac     }
69476995de1eSToby Isaac   }
6948a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
69496995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
69506995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
69516995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
69526995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6953a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6954a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6955a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6956a68b90caSToby Isaac     if (dof) {
6957a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6958a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6959a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6960a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6961a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6962a68b90caSToby Isaac       }
6963a68b90caSToby Isaac     }
6964a68b90caSToby Isaac   }
6965a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6966a68b90caSToby Isaac   PetscFunctionReturn(0);
6967a68b90caSToby Isaac }
6968a68b90caSToby Isaac 
6969f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6970a68b90caSToby Isaac {
6971f7c74593SToby Isaac   PetscSection aSec;
69720ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
69730ac89760SToby Isaac   const PetscInt *anchors;
69740ac89760SToby Isaac   PetscInt numFields, f;
697566ad2231SToby Isaac   IS aIS;
69760ac89760SToby Isaac   PetscErrorCode ierr;
69770ac89760SToby Isaac 
69780ac89760SToby Isaac   PetscFunctionBegin;
69790ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69800ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
69810ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
69820ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
69830ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6984302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6985a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
698666ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
69876995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
69886995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
69890ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
69900ac89760SToby Isaac   i[0] = 0;
69910ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
69920ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6993f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6994f19733c5SToby Isaac 
6995f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6996f19733c5SToby Isaac     if (!rDof) continue;
6997f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
69980ac89760SToby Isaac     if (numFields) {
69990ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
70000ac89760SToby Isaac         annz = 0;
7001f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7002f19733c5SToby Isaac           a = anchors[rOff + r];
70030ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
70040ac89760SToby Isaac           annz += aDof;
70050ac89760SToby Isaac         }
70060ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
70070ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
70080ac89760SToby Isaac         for (q = 0; q < dof; q++) {
70090ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
70100ac89760SToby Isaac         }
70110ac89760SToby Isaac       }
70120ac89760SToby Isaac     }
70130ac89760SToby Isaac     else {
70140ac89760SToby Isaac       annz = 0;
70150ac89760SToby Isaac       for (q = 0; q < dof; q++) {
70160ac89760SToby Isaac         a = anchors[off + q];
70170ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
70180ac89760SToby Isaac         annz += aDof;
70190ac89760SToby Isaac       }
70200ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
70210ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
70220ac89760SToby Isaac       for (q = 0; q < dof; q++) {
70230ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
70240ac89760SToby Isaac       }
70250ac89760SToby Isaac     }
70260ac89760SToby Isaac   }
70270ac89760SToby Isaac   nnz = i[m];
70280ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
70290ac89760SToby Isaac   offset = 0;
70300ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
70310ac89760SToby Isaac     if (numFields) {
70320ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
70330ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
70340ac89760SToby Isaac         for (q = 0; q < dof; q++) {
70350ac89760SToby Isaac           PetscInt rDof, rOff, r;
70360ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
70370ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
70380ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
70390ac89760SToby Isaac             PetscInt s;
70400ac89760SToby Isaac 
70410ac89760SToby Isaac             a = anchors[rOff + r];
70420ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
70430ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
70440ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
70450ac89760SToby Isaac               j[offset++] = aOff + s;
70460ac89760SToby Isaac             }
70470ac89760SToby Isaac           }
70480ac89760SToby Isaac         }
70490ac89760SToby Isaac       }
70500ac89760SToby Isaac     }
70510ac89760SToby Isaac     else {
70520ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
70530ac89760SToby Isaac       for (q = 0; q < dof; q++) {
70540ac89760SToby Isaac         PetscInt rDof, rOff, r;
70550ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
70560ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
70570ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
70580ac89760SToby Isaac           PetscInt s;
70590ac89760SToby Isaac 
70600ac89760SToby Isaac           a = anchors[rOff + r];
70610ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
70620ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
70630ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
70640ac89760SToby Isaac             j[offset++] = aOff + s;
70650ac89760SToby Isaac           }
70660ac89760SToby Isaac         }
70670ac89760SToby Isaac       }
70680ac89760SToby Isaac     }
70690ac89760SToby Isaac   }
70700ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
707125570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
707225570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
707366ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
70740ac89760SToby Isaac   PetscFunctionReturn(0);
70750ac89760SToby Isaac }
70760ac89760SToby Isaac 
707766ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
707866ad2231SToby Isaac {
7079f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7080f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
708166ad2231SToby Isaac   Mat            cMat;
708266ad2231SToby Isaac   PetscErrorCode ierr;
708366ad2231SToby Isaac 
708466ad2231SToby Isaac   PetscFunctionBegin;
708566ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7086a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
708766ad2231SToby Isaac   if (anchorSection) {
7088e228b242SToby Isaac     PetscDS  ds;
7089e228b242SToby Isaac     PetscInt nf;
7090e228b242SToby Isaac 
7091f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
7092f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7093f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
7094e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
7095302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
7096e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
709766ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
709866ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
709966ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
710066ad2231SToby Isaac   }
710166ad2231SToby Isaac   PetscFunctionReturn(0);
710266ad2231SToby Isaac }
7103