xref: /petsc/src/dm/impls/plex/plex.c (revision 7d5acc755cf74f687e4e4dd95bfddc97b275c363)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2af0996ceSBarry Smith #include <petsc/private/isimpl.h>
3e5c6e791SKarl Rupp #include <petsc/private/vecimpl.h>
48135c375SStefano Zampini #include <petsc/private/glvisvecimpl.h>
50c312b8eSJed Brown #include <petscsf.h>
6e228b242SToby Isaac #include <petscds.h>
7e412dcbdSMatthew G. Knepley #include <petscdraw.h>
8f19dbd58SToby Isaac #include <petscdmfield.h>
9552f7358SJed Brown 
10552f7358SJed Brown /* Logging support */
1167eb269bSLisandro Dalcin PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_InterpolateSF, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh;
12552f7358SJed Brown 
135a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
14552f7358SJed Brown 
15e5337592SStefano Zampini /*@
16e5337592SStefano Zampini   DMPlexRefineSimplexToTensor - Uniformly refines simplicial cells into tensor product cells.
17e5337592SStefano Zampini   3 quadrilaterals per triangle in 2D and 4 hexahedra per tetrahedron in 3D.
18e5337592SStefano Zampini 
19e5337592SStefano Zampini   Collective
20e5337592SStefano Zampini 
21e5337592SStefano Zampini   Input Parameters:
22e5337592SStefano Zampini . dm - The DMPlex object
23e5337592SStefano Zampini 
24e5337592SStefano Zampini   Output Parameters:
25e5337592SStefano Zampini . dmRefined - The refined DMPlex object
26e5337592SStefano Zampini 
27e5337592SStefano Zampini   Note: Returns NULL if the mesh is already a tensor product mesh.
28e5337592SStefano Zampini 
29e5337592SStefano Zampini   Level: intermediate
30e5337592SStefano Zampini 
31e5337592SStefano Zampini .seealso: DMPlexCreate(), DMPlexSetRefinementUniform()
32e5337592SStefano Zampini @*/
33e5337592SStefano Zampini PetscErrorCode DMPlexRefineSimplexToTensor(DM dm, DM *dmRefined)
34e5337592SStefano Zampini {
35e5337592SStefano Zampini   PetscInt         dim, cMax, fMax, cStart, cEnd, coneSize;
36e5337592SStefano Zampini   CellRefiner      cellRefiner;
37e5337592SStefano Zampini   PetscBool        lop, allnoop, localized;
38e5337592SStefano Zampini   PetscErrorCode   ierr;
39e5337592SStefano Zampini 
40e5337592SStefano Zampini   PetscFunctionBegin;
41e5337592SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42e5337592SStefano Zampini   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
43e5337592SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,&fMax,NULL,NULL);CHKERRQ(ierr);
44e5337592SStefano Zampini   if (cMax >= 0 || fMax >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle hybrid meshes yet");
45e5337592SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
46e5337592SStefano Zampini   if (!(cEnd - cStart)) cellRefiner = REFINER_NOOP;
47e5337592SStefano Zampini   else {
48e5337592SStefano Zampini     ierr = DMPlexGetConeSize(dm,cStart,&coneSize);CHKERRQ(ierr);
49e5337592SStefano Zampini     switch (dim) {
50e5337592SStefano Zampini     case 1:
51e5337592SStefano Zampini       cellRefiner = REFINER_NOOP;
52e5337592SStefano Zampini     break;
53e5337592SStefano Zampini     case 2:
54e5337592SStefano Zampini       switch (coneSize) {
55e5337592SStefano Zampini       case 3:
56e5337592SStefano Zampini         cellRefiner = REFINER_SIMPLEX_TO_HEX_2D;
57e5337592SStefano Zampini       break;
58e5337592SStefano Zampini       case 4:
59e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
60e5337592SStefano Zampini       break;
61e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
62e5337592SStefano Zampini       }
63e5337592SStefano Zampini     break;
64e5337592SStefano Zampini     case 3:
65e5337592SStefano Zampini       switch (coneSize) {
66e5337592SStefano Zampini       case 4:
67e5337592SStefano Zampini         cellRefiner = REFINER_SIMPLEX_TO_HEX_3D;
68e5337592SStefano Zampini       break;
69e5337592SStefano Zampini       case 6:
70e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
71e5337592SStefano Zampini       break;
72e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
73e5337592SStefano Zampini       }
74e5337592SStefano Zampini     break;
75e5337592SStefano Zampini     default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle dimension %D",dim);
76e5337592SStefano Zampini     }
77e5337592SStefano Zampini   }
78e5337592SStefano Zampini   /* return if we don't need to refine */
79e5337592SStefano Zampini   lop = (cellRefiner == REFINER_NOOP) ? PETSC_TRUE : PETSC_FALSE;
80e5337592SStefano Zampini   ierr = MPIU_Allreduce(&lop,&allnoop,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
81e5337592SStefano Zampini   if (allnoop) {
82e5337592SStefano Zampini     *dmRefined = NULL;
83e5337592SStefano Zampini     PetscFunctionReturn(0);
84e5337592SStefano Zampini   }
85e5337592SStefano Zampini   ierr = DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);CHKERRQ(ierr);
86e5337592SStefano Zampini   ierr = DMCopyBoundary(dm, *dmRefined);CHKERRQ(ierr);
87e5337592SStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
88e5337592SStefano Zampini   if (localized) {
89e5337592SStefano Zampini     ierr = DMLocalizeCoordinates(*dmRefined);CHKERRQ(ierr);
90e5337592SStefano Zampini   }
91e5337592SStefano Zampini   PetscFunctionReturn(0);
92e5337592SStefano Zampini }
93e5337592SStefano Zampini 
947afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
957e42fee7SMatthew G. Knepley {
96a99a26bcSAdrian Croucher   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior;
97a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
987e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
997e42fee7SMatthew G. Knepley 
1007e42fee7SMatthew G. Knepley   PetscFunctionBegin;
1017e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
102c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1037e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1047e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10580d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
10680d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1077e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1087e42fee7SMatthew G. Knepley   if (field >= 0) {
109a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
110a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
1117e42fee7SMatthew G. Knepley   } else {
112a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
113a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
1147e42fee7SMatthew G. Knepley   }
1155483465cSMatthew G. Knepley   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
116a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
1177e42fee7SMatthew G. Knepley     *sStart = vStart;
1187e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
119a99a26bcSAdrian Croucher     if (globalvcdof[0] == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1207e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_POINT_FIELD;
121a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
1227e42fee7SMatthew G. Knepley     *sStart = cStart;
1237e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
124a99a26bcSAdrian Croucher     if (globalvcdof[1] == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1257e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_CELL_FIELD;
1267e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
1277e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1287e42fee7SMatthew G. Knepley }
1297e42fee7SMatthew G. Knepley 
1307cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
131e412dcbdSMatthew G. Knepley {
132e412dcbdSMatthew G. Knepley   DM                 dm;
133d1df6f1dSMatthew G. Knepley   PetscSection       s;
134e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
135e412dcbdSMatthew G. Knepley   DM                 cdm;
136e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
137e412dcbdSMatthew G. Knepley   Vec                coordinates;
138e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
139e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
140339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
141339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
142d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
143e412dcbdSMatthew G. Knepley   const char        *name;
144339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
145e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
146e412dcbdSMatthew G. Knepley 
147e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
148d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
149d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
150d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
151d1df6f1dSMatthew G. Knepley 
152e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
153e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1548135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
155d1df6f1dSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
156d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
157e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
158e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
159e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
160e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
161e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
162e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
163e412dcbdSMatthew G. Knepley 
164e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
165339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
166e412dcbdSMatthew G. Knepley 
167e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
168e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
169e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1700c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1710c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
172e412dcbdSMatthew G. Knepley   }
173e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
175e412dcbdSMatthew G. Knepley 
176d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
177d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
178d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
179d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
180d1df6f1dSMatthew G. Knepley     IS   fis;
181d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
182d1df6f1dSMatthew G. Knepley     const char *fname;
183d1df6f1dSMatthew G. Knepley 
184d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
185d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
186d1df6f1dSMatthew G. Knepley 
187a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
188d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
189d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
190d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
191d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
192a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
193a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
194d1df6f1dSMatthew G. Knepley     }
195d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
196d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
197d1df6f1dSMatthew G. Knepley 
198d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
199d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
200d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
201d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
202d1df6f1dSMatthew G. Knepley 
203d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
204d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
205339e3443SMatthew G. Knepley       if (!flg) {
206d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
207d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
208d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
209339e3443SMatthew G. Knepley       }
210e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
211339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
212d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
213e412dcbdSMatthew G. Knepley 
214d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
215e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
21699a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
217e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
218e412dcbdSMatthew G. Knepley 
219d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
220339e3443SMatthew G. Knepley         if (a) {
221d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
222339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
223339e3443SMatthew G. Knepley         } else {
224339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
225339e3443SMatthew G. Knepley           PetscInt     numVals, va;
226339e3443SMatthew G. Knepley 
227d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
228d1df6f1dSMatthew 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);
229d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
230d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
231d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
232d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
233339e3443SMatthew G. Knepley             break;
234d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
235d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
236d1df6f1dSMatthew 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]);
237d1df6f1dSMatthew G. Knepley             break;
238d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
239339e3443SMatthew G. Knepley           }
240d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
241339e3443SMatthew G. Knepley         }
242e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
243e412dcbdSMatthew G. Knepley         switch (numCoords) {
244e412dcbdSMatthew G. Knepley         case 6:
245339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);CHKERRQ(ierr);
246e412dcbdSMatthew G. Knepley           break;
247e412dcbdSMatthew G. Knepley         case 8:
248339e3443SMatthew 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);
249339e3443SMatthew 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);
250e412dcbdSMatthew G. Knepley           break;
251e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
252e412dcbdSMatthew G. Knepley         }
253e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
254e412dcbdSMatthew G. Knepley       }
255d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
256e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
257e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
258e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
259d1df6f1dSMatthew G. Knepley     }
260d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
261d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
262d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
263d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
264d1df6f1dSMatthew G. Knepley     }
265d1df6f1dSMatthew G. Knepley   }
266e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
267e412dcbdSMatthew G. Knepley }
268e412dcbdSMatthew G. Knepley 
269684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
270684b87d9SLisandro Dalcin {
271684b87d9SLisandro Dalcin   DM                      dm;
272684b87d9SLisandro Dalcin   Vec                     locv;
273684b87d9SLisandro Dalcin   const char              *name;
274684b87d9SLisandro Dalcin   PetscSection            section;
275684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
276684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
277684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
278684b87d9SLisandro Dalcin 
279684b87d9SLisandro Dalcin   PetscFunctionBegin;
280684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
281684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
282684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
283684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
284684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
285684b87d9SLisandro Dalcin   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
286684b87d9SLisandro Dalcin   ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
287684b87d9SLisandro Dalcin   ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) locv);CHKERRQ(ierr);
288684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
289684b87d9SLisandro Dalcin }
290684b87d9SLisandro Dalcin 
291552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
292552f7358SJed Brown {
293552f7358SJed Brown   DM             dm;
294684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
295552f7358SJed Brown   PetscErrorCode ierr;
296552f7358SJed Brown 
297552f7358SJed Brown   PetscFunctionBegin;
298552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
29982f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
300552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
301b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
302f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3038135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
304684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
305684b87d9SLisandro Dalcin     PetscInt    i,numFields;
306684b87d9SLisandro Dalcin     PetscObject fe;
307ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
308684b87d9SLisandro Dalcin     Vec         locv = v;
309684b87d9SLisandro Dalcin     const char  *name;
310684b87d9SLisandro Dalcin     PetscInt    step;
311684b87d9SLisandro Dalcin     PetscReal   time;
312ef31f671SMatthew G. Knepley 
313ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
314684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
315684b87d9SLisandro Dalcin       ierr = DMGetField(dm, i, &fe);CHKERRQ(ierr);
316684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
317ef31f671SMatthew G. Knepley     }
318684b87d9SLisandro Dalcin     if (fem) {
319684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
320684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
321684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
322684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
323684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
324684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
325ef31f671SMatthew G. Knepley     }
326552f7358SJed Brown     if (isvtk) {
327684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
328b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
329b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
330684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
331b136c2c9SMatthew G. Knepley #else
332b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
333b136c2c9SMatthew G. Knepley #endif
334f13a32a3SMatthew G. Knepley     } else if (isdraw) {
335684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
336684b87d9SLisandro Dalcin     } else if (isglvis) {
337684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
338684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
339684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
340684b87d9SLisandro Dalcin     }
341684b87d9SLisandro Dalcin     if (fem) {ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);}
342552f7358SJed Brown   } else {
343684b87d9SLisandro Dalcin     PetscBool isseq;
344684b87d9SLisandro Dalcin 
345684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
34655f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
34755f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
348552f7358SJed Brown   }
349552f7358SJed Brown   PetscFunctionReturn(0);
350552f7358SJed Brown }
351552f7358SJed Brown 
352552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
353552f7358SJed Brown {
354552f7358SJed Brown   DM             dm;
355684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
356552f7358SJed Brown   PetscErrorCode ierr;
357552f7358SJed Brown 
358552f7358SJed Brown   PetscFunctionBegin;
359552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
36082f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
361552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
36233c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
363e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3648135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
365684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
366552f7358SJed Brown     Vec         locv;
367552f7358SJed Brown     const char *name;
368552f7358SJed Brown 
369c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
370552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
371552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
372552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
373552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
374552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
375c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
376b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
377b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
37839d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
379b136c2c9SMatthew G. Knepley #else
380b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
381b136c2c9SMatthew G. Knepley #endif
382552f7358SJed Brown   } else {
383684b87d9SLisandro Dalcin     PetscBool isseq;
384684b87d9SLisandro Dalcin 
385684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
38655f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
38755f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
388552f7358SJed Brown   }
389552f7358SJed Brown   PetscFunctionReturn(0);
390552f7358SJed Brown }
391552f7358SJed Brown 
392d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
393d930f514SMatthew G. Knepley {
394d930f514SMatthew G. Knepley   DM                dm;
395d930f514SMatthew G. Knepley   MPI_Comm          comm;
396d930f514SMatthew G. Knepley   PetscViewerFormat format;
397d930f514SMatthew G. Knepley   Vec               v;
398d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
399d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
400d930f514SMatthew G. Knepley 
401d930f514SMatthew G. Knepley   PetscFunctionBegin;
402d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
403d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4042c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
405d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
406d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
407d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
408d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
409d930f514SMatthew G. Knepley     const char *vecname;
410d930f514SMatthew G. Knepley     PetscInt    n, nroots;
411d930f514SMatthew G. Knepley 
412d930f514SMatthew G. Knepley     if (dm->sfNatural) {
413ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
414d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
415d930f514SMatthew G. Knepley       if (n == nroots) {
416d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
417d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
418d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
419d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
420d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
421d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
422d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
423d930f514SMatthew G. Knepley   } else {
424d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
425d930f514SMatthew G. Knepley     v = originalv;
426d930f514SMatthew G. Knepley   }
427d930f514SMatthew G. Knepley   if (ishdf5) {
428d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
42939d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
430d930f514SMatthew G. Knepley #else
431d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
432d930f514SMatthew G. Knepley #endif
433d930f514SMatthew G. Knepley   } else if (isvtk) {
434d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
435d930f514SMatthew G. Knepley   } else {
436d930f514SMatthew G. Knepley     PetscBool isseq;
437d930f514SMatthew G. Knepley 
438d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
439d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
440d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
441d930f514SMatthew G. Knepley   }
442d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
443d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
444d930f514SMatthew G. Knepley }
445d930f514SMatthew G. Knepley 
4462c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4472c40f234SMatthew G. Knepley {
4482c40f234SMatthew G. Knepley   DM             dm;
4492c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4502c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4512c40f234SMatthew G. Knepley 
4522c40f234SMatthew G. Knepley   PetscFunctionBegin;
4532c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4542c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4552c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4562c40f234SMatthew G. Knepley   if (ishdf5) {
4572c40f234SMatthew G. Knepley     DM          dmBC;
4582c40f234SMatthew G. Knepley     Vec         gv;
4592c40f234SMatthew G. Knepley     const char *name;
4602c40f234SMatthew G. Knepley 
4612c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4622c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4632c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4652c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4662c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4672c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4682c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4692c40f234SMatthew G. Knepley   } else {
4702c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4712c40f234SMatthew G. Knepley   }
4722c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4732c40f234SMatthew G. Knepley }
4742c40f234SMatthew G. Knepley 
4752c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4762c40f234SMatthew G. Knepley {
4772c40f234SMatthew G. Knepley   DM             dm;
4782c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4792c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4802c40f234SMatthew G. Knepley 
4812c40f234SMatthew G. Knepley   PetscFunctionBegin;
4822c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4832c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4842c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4852c40f234SMatthew G. Knepley   if (ishdf5) {
486878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
48739d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
488b136c2c9SMatthew G. Knepley #else
489b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
490878b459fSMatthew G. Knepley #endif
4912c40f234SMatthew G. Knepley   } else {
4922c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
493552f7358SJed Brown   }
494552f7358SJed Brown   PetscFunctionReturn(0);
495552f7358SJed Brown }
496552f7358SJed Brown 
497d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
498d930f514SMatthew G. Knepley {
499d930f514SMatthew G. Knepley   DM                dm;
500d930f514SMatthew G. Knepley   PetscViewerFormat format;
501d930f514SMatthew G. Knepley   PetscBool         ishdf5;
502d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
503d930f514SMatthew G. Knepley 
504d930f514SMatthew G. Knepley   PetscFunctionBegin;
505d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
506d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
507d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
508d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
509d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
510d930f514SMatthew G. Knepley     if (dm->sfNatural) {
511d930f514SMatthew G. Knepley       if (ishdf5) {
512d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
513d930f514SMatthew G. Knepley         Vec         v;
514d930f514SMatthew G. Knepley         const char *vecname;
515d930f514SMatthew G. Knepley 
516d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
517d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
518d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
51939d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
520d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
521d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
522d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
523d930f514SMatthew G. Knepley #else
524d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
525d930f514SMatthew G. Knepley #endif
526d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
527d930f514SMatthew G. Knepley     }
528d930f514SMatthew G. Knepley   }
529d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
530d930f514SMatthew G. Knepley }
531d930f514SMatthew G. Knepley 
5327cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
533731e8ddeSMatthew G. Knepley {
534731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
535731e8ddeSMatthew G. Knepley   Vec                coordinates;
536731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
537731e8ddeSMatthew G. Knepley   const char        *name[4];
538731e8ddeSMatthew G. Knepley   const PetscScalar *a;
539731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
540731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
541731e8ddeSMatthew G. Knepley 
542731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
543731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
544731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
545731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
546731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
547731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
548731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
549731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
550731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
551731e8ddeSMatthew G. Knepley   name[1]     = "edge";
552731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
553731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
554731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
555731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
556731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
557731e8ddeSMatthew G. Knepley 
558f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
559731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
560731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
561731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
562731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
563731e8ddeSMatthew G. Knepley 
564731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
565731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
566731e8ddeSMatthew G. Knepley       if (!dof) continue;
567731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
568731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
569f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
570731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
571731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
572731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
573731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
57441477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
575731e8ddeSMatthew G. Knepley         }
576731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
577731e8ddeSMatthew G. Knepley       }
578731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
579731e8ddeSMatthew G. Knepley     }
580731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
581731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
582731e8ddeSMatthew G. Knepley   }
583731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
584731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
585731e8ddeSMatthew G. Knepley }
586731e8ddeSMatthew G. Knepley 
5877cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
588552f7358SJed Brown {
589552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
590552f7358SJed Brown   DM                cdm;
591552f7358SJed Brown   DMLabel           markers;
592552f7358SJed Brown   PetscSection      coordSection;
593552f7358SJed Brown   Vec               coordinates;
594552f7358SJed Brown   PetscViewerFormat format;
595552f7358SJed Brown   PetscErrorCode    ierr;
596552f7358SJed Brown 
597552f7358SJed Brown   PetscFunctionBegin;
598552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
599552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
600552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
601552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
602552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
603552f7358SJed Brown     const char *name;
604f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
605552f7358SJed Brown     PetscInt    pStart, pEnd, p;
606552f7358SJed Brown     PetscMPIInt rank, size;
607552f7358SJed Brown 
60882f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
60982f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
610552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
611552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
612552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
613f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
614f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
615f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
616f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
617f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
618e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6194d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
620e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
621552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
622552f7358SJed Brown       PetscInt dof, off, s;
623552f7358SJed Brown 
624552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
625552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
626552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
627e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
628552f7358SJed Brown       }
629552f7358SJed Brown     }
630552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
631e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
632e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
633552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
634552f7358SJed Brown       PetscInt dof, off, c;
635552f7358SJed Brown 
636552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
637552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
638552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
639e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
640552f7358SJed Brown       }
641552f7358SJed Brown     }
642552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6434d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6440298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
645552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
646c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
647552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
648552f7358SJed Brown     if (size > 1) {
649552f7358SJed Brown       PetscSF sf;
650552f7358SJed Brown 
651552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
652552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
653552f7358SJed Brown     }
654552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
655552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6560588280cSMatthew G. Knepley     const char  *name, *color;
6570588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6580588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
659552f7358SJed Brown     PetscReal    scale         = 2.0;
6600588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6610588280cSMatthew G. Knepley     double       tcoords[3];
662552f7358SJed Brown     PetscScalar *coords;
6630588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
664552f7358SJed Brown     PetscMPIInt  rank, size;
6650588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
666552f7358SJed Brown 
6670588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
668552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
669c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6700588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6710588280cSMatthew G. Knepley     numColors  = 10;
6720588280cSMatthew G. Knepley     numLColors = 10;
6730588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
674c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
675c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
676c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6770588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
678c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6790588280cSMatthew G. Knepley     if (!useColors) {
6800588280cSMatthew G. Knepley       numColors = 3;
6810588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6820588280cSMatthew G. Knepley     }
683c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6840588280cSMatthew G. Knepley     if (!useColors) {
6850588280cSMatthew G. Knepley       numLColors = 4;
6860588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
6870588280cSMatthew G. Knepley     }
68882f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
68982f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
690552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
691770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
6920588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
693552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
694552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
695552f7358SJed Brown \\usetikzlibrary{arrows}\n\
6960588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
6970588280cSMatthew G. Knepley     if (size > 1) {
698f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
699770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
700770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
701770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
702770b213bSMatthew G Knepley         } else if (p > 0) {
703770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
704770b213bSMatthew G Knepley         }
705770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
706770b213bSMatthew G Knepley       }
7070588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7080588280cSMatthew G. Knepley     }
7090588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
710552f7358SJed Brown     /* Plot vertices */
711552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
712552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7134d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
714552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
715552f7358SJed Brown       PetscInt  off, dof, d;
7160588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
717552f7358SJed Brown 
718552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
719552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7200588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
721f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7220588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7230588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
724c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7250588280cSMatthew G. Knepley       }
7260588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7270588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
728552f7358SJed Brown       for (d = 0; d < dof; ++d) {
729552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7300588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
731552f7358SJed Brown       }
7320588280cSMatthew G. Knepley       color = colors[rank%numColors];
7330588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7340588280cSMatthew G. Knepley         PetscInt val;
735c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7360588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7370588280cSMatthew G. Knepley       }
7380588280cSMatthew G. Knepley       if (useNumbers) {
739e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7400588280cSMatthew G. Knepley       } else {
741e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7420588280cSMatthew G. Knepley       }
743552f7358SJed Brown     }
744552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
745552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
746552f7358SJed Brown     /* Plot edges */
7470588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
7480588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
749552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
750552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
751552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
752552f7358SJed Brown         const PetscInt *cone;
753552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
754552f7358SJed Brown 
755552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
756f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
757552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
758552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
759552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
760552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
761552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
762552f7358SJed Brown         for (d = 0; d < dof; ++d) {
7630588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
764c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
765552f7358SJed Brown         }
7660588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
7670588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
7680588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
7690588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
770f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
7710588280cSMatthew G. Knepley         }
7720588280cSMatthew G. Knepley         color = colors[rank%numColors];
7730588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7740588280cSMatthew G. Knepley           PetscInt val;
775c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7760750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7770588280cSMatthew G. Knepley         }
778e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
779552f7358SJed Brown       }
780552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
781552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
782552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
7830588280cSMatthew G. Knepley     }
784552f7358SJed Brown     /* Plot cells */
7850588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
7860588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
7870588280cSMatthew G. Knepley         const PetscInt *cone;
7880588280cSMatthew G. Knepley 
7890588280cSMatthew G. Knepley         color = colors[rank%numColors];
7900588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7910588280cSMatthew G. Knepley           PetscInt val;
792c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
7930588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7940588280cSMatthew G. Knepley         }
7950588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
796e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
7970588280cSMatthew G. Knepley       }
7980588280cSMatthew G. Knepley     } else {
799552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
800552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
8010298fd71SBarry Smith         PetscInt *closure = NULL;
802552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
803552f7358SJed Brown 
804552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
805552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
806552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
807552f7358SJed Brown           const PetscInt point = closure[p];
808552f7358SJed Brown 
809552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
810552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
811e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
812552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
813552f7358SJed Brown         }
814552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
815e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
816552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
817552f7358SJed Brown       }
8180588280cSMatthew G. Knepley     }
819552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8204d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8210588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
822770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
8230588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
8240588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
8250588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
8260588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
827552f7358SJed Brown   } else {
82882f516ccSBarry Smith     MPI_Comm    comm;
829834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
830f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
831552f7358SJed Brown     PetscInt    pStart, pEnd, p;
832a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
8331143a3c0SMatthew G. Knepley     const char *name;
834552f7358SJed Brown     PetscMPIInt size;
835552f7358SJed Brown 
83682f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
837552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
838c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
839f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
8405f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
841f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
842f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
843f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
844552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
845b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
846bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
847dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
848552f7358SJed Brown     if (depth == 1) {
849552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
850552f7358SJed Brown       pEnd = pEnd - pStart;
851552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
852f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
853552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
854552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
855552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
856552f7358SJed Brown       pEnd = pEnd - pStart;
857552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
858552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
859552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
860552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
861552f7358SJed Brown     } else {
862cbb7f117SMark Adams       PetscMPIInt rank;
863cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
864552f7358SJed Brown       for (d = 0; d <= dim; d++) {
865552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
866834065abSMatthew G. Knepley         pEnd    -= pStart;
867834065abSMatthew G. Knepley         pMax[d] -= pStart;
868552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
869834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
870552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
871834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
872cbb7f117SMark Adams           if (!rank) {
873834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
874834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
875834065abSMatthew G. Knepley           }
876cbb7f117SMark Adams         }
877552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
878552f7358SJed Brown       }
879552f7358SJed Brown     }
880834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
881c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
882a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
883a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
884a57dd577SMatthew G Knepley       DMLabel         label;
885a57dd577SMatthew G Knepley       const char     *name;
886a57dd577SMatthew G Knepley       IS              valueIS;
887a57dd577SMatthew G Knepley       const PetscInt *values;
888a57dd577SMatthew G Knepley       PetscInt        numValues, v;
889a57dd577SMatthew G Knepley 
890c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
891c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
892a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
893d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
894a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
895a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
896120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
897a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
898a57dd577SMatthew G Knepley         PetscInt size;
899a57dd577SMatthew G Knepley 
900a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
901a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
902d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
903a57dd577SMatthew G Knepley       }
904a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
905120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
906a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
9074d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
908a57dd577SMatthew G Knepley     }
909a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
9108e7ff633SMatthew G. Knepley     if (cdm) {
9118e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
9128e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
9138e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
9148e7ff633SMatthew G. Knepley     }
915552f7358SJed Brown   }
916552f7358SJed Brown   PetscFunctionReturn(0);
917552f7358SJed Brown }
918552f7358SJed Brown 
9197cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
920e412dcbdSMatthew G. Knepley {
921e412dcbdSMatthew G. Knepley   PetscDraw          draw;
922e412dcbdSMatthew G. Knepley   DM                 cdm;
923e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
924e412dcbdSMatthew G. Knepley   Vec                coordinates;
925e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
92629494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
927e412dcbdSMatthew G. Knepley   PetscBool          isnull;
928e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
929e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
930e412dcbdSMatthew G. Knepley 
931e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
932e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
933e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
934e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
935e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
936e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
937e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
938e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
939e412dcbdSMatthew G. Knepley 
940e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
941e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
942e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
943e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
944e412dcbdSMatthew G. Knepley 
945e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
946e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
947e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
9480c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
9490c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
950e412dcbdSMatthew G. Knepley   }
951e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
95229494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
95329494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
95429494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
955e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
956e412dcbdSMatthew G. Knepley 
957e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
958e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
95929494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
960e412dcbdSMatthew G. Knepley 
96129494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
962e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
96329494db1SLisandro Dalcin     switch (coneSize) {
96429494db1SLisandro Dalcin     case 3:
9650c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9660c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9670c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
968e412dcbdSMatthew G. Knepley       break;
96929494db1SLisandro Dalcin     case 4:
9700c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9710c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9720c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9730c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
974e412dcbdSMatthew G. Knepley       break;
97529494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
976e412dcbdSMatthew G. Knepley     }
977e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
978e412dcbdSMatthew G. Knepley   }
979e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
980e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
981e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
982e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
983e412dcbdSMatthew G. Knepley }
984e412dcbdSMatthew G. Knepley 
985552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
986552f7358SJed Brown {
987e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
988552f7358SJed Brown   PetscErrorCode    ierr;
989552f7358SJed Brown 
990552f7358SJed Brown   PetscFunctionBegin;
991552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
992552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
993552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
994fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
995c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
996e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
9978135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
998552f7358SJed Brown   if (iascii) {
9998135c375SStefano Zampini     PetscViewerFormat format;
10008135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
10018135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
10028135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
10038135c375SStefano Zampini     } else {
1004552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
10058135c375SStefano Zampini     }
1006c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1007c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
100839d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1009c6ccd67eSMatthew G. Knepley #else
1010c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1011552f7358SJed Brown #endif
1012e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1013fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1014e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1015e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
10168135c375SStefano Zampini   } else if (isglvis) {
10178135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
101862201deeSVaclav Hapla   } else {
1019a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1020fcf6c8fdSToby Isaac   }
1021cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1022cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1023cb3ba0daSMatthew G. Knepley   if (flg) {
1024cb3ba0daSMatthew G. Knepley     Vec ranks;
1025cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1026cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1027cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1028cb3ba0daSMatthew G. Knepley   }
1029552f7358SJed Brown   PetscFunctionReturn(0);
1030552f7358SJed Brown }
1031552f7358SJed Brown 
10322c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
10332c40f234SMatthew G. Knepley {
1034d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
10352c40f234SMatthew G. Knepley   PetscErrorCode ierr;
10362c40f234SMatthew G. Knepley 
10372c40f234SMatthew G. Knepley   PetscFunctionBegin;
10382c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10392c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
10402c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1041d4f5a9a0SVaclav Hapla   if (ishdf5) {
10422c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
10439c48423bSVaclav Hapla     PetscViewerFormat format;
10449c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
10459c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
10469c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
10479c48423bSVaclav Hapla     } else {
104839d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
10499c48423bSVaclav Hapla     }
10502c40f234SMatthew G. Knepley #else
10512c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1052552f7358SJed Brown #endif
1053d4f5a9a0SVaclav Hapla   } else {
1054d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1055552f7358SJed Brown   }
1056552f7358SJed Brown   PetscFunctionReturn(0);
1057552f7358SJed Brown }
1058552f7358SJed Brown 
1059552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1060552f7358SJed Brown {
1061552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1062552f7358SJed Brown   PetscErrorCode ierr;
1063552f7358SJed Brown 
1064552f7358SJed Brown   PetscFunctionBegin;
10658135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
10668135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
10670d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1068552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1069552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1070552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1071552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1072be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1073552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1074552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1075d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1076d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
107777623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1078a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1079552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1080552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1081a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1082a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1083d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1084d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1085d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1086d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1087d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1088d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1089fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1090552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1091552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1092552f7358SJed Brown   PetscFunctionReturn(0);
1093552f7358SJed Brown }
1094552f7358SJed Brown 
1095b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1096552f7358SJed Brown {
10978d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1098acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1099552f7358SJed Brown   PetscInt               localSize;
1100837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1101552f7358SJed Brown   PetscErrorCode         ierr;
1102b412c318SBarry Smith   MatType                mtype;
11031428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1104552f7358SJed Brown 
1105552f7358SJed Brown   PetscFunctionBegin;
1106607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1107b412c318SBarry Smith   mtype = dm->mattype;
1108552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1109552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1110552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
111182f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1112552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1113552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1114552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1115acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1116acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1117552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1118552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1119552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1120552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1121552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1122552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1123552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1124837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1125552f7358SJed Brown   if (!isShell) {
1126be36d101SStefano Zampini     PetscSection subSection;
1127837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
11280be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1129fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1130552f7358SJed Brown 
113100140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1132be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1133be36d101SStefano Zampini       PetscSection section;
1134be36d101SStefano Zampini       PetscInt     size;
1135be36d101SStefano Zampini 
1136be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1137be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1138be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1139be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1140be36d101SStefano Zampini     } else {
114100140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1142be36d101SStefano Zampini     }
1143552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1144be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1145a9d99c84SMatthew G. Knepley       PetscInt bdof;
1146a9d99c84SMatthew G. Knepley 
1147552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1148fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
11491d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
11501d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
11511d17a0a3SMatthew G. Knepley       if (dof) {
11521d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1153be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1154be36d101SStefano Zampini       }
1155be36d101SStefano Zampini       if (isMatIS) {
1156be36d101SStefano Zampini         PetscInt loff,c,off;
1157be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1158be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1159be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1160552f7358SJed Brown       }
11612a28c762SMatthew G Knepley     }
11622a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
11630be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11640be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11650be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11660be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
11674fa26246SMatthew G. Knepley     bs = bs < 0 ? 1 : bs;
1168be36d101SStefano Zampini     if (isMatIS) {
11694fa26246SMatthew G. Knepley       PetscInt l;
11704fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
11714fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
11724fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1173be36d101SStefano Zampini     }
1174be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1175be36d101SStefano Zampini     if (isMatIS) {
1176be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1177be36d101SStefano Zampini     }
11781795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
11798d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1180552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1181552f7358SJed Brown   }
1182b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1183552f7358SJed Brown   PetscFunctionReturn(0);
1184552f7358SJed Brown }
1185552f7358SJed Brown 
11867cd05799SMatthew G. Knepley /*@
1187a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1188be36d101SStefano Zampini 
1189be36d101SStefano Zampini   Not collective
1190be36d101SStefano Zampini 
1191be36d101SStefano Zampini   Input Parameter:
1192be36d101SStefano Zampini . mesh - The DMPlex
1193be36d101SStefano Zampini 
1194be36d101SStefano Zampini   Output Parameters:
1195be36d101SStefano Zampini . subsection - The subdomain section
1196be36d101SStefano Zampini 
1197be36d101SStefano Zampini   Level: developer
1198be36d101SStefano Zampini 
1199be36d101SStefano Zampini .seealso:
12007cd05799SMatthew G. Knepley @*/
1201be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1202be36d101SStefano Zampini {
1203be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1204be36d101SStefano Zampini   PetscErrorCode ierr;
1205be36d101SStefano Zampini 
1206be36d101SStefano Zampini   PetscFunctionBegin;
1207be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1208be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1209be36d101SStefano Zampini     PetscSection section;
1210be36d101SStefano Zampini     PetscSF      sf;
1211be36d101SStefano Zampini 
1212be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1213be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1214be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1215be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1216be36d101SStefano Zampini   }
1217be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1218be36d101SStefano Zampini   PetscFunctionReturn(0);
1219be36d101SStefano Zampini }
1220be36d101SStefano Zampini 
1221552f7358SJed Brown /*@
1222552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1223552f7358SJed Brown 
1224552f7358SJed Brown   Not collective
1225552f7358SJed Brown 
1226552f7358SJed Brown   Input Parameter:
1227552f7358SJed Brown . mesh - The DMPlex
1228552f7358SJed Brown 
1229552f7358SJed Brown   Output Parameters:
1230552f7358SJed Brown + pStart - The first mesh point
1231552f7358SJed Brown - pEnd   - The upper bound for mesh points
1232552f7358SJed Brown 
1233552f7358SJed Brown   Level: beginner
1234552f7358SJed Brown 
1235552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1236552f7358SJed Brown @*/
1237552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1238552f7358SJed Brown {
1239552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1240552f7358SJed Brown   PetscErrorCode ierr;
1241552f7358SJed Brown 
1242552f7358SJed Brown   PetscFunctionBegin;
1243552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1244552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1245552f7358SJed Brown   PetscFunctionReturn(0);
1246552f7358SJed Brown }
1247552f7358SJed Brown 
1248552f7358SJed Brown /*@
1249552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1250552f7358SJed Brown 
1251552f7358SJed Brown   Not collective
1252552f7358SJed Brown 
1253552f7358SJed Brown   Input Parameters:
1254552f7358SJed Brown + mesh - The DMPlex
1255552f7358SJed Brown . pStart - The first mesh point
1256552f7358SJed Brown - pEnd   - The upper bound for mesh points
1257552f7358SJed Brown 
1258552f7358SJed Brown   Output Parameters:
1259552f7358SJed Brown 
1260552f7358SJed Brown   Level: beginner
1261552f7358SJed Brown 
1262552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1263552f7358SJed Brown @*/
1264552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1265552f7358SJed Brown {
1266552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1267552f7358SJed Brown   PetscErrorCode ierr;
1268552f7358SJed Brown 
1269552f7358SJed Brown   PetscFunctionBegin;
1270552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1271552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1272552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1273552f7358SJed Brown   PetscFunctionReturn(0);
1274552f7358SJed Brown }
1275552f7358SJed Brown 
1276552f7358SJed Brown /*@
1277eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1278552f7358SJed Brown 
1279552f7358SJed Brown   Not collective
1280552f7358SJed Brown 
1281552f7358SJed Brown   Input Parameters:
1282552f7358SJed Brown + mesh - The DMPlex
1283eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1284552f7358SJed Brown 
1285552f7358SJed Brown   Output Parameter:
1286552f7358SJed Brown . size - The cone size for point p
1287552f7358SJed Brown 
1288552f7358SJed Brown   Level: beginner
1289552f7358SJed Brown 
1290552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1291552f7358SJed Brown @*/
1292552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1293552f7358SJed Brown {
1294552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1295552f7358SJed Brown   PetscErrorCode ierr;
1296552f7358SJed Brown 
1297552f7358SJed Brown   PetscFunctionBegin;
1298552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1299552f7358SJed Brown   PetscValidPointer(size, 3);
1300552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1301552f7358SJed Brown   PetscFunctionReturn(0);
1302552f7358SJed Brown }
1303552f7358SJed Brown 
1304552f7358SJed Brown /*@
1305eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1306552f7358SJed Brown 
1307552f7358SJed Brown   Not collective
1308552f7358SJed Brown 
1309552f7358SJed Brown   Input Parameters:
1310552f7358SJed Brown + mesh - The DMPlex
1311eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1312552f7358SJed Brown - size - The cone size for point p
1313552f7358SJed Brown 
1314552f7358SJed Brown   Output Parameter:
1315552f7358SJed Brown 
1316552f7358SJed Brown   Note:
1317552f7358SJed Brown   This should be called after DMPlexSetChart().
1318552f7358SJed Brown 
1319552f7358SJed Brown   Level: beginner
1320552f7358SJed Brown 
1321552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1322552f7358SJed Brown @*/
1323552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1324552f7358SJed Brown {
1325552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1326552f7358SJed Brown   PetscErrorCode ierr;
1327552f7358SJed Brown 
1328552f7358SJed Brown   PetscFunctionBegin;
1329552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1330552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
13310d644c17SKarl Rupp 
1332552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1333552f7358SJed Brown   PetscFunctionReturn(0);
1334552f7358SJed Brown }
1335552f7358SJed Brown 
1336f5a469b9SMatthew G. Knepley /*@
1337eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1338f5a469b9SMatthew G. Knepley 
1339f5a469b9SMatthew G. Knepley   Not collective
1340f5a469b9SMatthew G. Knepley 
1341f5a469b9SMatthew G. Knepley   Input Parameters:
1342f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1343eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1344f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1345f5a469b9SMatthew G. Knepley 
1346f5a469b9SMatthew G. Knepley   Output Parameter:
1347f5a469b9SMatthew G. Knepley 
1348f5a469b9SMatthew G. Knepley   Note:
1349f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1350f5a469b9SMatthew G. Knepley 
1351f5a469b9SMatthew G. Knepley   Level: beginner
1352f5a469b9SMatthew G. Knepley 
1353f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1354f5a469b9SMatthew G. Knepley @*/
1355f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1356f5a469b9SMatthew G. Knepley {
1357f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1358f5a469b9SMatthew G. Knepley   PetscInt       csize;
1359f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1360f5a469b9SMatthew G. Knepley 
1361f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1362f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1363f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1364f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1365f5a469b9SMatthew G. Knepley 
1366f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1367f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1368f5a469b9SMatthew G. Knepley }
1369f5a469b9SMatthew G. Knepley 
1370552f7358SJed Brown /*@C
1371eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1372552f7358SJed Brown 
1373552f7358SJed Brown   Not collective
1374552f7358SJed Brown 
1375552f7358SJed Brown   Input Parameters:
1376552f7358SJed Brown + mesh - The DMPlex
1377eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1378552f7358SJed Brown 
1379552f7358SJed Brown   Output Parameter:
1380552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1381552f7358SJed Brown 
1382552f7358SJed Brown   Level: beginner
1383552f7358SJed Brown 
13843813dfbdSMatthew G Knepley   Fortran Notes:
13853813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13863813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13873813dfbdSMatthew G Knepley 
13883813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1389552f7358SJed Brown 
1390552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1391552f7358SJed Brown @*/
1392552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1393552f7358SJed Brown {
1394552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1395552f7358SJed Brown   PetscInt       off;
1396552f7358SJed Brown   PetscErrorCode ierr;
1397552f7358SJed Brown 
1398552f7358SJed Brown   PetscFunctionBegin;
1399552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1400552f7358SJed Brown   PetscValidPointer(cone, 3);
1401552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1402552f7358SJed Brown   *cone = &mesh->cones[off];
1403552f7358SJed Brown   PetscFunctionReturn(0);
1404552f7358SJed Brown }
1405552f7358SJed Brown 
1406552f7358SJed Brown /*@
140792371b87SBarry Smith   DMPlexSetCone - Set the points on the in-edges for this point in the DAG; that is these are the points that cover the specific point
1408552f7358SJed Brown 
1409552f7358SJed Brown   Not collective
1410552f7358SJed Brown 
1411552f7358SJed Brown   Input Parameters:
1412552f7358SJed Brown + mesh - The DMPlex
1413eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1414552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1415552f7358SJed Brown 
1416552f7358SJed Brown   Output Parameter:
1417552f7358SJed Brown 
1418552f7358SJed Brown   Note:
1419552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1420552f7358SJed Brown 
142192371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
142292371b87SBarry Smith 
1423552f7358SJed Brown   Level: beginner
1424552f7358SJed Brown 
142592371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1426552f7358SJed Brown @*/
1427552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1428552f7358SJed Brown {
1429552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1430552f7358SJed Brown   PetscInt       pStart, pEnd;
1431552f7358SJed Brown   PetscInt       dof, off, c;
1432552f7358SJed Brown   PetscErrorCode ierr;
1433552f7358SJed Brown 
1434552f7358SJed Brown   PetscFunctionBegin;
1435552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1436552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1437552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1438552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1439552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
144082f516ccSBarry 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);
1441552f7358SJed Brown   for (c = 0; c < dof; ++c) {
144282f516ccSBarry 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);
1443552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1444552f7358SJed Brown   }
1445552f7358SJed Brown   PetscFunctionReturn(0);
1446552f7358SJed Brown }
1447552f7358SJed Brown 
1448552f7358SJed Brown /*@C
1449eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1450552f7358SJed Brown 
1451552f7358SJed Brown   Not collective
1452552f7358SJed Brown 
1453552f7358SJed Brown   Input Parameters:
1454552f7358SJed Brown + mesh - The DMPlex
1455eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1456552f7358SJed Brown 
1457552f7358SJed Brown   Output Parameter:
1458552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1459552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1460552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1461552f7358SJed Brown                     the index of the cone point on which to start.
1462552f7358SJed Brown 
1463552f7358SJed Brown   Level: beginner
1464552f7358SJed Brown 
14653813dfbdSMatthew G Knepley   Fortran Notes:
14663813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14673813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14683813dfbdSMatthew G Knepley 
14693813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1470552f7358SJed Brown 
1471552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1472552f7358SJed Brown @*/
1473552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1474552f7358SJed Brown {
1475552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1476552f7358SJed Brown   PetscInt       off;
1477552f7358SJed Brown   PetscErrorCode ierr;
1478552f7358SJed Brown 
1479552f7358SJed Brown   PetscFunctionBegin;
1480552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1481552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1482552f7358SJed Brown   {
1483552f7358SJed Brown     PetscInt dof;
1484552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1485552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1486552f7358SJed Brown   }
1487552f7358SJed Brown #endif
1488552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
14890d644c17SKarl Rupp 
1490552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1491552f7358SJed Brown   PetscFunctionReturn(0);
1492552f7358SJed Brown }
1493552f7358SJed Brown 
1494552f7358SJed Brown /*@
1495eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1496552f7358SJed Brown 
1497552f7358SJed Brown   Not collective
1498552f7358SJed Brown 
1499552f7358SJed Brown   Input Parameters:
1500552f7358SJed Brown + mesh - The DMPlex
1501eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1502552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1503552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1504552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1505552f7358SJed Brown                     the index of the cone point on which to start.
1506552f7358SJed Brown 
1507552f7358SJed Brown   Output Parameter:
1508552f7358SJed Brown 
1509552f7358SJed Brown   Note:
1510552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1511552f7358SJed Brown 
1512552f7358SJed Brown   Level: beginner
1513552f7358SJed Brown 
1514552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1515552f7358SJed Brown @*/
1516552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1517552f7358SJed Brown {
1518552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1519552f7358SJed Brown   PetscInt       pStart, pEnd;
1520552f7358SJed Brown   PetscInt       dof, off, c;
1521552f7358SJed Brown   PetscErrorCode ierr;
1522552f7358SJed Brown 
1523552f7358SJed Brown   PetscFunctionBegin;
1524552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1525552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1526552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1527552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1528552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
152982f516ccSBarry 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);
1530552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1531552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1532552f7358SJed Brown 
1533552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
153482f516ccSBarry 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);
1535552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1536552f7358SJed Brown   }
1537552f7358SJed Brown   PetscFunctionReturn(0);
1538552f7358SJed Brown }
1539552f7358SJed Brown 
15407cd05799SMatthew G. Knepley /*@
1541eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
15427cd05799SMatthew G. Knepley 
15437cd05799SMatthew G. Knepley   Not collective
15447cd05799SMatthew G. Knepley 
15457cd05799SMatthew G. Knepley   Input Parameters:
15467cd05799SMatthew G. Knepley + mesh - The DMPlex
1547eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15487cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15497cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
15507cd05799SMatthew G. Knepley 
15517cd05799SMatthew G. Knepley   Level: beginner
15527cd05799SMatthew G. Knepley 
15537cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15547cd05799SMatthew G. Knepley @*/
1555552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1556552f7358SJed Brown {
1557552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1558552f7358SJed Brown   PetscInt       pStart, pEnd;
1559552f7358SJed Brown   PetscInt       dof, off;
1560552f7358SJed Brown   PetscErrorCode ierr;
1561552f7358SJed Brown 
1562552f7358SJed Brown   PetscFunctionBegin;
1563552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1564552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
156582f516ccSBarry 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);
156682f516ccSBarry 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);
156777c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
156877c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
156977c88f5bSMatthew 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);
1570552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1571552f7358SJed Brown   PetscFunctionReturn(0);
1572552f7358SJed Brown }
1573552f7358SJed Brown 
15747cd05799SMatthew G. Knepley /*@
1575eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
15767cd05799SMatthew G. Knepley 
15777cd05799SMatthew G. Knepley   Not collective
15787cd05799SMatthew G. Knepley 
15797cd05799SMatthew G. Knepley   Input Parameters:
15807cd05799SMatthew G. Knepley + mesh - The DMPlex
1581eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
15827cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15837cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
15847cd05799SMatthew G. Knepley 
15857cd05799SMatthew G. Knepley   Level: beginner
15867cd05799SMatthew G. Knepley 
15877cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15887cd05799SMatthew G. Knepley @*/
158977c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
159077c88f5bSMatthew G Knepley {
159177c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
159277c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
159377c88f5bSMatthew G Knepley   PetscInt       dof, off;
159477c88f5bSMatthew G Knepley   PetscErrorCode ierr;
159577c88f5bSMatthew G Knepley 
159677c88f5bSMatthew G Knepley   PetscFunctionBegin;
159777c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
159877c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
159977c88f5bSMatthew 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);
160077c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
160177c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
160277c88f5bSMatthew 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);
160377c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
160477c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
160577c88f5bSMatthew G Knepley }
160677c88f5bSMatthew G Knepley 
1607552f7358SJed Brown /*@
1608eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
1609552f7358SJed Brown 
1610552f7358SJed Brown   Not collective
1611552f7358SJed Brown 
1612552f7358SJed Brown   Input Parameters:
1613552f7358SJed Brown + mesh - The DMPlex
1614eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1615552f7358SJed Brown 
1616552f7358SJed Brown   Output Parameter:
1617552f7358SJed Brown . size - The support size for point p
1618552f7358SJed Brown 
1619552f7358SJed Brown   Level: beginner
1620552f7358SJed Brown 
1621552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1622552f7358SJed Brown @*/
1623552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1624552f7358SJed Brown {
1625552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1626552f7358SJed Brown   PetscErrorCode ierr;
1627552f7358SJed Brown 
1628552f7358SJed Brown   PetscFunctionBegin;
1629552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1630552f7358SJed Brown   PetscValidPointer(size, 3);
1631552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1632552f7358SJed Brown   PetscFunctionReturn(0);
1633552f7358SJed Brown }
1634552f7358SJed Brown 
1635552f7358SJed Brown /*@
1636eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
1637552f7358SJed Brown 
1638552f7358SJed Brown   Not collective
1639552f7358SJed Brown 
1640552f7358SJed Brown   Input Parameters:
1641552f7358SJed Brown + mesh - The DMPlex
1642eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1643552f7358SJed Brown - size - The support size for point p
1644552f7358SJed Brown 
1645552f7358SJed Brown   Output Parameter:
1646552f7358SJed Brown 
1647552f7358SJed Brown   Note:
1648552f7358SJed Brown   This should be called after DMPlexSetChart().
1649552f7358SJed Brown 
1650552f7358SJed Brown   Level: beginner
1651552f7358SJed Brown 
1652552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1653552f7358SJed Brown @*/
1654552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1655552f7358SJed Brown {
1656552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1657552f7358SJed Brown   PetscErrorCode ierr;
1658552f7358SJed Brown 
1659552f7358SJed Brown   PetscFunctionBegin;
1660552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1661552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
16620d644c17SKarl Rupp 
1663552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1664552f7358SJed Brown   PetscFunctionReturn(0);
1665552f7358SJed Brown }
1666552f7358SJed Brown 
1667552f7358SJed Brown /*@C
1668eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
1669552f7358SJed Brown 
1670552f7358SJed Brown   Not collective
1671552f7358SJed Brown 
1672552f7358SJed Brown   Input Parameters:
1673552f7358SJed Brown + mesh - The DMPlex
1674eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1675552f7358SJed Brown 
1676552f7358SJed Brown   Output Parameter:
1677552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1678552f7358SJed Brown 
1679552f7358SJed Brown   Level: beginner
1680552f7358SJed Brown 
16813813dfbdSMatthew G Knepley   Fortran Notes:
16823813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16833813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16843813dfbdSMatthew G Knepley 
16853813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
16863813dfbdSMatthew G Knepley 
1687552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1688552f7358SJed Brown @*/
1689552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1690552f7358SJed Brown {
1691552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1692552f7358SJed Brown   PetscInt       off;
1693552f7358SJed Brown   PetscErrorCode ierr;
1694552f7358SJed Brown 
1695552f7358SJed Brown   PetscFunctionBegin;
1696552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1697552f7358SJed Brown   PetscValidPointer(support, 3);
1698552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1699552f7358SJed Brown   *support = &mesh->supports[off];
1700552f7358SJed Brown   PetscFunctionReturn(0);
1701552f7358SJed Brown }
1702552f7358SJed Brown 
1703552f7358SJed Brown /*@
170492371b87SBarry Smith   DMPlexSetSupport - Set the points on the out-edges for this point in the DAG, that is the list of points that this point covers
1705552f7358SJed Brown 
1706552f7358SJed Brown   Not collective
1707552f7358SJed Brown 
1708552f7358SJed Brown   Input Parameters:
1709552f7358SJed Brown + mesh - The DMPlex
1710eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
171192371b87SBarry Smith - support - An array of points which are on the out-edges for point p
1712552f7358SJed Brown 
1713552f7358SJed Brown   Output Parameter:
1714552f7358SJed Brown 
1715552f7358SJed Brown   Note:
1716552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1717552f7358SJed Brown 
1718552f7358SJed Brown   Level: beginner
1719552f7358SJed Brown 
172092371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1721552f7358SJed Brown @*/
1722552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1723552f7358SJed Brown {
1724552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1725552f7358SJed Brown   PetscInt       pStart, pEnd;
1726552f7358SJed Brown   PetscInt       dof, off, c;
1727552f7358SJed Brown   PetscErrorCode ierr;
1728552f7358SJed Brown 
1729552f7358SJed Brown   PetscFunctionBegin;
1730552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1731552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1732552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1733552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1734552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
173582f516ccSBarry 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);
1736552f7358SJed Brown   for (c = 0; c < dof; ++c) {
173782f516ccSBarry 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);
1738552f7358SJed Brown     mesh->supports[off+c] = support[c];
1739552f7358SJed Brown   }
1740552f7358SJed Brown   PetscFunctionReturn(0);
1741552f7358SJed Brown }
1742552f7358SJed Brown 
17437cd05799SMatthew G. Knepley /*@
1744eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
17457cd05799SMatthew G. Knepley 
17467cd05799SMatthew G. Knepley   Not collective
17477cd05799SMatthew G. Knepley 
17487cd05799SMatthew G. Knepley   Input Parameters:
17497cd05799SMatthew G. Knepley + mesh - The DMPlex
1750eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
17517cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
17527cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
17537cd05799SMatthew G. Knepley 
17547cd05799SMatthew G. Knepley   Level: beginner
17557cd05799SMatthew G. Knepley 
17567cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17577cd05799SMatthew G. Knepley @*/
1758552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1759552f7358SJed Brown {
1760552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1761552f7358SJed Brown   PetscInt       pStart, pEnd;
1762552f7358SJed Brown   PetscInt       dof, off;
1763552f7358SJed Brown   PetscErrorCode ierr;
1764552f7358SJed Brown 
1765552f7358SJed Brown   PetscFunctionBegin;
1766552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1767552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1768552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1769552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
177082f516ccSBarry 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);
177182f516ccSBarry 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);
177282f516ccSBarry 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);
1773552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1774552f7358SJed Brown   PetscFunctionReturn(0);
1775552f7358SJed Brown }
1776552f7358SJed Brown 
1777552f7358SJed Brown /*@C
1778eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
1779552f7358SJed Brown 
1780552f7358SJed Brown   Not collective
1781552f7358SJed Brown 
1782552f7358SJed Brown   Input Parameters:
1783552f7358SJed Brown + mesh - The DMPlex
1784eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1785552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17860298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1787552f7358SJed Brown 
1788552f7358SJed Brown   Output Parameters:
1789552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1790552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1791552f7358SJed Brown 
1792552f7358SJed Brown   Note:
17930298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1794552f7358SJed Brown 
17953813dfbdSMatthew G Knepley   Fortran Notes:
17963813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17973813dfbdSMatthew G Knepley   include petsc.h90 in your code.
17983813dfbdSMatthew G Knepley 
17993813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
18003813dfbdSMatthew G Knepley 
1801552f7358SJed Brown   Level: beginner
1802552f7358SJed Brown 
1803552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1804552f7358SJed Brown @*/
1805552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1806552f7358SJed Brown {
1807552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1808552f7358SJed Brown   PetscInt       *closure, *fifo;
18090298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1810552f7358SJed Brown   PetscInt        tmpSize, t;
1811552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1812552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1813552f7358SJed Brown   PetscErrorCode  ierr;
1814552f7358SJed Brown 
1815552f7358SJed Brown   PetscFunctionBegin;
1816552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1817552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1818552f7358SJed Brown   /* This is only 1-level */
1819552f7358SJed Brown   if (useCone) {
1820552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1821552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1822552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1823552f7358SJed Brown   } else {
1824552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1825552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1826552f7358SJed Brown   }
1827bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1828bfbcdd7aSMatthew G. Knepley     if (*points) {
1829bfbcdd7aSMatthew G. Knepley       closure = *points;
1830bfbcdd7aSMatthew G. Knepley     } else {
1831bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
183269291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1833bfbcdd7aSMatthew G. Knepley     }
1834bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1835bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1836bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1837bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1838bfbcdd7aSMatthew G. Knepley     }
1839bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1840bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1841bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1842bfbcdd7aSMatthew G. Knepley   }
184324c766afSToby Isaac   {
184424c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
184524c766afSToby Isaac 
184624c766afSToby Isaac     c = mesh->maxConeSize;
184724c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
184824c766afSToby Isaac     s = mesh->maxSupportSize;
184924c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
185024c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
185124c766afSToby Isaac   }
185269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1853bfbcdd7aSMatthew G. Knepley   if (*points) {
1854bfbcdd7aSMatthew G. Knepley     closure = *points;
1855bfbcdd7aSMatthew G. Knepley   } else {
185669291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
1857bfbcdd7aSMatthew G. Knepley   }
1858bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1859552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1860552f7358SJed Brown     const PetscInt cp = tmp[t];
1861552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1862552f7358SJed Brown 
1863552f7358SJed Brown     closure[closureSize]   = cp;
1864552f7358SJed Brown     closure[closureSize+1] = co;
1865552f7358SJed Brown     fifo[fifoSize]         = cp;
1866552f7358SJed Brown     fifo[fifoSize+1]       = co;
1867552f7358SJed Brown   }
1868bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1869552f7358SJed Brown   while (fifoSize - fifoStart) {
1870552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1871552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1872552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1873552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1874552f7358SJed Brown 
1875552f7358SJed Brown     if (useCone) {
1876552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1877552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1878552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1879552f7358SJed Brown     } else {
1880552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1881552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18820298fd71SBarry Smith       tmpO = NULL;
1883552f7358SJed Brown     }
1884552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1885552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1886552f7358SJed Brown       const PetscInt cp = tmp[i];
18872e1b13c2SMatthew 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. */
18882e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18892e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18902e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1891552f7358SJed Brown       PetscInt       c;
1892552f7358SJed Brown 
18932e1b13c2SMatthew G. Knepley       if (rev) {
18942e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
18952e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18962e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18972e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18982e1b13c2SMatthew G. Knepley       }
1899552f7358SJed Brown       /* Check for duplicate */
1900552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1901552f7358SJed Brown         if (closure[c] == cp) break;
1902552f7358SJed Brown       }
1903552f7358SJed Brown       if (c == closureSize) {
1904552f7358SJed Brown         closure[closureSize]   = cp;
1905552f7358SJed Brown         closure[closureSize+1] = co;
1906552f7358SJed Brown         fifo[fifoSize]         = cp;
1907552f7358SJed Brown         fifo[fifoSize+1]       = co;
1908552f7358SJed Brown         closureSize           += 2;
1909552f7358SJed Brown         fifoSize              += 2;
1910552f7358SJed Brown       }
1911552f7358SJed Brown     }
1912552f7358SJed Brown     fifoStart += 2;
1913552f7358SJed Brown   }
1914552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1915552f7358SJed Brown   if (points)    *points    = closure;
191669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
1917552f7358SJed Brown   PetscFunctionReturn(0);
1918552f7358SJed Brown }
1919552f7358SJed Brown 
19209bf0dad6SMatthew G. Knepley /*@C
1921eaf898f9SPatrick 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
19229bf0dad6SMatthew G. Knepley 
19239bf0dad6SMatthew G. Knepley   Not collective
19249bf0dad6SMatthew G. Knepley 
19259bf0dad6SMatthew G. Knepley   Input Parameters:
19269bf0dad6SMatthew G. Knepley + mesh - The DMPlex
1927eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
19289bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
19299bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
19309bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
19319bf0dad6SMatthew G. Knepley 
19329bf0dad6SMatthew G. Knepley   Output Parameters:
19339bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
19349bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
19359bf0dad6SMatthew G. Knepley 
19369bf0dad6SMatthew G. Knepley   Note:
19379bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
19389bf0dad6SMatthew G. Knepley 
19399bf0dad6SMatthew G. Knepley   Fortran Notes:
19409bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19419bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
19429bf0dad6SMatthew G. Knepley 
19439bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19449bf0dad6SMatthew G. Knepley 
19459bf0dad6SMatthew G. Knepley   Level: beginner
19469bf0dad6SMatthew G. Knepley 
19479bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
19489bf0dad6SMatthew G. Knepley @*/
19499bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
19509bf0dad6SMatthew G. Knepley {
19519bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
19529bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
19539bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
19549bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
19559bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
19569bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
19579bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
19589bf0dad6SMatthew G. Knepley 
19599bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
19609bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19619bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
19629bf0dad6SMatthew G. Knepley   /* This is only 1-level */
19639bf0dad6SMatthew G. Knepley   if (useCone) {
19649bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
19659bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
19669bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
19679bf0dad6SMatthew G. Knepley   } else {
19689bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
19699bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
19709bf0dad6SMatthew G. Knepley   }
19719bf0dad6SMatthew G. Knepley   if (depth == 1) {
19729bf0dad6SMatthew G. Knepley     if (*points) {
19739bf0dad6SMatthew G. Knepley       closure = *points;
19749bf0dad6SMatthew G. Knepley     } else {
19759bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
197669291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
19779bf0dad6SMatthew G. Knepley     }
19789bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
19799bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
19809bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19819bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
19829bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
19839bf0dad6SMatthew G. Knepley     }
19849bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
19859bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
19869bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
19879bf0dad6SMatthew G. Knepley   }
198824c766afSToby Isaac   {
198924c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
199024c766afSToby Isaac 
199124c766afSToby Isaac     c = mesh->maxConeSize;
199224c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
199324c766afSToby Isaac     s = mesh->maxSupportSize;
199424c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
199524c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
199624c766afSToby Isaac   }
199769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
19989bf0dad6SMatthew G. Knepley   if (*points) {
19999bf0dad6SMatthew G. Knepley     closure = *points;
20009bf0dad6SMatthew G. Knepley   } else {
200169291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
20029bf0dad6SMatthew G. Knepley   }
20039bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
20049bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
20059bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
20069bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
20079bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
20089bf0dad6SMatthew G. Knepley 
20099bf0dad6SMatthew G. Knepley     if (ornt < 0) {
20109bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
20119bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
201286b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
20139bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
20149bf0dad6SMatthew G. Knepley     }
20159bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
20169bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
20179bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
20189bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
20199bf0dad6SMatthew G. Knepley   }
20209bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
20219bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
20229bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
20239bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
20249bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
20259bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
20269bf0dad6SMatthew G. Knepley 
20279bf0dad6SMatthew G. Knepley     if (useCone) {
20289bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
20299bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
20309bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
20319bf0dad6SMatthew G. Knepley     } else {
20329bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
20339bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
20349bf0dad6SMatthew G. Knepley       tmpO = NULL;
20359bf0dad6SMatthew G. Knepley     }
20369bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
20379bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
20389bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
20399bf0dad6SMatthew 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. */
20409bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
20419bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
20429bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
20439bf0dad6SMatthew G. Knepley       PetscInt       c;
20449bf0dad6SMatthew G. Knepley 
20459bf0dad6SMatthew G. Knepley       if (rev) {
20469bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
20479bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
20489bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
20499bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
20509bf0dad6SMatthew G. Knepley       }
20519bf0dad6SMatthew G. Knepley       /* Check for duplicate */
20529bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
20539bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
20549bf0dad6SMatthew G. Knepley       }
20559bf0dad6SMatthew G. Knepley       if (c == closureSize) {
20569bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
20579bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
20589bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
20599bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
20609bf0dad6SMatthew G. Knepley         closureSize           += 2;
20619bf0dad6SMatthew G. Knepley         fifoSize              += 2;
20629bf0dad6SMatthew G. Knepley       }
20639bf0dad6SMatthew G. Knepley     }
20649bf0dad6SMatthew G. Knepley     fifoStart += 2;
20659bf0dad6SMatthew G. Knepley   }
20669bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
20679bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
206869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
20699bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
20709bf0dad6SMatthew G. Knepley }
20719bf0dad6SMatthew G. Knepley 
2072552f7358SJed Brown /*@C
2073eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2074552f7358SJed Brown 
2075552f7358SJed Brown   Not collective
2076552f7358SJed Brown 
2077552f7358SJed Brown   Input Parameters:
2078552f7358SJed Brown + mesh - The DMPlex
2079eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2080552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2081e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2082e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2083552f7358SJed Brown 
2084552f7358SJed Brown   Note:
20850298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2086552f7358SJed Brown 
20873813dfbdSMatthew G Knepley   Fortran Notes:
20883813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20893813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20903813dfbdSMatthew G Knepley 
20913813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
20923813dfbdSMatthew G Knepley 
2093552f7358SJed Brown   Level: beginner
2094552f7358SJed Brown 
2095552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2096552f7358SJed Brown @*/
2097552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2098552f7358SJed Brown {
2099552f7358SJed Brown   PetscErrorCode ierr;
2100552f7358SJed Brown 
2101552f7358SJed Brown   PetscFunctionBegin;
2102552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2103e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2104e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
210569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
21064ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2107552f7358SJed Brown   PetscFunctionReturn(0);
2108552f7358SJed Brown }
2109552f7358SJed Brown 
2110552f7358SJed Brown /*@
2111eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2112552f7358SJed Brown 
2113552f7358SJed Brown   Not collective
2114552f7358SJed Brown 
2115552f7358SJed Brown   Input Parameter:
2116552f7358SJed Brown . mesh - The DMPlex
2117552f7358SJed Brown 
2118552f7358SJed Brown   Output Parameters:
2119552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2120552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2121552f7358SJed Brown 
2122552f7358SJed Brown   Level: beginner
2123552f7358SJed Brown 
2124552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2125552f7358SJed Brown @*/
2126552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2127552f7358SJed Brown {
2128552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2129552f7358SJed Brown 
2130552f7358SJed Brown   PetscFunctionBegin;
2131552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2132552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2133552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2134552f7358SJed Brown   PetscFunctionReturn(0);
2135552f7358SJed Brown }
2136552f7358SJed Brown 
2137552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2138552f7358SJed Brown {
2139552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2140552f7358SJed Brown   PetscInt       size;
2141552f7358SJed Brown   PetscErrorCode ierr;
2142552f7358SJed Brown 
2143552f7358SJed Brown   PetscFunctionBegin;
2144552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2145552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2146552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
21471795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
21481795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2149552f7358SJed Brown   if (mesh->maxSupportSize) {
2150552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2151552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
21521795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2153552f7358SJed Brown   }
2154552f7358SJed Brown   PetscFunctionReturn(0);
2155552f7358SJed Brown }
2156552f7358SJed Brown 
2157276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2158552f7358SJed Brown {
2159552f7358SJed Brown   PetscErrorCode ierr;
2160552f7358SJed Brown 
2161552f7358SJed Brown   PetscFunctionBegin;
21624d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
21634d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2164c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2165736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2166f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2167f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2168f94b4a02SBlaise Bourdin 
21693dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
21703dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
2171f94b4a02SBlaise Bourdin     ierr = DMGetDefaultSection((*subdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
2172f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2173f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2174f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2175f94b4a02SBlaise Bourdin 
2176f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2177c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2178f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2179f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2180f94b4a02SBlaise Bourdin   }
2181552f7358SJed Brown   PetscFunctionReturn(0);
2182552f7358SJed Brown }
2183552f7358SJed Brown 
21842adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
21852adcc780SMatthew G. Knepley {
21862adcc780SMatthew G. Knepley   PetscErrorCode ierr;
21873dcd263cSBlaise Bourdin   PetscInt       i = 0;
21882adcc780SMatthew G. Knepley 
21892adcc780SMatthew G. Knepley   PetscFunctionBegin;
21902adcc780SMatthew G. Knepley   if (superdm) {ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);}
21912adcc780SMatthew G. Knepley   ierr = DMCreateSuperDM_Section_Private(dms, len, is, superdm);CHKERRQ(ierr);
2192c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
21933dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
21943dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
21953dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
21963dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
21973dcd263cSBlaise Bourdin 
21983dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
21993dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2200c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
22013dcd263cSBlaise Bourdin       ierr = DMGetDefaultSection((*superdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
22023dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
22033dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
22043dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
22053dcd263cSBlaise Bourdin 
22063dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2207c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
22083dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
22093dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
22103dcd263cSBlaise Bourdin       break;
22113dcd263cSBlaise Bourdin     }
22123dcd263cSBlaise Bourdin   }
22132adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
22142adcc780SMatthew G. Knepley }
22152adcc780SMatthew G. Knepley 
2216552f7358SJed Brown /*@
2217eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2218552f7358SJed Brown 
2219552f7358SJed Brown   Not collective
2220552f7358SJed Brown 
2221552f7358SJed Brown   Input Parameter:
2222552f7358SJed Brown . mesh - The DMPlex
2223552f7358SJed Brown 
2224552f7358SJed Brown   Output Parameter:
2225552f7358SJed Brown 
2226552f7358SJed Brown   Note:
2227552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2228552f7358SJed Brown 
2229552f7358SJed Brown   Level: beginner
2230552f7358SJed Brown 
2231552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2232552f7358SJed Brown @*/
2233552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2234552f7358SJed Brown {
2235552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2236552f7358SJed Brown   PetscInt      *offsets;
2237552f7358SJed Brown   PetscInt       supportSize;
2238552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2239552f7358SJed Brown   PetscErrorCode ierr;
2240552f7358SJed Brown 
2241552f7358SJed Brown   PetscFunctionBegin;
2242552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
224382f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2244552f7358SJed Brown   /* Calculate support sizes */
2245552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2246552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2247552f7358SJed Brown     PetscInt dof, off, c;
2248552f7358SJed Brown 
2249552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2250552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2251552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2252552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2253552f7358SJed Brown     }
2254552f7358SJed Brown   }
2255552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2256552f7358SJed Brown     PetscInt dof;
2257552f7358SJed Brown 
2258552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
22590d644c17SKarl Rupp 
2260552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2261552f7358SJed Brown   }
2262552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2263552f7358SJed Brown   /* Calculate supports */
2264552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
22651795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
22661795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2267552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2268552f7358SJed Brown     PetscInt dof, off, c;
2269552f7358SJed Brown 
2270552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2271552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2272552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2273552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2274552f7358SJed Brown       PetscInt       offS;
2275552f7358SJed Brown 
2276552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
22770d644c17SKarl Rupp 
2278552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2279552f7358SJed Brown       ++offsets[q];
2280552f7358SJed Brown     }
2281552f7358SJed Brown   }
2282552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2283552f7358SJed Brown   PetscFunctionReturn(0);
2284552f7358SJed Brown }
2285552f7358SJed Brown 
2286*7d5acc75SStefano Zampini static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);
2287*7d5acc75SStefano Zampini 
2288552f7358SJed Brown /*@
2289eaf898f9SPatrick Sanan   DMPlexStratify - The DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2290eaf898f9SPatrick Sanan   can be illustrated by a Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2291552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2292552f7358SJed Brown   the DAG.
2293552f7358SJed Brown 
2294bf4602e4SToby Isaac   Collective on dm
2295552f7358SJed Brown 
2296552f7358SJed Brown   Input Parameter:
2297552f7358SJed Brown . mesh - The DMPlex
2298552f7358SJed Brown 
2299552f7358SJed Brown   Output Parameter:
2300552f7358SJed Brown 
2301552f7358SJed Brown   Notes:
2302150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2303150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2304c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2305150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2306552f7358SJed Brown 
2307150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2308552f7358SJed Brown 
2309552f7358SJed Brown   Level: beginner
2310552f7358SJed Brown 
2311552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2312552f7358SJed Brown @*/
2313552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2314552f7358SJed Brown {
2315df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2316aa50250dSMatthew G. Knepley   DMLabel        label;
2317552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2318552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2319*7d5acc75SStefano Zampini   PetscInt       cMax, fMax, eMax, vMax;
2320552f7358SJed Brown   PetscErrorCode ierr;
2321552f7358SJed Brown 
2322552f7358SJed Brown   PetscFunctionBegin;
2323552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2324552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2325552f7358SJed Brown   /* Calculate depth */
2326aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2327c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2328aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2329552f7358SJed Brown   /* Initialize roots and count leaves */
2330552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2331552f7358SJed Brown     PetscInt coneSize, supportSize;
2332552f7358SJed Brown 
2333552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2334552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2335552f7358SJed Brown     if (!coneSize && supportSize) {
2336552f7358SJed Brown       ++numRoots;
2337aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2338552f7358SJed Brown     } else if (!supportSize && coneSize) {
2339552f7358SJed Brown       ++numLeaves;
2340552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2341552f7358SJed Brown       /* Isolated points */
2342aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2343552f7358SJed Brown     }
2344552f7358SJed Brown   }
2345552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2346552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2347552f7358SJed Brown       PetscInt coneSize, supportSize;
2348552f7358SJed Brown 
2349552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2350552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2351552f7358SJed Brown       if (!supportSize && coneSize) {
2352aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2353552f7358SJed Brown       }
2354552f7358SJed Brown     }
2355552f7358SJed Brown   } else {
235674ef644bSMatthew G. Knepley     IS       pointIS;
235774ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2358552f7358SJed Brown 
235974ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
236074ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
236174ef644bSMatthew G. Knepley     while (numPoints) {
236274ef644bSMatthew G. Knepley       const PetscInt *points;
236374ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
236474ef644bSMatthew G. Knepley 
236574ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
236674ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
236774ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
236874ef644bSMatthew G. Knepley         const PetscInt *support;
236974ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
237074ef644bSMatthew G. Knepley 
237174ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
237274ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
237374ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
237474ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2375552f7358SJed Brown         }
2376552f7358SJed Brown       }
23775edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
237874ef644bSMatthew G. Knepley       ++level;
237974ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
238074ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
238174ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
238274ef644bSMatthew G. Knepley       else         {numPoints = 0;}
238374ef644bSMatthew G. Knepley     }
238474ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
238574ef644bSMatthew G. Knepley   }
2386bf4602e4SToby Isaac   { /* just in case there is an empty process */
2387bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2388bf4602e4SToby Isaac 
2389bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
23904de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
23914de306b1SToby Isaac       IS pointIS;
23924de306b1SToby Isaac 
23934de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
23944de306b1SToby Isaac       if (pointIS) {
23954de306b1SToby Isaac         PetscInt  min, max, numPoints;
23964de306b1SToby Isaac         PetscInt  start;
23974de306b1SToby Isaac         PetscBool contig;
23984de306b1SToby Isaac 
23994de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
24004de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
24014de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
24024de306b1SToby Isaac         if (start == 0 && contig) {
24034de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
24044de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
24054de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
24064de306b1SToby Isaac         }
24074de306b1SToby Isaac       }
24084de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
24094de306b1SToby Isaac     }
24106d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2411bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2412bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2413bf4602e4SToby Isaac     }
2414bf4602e4SToby Isaac   }
2415df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2416*7d5acc75SStefano Zampini 
2417*7d5acc75SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
2418*7d5acc75SStefano Zampini   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
2419*7d5acc75SStefano Zampini     DMLabel  dimLabel;
2420*7d5acc75SStefano Zampini     PetscInt dim;
2421*7d5acc75SStefano Zampini 
2422*7d5acc75SStefano Zampini     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2423*7d5acc75SStefano Zampini     ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
2424*7d5acc75SStefano Zampini     if (!dimLabel) {
2425*7d5acc75SStefano Zampini       ierr = DMCreateLabel(dm, "dim");CHKERRQ(ierr);
2426*7d5acc75SStefano Zampini       ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
2427*7d5acc75SStefano Zampini     }
2428*7d5acc75SStefano Zampini     if (cMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);CHKERRQ(ierr);}
2429*7d5acc75SStefano Zampini     if (fMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);CHKERRQ(ierr);}
2430*7d5acc75SStefano Zampini     if (eMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);CHKERRQ(ierr);}
2431*7d5acc75SStefano Zampini     if (vMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);CHKERRQ(ierr);}
2432*7d5acc75SStefano Zampini   }
2433552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2434552f7358SJed Brown   PetscFunctionReturn(0);
2435552f7358SJed Brown }
2436552f7358SJed Brown 
2437552f7358SJed Brown /*@C
2438552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2439552f7358SJed Brown 
2440552f7358SJed Brown   Not Collective
2441552f7358SJed Brown 
2442552f7358SJed Brown   Input Parameters:
2443552f7358SJed Brown + dm - The DMPlex object
2444552f7358SJed Brown . numPoints - The number of input points for the join
2445552f7358SJed Brown - points - The input points
2446552f7358SJed Brown 
2447552f7358SJed Brown   Output Parameters:
2448552f7358SJed Brown + numCoveredPoints - The number of points in the join
2449552f7358SJed Brown - coveredPoints - The points in the join
2450552f7358SJed Brown 
2451552f7358SJed Brown   Level: intermediate
2452552f7358SJed Brown 
2453552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2454552f7358SJed Brown 
24553813dfbdSMatthew G Knepley   Fortran Notes:
24563813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24573813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24583813dfbdSMatthew G Knepley 
24593813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24603813dfbdSMatthew G Knepley 
2461552f7358SJed Brown .keywords: mesh
2462552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2463552f7358SJed Brown @*/
2464552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2465552f7358SJed Brown {
2466552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2467552f7358SJed Brown   PetscInt      *join[2];
2468552f7358SJed Brown   PetscInt       joinSize, i = 0;
2469552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2470552f7358SJed Brown   PetscErrorCode ierr;
2471552f7358SJed Brown 
2472552f7358SJed Brown   PetscFunctionBegin;
2473552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2474552f7358SJed Brown   PetscValidPointer(points, 2);
2475552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2476552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
247769291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
247869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2479552f7358SJed Brown   /* Copy in support of first point */
2480552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2481552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2482552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2483552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2484552f7358SJed Brown   }
2485552f7358SJed Brown   /* Check each successive support */
2486552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2487552f7358SJed Brown     PetscInt newJoinSize = 0;
2488552f7358SJed Brown 
2489552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2490552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2491552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2492552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2493552f7358SJed Brown 
2494552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2495552f7358SJed Brown         if (point == join[i][m]) {
2496552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2497552f7358SJed Brown           break;
2498552f7358SJed Brown         }
2499552f7358SJed Brown       }
2500552f7358SJed Brown     }
2501552f7358SJed Brown     joinSize = newJoinSize;
2502552f7358SJed Brown     i        = 1-i;
2503552f7358SJed Brown   }
2504552f7358SJed Brown   *numCoveredPoints = joinSize;
2505552f7358SJed Brown   *coveredPoints    = join[i];
250669291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2507552f7358SJed Brown   PetscFunctionReturn(0);
2508552f7358SJed Brown }
2509552f7358SJed Brown 
2510552f7358SJed Brown /*@C
2511552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2512552f7358SJed Brown 
2513552f7358SJed Brown   Not Collective
2514552f7358SJed Brown 
2515552f7358SJed Brown   Input Parameters:
2516552f7358SJed Brown + dm - The DMPlex object
2517552f7358SJed Brown . numPoints - The number of input points for the join
2518552f7358SJed Brown - points - The input points
2519552f7358SJed Brown 
2520552f7358SJed Brown   Output Parameters:
2521552f7358SJed Brown + numCoveredPoints - The number of points in the join
2522552f7358SJed Brown - coveredPoints - The points in the join
2523552f7358SJed Brown 
25243813dfbdSMatthew G Knepley   Fortran Notes:
25253813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25263813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25273813dfbdSMatthew G Knepley 
25283813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25293813dfbdSMatthew G Knepley 
2530552f7358SJed Brown   Level: intermediate
2531552f7358SJed Brown 
2532552f7358SJed Brown .keywords: mesh
2533552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2534552f7358SJed Brown @*/
2535552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2536552f7358SJed Brown {
2537552f7358SJed Brown   PetscErrorCode ierr;
2538552f7358SJed Brown 
2539552f7358SJed Brown   PetscFunctionBegin;
2540552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2541d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2542d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2543d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
254469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2545d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2546552f7358SJed Brown   PetscFunctionReturn(0);
2547552f7358SJed Brown }
2548552f7358SJed Brown 
2549552f7358SJed Brown /*@C
2550552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2551552f7358SJed Brown 
2552552f7358SJed Brown   Not Collective
2553552f7358SJed Brown 
2554552f7358SJed Brown   Input Parameters:
2555552f7358SJed Brown + dm - The DMPlex object
2556552f7358SJed Brown . numPoints - The number of input points for the join
2557552f7358SJed Brown - points - The input points
2558552f7358SJed Brown 
2559552f7358SJed Brown   Output Parameters:
2560552f7358SJed Brown + numCoveredPoints - The number of points in the join
2561552f7358SJed Brown - coveredPoints - The points in the join
2562552f7358SJed Brown 
25633813dfbdSMatthew G Knepley   Fortran Notes:
25643813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25653813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25663813dfbdSMatthew G Knepley 
25673813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25683813dfbdSMatthew G Knepley 
2569552f7358SJed Brown   Level: intermediate
2570552f7358SJed Brown 
2571552f7358SJed Brown .keywords: mesh
2572552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2573552f7358SJed Brown @*/
2574552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2575552f7358SJed Brown {
2576552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2577552f7358SJed Brown   PetscInt      *offsets, **closures;
2578552f7358SJed Brown   PetscInt      *join[2];
2579552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
258024c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2581552f7358SJed Brown   PetscErrorCode ierr;
2582552f7358SJed Brown 
2583552f7358SJed Brown   PetscFunctionBegin;
2584552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2585552f7358SJed Brown   PetscValidPointer(points, 2);
2586552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2587552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2588552f7358SJed Brown 
2589552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
25901795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
259169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
259224c766afSToby Isaac   ms      = mesh->maxSupportSize;
259324c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
259469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
259569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2596552f7358SJed Brown 
2597552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2598552f7358SJed Brown     PetscInt closureSize;
2599552f7358SJed Brown 
2600552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
26010d644c17SKarl Rupp 
2602552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2603552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2604552f7358SJed Brown       PetscInt pStart, pEnd, i;
2605552f7358SJed Brown 
2606552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2607552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2608552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2609552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2610552f7358SJed Brown           break;
2611552f7358SJed Brown         }
2612552f7358SJed Brown       }
2613552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2614552f7358SJed Brown     }
261582f516ccSBarry 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);
2616552f7358SJed Brown   }
2617552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2618552f7358SJed Brown     PetscInt dof;
2619552f7358SJed Brown 
2620552f7358SJed Brown     /* Copy in support of first point */
2621552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2622552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2623552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2624552f7358SJed Brown     }
2625552f7358SJed Brown     /* Check each successive cone */
2626552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2627552f7358SJed Brown       PetscInt newJoinSize = 0;
2628552f7358SJed Brown 
2629552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2630552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2631552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2632552f7358SJed Brown 
2633552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2634552f7358SJed Brown           if (point == join[i][m]) {
2635552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2636552f7358SJed Brown             break;
2637552f7358SJed Brown           }
2638552f7358SJed Brown         }
2639552f7358SJed Brown       }
2640552f7358SJed Brown       joinSize = newJoinSize;
2641552f7358SJed Brown       i        = 1-i;
2642552f7358SJed Brown     }
2643552f7358SJed Brown     if (joinSize) break;
2644552f7358SJed Brown   }
2645552f7358SJed Brown   *numCoveredPoints = joinSize;
2646552f7358SJed Brown   *coveredPoints    = join[i];
2647552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26480298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2649552f7358SJed Brown   }
2650552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
265169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
265269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
2653552f7358SJed Brown   PetscFunctionReturn(0);
2654552f7358SJed Brown }
2655552f7358SJed Brown 
2656552f7358SJed Brown /*@C
2657552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2658552f7358SJed Brown 
2659552f7358SJed Brown   Not Collective
2660552f7358SJed Brown 
2661552f7358SJed Brown   Input Parameters:
2662552f7358SJed Brown + dm - The DMPlex object
2663552f7358SJed Brown . numPoints - The number of input points for the meet
2664552f7358SJed Brown - points - The input points
2665552f7358SJed Brown 
2666552f7358SJed Brown   Output Parameters:
2667552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2668552f7358SJed Brown - coveredPoints - The points in the meet
2669552f7358SJed Brown 
2670552f7358SJed Brown   Level: intermediate
2671552f7358SJed Brown 
2672552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2673552f7358SJed Brown 
26743813dfbdSMatthew G Knepley   Fortran Notes:
26753813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26763813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26773813dfbdSMatthew G Knepley 
26783813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26793813dfbdSMatthew G Knepley 
2680552f7358SJed Brown .keywords: mesh
2681552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2682552f7358SJed Brown @*/
2683552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2684552f7358SJed Brown {
2685552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2686552f7358SJed Brown   PetscInt      *meet[2];
2687552f7358SJed Brown   PetscInt       meetSize, i = 0;
2688552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2689552f7358SJed Brown   PetscErrorCode ierr;
2690552f7358SJed Brown 
2691552f7358SJed Brown   PetscFunctionBegin;
2692552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2693552f7358SJed Brown   PetscValidPointer(points, 2);
2694552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2695552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
269669291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
269769291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2698552f7358SJed Brown   /* Copy in cone of first point */
2699552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2700552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2701552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2702552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2703552f7358SJed Brown   }
2704552f7358SJed Brown   /* Check each successive cone */
2705552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2706552f7358SJed Brown     PetscInt newMeetSize = 0;
2707552f7358SJed Brown 
2708552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2709552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2710552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2711552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2712552f7358SJed Brown 
2713552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2714552f7358SJed Brown         if (point == meet[i][m]) {
2715552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2716552f7358SJed Brown           break;
2717552f7358SJed Brown         }
2718552f7358SJed Brown       }
2719552f7358SJed Brown     }
2720552f7358SJed Brown     meetSize = newMeetSize;
2721552f7358SJed Brown     i        = 1-i;
2722552f7358SJed Brown   }
2723552f7358SJed Brown   *numCoveringPoints = meetSize;
2724552f7358SJed Brown   *coveringPoints    = meet[i];
272569291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2726552f7358SJed Brown   PetscFunctionReturn(0);
2727552f7358SJed Brown }
2728552f7358SJed Brown 
2729552f7358SJed Brown /*@C
2730552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2731552f7358SJed Brown 
2732552f7358SJed Brown   Not Collective
2733552f7358SJed Brown 
2734552f7358SJed Brown   Input Parameters:
2735552f7358SJed Brown + dm - The DMPlex object
2736552f7358SJed Brown . numPoints - The number of input points for the meet
2737552f7358SJed Brown - points - The input points
2738552f7358SJed Brown 
2739552f7358SJed Brown   Output Parameters:
2740552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2741552f7358SJed Brown - coveredPoints - The points in the meet
2742552f7358SJed Brown 
2743552f7358SJed Brown   Level: intermediate
2744552f7358SJed Brown 
27453813dfbdSMatthew G Knepley   Fortran Notes:
27463813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27473813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27483813dfbdSMatthew G Knepley 
27493813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27503813dfbdSMatthew G Knepley 
2751552f7358SJed Brown .keywords: mesh
2752552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2753552f7358SJed Brown @*/
2754552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2755552f7358SJed Brown {
2756552f7358SJed Brown   PetscErrorCode ierr;
2757552f7358SJed Brown 
2758552f7358SJed Brown   PetscFunctionBegin;
2759552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2760d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2761d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2762d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
276369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
2764d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2765552f7358SJed Brown   PetscFunctionReturn(0);
2766552f7358SJed Brown }
2767552f7358SJed Brown 
2768552f7358SJed Brown /*@C
2769552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2770552f7358SJed Brown 
2771552f7358SJed Brown   Not Collective
2772552f7358SJed Brown 
2773552f7358SJed Brown   Input Parameters:
2774552f7358SJed Brown + dm - The DMPlex object
2775552f7358SJed Brown . numPoints - The number of input points for the meet
2776552f7358SJed Brown - points - The input points
2777552f7358SJed Brown 
2778552f7358SJed Brown   Output Parameters:
2779552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2780552f7358SJed Brown - coveredPoints - The points in the meet
2781552f7358SJed Brown 
2782552f7358SJed Brown   Level: intermediate
2783552f7358SJed Brown 
27843813dfbdSMatthew G Knepley   Fortran Notes:
27853813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
27863813dfbdSMatthew G Knepley   include petsc.h90 in your code.
27873813dfbdSMatthew G Knepley 
27883813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
27893813dfbdSMatthew G Knepley 
2790552f7358SJed Brown .keywords: mesh
2791552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2792552f7358SJed Brown @*/
2793552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2794552f7358SJed Brown {
2795552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2796552f7358SJed Brown   PetscInt      *offsets, **closures;
2797552f7358SJed Brown   PetscInt      *meet[2];
2798552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
279924c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2800552f7358SJed Brown   PetscErrorCode ierr;
2801552f7358SJed Brown 
2802552f7358SJed Brown   PetscFunctionBegin;
2803552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2804552f7358SJed Brown   PetscValidPointer(points, 2);
2805552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2806552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2807552f7358SJed Brown 
2808552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2809785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
281069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
281124c766afSToby Isaac   mc      = mesh->maxConeSize;
281224c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
281369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
281469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
2815552f7358SJed Brown 
2816552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2817552f7358SJed Brown     PetscInt closureSize;
2818552f7358SJed Brown 
2819552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
28200d644c17SKarl Rupp 
2821552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2822552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2823552f7358SJed Brown       PetscInt pStart, pEnd, i;
2824552f7358SJed Brown 
2825552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2826552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2827552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2828552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2829552f7358SJed Brown           break;
2830552f7358SJed Brown         }
2831552f7358SJed Brown       }
2832552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2833552f7358SJed Brown     }
283482f516ccSBarry 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);
2835552f7358SJed Brown   }
2836552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2837552f7358SJed Brown     PetscInt dof;
2838552f7358SJed Brown 
2839552f7358SJed Brown     /* Copy in cone of first point */
2840552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2841552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2842552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2843552f7358SJed Brown     }
2844552f7358SJed Brown     /* Check each successive cone */
2845552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2846552f7358SJed Brown       PetscInt newMeetSize = 0;
2847552f7358SJed Brown 
2848552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2849552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2850552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2851552f7358SJed Brown 
2852552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2853552f7358SJed Brown           if (point == meet[i][m]) {
2854552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2855552f7358SJed Brown             break;
2856552f7358SJed Brown           }
2857552f7358SJed Brown         }
2858552f7358SJed Brown       }
2859552f7358SJed Brown       meetSize = newMeetSize;
2860552f7358SJed Brown       i        = 1-i;
2861552f7358SJed Brown     }
2862552f7358SJed Brown     if (meetSize) break;
2863552f7358SJed Brown   }
2864552f7358SJed Brown   *numCoveredPoints = meetSize;
2865552f7358SJed Brown   *coveredPoints    = meet[i];
2866552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
28670298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2868552f7358SJed Brown   }
2869552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
287069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
287169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
2872552f7358SJed Brown   PetscFunctionReturn(0);
2873552f7358SJed Brown }
2874552f7358SJed Brown 
28754e3744c5SMatthew G. Knepley /*@C
28764e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
28774e3744c5SMatthew G. Knepley 
28784e3744c5SMatthew G. Knepley   Not Collective
28794e3744c5SMatthew G. Knepley 
28804e3744c5SMatthew G. Knepley   Input Parameters:
28814e3744c5SMatthew G. Knepley + dmA - A DMPlex object
28824e3744c5SMatthew G. Knepley - dmB - A DMPlex object
28834e3744c5SMatthew G. Knepley 
28844e3744c5SMatthew G. Knepley   Output Parameters:
28854e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
28864e3744c5SMatthew G. Knepley 
28874e3744c5SMatthew G. Knepley   Level: intermediate
28884e3744c5SMatthew G. Knepley 
28894e3744c5SMatthew G. Knepley   Notes:
28904e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
28914e3744c5SMatthew G. Knepley 
28924e3744c5SMatthew G. Knepley .keywords: mesh
28934e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
28944e3744c5SMatthew G. Knepley @*/
28954e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
28964e3744c5SMatthew G. Knepley {
28974e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
28984e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
28994e3744c5SMatthew G. Knepley 
29004e3744c5SMatthew G. Knepley   PetscFunctionBegin;
29014e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
29024e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
29034e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
29044e3744c5SMatthew G. Knepley 
29054e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
29064e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
29074e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
29084e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
29094e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
29104e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
29114e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
29124e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
29134e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
29144e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
29154e3744c5SMatthew G. Knepley 
29164e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
29174e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
29184e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
29194e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
29204e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
29214e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
29224e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
29234e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
29244e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
29254e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
29264e3744c5SMatthew G. Knepley     }
29274e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
29284e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
29294e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
29304e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
29314e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
29324e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
29334e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
29344e3744c5SMatthew G. Knepley     }
29354e3744c5SMatthew G. Knepley   }
29364e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
29374e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
29384e3744c5SMatthew G. Knepley }
29394e3744c5SMatthew G. Knepley 
29407cd05799SMatthew G. Knepley /*@C
29417cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
29427cd05799SMatthew G. Knepley 
29437cd05799SMatthew G. Knepley   Not Collective
29447cd05799SMatthew G. Knepley 
29457cd05799SMatthew G. Knepley   Input Parameters:
29467cd05799SMatthew G. Knepley + dm         - The DMPlex
29477cd05799SMatthew G. Knepley . cellDim    - The cell dimension
29487cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
29497cd05799SMatthew G. Knepley 
29507cd05799SMatthew G. Knepley   Output Parameters:
29517cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
29527cd05799SMatthew G. Knepley 
29537cd05799SMatthew G. Knepley   Level: developer
29547cd05799SMatthew G. Knepley 
29557cd05799SMatthew G. Knepley   Notes:
29567cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
29577cd05799SMatthew G. Knepley 
29587cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
29597cd05799SMatthew G. Knepley @*/
296018ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2961a6dfd86eSKarl Rupp {
296282f516ccSBarry Smith   MPI_Comm       comm;
2963552f7358SJed Brown   PetscErrorCode ierr;
2964552f7358SJed Brown 
2965552f7358SJed Brown   PetscFunctionBegin;
296682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2967552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2968552f7358SJed Brown   switch (cellDim) {
2969552f7358SJed Brown   case 0:
2970552f7358SJed Brown     *numFaceVertices = 0;
2971552f7358SJed Brown     break;
2972552f7358SJed Brown   case 1:
2973552f7358SJed Brown     *numFaceVertices = 1;
2974552f7358SJed Brown     break;
2975552f7358SJed Brown   case 2:
2976552f7358SJed Brown     switch (numCorners) {
297719436ca2SJed Brown     case 3: /* triangle */
297819436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2979552f7358SJed Brown       break;
298019436ca2SJed Brown     case 4: /* quadrilateral */
298119436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2982552f7358SJed Brown       break;
298319436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
298419436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2985552f7358SJed Brown       break;
298619436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
298719436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2988552f7358SJed Brown       break;
2989552f7358SJed Brown     default:
2990f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2991552f7358SJed Brown     }
2992552f7358SJed Brown     break;
2993552f7358SJed Brown   case 3:
2994552f7358SJed Brown     switch (numCorners) {
299519436ca2SJed Brown     case 4: /* tetradehdron */
299619436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2997552f7358SJed Brown       break;
299819436ca2SJed Brown     case 6: /* tet cohesive cells */
299919436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3000552f7358SJed Brown       break;
300119436ca2SJed Brown     case 8: /* hexahedron */
300219436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3003552f7358SJed Brown       break;
300419436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
300519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3006552f7358SJed Brown       break;
300719436ca2SJed Brown     case 10: /* quadratic tetrahedron */
300819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3009552f7358SJed Brown       break;
301019436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
301119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3012552f7358SJed Brown       break;
301319436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
301419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3015552f7358SJed Brown       break;
301619436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
301719436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3018552f7358SJed Brown       break;
3019552f7358SJed Brown     default:
3020f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3021552f7358SJed Brown     }
3022552f7358SJed Brown     break;
3023552f7358SJed Brown   default:
3024f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3025552f7358SJed Brown   }
3026552f7358SJed Brown   PetscFunctionReturn(0);
3027552f7358SJed Brown }
3028552f7358SJed Brown 
3029552f7358SJed Brown /*@
3030aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3031552f7358SJed Brown 
3032552f7358SJed Brown   Not Collective
3033552f7358SJed Brown 
3034aa50250dSMatthew G. Knepley   Input Parameter:
3035552f7358SJed Brown . dm    - The DMPlex object
3036552f7358SJed Brown 
3037aa50250dSMatthew G. Knepley   Output Parameter:
3038aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3039552f7358SJed Brown 
3040552f7358SJed Brown   Level: developer
3041552f7358SJed Brown 
3042aa50250dSMatthew G. Knepley .keywords: mesh, points
3043aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3044aa50250dSMatthew G. Knepley @*/
3045aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3046aa50250dSMatthew G. Knepley {
3047aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
3048aa50250dSMatthew G. Knepley 
3049aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3050aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3051aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3052c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
3053c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3054aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3055aa50250dSMatthew G. Knepley }
3056aa50250dSMatthew G. Knepley 
3057aa50250dSMatthew G. Knepley /*@
3058aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3059aa50250dSMatthew G. Knepley 
3060aa50250dSMatthew G. Knepley   Not Collective
3061aa50250dSMatthew G. Knepley 
3062aa50250dSMatthew G. Knepley   Input Parameter:
3063aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3064aa50250dSMatthew G. Knepley 
3065aa50250dSMatthew G. Knepley   Output Parameter:
3066aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3067aa50250dSMatthew G. Knepley 
3068aa50250dSMatthew G. Knepley   Level: developer
3069552f7358SJed Brown 
3070552f7358SJed Brown .keywords: mesh, points
3071aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3072552f7358SJed Brown @*/
3073552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3074552f7358SJed Brown {
3075aa50250dSMatthew G. Knepley   DMLabel        label;
3076aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3077552f7358SJed Brown   PetscErrorCode ierr;
3078552f7358SJed Brown 
3079552f7358SJed Brown   PetscFunctionBegin;
3080552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3081552f7358SJed Brown   PetscValidPointer(depth, 2);
3082aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3083aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3084552f7358SJed Brown   *depth = d-1;
3085552f7358SJed Brown   PetscFunctionReturn(0);
3086552f7358SJed Brown }
3087552f7358SJed Brown 
3088552f7358SJed Brown /*@
3089552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3090552f7358SJed Brown 
3091552f7358SJed Brown   Not Collective
3092552f7358SJed Brown 
3093552f7358SJed Brown   Input Parameters:
3094552f7358SJed Brown + dm           - The DMPlex object
3095552f7358SJed Brown - stratumValue - The requested depth
3096552f7358SJed Brown 
3097552f7358SJed Brown   Output Parameters:
3098552f7358SJed Brown + start - The first point at this depth
3099552f7358SJed Brown - end   - One beyond the last point at this depth
3100552f7358SJed Brown 
3101552f7358SJed Brown   Level: developer
3102552f7358SJed Brown 
3103552f7358SJed Brown .keywords: mesh, points
3104552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3105552f7358SJed Brown @*/
31060adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
31070adebc6cSBarry Smith {
3108aa50250dSMatthew G. Knepley   DMLabel        label;
310963d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3110552f7358SJed Brown   PetscErrorCode ierr;
3111552f7358SJed Brown 
3112552f7358SJed Brown   PetscFunctionBegin;
3113552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
311463d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
311563d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3116552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
31170d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
311863d1a920SMatthew G. Knepley   if (stratumValue < 0) {
311963d1a920SMatthew G. Knepley     if (start) *start = pStart;
312063d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
312163d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3122552f7358SJed Brown   }
3123aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
312463d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
312563d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3126552f7358SJed Brown   PetscFunctionReturn(0);
3127552f7358SJed Brown }
3128552f7358SJed Brown 
3129552f7358SJed Brown /*@
3130552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3131552f7358SJed Brown 
3132552f7358SJed Brown   Not Collective
3133552f7358SJed Brown 
3134552f7358SJed Brown   Input Parameters:
3135552f7358SJed Brown + dm           - The DMPlex object
3136552f7358SJed Brown - stratumValue - The requested height
3137552f7358SJed Brown 
3138552f7358SJed Brown   Output Parameters:
3139552f7358SJed Brown + start - The first point at this height
3140552f7358SJed Brown - end   - One beyond the last point at this height
3141552f7358SJed Brown 
3142552f7358SJed Brown   Level: developer
3143552f7358SJed Brown 
3144552f7358SJed Brown .keywords: mesh, points
3145552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3146552f7358SJed Brown @*/
31470adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
31480adebc6cSBarry Smith {
3149aa50250dSMatthew G. Knepley   DMLabel        label;
315063d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3151552f7358SJed Brown   PetscErrorCode ierr;
3152552f7358SJed Brown 
3153552f7358SJed Brown   PetscFunctionBegin;
3154552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
315563d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
315663d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3157552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
31580d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
315963d1a920SMatthew G. Knepley   if (stratumValue < 0) {
316063d1a920SMatthew G. Knepley     if (start) *start = pStart;
316163d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
316263d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3163552f7358SJed Brown   }
3164aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
316513903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
316663d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
316763d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3168552f7358SJed Brown   PetscFunctionReturn(0);
3169552f7358SJed Brown }
3170552f7358SJed Brown 
3171552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
31727cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
3173a6dfd86eSKarl Rupp {
3174ee55978aSMatthew G. Knepley   PetscInt      *pMax;
3175470f9322SMatthew G. Knepley   PetscInt       depth, cellHeight, pStart = 0, pEnd = 0;
3176ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
3177fa716be8SMatthew G. Knepley   PetscBool     *isFE;
3178552f7358SJed Brown   PetscErrorCode ierr;
3179552f7358SJed Brown 
3180552f7358SJed Brown   PetscFunctionBegin;
3181fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
3182ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
3183fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
3184fa716be8SMatthew G. Knepley     PetscObject  obj;
3185fa716be8SMatthew G. Knepley     PetscClassId id;
3186fa716be8SMatthew G. Knepley 
3187ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
3188ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
3189fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
3190fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3191fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
3192fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
3193552f7358SJed Brown   }
319482f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
3195552f7358SJed Brown   if (numFields > 0) {
3196552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
3197552f7358SJed Brown     if (numComp) {
3198552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3199552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
3200d484f18aSToby Isaac         if (isFE[f]) {
3201d484f18aSToby Isaac           PetscFE           fe;
3202d484f18aSToby Isaac           PetscDualSpace    dspace;
3203d484f18aSToby Isaac           const PetscInt    ***perms;
3204d484f18aSToby Isaac           const PetscScalar ***flips;
3205d484f18aSToby Isaac           const PetscInt    *numDof;
3206d484f18aSToby Isaac 
3207d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
3208d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
3209d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
3210d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
3211d484f18aSToby Isaac           if (perms || flips) {
3212d484f18aSToby Isaac             DM               K;
3213d484f18aSToby Isaac             DMLabel          depthLabel;
3214d484f18aSToby Isaac             PetscInt         depth, h;
3215d484f18aSToby Isaac             PetscSectionSym  sym;
3216d484f18aSToby Isaac 
3217d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
3218d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
3219d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
3220d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
3221d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
3222d484f18aSToby Isaac               PetscDualSpace    hspace;
3223d484f18aSToby Isaac               PetscInt          kStart, kEnd;
3224d484f18aSToby Isaac               PetscInt          kConeSize;
3225d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3226d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3227d484f18aSToby Isaac 
3228d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3229d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3230d484f18aSToby Isaac               if (!hspace) continue;
3231d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3232d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3233d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3234d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3235d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3236d484f18aSToby Isaac               ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3237d484f18aSToby Isaac             }
3238d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3239d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3240d484f18aSToby Isaac           }
3241d484f18aSToby Isaac         }
3242552f7358SJed Brown       }
3243552f7358SJed Brown     }
3244552f7358SJed Brown   }
3245552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3246552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3247916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
32489ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
32499ac3fadcSMatthew 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);
3250470f9322SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
3251470f9322SMatthew G. Knepley   for (dep = 0; dep <= depth - cellHeight; ++dep) {
3252916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3253916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3254fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3255552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3256ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3257ee55978aSMatthew G. Knepley 
3258552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3259fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3260552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3261ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3262552f7358SJed Brown       }
3263ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3264552f7358SJed Brown     }
3265552f7358SJed Brown   }
32669ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3267fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3268552f7358SJed Brown   PetscFunctionReturn(0);
3269552f7358SJed Brown }
3270552f7358SJed Brown 
3271552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3272ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3273552f7358SJed Brown */
32747cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3275a6dfd86eSKarl Rupp {
3276552f7358SJed Brown   PetscInt       numFields;
3277552f7358SJed Brown   PetscInt       bc;
3278a68b90caSToby Isaac   PetscSection   aSec;
3279552f7358SJed Brown   PetscErrorCode ierr;
3280552f7358SJed Brown 
3281552f7358SJed Brown   PetscFunctionBegin;
3282552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3283552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3284552f7358SJed Brown     PetscInt        field = 0;
3285ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3286552f7358SJed Brown     const PetscInt *idx;
3287ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3288552f7358SJed Brown 
32890d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3290ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3291ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3292552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3293552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3294552f7358SJed Brown     for (i = 0; i < n; ++i) {
3295552f7358SJed Brown       const PetscInt p = idx[i];
329606ff1081SMatthew G. Knepley       PetscInt       numConst;
3297552f7358SJed Brown 
3298552f7358SJed Brown       if (numFields) {
3299552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3300552f7358SJed Brown       } else {
3301552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3302552f7358SJed Brown       }
330306ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
330406ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
330506ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3306552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3307552f7358SJed Brown     }
3308552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
330906ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3310552f7358SJed Brown   }
3311a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3312a68b90caSToby Isaac   if (aSec) {
3313a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3314a68b90caSToby Isaac 
3315a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3316a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3317ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3318a68b90caSToby Isaac 
3319a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3320a68b90caSToby Isaac       if (dof) {
3321a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3322a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3323a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3324a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3325a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3326a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3327a68b90caSToby Isaac         }
3328a68b90caSToby Isaac       }
3329a68b90caSToby Isaac     }
3330a68b90caSToby Isaac   }
3331552f7358SJed Brown   PetscFunctionReturn(0);
3332552f7358SJed Brown }
3333552f7358SJed Brown 
3334ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3335ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3336ab1d0545SMatthew G. Knepley */
33377cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
33380adebc6cSBarry Smith {
3339ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3340ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3341503335f2SSander Arens   PetscInt       numFields, cdof, maxDof = 0, pStart, pEnd, p, bc, f, d;
3342552f7358SJed Brown   PetscErrorCode ierr;
3343552f7358SJed Brown 
3344552f7358SJed Brown   PetscFunctionBegin;
3345552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3346ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3347ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3348552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3349503335f2SSander Arens   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr); maxDof = PetscMax(maxDof, cdof);}
3350ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3351ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3352ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3353ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3354ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3355ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3356ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3357ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3358552f7358SJed Brown 
3359ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3360ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3361ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3362ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3363ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3364ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3365ab1d0545SMatthew G. Knepley       const PetscInt *find;
3366503335f2SSander Arens       PetscInt        fdof, fcdof, c;
3367ab1d0545SMatthew G. Knepley 
3368503335f2SSander Arens       ierr = PetscSectionGetFieldDof(section, p, field, &fdof);CHKERRQ(ierr);
3369503335f2SSander Arens       if (!fdof) continue;
3370ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3371503335f2SSander Arens         for (d = 0; d < fdof; ++d) indices[d] = d;
3372503335f2SSander Arens         fcdof = fdof;
3373552f7358SJed Brown       } else {
3374503335f2SSander Arens         ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3375ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3376ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3377503335f2SSander Arens         for (c = 0; c < Nc; ++c) indices[d++] = comp[c];
3378503335f2SSander Arens         ierr = PetscSortRemoveDupsInt(&d, indices);CHKERRQ(ierr);
3379503335f2SSander Arens         for (c = d; c < fcdof; ++c) indices[c] = -1;
3380503335f2SSander Arens         fcdof = d;
3381552f7358SJed Brown       }
3382503335f2SSander Arens       ierr = PetscSectionSetFieldConstraintDof(section, p, field, fcdof);CHKERRQ(ierr);
3383ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3384552f7358SJed Brown     }
3385ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3386ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3387552f7358SJed Brown   }
3388ab1d0545SMatthew G. Knepley   /* Handle anchors */
3389ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3390ab1d0545SMatthew G. Knepley   if (aSec) {
3391ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3392552f7358SJed Brown 
3393ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3394ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3395ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3396503335f2SSander Arens       PetscInt dof, f;
3397ab1d0545SMatthew G. Knepley 
3398ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3399ab1d0545SMatthew G. Knepley       if (dof) {
3400ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3401ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3402ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3403ab1d0545SMatthew G. Knepley         }
3404ab1d0545SMatthew G. Knepley       }
3405ab1d0545SMatthew G. Knepley     }
3406ab1d0545SMatthew G. Knepley   }
3407ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3408ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3409ab1d0545SMatthew G. Knepley }
3410ab1d0545SMatthew G. Knepley 
3411ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
34127cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3413ab1d0545SMatthew G. Knepley {
3414ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3415ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3416ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3417ab1d0545SMatthew G. Knepley 
3418ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3419ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3420ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3421ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3422ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3423ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3424552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3425552f7358SJed Brown     PetscInt cdof, d;
3426552f7358SJed Brown 
3427552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3428552f7358SJed Brown     if (cdof) {
3429552f7358SJed Brown       if (numFields) {
3430552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3431552f7358SJed Brown 
3432552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3433ab1d0545SMatthew G. Knepley           const PetscInt *find;
3434ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3435552f7358SJed Brown 
3436552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3437ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3438ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3439ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3440ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3441ab1d0545SMatthew G. Knepley           numConst += fcdof;
3442552f7358SJed Brown           foff     += fdof;
3443552f7358SJed Brown         }
3444503335f2SSander Arens         if (cdof != numConst) {ierr = PetscSectionSetConstraintDof(section, p, numConst);CHKERRQ(ierr);}
3445552f7358SJed Brown       } else {
34460d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3447552f7358SJed Brown       }
3448552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3449552f7358SJed Brown     }
3450552f7358SJed Brown   }
3451552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3452552f7358SJed Brown   PetscFunctionReturn(0);
3453552f7358SJed Brown }
3454552f7358SJed Brown 
3455552f7358SJed Brown /*@C
3456552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3457552f7358SJed Brown 
3458552f7358SJed Brown   Not Collective
3459552f7358SJed Brown 
3460552f7358SJed Brown   Input Parameters:
3461552f7358SJed Brown + dm        - The DMPlex object
3462552f7358SJed Brown . dim       - The spatial dimension of the problem
3463552f7358SJed Brown . numFields - The number of fields in the problem
3464552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3465552f7358SJed 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
3466552f7358SJed Brown . numBC     - The number of boundary conditions
3467552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3468ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3469ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3470f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3471552f7358SJed Brown 
3472552f7358SJed Brown   Output Parameter:
3473552f7358SJed Brown . section - The PetscSection object
3474552f7358SJed Brown 
347595452b02SPatrick Sanan   Notes:
347695452b02SPatrick Sanan     numDof[f*(dim+1)+d] gives the number of dof for field f on points of dimension d. For instance, numDof[1] is the
3477f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3478f8f126e8SMatthew G. Knepley 
3479f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3480552f7358SJed Brown 
3481552f7358SJed Brown   Level: developer
3482552f7358SJed Brown 
34833813dfbdSMatthew G Knepley   Fortran Notes:
34843813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
34853813dfbdSMatthew G Knepley 
3486552f7358SJed Brown .keywords: mesh, elements
3487f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3488552f7358SJed Brown @*/
3489ab1d0545SMatthew 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)
3490a6dfd86eSKarl Rupp {
3491a68b90caSToby Isaac   PetscSection   aSec;
3492552f7358SJed Brown   PetscErrorCode ierr;
3493552f7358SJed Brown 
3494552f7358SJed Brown   PetscFunctionBegin;
3495552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3496ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3497f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3498552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3499a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3500ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3501ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3502ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3503ab1d0545SMatthew G. Knepley   }
3504ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3505552f7358SJed Brown   PetscFunctionReturn(0);
3506552f7358SJed Brown }
3507552f7358SJed Brown 
35080adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
35090adebc6cSBarry Smith {
3510efe440bfSMatthew G. Knepley   PetscSection   section, s;
3511efe440bfSMatthew G. Knepley   Mat            m;
35123e922f36SToby Isaac   PetscInt       maxHeight;
3513552f7358SJed Brown   PetscErrorCode ierr;
3514552f7358SJed Brown 
3515552f7358SJed Brown   PetscFunctionBegin;
351638221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
35173e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
35183e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
351982f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3520552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
35211d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3522efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3523efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3524efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3525efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3526efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3527552f7358SJed Brown   PetscFunctionReturn(0);
3528552f7358SJed Brown }
3529552f7358SJed Brown 
3530f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3531f19dbd58SToby Isaac {
3532f19dbd58SToby Isaac   Vec            coordsLocal;
3533f19dbd58SToby Isaac   DM             coordsDM;
3534f19dbd58SToby Isaac   PetscErrorCode ierr;
3535f19dbd58SToby Isaac 
3536f19dbd58SToby Isaac   PetscFunctionBegin;
3537f19dbd58SToby Isaac   *field = NULL;
3538f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3539f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3540f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3541f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3542f19dbd58SToby Isaac   }
3543f19dbd58SToby Isaac   PetscFunctionReturn(0);
3544f19dbd58SToby Isaac }
3545f19dbd58SToby Isaac 
35467cd05799SMatthew G. Knepley /*@C
35477cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
35487cd05799SMatthew G. Knepley 
35497cd05799SMatthew G. Knepley   Not Collective
35507cd05799SMatthew G. Knepley 
35517cd05799SMatthew G. Knepley   Input Parameters:
35527cd05799SMatthew G. Knepley . dm        - The DMPlex object
35537cd05799SMatthew G. Knepley 
35547cd05799SMatthew G. Knepley   Output Parameter:
35557cd05799SMatthew G. Knepley . section - The PetscSection object
35567cd05799SMatthew G. Knepley 
35577cd05799SMatthew G. Knepley   Level: developer
35587cd05799SMatthew G. Knepley 
35597cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
35607cd05799SMatthew G. Knepley @*/
35610adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
35620adebc6cSBarry Smith {
3563552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3564552f7358SJed Brown 
3565552f7358SJed Brown   PetscFunctionBegin;
3566552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3567552f7358SJed Brown   if (section) *section = mesh->coneSection;
3568552f7358SJed Brown   PetscFunctionReturn(0);
3569552f7358SJed Brown }
3570552f7358SJed Brown 
35717cd05799SMatthew G. Knepley /*@C
35727cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
35737cd05799SMatthew G. Knepley 
35747cd05799SMatthew G. Knepley   Not Collective
35757cd05799SMatthew G. Knepley 
35767cd05799SMatthew G. Knepley   Input Parameters:
35777cd05799SMatthew G. Knepley . dm        - The DMPlex object
35787cd05799SMatthew G. Knepley 
35797cd05799SMatthew G. Knepley   Output Parameter:
35807cd05799SMatthew G. Knepley . section - The PetscSection object
35817cd05799SMatthew G. Knepley 
35827cd05799SMatthew G. Knepley   Level: developer
35837cd05799SMatthew G. Knepley 
35847cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35857cd05799SMatthew G. Knepley @*/
35868cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
35878cb4d582SMatthew G. Knepley {
35888cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
35898cb4d582SMatthew G. Knepley 
35908cb4d582SMatthew G. Knepley   PetscFunctionBegin;
35918cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35928cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
35938cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
35948cb4d582SMatthew G. Knepley }
35958cb4d582SMatthew G. Knepley 
35967cd05799SMatthew G. Knepley /*@C
35977cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
35987cd05799SMatthew G. Knepley 
35997cd05799SMatthew G. Knepley   Not Collective
36007cd05799SMatthew G. Knepley 
36017cd05799SMatthew G. Knepley   Input Parameters:
36027cd05799SMatthew G. Knepley . dm        - The DMPlex object
36037cd05799SMatthew G. Knepley 
36047cd05799SMatthew G. Knepley   Output Parameter:
36057cd05799SMatthew G. Knepley . cones - The cone for each point
36067cd05799SMatthew G. Knepley 
36077cd05799SMatthew G. Knepley   Level: developer
36087cd05799SMatthew G. Knepley 
36097cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
36107cd05799SMatthew G. Knepley @*/
3611a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3612a6dfd86eSKarl Rupp {
3613552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3614552f7358SJed Brown 
3615552f7358SJed Brown   PetscFunctionBegin;
3616552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3617552f7358SJed Brown   if (cones) *cones = mesh->cones;
3618552f7358SJed Brown   PetscFunctionReturn(0);
3619552f7358SJed Brown }
3620552f7358SJed Brown 
36217cd05799SMatthew G. Knepley /*@C
36227cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
36237cd05799SMatthew G. Knepley 
36247cd05799SMatthew G. Knepley   Not Collective
36257cd05799SMatthew G. Knepley 
36267cd05799SMatthew G. Knepley   Input Parameters:
36277cd05799SMatthew G. Knepley . dm        - The DMPlex object
36287cd05799SMatthew G. Knepley 
36297cd05799SMatthew G. Knepley   Output Parameter:
36307cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
36317cd05799SMatthew G. Knepley 
36327cd05799SMatthew G. Knepley   Level: developer
36337cd05799SMatthew G. Knepley 
36347cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
36357cd05799SMatthew G. Knepley @*/
3636a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3637a6dfd86eSKarl Rupp {
3638552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3639552f7358SJed Brown 
3640552f7358SJed Brown   PetscFunctionBegin;
3641552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3642552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3643552f7358SJed Brown   PetscFunctionReturn(0);
3644552f7358SJed Brown }
3645552f7358SJed Brown 
3646552f7358SJed Brown /******************************** FEM Support **********************************/
3647552f7358SJed Brown 
36487391a63aSMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscInt point, PetscSection section)
36493194fc30SMatthew G. Knepley {
36507391a63aSMatthew G. Knepley   DMLabel        label;
36513194fc30SMatthew G. Knepley   PetscInt      *perm;
36527391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
36533194fc30SMatthew G. Knepley   PetscErrorCode ierr;
36543194fc30SMatthew G. Knepley 
36553194fc30SMatthew G. Knepley   PetscFunctionBegin;
36567391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
36573194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
36587391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
36597391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
36607391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
36617391a63aSMatthew G. Knepley   else if  (depth == dim) {
36627391a63aSMatthew G. Knepley     const PetscInt *cone;
36637391a63aSMatthew G. Knepley 
36647391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
36657391a63aSMatthew G. Knepley     eStart = cone[0];
36667391a63aSMatthew 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);
36677391a63aSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
36683194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
366989eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
36703194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
36713194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
36723194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
36733194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
36743194fc30SMatthew G. Knepley     k = k/Nc + 1;
367589eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
36763194fc30SMatthew G. Knepley   }
36773194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
36783194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
367989eabcffSMatthew G. Knepley     switch (dim) {
368089eabcffSMatthew G. Knepley     case 2:
36813194fc30SMatthew 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} */
36823194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
36833194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
36843194fc30SMatthew G. Knepley       k = k/Nc + 1;
36853194fc30SMatthew G. Knepley       /* The SEM order is
36863194fc30SMatthew G. Knepley 
36873194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
368889eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
36893194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
36903194fc30SMatthew G. Knepley       */
36913194fc30SMatthew G. Knepley       {
36923194fc30SMatthew G. Knepley         const PetscInt of   = 0;
36933194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
36943194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
36953194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
36963194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
36973194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
36983194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
36993194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
37003194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
37013194fc30SMatthew G. Knepley         PetscInt       o;
37023194fc30SMatthew G. Knepley 
37033194fc30SMatthew G. Knepley         /* bottom */
37043194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
37053194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
37063194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
37073194fc30SMatthew G. Knepley         /* middle */
37083194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
37093194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
37103194fc30SMatthew 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;
37113194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
37123194fc30SMatthew G. Knepley         }
37133194fc30SMatthew G. Knepley         /* top */
37143194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
37153194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
37163194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
37173194fc30SMatthew G. Knepley         foffset = offset;
37183194fc30SMatthew G. Knepley       }
371989eabcffSMatthew G. Knepley       break;
372089eabcffSMatthew G. Knepley     case 3:
372189eabcffSMatthew G. Knepley       /* The original hex closure is
372289eabcffSMatthew G. Knepley 
372389eabcffSMatthew G. Knepley          {c,
372489eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
372589eabcffSMatthew 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,
372689eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
372789eabcffSMatthew G. Knepley       */
372889eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
372989eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
373089eabcffSMatthew G. Knepley       k = k/Nc + 1;
373189eabcffSMatthew G. Knepley       /* The SEM order is
373289eabcffSMatthew G. Knepley          Bottom Slice
373389eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
373489eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
373589eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
373689eabcffSMatthew G. Knepley 
373789eabcffSMatthew G. Knepley          Middle Slice (j)
373889eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
373989eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
374089eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
374189eabcffSMatthew G. Knepley 
374289eabcffSMatthew G. Knepley          Top Slice
374389eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
374489eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
374589eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
374689eabcffSMatthew G. Knepley       */
374789eabcffSMatthew G. Knepley       {
374889eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
374989eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
375089eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
375189eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
375289eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
375389eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
375489eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
375589eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
375689eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
375789eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
375889eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
375989eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
376089eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
376189eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
376289eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
376389eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
376489eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
376589eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
376689eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
376789eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
376889eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
376989eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
377089eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
377189eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
377289eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
377389eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
377489eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
377589eabcffSMatthew G. Knepley         PetscInt       o, n;
377689eabcffSMatthew G. Knepley 
377789eabcffSMatthew G. Knepley         /* Bottom Slice */
377889eabcffSMatthew G. Knepley         /*   bottom */
377989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
378089eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
378189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
378289eabcffSMatthew G. Knepley         /*   middle */
378389eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
378489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3785316b7f87SMax 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;}
378689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
37873194fc30SMatthew G. Knepley         }
378889eabcffSMatthew G. Knepley         /*   top */
378989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
379089eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
379189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
379289eabcffSMatthew G. Knepley 
379389eabcffSMatthew G. Knepley         /* Middle Slice */
379489eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
379589eabcffSMatthew G. Knepley           /*   bottom */
379689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
379789eabcffSMatthew 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;
379889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
379989eabcffSMatthew G. Knepley           /*   middle */
380089eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
380189eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
380289eabcffSMatthew 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;
380389eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
380489eabcffSMatthew G. Knepley           }
380589eabcffSMatthew G. Knepley           /*   top */
380689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
380789eabcffSMatthew 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;
380889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
380989eabcffSMatthew G. Knepley         }
381089eabcffSMatthew G. Knepley 
381189eabcffSMatthew G. Knepley         /* Top Slice */
381289eabcffSMatthew G. Knepley         /*   bottom */
381389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
381489eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
381589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
381689eabcffSMatthew G. Knepley         /*   middle */
381789eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
381889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
381989eabcffSMatthew 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;
382089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
382189eabcffSMatthew G. Knepley         }
382289eabcffSMatthew G. Knepley         /*   top */
382389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
382489eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
382589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
382689eabcffSMatthew G. Knepley 
382789eabcffSMatthew G. Knepley         foffset = offset;
382889eabcffSMatthew G. Knepley       }
382989eabcffSMatthew G. Knepley       break;
383089eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
383189eabcffSMatthew G. Knepley     }
383289eabcffSMatthew G. Knepley   }
383389eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
38343194fc30SMatthew G. Knepley   /* Check permutation */
38353194fc30SMatthew G. Knepley   {
38363194fc30SMatthew G. Knepley     PetscInt *check;
38373194fc30SMatthew G. Knepley 
38383194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
38393194fc30SMatthew 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]);}
38403194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
38413194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
38423194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
38433194fc30SMatthew G. Knepley   }
38441fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
38453194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
38463194fc30SMatthew G. Knepley }
38473194fc30SMatthew G. Knepley 
3848e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3849e071409bSToby Isaac {
3850e071409bSToby Isaac   PetscDS        prob;
3851e071409bSToby Isaac   PetscInt       depth, Nf, h;
3852e071409bSToby Isaac   DMLabel        label;
3853e071409bSToby Isaac   PetscErrorCode ierr;
3854e071409bSToby Isaac 
3855e071409bSToby Isaac   PetscFunctionBeginHot;
3856e071409bSToby Isaac   prob    = dm->prob;
3857e071409bSToby Isaac   Nf      = prob->Nf;
3858e071409bSToby Isaac   label   = dm->depthLabel;
3859e071409bSToby Isaac   *dspace = NULL;
3860e071409bSToby Isaac   if (field < Nf) {
3861e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3862e071409bSToby Isaac 
3863e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3864e071409bSToby Isaac       PetscDualSpace dsp;
3865e071409bSToby Isaac 
3866e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3867e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3868e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3869e071409bSToby Isaac       h    = depth - 1 - h;
3870e071409bSToby Isaac       if (h) {
3871e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3872e071409bSToby Isaac       } else {
3873e071409bSToby Isaac         *dspace = dsp;
3874e071409bSToby Isaac       }
3875e071409bSToby Isaac     }
3876e071409bSToby Isaac   }
3877e071409bSToby Isaac   PetscFunctionReturn(0);
3878e071409bSToby Isaac }
3879e071409bSToby Isaac 
3880e071409bSToby Isaac 
38811a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3882a6dfd86eSKarl Rupp {
3883552f7358SJed Brown   PetscScalar    *array, *vArray;
3884d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
38851a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3886552f7358SJed Brown   PetscErrorCode  ierr;
3887552f7358SJed Brown 
38881b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
38892a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
38905a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
38915a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
38925a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
38933f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
38949df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
38959df71ca4SMatthew G. Knepley       PetscInt dof;
3896d9917b9dSMatthew G. Knepley 
38979df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38989df71ca4SMatthew G. Knepley       size += dof;
38999df71ca4SMatthew G. Knepley     }
39009df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
39019df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
39022a3aaacfSMatthew G. Knepley       PetscInt       dof;
39035a1bb5cfSMatthew G. Knepley 
39045a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
39052a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
39065a1bb5cfSMatthew G. Knepley       size += dof;
39075a1bb5cfSMatthew G. Knepley     }
39083f7cbbe7SMatthew G. Knepley     if (!values) {
39093f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
39103f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
39113f7cbbe7SMatthew G. Knepley     }
391269291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
3913982e9ed1SMatthew G. Knepley   } else {
3914982e9ed1SMatthew G. Knepley     array = *values;
3915982e9ed1SMatthew G. Knepley   }
39169df71ca4SMatthew G. Knepley   size = 0;
39175a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
39189df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
39199df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
39209df71ca4SMatthew G. Knepley     PetscScalar *varr;
3921d9917b9dSMatthew G. Knepley 
39229df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39239df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
39249df71ca4SMatthew G. Knepley     varr = &vArray[off];
39251a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
39261a271a75SMatthew G. Knepley       array[offset] = varr[d];
39279df71ca4SMatthew G. Knepley     }
39289df71ca4SMatthew G. Knepley     size += dof;
39299df71ca4SMatthew G. Knepley   }
39309df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
39319df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
39329df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
39335a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
39345a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
39355a1bb5cfSMatthew G. Knepley 
393652ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
39375a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
39385a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
39395a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
39405a1bb5cfSMatthew G. Knepley     if (o >= 0) {
39411a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
39421a271a75SMatthew G. Knepley         array[offset] = varr[d];
39435a1bb5cfSMatthew G. Knepley       }
39445a1bb5cfSMatthew G. Knepley     } else {
39451a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
39461a271a75SMatthew G. Knepley         array[offset] = varr[d];
39475a1bb5cfSMatthew G. Knepley       }
39485a1bb5cfSMatthew G. Knepley     }
39499df71ca4SMatthew G. Knepley     size += dof;
39505a1bb5cfSMatthew G. Knepley   }
39515a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
39529df71ca4SMatthew G. Knepley   if (!*values) {
39535a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
39545a1bb5cfSMatthew G. Knepley     *values = array;
39559df71ca4SMatthew G. Knepley   } else {
39568ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
39578c312ff3SMatthew G. Knepley     *csize = size;
39589df71ca4SMatthew G. Knepley   }
39595a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
39605a1bb5cfSMatthew G. Knepley }
3961d9917b9dSMatthew G. Knepley 
3962923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3963923c78e0SToby Isaac {
3964923c78e0SToby Isaac   const PetscInt *cla;
3965923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3966923c78e0SToby Isaac   PetscErrorCode ierr;
3967923c78e0SToby Isaac 
3968923c78e0SToby Isaac   PetscFunctionBeginHot;
3969923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3970923c78e0SToby Isaac   if (!*clPoints) {
3971923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3972923c78e0SToby Isaac 
3973923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3974923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3975923c78e0SToby Isaac     /* Compress out points not in the section */
3976923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3977923c78e0SToby Isaac       PetscInt r = pts[2*p];
3978923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3979923c78e0SToby Isaac         pts[q*2]   = r;
3980923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3981923c78e0SToby Isaac         ++q;
3982923c78e0SToby Isaac       }
3983923c78e0SToby Isaac     }
3984923c78e0SToby Isaac     np = q;
3985923c78e0SToby Isaac     cla = NULL;
3986923c78e0SToby Isaac   } else {
3987923c78e0SToby Isaac     PetscInt dof, off;
3988923c78e0SToby Isaac 
3989923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3990923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3991923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3992923c78e0SToby Isaac     np   = dof/2;
3993923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3994923c78e0SToby Isaac   }
3995923c78e0SToby Isaac   *numPoints = np;
3996923c78e0SToby Isaac   *points    = pts;
3997923c78e0SToby Isaac   *clp       = cla;
3998923c78e0SToby Isaac 
3999923c78e0SToby Isaac   PetscFunctionReturn(0);
4000923c78e0SToby Isaac }
4001923c78e0SToby Isaac 
4002923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4003923c78e0SToby Isaac {
4004923c78e0SToby Isaac   PetscErrorCode ierr;
4005923c78e0SToby Isaac 
4006923c78e0SToby Isaac   PetscFunctionBeginHot;
4007923c78e0SToby Isaac   if (!*clPoints) {
4008923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4009923c78e0SToby Isaac   } else {
4010923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4011923c78e0SToby Isaac   }
4012923c78e0SToby Isaac   *numPoints = 0;
4013923c78e0SToby Isaac   *points    = NULL;
4014923c78e0SToby Isaac   *clSec     = NULL;
4015923c78e0SToby Isaac   *clPoints  = NULL;
4016923c78e0SToby Isaac   *clp       = NULL;
4017923c78e0SToby Isaac   PetscFunctionReturn(0);
4018923c78e0SToby Isaac }
4019923c78e0SToby Isaac 
402097e99dd9SToby 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[])
40211a271a75SMatthew G. Knepley {
40221a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
402397e99dd9SToby Isaac   const PetscInt    **perms = NULL;
402497e99dd9SToby Isaac   const PetscScalar **flips = NULL;
40251a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
40261a271a75SMatthew G. Knepley 
40271a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4028fe02ba77SJed Brown   *size = 0;
402997e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
403097e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
403197e99dd9SToby Isaac     const PetscInt    point = points[2*p];
403297e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
403397e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
40341a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
40351a271a75SMatthew G. Knepley     const PetscScalar *varr;
40361a271a75SMatthew G. Knepley 
40371a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
40381a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
40391a271a75SMatthew G. Knepley     varr = &vArray[off];
404097e99dd9SToby Isaac     if (clperm) {
404197e99dd9SToby Isaac       if (perm) {
404297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
40431a271a75SMatthew G. Knepley       } else {
404497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
404597e99dd9SToby Isaac       }
404697e99dd9SToby Isaac       if (flip) {
404797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
404897e99dd9SToby Isaac       }
404997e99dd9SToby Isaac     } else {
405097e99dd9SToby Isaac       if (perm) {
405197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
405297e99dd9SToby Isaac       } else {
405397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
405497e99dd9SToby Isaac       }
405597e99dd9SToby Isaac       if (flip) {
405697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
40571a271a75SMatthew G. Knepley       }
40581a271a75SMatthew G. Knepley     }
405997e99dd9SToby Isaac     offset += dof;
406097e99dd9SToby Isaac   }
406197e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
40621a271a75SMatthew G. Knepley   *size = offset;
40631a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
40641a271a75SMatthew G. Knepley }
40651a271a75SMatthew G. Knepley 
406697e99dd9SToby 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[])
40671a271a75SMatthew G. Knepley {
40681a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
40691a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
40701a271a75SMatthew G. Knepley 
40711a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4072fe02ba77SJed Brown   *size = 0;
40731a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
407497e99dd9SToby Isaac     PetscInt          p;
407597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
407697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
40771a271a75SMatthew G. Knepley 
407897e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
407997e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
408097e99dd9SToby Isaac       const PetscInt    point = points[2*p];
408197e99dd9SToby Isaac       PetscInt          fdof, foff, b;
40821a271a75SMatthew G. Knepley       const PetscScalar *varr;
408397e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
408497e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
40851a271a75SMatthew G. Knepley 
40861a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
40871a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
40881a271a75SMatthew G. Knepley       varr = &vArray[foff];
408997e99dd9SToby Isaac       if (clperm) {
409097e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
409197e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
409297e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
40931a271a75SMatthew G. Knepley       } else {
409497e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
409597e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
409697e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
40971a271a75SMatthew G. Knepley       }
409897e99dd9SToby Isaac       offset += fdof;
40991a271a75SMatthew G. Knepley     }
410097e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
41011a271a75SMatthew G. Knepley   }
41021a271a75SMatthew G. Knepley   *size = offset;
41031a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
41041a271a75SMatthew G. Knepley }
41051a271a75SMatthew G. Knepley 
4106552f7358SJed Brown /*@C
4107552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4108552f7358SJed Brown 
4109552f7358SJed Brown   Not collective
4110552f7358SJed Brown 
4111552f7358SJed Brown   Input Parameters:
4112552f7358SJed Brown + dm - The DM
4113552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4114552f7358SJed Brown . v - The local vector
411522c1ee49SMatthew G. Knepley . point - The point in the DM
411622c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
411722c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4118552f7358SJed Brown 
4119552f7358SJed Brown   Output Parameters:
412022c1ee49SMatthew G. Knepley + csize - The number of values in the closure
412122c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
412222c1ee49SMatthew G. Knepley 
412322c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
412422c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
412522c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
412622c1ee49SMatthew G. Knepley $
412722c1ee49SMatthew G. Knepley $ A typical use could be
412822c1ee49SMatthew G. Knepley $
412922c1ee49SMatthew G. Knepley $  values = NULL;
413022c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
413122c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
413222c1ee49SMatthew G. Knepley $    <Compute on closure>
413322c1ee49SMatthew G. Knepley $  }
413422c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
413522c1ee49SMatthew G. Knepley $
413622c1ee49SMatthew G. Knepley $ or
413722c1ee49SMatthew G. Knepley $
413822c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
413922c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
414022c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
414122c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
414222c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
414322c1ee49SMatthew G. Knepley $      <Compute on closure>
414422c1ee49SMatthew G. Knepley $    }
414522c1ee49SMatthew G. Knepley $  }
414622c1ee49SMatthew G. Knepley $  PetscFree(values);
4147552f7358SJed Brown 
4148552f7358SJed Brown   Fortran Notes:
4149552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4150552f7358SJed Brown   include petsc.h90 in your code.
4151552f7358SJed Brown 
4152552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4153552f7358SJed Brown 
4154552f7358SJed Brown   Level: intermediate
4155552f7358SJed Brown 
4156552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4157552f7358SJed Brown @*/
4158552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4159552f7358SJed Brown {
4160552f7358SJed Brown   PetscSection       clSection;
4161d9917b9dSMatthew G. Knepley   IS                 clPoints;
41628a84db2dSMatthew G. Knepley   PetscScalar       *array;
41638a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4164552f7358SJed Brown   PetscInt          *points = NULL;
41658f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
41661a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4167552f7358SJed Brown   PetscErrorCode     ierr;
4168552f7358SJed Brown 
4169d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4170552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4171552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
41721a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
41731a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4174552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4175552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4176552f7358SJed Brown   if (depth == 1 && numFields < 2) {
41771a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4178552f7358SJed Brown     PetscFunctionReturn(0);
4179552f7358SJed Brown   }
41801a271a75SMatthew G. Knepley   /* Get points */
4181923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41821fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
41831a271a75SMatthew G. Knepley   /* Get array */
4184bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
41851a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4186552f7358SJed Brown 
41871a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4188552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
41891a271a75SMatthew G. Knepley       asize += dof;
4190552f7358SJed Brown     }
4191bd6c0d32SMatthew G. Knepley     if (!values) {
4192923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
41931a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4194bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4195bd6c0d32SMatthew G. Knepley     }
419669291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4197d0f6b257SMatthew G. Knepley   } else {
4198d0f6b257SMatthew G. Knepley     array = *values;
4199d0f6b257SMatthew G. Knepley   }
42008a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
42011a271a75SMatthew G. Knepley   /* Get values */
420297e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
420397e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
42041a271a75SMatthew G. Knepley   /* Cleanup points */
4205923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
42061a271a75SMatthew G. Knepley   /* Cleanup array */
42078a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4208d0f6b257SMatthew G. Knepley   if (!*values) {
4209552f7358SJed Brown     if (csize) *csize = size;
4210552f7358SJed Brown     *values = array;
4211d0f6b257SMatthew G. Knepley   } else {
4212f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4213d0f6b257SMatthew G. Knepley     *csize = size;
4214d0f6b257SMatthew G. Knepley   }
4215552f7358SJed Brown   PetscFunctionReturn(0);
4216552f7358SJed Brown }
4217552f7358SJed Brown 
4218552f7358SJed Brown /*@C
4219552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4220552f7358SJed Brown 
4221552f7358SJed Brown   Not collective
4222552f7358SJed Brown 
4223552f7358SJed Brown   Input Parameters:
4224552f7358SJed Brown + dm - The DM
42250298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4226552f7358SJed Brown . v - The local vector
4227eaf898f9SPatrick Sanan . point - The point in the DM
42280298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4229552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4230552f7358SJed Brown 
423122c1ee49SMatthew 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()
423222c1ee49SMatthew G. Knepley 
42333813dfbdSMatthew G Knepley   Fortran Notes:
42343813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
42353813dfbdSMatthew G Knepley   include petsc.h90 in your code.
42363813dfbdSMatthew G Knepley 
42373813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
42383813dfbdSMatthew G Knepley 
4239552f7358SJed Brown   Level: intermediate
4240552f7358SJed Brown 
4241552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4242552f7358SJed Brown @*/
42437c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4244a6dfd86eSKarl Rupp {
4245552f7358SJed Brown   PetscInt       size = 0;
4246552f7358SJed Brown   PetscErrorCode ierr;
4247552f7358SJed Brown 
4248552f7358SJed Brown   PetscFunctionBegin;
4249552f7358SJed Brown   /* Should work without recalculating size */
425069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4251c9fdaa05SMatthew G. Knepley   *values = NULL;
4252552f7358SJed Brown   PetscFunctionReturn(0);
4253552f7358SJed Brown }
4254552f7358SJed Brown 
4255552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4256552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4257552f7358SJed Brown 
425897e99dd9SToby 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[])
4259552f7358SJed Brown {
4260552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4261552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4262552f7358SJed Brown   PetscScalar    *a;
4263552f7358SJed Brown   PetscInt        off, cind = 0, k;
4264552f7358SJed Brown   PetscErrorCode  ierr;
4265552f7358SJed Brown 
4266552f7358SJed Brown   PetscFunctionBegin;
4267552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4268552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4269552f7358SJed Brown   a    = &array[off];
4270552f7358SJed Brown   if (!cdof || setBC) {
427197e99dd9SToby Isaac     if (clperm) {
427297e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
427397e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4274552f7358SJed Brown     } else {
427597e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
427697e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4277552f7358SJed Brown     }
4278552f7358SJed Brown   } else {
4279552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
428097e99dd9SToby Isaac     if (clperm) {
428197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4282552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
428397e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4284552f7358SJed Brown         }
4285552f7358SJed Brown       } else {
4286552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4287552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
428897e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
428997e99dd9SToby Isaac         }
429097e99dd9SToby Isaac       }
429197e99dd9SToby Isaac     } else {
429297e99dd9SToby Isaac       if (perm) {
429397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
429497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
429597e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
429697e99dd9SToby Isaac         }
429797e99dd9SToby Isaac       } else {
429897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
429997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
430097e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
430197e99dd9SToby Isaac         }
4302552f7358SJed Brown       }
4303552f7358SJed Brown     }
4304552f7358SJed Brown   }
4305552f7358SJed Brown   PetscFunctionReturn(0);
4306552f7358SJed Brown }
4307552f7358SJed Brown 
430897e99dd9SToby 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[])
4309a5e93ea8SMatthew G. Knepley {
4310a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4311a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4312a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4313a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4314a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4315a5e93ea8SMatthew G. Knepley 
4316a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4317a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4318a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4319a5e93ea8SMatthew G. Knepley   a    = &array[off];
4320a5e93ea8SMatthew G. Knepley   if (cdof) {
4321a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
432297e99dd9SToby Isaac     if (clperm) {
432397e99dd9SToby Isaac       if (perm) {
4324a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4325a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
432697e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
432797e99dd9SToby Isaac             cind++;
4328a5e93ea8SMatthew G. Knepley           }
4329a5e93ea8SMatthew G. Knepley         }
4330a5e93ea8SMatthew G. Knepley       } else {
4331a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4332a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
433397e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
433497e99dd9SToby Isaac             cind++;
433597e99dd9SToby Isaac           }
433697e99dd9SToby Isaac         }
433797e99dd9SToby Isaac       }
433897e99dd9SToby Isaac     } else {
433997e99dd9SToby Isaac       if (perm) {
434097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
434197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
434297e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
434397e99dd9SToby Isaac             cind++;
434497e99dd9SToby Isaac           }
434597e99dd9SToby Isaac         }
434697e99dd9SToby Isaac       } else {
434797e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
434897e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
434997e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
435097e99dd9SToby Isaac             cind++;
435197e99dd9SToby Isaac           }
4352a5e93ea8SMatthew G. Knepley         }
4353a5e93ea8SMatthew G. Knepley       }
4354a5e93ea8SMatthew G. Knepley     }
4355a5e93ea8SMatthew G. Knepley   }
4356a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4357a5e93ea8SMatthew G. Knepley }
4358a5e93ea8SMatthew G. Knepley 
435997e99dd9SToby 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[])
4360a6dfd86eSKarl Rupp {
4361552f7358SJed Brown   PetscScalar    *a;
43621a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
43631a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
436497e99dd9SToby Isaac   PetscInt        cind = 0, b;
4365552f7358SJed Brown   PetscErrorCode  ierr;
4366552f7358SJed Brown 
4367552f7358SJed Brown   PetscFunctionBegin;
4368552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4369552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
43701a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43711a271a75SMatthew G. Knepley   a    = &array[foff];
4372552f7358SJed Brown   if (!fcdof || setBC) {
437397e99dd9SToby Isaac     if (clperm) {
437497e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
437597e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4376552f7358SJed Brown     } else {
437797e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
437897e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4379552f7358SJed Brown     }
4380552f7358SJed Brown   } else {
4381552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
438297e99dd9SToby Isaac     if (clperm) {
438397e99dd9SToby Isaac       if (perm) {
438497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
438597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
438697e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4387552f7358SJed Brown         }
4388552f7358SJed Brown       } else {
438997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
439097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
439197e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
439297e99dd9SToby Isaac         }
439397e99dd9SToby Isaac       }
439497e99dd9SToby Isaac     } else {
439597e99dd9SToby Isaac       if (perm) {
439697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
439797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
439897e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
439997e99dd9SToby Isaac         }
440097e99dd9SToby Isaac       } else {
440197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
440297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
440397e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4404552f7358SJed Brown         }
4405552f7358SJed Brown       }
4406552f7358SJed Brown     }
4407552f7358SJed Brown   }
44081a271a75SMatthew G. Knepley   *offset += fdof;
4409552f7358SJed Brown   PetscFunctionReturn(0);
4410552f7358SJed Brown }
4411552f7358SJed Brown 
4412ba322698SMatthew 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[])
4413a5e93ea8SMatthew G. Knepley {
4414a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
44151a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
44161a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4417ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4418ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4419a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4420a5e93ea8SMatthew G. Knepley 
4421a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4422a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4423a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
44241a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
44251a271a75SMatthew G. Knepley   a    = &array[foff];
4426a5e93ea8SMatthew G. Knepley   if (fcdof) {
4427ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4428a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
442997e99dd9SToby Isaac     if (clperm) {
443097e99dd9SToby Isaac       if (perm) {
4431ba322698SMatthew G. Knepley         if (comps) {
4432ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4433ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4434ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4435ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4436ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4437ba322698SMatthew G. Knepley           }
4438ba322698SMatthew G. Knepley         } else {
443997e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
444097e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
444197e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4442a5e93ea8SMatthew G. Knepley               ++cind;
4443a5e93ea8SMatthew G. Knepley             }
4444a5e93ea8SMatthew G. Knepley           }
4445ba322698SMatthew G. Knepley         }
4446ba322698SMatthew G. Knepley       } else {
4447ba322698SMatthew G. Knepley         if (comps) {
4448ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4449ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4450ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4451ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4452ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4453ba322698SMatthew G. Knepley           }
4454a5e93ea8SMatthew G. Knepley         } else {
445597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
445697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
445797e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
445897e99dd9SToby Isaac               ++cind;
445997e99dd9SToby Isaac             }
446097e99dd9SToby Isaac           }
446197e99dd9SToby Isaac         }
4462ba322698SMatthew G. Knepley       }
446397e99dd9SToby Isaac     } else {
446497e99dd9SToby Isaac       if (perm) {
4465ba322698SMatthew G. Knepley         if (comps) {
4466ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4467ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4468ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4469ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4470ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4471ba322698SMatthew G. Knepley           }
4472ba322698SMatthew G. Knepley         } else {
447397e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
447497e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
447597e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
447697e99dd9SToby Isaac               ++cind;
447797e99dd9SToby Isaac             }
447897e99dd9SToby Isaac           }
4479ba322698SMatthew G. Knepley         }
4480ba322698SMatthew G. Knepley       } else {
4481ba322698SMatthew G. Knepley         if (comps) {
4482ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4483ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4484ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4485ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4486ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4487ba322698SMatthew G. Knepley           }
448897e99dd9SToby Isaac         } else {
448997e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
449097e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
449197e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4492a5e93ea8SMatthew G. Knepley               ++cind;
4493a5e93ea8SMatthew G. Knepley             }
4494a5e93ea8SMatthew G. Knepley           }
4495a5e93ea8SMatthew G. Knepley         }
4496a5e93ea8SMatthew G. Knepley       }
4497a5e93ea8SMatthew G. Knepley     }
4498ba322698SMatthew G. Knepley   }
44991a271a75SMatthew G. Knepley   *offset += fdof;
4500a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4501a5e93ea8SMatthew G. Knepley }
4502a5e93ea8SMatthew G. Knepley 
45038f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4504a6dfd86eSKarl Rupp {
4505552f7358SJed Brown   PetscScalar    *array;
45061b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
45071b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4508552f7358SJed Brown   PetscErrorCode  ierr;
4509552f7358SJed Brown 
45101b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4511b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4512b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4513b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4514b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4515b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4516b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4517b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4518b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4519b6ebb6e6SMatthew G. Knepley 
4520b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4521b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4522b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4523b6ebb6e6SMatthew G. Knepley     {
4524b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4525b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4526b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4527b6ebb6e6SMatthew G. Knepley 
4528b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4529b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4530b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4531b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4532b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4533b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4534b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4535b6ebb6e6SMatthew G. Knepley           }
4536b6ebb6e6SMatthew G. Knepley         } else {
4537b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4538b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4539b6ebb6e6SMatthew G. Knepley           }
4540b6ebb6e6SMatthew G. Knepley         }
4541b6ebb6e6SMatthew G. Knepley       } else {
4542b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4543b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4544b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4545b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4546b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4547b6ebb6e6SMatthew G. Knepley           }
4548b6ebb6e6SMatthew G. Knepley         } else {
4549b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4550b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4551b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4552b6ebb6e6SMatthew G. Knepley           }
4553b6ebb6e6SMatthew G. Knepley         }
4554b6ebb6e6SMatthew G. Knepley       }
4555b6ebb6e6SMatthew G. Knepley     }
4556b6ebb6e6SMatthew G. Knepley   }
4557b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4558b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4559b6ebb6e6SMatthew G. Knepley }
45601b406b76SMatthew G. Knepley 
45611b406b76SMatthew G. Knepley /*@C
45621b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
45631b406b76SMatthew G. Knepley 
45641b406b76SMatthew G. Knepley   Not collective
45651b406b76SMatthew G. Knepley 
45661b406b76SMatthew G. Knepley   Input Parameters:
45671b406b76SMatthew G. Knepley + dm - The DM
45681b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
45691b406b76SMatthew G. Knepley . v - The local vector
4570eaf898f9SPatrick Sanan . point - The point in the DM
45711b406b76SMatthew G. Knepley . values - The array of values
457222c1ee49SMatthew 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,
457322c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
45741b406b76SMatthew G. Knepley 
45751b406b76SMatthew G. Knepley   Fortran Notes:
45761b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
45771b406b76SMatthew G. Knepley 
45781b406b76SMatthew G. Knepley   Level: intermediate
45791b406b76SMatthew G. Knepley 
45801b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
45811b406b76SMatthew G. Knepley @*/
45821b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
45831b406b76SMatthew G. Knepley {
45841b406b76SMatthew G. Knepley   PetscSection    clSection;
45851b406b76SMatthew G. Knepley   IS              clPoints;
45861b406b76SMatthew G. Knepley   PetscScalar    *array;
45871b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
458897e99dd9SToby Isaac   const PetscInt *clp, *clperm;
45891a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
45901b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
45911b406b76SMatthew G. Knepley 
45921a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
45931b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45941b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
45951a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45961a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
45971b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
45981b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
45991b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
46008f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
46011b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
46021b406b76SMatthew G. Knepley   }
46031a271a75SMatthew G. Knepley   /* Get points */
460497e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4605923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
46061a271a75SMatthew G. Knepley   /* Get array */
4607552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
46081a271a75SMatthew G. Knepley   /* Get values */
4609ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
461097e99dd9SToby Isaac     PetscInt offset = 0, f;
4611552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
461297e99dd9SToby Isaac       const PetscInt    **perms = NULL;
461397e99dd9SToby Isaac       const PetscScalar **flips = NULL;
461497e99dd9SToby Isaac 
461597e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4616552f7358SJed Brown       switch (mode) {
4617552f7358SJed Brown       case INSERT_VALUES:
461897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
461997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
462097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
462197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
462297e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4623552f7358SJed Brown         } break;
4624552f7358SJed Brown       case INSERT_ALL_VALUES:
462597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
462697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
462797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
462897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
462997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4630552f7358SJed Brown         } break;
4631a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
463297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
463397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
463497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
463597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4636ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4637a5e93ea8SMatthew G. Knepley         } break;
4638552f7358SJed Brown       case ADD_VALUES:
463997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
464097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
464197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
464297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
464397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4644552f7358SJed Brown         } break;
4645552f7358SJed Brown       case ADD_ALL_VALUES:
464697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
464797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
464897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
464997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
465097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4651552f7358SJed Brown         } break;
4652304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
465397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
465497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
465597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
465697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4657ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4658304ab55fSMatthew G. Knepley         } break;
4659552f7358SJed Brown       default:
4660f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4661552f7358SJed Brown       }
466297e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
46631a271a75SMatthew G. Knepley     }
4664552f7358SJed Brown   } else {
46651a271a75SMatthew G. Knepley     PetscInt dof, off;
466697e99dd9SToby Isaac     const PetscInt    **perms = NULL;
466797e99dd9SToby Isaac     const PetscScalar **flips = NULL;
46681a271a75SMatthew G. Knepley 
466997e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4670552f7358SJed Brown     switch (mode) {
4671552f7358SJed Brown     case INSERT_VALUES:
467297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
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;
467697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
467797e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4678552f7358SJed Brown       } break;
4679552f7358SJed Brown     case INSERT_ALL_VALUES:
468097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
468197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
468297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
468397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
468497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
468597e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4686552f7358SJed Brown       } break;
4687a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
468897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
468997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
469097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
469197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
469297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
469397e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4694a5e93ea8SMatthew G. Knepley       } break;
4695552f7358SJed Brown     case ADD_VALUES:
469697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
469797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
469897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
469997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
470097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
470197e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4702552f7358SJed Brown       } break;
4703552f7358SJed Brown     case ADD_ALL_VALUES:
470497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
470597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
470697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
470797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
470897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
470997e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4710552f7358SJed Brown       } break;
4711304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
471297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
471397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
471497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
471597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
471697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
471797e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4718304ab55fSMatthew G. Knepley       } break;
4719552f7358SJed Brown     default:
4720f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4721552f7358SJed Brown     }
472297e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4723552f7358SJed Brown   }
47241a271a75SMatthew G. Knepley   /* Cleanup points */
4725923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
47261a271a75SMatthew G. Knepley   /* Cleanup array */
4727552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4728552f7358SJed Brown   PetscFunctionReturn(0);
4729552f7358SJed Brown }
4730552f7358SJed Brown 
4731ba322698SMatthew 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)
4732e07394fbSMatthew G. Knepley {
4733e07394fbSMatthew G. Knepley   PetscSection      clSection;
4734e07394fbSMatthew G. Knepley   IS                clPoints;
4735e07394fbSMatthew G. Knepley   PetscScalar       *array;
4736e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4737b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4738e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
473997e99dd9SToby Isaac   PetscInt          offset = 0, f;
4740e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4741e07394fbSMatthew G. Knepley 
4742e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4743e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4744e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4745e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4746e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4747e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4748e07394fbSMatthew G. Knepley   /* Get points */
4749b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4750923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4751e07394fbSMatthew G. Knepley   /* Get array */
4752e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4753e07394fbSMatthew G. Knepley   /* Get values */
4754e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
475597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
475697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
475797e99dd9SToby Isaac 
4758e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4759e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4760e07394fbSMatthew G. Knepley         PetscInt fdof;
4761e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4762e07394fbSMatthew G. Knepley         offset += fdof;
4763e07394fbSMatthew G. Knepley       }
4764e07394fbSMatthew G. Knepley       continue;
4765e07394fbSMatthew G. Knepley     }
476697e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4767e07394fbSMatthew G. Knepley     switch (mode) {
4768e07394fbSMatthew G. Knepley     case INSERT_VALUES:
476997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
477097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
477197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
477297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4773b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4774e07394fbSMatthew G. Knepley       } break;
4775e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
477697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
477797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
477897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
477997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4780b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4781e07394fbSMatthew G. Knepley         } break;
4782e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
478397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
478497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
478597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
478697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4787ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
4788e07394fbSMatthew G. Knepley       } break;
4789e07394fbSMatthew G. Knepley     case ADD_VALUES:
479097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
479197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
479297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
479397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4794b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4795e07394fbSMatthew G. Knepley       } break;
4796e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
479797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
479897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
479997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
480097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4801b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4802e07394fbSMatthew G. Knepley       } break;
4803e07394fbSMatthew G. Knepley     default:
4804f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4805e07394fbSMatthew G. Knepley     }
480697e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4807e07394fbSMatthew G. Knepley   }
4808e07394fbSMatthew G. Knepley   /* Cleanup points */
4809923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4810e07394fbSMatthew G. Knepley   /* Cleanup array */
4811e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4812e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4813e07394fbSMatthew G. Knepley }
4814e07394fbSMatthew G. Knepley 
48157cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4816552f7358SJed Brown {
4817552f7358SJed Brown   PetscMPIInt    rank;
4818552f7358SJed Brown   PetscInt       i, j;
4819552f7358SJed Brown   PetscErrorCode ierr;
4820552f7358SJed Brown 
4821552f7358SJed Brown   PetscFunctionBegin;
482282f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4823eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
4824e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4825e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4826b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4827b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4828e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4829b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4830519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
48317eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4832552f7358SJed Brown #else
4833b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4834552f7358SJed Brown #endif
4835552f7358SJed Brown     }
483677a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4837552f7358SJed Brown   }
4838552f7358SJed Brown   PetscFunctionReturn(0);
4839552f7358SJed Brown }
4840552f7358SJed Brown 
4841552f7358SJed Brown /* . off - The global offset of this point */
48424acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4843a6dfd86eSKarl Rupp {
4844e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4845552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4846552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4847552f7358SJed Brown   PetscInt        cind = 0, k;
4848552f7358SJed Brown   PetscErrorCode  ierr;
4849552f7358SJed Brown 
4850552f7358SJed Brown   PetscFunctionBegin;
4851552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4852552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4853552f7358SJed Brown   if (!cdof || setBC) {
48544acb8e1eSToby Isaac     if (perm) {
48554acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4856552f7358SJed Brown     } else {
48574acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4858552f7358SJed Brown     }
4859552f7358SJed Brown   } else {
4860552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
48614acb8e1eSToby Isaac     if (perm) {
48624acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
48634acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
48644acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
48654acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
48664acb8e1eSToby Isaac           ++cind;
48674acb8e1eSToby Isaac         } else {
48684acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
48694acb8e1eSToby Isaac         }
48704acb8e1eSToby Isaac       }
48714acb8e1eSToby Isaac     } else {
4872552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4873552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4874552f7358SJed Brown           /* Insert check for returning constrained indices */
4875e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4876552f7358SJed Brown           ++cind;
4877552f7358SJed Brown         } else {
4878e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4879552f7358SJed Brown         }
4880552f7358SJed Brown       }
4881552f7358SJed Brown     }
4882552f7358SJed Brown   }
4883e6ccafaeSMatthew G Knepley   *loff += dof;
4884552f7358SJed Brown   PetscFunctionReturn(0);
4885552f7358SJed Brown }
4886552f7358SJed Brown 
4887552f7358SJed Brown /* . off - The global offset of this point */
48884acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4889a6dfd86eSKarl Rupp {
4890552f7358SJed Brown   PetscInt       numFields, foff, f;
4891552f7358SJed Brown   PetscErrorCode ierr;
4892552f7358SJed Brown 
4893552f7358SJed Brown   PetscFunctionBegin;
4894552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4895552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
48964acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4897552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
48984acb8e1eSToby Isaac     PetscInt        cind = 0, b;
48994acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4900552f7358SJed Brown 
4901552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4902552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4903552f7358SJed Brown     if (!cfdof || setBC) {
49044acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
49054acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4906552f7358SJed Brown     } else {
4907552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
49084acb8e1eSToby Isaac       if (perm) {
49094acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
49104acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
49114acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4912552f7358SJed Brown             ++cind;
4913552f7358SJed Brown           } else {
49144acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4915552f7358SJed Brown           }
4916552f7358SJed Brown         }
4917552f7358SJed Brown       } else {
49184acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
49194acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
49204acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4921552f7358SJed Brown             ++cind;
4922552f7358SJed Brown           } else {
49234acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4924552f7358SJed Brown           }
4925552f7358SJed Brown         }
4926552f7358SJed Brown       }
4927552f7358SJed Brown     }
4928a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4929552f7358SJed Brown     foffs[f] += fdof;
4930552f7358SJed Brown   }
4931552f7358SJed Brown   PetscFunctionReturn(0);
4932552f7358SJed Brown }
4933552f7358SJed Brown 
49344acb8e1eSToby 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)
4935d3d1a6afSToby Isaac {
4936d3d1a6afSToby Isaac   Mat             cMat;
4937d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4938d3d1a6afSToby Isaac   IS              aIS;
4939d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4940d3d1a6afSToby Isaac   const PetscInt  *anchors;
4941e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4942d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4943d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4944d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4945d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4946d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4947d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4948d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
49496ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4950d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4951d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4952d3d1a6afSToby Isaac 
4953d3d1a6afSToby Isaac   PetscFunctionBegin;
4954d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4955d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4956d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4957d3d1a6afSToby Isaac 
4958a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4959d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4960d3d1a6afSToby Isaac   if (aSec) {
4961d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4962d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4963d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4964d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4965d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4966d3d1a6afSToby Isaac      * into the global matrix anyway) */
4967d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4968d3d1a6afSToby Isaac       PetscInt b    = points[p];
49694b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4970d3d1a6afSToby Isaac 
49714b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
49724b2f2278SToby Isaac       if (!bSecDof) {
49734b2f2278SToby Isaac         continue;
49744b2f2278SToby Isaac       }
4975d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4976d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4977d3d1a6afSToby Isaac       }
4978d3d1a6afSToby Isaac       if (bDof) {
4979d3d1a6afSToby Isaac         /* this point is constrained */
4980d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4981d3d1a6afSToby Isaac         PetscInt bOff, q;
4982d3d1a6afSToby Isaac 
4983d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4984d3d1a6afSToby Isaac         newNumPoints  += bDof;
4985d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4986d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4987d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4988d3d1a6afSToby Isaac           PetscInt aDof;
4989d3d1a6afSToby Isaac 
4990d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4991d3d1a6afSToby Isaac           newNumIndices += aDof;
4992d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4993d3d1a6afSToby Isaac             PetscInt fDof;
4994d3d1a6afSToby Isaac 
4995d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4996d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4997d3d1a6afSToby Isaac           }
4998d3d1a6afSToby Isaac         }
4999d3d1a6afSToby Isaac       }
5000d3d1a6afSToby Isaac       else {
5001d3d1a6afSToby Isaac         /* this point is not constrained */
5002d3d1a6afSToby Isaac         newNumPoints++;
50034b2f2278SToby Isaac         newNumIndices += bSecDof;
5004d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5005d3d1a6afSToby Isaac           PetscInt fDof;
5006d3d1a6afSToby Isaac 
5007d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5008d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5009d3d1a6afSToby Isaac         }
5010d3d1a6afSToby Isaac       }
5011d3d1a6afSToby Isaac     }
5012d3d1a6afSToby Isaac   }
5013d3d1a6afSToby Isaac   if (!anyConstrained) {
501472b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
501572b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
501672b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
501772b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
501872b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5019d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5020d3d1a6afSToby Isaac   }
5021d3d1a6afSToby Isaac 
50226ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
50236ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
50246ecaa68aSToby Isaac 
5025f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5026d3d1a6afSToby Isaac 
50276ecaa68aSToby Isaac   if (!outPoints && !outValues) {
50286ecaa68aSToby Isaac     if (offsets) {
50296ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
50306ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
50316ecaa68aSToby Isaac       }
50326ecaa68aSToby Isaac     }
50336ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
50346ecaa68aSToby Isaac     PetscFunctionReturn(0);
50356ecaa68aSToby Isaac   }
50366ecaa68aSToby Isaac 
5037f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5038d3d1a6afSToby Isaac 
5039f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5040d3d1a6afSToby Isaac 
5041d3d1a6afSToby Isaac   /* workspaces */
5042d3d1a6afSToby Isaac   if (numFields) {
5043d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
504469291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
504569291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5046d3d1a6afSToby Isaac     }
5047d3d1a6afSToby Isaac   }
5048d3d1a6afSToby Isaac   else {
504969291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
505069291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5051d3d1a6afSToby Isaac   }
5052d3d1a6afSToby Isaac 
5053d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5054d3d1a6afSToby Isaac   if (numFields) {
50554b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
50564b2f2278SToby Isaac 
5057d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5058d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
50594b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5060d3d1a6afSToby Isaac 
50614b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
50624b2f2278SToby Isaac       if (!bSecDof) {
50634b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
50644b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
50654b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
50664b2f2278SToby Isaac         }
50674b2f2278SToby Isaac         continue;
50684b2f2278SToby Isaac       }
5069d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5070d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5071d3d1a6afSToby Isaac       }
5072d3d1a6afSToby Isaac       if (bDof) {
5073d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5074d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5075d3d1a6afSToby Isaac 
5076d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5077d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5078d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5079d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5080d3d1a6afSToby Isaac             PetscInt aFDof;
5081d3d1a6afSToby Isaac 
5082d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5083d3d1a6afSToby Isaac             allFDof += aFDof;
5084d3d1a6afSToby Isaac           }
5085d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5086d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5087d3d1a6afSToby Isaac         }
5088d3d1a6afSToby Isaac       }
5089d3d1a6afSToby Isaac       else {
5090d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5091d3d1a6afSToby Isaac           PetscInt fDof;
5092d3d1a6afSToby Isaac 
5093d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5094d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5095d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5096d3d1a6afSToby Isaac         }
5097d3d1a6afSToby Isaac       }
5098d3d1a6afSToby Isaac     }
50994b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
51004b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
51014b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5102d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5103d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5104d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5105d3d1a6afSToby Isaac       }
510619f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
510719f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
510869291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5109d3d1a6afSToby Isaac     }
5110d3d1a6afSToby Isaac   }
5111d3d1a6afSToby Isaac   else {
5112d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5113d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
51144b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5115d3d1a6afSToby Isaac 
51164b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
51174b2f2278SToby Isaac       if (!bSecDof) {
51184b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
51194b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
51204b2f2278SToby Isaac         continue;
51214b2f2278SToby Isaac       }
5122d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5123d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5124d3d1a6afSToby Isaac       }
5125d3d1a6afSToby Isaac       if (bDof) {
51264b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5127d3d1a6afSToby Isaac 
5128d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5129d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5130d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5131d3d1a6afSToby Isaac 
5132d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5133d3d1a6afSToby Isaac           allDof += aDof;
5134d3d1a6afSToby Isaac         }
5135d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
51364b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5137d3d1a6afSToby Isaac       }
5138d3d1a6afSToby Isaac       else {
51394b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5140d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5141d3d1a6afSToby Isaac       }
5142d3d1a6afSToby Isaac     }
5143d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5144d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5145d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5146d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5147d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5148d3d1a6afSToby Isaac     }
514969291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5150d3d1a6afSToby Isaac   }
5151d3d1a6afSToby Isaac 
51526ecaa68aSToby Isaac   /* output arrays */
515369291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
51546ecaa68aSToby Isaac 
5155d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5156d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5157d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
515869291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
515969291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5160d3d1a6afSToby Isaac   if (numFields) {
5161d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5162d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5163d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
51644b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5165d3d1a6afSToby Isaac 
51664b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
51674b2f2278SToby Isaac       if (!bSecDof) {
51684b2f2278SToby Isaac         continue;
51694b2f2278SToby Isaac       }
5170d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5171d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5172d3d1a6afSToby Isaac       }
5173d3d1a6afSToby Isaac       if (bDof) {
5174d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5175d3d1a6afSToby Isaac 
5176d3d1a6afSToby Isaac         fStart[0] = 0;
5177d3d1a6afSToby Isaac         fEnd[0]   = 0;
5178d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5179d3d1a6afSToby Isaac           PetscInt fDof;
5180d3d1a6afSToby Isaac 
5181d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5182d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5183d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5184d3d1a6afSToby Isaac         }
5185d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
51864acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
5187d3d1a6afSToby Isaac 
5188d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5189d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5190d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5191d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5192d3d1a6afSToby Isaac 
5193d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5194d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5195d3d1a6afSToby Isaac         }
5196d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5197d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5198d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5199d3d1a6afSToby Isaac 
5200d3d1a6afSToby 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 */
5201d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5202d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5203302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
52044acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
5205d3d1a6afSToby Isaac         }
5206d3d1a6afSToby Isaac         newP += bDof;
5207d3d1a6afSToby Isaac 
52086ecaa68aSToby Isaac         if (outValues) {
5209d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5210d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5211d3d1a6afSToby 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);
5212d3d1a6afSToby Isaac           }
5213d3d1a6afSToby Isaac         }
52146ecaa68aSToby Isaac       }
5215d3d1a6afSToby Isaac       else {
5216d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5217d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5218d3d1a6afSToby Isaac         newP++;
5219d3d1a6afSToby Isaac       }
5220d3d1a6afSToby Isaac     }
5221d3d1a6afSToby Isaac   } else {
5222d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5223d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5224d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
52254b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5226d3d1a6afSToby Isaac 
52274b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
52284b2f2278SToby Isaac       if (!bSecDof) {
52294b2f2278SToby Isaac         continue;
52304b2f2278SToby Isaac       }
5231d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5232d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5233d3d1a6afSToby Isaac       }
5234d3d1a6afSToby Isaac       if (bDof) {
5235d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5236d3d1a6afSToby Isaac 
5237d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
52384acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
5239d3d1a6afSToby Isaac 
5240d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5241d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5242d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5243d3d1a6afSToby Isaac 
5244d3d1a6afSToby 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 */
5245d3d1a6afSToby Isaac 
5246d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5247d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5248302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
52494acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
5250d3d1a6afSToby Isaac         }
5251d3d1a6afSToby Isaac         newP += bDof;
5252d3d1a6afSToby Isaac 
5253d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
52546ecaa68aSToby Isaac         if (outValues) {
5255d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5256d3d1a6afSToby Isaac         }
52576ecaa68aSToby Isaac       }
5258d3d1a6afSToby Isaac       else {
5259d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5260d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5261d3d1a6afSToby Isaac         newP++;
5262d3d1a6afSToby Isaac       }
5263d3d1a6afSToby Isaac     }
5264d3d1a6afSToby Isaac   }
5265d3d1a6afSToby Isaac 
52666ecaa68aSToby Isaac   if (outValues) {
526769291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5268d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5269d3d1a6afSToby Isaac     /* multiply constraints on the right */
5270d3d1a6afSToby Isaac     if (numFields) {
5271d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5272d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5273d3d1a6afSToby Isaac 
5274d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5275d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5276d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5277d3d1a6afSToby Isaac           PetscInt c, r, k;
5278d3d1a6afSToby Isaac           PetscInt dof;
5279d3d1a6afSToby Isaac 
5280d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
52814b2f2278SToby Isaac           if (!dof) {
52824b2f2278SToby Isaac             continue;
52834b2f2278SToby Isaac           }
5284d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5285d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5286d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5287d3d1a6afSToby Isaac 
5288d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5289d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5290d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
52914acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5292d3d1a6afSToby Isaac                 }
5293d3d1a6afSToby Isaac               }
5294d3d1a6afSToby Isaac             }
5295d3d1a6afSToby Isaac           }
5296d3d1a6afSToby Isaac           else {
5297d3d1a6afSToby Isaac             /* copy this column as is */
5298d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5299d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5300d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5301d3d1a6afSToby Isaac               }
5302d3d1a6afSToby Isaac             }
5303d3d1a6afSToby Isaac           }
5304d3d1a6afSToby Isaac           oldOff += dof;
5305d3d1a6afSToby Isaac         }
5306d3d1a6afSToby Isaac       }
5307d3d1a6afSToby Isaac     }
5308d3d1a6afSToby Isaac     else {
5309d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5310d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5311d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5312d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5313d3d1a6afSToby Isaac         PetscInt c, r, k;
5314d3d1a6afSToby Isaac         PetscInt dof;
5315d3d1a6afSToby Isaac 
5316d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
53174b2f2278SToby Isaac         if (!dof) {
53184b2f2278SToby Isaac           continue;
53194b2f2278SToby Isaac         }
5320d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5321d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5322d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5323d3d1a6afSToby Isaac 
5324d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5325d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5326d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5327d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5328d3d1a6afSToby Isaac               }
5329d3d1a6afSToby Isaac             }
5330d3d1a6afSToby Isaac           }
5331d3d1a6afSToby Isaac         }
5332d3d1a6afSToby Isaac         else {
5333d3d1a6afSToby Isaac           /* copy this column as is */
5334d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5335d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5336d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5337d3d1a6afSToby Isaac             }
5338d3d1a6afSToby Isaac           }
5339d3d1a6afSToby Isaac         }
5340d3d1a6afSToby Isaac         oldOff += dof;
5341d3d1a6afSToby Isaac       }
5342d3d1a6afSToby Isaac     }
5343d3d1a6afSToby Isaac 
53446ecaa68aSToby Isaac     if (multiplyLeft) {
534569291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5346d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5347d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5348d3d1a6afSToby Isaac       if (numFields) {
5349d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5350d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5351d3d1a6afSToby Isaac 
5352d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5353d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5354d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5355d3d1a6afSToby Isaac             PetscInt c, r, k;
5356d3d1a6afSToby Isaac             PetscInt dof;
5357d3d1a6afSToby Isaac 
5358d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5359d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5360d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5361d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5362d3d1a6afSToby Isaac 
5363d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5364d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5365d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5366d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5367d3d1a6afSToby Isaac                   }
5368d3d1a6afSToby Isaac                 }
5369d3d1a6afSToby Isaac               }
5370d3d1a6afSToby Isaac             }
5371d3d1a6afSToby Isaac             else {
5372d3d1a6afSToby Isaac               /* copy this row as is */
5373d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5374d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5375d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5376d3d1a6afSToby Isaac                 }
5377d3d1a6afSToby Isaac               }
5378d3d1a6afSToby Isaac             }
5379d3d1a6afSToby Isaac             oldOff += dof;
5380d3d1a6afSToby Isaac           }
5381d3d1a6afSToby Isaac         }
5382d3d1a6afSToby Isaac       }
5383d3d1a6afSToby Isaac       else {
5384d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5385d3d1a6afSToby Isaac 
5386d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5387d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5388d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5389d3d1a6afSToby Isaac           PetscInt c, r, k;
5390d3d1a6afSToby Isaac           PetscInt dof;
5391d3d1a6afSToby Isaac 
5392d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5393d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5394d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5395d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5396d3d1a6afSToby Isaac 
5397d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5398d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5399d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5400d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5401d3d1a6afSToby Isaac                 }
5402d3d1a6afSToby Isaac               }
5403d3d1a6afSToby Isaac             }
5404d3d1a6afSToby Isaac           }
5405d3d1a6afSToby Isaac           else {
5406d3d1a6afSToby Isaac             /* copy this row as is */
54079fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5408d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5409d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5410d3d1a6afSToby Isaac               }
5411d3d1a6afSToby Isaac             }
5412d3d1a6afSToby Isaac           }
5413d3d1a6afSToby Isaac           oldOff += dof;
5414d3d1a6afSToby Isaac         }
5415d3d1a6afSToby Isaac       }
5416d3d1a6afSToby Isaac 
541769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
54186ecaa68aSToby Isaac     }
54196ecaa68aSToby Isaac     else {
54206ecaa68aSToby Isaac       newValues = tmpValues;
54216ecaa68aSToby Isaac     }
54226ecaa68aSToby Isaac   }
54236ecaa68aSToby Isaac 
5424d3d1a6afSToby Isaac   /* clean up */
542569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
542669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
54276ecaa68aSToby Isaac 
5428d3d1a6afSToby Isaac   if (numFields) {
5429d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
543069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
543169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
543269291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5433d3d1a6afSToby Isaac     }
5434d3d1a6afSToby Isaac   }
5435d3d1a6afSToby Isaac   else {
543669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
543769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
543869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5439d3d1a6afSToby Isaac   }
5440d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5441d3d1a6afSToby Isaac 
5442d3d1a6afSToby Isaac   /* output */
54436ecaa68aSToby Isaac   if (outPoints) {
5444d3d1a6afSToby Isaac     *outPoints = newPoints;
54456ecaa68aSToby Isaac   }
54466ecaa68aSToby Isaac   else {
544769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
54486ecaa68aSToby Isaac   }
544931620726SToby Isaac   if (outValues) {
5450d3d1a6afSToby Isaac     *outValues = newValues;
54516ecaa68aSToby Isaac   }
54526ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5453d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5454d3d1a6afSToby Isaac   }
5455d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5456d3d1a6afSToby Isaac }
5457d3d1a6afSToby Isaac 
54587cd05799SMatthew G. Knepley /*@C
5459a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
54607cd05799SMatthew G. Knepley 
54617cd05799SMatthew G. Knepley   Not collective
54627cd05799SMatthew G. Knepley 
54637cd05799SMatthew G. Knepley   Input Parameters:
54647cd05799SMatthew G. Knepley + dm - The DM
54657cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
54667cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
54677cd05799SMatthew G. Knepley - point - The mesh point
54687cd05799SMatthew G. Knepley 
54697cd05799SMatthew G. Knepley   Output parameters:
54707cd05799SMatthew G. Knepley + numIndices - The number of indices
54717cd05799SMatthew G. Knepley . indices - The indices
54727cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
54737cd05799SMatthew G. Knepley 
54747cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
54757cd05799SMatthew G. Knepley 
54767cd05799SMatthew G. Knepley   Level: advanced
54777cd05799SMatthew G. Knepley 
54787cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
54797cd05799SMatthew G. Knepley @*/
54806ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
54817773e69fSMatthew G. Knepley {
54827773e69fSMatthew G. Knepley   PetscSection    clSection;
54837773e69fSMatthew G. Knepley   IS              clPoints;
54847773e69fSMatthew G. Knepley   const PetscInt *clp;
54854acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
54867773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
54877773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
54887773e69fSMatthew G. Knepley   PetscInt        offsets[32];
54897773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
54907773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
54917773e69fSMatthew G. Knepley 
54927773e69fSMatthew G. Knepley   PetscFunctionBegin;
54937773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54947773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
54957773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
54967773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
54977773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
54987773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
54997773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
55007773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
55017773e69fSMatthew G. Knepley   /* Get points in closure */
5502923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55037773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
55047773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
55057773e69fSMatthew G. Knepley     PetscInt dof, fdof;
55067773e69fSMatthew G. Knepley 
55077773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
55087773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
55097773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
55107773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
55117773e69fSMatthew G. Knepley     }
55127773e69fSMatthew G. Knepley     Nind += dof;
55137773e69fSMatthew G. Knepley   }
55147773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
55158ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
55164acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
55174acb8e1eSToby Isaac   /* Get dual space symmetries */
55184acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
55194acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55204acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55214acb8e1eSToby Isaac   }
55227773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
55237773e69fSMatthew G. Knepley   {
55244acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
55257773e69fSMatthew G. Knepley     if (numPointsNew) {
55264acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
55274acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55284acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55294acb8e1eSToby Isaac       }
55304acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
55314acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
55324acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
55334acb8e1eSToby Isaac       }
5534923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55357773e69fSMatthew G. Knepley       numPoints = numPointsNew;
55367773e69fSMatthew G. Knepley       Nind      = NindNew;
55377773e69fSMatthew G. Knepley       points    = pointsNew;
55387773e69fSMatthew G. Knepley     }
55397773e69fSMatthew G. Knepley   }
55407773e69fSMatthew G. Knepley   /* Calculate indices */
554169291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
55427773e69fSMatthew G. Knepley   if (Nf) {
55436ecaa68aSToby Isaac     if (outOffsets) {
55446ecaa68aSToby Isaac       PetscInt f;
55456ecaa68aSToby Isaac 
554646bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
55476ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
55486ecaa68aSToby Isaac       }
55496ecaa68aSToby Isaac     }
55504acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
55514acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55524acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
55537773e69fSMatthew G. Knepley     }
55547773e69fSMatthew G. Knepley   } else {
55554acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
55564acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
55574acb8e1eSToby Isaac 
55584acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55594acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
55607773e69fSMatthew G. Knepley     }
55617773e69fSMatthew G. Knepley   }
55627773e69fSMatthew G. Knepley   /* Cleanup points */
55634acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
55644acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55654acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
55664acb8e1eSToby Isaac   }
55677773e69fSMatthew G. Knepley   if (numPointsNew) {
556869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
55697773e69fSMatthew G. Knepley   } else {
5570923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
55717773e69fSMatthew G. Knepley   }
55727773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
55737773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
55747773e69fSMatthew G. Knepley }
55757773e69fSMatthew G. Knepley 
55767cd05799SMatthew G. Knepley /*@C
55777cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
55787cd05799SMatthew G. Knepley 
55797cd05799SMatthew G. Knepley   Not collective
55807cd05799SMatthew G. Knepley 
55817cd05799SMatthew G. Knepley   Input Parameters:
55827cd05799SMatthew G. Knepley + dm - The DM
55837cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
55847cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
55857cd05799SMatthew G. Knepley . point - The mesh point
55867cd05799SMatthew G. Knepley . numIndices - The number of indices
55877cd05799SMatthew G. Knepley . indices - The indices
55887cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
55897cd05799SMatthew G. Knepley 
55907cd05799SMatthew G. Knepley   Level: advanced
55917cd05799SMatthew G. Knepley 
55927cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
55937cd05799SMatthew G. Knepley @*/
559446bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
55957773e69fSMatthew G. Knepley {
55967773e69fSMatthew G. Knepley   PetscErrorCode ierr;
55977773e69fSMatthew G. Knepley 
55987773e69fSMatthew G. Knepley   PetscFunctionBegin;
55997773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56007773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
560169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
56027773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
56037773e69fSMatthew G. Knepley }
56047773e69fSMatthew G. Knepley 
56057f5d1fdeSMatthew G. Knepley /*@C
56067f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
56077f5d1fdeSMatthew G. Knepley 
56087f5d1fdeSMatthew G. Knepley   Not collective
56097f5d1fdeSMatthew G. Knepley 
56107f5d1fdeSMatthew G. Knepley   Input Parameters:
56117f5d1fdeSMatthew G. Knepley + dm - The DM
5612ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5613ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
56147f5d1fdeSMatthew G. Knepley . A - The matrix
5615eaf898f9SPatrick Sanan . point - The point in the DM
56167f5d1fdeSMatthew G. Knepley . values - The array of values
56177f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
56187f5d1fdeSMatthew G. Knepley 
56197f5d1fdeSMatthew G. Knepley   Fortran Notes:
56207f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
56217f5d1fdeSMatthew G. Knepley 
56227f5d1fdeSMatthew G. Knepley   Level: intermediate
56237f5d1fdeSMatthew G. Knepley 
56247f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
56257f5d1fdeSMatthew G. Knepley @*/
56267c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5627552f7358SJed Brown {
5628552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
56291b406b76SMatthew G. Knepley   PetscSection        clSection;
56301b406b76SMatthew G. Knepley   IS                  clPoints;
5631d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
56321b406b76SMatthew G. Knepley   const PetscInt     *clp;
5633552f7358SJed Brown   PetscInt           *indices;
5634552f7358SJed Brown   PetscInt            offsets[32];
56354acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
56364acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5637923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
56384acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5639d3d1a6afSToby Isaac   PetscScalar        *newValues;
5640552f7358SJed Brown   PetscErrorCode      ierr;
5641552f7358SJed Brown 
5642552f7358SJed Brown   PetscFunctionBegin;
5643552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5644ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
56453dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5646ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
56473dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
56483dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5649552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
565082f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5651552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5652923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5653552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5654552f7358SJed Brown     PetscInt fdof;
5655552f7358SJed Brown 
5656552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5657552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5658552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5659552f7358SJed Brown       offsets[f+1] += fdof;
5660552f7358SJed Brown     }
5661552f7358SJed Brown     numIndices += dof;
5662552f7358SJed Brown   }
56630d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
56640d644c17SKarl Rupp 
56658ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
56664acb8e1eSToby Isaac   /* Get symmetries */
56674acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
56684acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56694acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
56704acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
56714acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
56724acb8e1eSToby Isaac 
56734acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
56744acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
56754acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
56764acb8e1eSToby Isaac 
56774acb8e1eSToby Isaac         if (!numFields) {
56784acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
56794acb8e1eSToby Isaac         } else {
56804acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
56814acb8e1eSToby Isaac         }
56824acb8e1eSToby Isaac         if (flip) {
56834acb8e1eSToby Isaac           PetscInt i, j, k;
56844acb8e1eSToby Isaac 
56854acb8e1eSToby Isaac           if (!valCopy) {
568669291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
56874acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
56884acb8e1eSToby Isaac             values = valCopy;
56894acb8e1eSToby Isaac           }
56904acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5691775f7be2SToby Isaac             PetscScalar fval = flip[i];
56924acb8e1eSToby Isaac 
56934acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
56944acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
56954acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
56964acb8e1eSToby Isaac             }
56974acb8e1eSToby Isaac           }
56984acb8e1eSToby Isaac         }
56994acb8e1eSToby Isaac         foffset += fdof;
57004acb8e1eSToby Isaac       }
57014acb8e1eSToby Isaac     }
57024acb8e1eSToby Isaac   }
57034acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5704d3d1a6afSToby Isaac   if (newNumPoints) {
57054acb8e1eSToby Isaac     if (valCopy) {
570669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
57074acb8e1eSToby Isaac     }
57084acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
57094acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57104acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57114acb8e1eSToby Isaac     }
57124acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
57134acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
57144acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
57154acb8e1eSToby Isaac     }
5716923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5717d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5718d3d1a6afSToby Isaac     numIndices = newNumIndices;
5719d3d1a6afSToby Isaac     points     = newPoints;
5720d3d1a6afSToby Isaac     values     = newValues;
5721d3d1a6afSToby Isaac   }
572269291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5723552f7358SJed Brown   if (numFields) {
57244acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
57254acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
57264acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5727552f7358SJed Brown     }
5728552f7358SJed Brown   } else {
57294acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
57304acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
57314acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
57324acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5733552f7358SJed Brown     }
5734552f7358SJed Brown   }
5735b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5736552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5737ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5738ab1d0545SMatthew G. Knepley     PetscInt i;
5739ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
57408ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5741ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5742ab1d0545SMatthew G. Knepley   }
5743552f7358SJed Brown   if (ierr) {
5744552f7358SJed Brown     PetscMPIInt    rank;
5745552f7358SJed Brown     PetscErrorCode ierr2;
5746552f7358SJed Brown 
574782f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5748e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5749b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
575069291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
5751552f7358SJed Brown     CHKERRQ(ierr);
5752552f7358SJed Brown   }
57534acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
57544acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57554acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
57564acb8e1eSToby Isaac   }
5757d3d1a6afSToby Isaac   if (newNumPoints) {
575869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
575969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
5760d3d1a6afSToby Isaac   }
5761d3d1a6afSToby Isaac   else {
57624acb8e1eSToby Isaac     if (valCopy) {
576369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
57644acb8e1eSToby Isaac     }
5765923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5766d3d1a6afSToby Isaac   }
576769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
5768552f7358SJed Brown   PetscFunctionReturn(0);
5769552f7358SJed Brown }
5770552f7358SJed Brown 
5771de41b84cSMatthew 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)
5772de41b84cSMatthew G. Knepley {
5773de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5774de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5775de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5776de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5777de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5778de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
57794ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5780de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5781de41b84cSMatthew G. Knepley 
5782de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5783de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5784de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5785de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5786de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5787de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5788de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5789de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5790de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5791de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5792de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5793de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5794de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5795de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5796de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5797de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5798de41b84cSMatthew G. Knepley   /* Column indices */
5799de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
58004ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5801de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5802de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5803de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5804de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5805de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5806de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5807de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5808de41b84cSMatthew G. Knepley       ++q;
5809de41b84cSMatthew G. Knepley     }
5810de41b84cSMatthew G. Knepley   }
5811de41b84cSMatthew G. Knepley   numCPoints = q;
5812de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5813de41b84cSMatthew G. Knepley     PetscInt fdof;
5814de41b84cSMatthew G. Knepley 
5815de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
58164ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5817de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5818de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5819de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5820de41b84cSMatthew G. Knepley     }
5821de41b84cSMatthew G. Knepley     numCIndices += dof;
5822de41b84cSMatthew G. Knepley   }
5823de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5824de41b84cSMatthew G. Knepley   /* Row indices */
5825de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5826de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
582769291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5828de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5829de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5830de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5831de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5832de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5833de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5834de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
58354ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
58364ca5e9f5SMatthew G. Knepley         if (!dof) continue;
58374ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
58384ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5839de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5840de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5841de41b84cSMatthew G. Knepley         ++q;
5842de41b84cSMatthew G. Knepley       }
5843de41b84cSMatthew G. Knepley     }
5844de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5845de41b84cSMatthew G. Knepley   }
5846de41b84cSMatthew G. Knepley   numFPoints = q;
5847de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5848de41b84cSMatthew G. Knepley     PetscInt fdof;
5849de41b84cSMatthew G. Knepley 
5850de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
58514ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5852de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5853de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5854de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5855de41b84cSMatthew G. Knepley     }
5856de41b84cSMatthew G. Knepley     numFIndices += dof;
5857de41b84cSMatthew G. Knepley   }
5858de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5859de41b84cSMatthew G. Knepley 
58608ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
58618ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
586269291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
586369291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5864de41b84cSMatthew G. Knepley   if (numFields) {
58654acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
58664acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
58674acb8e1eSToby Isaac 
58684acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58694acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58704acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5871de41b84cSMatthew G. Knepley     }
58724acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
58734acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
58744acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
58754acb8e1eSToby Isaac     }
58764acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
58774acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
58784acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
58794acb8e1eSToby Isaac     }
58804acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58814acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58824acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5883de41b84cSMatthew G. Knepley     }
5884de41b84cSMatthew G. Knepley   } else {
58854acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
58864acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
58874acb8e1eSToby Isaac 
58884acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58894acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
58904acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
58914acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
58924acb8e1eSToby Isaac 
58934acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
58944acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5895de41b84cSMatthew G. Knepley     }
58964acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
58974acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
58984acb8e1eSToby Isaac 
58994acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
59004acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5901de41b84cSMatthew G. Knepley     }
59024acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
59034acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5904de41b84cSMatthew G. Knepley   }
5905de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
59064acb8e1eSToby Isaac   /* TODO: flips */
5907de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5908de41b84cSMatthew G. Knepley   if (ierr) {
5909de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5910de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5911de41b84cSMatthew G. Knepley 
5912de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5913e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5914de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
591569291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
591669291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
5917de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5918de41b84cSMatthew G. Knepley   }
591969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
5920de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
592169291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
592269291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
5923de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5924de41b84cSMatthew G. Knepley }
5925de41b84cSMatthew G. Knepley 
59267c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
59277c927364SMatthew G. Knepley {
59287c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
59297c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
59307c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
59317c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
59327c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
59337c927364SMatthew G. Knepley   PetscErrorCode ierr;
59347c927364SMatthew G. Knepley 
59357c927364SMatthew G. Knepley   PetscFunctionBegin;
59367c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
59377c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
59387c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
59397c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
59407c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
59417c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
59427c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
59437c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
59447c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
59457c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
59467c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
59477c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
59487c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
59497c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
59507c927364SMatthew G. Knepley   /* Column indices */
59517c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
59527c927364SMatthew G. Knepley   maxFPoints = numCPoints;
59537c927364SMatthew G. Knepley   /* Compress out points not in the section */
59547c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
59557c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
59567c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
59577c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
59587c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
59597c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
59607c927364SMatthew G. Knepley       ++q;
59617c927364SMatthew G. Knepley     }
59627c927364SMatthew G. Knepley   }
59637c927364SMatthew G. Knepley   numCPoints = q;
59647c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
59657c927364SMatthew G. Knepley     PetscInt fdof;
59667c927364SMatthew G. Knepley 
59677c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
59687c927364SMatthew G. Knepley     if (!dof) continue;
59697c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
59707c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
59717c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
59727c927364SMatthew G. Knepley     }
59737c927364SMatthew G. Knepley     numCIndices += dof;
59747c927364SMatthew G. Knepley   }
59757c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
59767c927364SMatthew G. Knepley   /* Row indices */
59777c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
59787c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
597969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
59807c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
59817c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
59827c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59837c927364SMatthew G. Knepley     /* Compress out points not in the section */
59847c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
59857c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
59867c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
59877c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
59887c927364SMatthew G. Knepley         if (!dof) continue;
59897c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
59907c927364SMatthew G. Knepley         if (s < q) continue;
59917c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
59927c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
59937c927364SMatthew G. Knepley         ++q;
59947c927364SMatthew G. Knepley       }
59957c927364SMatthew G. Knepley     }
59967c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
59977c927364SMatthew G. Knepley   }
59987c927364SMatthew G. Knepley   numFPoints = q;
59997c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
60007c927364SMatthew G. Knepley     PetscInt fdof;
60017c927364SMatthew G. Knepley 
60027c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
60037c927364SMatthew G. Knepley     if (!dof) continue;
60047c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
60057c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
60067c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
60077c927364SMatthew G. Knepley     }
60087c927364SMatthew G. Knepley     numFIndices += dof;
60097c927364SMatthew G. Knepley   }
60107c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
60117c927364SMatthew G. Knepley 
60128ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
60138ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
60147c927364SMatthew G. Knepley   if (numFields) {
60154acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
60164acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
60174acb8e1eSToby Isaac 
60184acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
60194acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
60204acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
60217c927364SMatthew G. Knepley     }
60224acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
60234acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
60244acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
60254acb8e1eSToby Isaac     }
60264acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
60274acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
60284acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
60294acb8e1eSToby Isaac     }
60304acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
60314acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
60324acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
60337c927364SMatthew G. Knepley     }
60347c927364SMatthew G. Knepley   } else {
60354acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
60364acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
60374acb8e1eSToby Isaac 
60384acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
60394acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
60404acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
60414acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
60424acb8e1eSToby Isaac 
60434acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
60444acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
60457c927364SMatthew G. Knepley     }
60464acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
60474acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
60484acb8e1eSToby Isaac 
60494acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
60504acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
60517c927364SMatthew G. Knepley     }
60524acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
60534acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
60547c927364SMatthew G. Knepley   }
605569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
60567c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
60577c927364SMatthew G. Knepley   PetscFunctionReturn(0);
60587c927364SMatthew G. Knepley }
60597c927364SMatthew G. Knepley 
6060f96281f8SMatthew G. Knepley /*@
6061f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6062f96281f8SMatthew G. Knepley 
6063f96281f8SMatthew G. Knepley   Input Parameter:
6064f96281f8SMatthew G. Knepley . dm - The DMPlex object
6065f96281f8SMatthew G. Knepley 
6066f96281f8SMatthew G. Knepley   Output Parameters:
6067f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6068dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6069dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6070dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6071f96281f8SMatthew G. Knepley 
6072f96281f8SMatthew G. Knepley   Level: developer
6073f96281f8SMatthew G. Knepley 
6074dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6075f96281f8SMatthew G. Knepley @*/
6076770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6077552f7358SJed Brown {
6078552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6079770b213bSMatthew G Knepley   PetscInt       dim;
6080770b213bSMatthew G Knepley   PetscErrorCode ierr;
6081552f7358SJed Brown 
6082552f7358SJed Brown   PetscFunctionBegin;
6083552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6084c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6085*7d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6086770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
6087*7d5acc75SStefano Zampini   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6088770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6089770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6090552f7358SJed Brown   PetscFunctionReturn(0);
6091552f7358SJed Brown }
6092552f7358SJed Brown 
6093aeadca18SToby Isaac static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
60944a3e9fdbSToby Isaac {
60954a3e9fdbSToby Isaac   IS             is, his;
6096*7d5acc75SStefano Zampini   PetscInt       first = 0, stride;
60974a3e9fdbSToby Isaac   PetscBool      isStride;
60984a3e9fdbSToby Isaac   PetscErrorCode ierr;
60994a3e9fdbSToby Isaac 
61004a3e9fdbSToby Isaac   PetscFunctionBegin;
61014a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel, d, &is);CHKERRQ(ierr);
61024a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);CHKERRQ(ierr);
61034a3e9fdbSToby Isaac   if (isStride) {
61044a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(is, &first, &stride);CHKERRQ(ierr);
61054a3e9fdbSToby Isaac   }
6106*7d5acc75SStefano Zampini   if (is && (!isStride || stride != 1)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM is not stratified: depth %D IS is not contiguous", d);
61074a3e9fdbSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);CHKERRQ(ierr);
6108aeadca18SToby Isaac   ierr = DMLabelSetStratumIS(dimLabel, d, his);CHKERRQ(ierr);
61094a3e9fdbSToby Isaac   ierr = ISDestroy(&his);CHKERRQ(ierr);
61104a3e9fdbSToby Isaac   ierr = ISDestroy(&is);CHKERRQ(ierr);
61114a3e9fdbSToby Isaac   PetscFunctionReturn(0);
61124a3e9fdbSToby Isaac }
61134a3e9fdbSToby Isaac 
6114dc5a3409SMatthew G. Knepley /*@
6115dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6116dc5a3409SMatthew G. Knepley 
6117dc5a3409SMatthew G. Knepley   Input Parameters:
6118dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
6119dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6120dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6121dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6122dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6123dc5a3409SMatthew G. Knepley 
6124dc5a3409SMatthew G. Knepley   Level: developer
6125dc5a3409SMatthew G. Knepley 
6126dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6127dc5a3409SMatthew G. Knepley @*/
6128770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6129552f7358SJed Brown {
6130552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6131770b213bSMatthew G Knepley   PetscInt       dim;
6132770b213bSMatthew G Knepley   PetscErrorCode ierr;
6133552f7358SJed Brown 
6134552f7358SJed Brown   PetscFunctionBegin;
6135552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6136c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6137*7d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6138770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
6139*7d5acc75SStefano Zampini   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6140770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6141770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6142552f7358SJed Brown   PetscFunctionReturn(0);
6143552f7358SJed Brown }
6144552f7358SJed Brown 
61457cd05799SMatthew G. Knepley /*@C
61467cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
61477cd05799SMatthew G. Knepley 
61487cd05799SMatthew G. Knepley   Input Parameter:
61497cd05799SMatthew G. Knepley . dm   - The DMPlex object
61507cd05799SMatthew G. Knepley 
61517cd05799SMatthew G. Knepley   Output Parameter:
61527cd05799SMatthew G. Knepley . cellHeight - The height of a cell
61537cd05799SMatthew G. Knepley 
61547cd05799SMatthew G. Knepley   Level: developer
61557cd05799SMatthew G. Knepley 
61567cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
61577cd05799SMatthew G. Knepley @*/
6158552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6159552f7358SJed Brown {
6160552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6161552f7358SJed Brown 
6162552f7358SJed Brown   PetscFunctionBegin;
6163552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6164552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6165552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6166552f7358SJed Brown   PetscFunctionReturn(0);
6167552f7358SJed Brown }
6168552f7358SJed Brown 
61697cd05799SMatthew G. Knepley /*@C
61707cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
61717cd05799SMatthew G. Knepley 
61727cd05799SMatthew G. Knepley   Input Parameters:
61737cd05799SMatthew G. Knepley + dm   - The DMPlex object
61747cd05799SMatthew G. Knepley - cellHeight - The height of a cell
61757cd05799SMatthew G. Knepley 
61767cd05799SMatthew G. Knepley   Level: developer
61777cd05799SMatthew G. Knepley 
61787cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
61797cd05799SMatthew G. Knepley @*/
6180552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6181552f7358SJed Brown {
6182552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6183552f7358SJed Brown 
6184552f7358SJed Brown   PetscFunctionBegin;
6185552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6186552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6187552f7358SJed Brown   PetscFunctionReturn(0);
6188552f7358SJed Brown }
6189552f7358SJed Brown 
6190552f7358SJed Brown /* We can easily have a form that takes an IS instead */
6191ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6192552f7358SJed Brown {
6193552f7358SJed Brown   PetscSection   section, globalSection;
6194552f7358SJed Brown   PetscInt      *numbers, p;
6195552f7358SJed Brown   PetscErrorCode ierr;
6196552f7358SJed Brown 
6197552f7358SJed Brown   PetscFunctionBegin;
619882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6199552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6200552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6201552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6202552f7358SJed Brown   }
6203552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
620415b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6205854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6206552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6207552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6208ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6209ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6210552f7358SJed Brown   }
621182f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6212ef48cebcSMatthew G. Knepley   if (globalSize) {
6213ef48cebcSMatthew G. Knepley     PetscLayout layout;
6214ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6215ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6216ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6217ef48cebcSMatthew G. Knepley   }
6218552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6219552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6220552f7358SJed Brown   PetscFunctionReturn(0);
6221552f7358SJed Brown }
6222552f7358SJed Brown 
622381ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6224552f7358SJed Brown {
6225552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6226552f7358SJed Brown   PetscErrorCode ierr;
6227552f7358SJed Brown 
6228552f7358SJed Brown   PetscFunctionBegin;
6229552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6230552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
62310298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
623281ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
623381ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
623481ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6235552f7358SJed Brown }
623681ed3555SMatthew G. Knepley 
62377cd05799SMatthew G. Knepley /*@C
62387cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
62397cd05799SMatthew G. Knepley 
62407cd05799SMatthew G. Knepley   Input Parameter:
62417cd05799SMatthew G. Knepley . dm   - The DMPlex object
62427cd05799SMatthew G. Knepley 
62437cd05799SMatthew G. Knepley   Output Parameter:
62447cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
62457cd05799SMatthew G. Knepley 
62467cd05799SMatthew G. Knepley   Level: developer
62477cd05799SMatthew G. Knepley 
62487cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
62497cd05799SMatthew G. Knepley @*/
625081ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
625181ed3555SMatthew G. Knepley {
625281ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
625381ed3555SMatthew G. Knepley   PetscErrorCode ierr;
625481ed3555SMatthew G. Knepley 
625581ed3555SMatthew G. Knepley   PetscFunctionBegin;
625681ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
625781ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6258552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6259552f7358SJed Brown   PetscFunctionReturn(0);
6260552f7358SJed Brown }
6261552f7358SJed Brown 
626281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
626381ed3555SMatthew G. Knepley {
626481ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
626581ed3555SMatthew G. Knepley   PetscErrorCode ierr;
626681ed3555SMatthew G. Knepley 
626781ed3555SMatthew G. Knepley   PetscFunctionBegin;
626881ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
626981ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
627081ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
627181ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
627281ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
627381ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
627481ed3555SMatthew G. Knepley }
627581ed3555SMatthew G. Knepley 
62767cd05799SMatthew G. Knepley /*@C
62777cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
62787cd05799SMatthew G. Knepley 
62797cd05799SMatthew G. Knepley   Input Parameter:
62807cd05799SMatthew G. Knepley . dm   - The DMPlex object
62817cd05799SMatthew G. Knepley 
62827cd05799SMatthew G. Knepley   Output Parameter:
62837cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
62847cd05799SMatthew G. Knepley 
62857cd05799SMatthew G. Knepley   Level: developer
62867cd05799SMatthew G. Knepley 
62877cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
62887cd05799SMatthew G. Knepley @*/
6289552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6290552f7358SJed Brown {
6291552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6292552f7358SJed Brown   PetscErrorCode ierr;
6293552f7358SJed Brown 
6294552f7358SJed Brown   PetscFunctionBegin;
6295552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
629681ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6297552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6298552f7358SJed Brown   PetscFunctionReturn(0);
6299552f7358SJed Brown }
6300552f7358SJed Brown 
63017cd05799SMatthew G. Knepley /*@C
63027cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
63037cd05799SMatthew G. Knepley 
63047cd05799SMatthew G. Knepley   Input Parameter:
63057cd05799SMatthew G. Knepley . dm   - The DMPlex object
63067cd05799SMatthew G. Knepley 
63077cd05799SMatthew G. Knepley   Output Parameter:
63087cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
63097cd05799SMatthew G. Knepley 
63107cd05799SMatthew G. Knepley   Level: developer
63117cd05799SMatthew G. Knepley 
63127cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
63137cd05799SMatthew G. Knepley @*/
6314ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6315ef48cebcSMatthew G. Knepley {
6316ef48cebcSMatthew G. Knepley   IS             nums[4];
6317ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
6318ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6319ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6320ef48cebcSMatthew G. Knepley 
6321ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6322ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6323ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
63248abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
63258abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6326ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
6327ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
6328ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6329ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6330ef48cebcSMatthew G. Knepley 
6331ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
6332ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6333ef48cebcSMatthew G. Knepley     shift += gsize;
6334ef48cebcSMatthew G. Knepley   }
6335302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6336ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6337ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6338ef48cebcSMatthew G. Knepley }
6339ef48cebcSMatthew G. Knepley 
634008a22f4bSMatthew G. Knepley 
634108a22f4bSMatthew G. Knepley /*@
634208a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
634308a22f4bSMatthew G. Knepley 
634408a22f4bSMatthew G. Knepley   Input Parameter:
634508a22f4bSMatthew G. Knepley . dm - The DMPlex object
634608a22f4bSMatthew G. Knepley 
634708a22f4bSMatthew G. Knepley   Output Parameter:
634808a22f4bSMatthew G. Knepley . ranks - The rank field
634908a22f4bSMatthew G. Knepley 
635008a22f4bSMatthew G. Knepley   Options Database Keys:
635108a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
635208a22f4bSMatthew G. Knepley 
635308a22f4bSMatthew G. Knepley   Level: intermediate
635408a22f4bSMatthew G. Knepley 
635508a22f4bSMatthew G. Knepley .seealso: DMView()
635608a22f4bSMatthew G. Knepley @*/
635708a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
635808a22f4bSMatthew G. Knepley {
635908a22f4bSMatthew G. Knepley   DM             rdm;
636008a22f4bSMatthew G. Knepley   PetscDS        prob;
636108a22f4bSMatthew G. Knepley   PetscFE        fe;
636208a22f4bSMatthew G. Knepley   PetscScalar   *r;
636308a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
636408a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
636508a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
636608a22f4bSMatthew G. Knepley 
636708a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
636808a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
636908a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
637008a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
6371e8d98e54SMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, NULL, -1, &fe);CHKERRQ(ierr);
637208a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
637308a22f4bSMatthew G. Knepley   ierr = DMGetDS(rdm, &prob);CHKERRQ(ierr);
637408a22f4bSMatthew G. Knepley   ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe);CHKERRQ(ierr);
637508a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
637608a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
637708a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
637808a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
637908a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
638008a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
638108a22f4bSMatthew G. Knepley     PetscScalar *lr;
638208a22f4bSMatthew G. Knepley 
638308a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
638408a22f4bSMatthew G. Knepley     *lr = rank;
638508a22f4bSMatthew G. Knepley   }
638608a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
638708a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
638808a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
638908a22f4bSMatthew G. Knepley }
639008a22f4bSMatthew G. Knepley 
6391ca8062c8SMatthew G. Knepley /*@
6392ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6393ca8062c8SMatthew G. Knepley 
639469916449SMatthew G. Knepley   Input Parameter:
639569916449SMatthew G. Knepley . dm - The DMPlex object
6396ca8062c8SMatthew G. Knepley 
6397ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6398ca8062c8SMatthew G. Knepley 
6399ca8062c8SMatthew G. Knepley   Level: developer
6400ca8062c8SMatthew G. Knepley 
6401*7d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6402ca8062c8SMatthew G. Knepley @*/
6403ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6404ca8062c8SMatthew G. Knepley {
6405ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6406ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6407ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
6408ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
6409ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6410ca8062c8SMatthew G. Knepley 
6411ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6412ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6413ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6414ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6415ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6416ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6417ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6418ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6419ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6420ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
642142e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
642242e66dfaSMatthew G. Knepley       PetscInt  d;
642342e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
642442e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
642542e66dfaSMatthew G. Knepley       }
6426ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6427ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6428ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6429ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6430ca8062c8SMatthew G. Knepley       }
643142e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
64328ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6433ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
64348ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6435ca8062c8SMatthew G. Knepley         }
6436302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64378ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6438ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
64398ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6440ca8062c8SMatthew G. Knepley         }
6441302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64428ccfff9cSToby 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]);
64438ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6444ca8062c8SMatthew G. Knepley       }
644542e66dfaSMatthew G. Knepley     }
6446ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6447ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6448ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6449ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6450ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6451ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6452ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6453ca8062c8SMatthew G. Knepley       }
6454ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
64558ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6456ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
64578ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6458ca8062c8SMatthew G. Knepley         }
6459302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64608ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6461ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
64628ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6463ca8062c8SMatthew G. Knepley         }
6464302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
64658ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6466ca8062c8SMatthew G. Knepley       }
6467ca8062c8SMatthew G. Knepley     }
6468ca8062c8SMatthew G. Knepley   }
6469ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6470ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
64718ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6472ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6473ca8062c8SMatthew G. Knepley }
6474ca8062c8SMatthew G. Knepley 
6475ca8062c8SMatthew G. Knepley /*@
6476ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6477ca8062c8SMatthew G. Knepley 
6478ca8062c8SMatthew G. Knepley   Input Parameters:
6479ca8062c8SMatthew G. Knepley + dm - The DMPlex object
648058723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
648158723a97SMatthew G. Knepley - cellHeight - Normally 0
6482ca8062c8SMatthew G. Knepley 
6483ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6484ca8062c8SMatthew G. Knepley 
6485ca8062c8SMatthew G. Knepley   Level: developer
6486ca8062c8SMatthew G. Knepley 
6487*7d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckFaces()
6488ca8062c8SMatthew G. Knepley @*/
648958723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6490ca8062c8SMatthew G. Knepley {
649142363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6492ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6493ca8062c8SMatthew G. Knepley 
6494ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6495ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6496c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6497ca8062c8SMatthew G. Knepley   switch (dim) {
649842363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
649942363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
650042363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6501ca8062c8SMatthew G. Knepley   default:
65028ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6503ca8062c8SMatthew G. Knepley   }
650458723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
650558723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6506ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6507ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6508ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
650958723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
651058723a97SMatthew G. Knepley 
651158723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
651258723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
651358723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
651458723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
651558723a97SMatthew G. Knepley     }
651658723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65178ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6518ca8062c8SMatthew G. Knepley   }
651942363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
652042363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
652142363296SMatthew G. Knepley 
652242363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
652342363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
652442363296SMatthew G. Knepley       const PetscInt p = closure[cl];
652542363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
652642363296SMatthew G. Knepley     }
652742363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65288ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
652942363296SMatthew G. Knepley   }
6530ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6531ca8062c8SMatthew G. Knepley }
65329bf0dad6SMatthew G. Knepley 
65339bf0dad6SMatthew G. Knepley /*@
65349bf0dad6SMatthew 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
65359bf0dad6SMatthew G. Knepley 
65369bf0dad6SMatthew G. Knepley   Input Parameters:
65379bf0dad6SMatthew G. Knepley + dm - The DMPlex object
65389bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
65399bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
65409bf0dad6SMatthew G. Knepley 
65419bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
65429bf0dad6SMatthew G. Knepley 
65439bf0dad6SMatthew G. Knepley   Level: developer
65449bf0dad6SMatthew G. Knepley 
6545*7d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton()
65469bf0dad6SMatthew G. Knepley @*/
65479bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
65489bf0dad6SMatthew G. Knepley {
65493554e41dSMatthew G. Knepley   PetscInt       pMax[4];
65503554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
65519bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
65529bf0dad6SMatthew G. Knepley 
65539bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
65549bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6555c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
65569bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
655725abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
65583554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
65593554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
65603554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
65619bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
65629bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
65639bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
65649bf0dad6SMatthew G. Knepley 
656525abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
65669bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
65679bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
65689bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
65699bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65709bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
65719bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
65729bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
65739bf0dad6SMatthew G. Knepley       }
65743554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
65758ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
65769bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
65779bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
65789bf0dad6SMatthew G. Knepley 
65799bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
65809bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
65819bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
65829bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
65839bf0dad6SMatthew G. Knepley         }
65848ccfff9cSToby 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);
65859bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
65868ccfff9cSToby 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]);
65879bf0dad6SMatthew G. Knepley         }
65889bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
65899bf0dad6SMatthew G. Knepley       }
65909bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
65919bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
65929bf0dad6SMatthew G. Knepley     }
65933554e41dSMatthew G. Knepley   }
6594552f7358SJed Brown   PetscFunctionReturn(0);
6595552f7358SJed Brown }
65963913d7c8SMatthew G. Knepley 
6597bceba477SMatthew G. Knepley /* Pointwise interpolation
6598bceba477SMatthew G. Knepley      Just code FEM for now
6599bceba477SMatthew G. Knepley      u^f = I u^c
66004ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
66014ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
66024ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6603bceba477SMatthew G. Knepley */
6604bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6605bceba477SMatthew G. Knepley {
6606bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6607bceba477SMatthew G. Knepley   PetscInt       m, n;
6608a063dac3SMatthew G. Knepley   void          *ctx;
660968132eb9SMatthew G. Knepley   DM             cdm;
6610fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
6611bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6612bceba477SMatthew G. Knepley 
6613bceba477SMatthew G. Knepley   PetscFunctionBegin;
6614bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6615bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6616bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6617bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
661868132eb9SMatthew G. Knepley 
6619fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
6620bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6621bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6622fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
6623a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
662468132eb9SMatthew G. Knepley 
6625a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
662668132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
662768132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
662868132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
662968132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
66305d1c2e58SMatthew G. Knepley   /* Use naive scaling */
66315d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6632a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6633a063dac3SMatthew G. Knepley }
6634bceba477SMatthew G. Knepley 
66356dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6636a063dac3SMatthew G. Knepley {
663790748bafSMatthew G. Knepley   PetscErrorCode ierr;
66386dbf9973SLawrence Mitchell   VecScatter     ctx;
663990748bafSMatthew G. Knepley 
6640a063dac3SMatthew G. Knepley   PetscFunctionBegin;
66416dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
66426dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
66436dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6644bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6645bceba477SMatthew G. Knepley }
6646bceba477SMatthew G. Knepley 
6647bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
6648bd041c0cSMatthew G. Knepley {
6649bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
6650bd041c0cSMatthew G. Knepley   PetscInt       m, n;
6651bd041c0cSMatthew G. Knepley   void          *ctx;
6652bd041c0cSMatthew G. Knepley   DM             cdm;
6653bd041c0cSMatthew G. Knepley   PetscBool      regular;
6654bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
6655bd041c0cSMatthew G. Knepley 
6656bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
6657bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6658bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6659bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6660bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
6661bd041c0cSMatthew G. Knepley 
6662bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
6663bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6664bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
6665bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
6666bd041c0cSMatthew G. Knepley 
6667bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
6668bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
6669bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6670bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
6671bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
6672bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
6673bd041c0cSMatthew G. Knepley }
6674bd041c0cSMatthew G. Knepley 
6675fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6676fd59a867SMatthew G. Knepley {
6677fd59a867SMatthew G. Knepley   PetscSection   section;
6678ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6679ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6680286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6681ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6682ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6683fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6684fd59a867SMatthew G. Knepley 
6685fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6686ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6687ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6688ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6689ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6690ebb3236fSMatthew G. Knepley     PetscObject  obj;
6691ebb3236fSMatthew G. Knepley     PetscClassId id;
6692ebb3236fSMatthew G. Knepley 
6693ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6694ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6695ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6696ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
66978ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6698ebb3236fSMatthew G. Knepley   }
66992f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6700286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6701c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6702ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6703ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6704dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6705fdafae62SVaclav 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)");
6706fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
67072f900235SMatthew G. Knepley     PetscInt                field;
6708f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6709385532a5SToby Isaac     const char             *labelName;
6710385532a5SToby Isaac     DMLabel                 label;
67112f900235SMatthew G. Knepley 
6712f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6713385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6714f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6715fd59a867SMatthew G. Knepley   }
67162f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6717ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
67186c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6719ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6720ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6721ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6722ebb3236fSMatthew G. Knepley 
6723ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6724ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6725ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6726ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6727ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6728ebb3236fSMatthew G. Knepley   }
6729ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6730ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6731fd59a867SMatthew G. Knepley     const char             *bdLabel;
6732fd59a867SMatthew G. Knepley     DMLabel                 label;
67330e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6734fd59a867SMatthew G. Knepley     const PetscInt         *values;
67350e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6736f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6737f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6738fd59a867SMatthew G. Knepley 
6739f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6740c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6741385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6742ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
67437391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
67447391c1cbSMatthew G. Knepley       const char *bdname;
6745dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
67467391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
67477391c1cbSMatthew G. Knepley       if (duplicate) break;
67487391c1cbSMatthew G. Knepley     }
6749ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6750b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6751092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
67527391c1cbSMatthew G. Knepley     }
6753ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6754f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
675593a5981aSMatthew G. Knepley       PetscInt       *newidx;
6756ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
675793a5981aSMatthew G. Knepley 
6758fd59a867SMatthew G. Knepley       bcFields[bc] = field;
67590e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6760ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6761ebb3236fSMatthew G. Knepley         IS              tmp;
6762ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6763ebb3236fSMatthew G. Knepley 
6764c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6765ebb3236fSMatthew G. Knepley         if (!tmp) continue;
676693a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
676793a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6768ebb3236fSMatthew G. Knepley         if (isFE[field]) {
676993a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6770ebb3236fSMatthew G. Knepley         } else {
6771ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6772ebb3236fSMatthew G. Knepley         }
677393a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
677493a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6775fd59a867SMatthew G. Knepley       }
6776ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6777ebb3236fSMatthew G. Knepley       newn = 0;
6778ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6779ebb3236fSMatthew G. Knepley         IS              tmp;
6780ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6781ebb3236fSMatthew G. Knepley 
6782c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6783ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6784ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6785ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6786ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6787ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6788ebb3236fSMatthew G. Knepley         } else {
6789ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6790ebb3236fSMatthew G. Knepley         }
6791ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6792ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6793ebb3236fSMatthew G. Knepley       }
6794ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6795ebb3236fSMatthew G. Knepley     }
6796fd59a867SMatthew G. Knepley   }
6797fd59a867SMatthew G. Knepley   /* Handle discretization */
6798ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6799fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
68000bacfa0aSMatthew G. Knepley     PetscObject obj;
68010bacfa0aSMatthew G. Knepley 
68020bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6803ebb3236fSMatthew G. Knepley     if (isFE[f]) {
68040bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6805286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6806fd59a867SMatthew G. Knepley       PetscInt        d;
6807fd59a867SMatthew G. Knepley 
6808fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6809286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6810286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6811ebb3236fSMatthew G. Knepley     } else {
68120bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
68130bacfa0aSMatthew G. Knepley 
68140bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
68150bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6816ebb3236fSMatthew G. Knepley     }
6817fd59a867SMatthew G. Knepley   }
6818286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6819286d8613SMatthew G. Knepley     PetscInt d;
6820286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6821286d8613SMatthew 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.");
6822286d8613SMatthew G. Knepley     }
6823286d8613SMatthew G. Knepley   }
6824ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6825fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6826fd59a867SMatthew G. Knepley     PetscFE     fe;
6827fd59a867SMatthew G. Knepley     const char *name;
682818abd763SMatthew G. Knepley 
682918abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6830fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6831fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6832fd59a867SMatthew G. Knepley   }
6833fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6834fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
68350e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
68360e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6837286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6838ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6839bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6840bceba477SMatthew G. Knepley }
6841bceba477SMatthew G. Knepley 
68420aef6b92SMatthew G. Knepley /*@
68430aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
68440aef6b92SMatthew G. Knepley 
68450aef6b92SMatthew G. Knepley   Input Parameter:
68460aef6b92SMatthew G. Knepley . dm - The DMPlex object
68470aef6b92SMatthew G. Knepley 
68480aef6b92SMatthew G. Knepley   Output Parameter:
68490aef6b92SMatthew G. Knepley . regular - The flag
68500aef6b92SMatthew G. Knepley 
68510aef6b92SMatthew G. Knepley   Level: intermediate
68520aef6b92SMatthew G. Knepley 
68530aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
68540aef6b92SMatthew G. Knepley @*/
68550aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
68560aef6b92SMatthew G. Knepley {
68570aef6b92SMatthew G. Knepley   PetscFunctionBegin;
68580aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68590aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
68600aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
68610aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
68620aef6b92SMatthew G. Knepley }
68630aef6b92SMatthew G. Knepley 
68640aef6b92SMatthew G. Knepley /*@
68650aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
68660aef6b92SMatthew G. Knepley 
68670aef6b92SMatthew G. Knepley   Input Parameters:
68680aef6b92SMatthew G. Knepley + dm - The DMPlex object
68690aef6b92SMatthew G. Knepley - regular - The flag
68700aef6b92SMatthew G. Knepley 
68710aef6b92SMatthew G. Knepley   Level: intermediate
68720aef6b92SMatthew G. Knepley 
68730aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
68740aef6b92SMatthew G. Knepley @*/
68750aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
68760aef6b92SMatthew G. Knepley {
68770aef6b92SMatthew G. Knepley   PetscFunctionBegin;
68780aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68790aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
68800aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
68810aef6b92SMatthew G. Knepley }
68820aef6b92SMatthew G. Knepley 
6883f7c74593SToby Isaac /* anchors */
6884a68b90caSToby Isaac /*@
6885f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6886f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6887a68b90caSToby Isaac 
6888e228b242SToby Isaac   not collective
6889a68b90caSToby Isaac 
6890a68b90caSToby Isaac   Input Parameters:
6891a68b90caSToby Isaac . dm - The DMPlex object
6892a68b90caSToby Isaac 
6893a68b90caSToby Isaac   Output Parameters:
6894a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6895a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6896a68b90caSToby Isaac 
6897a68b90caSToby Isaac 
6898a68b90caSToby Isaac   Level: intermediate
6899a68b90caSToby Isaac 
6900f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6901a68b90caSToby Isaac @*/
6902a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6903a68b90caSToby Isaac {
6904a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
690541e6d900SToby Isaac   PetscErrorCode ierr;
6906a68b90caSToby Isaac 
6907a68b90caSToby Isaac   PetscFunctionBegin;
6908a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
690941e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6910a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6911a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6912a68b90caSToby Isaac   PetscFunctionReturn(0);
6913a68b90caSToby Isaac }
6914a68b90caSToby Isaac 
6915a68b90caSToby Isaac /*@
6916f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6917f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6918a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6919a68b90caSToby Isaac 
6920a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6921f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6922a68b90caSToby Isaac 
6923e228b242SToby Isaac   collective on dm
6924a68b90caSToby Isaac 
6925a68b90caSToby Isaac   Input Parameters:
6926a68b90caSToby Isaac + dm - The DMPlex object
6927e228b242SToby 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).
6928e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6929a68b90caSToby Isaac 
6930a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6931a68b90caSToby Isaac 
6932a68b90caSToby Isaac   Level: intermediate
6933a68b90caSToby Isaac 
6934f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6935a68b90caSToby Isaac @*/
6936a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6937a68b90caSToby Isaac {
6938a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6939e228b242SToby Isaac   PetscMPIInt    result;
6940a68b90caSToby Isaac   PetscErrorCode ierr;
6941a68b90caSToby Isaac 
6942a68b90caSToby Isaac   PetscFunctionBegin;
6943a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6944e228b242SToby Isaac   if (anchorSection) {
6945e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6946e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6947f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6948e228b242SToby Isaac   }
6949e228b242SToby Isaac   if (anchorIS) {
6950e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6951e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6952f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6953e228b242SToby Isaac   }
6954a68b90caSToby Isaac 
6955a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6956a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6957a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6958a68b90caSToby Isaac 
6959a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6960a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6961a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6962a68b90caSToby Isaac 
6963a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6964a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6965a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6966a68b90caSToby Isaac     const PetscInt *anchors;
6967a68b90caSToby Isaac 
6968a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6969a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6970a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6971a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6972a68b90caSToby Isaac       PetscInt p;
6973a68b90caSToby Isaac 
6974a68b90caSToby Isaac       p = anchors[a];
6975a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6976a68b90caSToby Isaac         PetscInt dof;
6977a68b90caSToby Isaac 
6978a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6979a68b90caSToby Isaac         if (dof) {
6980a68b90caSToby Isaac           PetscErrorCode ierr2;
6981a68b90caSToby Isaac 
6982a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
69838ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6984a68b90caSToby Isaac         }
6985a68b90caSToby Isaac       }
6986a68b90caSToby Isaac     }
6987a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6988a68b90caSToby Isaac   }
6989a68b90caSToby Isaac #endif
6990f7c74593SToby Isaac   /* reset the generic constraints */
6991f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6992a68b90caSToby Isaac   PetscFunctionReturn(0);
6993a68b90caSToby Isaac }
6994a68b90caSToby Isaac 
6995f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6996a68b90caSToby Isaac {
6997f7c74593SToby Isaac   PetscSection anchorSection;
69986995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6999a68b90caSToby Isaac   PetscErrorCode ierr;
7000a68b90caSToby Isaac 
7001a68b90caSToby Isaac   PetscFunctionBegin;
7002a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7003a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7004e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7005a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
70066995de1eSToby Isaac   if (numFields) {
7007719ab38cSToby Isaac     PetscInt f;
7008a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7009719ab38cSToby Isaac 
7010719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7011719ab38cSToby Isaac       PetscInt numComp;
7012719ab38cSToby Isaac 
7013719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7014719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7015719ab38cSToby Isaac     }
70166995de1eSToby Isaac   }
7017a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
70186995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
70196995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
70206995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
70216995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7022a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7023a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7024a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7025a68b90caSToby Isaac     if (dof) {
7026a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7027a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7028a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7029a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7030a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7031a68b90caSToby Isaac       }
7032a68b90caSToby Isaac     }
7033a68b90caSToby Isaac   }
7034a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7035a68b90caSToby Isaac   PetscFunctionReturn(0);
7036a68b90caSToby Isaac }
7037a68b90caSToby Isaac 
7038f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7039a68b90caSToby Isaac {
7040f7c74593SToby Isaac   PetscSection aSec;
70410ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
70420ac89760SToby Isaac   const PetscInt *anchors;
70430ac89760SToby Isaac   PetscInt numFields, f;
704466ad2231SToby Isaac   IS aIS;
70450ac89760SToby Isaac   PetscErrorCode ierr;
70460ac89760SToby Isaac 
70470ac89760SToby Isaac   PetscFunctionBegin;
70480ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70490ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
70500ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
70510ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
70520ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7053302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7054a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
705566ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
70566995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
70576995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
70580ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
70590ac89760SToby Isaac   i[0] = 0;
70600ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
70610ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7062f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7063f19733c5SToby Isaac 
7064f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7065f19733c5SToby Isaac     if (!rDof) continue;
7066f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
70670ac89760SToby Isaac     if (numFields) {
70680ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
70690ac89760SToby Isaac         annz = 0;
7070f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7071f19733c5SToby Isaac           a = anchors[rOff + r];
70720ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
70730ac89760SToby Isaac           annz += aDof;
70740ac89760SToby Isaac         }
70750ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
70760ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
70770ac89760SToby Isaac         for (q = 0; q < dof; q++) {
70780ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
70790ac89760SToby Isaac         }
70800ac89760SToby Isaac       }
70810ac89760SToby Isaac     }
70820ac89760SToby Isaac     else {
70830ac89760SToby Isaac       annz = 0;
70840ac89760SToby Isaac       for (q = 0; q < dof; q++) {
70850ac89760SToby Isaac         a = anchors[off + q];
70860ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
70870ac89760SToby Isaac         annz += aDof;
70880ac89760SToby Isaac       }
70890ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
70900ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
70910ac89760SToby Isaac       for (q = 0; q < dof; q++) {
70920ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
70930ac89760SToby Isaac       }
70940ac89760SToby Isaac     }
70950ac89760SToby Isaac   }
70960ac89760SToby Isaac   nnz = i[m];
70970ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
70980ac89760SToby Isaac   offset = 0;
70990ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
71000ac89760SToby Isaac     if (numFields) {
71010ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
71020ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
71030ac89760SToby Isaac         for (q = 0; q < dof; q++) {
71040ac89760SToby Isaac           PetscInt rDof, rOff, r;
71050ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
71060ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
71070ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
71080ac89760SToby Isaac             PetscInt s;
71090ac89760SToby Isaac 
71100ac89760SToby Isaac             a = anchors[rOff + r];
71110ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
71120ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
71130ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
71140ac89760SToby Isaac               j[offset++] = aOff + s;
71150ac89760SToby Isaac             }
71160ac89760SToby Isaac           }
71170ac89760SToby Isaac         }
71180ac89760SToby Isaac       }
71190ac89760SToby Isaac     }
71200ac89760SToby Isaac     else {
71210ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
71220ac89760SToby Isaac       for (q = 0; q < dof; q++) {
71230ac89760SToby Isaac         PetscInt rDof, rOff, r;
71240ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
71250ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
71260ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
71270ac89760SToby Isaac           PetscInt s;
71280ac89760SToby Isaac 
71290ac89760SToby Isaac           a = anchors[rOff + r];
71300ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
71310ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
71320ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
71330ac89760SToby Isaac             j[offset++] = aOff + s;
71340ac89760SToby Isaac           }
71350ac89760SToby Isaac         }
71360ac89760SToby Isaac       }
71370ac89760SToby Isaac     }
71380ac89760SToby Isaac   }
71390ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
714025570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
714125570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
714266ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
71430ac89760SToby Isaac   PetscFunctionReturn(0);
71440ac89760SToby Isaac }
71450ac89760SToby Isaac 
714666ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
714766ad2231SToby Isaac {
7148f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7149f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
715066ad2231SToby Isaac   Mat            cMat;
715166ad2231SToby Isaac   PetscErrorCode ierr;
715266ad2231SToby Isaac 
715366ad2231SToby Isaac   PetscFunctionBegin;
715466ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7155a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
715666ad2231SToby Isaac   if (anchorSection) {
7157e228b242SToby Isaac     PetscDS  ds;
7158e228b242SToby Isaac     PetscInt nf;
7159e228b242SToby Isaac 
7160f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
7161f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7162f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
7163e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
7164302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
7165e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
716666ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
716766ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
716866ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
716966ad2231SToby Isaac   }
717066ad2231SToby Isaac   PetscFunctionReturn(0);
717166ad2231SToby Isaac }
7172