xref: /petsc/src/dm/impls/plex/plex.c (revision c8dd51e93b97f0fcd9978b3b91c2e4f08a0c6347)
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   }
114a99a26bcSAdrian Croucher   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 
268552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
269552f7358SJed Brown {
270552f7358SJed Brown   DM             dm;
2718135c375SStefano Zampini   PetscBool      isvtk, ishdf5, isdraw, isseq, isglvis;
272552f7358SJed Brown   PetscErrorCode ierr;
273552f7358SJed Brown 
274552f7358SJed Brown   PetscFunctionBegin;
275552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
27682f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
277552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
278b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
279f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
2808135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
281b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
2828135c375SStefano Zampini   if (isvtk || ishdf5 || isglvis) {
283ef31f671SMatthew G. Knepley     PetscInt  numFields;
284ef31f671SMatthew G. Knepley     PetscBool fem = PETSC_FALSE;
285ef31f671SMatthew G. Knepley 
286ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
287ef31f671SMatthew G. Knepley     if (numFields) {
288ef31f671SMatthew G. Knepley       PetscObject fe;
289ef31f671SMatthew G. Knepley 
290ef31f671SMatthew G. Knepley       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
291ef31f671SMatthew G. Knepley       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
292ef31f671SMatthew G. Knepley     }
293bdd6f66aSToby Isaac     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
294ef31f671SMatthew G. Knepley   }
295552f7358SJed Brown   if (isvtk) {
296552f7358SJed Brown     PetscSection            section;
297b136c2c9SMatthew G. Knepley     PetscViewerVTKFieldType ft;
298b136c2c9SMatthew G. Knepley     PetscInt                pStart, pEnd;
299552f7358SJed Brown 
300552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
3017afe7537SMatthew G. Knepley     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
302632d4749SBarry Smith     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
303552f7358SJed Brown     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
304b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
305b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
30639d25373SMatthew G. Knepley     ierr = VecView_Plex_Local_HDF5_Internal(v, viewer);CHKERRQ(ierr);
307b136c2c9SMatthew G. Knepley #else
308b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
309b136c2c9SMatthew G. Knepley #endif
3108135c375SStefano Zampini   } else if (isglvis) {
3118135c375SStefano Zampini     ierr = VecView_GLVis(v, viewer);CHKERRQ(ierr);
312f13a32a3SMatthew G. Knepley   } else if (isdraw) {
313f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local_Draw(v, viewer);CHKERRQ(ierr);
314552f7358SJed Brown   } else {
31555f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
31655f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
317552f7358SJed Brown   }
318552f7358SJed Brown   PetscFunctionReturn(0);
319552f7358SJed Brown }
320552f7358SJed Brown 
321552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
322552f7358SJed Brown {
323552f7358SJed Brown   DM             dm;
324f13a32a3SMatthew G. Knepley   PetscReal      time = 0.0;
3258135c375SStefano Zampini   PetscBool      isvtk, ishdf5, isdraw, isseq, isglvis;
326552f7358SJed Brown   PetscErrorCode ierr;
327552f7358SJed Brown 
328552f7358SJed Brown   PetscFunctionBegin;
329552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
33082f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
331552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
33233c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
333e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3348135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3358135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
33655f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
3378135c375SStefano Zampini   if (isvtk || isglvis) {
3388135c375SStefano Zampini     PetscInt    num;
339552f7358SJed Brown     Vec         locv;
340552f7358SJed Brown     const char *name;
341552f7358SJed Brown 
342*c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
343552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
344552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
345552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
346552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
3478135c375SStefano Zampini     ierr = DMGetOutputSequenceNumber(dm, &num, &time);CHKERRQ(ierr);
348cf9ba882SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
3498135c375SStefano Zampini     ierr = PetscViewerGLVisSetSnapId(viewer, num);CHKERRQ(ierr);
350552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
351*c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
352b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
353b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
35439d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
355b136c2c9SMatthew G. Knepley #else
356b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
357b136c2c9SMatthew G. Knepley #endif
358e412dcbdSMatthew G. Knepley   } else if (isdraw) {
359f13a32a3SMatthew G. Knepley     Vec         locv;
360f13a32a3SMatthew G. Knepley     const char *name;
361f13a32a3SMatthew G. Knepley 
362*c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
363f13a32a3SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
364f13a32a3SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
365f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
366f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
367f13a32a3SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
368f13a32a3SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
369f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
370*c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
371552f7358SJed Brown   } else {
37255f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
37355f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
374552f7358SJed Brown   }
375552f7358SJed Brown   PetscFunctionReturn(0);
376552f7358SJed Brown }
377552f7358SJed Brown 
378d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
379d930f514SMatthew G. Knepley {
380d930f514SMatthew G. Knepley   DM                dm;
381d930f514SMatthew G. Knepley   MPI_Comm          comm;
382d930f514SMatthew G. Knepley   PetscViewerFormat format;
383d930f514SMatthew G. Knepley   Vec               v;
384d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
385d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
386d930f514SMatthew G. Knepley 
387d930f514SMatthew G. Knepley   PetscFunctionBegin;
388d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
389d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
3902c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
391d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
392d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
393d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
394d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
395d930f514SMatthew G. Knepley     const char *vecname;
396d930f514SMatthew G. Knepley     PetscInt    n, nroots;
397d930f514SMatthew G. Knepley 
398d930f514SMatthew G. Knepley     if (dm->sfNatural) {
399ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
400d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
401d930f514SMatthew G. Knepley       if (n == nroots) {
402d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
403d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
404d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
405d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
406d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
407d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
408d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
409d930f514SMatthew G. Knepley   } else {
410d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
411d930f514SMatthew G. Knepley     v = originalv;
412d930f514SMatthew G. Knepley   }
413d930f514SMatthew G. Knepley   if (ishdf5) {
414d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
41539d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
416d930f514SMatthew G. Knepley #else
417d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
418d930f514SMatthew G. Knepley #endif
419d930f514SMatthew G. Knepley   } else if (isvtk) {
420d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
421d930f514SMatthew G. Knepley   } else {
422d930f514SMatthew G. Knepley     PetscBool isseq;
423d930f514SMatthew G. Knepley 
424d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
425d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
426d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
427d930f514SMatthew G. Knepley   }
428d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
429d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
430d930f514SMatthew G. Knepley }
431d930f514SMatthew G. Knepley 
4322c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4332c40f234SMatthew G. Knepley {
4342c40f234SMatthew G. Knepley   DM             dm;
4352c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4362c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4372c40f234SMatthew G. Knepley 
4382c40f234SMatthew G. Knepley   PetscFunctionBegin;
4392c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4402c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4412c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4422c40f234SMatthew G. Knepley   if (ishdf5) {
4432c40f234SMatthew G. Knepley     DM          dmBC;
4442c40f234SMatthew G. Knepley     Vec         gv;
4452c40f234SMatthew G. Knepley     const char *name;
4462c40f234SMatthew G. Knepley 
4472c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4482c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4492c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4502c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4512c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4522c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4532c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4542c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4552c40f234SMatthew G. Knepley   } else {
4562c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4572c40f234SMatthew G. Knepley   }
4582c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4592c40f234SMatthew G. Knepley }
4602c40f234SMatthew G. Knepley 
4612c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4622c40f234SMatthew G. Knepley {
4632c40f234SMatthew G. Knepley   DM             dm;
4642c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4652c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4662c40f234SMatthew G. Knepley 
4672c40f234SMatthew G. Knepley   PetscFunctionBegin;
4682c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4692c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4702c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4712c40f234SMatthew G. Knepley   if (ishdf5) {
472878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
47339d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
474b136c2c9SMatthew G. Knepley #else
475b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
476878b459fSMatthew G. Knepley #endif
4772c40f234SMatthew G. Knepley   } else {
4782c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
479552f7358SJed Brown   }
480552f7358SJed Brown   PetscFunctionReturn(0);
481552f7358SJed Brown }
482552f7358SJed Brown 
483d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
484d930f514SMatthew G. Knepley {
485d930f514SMatthew G. Knepley   DM                dm;
486d930f514SMatthew G. Knepley   PetscViewerFormat format;
487d930f514SMatthew G. Knepley   PetscBool         ishdf5;
488d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
489d930f514SMatthew G. Knepley 
490d930f514SMatthew G. Knepley   PetscFunctionBegin;
491d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
492d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
493d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
494d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
495d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
496d930f514SMatthew G. Knepley     if (dm->sfNatural) {
497d930f514SMatthew G. Knepley       if (ishdf5) {
498d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
499d930f514SMatthew G. Knepley         Vec         v;
500d930f514SMatthew G. Knepley         const char *vecname;
501d930f514SMatthew G. Knepley 
502d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
503d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
504d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
50539d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
506d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
507d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
508d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
509d930f514SMatthew G. Knepley #else
510d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
511d930f514SMatthew G. Knepley #endif
512d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
513d930f514SMatthew G. Knepley     }
514d930f514SMatthew G. Knepley   }
515d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
516d930f514SMatthew G. Knepley }
517d930f514SMatthew G. Knepley 
5187cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
519731e8ddeSMatthew G. Knepley {
520731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
521731e8ddeSMatthew G. Knepley   Vec                coordinates;
522731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
523731e8ddeSMatthew G. Knepley   const char        *name[4];
524731e8ddeSMatthew G. Knepley   const PetscScalar *a;
525731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
526731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
527731e8ddeSMatthew G. Knepley 
528731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
529731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
530731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
531731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
532731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
533731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
534731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
535731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
536731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
537731e8ddeSMatthew G. Knepley   name[1]     = "edge";
538731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
539731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
540731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
541731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
542731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
543731e8ddeSMatthew G. Knepley 
544f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
545731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
546731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
547731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
548731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
549731e8ddeSMatthew G. Knepley 
550731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
551731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
552731e8ddeSMatthew G. Knepley       if (!dof) continue;
553731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
554731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
555f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
556731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
557731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
559731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
56041477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
561731e8ddeSMatthew G. Knepley         }
562731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
563731e8ddeSMatthew G. Knepley       }
564731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
565731e8ddeSMatthew G. Knepley     }
566731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
567731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
568731e8ddeSMatthew G. Knepley   }
569731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
570731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
571731e8ddeSMatthew G. Knepley }
572731e8ddeSMatthew G. Knepley 
5737cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
574552f7358SJed Brown {
575552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
576552f7358SJed Brown   DM                cdm;
577552f7358SJed Brown   DMLabel           markers;
578552f7358SJed Brown   PetscSection      coordSection;
579552f7358SJed Brown   Vec               coordinates;
580552f7358SJed Brown   PetscViewerFormat format;
581552f7358SJed Brown   PetscErrorCode    ierr;
582552f7358SJed Brown 
583552f7358SJed Brown   PetscFunctionBegin;
584552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
585552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
586552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
587552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
588552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
589552f7358SJed Brown     const char *name;
590f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
591552f7358SJed Brown     PetscInt    pStart, pEnd, p;
592552f7358SJed Brown     PetscMPIInt rank, size;
593552f7358SJed Brown 
59482f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
59582f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
596552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
597552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
598552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
599f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
600f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
601f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
602f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
603f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
604552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
605552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
6064d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
6074d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
608552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
609552f7358SJed Brown       PetscInt dof, off, s;
610552f7358SJed Brown 
611552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
612552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
613552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
614e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
615552f7358SJed Brown       }
616552f7358SJed Brown     }
617552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
618552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
619552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
620552f7358SJed Brown       PetscInt dof, off, c;
621552f7358SJed Brown 
622552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
623552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
624552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
625e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
626552f7358SJed Brown       }
627552f7358SJed Brown     }
628552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6294d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6300298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
631552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
632c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
633552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
634552f7358SJed Brown     if (size > 1) {
635552f7358SJed Brown       PetscSF sf;
636552f7358SJed Brown 
637552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
638552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
639552f7358SJed Brown     }
640552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
641552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6420588280cSMatthew G. Knepley     const char  *name, *color;
6430588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6440588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
645552f7358SJed Brown     PetscReal    scale         = 2.0;
6460588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6470588280cSMatthew G. Knepley     double       tcoords[3];
648552f7358SJed Brown     PetscScalar *coords;
6490588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
650552f7358SJed Brown     PetscMPIInt  rank, size;
6510588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
652552f7358SJed Brown 
6530588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
654552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
655c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6560588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6570588280cSMatthew G. Knepley     numColors  = 10;
6580588280cSMatthew G. Knepley     numLColors = 10;
6590588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
660c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
661c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
662c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6630588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
664c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6650588280cSMatthew G. Knepley     if (!useColors) {
6660588280cSMatthew G. Knepley       numColors = 3;
6670588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6680588280cSMatthew G. Knepley     }
669c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6700588280cSMatthew G. Knepley     if (!useColors) {
6710588280cSMatthew G. Knepley       numLColors = 4;
6720588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
6730588280cSMatthew G. Knepley     }
67482f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
67582f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
676552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
677770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
6780588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
679552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
680552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
681552f7358SJed Brown \\usetikzlibrary{arrows}\n\
6820588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
6830588280cSMatthew G. Knepley     if (size > 1) {
684f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
685770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
686770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
687770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
688770b213bSMatthew G Knepley         } else if (p > 0) {
689770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
690770b213bSMatthew G Knepley         }
691770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
692770b213bSMatthew G Knepley       }
6930588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
6940588280cSMatthew G. Knepley     }
6950588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
696552f7358SJed Brown     /* Plot vertices */
697552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
698552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
6994d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
700552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
701552f7358SJed Brown       PetscInt  off, dof, d;
7020588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
703552f7358SJed Brown 
704552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
705552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7060588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
707f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7080588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7090588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
710c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7110588280cSMatthew G. Knepley       }
7120588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7130588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
714552f7358SJed Brown       for (d = 0; d < dof; ++d) {
715552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7160588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
717552f7358SJed Brown       }
7180588280cSMatthew G. Knepley       color = colors[rank%numColors];
7190588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7200588280cSMatthew G. Knepley         PetscInt val;
721c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7220588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7230588280cSMatthew G. Knepley       }
7240588280cSMatthew G. Knepley       if (useNumbers) {
725e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7260588280cSMatthew G. Knepley       } else {
727e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7280588280cSMatthew G. Knepley       }
729552f7358SJed Brown     }
730552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
731552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
732552f7358SJed Brown     /* Plot edges */
7330588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
7340588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
735552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
736552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
737552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
738552f7358SJed Brown         const PetscInt *cone;
739552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
740552f7358SJed Brown 
741552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
742f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
743552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
744552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
745552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
746552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
747552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
748552f7358SJed Brown         for (d = 0; d < dof; ++d) {
7490588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
750c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
751552f7358SJed Brown         }
7520588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
7530588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
7540588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
7550588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
756f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
7570588280cSMatthew G. Knepley         }
7580588280cSMatthew G. Knepley         color = colors[rank%numColors];
7590588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7600588280cSMatthew G. Knepley           PetscInt val;
761c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7620750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7630588280cSMatthew G. Knepley         }
764e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
765552f7358SJed Brown       }
766552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
767552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
768552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
7690588280cSMatthew G. Knepley     }
770552f7358SJed Brown     /* Plot cells */
7710588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
7720588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
7730588280cSMatthew G. Knepley         const PetscInt *cone;
7740588280cSMatthew G. Knepley 
7750588280cSMatthew G. Knepley         color = colors[rank%numColors];
7760588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7770588280cSMatthew G. Knepley           PetscInt val;
778c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
7790588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7800588280cSMatthew G. Knepley         }
7810588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
782e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
7830588280cSMatthew G. Knepley       }
7840588280cSMatthew G. Knepley     } else {
785552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
786552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
7870298fd71SBarry Smith         PetscInt *closure = NULL;
788552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
789552f7358SJed Brown 
790552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
791552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
792552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
793552f7358SJed Brown           const PetscInt point = closure[p];
794552f7358SJed Brown 
795552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
796552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
797e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
798552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
799552f7358SJed Brown         }
800552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
801e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
802552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
803552f7358SJed Brown       }
8040588280cSMatthew G. Knepley     }
805552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8064d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8070588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
808770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
8090588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
8100588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
8110588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
8120588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
813552f7358SJed Brown   } else {
81482f516ccSBarry Smith     MPI_Comm    comm;
815834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
816f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
817552f7358SJed Brown     PetscInt    pStart, pEnd, p;
818a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
8191143a3c0SMatthew G. Knepley     const char *name;
820552f7358SJed Brown     PetscMPIInt size;
821552f7358SJed Brown 
82282f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
823552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
824c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
825f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8265f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
827f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
828f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
829f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
830552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
831b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
832bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
833dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
834552f7358SJed Brown     if (depth == 1) {
835552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
836552f7358SJed Brown       pEnd = pEnd - pStart;
837552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
838f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
839552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
840552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
841552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
842552f7358SJed Brown       pEnd = pEnd - pStart;
843552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
844552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
845552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
846552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
847552f7358SJed Brown     } else {
848cbb7f117SMark Adams       PetscMPIInt rank;
849cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
850552f7358SJed Brown       for (d = 0; d <= dim; d++) {
851552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
852834065abSMatthew G. Knepley         pEnd    -= pStart;
853834065abSMatthew G. Knepley         pMax[d] -= pStart;
854552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
855834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
856552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
857834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
858cbb7f117SMark Adams           if (!rank) {
859834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
860834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
861834065abSMatthew G. Knepley           }
862cbb7f117SMark Adams         }
863552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
864552f7358SJed Brown       }
865552f7358SJed Brown     }
866834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
867c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
868a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
869a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
870a57dd577SMatthew G Knepley       DMLabel         label;
871a57dd577SMatthew G Knepley       const char     *name;
872a57dd577SMatthew G Knepley       IS              valueIS;
873a57dd577SMatthew G Knepley       const PetscInt *values;
874a57dd577SMatthew G Knepley       PetscInt        numValues, v;
875a57dd577SMatthew G Knepley 
876c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
877c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
878a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
879d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
880a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
881a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
882120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
883a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
884a57dd577SMatthew G Knepley         PetscInt size;
885a57dd577SMatthew G Knepley 
886a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
887a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
888d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
889a57dd577SMatthew G Knepley       }
890a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
891120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
892a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
8934d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
894a57dd577SMatthew G Knepley     }
895a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
8968e7ff633SMatthew G. Knepley     if (cdm) {
8978e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
8988e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
8998e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
9008e7ff633SMatthew G. Knepley     }
901552f7358SJed Brown   }
902552f7358SJed Brown   PetscFunctionReturn(0);
903552f7358SJed Brown }
904552f7358SJed Brown 
9057cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
906e412dcbdSMatthew G. Knepley {
907e412dcbdSMatthew G. Knepley   PetscDraw          draw;
908e412dcbdSMatthew G. Knepley   DM                 cdm;
909e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
910e412dcbdSMatthew G. Knepley   Vec                coordinates;
911e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
91229494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
913e412dcbdSMatthew G. Knepley   PetscBool          isnull;
914e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
915e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
916e412dcbdSMatthew G. Knepley 
917e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
918e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
919e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
920e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
921e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
922e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
923e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
924e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
925e412dcbdSMatthew G. Knepley 
926e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
927e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
928e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
929e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
930e412dcbdSMatthew G. Knepley 
931e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
932e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
933e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
9340c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
9350c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
936e412dcbdSMatthew G. Knepley   }
937e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
93829494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
93929494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
94029494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
941e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
942e412dcbdSMatthew G. Knepley 
943e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
944e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
94529494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
946e412dcbdSMatthew G. Knepley 
94729494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
948e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
94929494db1SLisandro Dalcin     switch (coneSize) {
95029494db1SLisandro Dalcin     case 3:
9510c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9520c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9530c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
954e412dcbdSMatthew G. Knepley       break;
95529494db1SLisandro Dalcin     case 4:
9560c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9570c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9580c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9590c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
960e412dcbdSMatthew G. Knepley       break;
96129494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
962e412dcbdSMatthew G. Knepley     }
963e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
964e412dcbdSMatthew G. Knepley   }
965e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
966e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
967e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
968e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
969e412dcbdSMatthew G. Knepley }
970e412dcbdSMatthew G. Knepley 
971552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
972552f7358SJed Brown {
973e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
974552f7358SJed Brown   PetscErrorCode    ierr;
975552f7358SJed Brown 
976552f7358SJed Brown   PetscFunctionBegin;
977552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
978552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
979552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
980fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
981c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
982e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
9838135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
984552f7358SJed Brown   if (iascii) {
9858135c375SStefano Zampini     PetscViewerFormat format;
9868135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
9878135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
9888135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
9898135c375SStefano Zampini     } else {
990552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
9918135c375SStefano Zampini     }
992c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
993c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
9947afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
99539d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
9967afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
997c6ccd67eSMatthew G. Knepley #else
998c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
999552f7358SJed Brown #endif
1000e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1001fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1002e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1003e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
10048135c375SStefano Zampini   } else if (isglvis) {
10058135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
1006fcf6c8fdSToby Isaac   }
1007cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1008cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1009cb3ba0daSMatthew G. Knepley   if (flg) {
1010cb3ba0daSMatthew G. Knepley     Vec ranks;
1011cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1012cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1013cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1014cb3ba0daSMatthew G. Knepley   }
1015552f7358SJed Brown   PetscFunctionReturn(0);
1016552f7358SJed Brown }
1017552f7358SJed Brown 
10182c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
10192c40f234SMatthew G. Knepley {
10202c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
10212c40f234SMatthew G. Knepley   PetscErrorCode ierr;
10222c40f234SMatthew G. Knepley 
10232c40f234SMatthew G. Knepley   PetscFunctionBegin;
10242c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10252c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
10262c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
10272c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
10282c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
10292c40f234SMatthew G. Knepley   else if (ishdf5) {
10302c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
103139d25373SMatthew G. Knepley     ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
10322c40f234SMatthew G. Knepley #else
10332c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1034552f7358SJed Brown #endif
1035552f7358SJed Brown   }
1036552f7358SJed Brown   PetscFunctionReturn(0);
1037552f7358SJed Brown }
1038552f7358SJed Brown 
1039552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1040552f7358SJed Brown {
1041552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1042552f7358SJed Brown   PetscErrorCode ierr;
1043552f7358SJed Brown 
1044552f7358SJed Brown   PetscFunctionBegin;
10458135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
10468135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
10470d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1048552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1049552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1050552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1051552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1052be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1053552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1054552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1055d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1056d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
105777623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1058a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1059552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1060552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1061a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1062a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1063d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1064d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1065d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1066d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1067d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1068d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1069fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1070552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1071552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1072552f7358SJed Brown   PetscFunctionReturn(0);
1073552f7358SJed Brown }
1074552f7358SJed Brown 
1075b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1076552f7358SJed Brown {
10778d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1078acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1079552f7358SJed Brown   PetscInt               localSize;
1080837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1081552f7358SJed Brown   PetscErrorCode         ierr;
1082b412c318SBarry Smith   MatType                mtype;
10831428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1084552f7358SJed Brown 
1085552f7358SJed Brown   PetscFunctionBegin;
1086607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1087b412c318SBarry Smith   mtype = dm->mattype;
1088552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1089552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1090552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
109182f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1092552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1093552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1094552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1095acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1096acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1097552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1098552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1099552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1100552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1101552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1102552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1103552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1104837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1105552f7358SJed Brown   if (!isShell) {
1106be36d101SStefano Zampini     PetscSection subSection;
1107837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
11080be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1109fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1110552f7358SJed Brown 
111100140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1112be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1113be36d101SStefano Zampini       PetscSection section;
1114be36d101SStefano Zampini       PetscInt     size;
1115be36d101SStefano Zampini 
1116be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1117be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1118be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1119be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1120be36d101SStefano Zampini     } else {
112100140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1122be36d101SStefano Zampini     }
1123552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1124be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1125a9d99c84SMatthew G. Knepley       PetscInt bdof;
1126a9d99c84SMatthew G. Knepley 
1127552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1128fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
11291d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
11301d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
11311d17a0a3SMatthew G. Knepley       if (dof) {
11321d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1133be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1134be36d101SStefano Zampini       }
1135be36d101SStefano Zampini       if (isMatIS) {
1136be36d101SStefano Zampini         PetscInt loff,c,off;
1137be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1138be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1139be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1140552f7358SJed Brown       }
11412a28c762SMatthew G Knepley     }
11422a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
11430be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11440be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11450be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11460be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
11474fa26246SMatthew G. Knepley     bs = bs < 0 ? 1 : bs;
1148be36d101SStefano Zampini     if (isMatIS) {
11494fa26246SMatthew G. Knepley       PetscInt l;
11504fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
11514fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
11524fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1153be36d101SStefano Zampini     }
1154be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1155be36d101SStefano Zampini     if (isMatIS) {
1156be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1157be36d101SStefano Zampini     }
11581795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
11598d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1160552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1161552f7358SJed Brown   }
1162b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1163552f7358SJed Brown   PetscFunctionReturn(0);
1164552f7358SJed Brown }
1165552f7358SJed Brown 
11667cd05799SMatthew G. Knepley /*@
1167a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1168be36d101SStefano Zampini 
1169be36d101SStefano Zampini   Not collective
1170be36d101SStefano Zampini 
1171be36d101SStefano Zampini   Input Parameter:
1172be36d101SStefano Zampini . mesh - The DMPlex
1173be36d101SStefano Zampini 
1174be36d101SStefano Zampini   Output Parameters:
1175be36d101SStefano Zampini . subsection - The subdomain section
1176be36d101SStefano Zampini 
1177be36d101SStefano Zampini   Level: developer
1178be36d101SStefano Zampini 
1179be36d101SStefano Zampini .seealso:
11807cd05799SMatthew G. Knepley @*/
1181be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1182be36d101SStefano Zampini {
1183be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1184be36d101SStefano Zampini   PetscErrorCode ierr;
1185be36d101SStefano Zampini 
1186be36d101SStefano Zampini   PetscFunctionBegin;
1187be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1188be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1189be36d101SStefano Zampini     PetscSection section;
1190be36d101SStefano Zampini     PetscSF      sf;
1191be36d101SStefano Zampini 
1192be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1193be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1194be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1195be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1196be36d101SStefano Zampini   }
1197be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1198be36d101SStefano Zampini   PetscFunctionReturn(0);
1199be36d101SStefano Zampini }
1200be36d101SStefano Zampini 
1201552f7358SJed Brown /*@
1202552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1203552f7358SJed Brown 
1204552f7358SJed Brown   Not collective
1205552f7358SJed Brown 
1206552f7358SJed Brown   Input Parameter:
1207552f7358SJed Brown . mesh - The DMPlex
1208552f7358SJed Brown 
1209552f7358SJed Brown   Output Parameters:
1210552f7358SJed Brown + pStart - The first mesh point
1211552f7358SJed Brown - pEnd   - The upper bound for mesh points
1212552f7358SJed Brown 
1213552f7358SJed Brown   Level: beginner
1214552f7358SJed Brown 
1215552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1216552f7358SJed Brown @*/
1217552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1218552f7358SJed Brown {
1219552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1220552f7358SJed Brown   PetscErrorCode ierr;
1221552f7358SJed Brown 
1222552f7358SJed Brown   PetscFunctionBegin;
1223552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1224552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1225552f7358SJed Brown   PetscFunctionReturn(0);
1226552f7358SJed Brown }
1227552f7358SJed Brown 
1228552f7358SJed Brown /*@
1229552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1230552f7358SJed Brown 
1231552f7358SJed Brown   Not collective
1232552f7358SJed Brown 
1233552f7358SJed Brown   Input Parameters:
1234552f7358SJed Brown + mesh - The DMPlex
1235552f7358SJed Brown . pStart - The first mesh point
1236552f7358SJed Brown - pEnd   - The upper bound for mesh points
1237552f7358SJed Brown 
1238552f7358SJed Brown   Output Parameters:
1239552f7358SJed Brown 
1240552f7358SJed Brown   Level: beginner
1241552f7358SJed Brown 
1242552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1243552f7358SJed Brown @*/
1244552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1245552f7358SJed Brown {
1246552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1247552f7358SJed Brown   PetscErrorCode ierr;
1248552f7358SJed Brown 
1249552f7358SJed Brown   PetscFunctionBegin;
1250552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1251552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1252552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1253552f7358SJed Brown   PetscFunctionReturn(0);
1254552f7358SJed Brown }
1255552f7358SJed Brown 
1256552f7358SJed Brown /*@
1257eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1258552f7358SJed Brown 
1259552f7358SJed Brown   Not collective
1260552f7358SJed Brown 
1261552f7358SJed Brown   Input Parameters:
1262552f7358SJed Brown + mesh - The DMPlex
1263eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1264552f7358SJed Brown 
1265552f7358SJed Brown   Output Parameter:
1266552f7358SJed Brown . size - The cone size for point p
1267552f7358SJed Brown 
1268552f7358SJed Brown   Level: beginner
1269552f7358SJed Brown 
1270552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1271552f7358SJed Brown @*/
1272552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1273552f7358SJed Brown {
1274552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1275552f7358SJed Brown   PetscErrorCode ierr;
1276552f7358SJed Brown 
1277552f7358SJed Brown   PetscFunctionBegin;
1278552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1279552f7358SJed Brown   PetscValidPointer(size, 3);
1280552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1281552f7358SJed Brown   PetscFunctionReturn(0);
1282552f7358SJed Brown }
1283552f7358SJed Brown 
1284552f7358SJed Brown /*@
1285eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1286552f7358SJed Brown 
1287552f7358SJed Brown   Not collective
1288552f7358SJed Brown 
1289552f7358SJed Brown   Input Parameters:
1290552f7358SJed Brown + mesh - The DMPlex
1291eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1292552f7358SJed Brown - size - The cone size for point p
1293552f7358SJed Brown 
1294552f7358SJed Brown   Output Parameter:
1295552f7358SJed Brown 
1296552f7358SJed Brown   Note:
1297552f7358SJed Brown   This should be called after DMPlexSetChart().
1298552f7358SJed Brown 
1299552f7358SJed Brown   Level: beginner
1300552f7358SJed Brown 
1301552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1302552f7358SJed Brown @*/
1303552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1304552f7358SJed Brown {
1305552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1306552f7358SJed Brown   PetscErrorCode ierr;
1307552f7358SJed Brown 
1308552f7358SJed Brown   PetscFunctionBegin;
1309552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1310552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
13110d644c17SKarl Rupp 
1312552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1313552f7358SJed Brown   PetscFunctionReturn(0);
1314552f7358SJed Brown }
1315552f7358SJed Brown 
1316f5a469b9SMatthew G. Knepley /*@
1317eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1318f5a469b9SMatthew G. Knepley 
1319f5a469b9SMatthew G. Knepley   Not collective
1320f5a469b9SMatthew G. Knepley 
1321f5a469b9SMatthew G. Knepley   Input Parameters:
1322f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1323eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1324f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1325f5a469b9SMatthew G. Knepley 
1326f5a469b9SMatthew G. Knepley   Output Parameter:
1327f5a469b9SMatthew G. Knepley 
1328f5a469b9SMatthew G. Knepley   Note:
1329f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1330f5a469b9SMatthew G. Knepley 
1331f5a469b9SMatthew G. Knepley   Level: beginner
1332f5a469b9SMatthew G. Knepley 
1333f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1334f5a469b9SMatthew G. Knepley @*/
1335f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1336f5a469b9SMatthew G. Knepley {
1337f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1338f5a469b9SMatthew G. Knepley   PetscInt       csize;
1339f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1340f5a469b9SMatthew G. Knepley 
1341f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1342f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1343f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1344f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1345f5a469b9SMatthew G. Knepley 
1346f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1347f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1348f5a469b9SMatthew G. Knepley }
1349f5a469b9SMatthew G. Knepley 
1350552f7358SJed Brown /*@C
1351eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1352552f7358SJed Brown 
1353552f7358SJed Brown   Not collective
1354552f7358SJed Brown 
1355552f7358SJed Brown   Input Parameters:
1356552f7358SJed Brown + mesh - The DMPlex
1357eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1358552f7358SJed Brown 
1359552f7358SJed Brown   Output Parameter:
1360552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1361552f7358SJed Brown 
1362552f7358SJed Brown   Level: beginner
1363552f7358SJed Brown 
13643813dfbdSMatthew G Knepley   Fortran Notes:
13653813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13663813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13673813dfbdSMatthew G Knepley 
13683813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1369552f7358SJed Brown 
1370552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1371552f7358SJed Brown @*/
1372552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1373552f7358SJed Brown {
1374552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1375552f7358SJed Brown   PetscInt       off;
1376552f7358SJed Brown   PetscErrorCode ierr;
1377552f7358SJed Brown 
1378552f7358SJed Brown   PetscFunctionBegin;
1379552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1380552f7358SJed Brown   PetscValidPointer(cone, 3);
1381552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1382552f7358SJed Brown   *cone = &mesh->cones[off];
1383552f7358SJed Brown   PetscFunctionReturn(0);
1384552f7358SJed Brown }
1385552f7358SJed Brown 
1386552f7358SJed Brown /*@
1387eaf898f9SPatrick Sanan   DMPlexSetCone - Set the points on the in-edges for this point in the DAG
1388552f7358SJed Brown 
1389552f7358SJed Brown   Not collective
1390552f7358SJed Brown 
1391552f7358SJed Brown   Input Parameters:
1392552f7358SJed Brown + mesh - The DMPlex
1393eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1394552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1395552f7358SJed Brown 
1396552f7358SJed Brown   Output Parameter:
1397552f7358SJed Brown 
1398552f7358SJed Brown   Note:
1399552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1400552f7358SJed Brown 
1401552f7358SJed Brown   Level: beginner
1402552f7358SJed Brown 
1403552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1404552f7358SJed Brown @*/
1405552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1406552f7358SJed Brown {
1407552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1408552f7358SJed Brown   PetscInt       pStart, pEnd;
1409552f7358SJed Brown   PetscInt       dof, off, c;
1410552f7358SJed Brown   PetscErrorCode ierr;
1411552f7358SJed Brown 
1412552f7358SJed Brown   PetscFunctionBegin;
1413552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1414552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1415552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1416552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1417552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
141882f516ccSBarry 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);
1419552f7358SJed Brown   for (c = 0; c < dof; ++c) {
142082f516ccSBarry 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);
1421552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1422552f7358SJed Brown   }
1423552f7358SJed Brown   PetscFunctionReturn(0);
1424552f7358SJed Brown }
1425552f7358SJed Brown 
1426552f7358SJed Brown /*@C
1427eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1428552f7358SJed Brown 
1429552f7358SJed Brown   Not collective
1430552f7358SJed Brown 
1431552f7358SJed Brown   Input Parameters:
1432552f7358SJed Brown + mesh - The DMPlex
1433eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1434552f7358SJed Brown 
1435552f7358SJed Brown   Output Parameter:
1436552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1437552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1438552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1439552f7358SJed Brown                     the index of the cone point on which to start.
1440552f7358SJed Brown 
1441552f7358SJed Brown   Level: beginner
1442552f7358SJed Brown 
14433813dfbdSMatthew G Knepley   Fortran Notes:
14443813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14453813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14463813dfbdSMatthew G Knepley 
14473813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1448552f7358SJed Brown 
1449552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1450552f7358SJed Brown @*/
1451552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1452552f7358SJed Brown {
1453552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1454552f7358SJed Brown   PetscInt       off;
1455552f7358SJed Brown   PetscErrorCode ierr;
1456552f7358SJed Brown 
1457552f7358SJed Brown   PetscFunctionBegin;
1458552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1459552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1460552f7358SJed Brown   {
1461552f7358SJed Brown     PetscInt dof;
1462552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1463552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1464552f7358SJed Brown   }
1465552f7358SJed Brown #endif
1466552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
14670d644c17SKarl Rupp 
1468552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1469552f7358SJed Brown   PetscFunctionReturn(0);
1470552f7358SJed Brown }
1471552f7358SJed Brown 
1472552f7358SJed Brown /*@
1473eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1474552f7358SJed Brown 
1475552f7358SJed Brown   Not collective
1476552f7358SJed Brown 
1477552f7358SJed Brown   Input Parameters:
1478552f7358SJed Brown + mesh - The DMPlex
1479eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1480552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1481552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1482552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1483552f7358SJed Brown                     the index of the cone point on which to start.
1484552f7358SJed Brown 
1485552f7358SJed Brown   Output Parameter:
1486552f7358SJed Brown 
1487552f7358SJed Brown   Note:
1488552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1489552f7358SJed Brown 
1490552f7358SJed Brown   Level: beginner
1491552f7358SJed Brown 
1492552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1493552f7358SJed Brown @*/
1494552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1495552f7358SJed Brown {
1496552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1497552f7358SJed Brown   PetscInt       pStart, pEnd;
1498552f7358SJed Brown   PetscInt       dof, off, c;
1499552f7358SJed Brown   PetscErrorCode ierr;
1500552f7358SJed Brown 
1501552f7358SJed Brown   PetscFunctionBegin;
1502552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1503552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1504552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1505552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1506552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
150782f516ccSBarry 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);
1508552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1509552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1510552f7358SJed Brown 
1511552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
151282f516ccSBarry 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);
1513552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1514552f7358SJed Brown   }
1515552f7358SJed Brown   PetscFunctionReturn(0);
1516552f7358SJed Brown }
1517552f7358SJed Brown 
15187cd05799SMatthew G. Knepley /*@
1519eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
15207cd05799SMatthew G. Knepley 
15217cd05799SMatthew G. Knepley   Not collective
15227cd05799SMatthew G. Knepley 
15237cd05799SMatthew G. Knepley   Input Parameters:
15247cd05799SMatthew G. Knepley + mesh - The DMPlex
1525eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15267cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15277cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
15287cd05799SMatthew G. Knepley 
15297cd05799SMatthew G. Knepley   Level: beginner
15307cd05799SMatthew G. Knepley 
15317cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15327cd05799SMatthew G. Knepley @*/
1533552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1534552f7358SJed Brown {
1535552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1536552f7358SJed Brown   PetscInt       pStart, pEnd;
1537552f7358SJed Brown   PetscInt       dof, off;
1538552f7358SJed Brown   PetscErrorCode ierr;
1539552f7358SJed Brown 
1540552f7358SJed Brown   PetscFunctionBegin;
1541552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1542552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
154382f516ccSBarry 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);
154482f516ccSBarry 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);
154577c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
154677c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
154777c88f5bSMatthew 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);
1548552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1549552f7358SJed Brown   PetscFunctionReturn(0);
1550552f7358SJed Brown }
1551552f7358SJed Brown 
15527cd05799SMatthew G. Knepley /*@
1553eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
15547cd05799SMatthew G. Knepley 
15557cd05799SMatthew G. Knepley   Not collective
15567cd05799SMatthew G. Knepley 
15577cd05799SMatthew G. Knepley   Input Parameters:
15587cd05799SMatthew G. Knepley + mesh - The DMPlex
1559eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15607cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15617cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
15627cd05799SMatthew G. Knepley 
15637cd05799SMatthew G. Knepley   Level: beginner
15647cd05799SMatthew G. Knepley 
15657cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15667cd05799SMatthew G. Knepley @*/
156777c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
156877c88f5bSMatthew G Knepley {
156977c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
157077c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
157177c88f5bSMatthew G Knepley   PetscInt       dof, off;
157277c88f5bSMatthew G Knepley   PetscErrorCode ierr;
157377c88f5bSMatthew G Knepley 
157477c88f5bSMatthew G Knepley   PetscFunctionBegin;
157577c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
157677c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
157777c88f5bSMatthew 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);
157877c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
157977c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
158077c88f5bSMatthew 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);
158177c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
158277c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
158377c88f5bSMatthew G Knepley }
158477c88f5bSMatthew G Knepley 
1585552f7358SJed Brown /*@
1586eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
1587552f7358SJed Brown 
1588552f7358SJed Brown   Not collective
1589552f7358SJed Brown 
1590552f7358SJed Brown   Input Parameters:
1591552f7358SJed Brown + mesh - The DMPlex
1592eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1593552f7358SJed Brown 
1594552f7358SJed Brown   Output Parameter:
1595552f7358SJed Brown . size - The support size for point p
1596552f7358SJed Brown 
1597552f7358SJed Brown   Level: beginner
1598552f7358SJed Brown 
1599552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1600552f7358SJed Brown @*/
1601552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1602552f7358SJed Brown {
1603552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1604552f7358SJed Brown   PetscErrorCode ierr;
1605552f7358SJed Brown 
1606552f7358SJed Brown   PetscFunctionBegin;
1607552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1608552f7358SJed Brown   PetscValidPointer(size, 3);
1609552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1610552f7358SJed Brown   PetscFunctionReturn(0);
1611552f7358SJed Brown }
1612552f7358SJed Brown 
1613552f7358SJed Brown /*@
1614eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
1615552f7358SJed Brown 
1616552f7358SJed Brown   Not collective
1617552f7358SJed Brown 
1618552f7358SJed Brown   Input Parameters:
1619552f7358SJed Brown + mesh - The DMPlex
1620eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1621552f7358SJed Brown - size - The support size for point p
1622552f7358SJed Brown 
1623552f7358SJed Brown   Output Parameter:
1624552f7358SJed Brown 
1625552f7358SJed Brown   Note:
1626552f7358SJed Brown   This should be called after DMPlexSetChart().
1627552f7358SJed Brown 
1628552f7358SJed Brown   Level: beginner
1629552f7358SJed Brown 
1630552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1631552f7358SJed Brown @*/
1632552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1633552f7358SJed Brown {
1634552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1635552f7358SJed Brown   PetscErrorCode ierr;
1636552f7358SJed Brown 
1637552f7358SJed Brown   PetscFunctionBegin;
1638552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1639552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
16400d644c17SKarl Rupp 
1641552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1642552f7358SJed Brown   PetscFunctionReturn(0);
1643552f7358SJed Brown }
1644552f7358SJed Brown 
1645552f7358SJed Brown /*@C
1646eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
1647552f7358SJed Brown 
1648552f7358SJed Brown   Not collective
1649552f7358SJed Brown 
1650552f7358SJed Brown   Input Parameters:
1651552f7358SJed Brown + mesh - The DMPlex
1652eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1653552f7358SJed Brown 
1654552f7358SJed Brown   Output Parameter:
1655552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1656552f7358SJed Brown 
1657552f7358SJed Brown   Level: beginner
1658552f7358SJed Brown 
16593813dfbdSMatthew G Knepley   Fortran Notes:
16603813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16613813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16623813dfbdSMatthew G Knepley 
16633813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
16643813dfbdSMatthew G Knepley 
1665552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1666552f7358SJed Brown @*/
1667552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1668552f7358SJed Brown {
1669552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1670552f7358SJed Brown   PetscInt       off;
1671552f7358SJed Brown   PetscErrorCode ierr;
1672552f7358SJed Brown 
1673552f7358SJed Brown   PetscFunctionBegin;
1674552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1675552f7358SJed Brown   PetscValidPointer(support, 3);
1676552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1677552f7358SJed Brown   *support = &mesh->supports[off];
1678552f7358SJed Brown   PetscFunctionReturn(0);
1679552f7358SJed Brown }
1680552f7358SJed Brown 
1681552f7358SJed Brown /*@
1682eaf898f9SPatrick Sanan   DMPlexSetSupport - Set the points on the out-edges for this point in the DAG
1683552f7358SJed Brown 
1684552f7358SJed Brown   Not collective
1685552f7358SJed Brown 
1686552f7358SJed Brown   Input Parameters:
1687552f7358SJed Brown + mesh - The DMPlex
1688eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1689552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1690552f7358SJed Brown 
1691552f7358SJed Brown   Output Parameter:
1692552f7358SJed Brown 
1693552f7358SJed Brown   Note:
1694552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1695552f7358SJed Brown 
1696552f7358SJed Brown   Level: beginner
1697552f7358SJed Brown 
1698552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1699552f7358SJed Brown @*/
1700552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1701552f7358SJed Brown {
1702552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1703552f7358SJed Brown   PetscInt       pStart, pEnd;
1704552f7358SJed Brown   PetscInt       dof, off, c;
1705552f7358SJed Brown   PetscErrorCode ierr;
1706552f7358SJed Brown 
1707552f7358SJed Brown   PetscFunctionBegin;
1708552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1709552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1710552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1711552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1712552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
171382f516ccSBarry 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);
1714552f7358SJed Brown   for (c = 0; c < dof; ++c) {
171582f516ccSBarry 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);
1716552f7358SJed Brown     mesh->supports[off+c] = support[c];
1717552f7358SJed Brown   }
1718552f7358SJed Brown   PetscFunctionReturn(0);
1719552f7358SJed Brown }
1720552f7358SJed Brown 
17217cd05799SMatthew G. Knepley /*@
1722eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
17237cd05799SMatthew G. Knepley 
17247cd05799SMatthew G. Knepley   Not collective
17257cd05799SMatthew G. Knepley 
17267cd05799SMatthew G. Knepley   Input Parameters:
17277cd05799SMatthew G. Knepley + mesh - The DMPlex
1728eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17297cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
17307cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
17317cd05799SMatthew G. Knepley 
17327cd05799SMatthew G. Knepley   Level: beginner
17337cd05799SMatthew G. Knepley 
17347cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17357cd05799SMatthew G. Knepley @*/
1736552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1737552f7358SJed Brown {
1738552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1739552f7358SJed Brown   PetscInt       pStart, pEnd;
1740552f7358SJed Brown   PetscInt       dof, off;
1741552f7358SJed Brown   PetscErrorCode ierr;
1742552f7358SJed Brown 
1743552f7358SJed Brown   PetscFunctionBegin;
1744552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1745552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1746552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1747552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
174882f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
174982f516ccSBarry 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);
175082f516ccSBarry 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);
1751552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1752552f7358SJed Brown   PetscFunctionReturn(0);
1753552f7358SJed Brown }
1754552f7358SJed Brown 
1755552f7358SJed Brown /*@C
1756eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
1757552f7358SJed Brown 
1758552f7358SJed Brown   Not collective
1759552f7358SJed Brown 
1760552f7358SJed Brown   Input Parameters:
1761552f7358SJed Brown + mesh - The DMPlex
1762eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1763552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17640298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1765552f7358SJed Brown 
1766552f7358SJed Brown   Output Parameters:
1767552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1768552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1769552f7358SJed Brown 
1770552f7358SJed Brown   Note:
17710298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1772552f7358SJed Brown 
17733813dfbdSMatthew G Knepley   Fortran Notes:
17743813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17753813dfbdSMatthew G Knepley   include petsc.h90 in your code.
17763813dfbdSMatthew G Knepley 
17773813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17783813dfbdSMatthew G Knepley 
1779552f7358SJed Brown   Level: beginner
1780552f7358SJed Brown 
1781552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1782552f7358SJed Brown @*/
1783552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1784552f7358SJed Brown {
1785552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1786552f7358SJed Brown   PetscInt       *closure, *fifo;
17870298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1788552f7358SJed Brown   PetscInt        tmpSize, t;
1789552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1790552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1791552f7358SJed Brown   PetscErrorCode  ierr;
1792552f7358SJed Brown 
1793552f7358SJed Brown   PetscFunctionBegin;
1794552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1795552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1796552f7358SJed Brown   /* This is only 1-level */
1797552f7358SJed Brown   if (useCone) {
1798552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1799552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1800552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1801552f7358SJed Brown   } else {
1802552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1803552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1804552f7358SJed Brown   }
1805bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1806bfbcdd7aSMatthew G. Knepley     if (*points) {
1807bfbcdd7aSMatthew G. Knepley       closure = *points;
1808bfbcdd7aSMatthew G. Knepley     } else {
1809bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
181069291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1811bfbcdd7aSMatthew G. Knepley     }
1812bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1813bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1814bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1815bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1816bfbcdd7aSMatthew G. Knepley     }
1817bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1818bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1819bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1820bfbcdd7aSMatthew G. Knepley   }
182124c766afSToby Isaac   {
182224c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
182324c766afSToby Isaac 
182424c766afSToby Isaac     c = mesh->maxConeSize;
182524c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
182624c766afSToby Isaac     s = mesh->maxSupportSize;
182724c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
182824c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
182924c766afSToby Isaac   }
183069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1831bfbcdd7aSMatthew G. Knepley   if (*points) {
1832bfbcdd7aSMatthew G. Knepley     closure = *points;
1833bfbcdd7aSMatthew G. Knepley   } else {
183469291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1835bfbcdd7aSMatthew G. Knepley   }
1836bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1837552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1838552f7358SJed Brown     const PetscInt cp = tmp[t];
1839552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1840552f7358SJed Brown 
1841552f7358SJed Brown     closure[closureSize]   = cp;
1842552f7358SJed Brown     closure[closureSize+1] = co;
1843552f7358SJed Brown     fifo[fifoSize]         = cp;
1844552f7358SJed Brown     fifo[fifoSize+1]       = co;
1845552f7358SJed Brown   }
1846bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1847552f7358SJed Brown   while (fifoSize - fifoStart) {
1848552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1849552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1850552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1851552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1852552f7358SJed Brown 
1853552f7358SJed Brown     if (useCone) {
1854552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1855552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1856552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1857552f7358SJed Brown     } else {
1858552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1859552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18600298fd71SBarry Smith       tmpO = NULL;
1861552f7358SJed Brown     }
1862552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1863552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1864552f7358SJed Brown       const PetscInt cp = tmp[i];
18652e1b13c2SMatthew 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. */
18662e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18672e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18682e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1869552f7358SJed Brown       PetscInt       c;
1870552f7358SJed Brown 
18712e1b13c2SMatthew G. Knepley       if (rev) {
18722e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
18732e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18742e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18752e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18762e1b13c2SMatthew G. Knepley       }
1877552f7358SJed Brown       /* Check for duplicate */
1878552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1879552f7358SJed Brown         if (closure[c] == cp) break;
1880552f7358SJed Brown       }
1881552f7358SJed Brown       if (c == closureSize) {
1882552f7358SJed Brown         closure[closureSize]   = cp;
1883552f7358SJed Brown         closure[closureSize+1] = co;
1884552f7358SJed Brown         fifo[fifoSize]         = cp;
1885552f7358SJed Brown         fifo[fifoSize+1]       = co;
1886552f7358SJed Brown         closureSize           += 2;
1887552f7358SJed Brown         fifoSize              += 2;
1888552f7358SJed Brown       }
1889552f7358SJed Brown     }
1890552f7358SJed Brown     fifoStart += 2;
1891552f7358SJed Brown   }
1892552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1893552f7358SJed Brown   if (points)    *points    = closure;
189469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1895552f7358SJed Brown   PetscFunctionReturn(0);
1896552f7358SJed Brown }
1897552f7358SJed Brown 
18989bf0dad6SMatthew G. Knepley /*@C
1899eaf898f9SPatrick 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
19009bf0dad6SMatthew G. Knepley 
19019bf0dad6SMatthew G. Knepley   Not collective
19029bf0dad6SMatthew G. Knepley 
19039bf0dad6SMatthew G. Knepley   Input Parameters:
19049bf0dad6SMatthew G. Knepley + mesh - The DMPlex
1905eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
19069bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
19079bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
19089bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
19099bf0dad6SMatthew G. Knepley 
19109bf0dad6SMatthew G. Knepley   Output Parameters:
19119bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
19129bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
19139bf0dad6SMatthew G. Knepley 
19149bf0dad6SMatthew G. Knepley   Note:
19159bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
19169bf0dad6SMatthew G. Knepley 
19179bf0dad6SMatthew G. Knepley   Fortran Notes:
19189bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19199bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
19209bf0dad6SMatthew G. Knepley 
19219bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19229bf0dad6SMatthew G. Knepley 
19239bf0dad6SMatthew G. Knepley   Level: beginner
19249bf0dad6SMatthew G. Knepley 
19259bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
19269bf0dad6SMatthew G. Knepley @*/
19279bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
19289bf0dad6SMatthew G. Knepley {
19299bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
19309bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
19319bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
19329bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
19339bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
19349bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
19359bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
19369bf0dad6SMatthew G. Knepley 
19379bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
19389bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19399bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
19409bf0dad6SMatthew G. Knepley   /* This is only 1-level */
19419bf0dad6SMatthew G. Knepley   if (useCone) {
19429bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
19439bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
19449bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
19459bf0dad6SMatthew G. Knepley   } else {
19469bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
19479bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
19489bf0dad6SMatthew G. Knepley   }
19499bf0dad6SMatthew G. Knepley   if (depth == 1) {
19509bf0dad6SMatthew G. Knepley     if (*points) {
19519bf0dad6SMatthew G. Knepley       closure = *points;
19529bf0dad6SMatthew G. Knepley     } else {
19539bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
195469291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
19559bf0dad6SMatthew G. Knepley     }
19569bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
19579bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
19589bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19599bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
19609bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
19619bf0dad6SMatthew G. Knepley     }
19629bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
19639bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
19649bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
19659bf0dad6SMatthew G. Knepley   }
196624c766afSToby Isaac   {
196724c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
196824c766afSToby Isaac 
196924c766afSToby Isaac     c = mesh->maxConeSize;
197024c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
197124c766afSToby Isaac     s = mesh->maxSupportSize;
197224c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
197324c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
197424c766afSToby Isaac   }
197569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
19769bf0dad6SMatthew G. Knepley   if (*points) {
19779bf0dad6SMatthew G. Knepley     closure = *points;
19789bf0dad6SMatthew G. Knepley   } else {
197969291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
19809bf0dad6SMatthew G. Knepley   }
19819bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
19829bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
19839bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19849bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
19859bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
19869bf0dad6SMatthew G. Knepley 
19879bf0dad6SMatthew G. Knepley     if (ornt < 0) {
19889bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
19899bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
199086b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
19919bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
19929bf0dad6SMatthew G. Knepley     }
19939bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
19949bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
19959bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
19969bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
19979bf0dad6SMatthew G. Knepley   }
19989bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
19999bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
20009bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
20019bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
20029bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
20039bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
20049bf0dad6SMatthew G. Knepley 
20059bf0dad6SMatthew G. Knepley     if (useCone) {
20069bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
20079bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
20089bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
20099bf0dad6SMatthew G. Knepley     } else {
20109bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
20119bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
20129bf0dad6SMatthew G. Knepley       tmpO = NULL;
20139bf0dad6SMatthew G. Knepley     }
20149bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
20159bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
20169bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
20179bf0dad6SMatthew 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. */
20189bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
20199bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
20209bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
20219bf0dad6SMatthew G. Knepley       PetscInt       c;
20229bf0dad6SMatthew G. Knepley 
20239bf0dad6SMatthew G. Knepley       if (rev) {
20249bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
20259bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
20269bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
20279bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
20289bf0dad6SMatthew G. Knepley       }
20299bf0dad6SMatthew G. Knepley       /* Check for duplicate */
20309bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
20319bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
20329bf0dad6SMatthew G. Knepley       }
20339bf0dad6SMatthew G. Knepley       if (c == closureSize) {
20349bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
20359bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
20369bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
20379bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
20389bf0dad6SMatthew G. Knepley         closureSize           += 2;
20399bf0dad6SMatthew G. Knepley         fifoSize              += 2;
20409bf0dad6SMatthew G. Knepley       }
20419bf0dad6SMatthew G. Knepley     }
20429bf0dad6SMatthew G. Knepley     fifoStart += 2;
20439bf0dad6SMatthew G. Knepley   }
20449bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
20459bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
204669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
20479bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
20489bf0dad6SMatthew G. Knepley }
20499bf0dad6SMatthew G. Knepley 
2050552f7358SJed Brown /*@C
2051eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2052552f7358SJed Brown 
2053552f7358SJed Brown   Not collective
2054552f7358SJed Brown 
2055552f7358SJed Brown   Input Parameters:
2056552f7358SJed Brown + mesh - The DMPlex
2057eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2058552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2059e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2060e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2061552f7358SJed Brown 
2062552f7358SJed Brown   Note:
20630298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2064552f7358SJed Brown 
20653813dfbdSMatthew G Knepley   Fortran Notes:
20663813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20673813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20683813dfbdSMatthew G Knepley 
20693813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
20703813dfbdSMatthew G Knepley 
2071552f7358SJed Brown   Level: beginner
2072552f7358SJed Brown 
2073552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2074552f7358SJed Brown @*/
2075552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2076552f7358SJed Brown {
2077552f7358SJed Brown   PetscErrorCode ierr;
2078552f7358SJed Brown 
2079552f7358SJed Brown   PetscFunctionBegin;
2080552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2081e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2082e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
208369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
20844ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2085552f7358SJed Brown   PetscFunctionReturn(0);
2086552f7358SJed Brown }
2087552f7358SJed Brown 
2088552f7358SJed Brown /*@
2089eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2090552f7358SJed Brown 
2091552f7358SJed Brown   Not collective
2092552f7358SJed Brown 
2093552f7358SJed Brown   Input Parameter:
2094552f7358SJed Brown . mesh - The DMPlex
2095552f7358SJed Brown 
2096552f7358SJed Brown   Output Parameters:
2097552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2098552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2099552f7358SJed Brown 
2100552f7358SJed Brown   Level: beginner
2101552f7358SJed Brown 
2102552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2103552f7358SJed Brown @*/
2104552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2105552f7358SJed Brown {
2106552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2107552f7358SJed Brown 
2108552f7358SJed Brown   PetscFunctionBegin;
2109552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2110552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2111552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2112552f7358SJed Brown   PetscFunctionReturn(0);
2113552f7358SJed Brown }
2114552f7358SJed Brown 
2115552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2116552f7358SJed Brown {
2117552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2118552f7358SJed Brown   PetscInt       size;
2119552f7358SJed Brown   PetscErrorCode ierr;
2120552f7358SJed Brown 
2121552f7358SJed Brown   PetscFunctionBegin;
2122552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2123552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2124552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
21251795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
21261795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2127552f7358SJed Brown   if (mesh->maxSupportSize) {
2128552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2129552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
21301795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2131552f7358SJed Brown   }
2132552f7358SJed Brown   PetscFunctionReturn(0);
2133552f7358SJed Brown }
2134552f7358SJed Brown 
2135276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2136552f7358SJed Brown {
2137552f7358SJed Brown   PetscErrorCode ierr;
2138552f7358SJed Brown 
2139552f7358SJed Brown   PetscFunctionBegin;
21404d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
21414d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2142552f7358SJed Brown   PetscFunctionReturn(0);
2143552f7358SJed Brown }
2144552f7358SJed Brown 
21452adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
21462adcc780SMatthew G. Knepley {
21472adcc780SMatthew G. Knepley   PetscErrorCode ierr;
21482adcc780SMatthew G. Knepley 
21492adcc780SMatthew G. Knepley   PetscFunctionBegin;
21502adcc780SMatthew G. Knepley   if (superdm) {ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);}
21512adcc780SMatthew G. Knepley   ierr = DMCreateSuperDM_Section_Private(dms, len, is, superdm);CHKERRQ(ierr);
21522adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
21532adcc780SMatthew G. Knepley }
21542adcc780SMatthew G. Knepley 
2155552f7358SJed Brown /*@
2156eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2157552f7358SJed Brown 
2158552f7358SJed Brown   Not collective
2159552f7358SJed Brown 
2160552f7358SJed Brown   Input Parameter:
2161552f7358SJed Brown . mesh - The DMPlex
2162552f7358SJed Brown 
2163552f7358SJed Brown   Output Parameter:
2164552f7358SJed Brown 
2165552f7358SJed Brown   Note:
2166552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2167552f7358SJed Brown 
2168552f7358SJed Brown   Level: beginner
2169552f7358SJed Brown 
2170552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2171552f7358SJed Brown @*/
2172552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2173552f7358SJed Brown {
2174552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2175552f7358SJed Brown   PetscInt      *offsets;
2176552f7358SJed Brown   PetscInt       supportSize;
2177552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2178552f7358SJed Brown   PetscErrorCode ierr;
2179552f7358SJed Brown 
2180552f7358SJed Brown   PetscFunctionBegin;
2181552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
218282f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2183552f7358SJed Brown   /* Calculate support sizes */
2184552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2185552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2186552f7358SJed Brown     PetscInt dof, off, c;
2187552f7358SJed Brown 
2188552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2189552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2190552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2191552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2192552f7358SJed Brown     }
2193552f7358SJed Brown   }
2194552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2195552f7358SJed Brown     PetscInt dof;
2196552f7358SJed Brown 
2197552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
21980d644c17SKarl Rupp 
2199552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2200552f7358SJed Brown   }
2201552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2202552f7358SJed Brown   /* Calculate supports */
2203552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
22041795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
22051795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2206552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2207552f7358SJed Brown     PetscInt dof, off, c;
2208552f7358SJed Brown 
2209552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2210552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2211552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2212552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2213552f7358SJed Brown       PetscInt       offS;
2214552f7358SJed Brown 
2215552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
22160d644c17SKarl Rupp 
2217552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2218552f7358SJed Brown       ++offsets[q];
2219552f7358SJed Brown     }
2220552f7358SJed Brown   }
2221552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2222552f7358SJed Brown   PetscFunctionReturn(0);
2223552f7358SJed Brown }
2224552f7358SJed Brown 
2225552f7358SJed Brown /*@
2226eaf898f9SPatrick Sanan   DMPlexStratify - The DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2227eaf898f9SPatrick Sanan   can be illustrated by a Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2228552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2229552f7358SJed Brown   the DAG.
2230552f7358SJed Brown 
2231bf4602e4SToby Isaac   Collective on dm
2232552f7358SJed Brown 
2233552f7358SJed Brown   Input Parameter:
2234552f7358SJed Brown . mesh - The DMPlex
2235552f7358SJed Brown 
2236552f7358SJed Brown   Output Parameter:
2237552f7358SJed Brown 
2238552f7358SJed Brown   Notes:
2239150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2240150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2241c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2242150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2243552f7358SJed Brown 
2244150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2245552f7358SJed Brown 
2246552f7358SJed Brown   Level: beginner
2247552f7358SJed Brown 
2248552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2249552f7358SJed Brown @*/
2250552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2251552f7358SJed Brown {
2252df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2253aa50250dSMatthew G. Knepley   DMLabel        label;
2254552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2255552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2256552f7358SJed Brown   PetscErrorCode ierr;
2257552f7358SJed Brown 
2258552f7358SJed Brown   PetscFunctionBegin;
2259552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2260552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2261552f7358SJed Brown   /* Calculate depth */
2262aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2263c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2264aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2265552f7358SJed Brown   /* Initialize roots and count leaves */
2266552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2267552f7358SJed Brown     PetscInt coneSize, supportSize;
2268552f7358SJed Brown 
2269552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2270552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2271552f7358SJed Brown     if (!coneSize && supportSize) {
2272552f7358SJed Brown       ++numRoots;
2273aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2274552f7358SJed Brown     } else if (!supportSize && coneSize) {
2275552f7358SJed Brown       ++numLeaves;
2276552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2277552f7358SJed Brown       /* Isolated points */
2278aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2279552f7358SJed Brown     }
2280552f7358SJed Brown   }
2281552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2282552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2283552f7358SJed Brown       PetscInt coneSize, supportSize;
2284552f7358SJed Brown 
2285552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2286552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2287552f7358SJed Brown       if (!supportSize && coneSize) {
2288aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2289552f7358SJed Brown       }
2290552f7358SJed Brown     }
2291552f7358SJed Brown   } else {
229274ef644bSMatthew G. Knepley     IS       pointIS;
229374ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2294552f7358SJed Brown 
229574ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
229674ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
229774ef644bSMatthew G. Knepley     while (numPoints) {
229874ef644bSMatthew G. Knepley       const PetscInt *points;
229974ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
230074ef644bSMatthew G. Knepley 
230174ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
230274ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
230374ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
230474ef644bSMatthew G. Knepley         const PetscInt *support;
230574ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
230674ef644bSMatthew G. Knepley 
230774ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
230874ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
230974ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
231074ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2311552f7358SJed Brown         }
2312552f7358SJed Brown       }
23135edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
231474ef644bSMatthew G. Knepley       ++level;
231574ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
231674ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
231774ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
231874ef644bSMatthew G. Knepley       else         {numPoints = 0;}
231974ef644bSMatthew G. Knepley     }
232074ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
232174ef644bSMatthew G. Knepley   }
2322bf4602e4SToby Isaac   { /* just in case there is an empty process */
2323bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2324bf4602e4SToby Isaac 
2325bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
23264de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
23274de306b1SToby Isaac       IS pointIS;
23284de306b1SToby Isaac 
23294de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
23304de306b1SToby Isaac       if (pointIS) {
23314de306b1SToby Isaac         PetscInt  min, max, numPoints;
23324de306b1SToby Isaac         PetscInt  start;
23334de306b1SToby Isaac         PetscBool contig;
23344de306b1SToby Isaac 
23354de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
23364de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
23374de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
23384de306b1SToby Isaac         if (start == 0 && contig) {
23394de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
23404de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
23414de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
23424de306b1SToby Isaac         }
23434de306b1SToby Isaac       }
23444de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
23454de306b1SToby Isaac     }
23466d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2347bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2348bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2349bf4602e4SToby Isaac     }
2350bf4602e4SToby Isaac   }
2351bf4602e4SToby Isaac 
2352df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2353552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2354552f7358SJed Brown   PetscFunctionReturn(0);
2355552f7358SJed Brown }
2356552f7358SJed Brown 
2357552f7358SJed Brown /*@C
2358552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2359552f7358SJed Brown 
2360552f7358SJed Brown   Not Collective
2361552f7358SJed Brown 
2362552f7358SJed Brown   Input Parameters:
2363552f7358SJed Brown + dm - The DMPlex object
2364552f7358SJed Brown . numPoints - The number of input points for the join
2365552f7358SJed Brown - points - The input points
2366552f7358SJed Brown 
2367552f7358SJed Brown   Output Parameters:
2368552f7358SJed Brown + numCoveredPoints - The number of points in the join
2369552f7358SJed Brown - coveredPoints - The points in the join
2370552f7358SJed Brown 
2371552f7358SJed Brown   Level: intermediate
2372552f7358SJed Brown 
2373552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2374552f7358SJed Brown 
23753813dfbdSMatthew G Knepley   Fortran Notes:
23763813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23773813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23783813dfbdSMatthew G Knepley 
23793813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23803813dfbdSMatthew G Knepley 
2381552f7358SJed Brown .keywords: mesh
2382552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2383552f7358SJed Brown @*/
2384552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2385552f7358SJed Brown {
2386552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2387552f7358SJed Brown   PetscInt      *join[2];
2388552f7358SJed Brown   PetscInt       joinSize, i = 0;
2389552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2390552f7358SJed Brown   PetscErrorCode ierr;
2391552f7358SJed Brown 
2392552f7358SJed Brown   PetscFunctionBegin;
2393552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2394552f7358SJed Brown   PetscValidPointer(points, 2);
2395552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2396552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
239769291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
239869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2399552f7358SJed Brown   /* Copy in support of first point */
2400552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2401552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2402552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2403552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2404552f7358SJed Brown   }
2405552f7358SJed Brown   /* Check each successive support */
2406552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2407552f7358SJed Brown     PetscInt newJoinSize = 0;
2408552f7358SJed Brown 
2409552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2410552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2411552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2412552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2413552f7358SJed Brown 
2414552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2415552f7358SJed Brown         if (point == join[i][m]) {
2416552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2417552f7358SJed Brown           break;
2418552f7358SJed Brown         }
2419552f7358SJed Brown       }
2420552f7358SJed Brown     }
2421552f7358SJed Brown     joinSize = newJoinSize;
2422552f7358SJed Brown     i        = 1-i;
2423552f7358SJed Brown   }
2424552f7358SJed Brown   *numCoveredPoints = joinSize;
2425552f7358SJed Brown   *coveredPoints    = join[i];
242669291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2427552f7358SJed Brown   PetscFunctionReturn(0);
2428552f7358SJed Brown }
2429552f7358SJed Brown 
2430552f7358SJed Brown /*@C
2431552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2432552f7358SJed Brown 
2433552f7358SJed Brown   Not Collective
2434552f7358SJed Brown 
2435552f7358SJed Brown   Input Parameters:
2436552f7358SJed Brown + dm - The DMPlex object
2437552f7358SJed Brown . numPoints - The number of input points for the join
2438552f7358SJed Brown - points - The input points
2439552f7358SJed Brown 
2440552f7358SJed Brown   Output Parameters:
2441552f7358SJed Brown + numCoveredPoints - The number of points in the join
2442552f7358SJed Brown - coveredPoints - The points in the join
2443552f7358SJed Brown 
24443813dfbdSMatthew G Knepley   Fortran Notes:
24453813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24463813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24473813dfbdSMatthew G Knepley 
24483813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24493813dfbdSMatthew G Knepley 
2450552f7358SJed Brown   Level: intermediate
2451552f7358SJed Brown 
2452552f7358SJed Brown .keywords: mesh
2453552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2454552f7358SJed Brown @*/
2455552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2456552f7358SJed Brown {
2457552f7358SJed Brown   PetscErrorCode ierr;
2458552f7358SJed Brown 
2459552f7358SJed Brown   PetscFunctionBegin;
2460552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2461d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2462d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2463d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
246469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2465d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2466552f7358SJed Brown   PetscFunctionReturn(0);
2467552f7358SJed Brown }
2468552f7358SJed Brown 
2469552f7358SJed Brown /*@C
2470552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2471552f7358SJed Brown 
2472552f7358SJed Brown   Not Collective
2473552f7358SJed Brown 
2474552f7358SJed Brown   Input Parameters:
2475552f7358SJed Brown + dm - The DMPlex object
2476552f7358SJed Brown . numPoints - The number of input points for the join
2477552f7358SJed Brown - points - The input points
2478552f7358SJed Brown 
2479552f7358SJed Brown   Output Parameters:
2480552f7358SJed Brown + numCoveredPoints - The number of points in the join
2481552f7358SJed Brown - coveredPoints - The points in the join
2482552f7358SJed Brown 
24833813dfbdSMatthew G Knepley   Fortran Notes:
24843813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24853813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24863813dfbdSMatthew G Knepley 
24873813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24883813dfbdSMatthew G Knepley 
2489552f7358SJed Brown   Level: intermediate
2490552f7358SJed Brown 
2491552f7358SJed Brown .keywords: mesh
2492552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2493552f7358SJed Brown @*/
2494552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2495552f7358SJed Brown {
2496552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2497552f7358SJed Brown   PetscInt      *offsets, **closures;
2498552f7358SJed Brown   PetscInt      *join[2];
2499552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
250024c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2501552f7358SJed Brown   PetscErrorCode ierr;
2502552f7358SJed Brown 
2503552f7358SJed Brown   PetscFunctionBegin;
2504552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2505552f7358SJed Brown   PetscValidPointer(points, 2);
2506552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2507552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2508552f7358SJed Brown 
2509552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
25101795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
251169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
251224c766afSToby Isaac   ms      = mesh->maxSupportSize;
251324c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
251469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
251569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2516552f7358SJed Brown 
2517552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2518552f7358SJed Brown     PetscInt closureSize;
2519552f7358SJed Brown 
2520552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
25210d644c17SKarl Rupp 
2522552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2523552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2524552f7358SJed Brown       PetscInt pStart, pEnd, i;
2525552f7358SJed Brown 
2526552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2527552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2528552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2529552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2530552f7358SJed Brown           break;
2531552f7358SJed Brown         }
2532552f7358SJed Brown       }
2533552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2534552f7358SJed Brown     }
253582f516ccSBarry 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);
2536552f7358SJed Brown   }
2537552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2538552f7358SJed Brown     PetscInt dof;
2539552f7358SJed Brown 
2540552f7358SJed Brown     /* Copy in support of first point */
2541552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2542552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2543552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2544552f7358SJed Brown     }
2545552f7358SJed Brown     /* Check each successive cone */
2546552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2547552f7358SJed Brown       PetscInt newJoinSize = 0;
2548552f7358SJed Brown 
2549552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2550552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2551552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2552552f7358SJed Brown 
2553552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2554552f7358SJed Brown           if (point == join[i][m]) {
2555552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2556552f7358SJed Brown             break;
2557552f7358SJed Brown           }
2558552f7358SJed Brown         }
2559552f7358SJed Brown       }
2560552f7358SJed Brown       joinSize = newJoinSize;
2561552f7358SJed Brown       i        = 1-i;
2562552f7358SJed Brown     }
2563552f7358SJed Brown     if (joinSize) break;
2564552f7358SJed Brown   }
2565552f7358SJed Brown   *numCoveredPoints = joinSize;
2566552f7358SJed Brown   *coveredPoints    = join[i];
2567552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
25680298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2569552f7358SJed Brown   }
2570552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
257169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
257269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2573552f7358SJed Brown   PetscFunctionReturn(0);
2574552f7358SJed Brown }
2575552f7358SJed Brown 
2576552f7358SJed Brown /*@C
2577552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2578552f7358SJed Brown 
2579552f7358SJed Brown   Not Collective
2580552f7358SJed Brown 
2581552f7358SJed Brown   Input Parameters:
2582552f7358SJed Brown + dm - The DMPlex object
2583552f7358SJed Brown . numPoints - The number of input points for the meet
2584552f7358SJed Brown - points - The input points
2585552f7358SJed Brown 
2586552f7358SJed Brown   Output Parameters:
2587552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2588552f7358SJed Brown - coveredPoints - The points in the meet
2589552f7358SJed Brown 
2590552f7358SJed Brown   Level: intermediate
2591552f7358SJed Brown 
2592552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2593552f7358SJed Brown 
25943813dfbdSMatthew G Knepley   Fortran Notes:
25953813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25963813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25973813dfbdSMatthew G Knepley 
25983813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25993813dfbdSMatthew G Knepley 
2600552f7358SJed Brown .keywords: mesh
2601552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2602552f7358SJed Brown @*/
2603552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2604552f7358SJed Brown {
2605552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2606552f7358SJed Brown   PetscInt      *meet[2];
2607552f7358SJed Brown   PetscInt       meetSize, i = 0;
2608552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2609552f7358SJed Brown   PetscErrorCode ierr;
2610552f7358SJed Brown 
2611552f7358SJed Brown   PetscFunctionBegin;
2612552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2613552f7358SJed Brown   PetscValidPointer(points, 2);
2614552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2615552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
261669291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
261769291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2618552f7358SJed Brown   /* Copy in cone of first point */
2619552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2620552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2621552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2622552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2623552f7358SJed Brown   }
2624552f7358SJed Brown   /* Check each successive cone */
2625552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2626552f7358SJed Brown     PetscInt newMeetSize = 0;
2627552f7358SJed Brown 
2628552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2629552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2630552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2631552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2632552f7358SJed Brown 
2633552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2634552f7358SJed Brown         if (point == meet[i][m]) {
2635552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2636552f7358SJed Brown           break;
2637552f7358SJed Brown         }
2638552f7358SJed Brown       }
2639552f7358SJed Brown     }
2640552f7358SJed Brown     meetSize = newMeetSize;
2641552f7358SJed Brown     i        = 1-i;
2642552f7358SJed Brown   }
2643552f7358SJed Brown   *numCoveringPoints = meetSize;
2644552f7358SJed Brown   *coveringPoints    = meet[i];
264569291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2646552f7358SJed Brown   PetscFunctionReturn(0);
2647552f7358SJed Brown }
2648552f7358SJed Brown 
2649552f7358SJed Brown /*@C
2650552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2651552f7358SJed Brown 
2652552f7358SJed Brown   Not Collective
2653552f7358SJed Brown 
2654552f7358SJed Brown   Input Parameters:
2655552f7358SJed Brown + dm - The DMPlex object
2656552f7358SJed Brown . numPoints - The number of input points for the meet
2657552f7358SJed Brown - points - The input points
2658552f7358SJed Brown 
2659552f7358SJed Brown   Output Parameters:
2660552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2661552f7358SJed Brown - coveredPoints - The points in the meet
2662552f7358SJed Brown 
2663552f7358SJed Brown   Level: intermediate
2664552f7358SJed Brown 
26653813dfbdSMatthew G Knepley   Fortran Notes:
26663813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26673813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26683813dfbdSMatthew G Knepley 
26693813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26703813dfbdSMatthew G Knepley 
2671552f7358SJed Brown .keywords: mesh
2672552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2673552f7358SJed Brown @*/
2674552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2675552f7358SJed Brown {
2676552f7358SJed Brown   PetscErrorCode ierr;
2677552f7358SJed Brown 
2678552f7358SJed Brown   PetscFunctionBegin;
2679552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2680d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2681d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2682d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
268369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2684d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2685552f7358SJed Brown   PetscFunctionReturn(0);
2686552f7358SJed Brown }
2687552f7358SJed Brown 
2688552f7358SJed Brown /*@C
2689552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2690552f7358SJed Brown 
2691552f7358SJed Brown   Not Collective
2692552f7358SJed Brown 
2693552f7358SJed Brown   Input Parameters:
2694552f7358SJed Brown + dm - The DMPlex object
2695552f7358SJed Brown . numPoints - The number of input points for the meet
2696552f7358SJed Brown - points - The input points
2697552f7358SJed Brown 
2698552f7358SJed Brown   Output Parameters:
2699552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2700552f7358SJed Brown - coveredPoints - The points in the meet
2701552f7358SJed Brown 
2702552f7358SJed Brown   Level: intermediate
2703552f7358SJed Brown 
27043813dfbdSMatthew G Knepley   Fortran Notes:
27053813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27063813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27073813dfbdSMatthew G Knepley 
27083813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27093813dfbdSMatthew G Knepley 
2710552f7358SJed Brown .keywords: mesh
2711552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2712552f7358SJed Brown @*/
2713552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2714552f7358SJed Brown {
2715552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2716552f7358SJed Brown   PetscInt      *offsets, **closures;
2717552f7358SJed Brown   PetscInt      *meet[2];
2718552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
271924c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2720552f7358SJed Brown   PetscErrorCode ierr;
2721552f7358SJed Brown 
2722552f7358SJed Brown   PetscFunctionBegin;
2723552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2724552f7358SJed Brown   PetscValidPointer(points, 2);
2725552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2726552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2727552f7358SJed Brown 
2728552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2729785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
273069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
273124c766afSToby Isaac   mc      = mesh->maxConeSize;
273224c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
273369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
273469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2735552f7358SJed Brown 
2736552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2737552f7358SJed Brown     PetscInt closureSize;
2738552f7358SJed Brown 
2739552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
27400d644c17SKarl Rupp 
2741552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2742552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2743552f7358SJed Brown       PetscInt pStart, pEnd, i;
2744552f7358SJed Brown 
2745552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2746552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2747552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2748552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2749552f7358SJed Brown           break;
2750552f7358SJed Brown         }
2751552f7358SJed Brown       }
2752552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2753552f7358SJed Brown     }
275482f516ccSBarry 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);
2755552f7358SJed Brown   }
2756552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2757552f7358SJed Brown     PetscInt dof;
2758552f7358SJed Brown 
2759552f7358SJed Brown     /* Copy in cone of first point */
2760552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2761552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2762552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2763552f7358SJed Brown     }
2764552f7358SJed Brown     /* Check each successive cone */
2765552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2766552f7358SJed Brown       PetscInt newMeetSize = 0;
2767552f7358SJed Brown 
2768552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2769552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2770552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2771552f7358SJed Brown 
2772552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2773552f7358SJed Brown           if (point == meet[i][m]) {
2774552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2775552f7358SJed Brown             break;
2776552f7358SJed Brown           }
2777552f7358SJed Brown         }
2778552f7358SJed Brown       }
2779552f7358SJed Brown       meetSize = newMeetSize;
2780552f7358SJed Brown       i        = 1-i;
2781552f7358SJed Brown     }
2782552f7358SJed Brown     if (meetSize) break;
2783552f7358SJed Brown   }
2784552f7358SJed Brown   *numCoveredPoints = meetSize;
2785552f7358SJed Brown   *coveredPoints    = meet[i];
2786552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
27870298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2788552f7358SJed Brown   }
2789552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
279069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
279169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2792552f7358SJed Brown   PetscFunctionReturn(0);
2793552f7358SJed Brown }
2794552f7358SJed Brown 
27954e3744c5SMatthew G. Knepley /*@C
27964e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
27974e3744c5SMatthew G. Knepley 
27984e3744c5SMatthew G. Knepley   Not Collective
27994e3744c5SMatthew G. Knepley 
28004e3744c5SMatthew G. Knepley   Input Parameters:
28014e3744c5SMatthew G. Knepley + dmA - A DMPlex object
28024e3744c5SMatthew G. Knepley - dmB - A DMPlex object
28034e3744c5SMatthew G. Knepley 
28044e3744c5SMatthew G. Knepley   Output Parameters:
28054e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
28064e3744c5SMatthew G. Knepley 
28074e3744c5SMatthew G. Knepley   Level: intermediate
28084e3744c5SMatthew G. Knepley 
28094e3744c5SMatthew G. Knepley   Notes:
28104e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
28114e3744c5SMatthew G. Knepley 
28124e3744c5SMatthew G. Knepley .keywords: mesh
28134e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
28144e3744c5SMatthew G. Knepley @*/
28154e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
28164e3744c5SMatthew G. Knepley {
28174e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
28184e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
28194e3744c5SMatthew G. Knepley 
28204e3744c5SMatthew G. Knepley   PetscFunctionBegin;
28214e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
28224e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
28234e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
28244e3744c5SMatthew G. Knepley 
28254e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
28264e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
28274e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
28284e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
28294e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
28304e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
28314e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
28324e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
28334e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
28344e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
28354e3744c5SMatthew G. Knepley 
28364e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
28374e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
28384e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
28394e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
28404e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
28414e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
28424e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
28434e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
28444e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
28454e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
28464e3744c5SMatthew G. Knepley     }
28474e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
28484e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
28494e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
28504e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
28514e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
28524e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
28534e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
28544e3744c5SMatthew G. Knepley     }
28554e3744c5SMatthew G. Knepley   }
28564e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
28574e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
28584e3744c5SMatthew G. Knepley }
28594e3744c5SMatthew G. Knepley 
28607cd05799SMatthew G. Knepley /*@C
28617cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
28627cd05799SMatthew G. Knepley 
28637cd05799SMatthew G. Knepley   Not Collective
28647cd05799SMatthew G. Knepley 
28657cd05799SMatthew G. Knepley   Input Parameters:
28667cd05799SMatthew G. Knepley + dm         - The DMPlex
28677cd05799SMatthew G. Knepley . cellDim    - The cell dimension
28687cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
28697cd05799SMatthew G. Knepley 
28707cd05799SMatthew G. Knepley   Output Parameters:
28717cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
28727cd05799SMatthew G. Knepley 
28737cd05799SMatthew G. Knepley   Level: developer
28747cd05799SMatthew G. Knepley 
28757cd05799SMatthew G. Knepley   Notes:
28767cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
28777cd05799SMatthew G. Knepley 
28787cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
28797cd05799SMatthew G. Knepley @*/
288018ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2881a6dfd86eSKarl Rupp {
288282f516ccSBarry Smith   MPI_Comm       comm;
2883552f7358SJed Brown   PetscErrorCode ierr;
2884552f7358SJed Brown 
2885552f7358SJed Brown   PetscFunctionBegin;
288682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2887552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2888552f7358SJed Brown   switch (cellDim) {
2889552f7358SJed Brown   case 0:
2890552f7358SJed Brown     *numFaceVertices = 0;
2891552f7358SJed Brown     break;
2892552f7358SJed Brown   case 1:
2893552f7358SJed Brown     *numFaceVertices = 1;
2894552f7358SJed Brown     break;
2895552f7358SJed Brown   case 2:
2896552f7358SJed Brown     switch (numCorners) {
289719436ca2SJed Brown     case 3: /* triangle */
289819436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2899552f7358SJed Brown       break;
290019436ca2SJed Brown     case 4: /* quadrilateral */
290119436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2902552f7358SJed Brown       break;
290319436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
290419436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2905552f7358SJed Brown       break;
290619436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
290719436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2908552f7358SJed Brown       break;
2909552f7358SJed Brown     default:
2910f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2911552f7358SJed Brown     }
2912552f7358SJed Brown     break;
2913552f7358SJed Brown   case 3:
2914552f7358SJed Brown     switch (numCorners) {
291519436ca2SJed Brown     case 4: /* tetradehdron */
291619436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2917552f7358SJed Brown       break;
291819436ca2SJed Brown     case 6: /* tet cohesive cells */
291919436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2920552f7358SJed Brown       break;
292119436ca2SJed Brown     case 8: /* hexahedron */
292219436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2923552f7358SJed Brown       break;
292419436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
292519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2926552f7358SJed Brown       break;
292719436ca2SJed Brown     case 10: /* quadratic tetrahedron */
292819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2929552f7358SJed Brown       break;
293019436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
293119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2932552f7358SJed Brown       break;
293319436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
293419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2935552f7358SJed Brown       break;
293619436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
293719436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2938552f7358SJed Brown       break;
2939552f7358SJed Brown     default:
2940f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2941552f7358SJed Brown     }
2942552f7358SJed Brown     break;
2943552f7358SJed Brown   default:
2944f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2945552f7358SJed Brown   }
2946552f7358SJed Brown   PetscFunctionReturn(0);
2947552f7358SJed Brown }
2948552f7358SJed Brown 
2949552f7358SJed Brown /*@
2950aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2951552f7358SJed Brown 
2952552f7358SJed Brown   Not Collective
2953552f7358SJed Brown 
2954aa50250dSMatthew G. Knepley   Input Parameter:
2955552f7358SJed Brown . dm    - The DMPlex object
2956552f7358SJed Brown 
2957aa50250dSMatthew G. Knepley   Output Parameter:
2958aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2959552f7358SJed Brown 
2960552f7358SJed Brown   Level: developer
2961552f7358SJed Brown 
2962aa50250dSMatthew G. Knepley .keywords: mesh, points
2963aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2964aa50250dSMatthew G. Knepley @*/
2965aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2966aa50250dSMatthew G. Knepley {
2967aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2968aa50250dSMatthew G. Knepley 
2969aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2970aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2971aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2972c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2973c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2974aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2975aa50250dSMatthew G. Knepley }
2976aa50250dSMatthew G. Knepley 
2977aa50250dSMatthew G. Knepley /*@
2978aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2979aa50250dSMatthew G. Knepley 
2980aa50250dSMatthew G. Knepley   Not Collective
2981aa50250dSMatthew G. Knepley 
2982aa50250dSMatthew G. Knepley   Input Parameter:
2983aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2984aa50250dSMatthew G. Knepley 
2985aa50250dSMatthew G. Knepley   Output Parameter:
2986aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2987aa50250dSMatthew G. Knepley 
2988aa50250dSMatthew G. Knepley   Level: developer
2989552f7358SJed Brown 
2990552f7358SJed Brown .keywords: mesh, points
2991aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2992552f7358SJed Brown @*/
2993552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2994552f7358SJed Brown {
2995aa50250dSMatthew G. Knepley   DMLabel        label;
2996aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2997552f7358SJed Brown   PetscErrorCode ierr;
2998552f7358SJed Brown 
2999552f7358SJed Brown   PetscFunctionBegin;
3000552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3001552f7358SJed Brown   PetscValidPointer(depth, 2);
3002aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3003aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3004552f7358SJed Brown   *depth = d-1;
3005552f7358SJed Brown   PetscFunctionReturn(0);
3006552f7358SJed Brown }
3007552f7358SJed Brown 
3008552f7358SJed Brown /*@
3009552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3010552f7358SJed Brown 
3011552f7358SJed Brown   Not Collective
3012552f7358SJed Brown 
3013552f7358SJed Brown   Input Parameters:
3014552f7358SJed Brown + dm           - The DMPlex object
3015552f7358SJed Brown - stratumValue - The requested depth
3016552f7358SJed Brown 
3017552f7358SJed Brown   Output Parameters:
3018552f7358SJed Brown + start - The first point at this depth
3019552f7358SJed Brown - end   - One beyond the last point at this depth
3020552f7358SJed Brown 
3021552f7358SJed Brown   Level: developer
3022552f7358SJed Brown 
3023552f7358SJed Brown .keywords: mesh, points
3024552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3025552f7358SJed Brown @*/
30260adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
30270adebc6cSBarry Smith {
3028aa50250dSMatthew G. Knepley   DMLabel        label;
302963d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3030552f7358SJed Brown   PetscErrorCode ierr;
3031552f7358SJed Brown 
3032552f7358SJed Brown   PetscFunctionBegin;
3033552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
303463d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
303563d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3036552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
30370d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
303863d1a920SMatthew G. Knepley   if (stratumValue < 0) {
303963d1a920SMatthew G. Knepley     if (start) *start = pStart;
304063d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
304163d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3042552f7358SJed Brown   }
3043aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
304463d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
304563d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3046552f7358SJed Brown   PetscFunctionReturn(0);
3047552f7358SJed Brown }
3048552f7358SJed Brown 
3049552f7358SJed Brown /*@
3050552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3051552f7358SJed Brown 
3052552f7358SJed Brown   Not Collective
3053552f7358SJed Brown 
3054552f7358SJed Brown   Input Parameters:
3055552f7358SJed Brown + dm           - The DMPlex object
3056552f7358SJed Brown - stratumValue - The requested height
3057552f7358SJed Brown 
3058552f7358SJed Brown   Output Parameters:
3059552f7358SJed Brown + start - The first point at this height
3060552f7358SJed Brown - end   - One beyond the last point at this height
3061552f7358SJed Brown 
3062552f7358SJed Brown   Level: developer
3063552f7358SJed Brown 
3064552f7358SJed Brown .keywords: mesh, points
3065552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3066552f7358SJed Brown @*/
30670adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
30680adebc6cSBarry Smith {
3069aa50250dSMatthew G. Knepley   DMLabel        label;
307063d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3071552f7358SJed Brown   PetscErrorCode ierr;
3072552f7358SJed Brown 
3073552f7358SJed Brown   PetscFunctionBegin;
3074552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
307563d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
307663d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3077552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
30780d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
307963d1a920SMatthew G. Knepley   if (stratumValue < 0) {
308063d1a920SMatthew G. Knepley     if (start) *start = pStart;
308163d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
308263d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3083552f7358SJed Brown   }
3084aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
308513903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
308663d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
308763d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3088552f7358SJed Brown   PetscFunctionReturn(0);
3089552f7358SJed Brown }
3090552f7358SJed Brown 
3091552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
30927cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
3093a6dfd86eSKarl Rupp {
3094ee55978aSMatthew G. Knepley   PetscInt      *pMax;
3095470f9322SMatthew G. Knepley   PetscInt       depth, cellHeight, pStart = 0, pEnd = 0;
3096ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
3097fa716be8SMatthew G. Knepley   PetscBool     *isFE;
3098552f7358SJed Brown   PetscErrorCode ierr;
3099552f7358SJed Brown 
3100552f7358SJed Brown   PetscFunctionBegin;
3101fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
3102ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
3103fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
3104fa716be8SMatthew G. Knepley     PetscObject  obj;
3105fa716be8SMatthew G. Knepley     PetscClassId id;
3106fa716be8SMatthew G. Knepley 
3107ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
3108ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
3109fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
3110fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3111fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
3112fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
3113552f7358SJed Brown   }
311482f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
3115552f7358SJed Brown   if (numFields > 0) {
3116552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
3117552f7358SJed Brown     if (numComp) {
3118552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3119552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
3120d484f18aSToby Isaac         if (isFE[f]) {
3121d484f18aSToby Isaac           PetscFE           fe;
3122d484f18aSToby Isaac           PetscDualSpace    dspace;
3123d484f18aSToby Isaac           const PetscInt    ***perms;
3124d484f18aSToby Isaac           const PetscScalar ***flips;
3125d484f18aSToby Isaac           const PetscInt    *numDof;
3126d484f18aSToby Isaac 
3127d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
3128d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
3129d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
3130d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
3131d484f18aSToby Isaac           if (perms || flips) {
3132d484f18aSToby Isaac             DM               K;
3133d484f18aSToby Isaac             DMLabel          depthLabel;
3134d484f18aSToby Isaac             PetscInt         depth, h;
3135d484f18aSToby Isaac             PetscSectionSym  sym;
3136d484f18aSToby Isaac 
3137d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
3138d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
3139d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
3140d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
3141d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
3142d484f18aSToby Isaac               PetscDualSpace    hspace;
3143d484f18aSToby Isaac               PetscInt          kStart, kEnd;
3144d484f18aSToby Isaac               PetscInt          kConeSize;
3145d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3146d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3147d484f18aSToby Isaac 
3148d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3149d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3150d484f18aSToby Isaac               if (!hspace) continue;
3151d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3152d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3153d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3154d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3155d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3156d484f18aSToby Isaac               ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3157d484f18aSToby Isaac             }
3158d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3159d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3160d484f18aSToby Isaac           }
3161d484f18aSToby Isaac         }
3162552f7358SJed Brown       }
3163552f7358SJed Brown     }
3164552f7358SJed Brown   }
3165552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3166552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3167916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
31689ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
31699ac3fadcSMatthew 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);
3170470f9322SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
3171470f9322SMatthew G. Knepley   for (dep = 0; dep <= depth - cellHeight; ++dep) {
3172916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3173916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3174fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3175552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3176ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3177ee55978aSMatthew G. Knepley 
3178552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3179fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3180552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3181ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3182552f7358SJed Brown       }
3183ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3184552f7358SJed Brown     }
3185552f7358SJed Brown   }
31869ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3187fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3188552f7358SJed Brown   PetscFunctionReturn(0);
3189552f7358SJed Brown }
3190552f7358SJed Brown 
3191552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3192ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3193552f7358SJed Brown */
31947cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3195a6dfd86eSKarl Rupp {
3196552f7358SJed Brown   PetscInt       numFields;
3197552f7358SJed Brown   PetscInt       bc;
3198a68b90caSToby Isaac   PetscSection   aSec;
3199552f7358SJed Brown   PetscErrorCode ierr;
3200552f7358SJed Brown 
3201552f7358SJed Brown   PetscFunctionBegin;
3202552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3203552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3204552f7358SJed Brown     PetscInt        field = 0;
3205ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3206552f7358SJed Brown     const PetscInt *idx;
3207ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3208552f7358SJed Brown 
32090d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3210ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3211ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3212552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3213552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3214552f7358SJed Brown     for (i = 0; i < n; ++i) {
3215552f7358SJed Brown       const PetscInt p = idx[i];
321606ff1081SMatthew G. Knepley       PetscInt       numConst;
3217552f7358SJed Brown 
3218552f7358SJed Brown       if (numFields) {
3219552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3220552f7358SJed Brown       } else {
3221552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3222552f7358SJed Brown       }
322306ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
322406ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
322506ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3226552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3227552f7358SJed Brown     }
3228552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
322906ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3230552f7358SJed Brown   }
3231a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3232a68b90caSToby Isaac   if (aSec) {
3233a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3234a68b90caSToby Isaac 
3235a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3236a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3237ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3238a68b90caSToby Isaac 
3239a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3240a68b90caSToby Isaac       if (dof) {
3241a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3242a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3243a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3244a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3245a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3246a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3247a68b90caSToby Isaac         }
3248a68b90caSToby Isaac       }
3249a68b90caSToby Isaac     }
3250a68b90caSToby Isaac   }
3251552f7358SJed Brown   PetscFunctionReturn(0);
3252552f7358SJed Brown }
3253552f7358SJed Brown 
3254ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3255ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3256ab1d0545SMatthew G. Knepley */
32577cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
32580adebc6cSBarry Smith {
3259ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3260ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3261503335f2SSander Arens   PetscInt       numFields, cdof, maxDof = 0, pStart, pEnd, p, bc, f, d;
3262552f7358SJed Brown   PetscErrorCode ierr;
3263552f7358SJed Brown 
3264552f7358SJed Brown   PetscFunctionBegin;
3265552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3266ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3267ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3268552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3269503335f2SSander Arens   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr); maxDof = PetscMax(maxDof, cdof);}
3270ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3271ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3272ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3273ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3274ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3275ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3276ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3277ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3278552f7358SJed Brown 
3279ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3280ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3281ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3282ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3283ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3284ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3285ab1d0545SMatthew G. Knepley       const PetscInt *find;
3286503335f2SSander Arens       PetscInt        fdof, fcdof, c;
3287ab1d0545SMatthew G. Knepley 
3288503335f2SSander Arens       ierr = PetscSectionGetFieldDof(section, p, field, &fdof);CHKERRQ(ierr);
3289503335f2SSander Arens       if (!fdof) continue;
3290ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3291503335f2SSander Arens         for (d = 0; d < fdof; ++d) indices[d] = d;
3292503335f2SSander Arens         fcdof = fdof;
3293552f7358SJed Brown       } else {
3294503335f2SSander Arens         ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3295ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3296ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3297503335f2SSander Arens         for (c = 0; c < Nc; ++c) indices[d++] = comp[c];
3298503335f2SSander Arens         ierr = PetscSortRemoveDupsInt(&d, indices);CHKERRQ(ierr);
3299503335f2SSander Arens         for (c = d; c < fcdof; ++c) indices[c] = -1;
3300503335f2SSander Arens         fcdof = d;
3301552f7358SJed Brown       }
3302503335f2SSander Arens       ierr = PetscSectionSetFieldConstraintDof(section, p, field, fcdof);CHKERRQ(ierr);
3303ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3304552f7358SJed Brown     }
3305ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3306ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3307552f7358SJed Brown   }
3308ab1d0545SMatthew G. Knepley   /* Handle anchors */
3309ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3310ab1d0545SMatthew G. Knepley   if (aSec) {
3311ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3312552f7358SJed Brown 
3313ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3314ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3315ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3316503335f2SSander Arens       PetscInt dof, f;
3317ab1d0545SMatthew G. Knepley 
3318ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3319ab1d0545SMatthew G. Knepley       if (dof) {
3320ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3321ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3322ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3323ab1d0545SMatthew G. Knepley         }
3324ab1d0545SMatthew G. Knepley       }
3325ab1d0545SMatthew G. Knepley     }
3326ab1d0545SMatthew G. Knepley   }
3327ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3328ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3329ab1d0545SMatthew G. Knepley }
3330ab1d0545SMatthew G. Knepley 
3331ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
33327cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3333ab1d0545SMatthew G. Knepley {
3334ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3335ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3336ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3337ab1d0545SMatthew G. Knepley 
3338ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3339ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3340ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3341ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3342ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3343ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3344552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3345552f7358SJed Brown     PetscInt cdof, d;
3346552f7358SJed Brown 
3347552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3348552f7358SJed Brown     if (cdof) {
3349552f7358SJed Brown       if (numFields) {
3350552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3351552f7358SJed Brown 
3352552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3353ab1d0545SMatthew G. Knepley           const PetscInt *find;
3354ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3355552f7358SJed Brown 
3356552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3357ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3358ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3359ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3360ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3361ab1d0545SMatthew G. Knepley           numConst += fcdof;
3362552f7358SJed Brown           foff     += fdof;
3363552f7358SJed Brown         }
3364503335f2SSander Arens         if (cdof != numConst) {ierr = PetscSectionSetConstraintDof(section, p, numConst);CHKERRQ(ierr);}
3365552f7358SJed Brown       } else {
33660d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3367552f7358SJed Brown       }
3368552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3369552f7358SJed Brown     }
3370552f7358SJed Brown   }
3371552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3372552f7358SJed Brown   PetscFunctionReturn(0);
3373552f7358SJed Brown }
3374552f7358SJed Brown 
3375552f7358SJed Brown /*@C
3376552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3377552f7358SJed Brown 
3378552f7358SJed Brown   Not Collective
3379552f7358SJed Brown 
3380552f7358SJed Brown   Input Parameters:
3381552f7358SJed Brown + dm        - The DMPlex object
3382552f7358SJed Brown . dim       - The spatial dimension of the problem
3383552f7358SJed Brown . numFields - The number of fields in the problem
3384552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3385552f7358SJed 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
3386552f7358SJed Brown . numBC     - The number of boundary conditions
3387552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3388ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3389ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3390f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3391552f7358SJed Brown 
3392552f7358SJed Brown   Output Parameter:
3393552f7358SJed Brown . section - The PetscSection object
3394552f7358SJed Brown 
3395eaf898f9SPatrick 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
3396f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3397f8f126e8SMatthew G. Knepley 
3398f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3399552f7358SJed Brown 
3400552f7358SJed Brown   Level: developer
3401552f7358SJed Brown 
34023813dfbdSMatthew G Knepley   Fortran Notes:
34033813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
34043813dfbdSMatthew G Knepley 
3405552f7358SJed Brown .keywords: mesh, elements
3406f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3407552f7358SJed Brown @*/
3408ab1d0545SMatthew 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)
3409a6dfd86eSKarl Rupp {
3410a68b90caSToby Isaac   PetscSection   aSec;
3411552f7358SJed Brown   PetscErrorCode ierr;
3412552f7358SJed Brown 
3413552f7358SJed Brown   PetscFunctionBegin;
3414552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3415ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3416f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3417552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3418a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3419ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3420ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3421ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3422ab1d0545SMatthew G. Knepley   }
3423ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3424552f7358SJed Brown   PetscFunctionReturn(0);
3425552f7358SJed Brown }
3426552f7358SJed Brown 
34270adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
34280adebc6cSBarry Smith {
3429efe440bfSMatthew G. Knepley   PetscSection   section, s;
3430efe440bfSMatthew G. Knepley   Mat            m;
34313e922f36SToby Isaac   PetscInt       maxHeight;
3432552f7358SJed Brown   PetscErrorCode ierr;
3433552f7358SJed Brown 
3434552f7358SJed Brown   PetscFunctionBegin;
343538221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
34363e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
34373e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
343882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3439552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
34401d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3441efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3442efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3443efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3444efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3445efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3446552f7358SJed Brown   PetscFunctionReturn(0);
3447552f7358SJed Brown }
3448552f7358SJed Brown 
34497cd05799SMatthew G. Knepley /*@C
34507cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
34517cd05799SMatthew G. Knepley 
34527cd05799SMatthew G. Knepley   Not Collective
34537cd05799SMatthew G. Knepley 
34547cd05799SMatthew G. Knepley   Input Parameters:
34557cd05799SMatthew G. Knepley . dm        - The DMPlex object
34567cd05799SMatthew G. Knepley 
34577cd05799SMatthew G. Knepley   Output Parameter:
34587cd05799SMatthew G. Knepley . section - The PetscSection object
34597cd05799SMatthew G. Knepley 
34607cd05799SMatthew G. Knepley   Level: developer
34617cd05799SMatthew G. Knepley 
34627cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
34637cd05799SMatthew G. Knepley @*/
34640adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
34650adebc6cSBarry Smith {
3466552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3467552f7358SJed Brown 
3468552f7358SJed Brown   PetscFunctionBegin;
3469552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3470552f7358SJed Brown   if (section) *section = mesh->coneSection;
3471552f7358SJed Brown   PetscFunctionReturn(0);
3472552f7358SJed Brown }
3473552f7358SJed Brown 
34747cd05799SMatthew G. Knepley /*@C
34757cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
34767cd05799SMatthew G. Knepley 
34777cd05799SMatthew G. Knepley   Not Collective
34787cd05799SMatthew G. Knepley 
34797cd05799SMatthew G. Knepley   Input Parameters:
34807cd05799SMatthew G. Knepley . dm        - The DMPlex object
34817cd05799SMatthew G. Knepley 
34827cd05799SMatthew G. Knepley   Output Parameter:
34837cd05799SMatthew G. Knepley . section - The PetscSection object
34847cd05799SMatthew G. Knepley 
34857cd05799SMatthew G. Knepley   Level: developer
34867cd05799SMatthew G. Knepley 
34877cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34887cd05799SMatthew G. Knepley @*/
34898cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
34908cb4d582SMatthew G. Knepley {
34918cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
34928cb4d582SMatthew G. Knepley 
34938cb4d582SMatthew G. Knepley   PetscFunctionBegin;
34948cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34958cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
34968cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
34978cb4d582SMatthew G. Knepley }
34988cb4d582SMatthew G. Knepley 
34997cd05799SMatthew G. Knepley /*@C
35007cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
35017cd05799SMatthew G. Knepley 
35027cd05799SMatthew G. Knepley   Not Collective
35037cd05799SMatthew G. Knepley 
35047cd05799SMatthew G. Knepley   Input Parameters:
35057cd05799SMatthew G. Knepley . dm        - The DMPlex object
35067cd05799SMatthew G. Knepley 
35077cd05799SMatthew G. Knepley   Output Parameter:
35087cd05799SMatthew G. Knepley . cones - The cone for each point
35097cd05799SMatthew G. Knepley 
35107cd05799SMatthew G. Knepley   Level: developer
35117cd05799SMatthew G. Knepley 
35127cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35137cd05799SMatthew G. Knepley @*/
3514a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3515a6dfd86eSKarl Rupp {
3516552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3517552f7358SJed Brown 
3518552f7358SJed Brown   PetscFunctionBegin;
3519552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3520552f7358SJed Brown   if (cones) *cones = mesh->cones;
3521552f7358SJed Brown   PetscFunctionReturn(0);
3522552f7358SJed Brown }
3523552f7358SJed Brown 
35247cd05799SMatthew G. Knepley /*@C
35257cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
35267cd05799SMatthew G. Knepley 
35277cd05799SMatthew G. Knepley   Not Collective
35287cd05799SMatthew G. Knepley 
35297cd05799SMatthew G. Knepley   Input Parameters:
35307cd05799SMatthew G. Knepley . dm        - The DMPlex object
35317cd05799SMatthew G. Knepley 
35327cd05799SMatthew G. Knepley   Output Parameter:
35337cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
35347cd05799SMatthew G. Knepley 
35357cd05799SMatthew G. Knepley   Level: developer
35367cd05799SMatthew G. Knepley 
35377cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35387cd05799SMatthew G. Knepley @*/
3539a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3540a6dfd86eSKarl Rupp {
3541552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3542552f7358SJed Brown 
3543552f7358SJed Brown   PetscFunctionBegin;
3544552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3545552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3546552f7358SJed Brown   PetscFunctionReturn(0);
3547552f7358SJed Brown }
3548552f7358SJed Brown 
3549552f7358SJed Brown /******************************** FEM Support **********************************/
3550552f7358SJed Brown 
35517391a63aSMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscInt point, PetscSection section)
35523194fc30SMatthew G. Knepley {
35537391a63aSMatthew G. Knepley   DMLabel        label;
35543194fc30SMatthew G. Knepley   PetscInt      *perm;
35557391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
35563194fc30SMatthew G. Knepley   PetscErrorCode ierr;
35573194fc30SMatthew G. Knepley 
35583194fc30SMatthew G. Knepley   PetscFunctionBegin;
35597391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
35603194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
35617391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
35627391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
35637391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
35647391a63aSMatthew G. Knepley   else if  (depth == dim) {
35657391a63aSMatthew G. Knepley     const PetscInt *cone;
35667391a63aSMatthew G. Knepley 
35677391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
35687391a63aSMatthew G. Knepley     eStart = cone[0];
35697391a63aSMatthew 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);
35707391a63aSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
35713194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
357289eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
35733194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
35743194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
35753194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35763194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
35773194fc30SMatthew G. Knepley     k = k/Nc + 1;
357889eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
35793194fc30SMatthew G. Knepley   }
35803194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
35813194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
358289eabcffSMatthew G. Knepley     switch (dim) {
358389eabcffSMatthew G. Knepley     case 2:
35843194fc30SMatthew 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} */
35853194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35863194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
35873194fc30SMatthew G. Knepley       k = k/Nc + 1;
35883194fc30SMatthew G. Knepley       /* The SEM order is
35893194fc30SMatthew G. Knepley 
35903194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
359189eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
35923194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
35933194fc30SMatthew G. Knepley       */
35943194fc30SMatthew G. Knepley       {
35953194fc30SMatthew G. Knepley         const PetscInt of   = 0;
35963194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
35973194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
35983194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
35993194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
36003194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
36013194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
36023194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
36033194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
36043194fc30SMatthew G. Knepley         PetscInt       o;
36053194fc30SMatthew G. Knepley 
36063194fc30SMatthew G. Knepley         /* bottom */
36073194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
36083194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
36093194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
36103194fc30SMatthew G. Knepley         /* middle */
36113194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
36123194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
36133194fc30SMatthew 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;
36143194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
36153194fc30SMatthew G. Knepley         }
36163194fc30SMatthew G. Knepley         /* top */
36173194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
36183194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
36193194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
36203194fc30SMatthew G. Knepley         foffset = offset;
36213194fc30SMatthew G. Knepley       }
362289eabcffSMatthew G. Knepley       break;
362389eabcffSMatthew G. Knepley     case 3:
362489eabcffSMatthew G. Knepley       /* The original hex closure is
362589eabcffSMatthew G. Knepley 
362689eabcffSMatthew G. Knepley          {c,
362789eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
362889eabcffSMatthew 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,
362989eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
363089eabcffSMatthew G. Knepley       */
363189eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
363289eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
363389eabcffSMatthew G. Knepley       k = k/Nc + 1;
363489eabcffSMatthew G. Knepley       /* The SEM order is
363589eabcffSMatthew G. Knepley          Bottom Slice
363689eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
363789eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
363889eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
363989eabcffSMatthew G. Knepley 
364089eabcffSMatthew G. Knepley          Middle Slice (j)
364189eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
364289eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
364389eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
364489eabcffSMatthew G. Knepley 
364589eabcffSMatthew G. Knepley          Top Slice
364689eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
364789eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
364889eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
364989eabcffSMatthew G. Knepley       */
365089eabcffSMatthew G. Knepley       {
365189eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
365289eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
365389eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
365489eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
365589eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
365689eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
365789eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
365889eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
365989eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
366089eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
366189eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
366289eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
366389eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
366489eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
366589eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
366689eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
366789eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
366889eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
366989eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
367089eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
367189eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
367289eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
367389eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
367489eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
367589eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
367689eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
367789eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
367889eabcffSMatthew G. Knepley         PetscInt       o, n;
367989eabcffSMatthew G. Knepley 
368089eabcffSMatthew G. Knepley         /* Bottom Slice */
368189eabcffSMatthew G. Knepley         /*   bottom */
368289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
368389eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
368489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
368589eabcffSMatthew G. Knepley         /*   middle */
368689eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
368789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3688316b7f87SMax 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;}
368989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
36903194fc30SMatthew G. Knepley         }
369189eabcffSMatthew G. Knepley         /*   top */
369289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
369389eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
369489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
369589eabcffSMatthew G. Knepley 
369689eabcffSMatthew G. Knepley         /* Middle Slice */
369789eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
369889eabcffSMatthew G. Knepley           /*   bottom */
369989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
370089eabcffSMatthew 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;
370189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
370289eabcffSMatthew G. Knepley           /*   middle */
370389eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
370489eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
370589eabcffSMatthew 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;
370689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
370789eabcffSMatthew G. Knepley           }
370889eabcffSMatthew G. Knepley           /*   top */
370989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
371089eabcffSMatthew 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;
371189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
371289eabcffSMatthew G. Knepley         }
371389eabcffSMatthew G. Knepley 
371489eabcffSMatthew G. Knepley         /* Top Slice */
371589eabcffSMatthew G. Knepley         /*   bottom */
371689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
371789eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
371889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
371989eabcffSMatthew G. Knepley         /*   middle */
372089eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
372189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
372289eabcffSMatthew 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;
372389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
372489eabcffSMatthew G. Knepley         }
372589eabcffSMatthew G. Knepley         /*   top */
372689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
372789eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
372889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
372989eabcffSMatthew G. Knepley 
373089eabcffSMatthew G. Knepley         foffset = offset;
373189eabcffSMatthew G. Knepley       }
373289eabcffSMatthew G. Knepley       break;
373389eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
373489eabcffSMatthew G. Knepley     }
373589eabcffSMatthew G. Knepley   }
373689eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
37373194fc30SMatthew G. Knepley   /* Check permutation */
37383194fc30SMatthew G. Knepley   {
37393194fc30SMatthew G. Knepley     PetscInt *check;
37403194fc30SMatthew G. Knepley 
37413194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
37423194fc30SMatthew 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]);}
37433194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
37443194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
37453194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
37463194fc30SMatthew G. Knepley   }
37471fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
37483194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
37493194fc30SMatthew G. Knepley }
37503194fc30SMatthew G. Knepley 
3751e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3752e071409bSToby Isaac {
3753e071409bSToby Isaac   PetscDS        prob;
3754e071409bSToby Isaac   PetscInt       depth, Nf, h;
3755e071409bSToby Isaac   DMLabel        label;
3756e071409bSToby Isaac   PetscErrorCode ierr;
3757e071409bSToby Isaac 
3758e071409bSToby Isaac   PetscFunctionBeginHot;
3759e071409bSToby Isaac   prob    = dm->prob;
3760e071409bSToby Isaac   Nf      = prob->Nf;
3761e071409bSToby Isaac   label   = dm->depthLabel;
3762e071409bSToby Isaac   *dspace = NULL;
3763e071409bSToby Isaac   if (field < Nf) {
3764e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3765e071409bSToby Isaac 
3766e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3767e071409bSToby Isaac       PetscDualSpace dsp;
3768e071409bSToby Isaac 
3769e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3770e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3771e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3772e071409bSToby Isaac       h    = depth - 1 - h;
3773e071409bSToby Isaac       if (h) {
3774e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3775e071409bSToby Isaac       } else {
3776e071409bSToby Isaac         *dspace = dsp;
3777e071409bSToby Isaac       }
3778e071409bSToby Isaac     }
3779e071409bSToby Isaac   }
3780e071409bSToby Isaac   PetscFunctionReturn(0);
3781e071409bSToby Isaac }
3782e071409bSToby Isaac 
3783e071409bSToby Isaac 
37841a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3785a6dfd86eSKarl Rupp {
3786552f7358SJed Brown   PetscScalar    *array, *vArray;
3787d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
37881a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3789552f7358SJed Brown   PetscErrorCode  ierr;
3790552f7358SJed Brown 
37911b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
37922a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
37935a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
37945a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
37955a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
37963f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
37979df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
37989df71ca4SMatthew G. Knepley       PetscInt dof;
3799d9917b9dSMatthew G. Knepley 
38009df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38019df71ca4SMatthew G. Knepley       size += dof;
38029df71ca4SMatthew G. Knepley     }
38039df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
38049df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
38052a3aaacfSMatthew G. Knepley       PetscInt       dof;
38065a1bb5cfSMatthew G. Knepley 
38075a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
38082a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38095a1bb5cfSMatthew G. Knepley       size += dof;
38105a1bb5cfSMatthew G. Knepley     }
38113f7cbbe7SMatthew G. Knepley     if (!values) {
38123f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
38133f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
38143f7cbbe7SMatthew G. Knepley     }
381569291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
3816982e9ed1SMatthew G. Knepley   } else {
3817982e9ed1SMatthew G. Knepley     array = *values;
3818982e9ed1SMatthew G. Knepley   }
38199df71ca4SMatthew G. Knepley   size = 0;
38205a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
38219df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
38229df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
38239df71ca4SMatthew G. Knepley     PetscScalar *varr;
3824d9917b9dSMatthew G. Knepley 
38259df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38269df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
38279df71ca4SMatthew G. Knepley     varr = &vArray[off];
38281a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
38291a271a75SMatthew G. Knepley       array[offset] = varr[d];
38309df71ca4SMatthew G. Knepley     }
38319df71ca4SMatthew G. Knepley     size += dof;
38329df71ca4SMatthew G. Knepley   }
38339df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
38349df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
38359df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
38365a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
38375a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
38385a1bb5cfSMatthew G. Knepley 
383952ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
38405a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38415a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
38425a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
38435a1bb5cfSMatthew G. Knepley     if (o >= 0) {
38441a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
38451a271a75SMatthew G. Knepley         array[offset] = varr[d];
38465a1bb5cfSMatthew G. Knepley       }
38475a1bb5cfSMatthew G. Knepley     } else {
38481a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
38491a271a75SMatthew G. Knepley         array[offset] = varr[d];
38505a1bb5cfSMatthew G. Knepley       }
38515a1bb5cfSMatthew G. Knepley     }
38529df71ca4SMatthew G. Knepley     size += dof;
38535a1bb5cfSMatthew G. Knepley   }
38545a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
38559df71ca4SMatthew G. Knepley   if (!*values) {
38565a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
38575a1bb5cfSMatthew G. Knepley     *values = array;
38589df71ca4SMatthew G. Knepley   } else {
38598ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
38608c312ff3SMatthew G. Knepley     *csize = size;
38619df71ca4SMatthew G. Knepley   }
38625a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
38635a1bb5cfSMatthew G. Knepley }
3864d9917b9dSMatthew G. Knepley 
3865923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3866923c78e0SToby Isaac {
3867923c78e0SToby Isaac   const PetscInt *cla;
3868923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3869923c78e0SToby Isaac   PetscErrorCode ierr;
3870923c78e0SToby Isaac 
3871923c78e0SToby Isaac   PetscFunctionBeginHot;
3872923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3873923c78e0SToby Isaac   if (!*clPoints) {
3874923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3875923c78e0SToby Isaac 
3876923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3877923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3878923c78e0SToby Isaac     /* Compress out points not in the section */
3879923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3880923c78e0SToby Isaac       PetscInt r = pts[2*p];
3881923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3882923c78e0SToby Isaac         pts[q*2]   = r;
3883923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3884923c78e0SToby Isaac         ++q;
3885923c78e0SToby Isaac       }
3886923c78e0SToby Isaac     }
3887923c78e0SToby Isaac     np = q;
3888923c78e0SToby Isaac     cla = NULL;
3889923c78e0SToby Isaac   } else {
3890923c78e0SToby Isaac     PetscInt dof, off;
3891923c78e0SToby Isaac 
3892923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3893923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3894923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3895923c78e0SToby Isaac     np   = dof/2;
3896923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3897923c78e0SToby Isaac   }
3898923c78e0SToby Isaac   *numPoints = np;
3899923c78e0SToby Isaac   *points    = pts;
3900923c78e0SToby Isaac   *clp       = cla;
3901923c78e0SToby Isaac 
3902923c78e0SToby Isaac   PetscFunctionReturn(0);
3903923c78e0SToby Isaac }
3904923c78e0SToby Isaac 
3905923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3906923c78e0SToby Isaac {
3907923c78e0SToby Isaac   PetscErrorCode ierr;
3908923c78e0SToby Isaac 
3909923c78e0SToby Isaac   PetscFunctionBeginHot;
3910923c78e0SToby Isaac   if (!*clPoints) {
3911923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3912923c78e0SToby Isaac   } else {
3913923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3914923c78e0SToby Isaac   }
3915923c78e0SToby Isaac   *numPoints = 0;
3916923c78e0SToby Isaac   *points    = NULL;
3917923c78e0SToby Isaac   *clSec     = NULL;
3918923c78e0SToby Isaac   *clPoints  = NULL;
3919923c78e0SToby Isaac   *clp       = NULL;
3920923c78e0SToby Isaac   PetscFunctionReturn(0);
3921923c78e0SToby Isaac }
3922923c78e0SToby Isaac 
392397e99dd9SToby 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[])
39241a271a75SMatthew G. Knepley {
39251a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
392697e99dd9SToby Isaac   const PetscInt    **perms = NULL;
392797e99dd9SToby Isaac   const PetscScalar **flips = NULL;
39281a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39291a271a75SMatthew G. Knepley 
39301a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3931fe02ba77SJed Brown   *size = 0;
393297e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
393397e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
393497e99dd9SToby Isaac     const PetscInt    point = points[2*p];
393597e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
393697e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
39371a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
39381a271a75SMatthew G. Knepley     const PetscScalar *varr;
39391a271a75SMatthew G. Knepley 
39401a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39411a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
39421a271a75SMatthew G. Knepley     varr = &vArray[off];
394397e99dd9SToby Isaac     if (clperm) {
394497e99dd9SToby Isaac       if (perm) {
394597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
39461a271a75SMatthew G. Knepley       } else {
394797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
394897e99dd9SToby Isaac       }
394997e99dd9SToby Isaac       if (flip) {
395097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
395197e99dd9SToby Isaac       }
395297e99dd9SToby Isaac     } else {
395397e99dd9SToby Isaac       if (perm) {
395497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
395597e99dd9SToby Isaac       } else {
395697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
395797e99dd9SToby Isaac       }
395897e99dd9SToby Isaac       if (flip) {
395997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
39601a271a75SMatthew G. Knepley       }
39611a271a75SMatthew G. Knepley     }
396297e99dd9SToby Isaac     offset += dof;
396397e99dd9SToby Isaac   }
396497e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
39651a271a75SMatthew G. Knepley   *size = offset;
39661a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
39671a271a75SMatthew G. Knepley }
39681a271a75SMatthew G. Knepley 
396997e99dd9SToby 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[])
39701a271a75SMatthew G. Knepley {
39711a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
39721a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39731a271a75SMatthew G. Knepley 
39741a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3975fe02ba77SJed Brown   *size = 0;
39761a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
397797e99dd9SToby Isaac     PetscInt          p;
397897e99dd9SToby Isaac     const PetscInt    **perms = NULL;
397997e99dd9SToby Isaac     const PetscScalar **flips = NULL;
39801a271a75SMatthew G. Knepley 
398197e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
398297e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
398397e99dd9SToby Isaac       const PetscInt    point = points[2*p];
398497e99dd9SToby Isaac       PetscInt          fdof, foff, b;
39851a271a75SMatthew G. Knepley       const PetscScalar *varr;
398697e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
398797e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
39881a271a75SMatthew G. Knepley 
39891a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
39901a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
39911a271a75SMatthew G. Knepley       varr = &vArray[foff];
399297e99dd9SToby Isaac       if (clperm) {
399397e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
399497e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
399597e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
39961a271a75SMatthew G. Knepley       } else {
399797e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
399897e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
399997e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
40001a271a75SMatthew G. Knepley       }
400197e99dd9SToby Isaac       offset += fdof;
40021a271a75SMatthew G. Knepley     }
400397e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
40041a271a75SMatthew G. Knepley   }
40051a271a75SMatthew G. Knepley   *size = offset;
40061a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
40071a271a75SMatthew G. Knepley }
40081a271a75SMatthew G. Knepley 
4009552f7358SJed Brown /*@C
4010552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4011552f7358SJed Brown 
4012552f7358SJed Brown   Not collective
4013552f7358SJed Brown 
4014552f7358SJed Brown   Input Parameters:
4015552f7358SJed Brown + dm - The DM
4016552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4017552f7358SJed Brown . v - The local vector
401822c1ee49SMatthew G. Knepley . point - The point in the DM
401922c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
402022c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4021552f7358SJed Brown 
4022552f7358SJed Brown   Output Parameters:
402322c1ee49SMatthew G. Knepley + csize - The number of values in the closure
402422c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
402522c1ee49SMatthew G. Knepley 
402622c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
402722c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
402822c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
402922c1ee49SMatthew G. Knepley $
403022c1ee49SMatthew G. Knepley $ A typical use could be
403122c1ee49SMatthew G. Knepley $
403222c1ee49SMatthew G. Knepley $  values = NULL;
403322c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
403422c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
403522c1ee49SMatthew G. Knepley $    <Compute on closure>
403622c1ee49SMatthew G. Knepley $  }
403722c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
403822c1ee49SMatthew G. Knepley $
403922c1ee49SMatthew G. Knepley $ or
404022c1ee49SMatthew G. Knepley $
404122c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
404222c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
404322c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
404422c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
404522c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
404622c1ee49SMatthew G. Knepley $      <Compute on closure>
404722c1ee49SMatthew G. Knepley $    }
404822c1ee49SMatthew G. Knepley $  }
404922c1ee49SMatthew G. Knepley $  PetscFree(values);
4050552f7358SJed Brown 
4051552f7358SJed Brown   Fortran Notes:
4052552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4053552f7358SJed Brown   include petsc.h90 in your code.
4054552f7358SJed Brown 
4055552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4056552f7358SJed Brown 
4057552f7358SJed Brown   Level: intermediate
4058552f7358SJed Brown 
4059552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4060552f7358SJed Brown @*/
4061552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4062552f7358SJed Brown {
4063552f7358SJed Brown   PetscSection       clSection;
4064d9917b9dSMatthew G. Knepley   IS                 clPoints;
40658a84db2dSMatthew G. Knepley   PetscScalar       *array;
40668a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4067552f7358SJed Brown   PetscInt          *points = NULL;
40688f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
40691a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4070552f7358SJed Brown   PetscErrorCode     ierr;
4071552f7358SJed Brown 
4072d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4073552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4074552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
40751a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
40761a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4077552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4078552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4079552f7358SJed Brown   if (depth == 1 && numFields < 2) {
40801a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4081552f7358SJed Brown     PetscFunctionReturn(0);
4082552f7358SJed Brown   }
40831a271a75SMatthew G. Knepley   /* Get points */
4084923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40851fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
40861a271a75SMatthew G. Knepley   /* Get array */
4087bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
40881a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4089552f7358SJed Brown 
40901a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4091552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
40921a271a75SMatthew G. Knepley       asize += dof;
4093552f7358SJed Brown     }
4094bd6c0d32SMatthew G. Knepley     if (!values) {
4095923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40961a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4097bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4098bd6c0d32SMatthew G. Knepley     }
409969291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4100d0f6b257SMatthew G. Knepley   } else {
4101d0f6b257SMatthew G. Knepley     array = *values;
4102d0f6b257SMatthew G. Knepley   }
41038a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
41041a271a75SMatthew G. Knepley   /* Get values */
410597e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
410697e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
41071a271a75SMatthew G. Knepley   /* Cleanup points */
4108923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41091a271a75SMatthew G. Knepley   /* Cleanup array */
41108a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4111d0f6b257SMatthew G. Knepley   if (!*values) {
4112552f7358SJed Brown     if (csize) *csize = size;
4113552f7358SJed Brown     *values = array;
4114d0f6b257SMatthew G. Knepley   } else {
4115f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4116d0f6b257SMatthew G. Knepley     *csize = size;
4117d0f6b257SMatthew G. Knepley   }
4118552f7358SJed Brown   PetscFunctionReturn(0);
4119552f7358SJed Brown }
4120552f7358SJed Brown 
4121552f7358SJed Brown /*@C
4122552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4123552f7358SJed Brown 
4124552f7358SJed Brown   Not collective
4125552f7358SJed Brown 
4126552f7358SJed Brown   Input Parameters:
4127552f7358SJed Brown + dm - The DM
41280298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4129552f7358SJed Brown . v - The local vector
4130eaf898f9SPatrick Sanan . point - The point in the DM
41310298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4132552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4133552f7358SJed Brown 
413422c1ee49SMatthew 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()
413522c1ee49SMatthew G. Knepley 
41363813dfbdSMatthew G Knepley   Fortran Notes:
41373813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
41383813dfbdSMatthew G Knepley   include petsc.h90 in your code.
41393813dfbdSMatthew G Knepley 
41403813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
41413813dfbdSMatthew G Knepley 
4142552f7358SJed Brown   Level: intermediate
4143552f7358SJed Brown 
4144552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4145552f7358SJed Brown @*/
41467c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4147a6dfd86eSKarl Rupp {
4148552f7358SJed Brown   PetscInt       size = 0;
4149552f7358SJed Brown   PetscErrorCode ierr;
4150552f7358SJed Brown 
4151552f7358SJed Brown   PetscFunctionBegin;
4152552f7358SJed Brown   /* Should work without recalculating size */
415369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4154c9fdaa05SMatthew G. Knepley   *values = NULL;
4155552f7358SJed Brown   PetscFunctionReturn(0);
4156552f7358SJed Brown }
4157552f7358SJed Brown 
4158552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4159552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4160552f7358SJed Brown 
416197e99dd9SToby 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[])
4162552f7358SJed Brown {
4163552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4164552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4165552f7358SJed Brown   PetscScalar    *a;
4166552f7358SJed Brown   PetscInt        off, cind = 0, k;
4167552f7358SJed Brown   PetscErrorCode  ierr;
4168552f7358SJed Brown 
4169552f7358SJed Brown   PetscFunctionBegin;
4170552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4171552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4172552f7358SJed Brown   a    = &array[off];
4173552f7358SJed Brown   if (!cdof || setBC) {
417497e99dd9SToby Isaac     if (clperm) {
417597e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
417697e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4177552f7358SJed Brown     } else {
417897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
417997e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4180552f7358SJed Brown     }
4181552f7358SJed Brown   } else {
4182552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
418397e99dd9SToby Isaac     if (clperm) {
418497e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4185552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
418697e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4187552f7358SJed Brown         }
4188552f7358SJed Brown       } else {
4189552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4190552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
419197e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
419297e99dd9SToby Isaac         }
419397e99dd9SToby Isaac       }
419497e99dd9SToby Isaac     } else {
419597e99dd9SToby Isaac       if (perm) {
419697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
419797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
419897e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
419997e99dd9SToby Isaac         }
420097e99dd9SToby Isaac       } else {
420197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
420297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
420397e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
420497e99dd9SToby Isaac         }
4205552f7358SJed Brown       }
4206552f7358SJed Brown     }
4207552f7358SJed Brown   }
4208552f7358SJed Brown   PetscFunctionReturn(0);
4209552f7358SJed Brown }
4210552f7358SJed Brown 
421197e99dd9SToby 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[])
4212a5e93ea8SMatthew G. Knepley {
4213a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4214a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4215a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4216a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4217a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4218a5e93ea8SMatthew G. Knepley 
4219a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4220a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4221a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4222a5e93ea8SMatthew G. Knepley   a    = &array[off];
4223a5e93ea8SMatthew G. Knepley   if (cdof) {
4224a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
422597e99dd9SToby Isaac     if (clperm) {
422697e99dd9SToby Isaac       if (perm) {
4227a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4228a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
422997e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
423097e99dd9SToby Isaac             cind++;
4231a5e93ea8SMatthew G. Knepley           }
4232a5e93ea8SMatthew G. Knepley         }
4233a5e93ea8SMatthew G. Knepley       } else {
4234a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4235a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
423697e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
423797e99dd9SToby Isaac             cind++;
423897e99dd9SToby Isaac           }
423997e99dd9SToby Isaac         }
424097e99dd9SToby Isaac       }
424197e99dd9SToby Isaac     } else {
424297e99dd9SToby Isaac       if (perm) {
424397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
424497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
424597e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
424697e99dd9SToby Isaac             cind++;
424797e99dd9SToby Isaac           }
424897e99dd9SToby Isaac         }
424997e99dd9SToby Isaac       } else {
425097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
425197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
425297e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
425397e99dd9SToby Isaac             cind++;
425497e99dd9SToby Isaac           }
4255a5e93ea8SMatthew G. Knepley         }
4256a5e93ea8SMatthew G. Knepley       }
4257a5e93ea8SMatthew G. Knepley     }
4258a5e93ea8SMatthew G. Knepley   }
4259a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4260a5e93ea8SMatthew G. Knepley }
4261a5e93ea8SMatthew G. Knepley 
426297e99dd9SToby 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[])
4263a6dfd86eSKarl Rupp {
4264552f7358SJed Brown   PetscScalar    *a;
42651a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
42661a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
426797e99dd9SToby Isaac   PetscInt        cind = 0, b;
4268552f7358SJed Brown   PetscErrorCode  ierr;
4269552f7358SJed Brown 
4270552f7358SJed Brown   PetscFunctionBegin;
4271552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4272552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
42731a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
42741a271a75SMatthew G. Knepley   a    = &array[foff];
4275552f7358SJed Brown   if (!fcdof || setBC) {
427697e99dd9SToby Isaac     if (clperm) {
427797e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
427897e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4279552f7358SJed Brown     } else {
428097e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
428197e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4282552f7358SJed Brown     }
4283552f7358SJed Brown   } else {
4284552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
428597e99dd9SToby Isaac     if (clperm) {
428697e99dd9SToby Isaac       if (perm) {
428797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
428897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
428997e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4290552f7358SJed Brown         }
4291552f7358SJed Brown       } else {
429297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
429397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
429497e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
429597e99dd9SToby Isaac         }
429697e99dd9SToby Isaac       }
429797e99dd9SToby Isaac     } else {
429897e99dd9SToby Isaac       if (perm) {
429997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
430097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
430197e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
430297e99dd9SToby Isaac         }
430397e99dd9SToby Isaac       } else {
430497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
430597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
430697e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4307552f7358SJed Brown         }
4308552f7358SJed Brown       }
4309552f7358SJed Brown     }
4310552f7358SJed Brown   }
43111a271a75SMatthew G. Knepley   *offset += fdof;
4312552f7358SJed Brown   PetscFunctionReturn(0);
4313552f7358SJed Brown }
4314552f7358SJed Brown 
4315ba322698SMatthew 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[])
4316a5e93ea8SMatthew G. Knepley {
4317a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
43181a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
43191a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4320ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4321ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4322a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4323a5e93ea8SMatthew G. Knepley 
4324a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4325a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4326a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
43271a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43281a271a75SMatthew G. Knepley   a    = &array[foff];
4329a5e93ea8SMatthew G. Knepley   if (fcdof) {
4330ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4331a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
433297e99dd9SToby Isaac     if (clperm) {
433397e99dd9SToby Isaac       if (perm) {
4334ba322698SMatthew G. Knepley         if (comps) {
4335ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4336ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4337ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4338ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4339ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4340ba322698SMatthew G. Knepley           }
4341ba322698SMatthew G. Knepley         } else {
434297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
434397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
434497e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4345a5e93ea8SMatthew G. Knepley               ++cind;
4346a5e93ea8SMatthew G. Knepley             }
4347a5e93ea8SMatthew G. Knepley           }
4348ba322698SMatthew G. Knepley         }
4349ba322698SMatthew G. Knepley       } else {
4350ba322698SMatthew G. Knepley         if (comps) {
4351ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4352ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4353ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4354ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4355ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4356ba322698SMatthew G. Knepley           }
4357a5e93ea8SMatthew G. Knepley         } else {
435897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
435997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
436097e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
436197e99dd9SToby Isaac               ++cind;
436297e99dd9SToby Isaac             }
436397e99dd9SToby Isaac           }
436497e99dd9SToby Isaac         }
4365ba322698SMatthew G. Knepley       }
436697e99dd9SToby Isaac     } else {
436797e99dd9SToby Isaac       if (perm) {
4368ba322698SMatthew G. Knepley         if (comps) {
4369ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4370ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4371ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4372ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4373ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4374ba322698SMatthew G. Knepley           }
4375ba322698SMatthew G. Knepley         } else {
437697e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
437797e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
437897e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
437997e99dd9SToby Isaac               ++cind;
438097e99dd9SToby Isaac             }
438197e99dd9SToby Isaac           }
4382ba322698SMatthew G. Knepley         }
4383ba322698SMatthew G. Knepley       } else {
4384ba322698SMatthew G. Knepley         if (comps) {
4385ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4386ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4387ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4388ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4389ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4390ba322698SMatthew G. Knepley           }
439197e99dd9SToby Isaac         } else {
439297e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
439397e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
439497e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4395a5e93ea8SMatthew G. Knepley               ++cind;
4396a5e93ea8SMatthew G. Knepley             }
4397a5e93ea8SMatthew G. Knepley           }
4398a5e93ea8SMatthew G. Knepley         }
4399a5e93ea8SMatthew G. Knepley       }
4400a5e93ea8SMatthew G. Knepley     }
4401ba322698SMatthew G. Knepley   }
44021a271a75SMatthew G. Knepley   *offset += fdof;
4403a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4404a5e93ea8SMatthew G. Knepley }
4405a5e93ea8SMatthew G. Knepley 
44068f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4407a6dfd86eSKarl Rupp {
4408552f7358SJed Brown   PetscScalar    *array;
44091b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
44101b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4411552f7358SJed Brown   PetscErrorCode  ierr;
4412552f7358SJed Brown 
44131b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4414b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4415b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4416b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4417b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4418b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4419b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4420b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4421b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4422b6ebb6e6SMatthew G. Knepley 
4423b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4424b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4425b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4426b6ebb6e6SMatthew G. Knepley     {
4427b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4428b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4429b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4430b6ebb6e6SMatthew G. Knepley 
4431b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4432b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4433b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4434b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4435b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4436b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4437b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4438b6ebb6e6SMatthew G. Knepley           }
4439b6ebb6e6SMatthew G. Knepley         } else {
4440b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4441b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4442b6ebb6e6SMatthew G. Knepley           }
4443b6ebb6e6SMatthew G. Knepley         }
4444b6ebb6e6SMatthew G. Knepley       } else {
4445b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4446b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4447b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4448b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4449b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4450b6ebb6e6SMatthew G. Knepley           }
4451b6ebb6e6SMatthew G. Knepley         } else {
4452b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4453b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4454b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4455b6ebb6e6SMatthew G. Knepley           }
4456b6ebb6e6SMatthew G. Knepley         }
4457b6ebb6e6SMatthew G. Knepley       }
4458b6ebb6e6SMatthew G. Knepley     }
4459b6ebb6e6SMatthew G. Knepley   }
4460b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4461b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4462b6ebb6e6SMatthew G. Knepley }
44631b406b76SMatthew G. Knepley 
44641b406b76SMatthew G. Knepley /*@C
44651b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
44661b406b76SMatthew G. Knepley 
44671b406b76SMatthew G. Knepley   Not collective
44681b406b76SMatthew G. Knepley 
44691b406b76SMatthew G. Knepley   Input Parameters:
44701b406b76SMatthew G. Knepley + dm - The DM
44711b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
44721b406b76SMatthew G. Knepley . v - The local vector
4473eaf898f9SPatrick Sanan . point - The point in the DM
44741b406b76SMatthew G. Knepley . values - The array of values
447522c1ee49SMatthew 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,
447622c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
44771b406b76SMatthew G. Knepley 
44781b406b76SMatthew G. Knepley   Fortran Notes:
44791b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
44801b406b76SMatthew G. Knepley 
44811b406b76SMatthew G. Knepley   Level: intermediate
44821b406b76SMatthew G. Knepley 
44831b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
44841b406b76SMatthew G. Knepley @*/
44851b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
44861b406b76SMatthew G. Knepley {
44871b406b76SMatthew G. Knepley   PetscSection    clSection;
44881b406b76SMatthew G. Knepley   IS              clPoints;
44891b406b76SMatthew G. Knepley   PetscScalar    *array;
44901b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
449197e99dd9SToby Isaac   const PetscInt *clp, *clperm;
44921a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
44931b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
44941b406b76SMatthew G. Knepley 
44951a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
44961b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44971b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
44981a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44991a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
45001b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
45011b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
45021b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
45038f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
45041b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
45051b406b76SMatthew G. Knepley   }
45061a271a75SMatthew G. Knepley   /* Get points */
450797e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4508923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
45091a271a75SMatthew G. Knepley   /* Get array */
4510552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
45111a271a75SMatthew G. Knepley   /* Get values */
4512ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
451397e99dd9SToby Isaac     PetscInt offset = 0, f;
4514552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
451597e99dd9SToby Isaac       const PetscInt    **perms = NULL;
451697e99dd9SToby Isaac       const PetscScalar **flips = NULL;
451797e99dd9SToby Isaac 
451897e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4519552f7358SJed Brown       switch (mode) {
4520552f7358SJed Brown       case INSERT_VALUES:
452197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
452297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
452397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
452497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
452597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4526552f7358SJed Brown         } break;
4527552f7358SJed Brown       case INSERT_ALL_VALUES:
452897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
452997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
453097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
453197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
453297e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4533552f7358SJed Brown         } break;
4534a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
453597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
453697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
453797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
453897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4539ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4540a5e93ea8SMatthew G. Knepley         } break;
4541552f7358SJed Brown       case ADD_VALUES:
454297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
454397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
454497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
454597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
454697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4547552f7358SJed Brown         } break;
4548552f7358SJed Brown       case ADD_ALL_VALUES:
454997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
455097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
455197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
455297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
455397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4554552f7358SJed Brown         } break;
4555304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
455697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
455797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
455897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
455997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4560ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4561304ab55fSMatthew G. Knepley         } break;
4562552f7358SJed Brown       default:
4563f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4564552f7358SJed Brown       }
456597e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
45661a271a75SMatthew G. Knepley     }
4567552f7358SJed Brown   } else {
45681a271a75SMatthew G. Knepley     PetscInt dof, off;
456997e99dd9SToby Isaac     const PetscInt    **perms = NULL;
457097e99dd9SToby Isaac     const PetscScalar **flips = NULL;
45711a271a75SMatthew G. Knepley 
457297e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4573552f7358SJed Brown     switch (mode) {
4574552f7358SJed Brown     case INSERT_VALUES:
457597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
457697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
457797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
457897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
457997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
458097e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4581552f7358SJed Brown       } break;
4582552f7358SJed Brown     case INSERT_ALL_VALUES:
458397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
458497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
458597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
458697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
458797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
458897e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4589552f7358SJed Brown       } break;
4590a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
459197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
459297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
459397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
459497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
459597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
459697e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4597a5e93ea8SMatthew G. Knepley       } break;
4598552f7358SJed Brown     case ADD_VALUES:
459997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
460097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
460197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
460297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
460397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
460497e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4605552f7358SJed Brown       } break;
4606552f7358SJed Brown     case ADD_ALL_VALUES:
460797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
460897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
460997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
461097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
461197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
461297e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4613552f7358SJed Brown       } break;
4614304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
461597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
461697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
461797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
461897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
461997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
462097e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4621304ab55fSMatthew G. Knepley       } break;
4622552f7358SJed Brown     default:
4623f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4624552f7358SJed Brown     }
462597e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4626552f7358SJed Brown   }
46271a271a75SMatthew G. Knepley   /* Cleanup points */
4628923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
46291a271a75SMatthew G. Knepley   /* Cleanup array */
4630552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4631552f7358SJed Brown   PetscFunctionReturn(0);
4632552f7358SJed Brown }
4633552f7358SJed Brown 
4634ba322698SMatthew 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)
4635e07394fbSMatthew G. Knepley {
4636e07394fbSMatthew G. Knepley   PetscSection      clSection;
4637e07394fbSMatthew G. Knepley   IS                clPoints;
4638e07394fbSMatthew G. Knepley   PetscScalar       *array;
4639e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4640b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4641e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
464297e99dd9SToby Isaac   PetscInt          offset = 0, f;
4643e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4644e07394fbSMatthew G. Knepley 
4645e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4646e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4647e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4648e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4649e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4650e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4651e07394fbSMatthew G. Knepley   /* Get points */
4652b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4653923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4654e07394fbSMatthew G. Knepley   /* Get array */
4655e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4656e07394fbSMatthew G. Knepley   /* Get values */
4657e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
465897e99dd9SToby Isaac     const PetscInt    **perms = NULL;
465997e99dd9SToby Isaac     const PetscScalar **flips = NULL;
466097e99dd9SToby Isaac 
4661e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4662e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4663e07394fbSMatthew G. Knepley         PetscInt fdof;
4664e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4665e07394fbSMatthew G. Knepley         offset += fdof;
4666e07394fbSMatthew G. Knepley       }
4667e07394fbSMatthew G. Knepley       continue;
4668e07394fbSMatthew G. Knepley     }
466997e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4670e07394fbSMatthew G. Knepley     switch (mode) {
4671e07394fbSMatthew G. Knepley     case INSERT_VALUES:
467297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
467397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
467497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
467597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4676b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4677e07394fbSMatthew G. Knepley       } break;
4678e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
467997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
468097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
468197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
468297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4683b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4684e07394fbSMatthew G. Knepley         } break;
4685e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
468697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
468797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
468897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
468997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4690ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
4691e07394fbSMatthew G. Knepley       } break;
4692e07394fbSMatthew G. Knepley     case ADD_VALUES:
469397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
469497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
469597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
469697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4697b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4698e07394fbSMatthew G. Knepley       } break;
4699e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
470097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
470197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
470297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
470397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4704b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4705e07394fbSMatthew G. Knepley       } break;
4706e07394fbSMatthew G. Knepley     default:
4707f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4708e07394fbSMatthew G. Knepley     }
470997e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4710e07394fbSMatthew G. Knepley   }
4711e07394fbSMatthew G. Knepley   /* Cleanup points */
4712923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4713e07394fbSMatthew G. Knepley   /* Cleanup array */
4714e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4715e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4716e07394fbSMatthew G. Knepley }
4717e07394fbSMatthew G. Knepley 
47187cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4719552f7358SJed Brown {
4720552f7358SJed Brown   PetscMPIInt    rank;
4721552f7358SJed Brown   PetscInt       i, j;
4722552f7358SJed Brown   PetscErrorCode ierr;
4723552f7358SJed Brown 
4724552f7358SJed Brown   PetscFunctionBegin;
472582f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4726eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
4727e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4728e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4729b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4730b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4731e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4732b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4733519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
47347eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4735552f7358SJed Brown #else
4736b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4737552f7358SJed Brown #endif
4738552f7358SJed Brown     }
473977a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4740552f7358SJed Brown   }
4741552f7358SJed Brown   PetscFunctionReturn(0);
4742552f7358SJed Brown }
4743552f7358SJed Brown 
4744552f7358SJed Brown /* . off - The global offset of this point */
47454acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4746a6dfd86eSKarl Rupp {
4747e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4748552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4749552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4750552f7358SJed Brown   PetscInt        cind = 0, k;
4751552f7358SJed Brown   PetscErrorCode  ierr;
4752552f7358SJed Brown 
4753552f7358SJed Brown   PetscFunctionBegin;
4754552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4755552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4756552f7358SJed Brown   if (!cdof || setBC) {
47574acb8e1eSToby Isaac     if (perm) {
47584acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4759552f7358SJed Brown     } else {
47604acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4761552f7358SJed Brown     }
4762552f7358SJed Brown   } else {
4763552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
47644acb8e1eSToby Isaac     if (perm) {
47654acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
47664acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
47674acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
47684acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
47694acb8e1eSToby Isaac           ++cind;
47704acb8e1eSToby Isaac         } else {
47714acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
47724acb8e1eSToby Isaac         }
47734acb8e1eSToby Isaac       }
47744acb8e1eSToby Isaac     } else {
4775552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4776552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4777552f7358SJed Brown           /* Insert check for returning constrained indices */
4778e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4779552f7358SJed Brown           ++cind;
4780552f7358SJed Brown         } else {
4781e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4782552f7358SJed Brown         }
4783552f7358SJed Brown       }
4784552f7358SJed Brown     }
4785552f7358SJed Brown   }
4786e6ccafaeSMatthew G Knepley   *loff += dof;
4787552f7358SJed Brown   PetscFunctionReturn(0);
4788552f7358SJed Brown }
4789552f7358SJed Brown 
4790552f7358SJed Brown /* . off - The global offset of this point */
47914acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4792a6dfd86eSKarl Rupp {
4793552f7358SJed Brown   PetscInt       numFields, foff, f;
4794552f7358SJed Brown   PetscErrorCode ierr;
4795552f7358SJed Brown 
4796552f7358SJed Brown   PetscFunctionBegin;
4797552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4798552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
47994acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4800552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
48014acb8e1eSToby Isaac     PetscInt        cind = 0, b;
48024acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4803552f7358SJed Brown 
4804552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4805552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4806552f7358SJed Brown     if (!cfdof || setBC) {
48074acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
48084acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4809552f7358SJed Brown     } else {
4810552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
48114acb8e1eSToby Isaac       if (perm) {
48124acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
48134acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
48144acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4815552f7358SJed Brown             ++cind;
4816552f7358SJed Brown           } else {
48174acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4818552f7358SJed Brown           }
4819552f7358SJed Brown         }
4820552f7358SJed Brown       } else {
48214acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
48224acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
48234acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4824552f7358SJed Brown             ++cind;
4825552f7358SJed Brown           } else {
48264acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4827552f7358SJed Brown           }
4828552f7358SJed Brown         }
4829552f7358SJed Brown       }
4830552f7358SJed Brown     }
4831a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4832552f7358SJed Brown     foffs[f] += fdof;
4833552f7358SJed Brown   }
4834552f7358SJed Brown   PetscFunctionReturn(0);
4835552f7358SJed Brown }
4836552f7358SJed Brown 
48374acb8e1eSToby 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)
4838d3d1a6afSToby Isaac {
4839d3d1a6afSToby Isaac   Mat             cMat;
4840d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4841d3d1a6afSToby Isaac   IS              aIS;
4842d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4843d3d1a6afSToby Isaac   const PetscInt  *anchors;
4844e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4845d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4846d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4847d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4848d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4849d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4850d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4851d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
48526ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4853d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4854d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4855d3d1a6afSToby Isaac 
4856d3d1a6afSToby Isaac   PetscFunctionBegin;
4857d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4858d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4859d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4860d3d1a6afSToby Isaac 
4861a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4862d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4863d3d1a6afSToby Isaac   if (aSec) {
4864d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4865d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4866d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4867d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4868d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4869d3d1a6afSToby Isaac      * into the global matrix anyway) */
4870d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4871d3d1a6afSToby Isaac       PetscInt b    = points[p];
48724b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4873d3d1a6afSToby Isaac 
48744b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
48754b2f2278SToby Isaac       if (!bSecDof) {
48764b2f2278SToby Isaac         continue;
48774b2f2278SToby Isaac       }
4878d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4879d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4880d3d1a6afSToby Isaac       }
4881d3d1a6afSToby Isaac       if (bDof) {
4882d3d1a6afSToby Isaac         /* this point is constrained */
4883d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4884d3d1a6afSToby Isaac         PetscInt bOff, q;
4885d3d1a6afSToby Isaac 
4886d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4887d3d1a6afSToby Isaac         newNumPoints  += bDof;
4888d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4889d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4890d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4891d3d1a6afSToby Isaac           PetscInt aDof;
4892d3d1a6afSToby Isaac 
4893d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4894d3d1a6afSToby Isaac           newNumIndices += aDof;
4895d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4896d3d1a6afSToby Isaac             PetscInt fDof;
4897d3d1a6afSToby Isaac 
4898d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4899d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4900d3d1a6afSToby Isaac           }
4901d3d1a6afSToby Isaac         }
4902d3d1a6afSToby Isaac       }
4903d3d1a6afSToby Isaac       else {
4904d3d1a6afSToby Isaac         /* this point is not constrained */
4905d3d1a6afSToby Isaac         newNumPoints++;
49064b2f2278SToby Isaac         newNumIndices += bSecDof;
4907d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4908d3d1a6afSToby Isaac           PetscInt fDof;
4909d3d1a6afSToby Isaac 
4910d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4911d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4912d3d1a6afSToby Isaac         }
4913d3d1a6afSToby Isaac       }
4914d3d1a6afSToby Isaac     }
4915d3d1a6afSToby Isaac   }
4916d3d1a6afSToby Isaac   if (!anyConstrained) {
491772b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
491872b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
491972b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
492072b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
492172b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4922d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4923d3d1a6afSToby Isaac   }
4924d3d1a6afSToby Isaac 
49256ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
49266ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
49276ecaa68aSToby Isaac 
4928f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4929d3d1a6afSToby Isaac 
49306ecaa68aSToby Isaac   if (!outPoints && !outValues) {
49316ecaa68aSToby Isaac     if (offsets) {
49326ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
49336ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
49346ecaa68aSToby Isaac       }
49356ecaa68aSToby Isaac     }
49366ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
49376ecaa68aSToby Isaac     PetscFunctionReturn(0);
49386ecaa68aSToby Isaac   }
49396ecaa68aSToby Isaac 
4940f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4941d3d1a6afSToby Isaac 
4942f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4943d3d1a6afSToby Isaac 
4944d3d1a6afSToby Isaac   /* workspaces */
4945d3d1a6afSToby Isaac   if (numFields) {
4946d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
494769291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
494869291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4949d3d1a6afSToby Isaac     }
4950d3d1a6afSToby Isaac   }
4951d3d1a6afSToby Isaac   else {
495269291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
495369291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4954d3d1a6afSToby Isaac   }
4955d3d1a6afSToby Isaac 
4956d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4957d3d1a6afSToby Isaac   if (numFields) {
49584b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
49594b2f2278SToby Isaac 
4960d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4961d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
49624b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4963d3d1a6afSToby Isaac 
49644b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
49654b2f2278SToby Isaac       if (!bSecDof) {
49664b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
49674b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
49684b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
49694b2f2278SToby Isaac         }
49704b2f2278SToby Isaac         continue;
49714b2f2278SToby Isaac       }
4972d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4973d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4974d3d1a6afSToby Isaac       }
4975d3d1a6afSToby Isaac       if (bDof) {
4976d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4977d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4978d3d1a6afSToby Isaac 
4979d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4980d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4981d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4982d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4983d3d1a6afSToby Isaac             PetscInt aFDof;
4984d3d1a6afSToby Isaac 
4985d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4986d3d1a6afSToby Isaac             allFDof += aFDof;
4987d3d1a6afSToby Isaac           }
4988d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4989d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4990d3d1a6afSToby Isaac         }
4991d3d1a6afSToby Isaac       }
4992d3d1a6afSToby Isaac       else {
4993d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4994d3d1a6afSToby Isaac           PetscInt fDof;
4995d3d1a6afSToby Isaac 
4996d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4997d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4998d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4999d3d1a6afSToby Isaac         }
5000d3d1a6afSToby Isaac       }
5001d3d1a6afSToby Isaac     }
50024b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
50034b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
50044b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5005d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5006d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5007d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5008d3d1a6afSToby Isaac       }
500919f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
501019f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
501169291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5012d3d1a6afSToby Isaac     }
5013d3d1a6afSToby Isaac   }
5014d3d1a6afSToby Isaac   else {
5015d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5016d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
50174b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5018d3d1a6afSToby Isaac 
50194b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
50204b2f2278SToby Isaac       if (!bSecDof) {
50214b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
50224b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
50234b2f2278SToby Isaac         continue;
50244b2f2278SToby Isaac       }
5025d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5026d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5027d3d1a6afSToby Isaac       }
5028d3d1a6afSToby Isaac       if (bDof) {
50294b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5030d3d1a6afSToby Isaac 
5031d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5032d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5033d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5034d3d1a6afSToby Isaac 
5035d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5036d3d1a6afSToby Isaac           allDof += aDof;
5037d3d1a6afSToby Isaac         }
5038d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
50394b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5040d3d1a6afSToby Isaac       }
5041d3d1a6afSToby Isaac       else {
50424b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5043d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5044d3d1a6afSToby Isaac       }
5045d3d1a6afSToby Isaac     }
5046d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5047d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5048d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5049d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5050d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5051d3d1a6afSToby Isaac     }
505269291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5053d3d1a6afSToby Isaac   }
5054d3d1a6afSToby Isaac 
50556ecaa68aSToby Isaac   /* output arrays */
505669291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
50576ecaa68aSToby Isaac 
5058d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5059d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5060d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
506169291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
506269291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5063d3d1a6afSToby Isaac   if (numFields) {
5064d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5065d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5066d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
50674b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5068d3d1a6afSToby Isaac 
50694b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
50704b2f2278SToby Isaac       if (!bSecDof) {
50714b2f2278SToby Isaac         continue;
50724b2f2278SToby Isaac       }
5073d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5074d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5075d3d1a6afSToby Isaac       }
5076d3d1a6afSToby Isaac       if (bDof) {
5077d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5078d3d1a6afSToby Isaac 
5079d3d1a6afSToby Isaac         fStart[0] = 0;
5080d3d1a6afSToby Isaac         fEnd[0]   = 0;
5081d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5082d3d1a6afSToby Isaac           PetscInt fDof;
5083d3d1a6afSToby Isaac 
5084d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5085d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5086d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5087d3d1a6afSToby Isaac         }
5088d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
50894acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
5090d3d1a6afSToby Isaac 
5091d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5092d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5093d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5094d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5095d3d1a6afSToby Isaac 
5096d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5097d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5098d3d1a6afSToby Isaac         }
5099d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5100d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5101d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5102d3d1a6afSToby Isaac 
5103d3d1a6afSToby 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 */
5104d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5105d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5106302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
51074acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
5108d3d1a6afSToby Isaac         }
5109d3d1a6afSToby Isaac         newP += bDof;
5110d3d1a6afSToby Isaac 
51116ecaa68aSToby Isaac         if (outValues) {
5112d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5113d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5114d3d1a6afSToby 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);
5115d3d1a6afSToby Isaac           }
5116d3d1a6afSToby Isaac         }
51176ecaa68aSToby Isaac       }
5118d3d1a6afSToby Isaac       else {
5119d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5120d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5121d3d1a6afSToby Isaac         newP++;
5122d3d1a6afSToby Isaac       }
5123d3d1a6afSToby Isaac     }
5124d3d1a6afSToby Isaac   } else {
5125d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5126d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5127d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
51284b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5129d3d1a6afSToby Isaac 
51304b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
51314b2f2278SToby Isaac       if (!bSecDof) {
51324b2f2278SToby Isaac         continue;
51334b2f2278SToby Isaac       }
5134d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5135d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5136d3d1a6afSToby Isaac       }
5137d3d1a6afSToby Isaac       if (bDof) {
5138d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5139d3d1a6afSToby Isaac 
5140d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
51414acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
5142d3d1a6afSToby Isaac 
5143d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5144d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5145d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5146d3d1a6afSToby Isaac 
5147d3d1a6afSToby 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 */
5148d3d1a6afSToby Isaac 
5149d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5150d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5151302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
51524acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
5153d3d1a6afSToby Isaac         }
5154d3d1a6afSToby Isaac         newP += bDof;
5155d3d1a6afSToby Isaac 
5156d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
51576ecaa68aSToby Isaac         if (outValues) {
5158d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5159d3d1a6afSToby Isaac         }
51606ecaa68aSToby Isaac       }
5161d3d1a6afSToby Isaac       else {
5162d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5163d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5164d3d1a6afSToby Isaac         newP++;
5165d3d1a6afSToby Isaac       }
5166d3d1a6afSToby Isaac     }
5167d3d1a6afSToby Isaac   }
5168d3d1a6afSToby Isaac 
51696ecaa68aSToby Isaac   if (outValues) {
517069291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5171d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5172d3d1a6afSToby Isaac     /* multiply constraints on the right */
5173d3d1a6afSToby Isaac     if (numFields) {
5174d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5175d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5176d3d1a6afSToby Isaac 
5177d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5178d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5179d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5180d3d1a6afSToby Isaac           PetscInt c, r, k;
5181d3d1a6afSToby Isaac           PetscInt dof;
5182d3d1a6afSToby Isaac 
5183d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
51844b2f2278SToby Isaac           if (!dof) {
51854b2f2278SToby Isaac             continue;
51864b2f2278SToby Isaac           }
5187d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5188d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5189d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5190d3d1a6afSToby Isaac 
5191d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5192d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5193d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
51944acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5195d3d1a6afSToby Isaac                 }
5196d3d1a6afSToby Isaac               }
5197d3d1a6afSToby Isaac             }
5198d3d1a6afSToby Isaac           }
5199d3d1a6afSToby Isaac           else {
5200d3d1a6afSToby Isaac             /* copy this column as is */
5201d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5202d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5203d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5204d3d1a6afSToby Isaac               }
5205d3d1a6afSToby Isaac             }
5206d3d1a6afSToby Isaac           }
5207d3d1a6afSToby Isaac           oldOff += dof;
5208d3d1a6afSToby Isaac         }
5209d3d1a6afSToby Isaac       }
5210d3d1a6afSToby Isaac     }
5211d3d1a6afSToby Isaac     else {
5212d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5213d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5214d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5215d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5216d3d1a6afSToby Isaac         PetscInt c, r, k;
5217d3d1a6afSToby Isaac         PetscInt dof;
5218d3d1a6afSToby Isaac 
5219d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
52204b2f2278SToby Isaac         if (!dof) {
52214b2f2278SToby Isaac           continue;
52224b2f2278SToby Isaac         }
5223d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5224d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5225d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5226d3d1a6afSToby Isaac 
5227d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5228d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5229d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5230d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5231d3d1a6afSToby Isaac               }
5232d3d1a6afSToby Isaac             }
5233d3d1a6afSToby Isaac           }
5234d3d1a6afSToby Isaac         }
5235d3d1a6afSToby Isaac         else {
5236d3d1a6afSToby Isaac           /* copy this column as is */
5237d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5238d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5239d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5240d3d1a6afSToby Isaac             }
5241d3d1a6afSToby Isaac           }
5242d3d1a6afSToby Isaac         }
5243d3d1a6afSToby Isaac         oldOff += dof;
5244d3d1a6afSToby Isaac       }
5245d3d1a6afSToby Isaac     }
5246d3d1a6afSToby Isaac 
52476ecaa68aSToby Isaac     if (multiplyLeft) {
524869291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5249d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5250d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5251d3d1a6afSToby Isaac       if (numFields) {
5252d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5253d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5254d3d1a6afSToby Isaac 
5255d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5256d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5257d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5258d3d1a6afSToby Isaac             PetscInt c, r, k;
5259d3d1a6afSToby Isaac             PetscInt dof;
5260d3d1a6afSToby Isaac 
5261d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5262d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5263d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5264d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5265d3d1a6afSToby Isaac 
5266d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5267d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5268d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5269d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5270d3d1a6afSToby Isaac                   }
5271d3d1a6afSToby Isaac                 }
5272d3d1a6afSToby Isaac               }
5273d3d1a6afSToby Isaac             }
5274d3d1a6afSToby Isaac             else {
5275d3d1a6afSToby Isaac               /* copy this row as is */
5276d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5277d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5278d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5279d3d1a6afSToby Isaac                 }
5280d3d1a6afSToby Isaac               }
5281d3d1a6afSToby Isaac             }
5282d3d1a6afSToby Isaac             oldOff += dof;
5283d3d1a6afSToby Isaac           }
5284d3d1a6afSToby Isaac         }
5285d3d1a6afSToby Isaac       }
5286d3d1a6afSToby Isaac       else {
5287d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5288d3d1a6afSToby Isaac 
5289d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5290d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5291d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5292d3d1a6afSToby Isaac           PetscInt c, r, k;
5293d3d1a6afSToby Isaac           PetscInt dof;
5294d3d1a6afSToby Isaac 
5295d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5296d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5297d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5298d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5299d3d1a6afSToby Isaac 
5300d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5301d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5302d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5303d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5304d3d1a6afSToby Isaac                 }
5305d3d1a6afSToby Isaac               }
5306d3d1a6afSToby Isaac             }
5307d3d1a6afSToby Isaac           }
5308d3d1a6afSToby Isaac           else {
5309d3d1a6afSToby Isaac             /* copy this row as is */
53109fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5311d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5312d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5313d3d1a6afSToby Isaac               }
5314d3d1a6afSToby Isaac             }
5315d3d1a6afSToby Isaac           }
5316d3d1a6afSToby Isaac           oldOff += dof;
5317d3d1a6afSToby Isaac         }
5318d3d1a6afSToby Isaac       }
5319d3d1a6afSToby Isaac 
532069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
53216ecaa68aSToby Isaac     }
53226ecaa68aSToby Isaac     else {
53236ecaa68aSToby Isaac       newValues = tmpValues;
53246ecaa68aSToby Isaac     }
53256ecaa68aSToby Isaac   }
53266ecaa68aSToby Isaac 
5327d3d1a6afSToby Isaac   /* clean up */
532869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
532969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
53306ecaa68aSToby Isaac 
5331d3d1a6afSToby Isaac   if (numFields) {
5332d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
533369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
533469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
533569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5336d3d1a6afSToby Isaac     }
5337d3d1a6afSToby Isaac   }
5338d3d1a6afSToby Isaac   else {
533969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
534069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
534169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5342d3d1a6afSToby Isaac   }
5343d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5344d3d1a6afSToby Isaac 
5345d3d1a6afSToby Isaac   /* output */
53466ecaa68aSToby Isaac   if (outPoints) {
5347d3d1a6afSToby Isaac     *outPoints = newPoints;
53486ecaa68aSToby Isaac   }
53496ecaa68aSToby Isaac   else {
535069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
53516ecaa68aSToby Isaac   }
535231620726SToby Isaac   if (outValues) {
5353d3d1a6afSToby Isaac     *outValues = newValues;
53546ecaa68aSToby Isaac   }
53556ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5356d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5357d3d1a6afSToby Isaac   }
5358d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5359d3d1a6afSToby Isaac }
5360d3d1a6afSToby Isaac 
53617cd05799SMatthew G. Knepley /*@C
5362a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
53637cd05799SMatthew G. Knepley 
53647cd05799SMatthew G. Knepley   Not collective
53657cd05799SMatthew G. Knepley 
53667cd05799SMatthew G. Knepley   Input Parameters:
53677cd05799SMatthew G. Knepley + dm - The DM
53687cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
53697cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
53707cd05799SMatthew G. Knepley - point - The mesh point
53717cd05799SMatthew G. Knepley 
53727cd05799SMatthew G. Knepley   Output parameters:
53737cd05799SMatthew G. Knepley + numIndices - The number of indices
53747cd05799SMatthew G. Knepley . indices - The indices
53757cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
53767cd05799SMatthew G. Knepley 
53777cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
53787cd05799SMatthew G. Knepley 
53797cd05799SMatthew G. Knepley   Level: advanced
53807cd05799SMatthew G. Knepley 
53817cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
53827cd05799SMatthew G. Knepley @*/
53836ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
53847773e69fSMatthew G. Knepley {
53857773e69fSMatthew G. Knepley   PetscSection    clSection;
53867773e69fSMatthew G. Knepley   IS              clPoints;
53877773e69fSMatthew G. Knepley   const PetscInt *clp;
53884acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
53897773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
53907773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
53917773e69fSMatthew G. Knepley   PetscInt        offsets[32];
53927773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
53937773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
53947773e69fSMatthew G. Knepley 
53957773e69fSMatthew G. Knepley   PetscFunctionBegin;
53967773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53977773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
53987773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
53997773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
54007773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
54017773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
54027773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
54037773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
54047773e69fSMatthew G. Knepley   /* Get points in closure */
5405923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54067773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
54077773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
54087773e69fSMatthew G. Knepley     PetscInt dof, fdof;
54097773e69fSMatthew G. Knepley 
54107773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
54117773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
54127773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
54137773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
54147773e69fSMatthew G. Knepley     }
54157773e69fSMatthew G. Knepley     Nind += dof;
54167773e69fSMatthew G. Knepley   }
54177773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
54188ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
54194acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
54204acb8e1eSToby Isaac   /* Get dual space symmetries */
54214acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
54224acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54234acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54244acb8e1eSToby Isaac   }
54257773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
54267773e69fSMatthew G. Knepley   {
54274acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
54287773e69fSMatthew G. Knepley     if (numPointsNew) {
54294acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
54304acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54314acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54324acb8e1eSToby Isaac       }
54334acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
54344acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
54354acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
54364acb8e1eSToby Isaac       }
5437923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54387773e69fSMatthew G. Knepley       numPoints = numPointsNew;
54397773e69fSMatthew G. Knepley       Nind      = NindNew;
54407773e69fSMatthew G. Knepley       points    = pointsNew;
54417773e69fSMatthew G. Knepley     }
54427773e69fSMatthew G. Knepley   }
54437773e69fSMatthew G. Knepley   /* Calculate indices */
544469291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
54457773e69fSMatthew G. Knepley   if (Nf) {
54466ecaa68aSToby Isaac     if (outOffsets) {
54476ecaa68aSToby Isaac       PetscInt f;
54486ecaa68aSToby Isaac 
544946bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
54506ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
54516ecaa68aSToby Isaac       }
54526ecaa68aSToby Isaac     }
54534acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
54544acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54554acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
54567773e69fSMatthew G. Knepley     }
54577773e69fSMatthew G. Knepley   } else {
54584acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
54594acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
54604acb8e1eSToby Isaac 
54614acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54624acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
54637773e69fSMatthew G. Knepley     }
54647773e69fSMatthew G. Knepley   }
54657773e69fSMatthew G. Knepley   /* Cleanup points */
54664acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
54674acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54684acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
54694acb8e1eSToby Isaac   }
54707773e69fSMatthew G. Knepley   if (numPointsNew) {
547169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
54727773e69fSMatthew G. Knepley   } else {
5473923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
54747773e69fSMatthew G. Knepley   }
54757773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
54767773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
54777773e69fSMatthew G. Knepley }
54787773e69fSMatthew G. Knepley 
54797cd05799SMatthew G. Knepley /*@C
54807cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
54817cd05799SMatthew G. Knepley 
54827cd05799SMatthew G. Knepley   Not collective
54837cd05799SMatthew G. Knepley 
54847cd05799SMatthew G. Knepley   Input Parameters:
54857cd05799SMatthew G. Knepley + dm - The DM
54867cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
54877cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
54887cd05799SMatthew G. Knepley . point - The mesh point
54897cd05799SMatthew G. Knepley . numIndices - The number of indices
54907cd05799SMatthew G. Knepley . indices - The indices
54917cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
54927cd05799SMatthew G. Knepley 
54937cd05799SMatthew G. Knepley   Level: advanced
54947cd05799SMatthew G. Knepley 
54957cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
54967cd05799SMatthew G. Knepley @*/
549746bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
54987773e69fSMatthew G. Knepley {
54997773e69fSMatthew G. Knepley   PetscErrorCode ierr;
55007773e69fSMatthew G. Knepley 
55017773e69fSMatthew G. Knepley   PetscFunctionBegin;
55027773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55037773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
550469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
55057773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55067773e69fSMatthew G. Knepley }
55077773e69fSMatthew G. Knepley 
55087f5d1fdeSMatthew G. Knepley /*@C
55097f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
55107f5d1fdeSMatthew G. Knepley 
55117f5d1fdeSMatthew G. Knepley   Not collective
55127f5d1fdeSMatthew G. Knepley 
55137f5d1fdeSMatthew G. Knepley   Input Parameters:
55147f5d1fdeSMatthew G. Knepley + dm - The DM
5515ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5516ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
55177f5d1fdeSMatthew G. Knepley . A - The matrix
5518eaf898f9SPatrick Sanan . point - The point in the DM
55197f5d1fdeSMatthew G. Knepley . values - The array of values
55207f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
55217f5d1fdeSMatthew G. Knepley 
55227f5d1fdeSMatthew G. Knepley   Fortran Notes:
55237f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
55247f5d1fdeSMatthew G. Knepley 
55257f5d1fdeSMatthew G. Knepley   Level: intermediate
55267f5d1fdeSMatthew G. Knepley 
55277f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
55287f5d1fdeSMatthew G. Knepley @*/
55297c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5530552f7358SJed Brown {
5531552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
55321b406b76SMatthew G. Knepley   PetscSection        clSection;
55331b406b76SMatthew G. Knepley   IS                  clPoints;
5534d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
55351b406b76SMatthew G. Knepley   const PetscInt     *clp;
5536552f7358SJed Brown   PetscInt           *indices;
5537552f7358SJed Brown   PetscInt            offsets[32];
55384acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
55394acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5540923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
55414acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5542d3d1a6afSToby Isaac   PetscScalar        *newValues;
5543552f7358SJed Brown   PetscErrorCode      ierr;
5544552f7358SJed Brown 
5545552f7358SJed Brown   PetscFunctionBegin;
5546552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5547ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
55483dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5549ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
55503dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
55513dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5552552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
555382f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5554552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5555923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5556552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5557552f7358SJed Brown     PetscInt fdof;
5558552f7358SJed Brown 
5559552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5560552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5561552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5562552f7358SJed Brown       offsets[f+1] += fdof;
5563552f7358SJed Brown     }
5564552f7358SJed Brown     numIndices += dof;
5565552f7358SJed Brown   }
55660d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
55670d644c17SKarl Rupp 
55688ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
55694acb8e1eSToby Isaac   /* Get symmetries */
55704acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
55714acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
55724acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
55734acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
55744acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
55754acb8e1eSToby Isaac 
55764acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
55774acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
55784acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
55794acb8e1eSToby Isaac 
55804acb8e1eSToby Isaac         if (!numFields) {
55814acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
55824acb8e1eSToby Isaac         } else {
55834acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
55844acb8e1eSToby Isaac         }
55854acb8e1eSToby Isaac         if (flip) {
55864acb8e1eSToby Isaac           PetscInt i, j, k;
55874acb8e1eSToby Isaac 
55884acb8e1eSToby Isaac           if (!valCopy) {
558969291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
55904acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
55914acb8e1eSToby Isaac             values = valCopy;
55924acb8e1eSToby Isaac           }
55934acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5594775f7be2SToby Isaac             PetscScalar fval = flip[i];
55954acb8e1eSToby Isaac 
55964acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
55974acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
55984acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
55994acb8e1eSToby Isaac             }
56004acb8e1eSToby Isaac           }
56014acb8e1eSToby Isaac         }
56024acb8e1eSToby Isaac         foffset += fdof;
56034acb8e1eSToby Isaac       }
56044acb8e1eSToby Isaac     }
56054acb8e1eSToby Isaac   }
56064acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5607d3d1a6afSToby Isaac   if (newNumPoints) {
56084acb8e1eSToby Isaac     if (valCopy) {
560969291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56104acb8e1eSToby Isaac     }
56114acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56124acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56134acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56144acb8e1eSToby Isaac     }
56154acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
56164acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56174acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
56184acb8e1eSToby Isaac     }
5619923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5620d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5621d3d1a6afSToby Isaac     numIndices = newNumIndices;
5622d3d1a6afSToby Isaac     points     = newPoints;
5623d3d1a6afSToby Isaac     values     = newValues;
5624d3d1a6afSToby Isaac   }
562569291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5626552f7358SJed Brown   if (numFields) {
56274acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
56284acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
56294acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5630552f7358SJed Brown     }
5631552f7358SJed Brown   } else {
56324acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
56334acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
56344acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
56354acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5636552f7358SJed Brown     }
5637552f7358SJed Brown   }
5638b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5639552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5640ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5641ab1d0545SMatthew G. Knepley     PetscInt i;
5642ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
56438ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5644ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5645ab1d0545SMatthew G. Knepley   }
5646552f7358SJed Brown   if (ierr) {
5647552f7358SJed Brown     PetscMPIInt    rank;
5648552f7358SJed Brown     PetscErrorCode ierr2;
5649552f7358SJed Brown 
565082f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5651e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5652b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
565369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
5654552f7358SJed Brown     CHKERRQ(ierr);
5655552f7358SJed Brown   }
56564acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
56574acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56584acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56594acb8e1eSToby Isaac   }
5660d3d1a6afSToby Isaac   if (newNumPoints) {
566169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
566269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
5663d3d1a6afSToby Isaac   }
5664d3d1a6afSToby Isaac   else {
56654acb8e1eSToby Isaac     if (valCopy) {
566669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56674acb8e1eSToby Isaac     }
5668923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5669d3d1a6afSToby Isaac   }
567069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5671552f7358SJed Brown   PetscFunctionReturn(0);
5672552f7358SJed Brown }
5673552f7358SJed Brown 
5674de41b84cSMatthew 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)
5675de41b84cSMatthew G. Knepley {
5676de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5677de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5678de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5679de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5680de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5681de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
56824ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5683de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5684de41b84cSMatthew G. Knepley 
5685de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5686de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5687de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5688de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5689de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5690de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5691de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5692de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5693de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5694de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5695de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5696de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5697de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5698de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5699de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5700de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5701de41b84cSMatthew G. Knepley   /* Column indices */
5702de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57034ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5704de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5705de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5706de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5707de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5708de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5709de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5710de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5711de41b84cSMatthew G. Knepley       ++q;
5712de41b84cSMatthew G. Knepley     }
5713de41b84cSMatthew G. Knepley   }
5714de41b84cSMatthew G. Knepley   numCPoints = q;
5715de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5716de41b84cSMatthew G. Knepley     PetscInt fdof;
5717de41b84cSMatthew G. Knepley 
5718de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
57194ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5720de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5721de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5722de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5723de41b84cSMatthew G. Knepley     }
5724de41b84cSMatthew G. Knepley     numCIndices += dof;
5725de41b84cSMatthew G. Knepley   }
5726de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5727de41b84cSMatthew G. Knepley   /* Row indices */
5728de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5729de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
573069291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5731de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5732de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5733de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5734de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5735de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5736de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5737de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
57384ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
57394ca5e9f5SMatthew G. Knepley         if (!dof) continue;
57404ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
57414ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5742de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5743de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5744de41b84cSMatthew G. Knepley         ++q;
5745de41b84cSMatthew G. Knepley       }
5746de41b84cSMatthew G. Knepley     }
5747de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5748de41b84cSMatthew G. Knepley   }
5749de41b84cSMatthew G. Knepley   numFPoints = q;
5750de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5751de41b84cSMatthew G. Knepley     PetscInt fdof;
5752de41b84cSMatthew G. Knepley 
5753de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
57544ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5755de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5756de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5757de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5758de41b84cSMatthew G. Knepley     }
5759de41b84cSMatthew G. Knepley     numFIndices += dof;
5760de41b84cSMatthew G. Knepley   }
5761de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5762de41b84cSMatthew G. Knepley 
57638ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
57648ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
576569291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
576669291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5767de41b84cSMatthew G. Knepley   if (numFields) {
57684acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
57694acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
57704acb8e1eSToby Isaac 
57714acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57724acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57734acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5774de41b84cSMatthew G. Knepley     }
57754acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
57764acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57774acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
57784acb8e1eSToby Isaac     }
57794acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
57804acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57814acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
57824acb8e1eSToby Isaac     }
57834acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57844acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57854acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5786de41b84cSMatthew G. Knepley     }
5787de41b84cSMatthew G. Knepley   } else {
57884acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
57894acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
57904acb8e1eSToby Isaac 
57914acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57924acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57934acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
57944acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
57954acb8e1eSToby Isaac 
57964acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57974acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5798de41b84cSMatthew G. Knepley     }
57994acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
58004acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
58014acb8e1eSToby Isaac 
58024acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
58034acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5804de41b84cSMatthew G. Knepley     }
58054acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58064acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5807de41b84cSMatthew G. Knepley   }
5808de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
58094acb8e1eSToby Isaac   /* TODO: flips */
5810de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5811de41b84cSMatthew G. Knepley   if (ierr) {
5812de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5813de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5814de41b84cSMatthew G. Knepley 
5815de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5816e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5817de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
581869291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
581969291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
5820de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5821de41b84cSMatthew G. Knepley   }
582269291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5823de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
582469291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
582569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5826de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5827de41b84cSMatthew G. Knepley }
5828de41b84cSMatthew G. Knepley 
58297c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
58307c927364SMatthew G. Knepley {
58317c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
58327c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
58337c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
58347c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
58357c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
58367c927364SMatthew G. Knepley   PetscErrorCode ierr;
58377c927364SMatthew G. Knepley 
58387c927364SMatthew G. Knepley   PetscFunctionBegin;
58397c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
58407c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
58417c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
58427c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
58437c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
58447c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
58457c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
58467c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
58477c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
58487c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
58497c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
58507c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
58517c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
58527c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
58537c927364SMatthew G. Knepley   /* Column indices */
58547c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
58557c927364SMatthew G. Knepley   maxFPoints = numCPoints;
58567c927364SMatthew G. Knepley   /* Compress out points not in the section */
58577c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
58587c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
58597c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
58607c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
58617c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
58627c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
58637c927364SMatthew G. Knepley       ++q;
58647c927364SMatthew G. Knepley     }
58657c927364SMatthew G. Knepley   }
58667c927364SMatthew G. Knepley   numCPoints = q;
58677c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
58687c927364SMatthew G. Knepley     PetscInt fdof;
58697c927364SMatthew G. Knepley 
58707c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
58717c927364SMatthew G. Knepley     if (!dof) continue;
58727c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
58737c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
58747c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
58757c927364SMatthew G. Knepley     }
58767c927364SMatthew G. Knepley     numCIndices += dof;
58777c927364SMatthew G. Knepley   }
58787c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
58797c927364SMatthew G. Knepley   /* Row indices */
58807c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
58817c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
588269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
58837c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
58847c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
58857c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
58867c927364SMatthew G. Knepley     /* Compress out points not in the section */
58877c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
58887c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
58897c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
58907c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
58917c927364SMatthew G. Knepley         if (!dof) continue;
58927c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
58937c927364SMatthew G. Knepley         if (s < q) continue;
58947c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
58957c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
58967c927364SMatthew G. Knepley         ++q;
58977c927364SMatthew G. Knepley       }
58987c927364SMatthew G. Knepley     }
58997c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59007c927364SMatthew G. Knepley   }
59017c927364SMatthew G. Knepley   numFPoints = q;
59027c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
59037c927364SMatthew G. Knepley     PetscInt fdof;
59047c927364SMatthew G. Knepley 
59057c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
59067c927364SMatthew G. Knepley     if (!dof) continue;
59077c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59087c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
59097c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
59107c927364SMatthew G. Knepley     }
59117c927364SMatthew G. Knepley     numFIndices += dof;
59127c927364SMatthew G. Knepley   }
59137c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
59147c927364SMatthew G. Knepley 
59158ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
59168ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
59177c927364SMatthew G. Knepley   if (numFields) {
59184acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
59194acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
59204acb8e1eSToby Isaac 
59214acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
59224acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
59234acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
59247c927364SMatthew G. Knepley     }
59254acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
59264acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
59274acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
59284acb8e1eSToby Isaac     }
59294acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
59304acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
59314acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
59324acb8e1eSToby Isaac     }
59334acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
59344acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
59354acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
59367c927364SMatthew G. Knepley     }
59377c927364SMatthew G. Knepley   } else {
59384acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
59394acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
59404acb8e1eSToby Isaac 
59414acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59424acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
59434acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
59444acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
59454acb8e1eSToby Isaac 
59464acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
59474acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
59487c927364SMatthew G. Knepley     }
59494acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
59504acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
59514acb8e1eSToby Isaac 
59524acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
59534acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
59547c927364SMatthew G. Knepley     }
59554acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59564acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
59577c927364SMatthew G. Knepley   }
595869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
59597c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
59607c927364SMatthew G. Knepley   PetscFunctionReturn(0);
59617c927364SMatthew G. Knepley }
59627c927364SMatthew G. Knepley 
5963f96281f8SMatthew G. Knepley /*@
5964f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5965f96281f8SMatthew G. Knepley 
5966f96281f8SMatthew G. Knepley   Input Parameter:
5967f96281f8SMatthew G. Knepley . dm - The DMPlex object
5968f96281f8SMatthew G. Knepley 
5969f96281f8SMatthew G. Knepley   Output Parameters:
5970f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5971dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5972dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5973dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5974f96281f8SMatthew G. Knepley 
5975f96281f8SMatthew G. Knepley   Level: developer
5976f96281f8SMatthew G. Knepley 
5977dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5978f96281f8SMatthew G. Knepley @*/
5979770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5980552f7358SJed Brown {
5981552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5982770b213bSMatthew G Knepley   PetscInt       dim;
5983770b213bSMatthew G Knepley   PetscErrorCode ierr;
5984552f7358SJed Brown 
5985552f7358SJed Brown   PetscFunctionBegin;
5986552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5987c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5988770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5989770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5990770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5991770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5992552f7358SJed Brown   PetscFunctionReturn(0);
5993552f7358SJed Brown }
5994552f7358SJed Brown 
5995dc5a3409SMatthew G. Knepley /*@
5996dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5997dc5a3409SMatthew G. Knepley 
5998dc5a3409SMatthew G. Knepley   Input Parameters:
5999dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
6000dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6001dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6002dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6003dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6004dc5a3409SMatthew G. Knepley 
6005dc5a3409SMatthew G. Knepley   Level: developer
6006dc5a3409SMatthew G. Knepley 
6007dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6008dc5a3409SMatthew G. Knepley @*/
6009770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6010552f7358SJed Brown {
6011552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6012770b213bSMatthew G Knepley   PetscInt       dim;
6013770b213bSMatthew G Knepley   PetscErrorCode ierr;
6014552f7358SJed Brown 
6015552f7358SJed Brown   PetscFunctionBegin;
6016552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6017c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6018770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
6019770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
6020770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
6021770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
6022552f7358SJed Brown   PetscFunctionReturn(0);
6023552f7358SJed Brown }
6024552f7358SJed Brown 
60257cd05799SMatthew G. Knepley /*@C
60267cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
60277cd05799SMatthew G. Knepley 
60287cd05799SMatthew G. Knepley   Input Parameter:
60297cd05799SMatthew G. Knepley . dm   - The DMPlex object
60307cd05799SMatthew G. Knepley 
60317cd05799SMatthew G. Knepley   Output Parameter:
60327cd05799SMatthew G. Knepley . cellHeight - The height of a cell
60337cd05799SMatthew G. Knepley 
60347cd05799SMatthew G. Knepley   Level: developer
60357cd05799SMatthew G. Knepley 
60367cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
60377cd05799SMatthew G. Knepley @*/
6038552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6039552f7358SJed Brown {
6040552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6041552f7358SJed Brown 
6042552f7358SJed Brown   PetscFunctionBegin;
6043552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6044552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6045552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6046552f7358SJed Brown   PetscFunctionReturn(0);
6047552f7358SJed Brown }
6048552f7358SJed Brown 
60497cd05799SMatthew G. Knepley /*@C
60507cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
60517cd05799SMatthew G. Knepley 
60527cd05799SMatthew G. Knepley   Input Parameters:
60537cd05799SMatthew G. Knepley + dm   - The DMPlex object
60547cd05799SMatthew G. Knepley - cellHeight - The height of a cell
60557cd05799SMatthew G. Knepley 
60567cd05799SMatthew G. Knepley   Level: developer
60577cd05799SMatthew G. Knepley 
60587cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
60597cd05799SMatthew G. Knepley @*/
6060552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6061552f7358SJed Brown {
6062552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6063552f7358SJed Brown 
6064552f7358SJed Brown   PetscFunctionBegin;
6065552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6066552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6067552f7358SJed Brown   PetscFunctionReturn(0);
6068552f7358SJed Brown }
6069552f7358SJed Brown 
6070552f7358SJed Brown /* We can easily have a form that takes an IS instead */
6071ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6072552f7358SJed Brown {
6073552f7358SJed Brown   PetscSection   section, globalSection;
6074552f7358SJed Brown   PetscInt      *numbers, p;
6075552f7358SJed Brown   PetscErrorCode ierr;
6076552f7358SJed Brown 
6077552f7358SJed Brown   PetscFunctionBegin;
607882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6079552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6080552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6081552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6082552f7358SJed Brown   }
6083552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
608415b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6085854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6086552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6087552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6088ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6089ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6090552f7358SJed Brown   }
609182f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6092ef48cebcSMatthew G. Knepley   if (globalSize) {
6093ef48cebcSMatthew G. Knepley     PetscLayout layout;
6094ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6095ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6096ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6097ef48cebcSMatthew G. Knepley   }
6098552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6099552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6100552f7358SJed Brown   PetscFunctionReturn(0);
6101552f7358SJed Brown }
6102552f7358SJed Brown 
610381ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6104552f7358SJed Brown {
6105552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6106552f7358SJed Brown   PetscErrorCode ierr;
6107552f7358SJed Brown 
6108552f7358SJed Brown   PetscFunctionBegin;
6109552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6110552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
61110298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
611281ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
611381ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
611481ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6115552f7358SJed Brown }
611681ed3555SMatthew G. Knepley 
61177cd05799SMatthew G. Knepley /*@C
61187cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
61197cd05799SMatthew G. Knepley 
61207cd05799SMatthew G. Knepley   Input Parameter:
61217cd05799SMatthew G. Knepley . dm   - The DMPlex object
61227cd05799SMatthew G. Knepley 
61237cd05799SMatthew G. Knepley   Output Parameter:
61247cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
61257cd05799SMatthew G. Knepley 
61267cd05799SMatthew G. Knepley   Level: developer
61277cd05799SMatthew G. Knepley 
61287cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
61297cd05799SMatthew G. Knepley @*/
613081ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
613181ed3555SMatthew G. Knepley {
613281ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
613381ed3555SMatthew G. Knepley   PetscErrorCode ierr;
613481ed3555SMatthew G. Knepley 
613581ed3555SMatthew G. Knepley   PetscFunctionBegin;
613681ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
613781ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6138552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6139552f7358SJed Brown   PetscFunctionReturn(0);
6140552f7358SJed Brown }
6141552f7358SJed Brown 
614281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
614381ed3555SMatthew G. Knepley {
614481ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
614581ed3555SMatthew G. Knepley   PetscErrorCode ierr;
614681ed3555SMatthew G. Knepley 
614781ed3555SMatthew G. Knepley   PetscFunctionBegin;
614881ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
614981ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
615081ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
615181ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
615281ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
615381ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
615481ed3555SMatthew G. Knepley }
615581ed3555SMatthew G. Knepley 
61567cd05799SMatthew G. Knepley /*@C
61577cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
61587cd05799SMatthew G. Knepley 
61597cd05799SMatthew G. Knepley   Input Parameter:
61607cd05799SMatthew G. Knepley . dm   - The DMPlex object
61617cd05799SMatthew G. Knepley 
61627cd05799SMatthew G. Knepley   Output Parameter:
61637cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
61647cd05799SMatthew G. Knepley 
61657cd05799SMatthew G. Knepley   Level: developer
61667cd05799SMatthew G. Knepley 
61677cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
61687cd05799SMatthew G. Knepley @*/
6169552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6170552f7358SJed Brown {
6171552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6172552f7358SJed Brown   PetscErrorCode ierr;
6173552f7358SJed Brown 
6174552f7358SJed Brown   PetscFunctionBegin;
6175552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
617681ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6177552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6178552f7358SJed Brown   PetscFunctionReturn(0);
6179552f7358SJed Brown }
6180552f7358SJed Brown 
61817cd05799SMatthew G. Knepley /*@C
61827cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
61837cd05799SMatthew G. Knepley 
61847cd05799SMatthew G. Knepley   Input Parameter:
61857cd05799SMatthew G. Knepley . dm   - The DMPlex object
61867cd05799SMatthew G. Knepley 
61877cd05799SMatthew G. Knepley   Output Parameter:
61887cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
61897cd05799SMatthew G. Knepley 
61907cd05799SMatthew G. Knepley   Level: developer
61917cd05799SMatthew G. Knepley 
61927cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
61937cd05799SMatthew G. Knepley @*/
6194ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6195ef48cebcSMatthew G. Knepley {
6196ef48cebcSMatthew G. Knepley   IS             nums[4];
6197ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
6198ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6199ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6200ef48cebcSMatthew G. Knepley 
6201ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6202ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6203ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
62048abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
62058abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6206ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
6207ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
6208ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6209ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6210ef48cebcSMatthew G. Knepley 
6211ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
6212ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6213ef48cebcSMatthew G. Knepley     shift += gsize;
6214ef48cebcSMatthew G. Knepley   }
6215302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6216ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6217ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6218ef48cebcSMatthew G. Knepley }
6219ef48cebcSMatthew G. Knepley 
622008a22f4bSMatthew G. Knepley 
622108a22f4bSMatthew G. Knepley /*@
622208a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
622308a22f4bSMatthew G. Knepley 
622408a22f4bSMatthew G. Knepley   Input Parameter:
622508a22f4bSMatthew G. Knepley . dm - The DMPlex object
622608a22f4bSMatthew G. Knepley 
622708a22f4bSMatthew G. Knepley   Output Parameter:
622808a22f4bSMatthew G. Knepley . ranks - The rank field
622908a22f4bSMatthew G. Knepley 
623008a22f4bSMatthew G. Knepley   Options Database Keys:
623108a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
623208a22f4bSMatthew G. Knepley 
623308a22f4bSMatthew G. Knepley   Level: intermediate
623408a22f4bSMatthew G. Knepley 
623508a22f4bSMatthew G. Knepley .seealso: DMView()
623608a22f4bSMatthew G. Knepley @*/
623708a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
623808a22f4bSMatthew G. Knepley {
623908a22f4bSMatthew G. Knepley   DM             rdm;
624008a22f4bSMatthew G. Knepley   PetscDS        prob;
624108a22f4bSMatthew G. Knepley   PetscFE        fe;
624208a22f4bSMatthew G. Knepley   PetscScalar   *r;
624308a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
624408a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
624508a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
624608a22f4bSMatthew G. Knepley 
624708a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
624808a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
624908a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
625008a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
625108a22f4bSMatthew G. Knepley   ierr = PetscFECreateDefault(rdm, dim, 1, PETSC_TRUE, NULL, -1, &fe);CHKERRQ(ierr);
625208a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
625308a22f4bSMatthew G. Knepley   ierr = DMGetDS(rdm, &prob);CHKERRQ(ierr);
625408a22f4bSMatthew G. Knepley   ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe);CHKERRQ(ierr);
625508a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
625608a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
625708a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
625808a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
625908a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
626008a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
626108a22f4bSMatthew G. Knepley     PetscScalar *lr;
626208a22f4bSMatthew G. Knepley 
626308a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
626408a22f4bSMatthew G. Knepley     *lr = rank;
626508a22f4bSMatthew G. Knepley   }
626608a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
626708a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
626808a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
626908a22f4bSMatthew G. Knepley }
627008a22f4bSMatthew G. Knepley 
6271ca8062c8SMatthew G. Knepley /*@
6272ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6273ca8062c8SMatthew G. Knepley 
627469916449SMatthew G. Knepley   Input Parameter:
627569916449SMatthew G. Knepley . dm - The DMPlex object
6276ca8062c8SMatthew G. Knepley 
6277ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6278ca8062c8SMatthew G. Knepley 
6279ca8062c8SMatthew G. Knepley   Level: developer
6280ca8062c8SMatthew G. Knepley 
62819bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
6282ca8062c8SMatthew G. Knepley @*/
6283ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6284ca8062c8SMatthew G. Knepley {
6285ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6286ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6287ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
6288ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
6289ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6290ca8062c8SMatthew G. Knepley 
6291ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6292ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6293ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6294ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6295ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6296ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6297ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6298ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6299ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6300ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
630142e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
630242e66dfaSMatthew G. Knepley       PetscInt  d;
630342e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
630442e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
630542e66dfaSMatthew G. Knepley       }
6306ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6307ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6308ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6309ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6310ca8062c8SMatthew G. Knepley       }
631142e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
63128ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6313ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
63148ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6315ca8062c8SMatthew G. Knepley         }
6316302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63178ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6318ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
63198ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6320ca8062c8SMatthew G. Knepley         }
6321302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63228ccfff9cSToby 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]);
63238ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6324ca8062c8SMatthew G. Knepley       }
632542e66dfaSMatthew G. Knepley     }
6326ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6327ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6328ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6329ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6330ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6331ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6332ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6333ca8062c8SMatthew G. Knepley       }
6334ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
63358ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6336ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
63378ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6338ca8062c8SMatthew G. Knepley         }
6339302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63408ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6341ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
63428ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6343ca8062c8SMatthew G. Knepley         }
6344302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
63458ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6346ca8062c8SMatthew G. Knepley       }
6347ca8062c8SMatthew G. Knepley     }
6348ca8062c8SMatthew G. Knepley   }
6349ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6350ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
63518ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6352ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6353ca8062c8SMatthew G. Knepley }
6354ca8062c8SMatthew G. Knepley 
6355ca8062c8SMatthew G. Knepley /*@
6356ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6357ca8062c8SMatthew G. Knepley 
6358ca8062c8SMatthew G. Knepley   Input Parameters:
6359ca8062c8SMatthew G. Knepley + dm - The DMPlex object
636058723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
636158723a97SMatthew G. Knepley - cellHeight - Normally 0
6362ca8062c8SMatthew G. Knepley 
6363ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6364ca8062c8SMatthew G. Knepley 
6365ca8062c8SMatthew G. Knepley   Level: developer
6366ca8062c8SMatthew G. Knepley 
63679bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
6368ca8062c8SMatthew G. Knepley @*/
636958723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6370ca8062c8SMatthew G. Knepley {
637142363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6372ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6373ca8062c8SMatthew G. Knepley 
6374ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6375ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6376c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6377ca8062c8SMatthew G. Knepley   switch (dim) {
637842363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
637942363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
638042363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6381ca8062c8SMatthew G. Knepley   default:
63828ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6383ca8062c8SMatthew G. Knepley   }
638458723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
638558723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6386ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6387ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6388ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
638958723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
639058723a97SMatthew G. Knepley 
639158723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
639258723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
639358723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
639458723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
639558723a97SMatthew G. Knepley     }
639658723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
63978ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6398ca8062c8SMatthew G. Knepley   }
639942363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
640042363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
640142363296SMatthew G. Knepley 
640242363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
640342363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
640442363296SMatthew G. Knepley       const PetscInt p = closure[cl];
640542363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
640642363296SMatthew G. Knepley     }
640742363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
64088ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
640942363296SMatthew G. Knepley   }
6410ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6411ca8062c8SMatthew G. Knepley }
64129bf0dad6SMatthew G. Knepley 
64139bf0dad6SMatthew G. Knepley /*@
64149bf0dad6SMatthew 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
64159bf0dad6SMatthew G. Knepley 
64169bf0dad6SMatthew G. Knepley   Input Parameters:
64179bf0dad6SMatthew G. Knepley + dm - The DMPlex object
64189bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
64199bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
64209bf0dad6SMatthew G. Knepley 
64219bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
64229bf0dad6SMatthew G. Knepley 
64239bf0dad6SMatthew G. Knepley   Level: developer
64249bf0dad6SMatthew G. Knepley 
64259bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
64269bf0dad6SMatthew G. Knepley @*/
64279bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
64289bf0dad6SMatthew G. Knepley {
64293554e41dSMatthew G. Knepley   PetscInt       pMax[4];
64303554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
64319bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
64329bf0dad6SMatthew G. Knepley 
64339bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
64349bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6435c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
64369bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
643725abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
64383554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
64393554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
64403554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
64419bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
64429bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
64439bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
64449bf0dad6SMatthew G. Knepley 
644525abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
64469bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
64479bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
64489bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
64499bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
64509bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
64519bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
64529bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
64539bf0dad6SMatthew G. Knepley       }
64543554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
64558ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
64569bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
64579bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
64589bf0dad6SMatthew G. Knepley 
64599bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
64609bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
64619bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
64629bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
64639bf0dad6SMatthew G. Knepley         }
64648ccfff9cSToby 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);
64659bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
64668ccfff9cSToby 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]);
64679bf0dad6SMatthew G. Knepley         }
64689bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
64699bf0dad6SMatthew G. Knepley       }
64709bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
64719bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
64729bf0dad6SMatthew G. Knepley     }
64733554e41dSMatthew G. Knepley   }
6474552f7358SJed Brown   PetscFunctionReturn(0);
6475552f7358SJed Brown }
64763913d7c8SMatthew G. Knepley 
6477bceba477SMatthew G. Knepley /* Pointwise interpolation
6478bceba477SMatthew G. Knepley      Just code FEM for now
6479bceba477SMatthew G. Knepley      u^f = I u^c
64804ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
64814ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
64824ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6483bceba477SMatthew G. Knepley */
6484bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6485bceba477SMatthew G. Knepley {
6486bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6487bceba477SMatthew G. Knepley   PetscInt       m, n;
6488a063dac3SMatthew G. Knepley   void          *ctx;
648968132eb9SMatthew G. Knepley   DM             cdm;
649068132eb9SMatthew G. Knepley   PetscBool      regular;
6491bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6492bceba477SMatthew G. Knepley 
6493bceba477SMatthew G. Knepley   PetscFunctionBegin;
6494bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6495bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6496bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6497bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
649868132eb9SMatthew G. Knepley 
6499bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6500bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6501bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6502a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
650368132eb9SMatthew G. Knepley 
6504a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
650568132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
650668132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
650768132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
650868132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
65095d1c2e58SMatthew G. Knepley   /* Use naive scaling */
65105d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6511a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6512a063dac3SMatthew G. Knepley }
6513bceba477SMatthew G. Knepley 
65146dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6515a063dac3SMatthew G. Knepley {
651690748bafSMatthew G. Knepley   PetscErrorCode ierr;
65176dbf9973SLawrence Mitchell   VecScatter     ctx;
651890748bafSMatthew G. Knepley 
6519a063dac3SMatthew G. Knepley   PetscFunctionBegin;
65206dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
65216dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
65226dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6523bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6524bceba477SMatthew G. Knepley }
6525bceba477SMatthew G. Knepley 
6526bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
6527bd041c0cSMatthew G. Knepley {
6528bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
6529bd041c0cSMatthew G. Knepley   PetscInt       m, n;
6530bd041c0cSMatthew G. Knepley   void          *ctx;
6531bd041c0cSMatthew G. Knepley   DM             cdm;
6532bd041c0cSMatthew G. Knepley   PetscBool      regular;
6533bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
6534bd041c0cSMatthew G. Knepley 
6535bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
6536bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6537bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6538bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6539bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
6540bd041c0cSMatthew G. Knepley 
6541bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
6542bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6543bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
6544bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
6545bd041c0cSMatthew G. Knepley 
6546bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
6547bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
6548bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6549bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6550bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
6551bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
6552bd041c0cSMatthew G. Knepley }
6553bd041c0cSMatthew G. Knepley 
6554fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6555fd59a867SMatthew G. Knepley {
6556fd59a867SMatthew G. Knepley   PetscSection   section;
6557ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6558ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6559286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6560ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6561ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6562fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6563fd59a867SMatthew G. Knepley 
6564fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6565ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6566ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6567ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6568ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6569ebb3236fSMatthew G. Knepley     PetscObject  obj;
6570ebb3236fSMatthew G. Knepley     PetscClassId id;
6571ebb3236fSMatthew G. Knepley 
6572ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6573ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6574ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6575ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
65768ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6577ebb3236fSMatthew G. Knepley   }
65782f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6579286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6580c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6581ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6582ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6583dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6584fdafae62SVaclav 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)");
6585fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
65862f900235SMatthew G. Knepley     PetscInt                field;
6587f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6588385532a5SToby Isaac     const char             *labelName;
6589385532a5SToby Isaac     DMLabel                 label;
65902f900235SMatthew G. Knepley 
6591f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6592385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6593f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6594fd59a867SMatthew G. Knepley   }
65952f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6596ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
65976c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6598ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6599ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6600ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6601ebb3236fSMatthew G. Knepley 
6602ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6603ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6604ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6605ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6606ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6607ebb3236fSMatthew G. Knepley   }
6608ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6609ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6610fd59a867SMatthew G. Knepley     const char             *bdLabel;
6611fd59a867SMatthew G. Knepley     DMLabel                 label;
66120e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6613fd59a867SMatthew G. Knepley     const PetscInt         *values;
66140e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6615f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6616f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6617fd59a867SMatthew G. Knepley 
6618f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6619c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6620385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6621ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
66227391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
66237391c1cbSMatthew G. Knepley       const char *bdname;
6624dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
66257391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
66267391c1cbSMatthew G. Knepley       if (duplicate) break;
66277391c1cbSMatthew G. Knepley     }
6628ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6629b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6630092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
66317391c1cbSMatthew G. Knepley     }
6632ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6633f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
663493a5981aSMatthew G. Knepley       PetscInt       *newidx;
6635ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
663693a5981aSMatthew G. Knepley 
6637fd59a867SMatthew G. Knepley       bcFields[bc] = field;
66380e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6639ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6640ebb3236fSMatthew G. Knepley         IS              tmp;
6641ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6642ebb3236fSMatthew G. Knepley 
6643c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6644ebb3236fSMatthew G. Knepley         if (!tmp) continue;
664593a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
664693a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6647ebb3236fSMatthew G. Knepley         if (isFE[field]) {
664893a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6649ebb3236fSMatthew G. Knepley         } else {
6650ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6651ebb3236fSMatthew G. Knepley         }
665293a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
665393a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6654fd59a867SMatthew G. Knepley       }
6655ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6656ebb3236fSMatthew G. Knepley       newn = 0;
6657ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6658ebb3236fSMatthew G. Knepley         IS              tmp;
6659ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6660ebb3236fSMatthew G. Knepley 
6661c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6662ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6663ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6664ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6665ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6666ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6667ebb3236fSMatthew G. Knepley         } else {
6668ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6669ebb3236fSMatthew G. Knepley         }
6670ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6671ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6672ebb3236fSMatthew G. Knepley       }
6673ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6674ebb3236fSMatthew G. Knepley     }
6675fd59a867SMatthew G. Knepley   }
6676fd59a867SMatthew G. Knepley   /* Handle discretization */
6677ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6678fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
66790bacfa0aSMatthew G. Knepley     PetscObject obj;
66800bacfa0aSMatthew G. Knepley 
66810bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6682ebb3236fSMatthew G. Knepley     if (isFE[f]) {
66830bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6684286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6685fd59a867SMatthew G. Knepley       PetscInt        d;
6686fd59a867SMatthew G. Knepley 
6687fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6688286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6689286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6690ebb3236fSMatthew G. Knepley     } else {
66910bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
66920bacfa0aSMatthew G. Knepley 
66930bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
66940bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6695ebb3236fSMatthew G. Knepley     }
6696fd59a867SMatthew G. Knepley   }
6697286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6698286d8613SMatthew G. Knepley     PetscInt d;
6699286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6700286d8613SMatthew 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.");
6701286d8613SMatthew G. Knepley     }
6702286d8613SMatthew G. Knepley   }
6703ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6704fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6705fd59a867SMatthew G. Knepley     PetscFE     fe;
6706fd59a867SMatthew G. Knepley     const char *name;
670718abd763SMatthew G. Knepley 
670818abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6709fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6710fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6711fd59a867SMatthew G. Knepley   }
6712fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6713fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
67140e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
67150e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6716286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6717ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6718bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6719bceba477SMatthew G. Knepley }
6720bceba477SMatthew G. Knepley 
67210aef6b92SMatthew G. Knepley /*@
67220aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
67230aef6b92SMatthew G. Knepley 
67240aef6b92SMatthew G. Knepley   Input Parameter:
67250aef6b92SMatthew G. Knepley . dm - The DMPlex object
67260aef6b92SMatthew G. Knepley 
67270aef6b92SMatthew G. Knepley   Output Parameter:
67280aef6b92SMatthew G. Knepley . regular - The flag
67290aef6b92SMatthew G. Knepley 
67300aef6b92SMatthew G. Knepley   Level: intermediate
67310aef6b92SMatthew G. Knepley 
67320aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
67330aef6b92SMatthew G. Knepley @*/
67340aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
67350aef6b92SMatthew G. Knepley {
67360aef6b92SMatthew G. Knepley   PetscFunctionBegin;
67370aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67380aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
67390aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
67400aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
67410aef6b92SMatthew G. Knepley }
67420aef6b92SMatthew G. Knepley 
67430aef6b92SMatthew G. Knepley /*@
67440aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
67450aef6b92SMatthew G. Knepley 
67460aef6b92SMatthew G. Knepley   Input Parameters:
67470aef6b92SMatthew G. Knepley + dm - The DMPlex object
67480aef6b92SMatthew G. Knepley - regular - The flag
67490aef6b92SMatthew G. Knepley 
67500aef6b92SMatthew G. Knepley   Level: intermediate
67510aef6b92SMatthew G. Knepley 
67520aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
67530aef6b92SMatthew G. Knepley @*/
67540aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
67550aef6b92SMatthew G. Knepley {
67560aef6b92SMatthew G. Knepley   PetscFunctionBegin;
67570aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67580aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
67590aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
67600aef6b92SMatthew G. Knepley }
67610aef6b92SMatthew G. Knepley 
6762f7c74593SToby Isaac /* anchors */
6763a68b90caSToby Isaac /*@
6764f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6765f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6766a68b90caSToby Isaac 
6767e228b242SToby Isaac   not collective
6768a68b90caSToby Isaac 
6769a68b90caSToby Isaac   Input Parameters:
6770a68b90caSToby Isaac . dm - The DMPlex object
6771a68b90caSToby Isaac 
6772a68b90caSToby Isaac   Output Parameters:
6773a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6774a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6775a68b90caSToby Isaac 
6776a68b90caSToby Isaac 
6777a68b90caSToby Isaac   Level: intermediate
6778a68b90caSToby Isaac 
6779f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6780a68b90caSToby Isaac @*/
6781a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6782a68b90caSToby Isaac {
6783a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
678441e6d900SToby Isaac   PetscErrorCode ierr;
6785a68b90caSToby Isaac 
6786a68b90caSToby Isaac   PetscFunctionBegin;
6787a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
678841e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6789a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6790a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6791a68b90caSToby Isaac   PetscFunctionReturn(0);
6792a68b90caSToby Isaac }
6793a68b90caSToby Isaac 
6794a68b90caSToby Isaac /*@
6795f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6796f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6797a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6798a68b90caSToby Isaac 
6799a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6800f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6801a68b90caSToby Isaac 
6802e228b242SToby Isaac   collective on dm
6803a68b90caSToby Isaac 
6804a68b90caSToby Isaac   Input Parameters:
6805a68b90caSToby Isaac + dm - The DMPlex object
6806e228b242SToby 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).
6807e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6808a68b90caSToby Isaac 
6809a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6810a68b90caSToby Isaac 
6811a68b90caSToby Isaac   Level: intermediate
6812a68b90caSToby Isaac 
6813f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6814a68b90caSToby Isaac @*/
6815a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6816a68b90caSToby Isaac {
6817a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6818e228b242SToby Isaac   PetscMPIInt    result;
6819a68b90caSToby Isaac   PetscErrorCode ierr;
6820a68b90caSToby Isaac 
6821a68b90caSToby Isaac   PetscFunctionBegin;
6822a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6823e228b242SToby Isaac   if (anchorSection) {
6824e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6825e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6826f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6827e228b242SToby Isaac   }
6828e228b242SToby Isaac   if (anchorIS) {
6829e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6830e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6831f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6832e228b242SToby Isaac   }
6833a68b90caSToby Isaac 
6834a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6835a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6836a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6837a68b90caSToby Isaac 
6838a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6839a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6840a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6841a68b90caSToby Isaac 
6842a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6843a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6844a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6845a68b90caSToby Isaac     const PetscInt *anchors;
6846a68b90caSToby Isaac 
6847a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6848a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6849a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6850a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6851a68b90caSToby Isaac       PetscInt p;
6852a68b90caSToby Isaac 
6853a68b90caSToby Isaac       p = anchors[a];
6854a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6855a68b90caSToby Isaac         PetscInt dof;
6856a68b90caSToby Isaac 
6857a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6858a68b90caSToby Isaac         if (dof) {
6859a68b90caSToby Isaac           PetscErrorCode ierr2;
6860a68b90caSToby Isaac 
6861a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
68628ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6863a68b90caSToby Isaac         }
6864a68b90caSToby Isaac       }
6865a68b90caSToby Isaac     }
6866a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6867a68b90caSToby Isaac   }
6868a68b90caSToby Isaac #endif
6869f7c74593SToby Isaac   /* reset the generic constraints */
6870f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6871a68b90caSToby Isaac   PetscFunctionReturn(0);
6872a68b90caSToby Isaac }
6873a68b90caSToby Isaac 
6874f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6875a68b90caSToby Isaac {
6876f7c74593SToby Isaac   PetscSection anchorSection;
68776995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6878a68b90caSToby Isaac   PetscErrorCode ierr;
6879a68b90caSToby Isaac 
6880a68b90caSToby Isaac   PetscFunctionBegin;
6881a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6882a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6883e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6884a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
68856995de1eSToby Isaac   if (numFields) {
6886719ab38cSToby Isaac     PetscInt f;
6887a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6888719ab38cSToby Isaac 
6889719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6890719ab38cSToby Isaac       PetscInt numComp;
6891719ab38cSToby Isaac 
6892719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6893719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6894719ab38cSToby Isaac     }
68956995de1eSToby Isaac   }
6896a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
68976995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
68986995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
68996995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
69006995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6901a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6902a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6903a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6904a68b90caSToby Isaac     if (dof) {
6905a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6906a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6907a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6908a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6909a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6910a68b90caSToby Isaac       }
6911a68b90caSToby Isaac     }
6912a68b90caSToby Isaac   }
6913a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6914a68b90caSToby Isaac   PetscFunctionReturn(0);
6915a68b90caSToby Isaac }
6916a68b90caSToby Isaac 
6917f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6918a68b90caSToby Isaac {
6919f7c74593SToby Isaac   PetscSection aSec;
69200ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
69210ac89760SToby Isaac   const PetscInt *anchors;
69220ac89760SToby Isaac   PetscInt numFields, f;
692366ad2231SToby Isaac   IS aIS;
69240ac89760SToby Isaac   PetscErrorCode ierr;
69250ac89760SToby Isaac 
69260ac89760SToby Isaac   PetscFunctionBegin;
69270ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69280ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
69290ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
69300ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
69310ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6932302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6933a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
693466ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
69356995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
69366995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
69370ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
69380ac89760SToby Isaac   i[0] = 0;
69390ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
69400ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6941f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6942f19733c5SToby Isaac 
6943f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6944f19733c5SToby Isaac     if (!rDof) continue;
6945f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
69460ac89760SToby Isaac     if (numFields) {
69470ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
69480ac89760SToby Isaac         annz = 0;
6949f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6950f19733c5SToby Isaac           a = anchors[rOff + r];
69510ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
69520ac89760SToby Isaac           annz += aDof;
69530ac89760SToby Isaac         }
69540ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
69550ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
69560ac89760SToby Isaac         for (q = 0; q < dof; q++) {
69570ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
69580ac89760SToby Isaac         }
69590ac89760SToby Isaac       }
69600ac89760SToby Isaac     }
69610ac89760SToby Isaac     else {
69620ac89760SToby Isaac       annz = 0;
69630ac89760SToby Isaac       for (q = 0; q < dof; q++) {
69640ac89760SToby Isaac         a = anchors[off + q];
69650ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
69660ac89760SToby Isaac         annz += aDof;
69670ac89760SToby Isaac       }
69680ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
69690ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
69700ac89760SToby Isaac       for (q = 0; q < dof; q++) {
69710ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
69720ac89760SToby Isaac       }
69730ac89760SToby Isaac     }
69740ac89760SToby Isaac   }
69750ac89760SToby Isaac   nnz = i[m];
69760ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
69770ac89760SToby Isaac   offset = 0;
69780ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
69790ac89760SToby Isaac     if (numFields) {
69800ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
69810ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
69820ac89760SToby Isaac         for (q = 0; q < dof; q++) {
69830ac89760SToby Isaac           PetscInt rDof, rOff, r;
69840ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
69850ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
69860ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
69870ac89760SToby Isaac             PetscInt s;
69880ac89760SToby Isaac 
69890ac89760SToby Isaac             a = anchors[rOff + r];
69900ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
69910ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
69920ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
69930ac89760SToby Isaac               j[offset++] = aOff + s;
69940ac89760SToby Isaac             }
69950ac89760SToby Isaac           }
69960ac89760SToby Isaac         }
69970ac89760SToby Isaac       }
69980ac89760SToby Isaac     }
69990ac89760SToby Isaac     else {
70000ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
70010ac89760SToby Isaac       for (q = 0; q < dof; q++) {
70020ac89760SToby Isaac         PetscInt rDof, rOff, r;
70030ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
70040ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
70050ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
70060ac89760SToby Isaac           PetscInt s;
70070ac89760SToby Isaac 
70080ac89760SToby Isaac           a = anchors[rOff + r];
70090ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
70100ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
70110ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
70120ac89760SToby Isaac             j[offset++] = aOff + s;
70130ac89760SToby Isaac           }
70140ac89760SToby Isaac         }
70150ac89760SToby Isaac       }
70160ac89760SToby Isaac     }
70170ac89760SToby Isaac   }
70180ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
701925570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
702025570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
702166ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
70220ac89760SToby Isaac   PetscFunctionReturn(0);
70230ac89760SToby Isaac }
70240ac89760SToby Isaac 
702566ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
702666ad2231SToby Isaac {
7027f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7028f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
702966ad2231SToby Isaac   Mat            cMat;
703066ad2231SToby Isaac   PetscErrorCode ierr;
703166ad2231SToby Isaac 
703266ad2231SToby Isaac   PetscFunctionBegin;
703366ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7034a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
703566ad2231SToby Isaac   if (anchorSection) {
7036e228b242SToby Isaac     PetscDS  ds;
7037e228b242SToby Isaac     PetscInt nf;
7038e228b242SToby Isaac 
7039f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
7040f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7041f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
7042e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
7043302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
7044e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
704566ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
704666ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
704766ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
704866ad2231SToby Isaac   }
704966ad2231SToby Isaac   PetscFunctionReturn(0);
705066ad2231SToby Isaac }
7051