xref: /petsc/src/dm/impls/plex/plex.c (revision 1428887c243271c486e16324c330c7c9a6d58f62)
134541f0dSBarry Smith #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2d53a3d6fSBarry Smith #include <petsc-private/isimpl.h>
30c312b8eSJed Brown #include <petscsf.h>
4e228b242SToby Isaac #include <petscds.h>
5552f7358SJed Brown 
6552f7358SJed Brown /* Logging support */
73d822a50SMichael Lange PetscLogEvent DMPLEX_Interpolate, PETSCPARTITIONER_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Stratify, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM;
8552f7358SJed Brown 
95a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_Seq(Vec, PetscViewer);
105a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
112c40f234SMatthew G. Knepley PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);
12552f7358SJed Brown 
137e42fee7SMatthew G. Knepley #undef __FUNCT__
147afe7537SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFieldType_Internal"
157afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
167e42fee7SMatthew G. Knepley {
177e42fee7SMatthew G. Knepley   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, vdof = 0, cdof = 0;
187e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
197e42fee7SMatthew G. Knepley 
207e42fee7SMatthew G. Knepley   PetscFunctionBegin;
217e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
22c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
237e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
247e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
257e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
267e42fee7SMatthew G. Knepley   if (field >= 0) {
277e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vdof);CHKERRQ(ierr);}
287e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &cdof);CHKERRQ(ierr);}
297e42fee7SMatthew G. Knepley   } else {
307e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vdof);CHKERRQ(ierr);}
317e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &cdof);CHKERRQ(ierr);}
327e42fee7SMatthew G. Knepley   }
337e42fee7SMatthew G. Knepley   if (vdof) {
347e42fee7SMatthew G. Knepley     *sStart = vStart;
357e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
367e42fee7SMatthew G. Knepley     if (vdof == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
377e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_POINT_FIELD;
387e42fee7SMatthew G. Knepley   } else if (cdof) {
397e42fee7SMatthew G. Knepley     *sStart = cStart;
407e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
417e42fee7SMatthew G. Knepley     if (cdof == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
427e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_CELL_FIELD;
437e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
447e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
457e42fee7SMatthew G. Knepley }
467e42fee7SMatthew G. Knepley 
47552f7358SJed Brown #undef __FUNCT__
48552f7358SJed Brown #define __FUNCT__ "VecView_Plex_Local"
49552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
50552f7358SJed Brown {
51552f7358SJed Brown   DM             dm;
52b136c2c9SMatthew G. Knepley   PetscBool      isvtk, ishdf5, isseq;
53552f7358SJed Brown   PetscErrorCode ierr;
54552f7358SJed Brown 
55552f7358SJed Brown   PetscFunctionBegin;
56552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
5782f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
58552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
59b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
60b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
61ef31f671SMatthew G. Knepley   if (isvtk || ishdf5) {
62ef31f671SMatthew G. Knepley     PetscInt  numFields;
63ef31f671SMatthew G. Knepley     PetscBool fem = PETSC_FALSE;
64ef31f671SMatthew G. Knepley 
65ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
66ef31f671SMatthew G. Knepley     if (numFields) {
67ef31f671SMatthew G. Knepley       PetscObject fe;
68ef31f671SMatthew G. Knepley 
69ef31f671SMatthew G. Knepley       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
70ef31f671SMatthew G. Knepley       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
71ef31f671SMatthew G. Knepley     }
72d7ddef95SMatthew G. Knepley     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
73ef31f671SMatthew G. Knepley   }
74552f7358SJed Brown   if (isvtk) {
75552f7358SJed Brown     PetscSection            section;
76b136c2c9SMatthew G. Knepley     PetscViewerVTKFieldType ft;
77b136c2c9SMatthew G. Knepley     PetscInt                pStart, pEnd;
78552f7358SJed Brown 
79552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
807afe7537SMatthew G. Knepley     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
81552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr); /* viewer drops reference */
82552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
83552f7358SJed Brown     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
84b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
85b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
86b136c2c9SMatthew G. Knepley     ierr = VecView_Plex_Local_HDF5(v, viewer);CHKERRQ(ierr);
87b136c2c9SMatthew G. Knepley #else
88b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
89b136c2c9SMatthew G. Knepley #endif
90552f7358SJed Brown   } else {
9155f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
9255f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
93552f7358SJed Brown   }
94552f7358SJed Brown   PetscFunctionReturn(0);
95552f7358SJed Brown }
96552f7358SJed Brown 
97552f7358SJed Brown #undef __FUNCT__
98552f7358SJed Brown #define __FUNCT__ "VecView_Plex"
99552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
100552f7358SJed Brown {
101552f7358SJed Brown   DM             dm;
10255f2e967SMatthew G. Knepley   PetscBool      isvtk, ishdf5, isseq;
103552f7358SJed Brown   PetscErrorCode ierr;
104552f7358SJed Brown 
105552f7358SJed Brown   PetscFunctionBegin;
106552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
10782f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
108552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
10933c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
11055f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
111552f7358SJed Brown   if (isvtk) {
112552f7358SJed Brown     Vec         locv;
113552f7358SJed Brown     const char *name;
114552f7358SJed Brown 
115552f7358SJed Brown     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
116552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
117552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
118552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
119552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
120552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
121552f7358SJed Brown     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
122b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
123b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
124b136c2c9SMatthew G. Knepley     ierr = VecView_Plex_HDF5(v, viewer);CHKERRQ(ierr);
125b136c2c9SMatthew G. Knepley #else
126b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
127b136c2c9SMatthew G. Knepley #endif
128552f7358SJed Brown   } else {
12955f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
13055f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
131552f7358SJed Brown   }
132552f7358SJed Brown   PetscFunctionReturn(0);
133552f7358SJed Brown }
134552f7358SJed Brown 
135552f7358SJed Brown #undef __FUNCT__
1362c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex_Local"
1372c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
1382c40f234SMatthew G. Knepley {
1392c40f234SMatthew G. Knepley   DM             dm;
1402c40f234SMatthew G. Knepley   PetscBool      ishdf5;
1412c40f234SMatthew G. Knepley   PetscErrorCode ierr;
1422c40f234SMatthew G. Knepley 
1432c40f234SMatthew G. Knepley   PetscFunctionBegin;
1442c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
1452c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
1462c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1472c40f234SMatthew G. Knepley   if (ishdf5) {
1482c40f234SMatthew G. Knepley     DM          dmBC;
1492c40f234SMatthew G. Knepley     Vec         gv;
1502c40f234SMatthew G. Knepley     const char *name;
1512c40f234SMatthew G. Knepley 
1522c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
1532c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
1542c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
1552c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
1562c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
1572c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
1582c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
1592c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
1602c40f234SMatthew G. Knepley   } else {
1612c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
1622c40f234SMatthew G. Knepley   }
1632c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
1642c40f234SMatthew G. Knepley }
1652c40f234SMatthew G. Knepley 
1662c40f234SMatthew G. Knepley #undef __FUNCT__
1672c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex"
1682c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
1692c40f234SMatthew G. Knepley {
1702c40f234SMatthew G. Knepley   DM             dm;
1712c40f234SMatthew G. Knepley   PetscBool      ishdf5;
1722c40f234SMatthew G. Knepley   PetscErrorCode ierr;
1732c40f234SMatthew G. Knepley 
1742c40f234SMatthew G. Knepley   PetscFunctionBegin;
1752c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
1762c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
1772c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
1782c40f234SMatthew G. Knepley   if (ishdf5) {
179878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
180b136c2c9SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5(v, viewer);CHKERRQ(ierr);
181b136c2c9SMatthew G. Knepley #else
182b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
183878b459fSMatthew G. Knepley #endif
1842c40f234SMatthew G. Knepley   } else {
1852c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
186552f7358SJed Brown   }
187552f7358SJed Brown   PetscFunctionReturn(0);
188552f7358SJed Brown }
189552f7358SJed Brown 
190552f7358SJed Brown #undef __FUNCT__
191552f7358SJed Brown #define __FUNCT__ "DMPlexView_Ascii"
192552f7358SJed Brown PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
193552f7358SJed Brown {
194552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
195552f7358SJed Brown   DM                cdm;
196552f7358SJed Brown   DMLabel           markers;
197552f7358SJed Brown   PetscSection      coordSection;
198552f7358SJed Brown   Vec               coordinates;
199552f7358SJed Brown   PetscViewerFormat format;
200552f7358SJed Brown   PetscErrorCode    ierr;
201552f7358SJed Brown 
202552f7358SJed Brown   PetscFunctionBegin;
203552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
204552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
205552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
206552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
207552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
208552f7358SJed Brown     const char *name;
209552f7358SJed Brown     PetscInt    maxConeSize, maxSupportSize;
210552f7358SJed Brown     PetscInt    pStart, pEnd, p;
211552f7358SJed Brown     PetscMPIInt rank, size;
212552f7358SJed Brown 
21382f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
21482f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
215552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
216552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
217552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
218552f7358SJed Brown     ierr = PetscViewerASCIISynchronizedAllow(viewer, PETSC_TRUE);CHKERRQ(ierr);
219552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);CHKERRQ(ierr);
220552f7358SJed Brown     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "Max sizes cone: %D support: %D\n", maxConeSize, maxSupportSize);CHKERRQ(ierr);
221552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
222552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
223552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
224552f7358SJed Brown       PetscInt dof, off, s;
225552f7358SJed Brown 
226552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
227552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
228552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
229552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%D]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
230552f7358SJed Brown       }
231552f7358SJed Brown     }
232552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
233552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
234552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
235552f7358SJed Brown       PetscInt dof, off, c;
236552f7358SJed Brown 
237552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
238552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
239552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
240552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%D]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
241552f7358SJed Brown       }
242552f7358SJed Brown     }
243552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
2440298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
245552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
246552f7358SJed Brown     ierr = DMPlexGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
247552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
248552f7358SJed Brown     if (size > 1) {
249552f7358SJed Brown       PetscSF sf;
250552f7358SJed Brown 
251552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
252552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
253552f7358SJed Brown     }
254552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
255552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
256552f7358SJed Brown     const char  *name;
257552f7358SJed Brown     const char  *colors[3] = {"red", "blue", "green"};
258552f7358SJed Brown     const int    numColors  = 3;
259552f7358SJed Brown     PetscReal    scale      = 2.0;
260552f7358SJed Brown     PetscScalar *coords;
261552f7358SJed Brown     PetscInt     depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
262552f7358SJed Brown     PetscMPIInt  rank, size;
263552f7358SJed Brown 
264552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
26582f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
26682f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
267552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
268552f7358SJed Brown     ierr = PetscViewerASCIISynchronizedAllow(viewer, PETSC_TRUE);CHKERRQ(ierr);
269770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
270770b213bSMatthew G Knepley \\documentclass[crop,multi=false]{standalone}\n\n\
271552f7358SJed Brown \\usepackage{tikz}\n\
272552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
273552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
274552f7358SJed Brown \\usetikzlibrary{arrows}\n\
275552f7358SJed Brown \\begin{document}\n\
276f738dd31SMatthew G. Knepley \\vbox{\n");CHKERRQ(ierr);
277f738dd31SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
278770b213bSMatthew G Knepley     for (p = 0; p < size; ++p) {
279770b213bSMatthew G Knepley       if (p > 0 && p == size-1) {
280770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
281770b213bSMatthew G Knepley       } else if (p > 0) {
282770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
283770b213bSMatthew G Knepley       }
284770b213bSMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
285770b213bSMatthew G Knepley     }
286770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n\
287f738dd31SMatthew G. Knepley \\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 8.0/scale);CHKERRQ(ierr);
288552f7358SJed Brown     /* Plot vertices */
289552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
290552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
291552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
292552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
293552f7358SJed Brown       PetscInt off, dof, d;
294552f7358SJed Brown 
295552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
296552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
297552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
298552f7358SJed Brown       for (d = 0; d < dof; ++d) {
299552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
3006712e2f1SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)(scale*PetscRealPart(coords[off+d])));CHKERRQ(ierr);
301552f7358SJed Brown       }
302552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%D) [draw,shape=circle,color=%s] {%D} --\n", v, rank, colors[rank%numColors], v);CHKERRQ(ierr);
303552f7358SJed Brown     }
304552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
305552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
306552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
307552f7358SJed Brown     /* Plot edges */
308552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
309552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
310552f7358SJed Brown     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
311552f7358SJed Brown     for (e = eStart; e < eEnd; ++e) {
312552f7358SJed Brown       const PetscInt *cone;
313552f7358SJed Brown       PetscInt        coneSize, offA, offB, dof, d;
314552f7358SJed Brown 
315552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
31682f516ccSBarry Smith       if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %d cone should have two vertices, not %d", e, coneSize);
317552f7358SJed Brown       ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
318552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
319552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
320552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
321552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
322552f7358SJed Brown       for (d = 0; d < dof; ++d) {
323552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
3246712e2f1SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)(scale*0.5*PetscRealPart(coords[offA+d]+coords[offB+d])));CHKERRQ(ierr);
325552f7358SJed Brown       }
326552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%D) [draw,shape=circle,color=%s] {%D} --\n", e, rank, colors[rank%numColors], e);CHKERRQ(ierr);
327552f7358SJed Brown     }
328552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
329552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
330552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
331552f7358SJed Brown     /* Plot cells */
332552f7358SJed Brown     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
333552f7358SJed Brown     for (c = cStart; c < cEnd; ++c) {
3340298fd71SBarry Smith       PetscInt *closure = NULL;
335552f7358SJed Brown       PetscInt  closureSize, firstPoint = -1;
336552f7358SJed Brown 
337552f7358SJed Brown       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
338552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
339552f7358SJed Brown       for (p = 0; p < closureSize*2; p += 2) {
340552f7358SJed Brown         const PetscInt point = closure[p];
341552f7358SJed Brown 
342552f7358SJed Brown         if ((point < vStart) || (point >= vEnd)) continue;
343552f7358SJed Brown         if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
344552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%D)", point, rank);CHKERRQ(ierr);
345552f7358SJed Brown         if (firstPoint < 0) firstPoint = point;
346552f7358SJed Brown       }
347552f7358SJed Brown       /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
348552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%D);\n", firstPoint, rank);CHKERRQ(ierr);
349552f7358SJed Brown       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
350552f7358SJed Brown     }
351552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
352f738dd31SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n}\n");CHKERRQ(ierr);
353770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
354552f7358SJed Brown   } else {
35582f516ccSBarry Smith     MPI_Comm    comm;
356834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
357834065abSMatthew G. Knepley     PetscInt    locDepth, depth, dim, d, pMax[4];
358552f7358SJed Brown     PetscInt    pStart, pEnd, p;
359a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
3601143a3c0SMatthew G. Knepley     const char *name;
361552f7358SJed Brown     PetscMPIInt size;
362552f7358SJed Brown 
36382f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
364552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
365c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3665f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
3671143a3c0SMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);CHKERRQ(ierr);}
3681143a3c0SMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);CHKERRQ(ierr);}
369552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
370552f7358SJed Brown     ierr = MPI_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
371bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
372dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
373552f7358SJed Brown     if (depth == 1) {
374552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
375552f7358SJed Brown       pEnd = pEnd - pStart;
376552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
377552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", 0);CHKERRQ(ierr);
378552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
379552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
380552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
381552f7358SJed Brown       pEnd = pEnd - pStart;
382552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
383552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
384552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
385552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
386552f7358SJed Brown     } else {
387552f7358SJed Brown       for (d = 0; d <= dim; d++) {
388552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
389834065abSMatthew G. Knepley         pEnd    -= pStart;
390834065abSMatthew G. Knepley         pMax[d] -= pStart;
391552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
392834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
393552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
394834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
395834065abSMatthew G. Knepley           if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
396834065abSMatthew G. Knepley           else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
397834065abSMatthew G. Knepley         }
398552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
399552f7358SJed Brown       }
400552f7358SJed Brown     }
401834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
402a57dd577SMatthew G Knepley     ierr = DMPlexGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
403a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
404a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
405a57dd577SMatthew G Knepley       DMLabel         label;
406a57dd577SMatthew G Knepley       const char     *name;
407a57dd577SMatthew G Knepley       IS              valueIS;
408a57dd577SMatthew G Knepley       const PetscInt *values;
409a57dd577SMatthew G Knepley       PetscInt        numValues, v;
410a57dd577SMatthew G Knepley 
411a57dd577SMatthew G Knepley       ierr = DMPlexGetLabelName(dm, l, &name);CHKERRQ(ierr);
412a57dd577SMatthew G Knepley       ierr = DMPlexGetLabel(dm, name, &label);CHKERRQ(ierr);
413a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
414a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %d strata of sizes (", name, numValues);CHKERRQ(ierr);
415a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
416a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
417a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
418a57dd577SMatthew G Knepley         PetscInt size;
419a57dd577SMatthew G Knepley 
420a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
421a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
422a57dd577SMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%d", size);CHKERRQ(ierr);
423a57dd577SMatthew G Knepley       }
424a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
425a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
4264d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
427a57dd577SMatthew G Knepley     }
428552f7358SJed Brown   }
429552f7358SJed Brown   PetscFunctionReturn(0);
430552f7358SJed Brown }
431552f7358SJed Brown 
432552f7358SJed Brown #undef __FUNCT__
433552f7358SJed Brown #define __FUNCT__ "DMView_Plex"
434552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
435552f7358SJed Brown {
436fcf6c8fdSToby Isaac   PetscBool      iascii, ishdf5, isvtk;
437552f7358SJed Brown   PetscErrorCode ierr;
438552f7358SJed Brown 
439552f7358SJed Brown   PetscFunctionBegin;
440552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
441552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
442552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
443fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
444c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
445552f7358SJed Brown   if (iascii) {
446552f7358SJed Brown     ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
447c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
448c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4497afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
450c6ccd67eSMatthew G. Knepley     ierr = DMPlexView_HDF5(dm, viewer);CHKERRQ(ierr);
4517afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
452c6ccd67eSMatthew G. Knepley #else
453c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
454552f7358SJed Brown #endif
45511aeaf0aSBarry Smith   }
456fcf6c8fdSToby Isaac   else if (isvtk) {
457fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
458fcf6c8fdSToby Isaac   }
459552f7358SJed Brown   PetscFunctionReturn(0);
460552f7358SJed Brown }
461552f7358SJed Brown 
4622c40f234SMatthew G. Knepley #undef __FUNCT__
4632c40f234SMatthew G. Knepley #define __FUNCT__ "DMLoad_Plex"
4642c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
4652c40f234SMatthew G. Knepley {
4662c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4672c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4682c40f234SMatthew G. Knepley 
4692c40f234SMatthew G. Knepley   PetscFunctionBegin;
4702c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4712c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
4722c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
4732c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
4742c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
4752c40f234SMatthew G. Knepley   else if (ishdf5) {
4762c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
47710b7db4eSMatthew G. Knepley     ierr = DMPlexLoad_HDF5(dm, viewer);CHKERRQ(ierr);
4782c40f234SMatthew G. Knepley #else
4792c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
480552f7358SJed Brown #endif
481552f7358SJed Brown   }
482552f7358SJed Brown   PetscFunctionReturn(0);
483552f7358SJed Brown }
484552f7358SJed Brown 
485552f7358SJed Brown #undef __FUNCT__
4866c73c22cSMatthew G. Knepley #define __FUNCT__ "BoundaryDestroy"
4876c73c22cSMatthew G. Knepley static PetscErrorCode BoundaryDestroy(DMBoundary *boundary)
4886c73c22cSMatthew G. Knepley {
4896c73c22cSMatthew G. Knepley   DMBoundary     b, next;
4906c73c22cSMatthew G. Knepley   PetscErrorCode ierr;
4916c73c22cSMatthew G. Knepley 
4928e136ac0SMatthew G. Knepley   PetscFunctionBegin;
4936c73c22cSMatthew G. Knepley   if (!boundary) PetscFunctionReturn(0);
4946c73c22cSMatthew G. Knepley   b = *boundary;
4956c73c22cSMatthew G. Knepley   *boundary = NULL;
4966c73c22cSMatthew G. Knepley   for (; b; b = next) {
4976c73c22cSMatthew G. Knepley     next = b->next;
4986c73c22cSMatthew G. Knepley     ierr = PetscFree(b->ids);CHKERRQ(ierr);
4996c73c22cSMatthew G. Knepley     ierr = PetscFree(b->name);CHKERRQ(ierr);
50063d5297fSMatthew G. Knepley     ierr = PetscFree(b->labelname);CHKERRQ(ierr);
5016c73c22cSMatthew G. Knepley     ierr = PetscFree(b);CHKERRQ(ierr);
5026c73c22cSMatthew G. Knepley   }
5036c73c22cSMatthew G. Knepley   PetscFunctionReturn(0);
5046c73c22cSMatthew G. Knepley }
5056c73c22cSMatthew G. Knepley 
5066c73c22cSMatthew G. Knepley #undef __FUNCT__
507552f7358SJed Brown #define __FUNCT__ "DMDestroy_Plex"
508552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
509552f7358SJed Brown {
510552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
511a85475f2SMatthew G. Knepley   PlexLabel      next = mesh->labels;
512552f7358SJed Brown   PetscErrorCode ierr;
513552f7358SJed Brown 
514552f7358SJed Brown   PetscFunctionBegin;
5150d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
516552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
517552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
518552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
519552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
520552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
521552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
522d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
523d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
52477623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
525552f7358SJed Brown   while (next) {
526a85475f2SMatthew G. Knepley     PlexLabel tmp = next->next;
527552f7358SJed Brown 
528a85475f2SMatthew G. Knepley     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
529a85475f2SMatthew G. Knepley     ierr = PetscFree(next);CHKERRQ(ierr);
530552f7358SJed Brown     next = tmp;
531552f7358SJed Brown   }
5323913d7c8SMatthew G. Knepley   ierr = DMDestroy(&mesh->coarseMesh);CHKERRQ(ierr);
533a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
534552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
535552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
5366c73c22cSMatthew G. Knepley   ierr = BoundaryDestroy(&mesh->boundary);CHKERRQ(ierr);
537a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
538a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
539d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
540d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
541d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
542d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
543d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
544d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
545552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
546552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
547552f7358SJed Brown   PetscFunctionReturn(0);
548552f7358SJed Brown }
549552f7358SJed Brown 
550552f7358SJed Brown #undef __FUNCT__
551552f7358SJed Brown #define __FUNCT__ "DMCreateMatrix_Plex"
552b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
553552f7358SJed Brown {
554552f7358SJed Brown   PetscSection   section, sectionGlobal;
555552f7358SJed Brown   PetscInt       bs = -1;
556552f7358SJed Brown   PetscInt       localSize;
55789545effSMatthew G. Knepley   PetscBool      isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock;
558552f7358SJed Brown   PetscErrorCode ierr;
559b412c318SBarry Smith   MatType        mtype;
560*1428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
561552f7358SJed Brown 
562552f7358SJed Brown   PetscFunctionBegin;
563607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
564b412c318SBarry Smith   mtype = dm->mattype;
565552f7358SJed Brown   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
566552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
567552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
568552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
56982f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
570552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
571552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
572552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
573552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
574552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
575552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
576552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
577552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
578552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
579552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
580552f7358SJed Brown   if (!isShell) {
581552f7358SJed Brown     PetscBool fillMatrix = (PetscBool) !dm->prealloc_only;
5822a28c762SMatthew G Knepley     PetscInt *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin;
583552f7358SJed Brown 
584552f7358SJed Brown     if (bs < 0) {
585552f7358SJed Brown       if (isBlock || isSeqBlock || isMPIBlock || isSymBlock || isSymSeqBlock || isSymMPIBlock) {
586fad22124SMatthew G Knepley         PetscInt pStart, pEnd, p, dof, cdof;
587552f7358SJed Brown 
588552f7358SJed Brown         ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
589552f7358SJed Brown         for (p = pStart; p < pEnd; ++p) {
590552f7358SJed Brown           ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
591fad22124SMatthew G Knepley           ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
592fad22124SMatthew G Knepley           if (dof-cdof) {
5932a28c762SMatthew G Knepley             if (bs < 0) {
594fad22124SMatthew G Knepley               bs = dof-cdof;
595fad22124SMatthew G Knepley             } else if (bs != dof-cdof) {
5962a28c762SMatthew G Knepley               /* Layout does not admit a pointwise block size */
5972a28c762SMatthew G Knepley               bs = 1;
598552f7358SJed Brown               break;
599552f7358SJed Brown             }
600552f7358SJed Brown           }
6012a28c762SMatthew G Knepley         }
6022a28c762SMatthew G Knepley         /* Must have same blocksize on all procs (some might have no points) */
6032a28c762SMatthew G Knepley         bsLocal = bs;
60482f516ccSBarry Smith         ierr = MPI_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
6052a28c762SMatthew G Knepley         bsLocal = bs < 0 ? bsMax : bs;
60682f516ccSBarry Smith         ierr = MPI_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
6072a28c762SMatthew G Knepley         if (bsMin != bsMax) {
6082a28c762SMatthew G Knepley           bs = 1;
6092a28c762SMatthew G Knepley         } else {
6102a28c762SMatthew G Knepley           bs = bsMax;
6112a28c762SMatthew G Knepley         }
612552f7358SJed Brown       } else {
613552f7358SJed Brown         bs = 1;
614552f7358SJed Brown       }
615552f7358SJed Brown     }
6161795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
617552f7358SJed Brown     ierr = DMPlexPreallocateOperator(dm, bs, section, sectionGlobal, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
618552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
619*1428887cSShri Abhyankar 
620*1428887cSShri Abhyankar     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work */
621*1428887cSShri Abhyankar     ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
622*1428887cSShri Abhyankar     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
623552f7358SJed Brown   }
624552f7358SJed Brown   PetscFunctionReturn(0);
625552f7358SJed Brown }
626552f7358SJed Brown 
627552f7358SJed Brown #undef __FUNCT__
628552f7358SJed Brown #define __FUNCT__ "DMPlexGetChart"
629552f7358SJed Brown /*@
630552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
631552f7358SJed Brown 
632552f7358SJed Brown   Not collective
633552f7358SJed Brown 
634552f7358SJed Brown   Input Parameter:
635552f7358SJed Brown . mesh - The DMPlex
636552f7358SJed Brown 
637552f7358SJed Brown   Output Parameters:
638552f7358SJed Brown + pStart - The first mesh point
639552f7358SJed Brown - pEnd   - The upper bound for mesh points
640552f7358SJed Brown 
641552f7358SJed Brown   Level: beginner
642552f7358SJed Brown 
643552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
644552f7358SJed Brown @*/
645552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
646552f7358SJed Brown {
647552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
648552f7358SJed Brown   PetscErrorCode ierr;
649552f7358SJed Brown 
650552f7358SJed Brown   PetscFunctionBegin;
651552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
652552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
653552f7358SJed Brown   PetscFunctionReturn(0);
654552f7358SJed Brown }
655552f7358SJed Brown 
656552f7358SJed Brown #undef __FUNCT__
657552f7358SJed Brown #define __FUNCT__ "DMPlexSetChart"
658552f7358SJed Brown /*@
659552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
660552f7358SJed Brown 
661552f7358SJed Brown   Not collective
662552f7358SJed Brown 
663552f7358SJed Brown   Input Parameters:
664552f7358SJed Brown + mesh - The DMPlex
665552f7358SJed Brown . pStart - The first mesh point
666552f7358SJed Brown - pEnd   - The upper bound for mesh points
667552f7358SJed Brown 
668552f7358SJed Brown   Output Parameters:
669552f7358SJed Brown 
670552f7358SJed Brown   Level: beginner
671552f7358SJed Brown 
672552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
673552f7358SJed Brown @*/
674552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
675552f7358SJed Brown {
676552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
677552f7358SJed Brown   PetscErrorCode ierr;
678552f7358SJed Brown 
679552f7358SJed Brown   PetscFunctionBegin;
680552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
681552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
682552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
683552f7358SJed Brown   PetscFunctionReturn(0);
684552f7358SJed Brown }
685552f7358SJed Brown 
686552f7358SJed Brown #undef __FUNCT__
687552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSize"
688552f7358SJed Brown /*@
689552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
690552f7358SJed Brown 
691552f7358SJed Brown   Not collective
692552f7358SJed Brown 
693552f7358SJed Brown   Input Parameters:
694552f7358SJed Brown + mesh - The DMPlex
695552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
696552f7358SJed Brown 
697552f7358SJed Brown   Output Parameter:
698552f7358SJed Brown . size - The cone size for point p
699552f7358SJed Brown 
700552f7358SJed Brown   Level: beginner
701552f7358SJed Brown 
702552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
703552f7358SJed Brown @*/
704552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
705552f7358SJed Brown {
706552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
707552f7358SJed Brown   PetscErrorCode ierr;
708552f7358SJed Brown 
709552f7358SJed Brown   PetscFunctionBegin;
710552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
711552f7358SJed Brown   PetscValidPointer(size, 3);
712552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
713552f7358SJed Brown   PetscFunctionReturn(0);
714552f7358SJed Brown }
715552f7358SJed Brown 
716552f7358SJed Brown #undef __FUNCT__
717552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeSize"
718552f7358SJed Brown /*@
719552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
720552f7358SJed Brown 
721552f7358SJed Brown   Not collective
722552f7358SJed Brown 
723552f7358SJed Brown   Input Parameters:
724552f7358SJed Brown + mesh - The DMPlex
725552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
726552f7358SJed Brown - size - The cone size for point p
727552f7358SJed Brown 
728552f7358SJed Brown   Output Parameter:
729552f7358SJed Brown 
730552f7358SJed Brown   Note:
731552f7358SJed Brown   This should be called after DMPlexSetChart().
732552f7358SJed Brown 
733552f7358SJed Brown   Level: beginner
734552f7358SJed Brown 
735552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
736552f7358SJed Brown @*/
737552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
738552f7358SJed Brown {
739552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
740552f7358SJed Brown   PetscErrorCode ierr;
741552f7358SJed Brown 
742552f7358SJed Brown   PetscFunctionBegin;
743552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
744552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
7450d644c17SKarl Rupp 
746552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
747552f7358SJed Brown   PetscFunctionReturn(0);
748552f7358SJed Brown }
749552f7358SJed Brown 
750552f7358SJed Brown #undef __FUNCT__
751f5a469b9SMatthew G. Knepley #define __FUNCT__ "DMPlexAddConeSize"
752f5a469b9SMatthew G. Knepley /*@
753f5a469b9SMatthew G. Knepley   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG
754f5a469b9SMatthew G. Knepley 
755f5a469b9SMatthew G. Knepley   Not collective
756f5a469b9SMatthew G. Knepley 
757f5a469b9SMatthew G. Knepley   Input Parameters:
758f5a469b9SMatthew G. Knepley + mesh - The DMPlex
759f5a469b9SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
760f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
761f5a469b9SMatthew G. Knepley 
762f5a469b9SMatthew G. Knepley   Output Parameter:
763f5a469b9SMatthew G. Knepley 
764f5a469b9SMatthew G. Knepley   Note:
765f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
766f5a469b9SMatthew G. Knepley 
767f5a469b9SMatthew G. Knepley   Level: beginner
768f5a469b9SMatthew G. Knepley 
769f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
770f5a469b9SMatthew G. Knepley @*/
771f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
772f5a469b9SMatthew G. Knepley {
773f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
774f5a469b9SMatthew G. Knepley   PetscInt       csize;
775f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
776f5a469b9SMatthew G. Knepley 
777f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
778f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
779f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
780f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
781f5a469b9SMatthew G. Knepley 
782f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
783f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
784f5a469b9SMatthew G. Knepley }
785f5a469b9SMatthew G. Knepley 
786f5a469b9SMatthew G. Knepley #undef __FUNCT__
787552f7358SJed Brown #define __FUNCT__ "DMPlexGetCone"
788552f7358SJed Brown /*@C
789552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
790552f7358SJed Brown 
791552f7358SJed Brown   Not collective
792552f7358SJed Brown 
793552f7358SJed Brown   Input Parameters:
794552f7358SJed Brown + mesh - The DMPlex
795552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
796552f7358SJed Brown 
797552f7358SJed Brown   Output Parameter:
798552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
799552f7358SJed Brown 
800552f7358SJed Brown   Level: beginner
801552f7358SJed Brown 
8023813dfbdSMatthew G Knepley   Fortran Notes:
8033813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
8043813dfbdSMatthew G Knepley   include petsc.h90 in your code.
8053813dfbdSMatthew G Knepley 
8063813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
807552f7358SJed Brown 
808552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
809552f7358SJed Brown @*/
810552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
811552f7358SJed Brown {
812552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
813552f7358SJed Brown   PetscInt       off;
814552f7358SJed Brown   PetscErrorCode ierr;
815552f7358SJed Brown 
816552f7358SJed Brown   PetscFunctionBegin;
817552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
818552f7358SJed Brown   PetscValidPointer(cone, 3);
819552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
820552f7358SJed Brown   *cone = &mesh->cones[off];
821552f7358SJed Brown   PetscFunctionReturn(0);
822552f7358SJed Brown }
823552f7358SJed Brown 
824552f7358SJed Brown #undef __FUNCT__
825552f7358SJed Brown #define __FUNCT__ "DMPlexSetCone"
826552f7358SJed Brown /*@
827552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
828552f7358SJed Brown 
829552f7358SJed Brown   Not collective
830552f7358SJed Brown 
831552f7358SJed Brown   Input Parameters:
832552f7358SJed Brown + mesh - The DMPlex
833552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
834552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
835552f7358SJed Brown 
836552f7358SJed Brown   Output Parameter:
837552f7358SJed Brown 
838552f7358SJed Brown   Note:
839552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
840552f7358SJed Brown 
841552f7358SJed Brown   Level: beginner
842552f7358SJed Brown 
843552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
844552f7358SJed Brown @*/
845552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
846552f7358SJed Brown {
847552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
848552f7358SJed Brown   PetscInt       pStart, pEnd;
849552f7358SJed Brown   PetscInt       dof, off, c;
850552f7358SJed Brown   PetscErrorCode ierr;
851552f7358SJed Brown 
852552f7358SJed Brown   PetscFunctionBegin;
853552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
854552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
855552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
856552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
857552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
85882f516ccSBarry 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);
859552f7358SJed Brown   for (c = 0; c < dof; ++c) {
86082f516ccSBarry 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);
861552f7358SJed Brown     mesh->cones[off+c] = cone[c];
862552f7358SJed Brown   }
863552f7358SJed Brown   PetscFunctionReturn(0);
864552f7358SJed Brown }
865552f7358SJed Brown 
866552f7358SJed Brown #undef __FUNCT__
867552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientation"
868552f7358SJed Brown /*@C
869552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
870552f7358SJed Brown 
871552f7358SJed Brown   Not collective
872552f7358SJed Brown 
873552f7358SJed Brown   Input Parameters:
874552f7358SJed Brown + mesh - The DMPlex
875552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
876552f7358SJed Brown 
877552f7358SJed Brown   Output Parameter:
878552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
879552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
880552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
881552f7358SJed Brown                     the index of the cone point on which to start.
882552f7358SJed Brown 
883552f7358SJed Brown   Level: beginner
884552f7358SJed Brown 
8853813dfbdSMatthew G Knepley   Fortran Notes:
8863813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
8873813dfbdSMatthew G Knepley   include petsc.h90 in your code.
8883813dfbdSMatthew G Knepley 
8893813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
890552f7358SJed Brown 
891552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
892552f7358SJed Brown @*/
893552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
894552f7358SJed Brown {
895552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
896552f7358SJed Brown   PetscInt       off;
897552f7358SJed Brown   PetscErrorCode ierr;
898552f7358SJed Brown 
899552f7358SJed Brown   PetscFunctionBegin;
900552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
901552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
902552f7358SJed Brown   {
903552f7358SJed Brown     PetscInt dof;
904552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
905552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
906552f7358SJed Brown   }
907552f7358SJed Brown #endif
908552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
9090d644c17SKarl Rupp 
910552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
911552f7358SJed Brown   PetscFunctionReturn(0);
912552f7358SJed Brown }
913552f7358SJed Brown 
914552f7358SJed Brown #undef __FUNCT__
915552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeOrientation"
916552f7358SJed Brown /*@
917552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
918552f7358SJed Brown 
919552f7358SJed Brown   Not collective
920552f7358SJed Brown 
921552f7358SJed Brown   Input Parameters:
922552f7358SJed Brown + mesh - The DMPlex
923552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
924552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
925552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
926552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
927552f7358SJed Brown                     the index of the cone point on which to start.
928552f7358SJed Brown 
929552f7358SJed Brown   Output Parameter:
930552f7358SJed Brown 
931552f7358SJed Brown   Note:
932552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
933552f7358SJed Brown 
934552f7358SJed Brown   Level: beginner
935552f7358SJed Brown 
936552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
937552f7358SJed Brown @*/
938552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
939552f7358SJed Brown {
940552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
941552f7358SJed Brown   PetscInt       pStart, pEnd;
942552f7358SJed Brown   PetscInt       dof, off, c;
943552f7358SJed Brown   PetscErrorCode ierr;
944552f7358SJed Brown 
945552f7358SJed Brown   PetscFunctionBegin;
946552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
947552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
948552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
949552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
950552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
95182f516ccSBarry 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);
952552f7358SJed Brown   for (c = 0; c < dof; ++c) {
953552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
954552f7358SJed Brown 
955552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
95682f516ccSBarry 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);
957552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
958552f7358SJed Brown   }
959552f7358SJed Brown   PetscFunctionReturn(0);
960552f7358SJed Brown }
961552f7358SJed Brown 
962552f7358SJed Brown #undef __FUNCT__
963552f7358SJed Brown #define __FUNCT__ "DMPlexInsertCone"
964552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
965552f7358SJed Brown {
966552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
967552f7358SJed Brown   PetscInt       pStart, pEnd;
968552f7358SJed Brown   PetscInt       dof, off;
969552f7358SJed Brown   PetscErrorCode ierr;
970552f7358SJed Brown 
971552f7358SJed Brown   PetscFunctionBegin;
972552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
973552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
97482f516ccSBarry 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);
97582f516ccSBarry 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);
97677c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
97777c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
97877c88f5bSMatthew 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);
979552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
980552f7358SJed Brown   PetscFunctionReturn(0);
981552f7358SJed Brown }
982552f7358SJed Brown 
983552f7358SJed Brown #undef __FUNCT__
98477c88f5bSMatthew G Knepley #define __FUNCT__ "DMPlexInsertConeOrientation"
98577c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
98677c88f5bSMatthew G Knepley {
98777c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
98877c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
98977c88f5bSMatthew G Knepley   PetscInt       dof, off;
99077c88f5bSMatthew G Knepley   PetscErrorCode ierr;
99177c88f5bSMatthew G Knepley 
99277c88f5bSMatthew G Knepley   PetscFunctionBegin;
99377c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99477c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
99577c88f5bSMatthew 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);
99677c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
99777c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
99877c88f5bSMatthew 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);
99977c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
100077c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
100177c88f5bSMatthew G Knepley }
100277c88f5bSMatthew G Knepley 
100377c88f5bSMatthew G Knepley #undef __FUNCT__
1004552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupportSize"
1005552f7358SJed Brown /*@
1006552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
1007552f7358SJed Brown 
1008552f7358SJed Brown   Not collective
1009552f7358SJed Brown 
1010552f7358SJed Brown   Input Parameters:
1011552f7358SJed Brown + mesh - The DMPlex
1012552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1013552f7358SJed Brown 
1014552f7358SJed Brown   Output Parameter:
1015552f7358SJed Brown . size - The support size for point p
1016552f7358SJed Brown 
1017552f7358SJed Brown   Level: beginner
1018552f7358SJed Brown 
1019552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1020552f7358SJed Brown @*/
1021552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1022552f7358SJed Brown {
1023552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1024552f7358SJed Brown   PetscErrorCode ierr;
1025552f7358SJed Brown 
1026552f7358SJed Brown   PetscFunctionBegin;
1027552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1028552f7358SJed Brown   PetscValidPointer(size, 3);
1029552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1030552f7358SJed Brown   PetscFunctionReturn(0);
1031552f7358SJed Brown }
1032552f7358SJed Brown 
1033552f7358SJed Brown #undef __FUNCT__
1034552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupportSize"
1035552f7358SJed Brown /*@
1036552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
1037552f7358SJed Brown 
1038552f7358SJed Brown   Not collective
1039552f7358SJed Brown 
1040552f7358SJed Brown   Input Parameters:
1041552f7358SJed Brown + mesh - The DMPlex
1042552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1043552f7358SJed Brown - size - The support size for point p
1044552f7358SJed Brown 
1045552f7358SJed Brown   Output Parameter:
1046552f7358SJed Brown 
1047552f7358SJed Brown   Note:
1048552f7358SJed Brown   This should be called after DMPlexSetChart().
1049552f7358SJed Brown 
1050552f7358SJed Brown   Level: beginner
1051552f7358SJed Brown 
1052552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1053552f7358SJed Brown @*/
1054552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1055552f7358SJed Brown {
1056552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1057552f7358SJed Brown   PetscErrorCode ierr;
1058552f7358SJed Brown 
1059552f7358SJed Brown   PetscFunctionBegin;
1060552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1061552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
10620d644c17SKarl Rupp 
1063552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1064552f7358SJed Brown   PetscFunctionReturn(0);
1065552f7358SJed Brown }
1066552f7358SJed Brown 
1067552f7358SJed Brown #undef __FUNCT__
1068552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupport"
1069552f7358SJed Brown /*@C
1070552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
1071552f7358SJed Brown 
1072552f7358SJed Brown   Not collective
1073552f7358SJed Brown 
1074552f7358SJed Brown   Input Parameters:
1075552f7358SJed Brown + mesh - The DMPlex
1076552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1077552f7358SJed Brown 
1078552f7358SJed Brown   Output Parameter:
1079552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1080552f7358SJed Brown 
1081552f7358SJed Brown   Level: beginner
1082552f7358SJed Brown 
10833813dfbdSMatthew G Knepley   Fortran Notes:
10843813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
10853813dfbdSMatthew G Knepley   include petsc.h90 in your code.
10863813dfbdSMatthew G Knepley 
10873813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
10883813dfbdSMatthew G Knepley 
1089552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1090552f7358SJed Brown @*/
1091552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1092552f7358SJed Brown {
1093552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1094552f7358SJed Brown   PetscInt       off;
1095552f7358SJed Brown   PetscErrorCode ierr;
1096552f7358SJed Brown 
1097552f7358SJed Brown   PetscFunctionBegin;
1098552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1099552f7358SJed Brown   PetscValidPointer(support, 3);
1100552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1101552f7358SJed Brown   *support = &mesh->supports[off];
1102552f7358SJed Brown   PetscFunctionReturn(0);
1103552f7358SJed Brown }
1104552f7358SJed Brown 
1105552f7358SJed Brown #undef __FUNCT__
1106552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupport"
1107552f7358SJed Brown /*@
1108552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
1109552f7358SJed Brown 
1110552f7358SJed Brown   Not collective
1111552f7358SJed Brown 
1112552f7358SJed Brown   Input Parameters:
1113552f7358SJed Brown + mesh - The DMPlex
1114552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1115552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1116552f7358SJed Brown 
1117552f7358SJed Brown   Output Parameter:
1118552f7358SJed Brown 
1119552f7358SJed Brown   Note:
1120552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1121552f7358SJed Brown 
1122552f7358SJed Brown   Level: beginner
1123552f7358SJed Brown 
1124552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1125552f7358SJed Brown @*/
1126552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1127552f7358SJed Brown {
1128552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1129552f7358SJed Brown   PetscInt       pStart, pEnd;
1130552f7358SJed Brown   PetscInt       dof, off, c;
1131552f7358SJed Brown   PetscErrorCode ierr;
1132552f7358SJed Brown 
1133552f7358SJed Brown   PetscFunctionBegin;
1134552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1135552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1136552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1137552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1138552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
113982f516ccSBarry 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);
1140552f7358SJed Brown   for (c = 0; c < dof; ++c) {
114182f516ccSBarry 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);
1142552f7358SJed Brown     mesh->supports[off+c] = support[c];
1143552f7358SJed Brown   }
1144552f7358SJed Brown   PetscFunctionReturn(0);
1145552f7358SJed Brown }
1146552f7358SJed Brown 
1147552f7358SJed Brown #undef __FUNCT__
1148552f7358SJed Brown #define __FUNCT__ "DMPlexInsertSupport"
1149552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1150552f7358SJed Brown {
1151552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1152552f7358SJed Brown   PetscInt       pStart, pEnd;
1153552f7358SJed Brown   PetscInt       dof, off;
1154552f7358SJed Brown   PetscErrorCode ierr;
1155552f7358SJed Brown 
1156552f7358SJed Brown   PetscFunctionBegin;
1157552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1158552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1159552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1160552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
116182f516ccSBarry 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);
116282f516ccSBarry 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);
116382f516ccSBarry 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);
1164552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1165552f7358SJed Brown   PetscFunctionReturn(0);
1166552f7358SJed Brown }
1167552f7358SJed Brown 
1168552f7358SJed Brown #undef __FUNCT__
1169552f7358SJed Brown #define __FUNCT__ "DMPlexGetTransitiveClosure"
1170552f7358SJed Brown /*@C
1171552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1172552f7358SJed Brown 
1173552f7358SJed Brown   Not collective
1174552f7358SJed Brown 
1175552f7358SJed Brown   Input Parameters:
1176552f7358SJed Brown + mesh - The DMPlex
1177552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1178552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
11790298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1180552f7358SJed Brown 
1181552f7358SJed Brown   Output Parameters:
1182552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1183552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1184552f7358SJed Brown 
1185552f7358SJed Brown   Note:
11860298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1187552f7358SJed Brown 
11883813dfbdSMatthew G Knepley   Fortran Notes:
11893813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
11903813dfbdSMatthew G Knepley   include petsc.h90 in your code.
11913813dfbdSMatthew G Knepley 
11923813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
11933813dfbdSMatthew G Knepley 
1194552f7358SJed Brown   Level: beginner
1195552f7358SJed Brown 
1196552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1197552f7358SJed Brown @*/
1198552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1199552f7358SJed Brown {
1200552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1201552f7358SJed Brown   PetscInt       *closure, *fifo;
12020298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1203552f7358SJed Brown   PetscInt        tmpSize, t;
1204552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1205552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1206552f7358SJed Brown   PetscErrorCode  ierr;
1207552f7358SJed Brown 
1208552f7358SJed Brown   PetscFunctionBegin;
1209552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1210552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1211552f7358SJed Brown   /* This is only 1-level */
1212552f7358SJed Brown   if (useCone) {
1213552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1214552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1215552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1216552f7358SJed Brown   } else {
1217552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1218552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1219552f7358SJed Brown   }
1220bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1221bfbcdd7aSMatthew G. Knepley     if (*points) {
1222bfbcdd7aSMatthew G. Knepley       closure = *points;
1223bfbcdd7aSMatthew G. Knepley     } else {
1224bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1225bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1226bfbcdd7aSMatthew G. Knepley     }
1227bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1228bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1229bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1230bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1231bfbcdd7aSMatthew G. Knepley     }
1232bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1233bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1234bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1235bfbcdd7aSMatthew G. Knepley   }
123624c766afSToby Isaac   {
123724c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
123824c766afSToby Isaac 
123924c766afSToby Isaac     c = mesh->maxConeSize;
124024c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
124124c766afSToby Isaac     s = mesh->maxSupportSize;
124224c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
124324c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
124424c766afSToby Isaac   }
1245bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1246bfbcdd7aSMatthew G. Knepley   if (*points) {
1247bfbcdd7aSMatthew G. Knepley     closure = *points;
1248bfbcdd7aSMatthew G. Knepley   } else {
1249bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1250bfbcdd7aSMatthew G. Knepley   }
1251bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1252552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1253552f7358SJed Brown     const PetscInt cp = tmp[t];
1254552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1255552f7358SJed Brown 
1256552f7358SJed Brown     closure[closureSize]   = cp;
1257552f7358SJed Brown     closure[closureSize+1] = co;
1258552f7358SJed Brown     fifo[fifoSize]         = cp;
1259552f7358SJed Brown     fifo[fifoSize+1]       = co;
1260552f7358SJed Brown   }
1261bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1262552f7358SJed Brown   while (fifoSize - fifoStart) {
1263552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1264552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1265552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1266552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1267552f7358SJed Brown 
1268552f7358SJed Brown     if (useCone) {
1269552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1270552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1271552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1272552f7358SJed Brown     } else {
1273552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1274552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
12750298fd71SBarry Smith       tmpO = NULL;
1276552f7358SJed Brown     }
1277552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1278552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1279552f7358SJed Brown       const PetscInt cp = tmp[i];
12802e1b13c2SMatthew 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. */
12812e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
12822e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
12832e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1284552f7358SJed Brown       PetscInt       c;
1285552f7358SJed Brown 
12862e1b13c2SMatthew G. Knepley       if (rev) {
12872e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
12882e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
12892e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
12902e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
12912e1b13c2SMatthew G. Knepley       }
1292552f7358SJed Brown       /* Check for duplicate */
1293552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1294552f7358SJed Brown         if (closure[c] == cp) break;
1295552f7358SJed Brown       }
1296552f7358SJed Brown       if (c == closureSize) {
1297552f7358SJed Brown         closure[closureSize]   = cp;
1298552f7358SJed Brown         closure[closureSize+1] = co;
1299552f7358SJed Brown         fifo[fifoSize]         = cp;
1300552f7358SJed Brown         fifo[fifoSize+1]       = co;
1301552f7358SJed Brown         closureSize           += 2;
1302552f7358SJed Brown         fifoSize              += 2;
1303552f7358SJed Brown       }
1304552f7358SJed Brown     }
1305552f7358SJed Brown     fifoStart += 2;
1306552f7358SJed Brown   }
1307552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1308552f7358SJed Brown   if (points)    *points    = closure;
1309552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1310552f7358SJed Brown   PetscFunctionReturn(0);
1311552f7358SJed Brown }
1312552f7358SJed Brown 
1313552f7358SJed Brown #undef __FUNCT__
13149bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetTransitiveClosure_Internal"
13159bf0dad6SMatthew G. Knepley /*@C
13169bf0dad6SMatthew G. Knepley   DMPlexGetTransitiveClosure_Internal - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG with a specified initial orientation
13179bf0dad6SMatthew G. Knepley 
13189bf0dad6SMatthew G. Knepley   Not collective
13199bf0dad6SMatthew G. Knepley 
13209bf0dad6SMatthew G. Knepley   Input Parameters:
13219bf0dad6SMatthew G. Knepley + mesh - The DMPlex
13229bf0dad6SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
13239bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
13249bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
13259bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
13269bf0dad6SMatthew G. Knepley 
13279bf0dad6SMatthew G. Knepley   Output Parameters:
13289bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
13299bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
13309bf0dad6SMatthew G. Knepley 
13319bf0dad6SMatthew G. Knepley   Note:
13329bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
13339bf0dad6SMatthew G. Knepley 
13349bf0dad6SMatthew G. Knepley   Fortran Notes:
13359bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13369bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
13379bf0dad6SMatthew G. Knepley 
13389bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
13399bf0dad6SMatthew G. Knepley 
13409bf0dad6SMatthew G. Knepley   Level: beginner
13419bf0dad6SMatthew G. Knepley 
13429bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
13439bf0dad6SMatthew G. Knepley @*/
13449bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
13459bf0dad6SMatthew G. Knepley {
13469bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
13479bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
13489bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
13499bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
13509bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
13519bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
13529bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
13539bf0dad6SMatthew G. Knepley 
13549bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
13559bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13569bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
13579bf0dad6SMatthew G. Knepley   /* This is only 1-level */
13589bf0dad6SMatthew G. Knepley   if (useCone) {
13599bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
13609bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
13619bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
13629bf0dad6SMatthew G. Knepley   } else {
13639bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
13649bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
13659bf0dad6SMatthew G. Knepley   }
13669bf0dad6SMatthew G. Knepley   if (depth == 1) {
13679bf0dad6SMatthew G. Knepley     if (*points) {
13689bf0dad6SMatthew G. Knepley       closure = *points;
13699bf0dad6SMatthew G. Knepley     } else {
13709bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
13719bf0dad6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
13729bf0dad6SMatthew G. Knepley     }
13739bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
13749bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
13759bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
13769bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
13779bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
13789bf0dad6SMatthew G. Knepley     }
13799bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
13809bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
13819bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
13829bf0dad6SMatthew G. Knepley   }
138324c766afSToby Isaac   {
138424c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
138524c766afSToby Isaac 
138624c766afSToby Isaac     c = mesh->maxConeSize;
138724c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
138824c766afSToby Isaac     s = mesh->maxSupportSize;
138924c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
139024c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
139124c766afSToby Isaac   }
13929bf0dad6SMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
13939bf0dad6SMatthew G. Knepley   if (*points) {
13949bf0dad6SMatthew G. Knepley     closure = *points;
13959bf0dad6SMatthew G. Knepley   } else {
13969bf0dad6SMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
13979bf0dad6SMatthew G. Knepley   }
13989bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
13999bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
14009bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
14019bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
14029bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
14039bf0dad6SMatthew G. Knepley 
14049bf0dad6SMatthew G. Knepley     if (ornt < 0) {
14059bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
14069bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
14079bf0dad6SMatthew G. Knepley       coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
14089bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
14099bf0dad6SMatthew G. Knepley     }
14109bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
14119bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
14129bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
14139bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
14149bf0dad6SMatthew G. Knepley   }
14159bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
14169bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
14179bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
14189bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
14199bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
14209bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
14219bf0dad6SMatthew G. Knepley 
14229bf0dad6SMatthew G. Knepley     if (useCone) {
14239bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
14249bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
14259bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
14269bf0dad6SMatthew G. Knepley     } else {
14279bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
14289bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
14299bf0dad6SMatthew G. Knepley       tmpO = NULL;
14309bf0dad6SMatthew G. Knepley     }
14319bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
14329bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
14339bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
14349bf0dad6SMatthew 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. */
14359bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
14369bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
14379bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
14389bf0dad6SMatthew G. Knepley       PetscInt       c;
14399bf0dad6SMatthew G. Knepley 
14409bf0dad6SMatthew G. Knepley       if (rev) {
14419bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
14429bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
14439bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
14449bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
14459bf0dad6SMatthew G. Knepley       }
14469bf0dad6SMatthew G. Knepley       /* Check for duplicate */
14479bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
14489bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
14499bf0dad6SMatthew G. Knepley       }
14509bf0dad6SMatthew G. Knepley       if (c == closureSize) {
14519bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
14529bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
14539bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
14549bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
14559bf0dad6SMatthew G. Knepley         closureSize           += 2;
14569bf0dad6SMatthew G. Knepley         fifoSize              += 2;
14579bf0dad6SMatthew G. Knepley       }
14589bf0dad6SMatthew G. Knepley     }
14599bf0dad6SMatthew G. Knepley     fifoStart += 2;
14609bf0dad6SMatthew G. Knepley   }
14619bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
14629bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
14639bf0dad6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
14649bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
14659bf0dad6SMatthew G. Knepley }
14669bf0dad6SMatthew G. Knepley 
14679bf0dad6SMatthew G. Knepley #undef __FUNCT__
1468552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreTransitiveClosure"
1469552f7358SJed Brown /*@C
1470552f7358SJed Brown   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1471552f7358SJed Brown 
1472552f7358SJed Brown   Not collective
1473552f7358SJed Brown 
1474552f7358SJed Brown   Input Parameters:
1475552f7358SJed Brown + mesh - The DMPlex
1476552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1477552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1478e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1479e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
1480552f7358SJed Brown 
1481552f7358SJed Brown   Note:
14820298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
1483552f7358SJed Brown 
14843813dfbdSMatthew G Knepley   Fortran Notes:
14853813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14863813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14873813dfbdSMatthew G Knepley 
14883813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
14893813dfbdSMatthew G Knepley 
1490552f7358SJed Brown   Level: beginner
1491552f7358SJed Brown 
1492552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1493552f7358SJed Brown @*/
1494552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1495552f7358SJed Brown {
1496552f7358SJed Brown   PetscErrorCode ierr;
1497552f7358SJed Brown 
1498552f7358SJed Brown   PetscFunctionBegin;
1499552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1500e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
1501e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
1502552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
15034ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
1504552f7358SJed Brown   PetscFunctionReturn(0);
1505552f7358SJed Brown }
1506552f7358SJed Brown 
1507552f7358SJed Brown #undef __FUNCT__
1508552f7358SJed Brown #define __FUNCT__ "DMPlexGetMaxSizes"
1509552f7358SJed Brown /*@
1510552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
1511552f7358SJed Brown 
1512552f7358SJed Brown   Not collective
1513552f7358SJed Brown 
1514552f7358SJed Brown   Input Parameter:
1515552f7358SJed Brown . mesh - The DMPlex
1516552f7358SJed Brown 
1517552f7358SJed Brown   Output Parameters:
1518552f7358SJed Brown + maxConeSize - The maximum number of in-edges
1519552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
1520552f7358SJed Brown 
1521552f7358SJed Brown   Level: beginner
1522552f7358SJed Brown 
1523552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1524552f7358SJed Brown @*/
1525552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1526552f7358SJed Brown {
1527552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
1528552f7358SJed Brown 
1529552f7358SJed Brown   PetscFunctionBegin;
1530552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1531552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1532552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1533552f7358SJed Brown   PetscFunctionReturn(0);
1534552f7358SJed Brown }
1535552f7358SJed Brown 
1536552f7358SJed Brown #undef __FUNCT__
1537552f7358SJed Brown #define __FUNCT__ "DMSetUp_Plex"
1538552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
1539552f7358SJed Brown {
1540552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1541552f7358SJed Brown   PetscInt       size;
1542552f7358SJed Brown   PetscErrorCode ierr;
1543552f7358SJed Brown 
1544552f7358SJed Brown   PetscFunctionBegin;
1545552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1546552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
1547552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
15481795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
15491795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
1550552f7358SJed Brown   if (mesh->maxSupportSize) {
1551552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
1552552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
15531795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
1554552f7358SJed Brown   }
1555552f7358SJed Brown   PetscFunctionReturn(0);
1556552f7358SJed Brown }
1557552f7358SJed Brown 
1558552f7358SJed Brown #undef __FUNCT__
1559552f7358SJed Brown #define __FUNCT__ "DMCreateSubDM_Plex"
1560552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1561552f7358SJed Brown {
1562552f7358SJed Brown   PetscErrorCode ierr;
1563552f7358SJed Brown 
1564552f7358SJed Brown   PetscFunctionBegin;
15654d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
15664d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1567552f7358SJed Brown   PetscFunctionReturn(0);
1568552f7358SJed Brown }
1569552f7358SJed Brown 
1570552f7358SJed Brown #undef __FUNCT__
1571552f7358SJed Brown #define __FUNCT__ "DMPlexSymmetrize"
1572552f7358SJed Brown /*@
1573552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
1574552f7358SJed Brown 
1575552f7358SJed Brown   Not collective
1576552f7358SJed Brown 
1577552f7358SJed Brown   Input Parameter:
1578552f7358SJed Brown . mesh - The DMPlex
1579552f7358SJed Brown 
1580552f7358SJed Brown   Output Parameter:
1581552f7358SJed Brown 
1582552f7358SJed Brown   Note:
1583552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
1584552f7358SJed Brown 
1585552f7358SJed Brown   Level: beginner
1586552f7358SJed Brown 
1587552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
1588552f7358SJed Brown @*/
1589552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
1590552f7358SJed Brown {
1591552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1592552f7358SJed Brown   PetscInt      *offsets;
1593552f7358SJed Brown   PetscInt       supportSize;
1594552f7358SJed Brown   PetscInt       pStart, pEnd, p;
1595552f7358SJed Brown   PetscErrorCode ierr;
1596552f7358SJed Brown 
1597552f7358SJed Brown   PetscFunctionBegin;
1598552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
159982f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
1600552f7358SJed Brown   /* Calculate support sizes */
1601552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1602552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
1603552f7358SJed Brown     PetscInt dof, off, c;
1604552f7358SJed Brown 
1605552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1606552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1607552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
1608552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
1609552f7358SJed Brown     }
1610552f7358SJed Brown   }
1611552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
1612552f7358SJed Brown     PetscInt dof;
1613552f7358SJed Brown 
1614552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
16150d644c17SKarl Rupp 
1616552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
1617552f7358SJed Brown   }
1618552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
1619552f7358SJed Brown   /* Calculate supports */
1620552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
16211795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
16221795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
1623552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
1624552f7358SJed Brown     PetscInt dof, off, c;
1625552f7358SJed Brown 
1626552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1627552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1628552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
1629552f7358SJed Brown       const PetscInt q = mesh->cones[c];
1630552f7358SJed Brown       PetscInt       offS;
1631552f7358SJed Brown 
1632552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
16330d644c17SKarl Rupp 
1634552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
1635552f7358SJed Brown       ++offsets[q];
1636552f7358SJed Brown     }
1637552f7358SJed Brown   }
1638552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
1639552f7358SJed Brown   PetscFunctionReturn(0);
1640552f7358SJed Brown }
1641552f7358SJed Brown 
1642552f7358SJed Brown #undef __FUNCT__
1643552f7358SJed Brown #define __FUNCT__ "DMPlexStratify"
1644552f7358SJed Brown /*@
1645552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
1646552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
1647552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
1648552f7358SJed Brown   the DAG.
1649552f7358SJed Brown 
1650552f7358SJed Brown   Not collective
1651552f7358SJed Brown 
1652552f7358SJed Brown   Input Parameter:
1653552f7358SJed Brown . mesh - The DMPlex
1654552f7358SJed Brown 
1655552f7358SJed Brown   Output Parameter:
1656552f7358SJed Brown 
1657552f7358SJed Brown   Notes:
1658150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
1659150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
1660150b719bSJed Brown   manually via DMPlexGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
1661150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
1662552f7358SJed Brown 
1663150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
1664552f7358SJed Brown 
1665552f7358SJed Brown   Level: beginner
1666552f7358SJed Brown 
1667552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
1668552f7358SJed Brown @*/
1669552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
1670552f7358SJed Brown {
1671aa50250dSMatthew G. Knepley   DMLabel        label;
1672552f7358SJed Brown   PetscInt       pStart, pEnd, p;
1673552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
1674552f7358SJed Brown   PetscErrorCode ierr;
1675552f7358SJed Brown 
1676552f7358SJed Brown   PetscFunctionBegin;
1677552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1678552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
1679552f7358SJed Brown   /* Calculate depth */
1680aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1681aa50250dSMatthew G. Knepley   ierr = DMPlexCreateLabel(dm, "depth");CHKERRQ(ierr);
1682aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
1683552f7358SJed Brown   /* Initialize roots and count leaves */
1684552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
1685552f7358SJed Brown     PetscInt coneSize, supportSize;
1686552f7358SJed Brown 
1687552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1688552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
1689552f7358SJed Brown     if (!coneSize && supportSize) {
1690552f7358SJed Brown       ++numRoots;
1691aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
1692552f7358SJed Brown     } else if (!supportSize && coneSize) {
1693552f7358SJed Brown       ++numLeaves;
1694552f7358SJed Brown     } else if (!supportSize && !coneSize) {
1695552f7358SJed Brown       /* Isolated points */
1696aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
1697552f7358SJed Brown     }
1698552f7358SJed Brown   }
1699552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
1700552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
1701552f7358SJed Brown       PetscInt coneSize, supportSize;
1702552f7358SJed Brown 
1703552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1704552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
1705552f7358SJed Brown       if (!supportSize && coneSize) {
1706aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
1707552f7358SJed Brown       }
1708552f7358SJed Brown     }
1709552f7358SJed Brown   } else {
171074ef644bSMatthew G. Knepley     IS       pointIS;
171174ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
1712552f7358SJed Brown 
171374ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
171474ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
171574ef644bSMatthew G. Knepley     while (numPoints) {
171674ef644bSMatthew G. Knepley       const PetscInt *points;
171774ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
171874ef644bSMatthew G. Knepley 
171974ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
172074ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
172174ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
172274ef644bSMatthew G. Knepley         const PetscInt *support;
172374ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
172474ef644bSMatthew G. Knepley 
172574ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
172674ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
172774ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
172874ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
1729552f7358SJed Brown         }
1730552f7358SJed Brown       }
17315edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
173274ef644bSMatthew G. Knepley       ++level;
173374ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
173474ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
173574ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
173674ef644bSMatthew G. Knepley       else         {numPoints = 0;}
173774ef644bSMatthew G. Knepley     }
173874ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
173974ef644bSMatthew G. Knepley   }
1740552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
1741552f7358SJed Brown   PetscFunctionReturn(0);
1742552f7358SJed Brown }
1743552f7358SJed Brown 
1744552f7358SJed Brown #undef __FUNCT__
1745552f7358SJed Brown #define __FUNCT__ "DMPlexGetJoin"
1746552f7358SJed Brown /*@C
1747552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
1748552f7358SJed Brown 
1749552f7358SJed Brown   Not Collective
1750552f7358SJed Brown 
1751552f7358SJed Brown   Input Parameters:
1752552f7358SJed Brown + dm - The DMPlex object
1753552f7358SJed Brown . numPoints - The number of input points for the join
1754552f7358SJed Brown - points - The input points
1755552f7358SJed Brown 
1756552f7358SJed Brown   Output Parameters:
1757552f7358SJed Brown + numCoveredPoints - The number of points in the join
1758552f7358SJed Brown - coveredPoints - The points in the join
1759552f7358SJed Brown 
1760552f7358SJed Brown   Level: intermediate
1761552f7358SJed Brown 
1762552f7358SJed Brown   Note: Currently, this is restricted to a single level join
1763552f7358SJed Brown 
17643813dfbdSMatthew G Knepley   Fortran Notes:
17653813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17663813dfbdSMatthew G Knepley   include petsc.h90 in your code.
17673813dfbdSMatthew G Knepley 
17683813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17693813dfbdSMatthew G Knepley 
1770552f7358SJed Brown .keywords: mesh
1771552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
1772552f7358SJed Brown @*/
1773552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
1774552f7358SJed Brown {
1775552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1776552f7358SJed Brown   PetscInt      *join[2];
1777552f7358SJed Brown   PetscInt       joinSize, i = 0;
1778552f7358SJed Brown   PetscInt       dof, off, p, c, m;
1779552f7358SJed Brown   PetscErrorCode ierr;
1780552f7358SJed Brown 
1781552f7358SJed Brown   PetscFunctionBegin;
1782552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1783552f7358SJed Brown   PetscValidPointer(points, 2);
1784552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
1785552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
1786552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
1787552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
1788552f7358SJed Brown   /* Copy in support of first point */
1789552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
1790552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
1791552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
1792552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
1793552f7358SJed Brown   }
1794552f7358SJed Brown   /* Check each successive support */
1795552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
1796552f7358SJed Brown     PetscInt newJoinSize = 0;
1797552f7358SJed Brown 
1798552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
1799552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
1800552f7358SJed Brown     for (c = 0; c < dof; ++c) {
1801552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
1802552f7358SJed Brown 
1803552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
1804552f7358SJed Brown         if (point == join[i][m]) {
1805552f7358SJed Brown           join[1-i][newJoinSize++] = point;
1806552f7358SJed Brown           break;
1807552f7358SJed Brown         }
1808552f7358SJed Brown       }
1809552f7358SJed Brown     }
1810552f7358SJed Brown     joinSize = newJoinSize;
1811552f7358SJed Brown     i        = 1-i;
1812552f7358SJed Brown   }
1813552f7358SJed Brown   *numCoveredPoints = joinSize;
1814552f7358SJed Brown   *coveredPoints    = join[i];
1815552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
1816552f7358SJed Brown   PetscFunctionReturn(0);
1817552f7358SJed Brown }
1818552f7358SJed Brown 
1819552f7358SJed Brown #undef __FUNCT__
1820552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreJoin"
1821552f7358SJed Brown /*@C
1822552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
1823552f7358SJed Brown 
1824552f7358SJed Brown   Not Collective
1825552f7358SJed Brown 
1826552f7358SJed Brown   Input Parameters:
1827552f7358SJed Brown + dm - The DMPlex object
1828552f7358SJed Brown . numPoints - The number of input points for the join
1829552f7358SJed Brown - points - The input points
1830552f7358SJed Brown 
1831552f7358SJed Brown   Output Parameters:
1832552f7358SJed Brown + numCoveredPoints - The number of points in the join
1833552f7358SJed Brown - coveredPoints - The points in the join
1834552f7358SJed Brown 
18353813dfbdSMatthew G Knepley   Fortran Notes:
18363813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18373813dfbdSMatthew G Knepley   include petsc.h90 in your code.
18383813dfbdSMatthew G Knepley 
18393813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
18403813dfbdSMatthew G Knepley 
1841552f7358SJed Brown   Level: intermediate
1842552f7358SJed Brown 
1843552f7358SJed Brown .keywords: mesh
1844552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
1845552f7358SJed Brown @*/
1846552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
1847552f7358SJed Brown {
1848552f7358SJed Brown   PetscErrorCode ierr;
1849552f7358SJed Brown 
1850552f7358SJed Brown   PetscFunctionBegin;
1851552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1852d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
1853d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
1854d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
1855552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
1856d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
1857552f7358SJed Brown   PetscFunctionReturn(0);
1858552f7358SJed Brown }
1859552f7358SJed Brown 
1860552f7358SJed Brown #undef __FUNCT__
1861552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullJoin"
1862552f7358SJed Brown /*@C
1863552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
1864552f7358SJed Brown 
1865552f7358SJed Brown   Not Collective
1866552f7358SJed Brown 
1867552f7358SJed Brown   Input Parameters:
1868552f7358SJed Brown + dm - The DMPlex object
1869552f7358SJed Brown . numPoints - The number of input points for the join
1870552f7358SJed Brown - points - The input points
1871552f7358SJed Brown 
1872552f7358SJed Brown   Output Parameters:
1873552f7358SJed Brown + numCoveredPoints - The number of points in the join
1874552f7358SJed Brown - coveredPoints - The points in the join
1875552f7358SJed Brown 
18763813dfbdSMatthew G Knepley   Fortran Notes:
18773813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18783813dfbdSMatthew G Knepley   include petsc.h90 in your code.
18793813dfbdSMatthew G Knepley 
18803813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
18813813dfbdSMatthew G Knepley 
1882552f7358SJed Brown   Level: intermediate
1883552f7358SJed Brown 
1884552f7358SJed Brown .keywords: mesh
1885552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
1886552f7358SJed Brown @*/
1887552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
1888552f7358SJed Brown {
1889552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1890552f7358SJed Brown   PetscInt      *offsets, **closures;
1891552f7358SJed Brown   PetscInt      *join[2];
1892552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
189324c766afSToby Isaac   PetscInt       p, d, c, m, ms;
1894552f7358SJed Brown   PetscErrorCode ierr;
1895552f7358SJed Brown 
1896552f7358SJed Brown   PetscFunctionBegin;
1897552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1898552f7358SJed Brown   PetscValidPointer(points, 2);
1899552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
1900552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
1901552f7358SJed Brown 
1902552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
19031795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
1904552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
190524c766afSToby Isaac   ms      = mesh->maxSupportSize;
190624c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
1907552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
1908552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
1909552f7358SJed Brown 
1910552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
1911552f7358SJed Brown     PetscInt closureSize;
1912552f7358SJed Brown 
1913552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
19140d644c17SKarl Rupp 
1915552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
1916552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
1917552f7358SJed Brown       PetscInt pStart, pEnd, i;
1918552f7358SJed Brown 
1919552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1920552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
1921552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
1922552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
1923552f7358SJed Brown           break;
1924552f7358SJed Brown         }
1925552f7358SJed Brown       }
1926552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
1927552f7358SJed Brown     }
192882f516ccSBarry 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);
1929552f7358SJed Brown   }
1930552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
1931552f7358SJed Brown     PetscInt dof;
1932552f7358SJed Brown 
1933552f7358SJed Brown     /* Copy in support of first point */
1934552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
1935552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
1936552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
1937552f7358SJed Brown     }
1938552f7358SJed Brown     /* Check each successive cone */
1939552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
1940552f7358SJed Brown       PetscInt newJoinSize = 0;
1941552f7358SJed Brown 
1942552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
1943552f7358SJed Brown       for (c = 0; c < dof; ++c) {
1944552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
1945552f7358SJed Brown 
1946552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
1947552f7358SJed Brown           if (point == join[i][m]) {
1948552f7358SJed Brown             join[1-i][newJoinSize++] = point;
1949552f7358SJed Brown             break;
1950552f7358SJed Brown           }
1951552f7358SJed Brown         }
1952552f7358SJed Brown       }
1953552f7358SJed Brown       joinSize = newJoinSize;
1954552f7358SJed Brown       i        = 1-i;
1955552f7358SJed Brown     }
1956552f7358SJed Brown     if (joinSize) break;
1957552f7358SJed Brown   }
1958552f7358SJed Brown   *numCoveredPoints = joinSize;
1959552f7358SJed Brown   *coveredPoints    = join[i];
1960552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
19610298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
1962552f7358SJed Brown   }
1963552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
1964552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
1965552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
1966552f7358SJed Brown   PetscFunctionReturn(0);
1967552f7358SJed Brown }
1968552f7358SJed Brown 
1969552f7358SJed Brown #undef __FUNCT__
1970552f7358SJed Brown #define __FUNCT__ "DMPlexGetMeet"
1971552f7358SJed Brown /*@C
1972552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
1973552f7358SJed Brown 
1974552f7358SJed Brown   Not Collective
1975552f7358SJed Brown 
1976552f7358SJed Brown   Input Parameters:
1977552f7358SJed Brown + dm - The DMPlex object
1978552f7358SJed Brown . numPoints - The number of input points for the meet
1979552f7358SJed Brown - points - The input points
1980552f7358SJed Brown 
1981552f7358SJed Brown   Output Parameters:
1982552f7358SJed Brown + numCoveredPoints - The number of points in the meet
1983552f7358SJed Brown - coveredPoints - The points in the meet
1984552f7358SJed Brown 
1985552f7358SJed Brown   Level: intermediate
1986552f7358SJed Brown 
1987552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
1988552f7358SJed Brown 
19893813dfbdSMatthew G Knepley   Fortran Notes:
19903813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19913813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19923813dfbdSMatthew G Knepley 
19933813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19943813dfbdSMatthew G Knepley 
1995552f7358SJed Brown .keywords: mesh
1996552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
1997552f7358SJed Brown @*/
1998552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
1999552f7358SJed Brown {
2000552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2001552f7358SJed Brown   PetscInt      *meet[2];
2002552f7358SJed Brown   PetscInt       meetSize, i = 0;
2003552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2004552f7358SJed Brown   PetscErrorCode ierr;
2005552f7358SJed Brown 
2006552f7358SJed Brown   PetscFunctionBegin;
2007552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2008552f7358SJed Brown   PetscValidPointer(points, 2);
2009552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2010552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
2011552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2012552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2013552f7358SJed Brown   /* Copy in cone of first point */
2014552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2015552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2016552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2017552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2018552f7358SJed Brown   }
2019552f7358SJed Brown   /* Check each successive cone */
2020552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2021552f7358SJed Brown     PetscInt newMeetSize = 0;
2022552f7358SJed Brown 
2023552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2024552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2025552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2026552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2027552f7358SJed Brown 
2028552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2029552f7358SJed Brown         if (point == meet[i][m]) {
2030552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2031552f7358SJed Brown           break;
2032552f7358SJed Brown         }
2033552f7358SJed Brown       }
2034552f7358SJed Brown     }
2035552f7358SJed Brown     meetSize = newMeetSize;
2036552f7358SJed Brown     i        = 1-i;
2037552f7358SJed Brown   }
2038552f7358SJed Brown   *numCoveringPoints = meetSize;
2039552f7358SJed Brown   *coveringPoints    = meet[i];
2040552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2041552f7358SJed Brown   PetscFunctionReturn(0);
2042552f7358SJed Brown }
2043552f7358SJed Brown 
2044552f7358SJed Brown #undef __FUNCT__
2045552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreMeet"
2046552f7358SJed Brown /*@C
2047552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2048552f7358SJed Brown 
2049552f7358SJed Brown   Not Collective
2050552f7358SJed Brown 
2051552f7358SJed Brown   Input Parameters:
2052552f7358SJed Brown + dm - The DMPlex object
2053552f7358SJed Brown . numPoints - The number of input points for the meet
2054552f7358SJed Brown - points - The input points
2055552f7358SJed Brown 
2056552f7358SJed Brown   Output Parameters:
2057552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2058552f7358SJed Brown - coveredPoints - The points in the meet
2059552f7358SJed Brown 
2060552f7358SJed Brown   Level: intermediate
2061552f7358SJed Brown 
20623813dfbdSMatthew G Knepley   Fortran Notes:
20633813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20643813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20653813dfbdSMatthew G Knepley 
20663813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
20673813dfbdSMatthew G Knepley 
2068552f7358SJed Brown .keywords: mesh
2069552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2070552f7358SJed Brown @*/
2071552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2072552f7358SJed Brown {
2073552f7358SJed Brown   PetscErrorCode ierr;
2074552f7358SJed Brown 
2075552f7358SJed Brown   PetscFunctionBegin;
2076552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2077d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2078d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2079d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
2080552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2081d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2082552f7358SJed Brown   PetscFunctionReturn(0);
2083552f7358SJed Brown }
2084552f7358SJed Brown 
2085552f7358SJed Brown #undef __FUNCT__
2086552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullMeet"
2087552f7358SJed Brown /*@C
2088552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2089552f7358SJed Brown 
2090552f7358SJed Brown   Not Collective
2091552f7358SJed Brown 
2092552f7358SJed Brown   Input Parameters:
2093552f7358SJed Brown + dm - The DMPlex object
2094552f7358SJed Brown . numPoints - The number of input points for the meet
2095552f7358SJed Brown - points - The input points
2096552f7358SJed Brown 
2097552f7358SJed Brown   Output Parameters:
2098552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2099552f7358SJed Brown - coveredPoints - The points in the meet
2100552f7358SJed Brown 
2101552f7358SJed Brown   Level: intermediate
2102552f7358SJed Brown 
21033813dfbdSMatthew G Knepley   Fortran Notes:
21043813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21053813dfbdSMatthew G Knepley   include petsc.h90 in your code.
21063813dfbdSMatthew G Knepley 
21073813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
21083813dfbdSMatthew G Knepley 
2109552f7358SJed Brown .keywords: mesh
2110552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2111552f7358SJed Brown @*/
2112552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2113552f7358SJed Brown {
2114552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2115552f7358SJed Brown   PetscInt      *offsets, **closures;
2116552f7358SJed Brown   PetscInt      *meet[2];
2117552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
211824c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2119552f7358SJed Brown   PetscErrorCode ierr;
2120552f7358SJed Brown 
2121552f7358SJed Brown   PetscFunctionBegin;
2122552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2123552f7358SJed Brown   PetscValidPointer(points, 2);
2124552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2125552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2126552f7358SJed Brown 
2127552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2128785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
2129552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
213024c766afSToby Isaac   mc      = mesh->maxConeSize;
213124c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2132552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2133552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2134552f7358SJed Brown 
2135552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2136552f7358SJed Brown     PetscInt closureSize;
2137552f7358SJed Brown 
2138552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
21390d644c17SKarl Rupp 
2140552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2141552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2142552f7358SJed Brown       PetscInt pStart, pEnd, i;
2143552f7358SJed Brown 
2144552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2145552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2146552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2147552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2148552f7358SJed Brown           break;
2149552f7358SJed Brown         }
2150552f7358SJed Brown       }
2151552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2152552f7358SJed Brown     }
215382f516ccSBarry 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);
2154552f7358SJed Brown   }
2155552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2156552f7358SJed Brown     PetscInt dof;
2157552f7358SJed Brown 
2158552f7358SJed Brown     /* Copy in cone of first point */
2159552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2160552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2161552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2162552f7358SJed Brown     }
2163552f7358SJed Brown     /* Check each successive cone */
2164552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2165552f7358SJed Brown       PetscInt newMeetSize = 0;
2166552f7358SJed Brown 
2167552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2168552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2169552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2170552f7358SJed Brown 
2171552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2172552f7358SJed Brown           if (point == meet[i][m]) {
2173552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2174552f7358SJed Brown             break;
2175552f7358SJed Brown           }
2176552f7358SJed Brown         }
2177552f7358SJed Brown       }
2178552f7358SJed Brown       meetSize = newMeetSize;
2179552f7358SJed Brown       i        = 1-i;
2180552f7358SJed Brown     }
2181552f7358SJed Brown     if (meetSize) break;
2182552f7358SJed Brown   }
2183552f7358SJed Brown   *numCoveredPoints = meetSize;
2184552f7358SJed Brown   *coveredPoints    = meet[i];
2185552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
21860298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2187552f7358SJed Brown   }
2188552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2189552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2190552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2191552f7358SJed Brown   PetscFunctionReturn(0);
2192552f7358SJed Brown }
2193552f7358SJed Brown 
2194552f7358SJed Brown #undef __FUNCT__
21954e3744c5SMatthew G. Knepley #define __FUNCT__ "DMPlexEqual"
21964e3744c5SMatthew G. Knepley /*@C
21974e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
21984e3744c5SMatthew G. Knepley 
21994e3744c5SMatthew G. Knepley   Not Collective
22004e3744c5SMatthew G. Knepley 
22014e3744c5SMatthew G. Knepley   Input Parameters:
22024e3744c5SMatthew G. Knepley + dmA - A DMPlex object
22034e3744c5SMatthew G. Knepley - dmB - A DMPlex object
22044e3744c5SMatthew G. Knepley 
22054e3744c5SMatthew G. Knepley   Output Parameters:
22064e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
22074e3744c5SMatthew G. Knepley 
22084e3744c5SMatthew G. Knepley   Level: intermediate
22094e3744c5SMatthew G. Knepley 
22104e3744c5SMatthew G. Knepley   Notes:
22114e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
22124e3744c5SMatthew G. Knepley 
22134e3744c5SMatthew G. Knepley .keywords: mesh
22144e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
22154e3744c5SMatthew G. Knepley @*/
22164e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
22174e3744c5SMatthew G. Knepley {
22184e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
22194e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
22204e3744c5SMatthew G. Knepley 
22214e3744c5SMatthew G. Knepley   PetscFunctionBegin;
22224e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
22234e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
22244e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
22254e3744c5SMatthew G. Knepley 
22264e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
22274e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
22284e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
22294e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
22304e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
22314e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
22324e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
22334e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
22344e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
22354e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
22364e3744c5SMatthew G. Knepley 
22374e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
22384e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
22394e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
22404e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
22414e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
22424e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
22434e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
22444e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
22454e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
22464e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
22474e3744c5SMatthew G. Knepley     }
22484e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
22494e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
22504e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
22514e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
22524e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
22534e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
22544e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
22554e3744c5SMatthew G. Knepley     }
22564e3744c5SMatthew G. Knepley   }
22574e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
22584e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
22594e3744c5SMatthew G. Knepley }
22604e3744c5SMatthew G. Knepley 
22614e3744c5SMatthew G. Knepley #undef __FUNCT__
226218ad9376SMatthew G. Knepley #define __FUNCT__ "DMPlexGetNumFaceVertices"
226318ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2264a6dfd86eSKarl Rupp {
226582f516ccSBarry Smith   MPI_Comm       comm;
2266552f7358SJed Brown   PetscErrorCode ierr;
2267552f7358SJed Brown 
2268552f7358SJed Brown   PetscFunctionBegin;
226982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2270552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2271552f7358SJed Brown   switch (cellDim) {
2272552f7358SJed Brown   case 0:
2273552f7358SJed Brown     *numFaceVertices = 0;
2274552f7358SJed Brown     break;
2275552f7358SJed Brown   case 1:
2276552f7358SJed Brown     *numFaceVertices = 1;
2277552f7358SJed Brown     break;
2278552f7358SJed Brown   case 2:
2279552f7358SJed Brown     switch (numCorners) {
228019436ca2SJed Brown     case 3: /* triangle */
228119436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2282552f7358SJed Brown       break;
228319436ca2SJed Brown     case 4: /* quadrilateral */
228419436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2285552f7358SJed Brown       break;
228619436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
228719436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2288552f7358SJed Brown       break;
228919436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
229019436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2291552f7358SJed Brown       break;
2292552f7358SJed Brown     default:
2293552f7358SJed Brown       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %d for dimension %d", numCorners, cellDim);
2294552f7358SJed Brown     }
2295552f7358SJed Brown     break;
2296552f7358SJed Brown   case 3:
2297552f7358SJed Brown     switch (numCorners) {
229819436ca2SJed Brown     case 4: /* tetradehdron */
229919436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2300552f7358SJed Brown       break;
230119436ca2SJed Brown     case 6: /* tet cohesive cells */
230219436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2303552f7358SJed Brown       break;
230419436ca2SJed Brown     case 8: /* hexahedron */
230519436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2306552f7358SJed Brown       break;
230719436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
230819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2309552f7358SJed Brown       break;
231019436ca2SJed Brown     case 10: /* quadratic tetrahedron */
231119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2312552f7358SJed Brown       break;
231319436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
231419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2315552f7358SJed Brown       break;
231619436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
231719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2318552f7358SJed Brown       break;
231919436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
232019436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2321552f7358SJed Brown       break;
2322552f7358SJed Brown     default:
2323552f7358SJed Brown       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %d for dimension %d", numCorners, cellDim);
2324552f7358SJed Brown     }
2325552f7358SJed Brown     break;
2326552f7358SJed Brown   default:
2327552f7358SJed Brown     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %d", cellDim);
2328552f7358SJed Brown   }
2329552f7358SJed Brown   PetscFunctionReturn(0);
2330552f7358SJed Brown }
2331552f7358SJed Brown 
2332552f7358SJed Brown #undef __FUNCT__
2333d49fcaecSMatthew G. Knepley #define __FUNCT__ "DMPlexLocalizeCoordinate_Internal"
2334d49fcaecSMatthew G. Knepley PetscErrorCode DMPlexLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
2335d49fcaecSMatthew G. Knepley {
2336d49fcaecSMatthew G. Knepley   PetscInt d;
2337d49fcaecSMatthew G. Knepley 
2338d49fcaecSMatthew G. Knepley   PetscFunctionBegin;
2339d49fcaecSMatthew G. Knepley   if (!dm->maxCell) {
2340d49fcaecSMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
2341d49fcaecSMatthew G. Knepley   } else {
2342d49fcaecSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
2343d49fcaecSMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
2344858aadebSMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
2345d49fcaecSMatthew G. Knepley       } else {
2346d49fcaecSMatthew G. Knepley         out[d] = in[d];
2347d49fcaecSMatthew G. Knepley       }
2348d49fcaecSMatthew G. Knepley     }
2349d49fcaecSMatthew G. Knepley   }
2350d49fcaecSMatthew G. Knepley   PetscFunctionReturn(0);
2351d49fcaecSMatthew G. Knepley }
2352d49fcaecSMatthew G. Knepley 
2353d49fcaecSMatthew G. Knepley #undef __FUNCT__
2354d49fcaecSMatthew G. Knepley #define __FUNCT__ "DMPlexLocalizeAddCoordinate_Internal"
2355d49fcaecSMatthew G. Knepley PetscErrorCode DMPlexLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
2356d49fcaecSMatthew G. Knepley {
2357d49fcaecSMatthew G. Knepley   PetscInt d;
2358d49fcaecSMatthew G. Knepley 
2359d49fcaecSMatthew G. Knepley   PetscFunctionBegin;
2360d49fcaecSMatthew G. Knepley   if (!dm->maxCell) {
2361d49fcaecSMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
2362d49fcaecSMatthew G. Knepley   } else {
2363d49fcaecSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
2364d49fcaecSMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
2365858aadebSMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
2366d49fcaecSMatthew G. Knepley       } else {
2367d49fcaecSMatthew G. Knepley         out[d] += in[d];
2368d49fcaecSMatthew G. Knepley       }
2369d49fcaecSMatthew G. Knepley     }
2370d49fcaecSMatthew G. Knepley   }
2371d49fcaecSMatthew G. Knepley   PetscFunctionReturn(0);
2372d49fcaecSMatthew G. Knepley }
2373d49fcaecSMatthew G. Knepley 
2374d49fcaecSMatthew G. Knepley #undef __FUNCT__
2375d49fcaecSMatthew G. Knepley #define __FUNCT__ "DMPlexLocalizeCoordinates"
2376d49fcaecSMatthew G. Knepley PetscErrorCode DMPlexLocalizeCoordinates(DM dm)
2377d49fcaecSMatthew G. Knepley {
2378d49fcaecSMatthew G. Knepley   PetscSection   coordSection, cSection;
2379d49fcaecSMatthew G. Knepley   Vec            coordinates,  cVec;
2380d49fcaecSMatthew G. Knepley   PetscScalar   *coords, *coords2, *anchor;
23818c7f9fc6SMatthew G. Knepley   PetscInt       Nc, cStart, cEnd, c, vStart, vEnd, v, dof, d, off, off2, bs, coordSize;
2382d49fcaecSMatthew G. Knepley   PetscErrorCode ierr;
2383d49fcaecSMatthew G. Knepley 
2384d49fcaecSMatthew G. Knepley   PetscFunctionBegin;
2385d49fcaecSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2386d49fcaecSMatthew G. Knepley   if (!dm->maxCell) PetscFunctionReturn(0);
2387d49fcaecSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2388d49fcaecSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
2389d49fcaecSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2390d49fcaecSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2391d49fcaecSMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
23928c7f9fc6SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
23938c7f9fc6SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
23948c7f9fc6SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
2395d49fcaecSMatthew G. Knepley   ierr = PetscSectionSetChart(cSection, cStart, vEnd);CHKERRQ(ierr);
2396d49fcaecSMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
2397d49fcaecSMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
2398d49fcaecSMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
23998c7f9fc6SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
2400d49fcaecSMatthew G. Knepley   }
2401d49fcaecSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2402d49fcaecSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &dof, NULL);CHKERRQ(ierr);
2403d49fcaecSMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
24048c7f9fc6SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
2405d49fcaecSMatthew G. Knepley   }
2406d49fcaecSMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
2407d49fcaecSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
2408d49fcaecSMatthew G. Knepley   ierr = VecCreate(PetscObjectComm((PetscObject) dm), &cVec);CHKERRQ(ierr);
2409d49fcaecSMatthew G. Knepley   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
2410d49fcaecSMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
2411d49fcaecSMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
2412d49fcaecSMatthew G. Knepley   ierr = VecSetType(cVec,VECSTANDARD);CHKERRQ(ierr);
2413d49fcaecSMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
2414d49fcaecSMatthew G. Knepley   ierr = VecGetArray(cVec,        &coords2);CHKERRQ(ierr);
2415d49fcaecSMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
2416d49fcaecSMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
2417d49fcaecSMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
2418d49fcaecSMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
2419d49fcaecSMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
2420d49fcaecSMatthew G. Knepley   }
2421d49fcaecSMatthew G. Knepley   ierr = DMGetWorkArray(dm, 3, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
2422d49fcaecSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2423d49fcaecSMatthew G. Knepley     PetscScalar *cellCoords = NULL;
2424d49fcaecSMatthew G. Knepley     PetscInt     b;
2425d49fcaecSMatthew G. Knepley 
2426d49fcaecSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
2427d49fcaecSMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
2428d49fcaecSMatthew G. Knepley     for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
2429d49fcaecSMatthew G. Knepley     for (d = 0; d < dof/bs; ++d) {ierr = DMPlexLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
2430d49fcaecSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
2431d49fcaecSMatthew G. Knepley   }
2432d49fcaecSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 3, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
2433d49fcaecSMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
2434d49fcaecSMatthew G. Knepley   ierr = VecRestoreArray(cVec,        &coords2);CHKERRQ(ierr);
243546e270d4SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
2436d49fcaecSMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
2437d49fcaecSMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
2438d49fcaecSMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
2439d49fcaecSMatthew G. Knepley   PetscFunctionReturn(0);
2440d49fcaecSMatthew G. Knepley }
2441d49fcaecSMatthew G. Knepley 
2442d49fcaecSMatthew G. Knepley #undef __FUNCT__
2443aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepthLabel"
2444552f7358SJed Brown /*@
2445aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2446552f7358SJed Brown 
2447552f7358SJed Brown   Not Collective
2448552f7358SJed Brown 
2449aa50250dSMatthew G. Knepley   Input Parameter:
2450552f7358SJed Brown . dm    - The DMPlex object
2451552f7358SJed Brown 
2452aa50250dSMatthew G. Knepley   Output Parameter:
2453aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2454552f7358SJed Brown 
2455552f7358SJed Brown   Level: developer
2456552f7358SJed Brown 
2457aa50250dSMatthew G. Knepley .keywords: mesh, points
2458aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2459aa50250dSMatthew G. Knepley @*/
2460aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2461aa50250dSMatthew G. Knepley {
2462aa50250dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2463aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2464aa50250dSMatthew G. Knepley 
2465aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2466aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2467aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
246863d1a920SMatthew G. Knepley   if (!mesh->depthLabel) {ierr = DMPlexGetLabel(dm, "depth", &mesh->depthLabel);CHKERRQ(ierr);}
2469aa50250dSMatthew G. Knepley   *depthLabel = mesh->depthLabel;
2470aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2471aa50250dSMatthew G. Knepley }
2472aa50250dSMatthew G. Knepley 
2473aa50250dSMatthew G. Knepley #undef __FUNCT__
2474aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepth"
2475aa50250dSMatthew G. Knepley /*@
2476aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2477aa50250dSMatthew G. Knepley 
2478aa50250dSMatthew G. Knepley   Not Collective
2479aa50250dSMatthew G. Knepley 
2480aa50250dSMatthew G. Knepley   Input Parameter:
2481aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2482aa50250dSMatthew G. Knepley 
2483aa50250dSMatthew G. Knepley   Output Parameter:
2484aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2485aa50250dSMatthew G. Knepley 
2486aa50250dSMatthew G. Knepley   Level: developer
2487552f7358SJed Brown 
2488552f7358SJed Brown .keywords: mesh, points
2489aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2490552f7358SJed Brown @*/
2491552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2492552f7358SJed Brown {
2493aa50250dSMatthew G. Knepley   DMLabel        label;
2494aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2495552f7358SJed Brown   PetscErrorCode ierr;
2496552f7358SJed Brown 
2497552f7358SJed Brown   PetscFunctionBegin;
2498552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2499552f7358SJed Brown   PetscValidPointer(depth, 2);
2500aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2501aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2502552f7358SJed Brown   *depth = d-1;
2503552f7358SJed Brown   PetscFunctionReturn(0);
2504552f7358SJed Brown }
2505552f7358SJed Brown 
2506552f7358SJed Brown #undef __FUNCT__
2507552f7358SJed Brown #define __FUNCT__ "DMPlexGetDepthStratum"
2508552f7358SJed Brown /*@
2509552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
2510552f7358SJed Brown 
2511552f7358SJed Brown   Not Collective
2512552f7358SJed Brown 
2513552f7358SJed Brown   Input Parameters:
2514552f7358SJed Brown + dm           - The DMPlex object
2515552f7358SJed Brown - stratumValue - The requested depth
2516552f7358SJed Brown 
2517552f7358SJed Brown   Output Parameters:
2518552f7358SJed Brown + start - The first point at this depth
2519552f7358SJed Brown - end   - One beyond the last point at this depth
2520552f7358SJed Brown 
2521552f7358SJed Brown   Level: developer
2522552f7358SJed Brown 
2523552f7358SJed Brown .keywords: mesh, points
2524552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2525552f7358SJed Brown @*/
25260adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
25270adebc6cSBarry Smith {
2528aa50250dSMatthew G. Knepley   DMLabel        label;
252963d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
2530552f7358SJed Brown   PetscErrorCode ierr;
2531552f7358SJed Brown 
2532552f7358SJed Brown   PetscFunctionBegin;
2533552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
253463d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
253563d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2536552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
25370d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
253863d1a920SMatthew G. Knepley   if (stratumValue < 0) {
253963d1a920SMatthew G. Knepley     if (start) *start = pStart;
254063d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
254163d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2542552f7358SJed Brown   }
2543aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
254463d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
254563d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
2546552f7358SJed Brown   PetscFunctionReturn(0);
2547552f7358SJed Brown }
2548552f7358SJed Brown 
2549552f7358SJed Brown #undef __FUNCT__
2550552f7358SJed Brown #define __FUNCT__ "DMPlexGetHeightStratum"
2551552f7358SJed Brown /*@
2552552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
2553552f7358SJed Brown 
2554552f7358SJed Brown   Not Collective
2555552f7358SJed Brown 
2556552f7358SJed Brown   Input Parameters:
2557552f7358SJed Brown + dm           - The DMPlex object
2558552f7358SJed Brown - stratumValue - The requested height
2559552f7358SJed Brown 
2560552f7358SJed Brown   Output Parameters:
2561552f7358SJed Brown + start - The first point at this height
2562552f7358SJed Brown - end   - One beyond the last point at this height
2563552f7358SJed Brown 
2564552f7358SJed Brown   Level: developer
2565552f7358SJed Brown 
2566552f7358SJed Brown .keywords: mesh, points
2567552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
2568552f7358SJed Brown @*/
25690adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
25700adebc6cSBarry Smith {
2571aa50250dSMatthew G. Knepley   DMLabel        label;
257263d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
2573552f7358SJed Brown   PetscErrorCode ierr;
2574552f7358SJed Brown 
2575552f7358SJed Brown   PetscFunctionBegin;
2576552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
257763d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
257863d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2579552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
25800d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
258163d1a920SMatthew G. Knepley   if (stratumValue < 0) {
258263d1a920SMatthew G. Knepley     if (start) *start = pStart;
258363d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
258463d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2585552f7358SJed Brown   }
2586aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2587aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
258863d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
258963d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
2590552f7358SJed Brown   PetscFunctionReturn(0);
2591552f7358SJed Brown }
2592552f7358SJed Brown 
2593552f7358SJed Brown #undef __FUNCT__
2594552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionInitial"
2595552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
2596a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
2597a6dfd86eSKarl Rupp {
2598552f7358SJed Brown   PetscInt      *numDofTot;
2599916f0f19SMatthew G. Knepley   PetscInt       depth, pStart = 0, pEnd = 0;
2600916f0f19SMatthew G. Knepley   PetscInt       p, d, dep, f;
2601552f7358SJed Brown   PetscErrorCode ierr;
2602552f7358SJed Brown 
2603552f7358SJed Brown   PetscFunctionBegin;
2604854ce69bSBarry Smith   ierr = PetscMalloc1(dim+1, &numDofTot);CHKERRQ(ierr);
2605552f7358SJed Brown   for (d = 0; d <= dim; ++d) {
2606552f7358SJed Brown     numDofTot[d] = 0;
26070d644c17SKarl Rupp     for (f = 0; f < numFields; ++f) numDofTot[d] += numDof[f*(dim+1)+d];
2608552f7358SJed Brown   }
260982f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
2610552f7358SJed Brown   if (numFields > 0) {
2611552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
2612552f7358SJed Brown     if (numComp) {
2613552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2614552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
2615552f7358SJed Brown       }
2616552f7358SJed Brown     }
2617552f7358SJed Brown   }
2618552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2619552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
2620916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2621916f0f19SMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
2622916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
2623916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
2624552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2625552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2626552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
2627552f7358SJed Brown       }
2628552f7358SJed Brown       ierr = PetscSectionSetDof(*section, p, numDofTot[d]);CHKERRQ(ierr);
2629552f7358SJed Brown     }
2630552f7358SJed Brown   }
2631552f7358SJed Brown   ierr = PetscFree(numDofTot);CHKERRQ(ierr);
2632552f7358SJed Brown   PetscFunctionReturn(0);
2633552f7358SJed Brown }
2634552f7358SJed Brown 
2635552f7358SJed Brown #undef __FUNCT__
2636552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCDof"
2637552f7358SJed Brown /* Set the number of dof on each point and separate by fields
2638552f7358SJed Brown    If constDof is PETSC_DETERMINE, constrain every dof on the point
2639552f7358SJed Brown */
2640a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC,const PetscInt bcField[],const IS bcPoints[], PetscInt constDof, PetscSection section)
2641a6dfd86eSKarl Rupp {
2642552f7358SJed Brown   PetscInt       numFields;
2643552f7358SJed Brown   PetscInt       bc;
2644a68b90caSToby Isaac   PetscSection   aSec;
2645552f7358SJed Brown   PetscErrorCode ierr;
2646552f7358SJed Brown 
2647552f7358SJed Brown   PetscFunctionBegin;
2648552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
2649552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
2650552f7358SJed Brown     PetscInt        field = 0;
2651552f7358SJed Brown     const PetscInt *idx;
2652552f7358SJed Brown     PetscInt        n, i;
2653552f7358SJed Brown 
26540d644c17SKarl Rupp     if (numFields) field = bcField[bc];
2655552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
2656552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
2657552f7358SJed Brown     for (i = 0; i < n; ++i) {
2658552f7358SJed Brown       const PetscInt p        = idx[i];
2659552f7358SJed Brown       PetscInt       numConst = constDof;
2660552f7358SJed Brown 
2661552f7358SJed Brown       /* Constrain every dof on the point */
2662552f7358SJed Brown       if (numConst < 0) {
2663552f7358SJed Brown         if (numFields) {
2664552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
2665552f7358SJed Brown         } else {
2666552f7358SJed Brown           ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
2667552f7358SJed Brown         }
2668552f7358SJed Brown       }
2669552f7358SJed Brown       if (numFields) {
2670552f7358SJed Brown         ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);
2671552f7358SJed Brown       }
2672552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
2673552f7358SJed Brown     }
2674552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
2675552f7358SJed Brown   }
2676a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
2677a68b90caSToby Isaac   if (aSec) {
2678a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
2679a68b90caSToby Isaac 
2680a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
2681a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
2682a68b90caSToby Isaac       PetscInt dof;
2683a68b90caSToby Isaac 
2684a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
2685a68b90caSToby Isaac       if (dof) {
2686a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
2687a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
2688a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
2689a68b90caSToby Isaac         if (numFields) {
2690a68b90caSToby Isaac           PetscInt f;
2691a68b90caSToby Isaac 
2692a68b90caSToby Isaac           for (f = 0; f < numFields; f++) {
2693a68b90caSToby Isaac             ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
2694a68b90caSToby Isaac             ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
2695a68b90caSToby Isaac           }
2696a68b90caSToby Isaac         }
2697a68b90caSToby Isaac       }
2698a68b90caSToby Isaac     }
2699a68b90caSToby Isaac   }
2700552f7358SJed Brown   PetscFunctionReturn(0);
2701552f7358SJed Brown }
2702552f7358SJed Brown 
2703552f7358SJed Brown #undef __FUNCT__
2704552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCIndicesAll"
2705552f7358SJed Brown /* Set the constrained indices on each point and separate by fields */
27060adebc6cSBarry Smith PetscErrorCode DMPlexCreateSectionBCIndicesAll(DM dm, PetscSection section)
27070adebc6cSBarry Smith {
2708552f7358SJed Brown   PetscInt      *maxConstraints;
2709552f7358SJed Brown   PetscInt       numFields, f, pStart = 0, pEnd = 0, p;
2710552f7358SJed Brown   PetscErrorCode ierr;
2711552f7358SJed Brown 
2712552f7358SJed Brown   PetscFunctionBegin;
2713552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
2714552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
2715854ce69bSBarry Smith   ierr = PetscMalloc1(numFields+1, &maxConstraints);CHKERRQ(ierr);
27160d644c17SKarl Rupp   for (f = 0; f <= numFields; ++f) maxConstraints[f] = 0;
2717552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2718552f7358SJed Brown     PetscInt cdof;
2719552f7358SJed Brown 
2720552f7358SJed Brown     if (numFields) {
2721552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2722552f7358SJed Brown         ierr              = PetscSectionGetFieldConstraintDof(section, p, f, &cdof);CHKERRQ(ierr);
2723552f7358SJed Brown         maxConstraints[f] = PetscMax(maxConstraints[f], cdof);
2724552f7358SJed Brown       }
2725552f7358SJed Brown     } else {
2726552f7358SJed Brown       ierr              = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
2727552f7358SJed Brown       maxConstraints[0] = PetscMax(maxConstraints[0], cdof);
2728552f7358SJed Brown     }
2729552f7358SJed Brown   }
2730552f7358SJed Brown   for (f = 0; f < numFields; ++f) {
2731552f7358SJed Brown     maxConstraints[numFields] += maxConstraints[f];
2732552f7358SJed Brown   }
2733552f7358SJed Brown   if (maxConstraints[numFields]) {
2734552f7358SJed Brown     PetscInt *indices;
2735552f7358SJed Brown 
2736785e854fSJed Brown     ierr = PetscMalloc1(maxConstraints[numFields], &indices);CHKERRQ(ierr);
2737552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2738552f7358SJed Brown       PetscInt cdof, d;
2739552f7358SJed Brown 
2740552f7358SJed Brown       ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
2741552f7358SJed Brown       if (cdof) {
2742552f7358SJed Brown         if (cdof > maxConstraints[numFields]) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_LIB, "Likely memory corruption, point %D cDof %D > maxConstraints %D", p, cdof, maxConstraints[numFields]);
2743552f7358SJed Brown         if (numFields) {
2744552f7358SJed Brown           PetscInt numConst = 0, foff = 0;
2745552f7358SJed Brown 
2746552f7358SJed Brown           for (f = 0; f < numFields; ++f) {
2747552f7358SJed Brown             PetscInt cfdof, fdof;
2748552f7358SJed Brown 
2749552f7358SJed Brown             ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
2750552f7358SJed Brown             ierr = PetscSectionGetFieldConstraintDof(section, p, f, &cfdof);CHKERRQ(ierr);
2751552f7358SJed Brown             /* Change constraint numbering from absolute local dof number to field relative local dof number */
27520d644c17SKarl Rupp             for (d = 0; d < cfdof; ++d) indices[numConst+d] = d;
2753552f7358SJed Brown             ierr = PetscSectionSetFieldConstraintIndices(section, p, f, &indices[numConst]);CHKERRQ(ierr);
27540d644c17SKarl Rupp             for (d = 0; d < cfdof; ++d) indices[numConst+d] += foff;
2755552f7358SJed Brown             numConst += cfdof;
2756552f7358SJed Brown             foff     += fdof;
2757552f7358SJed Brown           }
2758552f7358SJed Brown           if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
2759552f7358SJed Brown         } else {
27600d644c17SKarl Rupp           for (d = 0; d < cdof; ++d) indices[d] = d;
2761552f7358SJed Brown         }
2762552f7358SJed Brown         ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
2763552f7358SJed Brown       }
2764552f7358SJed Brown     }
2765552f7358SJed Brown     ierr = PetscFree(indices);CHKERRQ(ierr);
2766552f7358SJed Brown   }
2767552f7358SJed Brown   ierr = PetscFree(maxConstraints);CHKERRQ(ierr);
2768552f7358SJed Brown   PetscFunctionReturn(0);
2769552f7358SJed Brown }
2770552f7358SJed Brown 
2771552f7358SJed Brown #undef __FUNCT__
2772552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCIndicesField"
2773552f7358SJed Brown /* Set the constrained field indices on each point */
27740adebc6cSBarry Smith PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt field, IS bcPoints, IS constraintIndices, PetscSection section)
27750adebc6cSBarry Smith {
2776552f7358SJed Brown   const PetscInt *points, *indices;
2777552f7358SJed Brown   PetscInt        numFields, maxDof, numPoints, p, numConstraints;
2778552f7358SJed Brown   PetscErrorCode  ierr;
2779552f7358SJed Brown 
2780552f7358SJed Brown   PetscFunctionBegin;
2781552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
2782552f7358SJed Brown   if ((field < 0) || (field >= numFields)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Section field %d should be in [%d, %d)", field, 0, numFields);
2783552f7358SJed Brown 
2784552f7358SJed Brown   ierr = ISGetLocalSize(bcPoints, &numPoints);CHKERRQ(ierr);
2785552f7358SJed Brown   ierr = ISGetIndices(bcPoints, &points);CHKERRQ(ierr);
2786552f7358SJed Brown   if (!constraintIndices) {
2787552f7358SJed Brown     PetscInt *idx, i;
2788552f7358SJed Brown 
2789552f7358SJed Brown     ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
2790785e854fSJed Brown     ierr = PetscMalloc1(maxDof, &idx);CHKERRQ(ierr);
27910d644c17SKarl Rupp     for (i = 0; i < maxDof; ++i) idx[i] = i;
2792552f7358SJed Brown     for (p = 0; p < numPoints; ++p) {
2793552f7358SJed Brown       ierr = PetscSectionSetFieldConstraintIndices(section, points[p], field, idx);CHKERRQ(ierr);
2794552f7358SJed Brown     }
2795552f7358SJed Brown     ierr = PetscFree(idx);CHKERRQ(ierr);
2796552f7358SJed Brown   } else {
2797552f7358SJed Brown     ierr = ISGetLocalSize(constraintIndices, &numConstraints);CHKERRQ(ierr);
2798552f7358SJed Brown     ierr = ISGetIndices(constraintIndices, &indices);CHKERRQ(ierr);
2799552f7358SJed Brown     for (p = 0; p < numPoints; ++p) {
2800552f7358SJed Brown       PetscInt fcdof;
2801552f7358SJed Brown 
2802552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintDof(section, points[p], field, &fcdof);CHKERRQ(ierr);
280382f516ccSBarry Smith       if (fcdof != numConstraints) SETERRQ4(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Section point %d field %d has %d constraints, but yo ugave %d indices", p, field, fcdof, numConstraints);
2804552f7358SJed Brown       ierr = PetscSectionSetFieldConstraintIndices(section, points[p], field, indices);CHKERRQ(ierr);
2805552f7358SJed Brown     }
2806552f7358SJed Brown     ierr = ISRestoreIndices(constraintIndices, &indices);CHKERRQ(ierr);
2807552f7358SJed Brown   }
2808552f7358SJed Brown   ierr = ISRestoreIndices(bcPoints, &points);CHKERRQ(ierr);
2809552f7358SJed Brown   PetscFunctionReturn(0);
2810552f7358SJed Brown }
2811552f7358SJed Brown 
2812552f7358SJed Brown #undef __FUNCT__
2813552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCIndices"
2814552f7358SJed Brown /* Set the constrained indices on each point and separate by fields */
28150adebc6cSBarry Smith PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
28160adebc6cSBarry Smith {
2817552f7358SJed Brown   PetscInt      *indices;
2818552f7358SJed Brown   PetscInt       numFields, maxDof, f, pStart = 0, pEnd = 0, p;
2819552f7358SJed Brown   PetscErrorCode ierr;
2820552f7358SJed Brown 
2821552f7358SJed Brown   PetscFunctionBegin;
2822552f7358SJed Brown   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
2823785e854fSJed Brown   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
2824552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
282582f516ccSBarry Smith   if (!numFields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "This function only works after users have set field constraint indices.");
2826552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
2827552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2828552f7358SJed Brown     PetscInt cdof, d;
2829552f7358SJed Brown 
2830552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
2831552f7358SJed Brown     if (cdof) {
2832552f7358SJed Brown       PetscInt numConst = 0, foff = 0;
2833552f7358SJed Brown 
2834552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2835552f7358SJed Brown         const PetscInt *fcind;
2836552f7358SJed Brown         PetscInt        fdof, fcdof;
2837552f7358SJed Brown 
2838552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
2839552f7358SJed Brown         ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
2840552f7358SJed Brown         if (fcdof) {ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &fcind);CHKERRQ(ierr);}
2841552f7358SJed Brown         /* Change constraint numbering from field relative local dof number to absolute local dof number */
28420d644c17SKarl Rupp         for (d = 0; d < fcdof; ++d) indices[numConst+d] = fcind[d]+foff;
2843552f7358SJed Brown         foff     += fdof;
2844552f7358SJed Brown         numConst += fcdof;
2845552f7358SJed Brown       }
284682f516ccSBarry Smith       if (cdof != numConst) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
2847552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
2848552f7358SJed Brown     }
2849552f7358SJed Brown   }
2850552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
2851552f7358SJed Brown   PetscFunctionReturn(0);
2852552f7358SJed Brown }
2853552f7358SJed Brown 
2854552f7358SJed Brown #undef __FUNCT__
2855552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSection"
2856552f7358SJed Brown /*@C
2857552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
2858552f7358SJed Brown 
2859552f7358SJed Brown   Not Collective
2860552f7358SJed Brown 
2861552f7358SJed Brown   Input Parameters:
2862552f7358SJed Brown + dm        - The DMPlex object
2863552f7358SJed Brown . dim       - The spatial dimension of the problem
2864552f7358SJed Brown . numFields - The number of fields in the problem
2865552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
2866552f7358SJed 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
2867552f7358SJed Brown . numBC     - The number of boundary conditions
2868552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
2869f8f126e8SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the sieve points to which each boundary condition applies
2870f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
2871552f7358SJed Brown 
2872552f7358SJed Brown   Output Parameter:
2873552f7358SJed Brown . section - The PetscSection object
2874552f7358SJed Brown 
2875552f7358SJed Brown   Notes: numDof[f*(dim+1)+d] gives the number of dof for field f on sieve points of dimension d. For instance, numDof[1] is the
2876f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
2877f8f126e8SMatthew G. Knepley 
2878f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
2879552f7358SJed Brown 
2880552f7358SJed Brown   Level: developer
2881552f7358SJed Brown 
28823813dfbdSMatthew G Knepley   Fortran Notes:
28833813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
28843813dfbdSMatthew G Knepley 
2885552f7358SJed Brown .keywords: mesh, elements
2886f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
2887552f7358SJed Brown @*/
2888f8f126e8SMatthew G. Knepley PetscErrorCode DMPlexCreateSection(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscInt numBC,const PetscInt bcField[],const IS bcPoints[], IS perm, PetscSection *section)
2889a6dfd86eSKarl Rupp {
2890a68b90caSToby Isaac   PetscSection   aSec;
2891552f7358SJed Brown   PetscErrorCode ierr;
2892552f7358SJed Brown 
2893552f7358SJed Brown   PetscFunctionBegin;
2894552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
2895552f7358SJed Brown   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcPoints, PETSC_DETERMINE, *section);CHKERRQ(ierr);
2896f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
2897552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
2898a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
2899a68b90caSToby Isaac   if (numBC || aSec) {ierr = DMPlexCreateSectionBCIndicesAll(dm, *section);CHKERRQ(ierr);}
2900ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
2901552f7358SJed Brown   PetscFunctionReturn(0);
2902552f7358SJed Brown }
2903552f7358SJed Brown 
2904552f7358SJed Brown #undef __FUNCT__
2905552f7358SJed Brown #define __FUNCT__ "DMCreateCoordinateDM_Plex"
29060adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
29070adebc6cSBarry Smith {
2908552f7358SJed Brown   PetscSection   section;
2909552f7358SJed Brown   PetscErrorCode ierr;
2910552f7358SJed Brown 
2911552f7358SJed Brown   PetscFunctionBegin;
291238221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
291382f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
2914552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
29151d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
2916552f7358SJed Brown   PetscFunctionReturn(0);
2917552f7358SJed Brown }
2918552f7358SJed Brown 
2919552f7358SJed Brown #undef __FUNCT__
2920552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSection"
29210adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
29220adebc6cSBarry Smith {
2923552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2924552f7358SJed Brown 
2925552f7358SJed Brown   PetscFunctionBegin;
2926552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2927552f7358SJed Brown   if (section) *section = mesh->coneSection;
2928552f7358SJed Brown   PetscFunctionReturn(0);
2929552f7358SJed Brown }
2930552f7358SJed Brown 
2931552f7358SJed Brown #undef __FUNCT__
29328cb4d582SMatthew G. Knepley #define __FUNCT__ "DMPlexGetSupportSection"
29338cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
29348cb4d582SMatthew G. Knepley {
29358cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
29368cb4d582SMatthew G. Knepley 
29378cb4d582SMatthew G. Knepley   PetscFunctionBegin;
29388cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29398cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
29408cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
29418cb4d582SMatthew G. Knepley }
29428cb4d582SMatthew G. Knepley 
29438cb4d582SMatthew G. Knepley #undef __FUNCT__
2944552f7358SJed Brown #define __FUNCT__ "DMPlexGetCones"
2945a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
2946a6dfd86eSKarl Rupp {
2947552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2948552f7358SJed Brown 
2949552f7358SJed Brown   PetscFunctionBegin;
2950552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2951552f7358SJed Brown   if (cones) *cones = mesh->cones;
2952552f7358SJed Brown   PetscFunctionReturn(0);
2953552f7358SJed Brown }
2954552f7358SJed Brown 
2955552f7358SJed Brown #undef __FUNCT__
2956552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientations"
2957a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
2958a6dfd86eSKarl Rupp {
2959552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2960552f7358SJed Brown 
2961552f7358SJed Brown   PetscFunctionBegin;
2962552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2963552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
2964552f7358SJed Brown   PetscFunctionReturn(0);
2965552f7358SJed Brown }
2966552f7358SJed Brown 
2967552f7358SJed Brown /******************************** FEM Support **********************************/
2968552f7358SJed Brown 
2969552f7358SJed Brown #undef __FUNCT__
29701a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Depth1_Static"
29711a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
2972a6dfd86eSKarl Rupp {
2973552f7358SJed Brown   PetscScalar    *array, *vArray;
2974d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
29751a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
2976552f7358SJed Brown   PetscErrorCode  ierr;
2977552f7358SJed Brown 
29781b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
29792a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
29805a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
29815a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
29825a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
29833f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
29849df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
29859df71ca4SMatthew G. Knepley       PetscInt dof;
2986d9917b9dSMatthew G. Knepley 
29879df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
29889df71ca4SMatthew G. Knepley       size += dof;
29899df71ca4SMatthew G. Knepley     }
29909df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
29919df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
29922a3aaacfSMatthew G. Knepley       PetscInt       dof;
29935a1bb5cfSMatthew G. Knepley 
29945a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
29952a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
29965a1bb5cfSMatthew G. Knepley       size += dof;
29975a1bb5cfSMatthew G. Knepley     }
29983f7cbbe7SMatthew G. Knepley     if (!values) {
29993f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
30003f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
30013f7cbbe7SMatthew G. Knepley     }
30025a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3003982e9ed1SMatthew G. Knepley   } else {
3004982e9ed1SMatthew G. Knepley     array = *values;
3005982e9ed1SMatthew G. Knepley   }
30069df71ca4SMatthew G. Knepley   size = 0;
30075a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
30089df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
30099df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
30109df71ca4SMatthew G. Knepley     PetscScalar *varr;
3011d9917b9dSMatthew G. Knepley 
30129df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
30139df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
30149df71ca4SMatthew G. Knepley     varr = &vArray[off];
30151a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
30161a271a75SMatthew G. Knepley       array[offset] = varr[d];
30179df71ca4SMatthew G. Knepley     }
30189df71ca4SMatthew G. Knepley     size += dof;
30199df71ca4SMatthew G. Knepley   }
30209df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
30219df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
30229df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
30235a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
30245a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
30255a1bb5cfSMatthew G. Knepley 
302652ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
30275a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
30285a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
30295a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
30305a1bb5cfSMatthew G. Knepley     if (o >= 0) {
30311a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
30321a271a75SMatthew G. Knepley         array[offset] = varr[d];
30335a1bb5cfSMatthew G. Knepley       }
30345a1bb5cfSMatthew G. Knepley     } else {
30351a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
30361a271a75SMatthew G. Knepley         array[offset] = varr[d];
30375a1bb5cfSMatthew G. Knepley       }
30385a1bb5cfSMatthew G. Knepley     }
30399df71ca4SMatthew G. Knepley     size += dof;
30405a1bb5cfSMatthew G. Knepley   }
30415a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
30429df71ca4SMatthew G. Knepley   if (!*values) {
30435a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
30445a1bb5cfSMatthew G. Knepley     *values = array;
30459df71ca4SMatthew G. Knepley   } else {
30469df71ca4SMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %d < actual size %d", *csize, size);
30478c312ff3SMatthew G. Knepley     *csize = size;
30489df71ca4SMatthew G. Knepley   }
30495a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
30505a1bb5cfSMatthew G. Knepley }
3051d9917b9dSMatthew G. Knepley 
3052d9917b9dSMatthew G. Knepley #undef __FUNCT__
30531a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Static"
30541a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Static(PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
30551a271a75SMatthew G. Knepley {
30561a271a75SMatthew G. Knepley   PetscInt       offset = 0, p;
30571a271a75SMatthew G. Knepley   PetscErrorCode ierr;
30581a271a75SMatthew G. Knepley 
30591a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3060fe02ba77SJed Brown   *size = 0;
30611a271a75SMatthew G. Knepley   for (p = 0; p < numPoints*2; p += 2) {
30621a271a75SMatthew G. Knepley     const PetscInt point = points[p];
30631a271a75SMatthew G. Knepley     const PetscInt o     = points[p+1];
30641a271a75SMatthew G. Knepley     PetscInt       dof, off, d;
30651a271a75SMatthew G. Knepley     const PetscScalar *varr;
30661a271a75SMatthew G. Knepley 
30671a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
30681a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
30691a271a75SMatthew G. Knepley     varr = &vArray[off];
30701a271a75SMatthew G. Knepley     if (o >= 0) {
30711a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset)    array[offset] = varr[d];
30721a271a75SMatthew G. Knepley     } else {
30731a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) array[offset] = varr[d];
30741a271a75SMatthew G. Knepley     }
30751a271a75SMatthew G. Knepley   }
30761a271a75SMatthew G. Knepley   *size = offset;
30771a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
30781a271a75SMatthew G. Knepley }
30791a271a75SMatthew G. Knepley 
30801a271a75SMatthew G. Knepley #undef __FUNCT__
30811a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Fields_Static"
30821a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Fields_Static(PetscSection section, PetscInt numPoints, const PetscInt points[], PetscInt numFields, const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
30831a271a75SMatthew G. Knepley {
30841a271a75SMatthew G. Knepley   PetscInt       offset = 0, f;
30851a271a75SMatthew G. Knepley   PetscErrorCode ierr;
30861a271a75SMatthew G. Knepley 
30871a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3088fe02ba77SJed Brown   *size = 0;
30891a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
30901a271a75SMatthew G. Knepley     PetscInt fcomp, p;
30911a271a75SMatthew G. Knepley 
30921a271a75SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
30931a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
30941a271a75SMatthew G. Knepley       const PetscInt point = points[p];
30951a271a75SMatthew G. Knepley       const PetscInt o     = points[p+1];
30961a271a75SMatthew G. Knepley       PetscInt       fdof, foff, d, c;
30971a271a75SMatthew G. Knepley       const PetscScalar *varr;
30981a271a75SMatthew G. Knepley 
30991a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
31001a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
31011a271a75SMatthew G. Knepley       varr = &vArray[foff];
31021a271a75SMatthew G. Knepley       if (o >= 0) {
31031a271a75SMatthew G. Knepley         for (d = 0; d < fdof; ++d, ++offset) array[offset] = varr[d];
31041a271a75SMatthew G. Knepley       } else {
31051a271a75SMatthew G. Knepley         for (d = fdof/fcomp-1; d >= 0; --d) {
31061a271a75SMatthew G. Knepley           for (c = 0; c < fcomp; ++c, ++offset) {
31071a271a75SMatthew G. Knepley             array[offset] = varr[d*fcomp+c];
31081a271a75SMatthew G. Knepley           }
31091a271a75SMatthew G. Knepley         }
31101a271a75SMatthew G. Knepley       }
31111a271a75SMatthew G. Knepley     }
31121a271a75SMatthew G. Knepley   }
31131a271a75SMatthew G. Knepley   *size = offset;
31141a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
31151a271a75SMatthew G. Knepley }
31161a271a75SMatthew G. Knepley 
31171a271a75SMatthew G. Knepley #undef __FUNCT__
3118552f7358SJed Brown #define __FUNCT__ "DMPlexVecGetClosure"
3119552f7358SJed Brown /*@C
3120552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3121552f7358SJed Brown 
3122552f7358SJed Brown   Not collective
3123552f7358SJed Brown 
3124552f7358SJed Brown   Input Parameters:
3125552f7358SJed Brown + dm - The DM
3126552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3127552f7358SJed Brown . v - The local vector
3128552f7358SJed Brown - point - The sieve point in the DM
3129552f7358SJed Brown 
3130552f7358SJed Brown   Output Parameters:
3131552f7358SJed Brown + csize - The number of values in the closure, or NULL
3132552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3133552f7358SJed Brown 
3134552f7358SJed Brown   Fortran Notes:
3135552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3136552f7358SJed Brown   include petsc.h90 in your code.
3137552f7358SJed Brown 
3138552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
3139552f7358SJed Brown 
3140552f7358SJed Brown   Level: intermediate
3141552f7358SJed Brown 
3142552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3143552f7358SJed Brown @*/
3144552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3145552f7358SJed Brown {
3146552f7358SJed Brown   PetscSection    clSection;
3147d9917b9dSMatthew G. Knepley   IS              clPoints;
3148552f7358SJed Brown   PetscScalar    *array, *vArray;
3149552f7358SJed Brown   PetscInt       *points = NULL;
3150d9917b9dSMatthew G. Knepley   const PetscInt *clp;
31511a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, size;
3152552f7358SJed Brown   PetscErrorCode  ierr;
3153552f7358SJed Brown 
3154d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
3155552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3156552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
31571a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
31581a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3159552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3160552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3161552f7358SJed Brown   if (depth == 1 && numFields < 2) {
31621a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
3163552f7358SJed Brown     PetscFunctionReturn(0);
3164552f7358SJed Brown   }
31651a271a75SMatthew G. Knepley   /* Get points */
3166d9917b9dSMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);CHKERRQ(ierr);
3167d9917b9dSMatthew G. Knepley   if (!clPoints) {
31681a271a75SMatthew G. Knepley     PetscInt pStart, pEnd, p, q;
3169552f7358SJed Brown 
31701a271a75SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3171552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
3172552f7358SJed Brown     /* Compress out points not in the section */
3173552f7358SJed Brown     for (p = 0, q = 0; p < numPoints*2; p += 2) {
3174552f7358SJed Brown       if ((points[p] >= pStart) && (points[p] < pEnd)) {
3175552f7358SJed Brown         points[q*2]   = points[p];
3176552f7358SJed Brown         points[q*2+1] = points[p+1];
3177552f7358SJed Brown         ++q;
3178552f7358SJed Brown       }
3179552f7358SJed Brown     }
3180552f7358SJed Brown     numPoints = q;
3181d9917b9dSMatthew G. Knepley   } else {
3182d9917b9dSMatthew G. Knepley     PetscInt dof, off;
3183552f7358SJed Brown 
3184d9917b9dSMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
3185d9917b9dSMatthew G. Knepley     ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
3186d9917b9dSMatthew G. Knepley     ierr = ISGetIndices(clPoints, &clp);CHKERRQ(ierr);
31871a271a75SMatthew G. Knepley     numPoints = dof/2;
3188d9917b9dSMatthew G. Knepley     points    = (PetscInt *) &clp[off];
3189552f7358SJed Brown   }
31901a271a75SMatthew G. Knepley   /* Get array */
3191552f7358SJed Brown   if (!values || !*values) {
31921a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
3193552f7358SJed Brown 
31941a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
3195552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
31961a271a75SMatthew G. Knepley       asize += dof;
3197552f7358SJed Brown     }
3198bd6c0d32SMatthew G. Knepley     if (!values) {
3199d9917b9dSMatthew G. Knepley       if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
32001a271a75SMatthew G. Knepley       else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
32011a271a75SMatthew G. Knepley       if (csize) *csize = asize;
3202bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
3203bd6c0d32SMatthew G. Knepley     }
32041a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
3205d0f6b257SMatthew G. Knepley   } else {
3206d0f6b257SMatthew G. Knepley     array = *values;
3207d0f6b257SMatthew G. Knepley   }
3208552f7358SJed Brown   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
32091a271a75SMatthew G. Knepley   /* Get values */
3210ef90cfe2SMatthew G. Knepley   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(section, numPoints, points, numFields, vArray, &size, array);CHKERRQ(ierr);}
32111a271a75SMatthew G. Knepley   else               {ierr = DMPlexVecGetClosure_Static(section, numPoints, points, vArray, &size, array);CHKERRQ(ierr);}
32121a271a75SMatthew G. Knepley   /* Cleanup points */
32131a271a75SMatthew G. Knepley   if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
32141a271a75SMatthew G. Knepley   else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
32151a271a75SMatthew G. Knepley   /* Cleanup array */
3216552f7358SJed Brown   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
3217d0f6b257SMatthew G. Knepley   if (!*values) {
3218552f7358SJed Brown     if (csize) *csize = size;
3219552f7358SJed Brown     *values = array;
3220d0f6b257SMatthew G. Knepley   } else {
3221d0f6b257SMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %d < actual size %d", *csize, size);
3222d0f6b257SMatthew G. Knepley     *csize = size;
3223d0f6b257SMatthew G. Knepley   }
3224552f7358SJed Brown   PetscFunctionReturn(0);
3225552f7358SJed Brown }
3226552f7358SJed Brown 
3227552f7358SJed Brown #undef __FUNCT__
3228552f7358SJed Brown #define __FUNCT__ "DMPlexVecRestoreClosure"
3229552f7358SJed Brown /*@C
3230552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
3231552f7358SJed Brown 
3232552f7358SJed Brown   Not collective
3233552f7358SJed Brown 
3234552f7358SJed Brown   Input Parameters:
3235552f7358SJed Brown + dm - The DM
32360298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
3237552f7358SJed Brown . v - The local vector
3238552f7358SJed Brown . point - The sieve point in the DM
32390298fd71SBarry Smith . csize - The number of values in the closure, or NULL
3240552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3241552f7358SJed Brown 
32423813dfbdSMatthew G Knepley   Fortran Notes:
32433813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32443813dfbdSMatthew G Knepley   include petsc.h90 in your code.
32453813dfbdSMatthew G Knepley 
32463813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
32473813dfbdSMatthew G Knepley 
3248552f7358SJed Brown   Level: intermediate
3249552f7358SJed Brown 
3250552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3251552f7358SJed Brown @*/
32527c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3253a6dfd86eSKarl Rupp {
3254552f7358SJed Brown   PetscInt       size = 0;
3255552f7358SJed Brown   PetscErrorCode ierr;
3256552f7358SJed Brown 
3257552f7358SJed Brown   PetscFunctionBegin;
3258552f7358SJed Brown   /* Should work without recalculating size */
3259552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
3260552f7358SJed Brown   PetscFunctionReturn(0);
3261552f7358SJed Brown }
3262552f7358SJed Brown 
3263552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
3264552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
3265552f7358SJed Brown 
3266552f7358SJed Brown #undef __FUNCT__
3267552f7358SJed Brown #define __FUNCT__ "updatePoint_private"
32681a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, PetscInt orientation, const PetscScalar values[], PetscScalar array[])
3269552f7358SJed Brown {
3270552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
3271552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3272552f7358SJed Brown   PetscScalar    *a;
3273552f7358SJed Brown   PetscInt        off, cind = 0, k;
3274552f7358SJed Brown   PetscErrorCode  ierr;
3275552f7358SJed Brown 
3276552f7358SJed Brown   PetscFunctionBegin;
3277552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3278552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3279552f7358SJed Brown   a    = &array[off];
3280552f7358SJed Brown   if (!cdof || setBC) {
3281552f7358SJed Brown     if (orientation >= 0) {
3282552f7358SJed Brown       for (k = 0; k < dof; ++k) {
3283552f7358SJed Brown         fuse(&a[k], values[k]);
3284552f7358SJed Brown       }
3285552f7358SJed Brown     } else {
3286552f7358SJed Brown       for (k = 0; k < dof; ++k) {
3287552f7358SJed Brown         fuse(&a[k], values[dof-k-1]);
3288552f7358SJed Brown       }
3289552f7358SJed Brown     }
3290552f7358SJed Brown   } else {
3291552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
3292552f7358SJed Brown     if (orientation >= 0) {
3293552f7358SJed Brown       for (k = 0; k < dof; ++k) {
3294552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3295552f7358SJed Brown         fuse(&a[k], values[k]);
3296552f7358SJed Brown       }
3297552f7358SJed Brown     } else {
3298552f7358SJed Brown       for (k = 0; k < dof; ++k) {
3299552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3300552f7358SJed Brown         fuse(&a[k], values[dof-k-1]);
3301552f7358SJed Brown       }
3302552f7358SJed Brown     }
3303552f7358SJed Brown   }
3304552f7358SJed Brown   PetscFunctionReturn(0);
3305552f7358SJed Brown }
3306552f7358SJed Brown 
3307552f7358SJed Brown #undef __FUNCT__
3308a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointBC_private"
33091a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscInt orientation, const PetscScalar values[], PetscScalar array[])
3310a5e93ea8SMatthew G. Knepley {
3311a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
3312a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3313a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
3314a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
3315a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
3316a5e93ea8SMatthew G. Knepley 
3317a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
3318a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3319a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3320a5e93ea8SMatthew G. Knepley   a    = &array[off];
3321a5e93ea8SMatthew G. Knepley   if (cdof) {
3322a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
3323a5e93ea8SMatthew G. Knepley     if (orientation >= 0) {
3324a5e93ea8SMatthew G. Knepley       for (k = 0; k < dof; ++k) {
3325a5e93ea8SMatthew G. Knepley         if ((cind < cdof) && (k == cdofs[cind])) {
3326a5e93ea8SMatthew G. Knepley           fuse(&a[k], values[k]);
3327a5e93ea8SMatthew G. Knepley           ++cind;
3328a5e93ea8SMatthew G. Knepley         }
3329a5e93ea8SMatthew G. Knepley       }
3330a5e93ea8SMatthew G. Knepley     } else {
3331a5e93ea8SMatthew G. Knepley       for (k = 0; k < dof; ++k) {
3332a5e93ea8SMatthew G. Knepley         if ((cind < cdof) && (k == cdofs[cind])) {
3333a5e93ea8SMatthew G. Knepley           fuse(&a[k], values[dof-k-1]);
3334a5e93ea8SMatthew G. Knepley           ++cind;
3335a5e93ea8SMatthew G. Knepley         }
3336a5e93ea8SMatthew G. Knepley       }
3337a5e93ea8SMatthew G. Knepley     }
3338a5e93ea8SMatthew G. Knepley   }
3339a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3340a5e93ea8SMatthew G. Knepley }
3341a5e93ea8SMatthew G. Knepley 
3342a5e93ea8SMatthew G. Knepley #undef __FUNCT__
3343552f7358SJed Brown #define __FUNCT__ "updatePointFields_private"
33441a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, PetscInt o, PetscInt f, PetscInt fcomp, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscScalar values[], PetscInt *offset, PetscScalar array[])
3345a6dfd86eSKarl Rupp {
3346552f7358SJed Brown   PetscScalar    *a;
33471a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
33481a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
33491a271a75SMatthew G. Knepley   PetscInt        cind = 0, k, c;
3350552f7358SJed Brown   PetscErrorCode  ierr;
3351552f7358SJed Brown 
3352552f7358SJed Brown   PetscFunctionBegin;
3353552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
3354552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
33551a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
33561a271a75SMatthew G. Knepley   a    = &array[foff];
3357552f7358SJed Brown   if (!fcdof || setBC) {
33581a271a75SMatthew G. Knepley     if (o >= 0) {
33591a271a75SMatthew G. Knepley       for (k = 0; k < fdof; ++k) fuse(&a[k], values[foffset+k]);
3360552f7358SJed Brown     } else {
3361552f7358SJed Brown       for (k = fdof/fcomp-1; k >= 0; --k) {
3362552f7358SJed Brown         for (c = 0; c < fcomp; ++c) {
33631a271a75SMatthew G. Knepley           fuse(&a[(fdof/fcomp-1-k)*fcomp+c], values[foffset+k*fcomp+c]);
3364552f7358SJed Brown         }
3365552f7358SJed Brown       }
3366552f7358SJed Brown     }
3367552f7358SJed Brown   } else {
3368552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
33691a271a75SMatthew G. Knepley     if (o >= 0) {
3370552f7358SJed Brown       for (k = 0; k < fdof; ++k) {
3371552f7358SJed Brown         if ((cind < fcdof) && (k == fcdofs[cind])) {++cind; continue;}
33721a271a75SMatthew G. Knepley         fuse(&a[k], values[foffset+k]);
3373552f7358SJed Brown       }
3374552f7358SJed Brown     } else {
3375552f7358SJed Brown       for (k = fdof/fcomp-1; k >= 0; --k) {
3376552f7358SJed Brown         for (c = 0; c < fcomp; ++c) {
3377552f7358SJed Brown           if ((cind < fcdof) && (k*fcomp+c == fcdofs[cind])) {++cind; continue;}
33781a271a75SMatthew G. Knepley           fuse(&a[(fdof/fcomp-1-k)*fcomp+c], values[foffset+k*fcomp+c]);
3379552f7358SJed Brown         }
3380552f7358SJed Brown       }
3381552f7358SJed Brown     }
3382552f7358SJed Brown   }
33831a271a75SMatthew G. Knepley   *offset += fdof;
3384552f7358SJed Brown   PetscFunctionReturn(0);
3385552f7358SJed Brown }
3386552f7358SJed Brown 
3387552f7358SJed Brown #undef __FUNCT__
3388a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointFieldsBC_private"
33891a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, PetscInt o, PetscInt f, PetscInt fcomp, void (*fuse)(PetscScalar*, PetscScalar), const PetscScalar values[], PetscInt *offset, PetscScalar array[])
3390a5e93ea8SMatthew G. Knepley {
3391a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
33921a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
33931a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
33941a271a75SMatthew G. Knepley   PetscInt        cind = 0, k, c;
3395a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
3396a5e93ea8SMatthew G. Knepley 
3397a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
3398a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
3399a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
34001a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
34011a271a75SMatthew G. Knepley   a    = &array[foff];
3402a5e93ea8SMatthew G. Knepley   if (fcdof) {
3403a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
34041a271a75SMatthew G. Knepley     if (o >= 0) {
3405a5e93ea8SMatthew G. Knepley       for (k = 0; k < fdof; ++k) {
3406a5e93ea8SMatthew G. Knepley         if ((cind < fcdof) && (k == fcdofs[cind])) {
34071a271a75SMatthew G. Knepley           fuse(&a[k], values[foffset+k]);
3408a5e93ea8SMatthew G. Knepley           ++cind;
3409a5e93ea8SMatthew G. Knepley         }
3410a5e93ea8SMatthew G. Knepley       }
3411a5e93ea8SMatthew G. Knepley     } else {
3412a5e93ea8SMatthew G. Knepley       for (k = fdof/fcomp-1; k >= 0; --k) {
3413a5e93ea8SMatthew G. Knepley         for (c = 0; c < fcomp; ++c) {
3414a5e93ea8SMatthew G. Knepley           if ((cind < fcdof) && (k*fcomp+c == fcdofs[cind])) {
34151a271a75SMatthew G. Knepley             fuse(&a[(fdof/fcomp-1-k)*fcomp+c], values[foffset+k*fcomp+c]);
3416a5e93ea8SMatthew G. Knepley             ++cind;
3417a5e93ea8SMatthew G. Knepley           }
3418a5e93ea8SMatthew G. Knepley         }
3419a5e93ea8SMatthew G. Knepley       }
3420a5e93ea8SMatthew G. Knepley     }
3421a5e93ea8SMatthew G. Knepley   }
34221a271a75SMatthew G. Knepley   *offset += fdof;
3423a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3424a5e93ea8SMatthew G. Knepley }
3425a5e93ea8SMatthew G. Knepley 
3426a5e93ea8SMatthew G. Knepley #undef __FUNCT__
34271b406b76SMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure_Static"
34281a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
3429a6dfd86eSKarl Rupp {
3430552f7358SJed Brown   PetscScalar    *array;
34311b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
34321b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
3433552f7358SJed Brown   PetscErrorCode  ierr;
3434552f7358SJed Brown 
34351b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
3436b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3437b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
3438b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3439b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
3440b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
3441b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
3442b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
3443b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
3444b6ebb6e6SMatthew G. Knepley 
3445b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
3446b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
3447b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
3448b6ebb6e6SMatthew G. Knepley     {
3449b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3450b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
3451b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
3452b6ebb6e6SMatthew G. Knepley 
3453b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
3454b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
3455b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
3456b6ebb6e6SMatthew G. Knepley       if (!cdof) {
3457b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
3458b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
3459b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
3460b6ebb6e6SMatthew G. Knepley           }
3461b6ebb6e6SMatthew G. Knepley         } else {
3462b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
3463b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
3464b6ebb6e6SMatthew G. Knepley           }
3465b6ebb6e6SMatthew G. Knepley         }
3466b6ebb6e6SMatthew G. Knepley       } else {
3467b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
3468b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
3469b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
3470b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3471b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
3472b6ebb6e6SMatthew G. Knepley           }
3473b6ebb6e6SMatthew G. Knepley         } else {
3474b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
3475b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3476b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
3477b6ebb6e6SMatthew G. Knepley           }
3478b6ebb6e6SMatthew G. Knepley         }
3479b6ebb6e6SMatthew G. Knepley       }
3480b6ebb6e6SMatthew G. Knepley     }
3481b6ebb6e6SMatthew G. Knepley   }
3482b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
3483b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
3484b6ebb6e6SMatthew G. Knepley }
34851b406b76SMatthew G. Knepley 
34861b406b76SMatthew G. Knepley #undef __FUNCT__
34871b406b76SMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure"
34881b406b76SMatthew G. Knepley /*@C
34891b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
34901b406b76SMatthew G. Knepley 
34911b406b76SMatthew G. Knepley   Not collective
34921b406b76SMatthew G. Knepley 
34931b406b76SMatthew G. Knepley   Input Parameters:
34941b406b76SMatthew G. Knepley + dm - The DM
34951b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
34961b406b76SMatthew G. Knepley . v - The local vector
34971b406b76SMatthew G. Knepley . point - The sieve point in the DM
34981b406b76SMatthew G. Knepley . values - The array of values
34991b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
35001b406b76SMatthew G. Knepley 
35011b406b76SMatthew G. Knepley   Fortran Notes:
35021b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
35031b406b76SMatthew G. Knepley 
35041b406b76SMatthew G. Knepley   Level: intermediate
35051b406b76SMatthew G. Knepley 
35061b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
35071b406b76SMatthew G. Knepley @*/
35081b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
35091b406b76SMatthew G. Knepley {
35101b406b76SMatthew G. Knepley   PetscSection    clSection;
35111b406b76SMatthew G. Knepley   IS              clPoints;
35121b406b76SMatthew G. Knepley   PetscScalar    *array;
35131b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
35141b406b76SMatthew G. Knepley   const PetscInt *clp;
35151a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
35161b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
35171b406b76SMatthew G. Knepley 
35181a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
35191b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35201b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
35211a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
35221a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
35231b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
35241b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
35251b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
35261a271a75SMatthew G. Knepley     ierr = DMPlexVecSetClosure_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
35271b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
35281b406b76SMatthew G. Knepley   }
35291a271a75SMatthew G. Knepley   /* Get points */
35301b406b76SMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);CHKERRQ(ierr);
35311b406b76SMatthew G. Knepley   if (!clPoints) {
35321a271a75SMatthew G. Knepley     PetscInt pStart, pEnd, q;
35331a271a75SMatthew G. Knepley 
35341a271a75SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3535552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
3536552f7358SJed Brown     /* Compress out points not in the section */
3537552f7358SJed Brown     for (p = 0, q = 0; p < numPoints*2; p += 2) {
3538552f7358SJed Brown       if ((points[p] >= pStart) && (points[p] < pEnd)) {
3539552f7358SJed Brown         points[q*2]   = points[p];
3540552f7358SJed Brown         points[q*2+1] = points[p+1];
3541552f7358SJed Brown         ++q;
3542552f7358SJed Brown       }
3543552f7358SJed Brown     }
3544552f7358SJed Brown     numPoints = q;
35451b406b76SMatthew G. Knepley   } else {
35461b406b76SMatthew G. Knepley     PetscInt dof, off;
3547552f7358SJed Brown 
35481b406b76SMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
35491b406b76SMatthew G. Knepley     ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
35501b406b76SMatthew G. Knepley     ierr = ISGetIndices(clPoints, &clp);CHKERRQ(ierr);
35511a271a75SMatthew G. Knepley     numPoints = dof/2;
35521b406b76SMatthew G. Knepley     points    = (PetscInt *) &clp[off];
3553552f7358SJed Brown   }
35541a271a75SMatthew G. Knepley   /* Get array */
3555552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
35561a271a75SMatthew G. Knepley   /* Get values */
3557ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
35581a271a75SMatthew G. Knepley     PetscInt offset = 0, fcomp, f;
3559552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
35601a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
3561552f7358SJed Brown       switch (mode) {
3562552f7358SJed Brown       case INSERT_VALUES:
3563552f7358SJed Brown         for (p = 0; p < numPoints*2; p += 2) {
35641a271a75SMatthew G. Knepley           const PetscInt point = points[p];
35651a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
35661a271a75SMatthew G. Knepley           updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_FALSE, values, &offset, array);
3567552f7358SJed Brown         } break;
3568552f7358SJed Brown       case INSERT_ALL_VALUES:
3569552f7358SJed Brown         for (p = 0; p < numPoints*2; p += 2) {
35701a271a75SMatthew G. Knepley           const PetscInt point = points[p];
35711a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
35721a271a75SMatthew G. Knepley           updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_TRUE, values, &offset, array);
3573552f7358SJed Brown         } break;
3574a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
3575a5e93ea8SMatthew G. Knepley         for (p = 0; p < numPoints*2; p += 2) {
35761a271a75SMatthew G. Knepley           const PetscInt point = points[p];
35771a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
35781a271a75SMatthew G. Knepley           updatePointFieldsBC_private(section, point, o, f, fcomp, insert, values, &offset, array);
3579a5e93ea8SMatthew G. Knepley         } break;
3580552f7358SJed Brown       case ADD_VALUES:
3581552f7358SJed Brown         for (p = 0; p < numPoints*2; p += 2) {
35821a271a75SMatthew G. Knepley           const PetscInt point = points[p];
35831a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
35841a271a75SMatthew G. Knepley           updatePointFields_private(section, point, o, f, fcomp, add, PETSC_FALSE, values, &offset, array);
3585552f7358SJed Brown         } break;
3586552f7358SJed Brown       case ADD_ALL_VALUES:
3587552f7358SJed Brown         for (p = 0; p < numPoints*2; p += 2) {
35881a271a75SMatthew G. Knepley           const PetscInt point = points[p];
35891a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
35901a271a75SMatthew G. Knepley           updatePointFields_private(section, point, o, f, fcomp, add, PETSC_TRUE, values, &offset, array);
3591552f7358SJed Brown         } break;
3592552f7358SJed Brown       default:
359382f516ccSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %D", mode);
3594552f7358SJed Brown       }
35951a271a75SMatthew G. Knepley     }
3596552f7358SJed Brown   } else {
35971a271a75SMatthew G. Knepley     PetscInt dof, off;
35981a271a75SMatthew G. Knepley 
3599552f7358SJed Brown     switch (mode) {
3600552f7358SJed Brown     case INSERT_VALUES:
3601552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3602552f7358SJed Brown         PetscInt o = points[p+1];
3603552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
3604552f7358SJed Brown         updatePoint_private(section, points[p], dof, insert, PETSC_FALSE, o, &values[off], array);
3605552f7358SJed Brown       } break;
3606552f7358SJed Brown     case INSERT_ALL_VALUES:
3607552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3608552f7358SJed Brown         PetscInt o = points[p+1];
3609552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
3610552f7358SJed Brown         updatePoint_private(section, points[p], dof, insert, PETSC_TRUE,  o, &values[off], array);
3611552f7358SJed Brown       } break;
3612a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
3613a5e93ea8SMatthew G. Knepley       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3614a5e93ea8SMatthew G. Knepley         PetscInt o = points[p+1];
3615a5e93ea8SMatthew G. Knepley         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
3616a5e93ea8SMatthew G. Knepley         updatePointBC_private(section, points[p], dof, insert,  o, &values[off], array);
3617a5e93ea8SMatthew G. Knepley       } break;
3618552f7358SJed Brown     case ADD_VALUES:
3619552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3620552f7358SJed Brown         PetscInt o = points[p+1];
3621552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
3622552f7358SJed Brown         updatePoint_private(section, points[p], dof, add,    PETSC_FALSE, o, &values[off], array);
3623552f7358SJed Brown       } break;
3624552f7358SJed Brown     case ADD_ALL_VALUES:
3625552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3626552f7358SJed Brown         PetscInt o = points[p+1];
3627552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
3628552f7358SJed Brown         updatePoint_private(section, points[p], dof, add,    PETSC_TRUE,  o, &values[off], array);
3629552f7358SJed Brown       } break;
3630552f7358SJed Brown     default:
363182f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %D", mode);
3632552f7358SJed Brown     }
3633552f7358SJed Brown   }
36341a271a75SMatthew G. Knepley   /* Cleanup points */
36351a271a75SMatthew G. Knepley   if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
36361a271a75SMatthew G. Knepley   else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
36371a271a75SMatthew G. Knepley   /* Cleanup array */
3638552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
3639552f7358SJed Brown   PetscFunctionReturn(0);
3640552f7358SJed Brown }
3641552f7358SJed Brown 
3642552f7358SJed Brown #undef __FUNCT__
3643e07394fbSMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetFieldClosure_Internal"
3644e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
3645e07394fbSMatthew G. Knepley {
3646e07394fbSMatthew G. Knepley   PetscSection    clSection;
3647e07394fbSMatthew G. Knepley   IS              clPoints;
3648e07394fbSMatthew G. Knepley   PetscScalar    *array;
3649e07394fbSMatthew G. Knepley   PetscInt       *points = NULL;
3650e07394fbSMatthew G. Knepley   const PetscInt *clp;
3651e07394fbSMatthew G. Knepley   PetscInt        numFields, numPoints, p;
3652e07394fbSMatthew G. Knepley   PetscInt        offset = 0, fcomp, f;
3653e07394fbSMatthew G. Knepley   PetscErrorCode  ierr;
3654e07394fbSMatthew G. Knepley 
3655e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
3656e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3657e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
3658e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
3659e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3660e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3661e07394fbSMatthew G. Knepley   /* Get points */
3662e07394fbSMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);CHKERRQ(ierr);
3663e07394fbSMatthew G. Knepley   if (!clPoints) {
3664e07394fbSMatthew G. Knepley     PetscInt pStart, pEnd, q;
3665e07394fbSMatthew G. Knepley 
3666e07394fbSMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3667e07394fbSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
3668e07394fbSMatthew G. Knepley     /* Compress out points not in the section */
3669e07394fbSMatthew G. Knepley     for (p = 0, q = 0; p < numPoints*2; p += 2) {
3670e07394fbSMatthew G. Knepley       if ((points[p] >= pStart) && (points[p] < pEnd)) {
3671e07394fbSMatthew G. Knepley         points[q*2]   = points[p];
3672e07394fbSMatthew G. Knepley         points[q*2+1] = points[p+1];
3673e07394fbSMatthew G. Knepley         ++q;
3674e07394fbSMatthew G. Knepley       }
3675e07394fbSMatthew G. Knepley     }
3676e07394fbSMatthew G. Knepley     numPoints = q;
3677e07394fbSMatthew G. Knepley   } else {
3678e07394fbSMatthew G. Knepley     PetscInt dof, off;
3679e07394fbSMatthew G. Knepley 
3680e07394fbSMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
3681e07394fbSMatthew G. Knepley     ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
3682e07394fbSMatthew G. Knepley     ierr = ISGetIndices(clPoints, &clp);CHKERRQ(ierr);
3683e07394fbSMatthew G. Knepley     numPoints = dof/2;
3684e07394fbSMatthew G. Knepley     points    = (PetscInt *) &clp[off];
3685e07394fbSMatthew G. Knepley   }
3686e07394fbSMatthew G. Knepley   /* Get array */
3687e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
3688e07394fbSMatthew G. Knepley   /* Get values */
3689e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
3690e07394fbSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
3691e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
3692e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
3693e07394fbSMatthew G. Knepley         PetscInt fdof;
3694e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
3695e07394fbSMatthew G. Knepley         offset += fdof;
3696e07394fbSMatthew G. Knepley       }
3697e07394fbSMatthew G. Knepley       continue;
3698e07394fbSMatthew G. Knepley     }
3699e07394fbSMatthew G. Knepley     switch (mode) {
3700e07394fbSMatthew G. Knepley     case INSERT_VALUES:
3701e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
3702e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
3703e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
3704e07394fbSMatthew G. Knepley         updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_FALSE, values, &offset, array);
3705e07394fbSMatthew G. Knepley       } break;
3706e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
3707e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
3708e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
3709e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
3710e07394fbSMatthew G. Knepley         updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_TRUE, values, &offset, array);
3711e07394fbSMatthew G. Knepley         } break;
3712e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
3713e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
3714e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
3715e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
3716e07394fbSMatthew G. Knepley         updatePointFieldsBC_private(section, point, o, f, fcomp, insert, values, &offset, array);
3717e07394fbSMatthew G. Knepley       } break;
3718e07394fbSMatthew G. Knepley     case ADD_VALUES:
3719e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
3720e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
3721e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
3722e07394fbSMatthew G. Knepley         updatePointFields_private(section, point, o, f, fcomp, add, PETSC_FALSE, values, &offset, array);
3723e07394fbSMatthew G. Knepley       } break;
3724e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
3725e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
3726e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
3727e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
3728e07394fbSMatthew G. Knepley         updatePointFields_private(section, point, o, f, fcomp, add, PETSC_TRUE, values, &offset, array);
3729e07394fbSMatthew G. Knepley       } break;
3730e07394fbSMatthew G. Knepley     default:
3731e07394fbSMatthew G. Knepley       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %D", mode);
3732e07394fbSMatthew G. Knepley     }
3733e07394fbSMatthew G. Knepley   }
3734e07394fbSMatthew G. Knepley   /* Cleanup points */
3735e07394fbSMatthew G. Knepley   if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
3736e07394fbSMatthew G. Knepley   else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
3737e07394fbSMatthew G. Knepley   /* Cleanup array */
3738e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
3739e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
3740e07394fbSMatthew G. Knepley }
3741e07394fbSMatthew G. Knepley 
3742e07394fbSMatthew G. Knepley #undef __FUNCT__
3743552f7358SJed Brown #define __FUNCT__ "DMPlexPrintMatSetValues"
3744b0ecff45SMatthew G. Knepley PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
3745552f7358SJed Brown {
3746552f7358SJed Brown   PetscMPIInt    rank;
3747552f7358SJed Brown   PetscInt       i, j;
3748552f7358SJed Brown   PetscErrorCode ierr;
3749552f7358SJed Brown 
3750552f7358SJed Brown   PetscFunctionBegin;
375182f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
375277a6746dSMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer, "[%D]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
3753b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%D]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
3754b0ecff45SMatthew G. Knepley   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%D]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
3755b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
3756b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
375777a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "[%D]", rank);CHKERRQ(ierr);
3758b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
3759519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
37607eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
3761552f7358SJed Brown #else
3762b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
3763552f7358SJed Brown #endif
3764552f7358SJed Brown     }
376577a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
3766552f7358SJed Brown   }
3767552f7358SJed Brown   PetscFunctionReturn(0);
3768552f7358SJed Brown }
3769552f7358SJed Brown 
3770552f7358SJed Brown #undef __FUNCT__
3771552f7358SJed Brown #define __FUNCT__ "indicesPoint_private"
3772552f7358SJed Brown /* . off - The global offset of this point */
3773e6ccafaeSMatthew G Knepley PetscErrorCode indicesPoint_private(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, PetscInt orientation, PetscInt indices[])
3774a6dfd86eSKarl Rupp {
3775e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
3776552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
3777552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3778552f7358SJed Brown   PetscInt        cind = 0, k;
3779552f7358SJed Brown   PetscErrorCode  ierr;
3780552f7358SJed Brown 
3781552f7358SJed Brown   PetscFunctionBegin;
3782552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
3783552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3784552f7358SJed Brown   if (!cdof || setBC) {
3785552f7358SJed Brown     if (orientation >= 0) {
3786e6ccafaeSMatthew G Knepley       for (k = 0; k < dof; ++k) indices[*loff+k] = off+k;
3787552f7358SJed Brown     } else {
3788e6ccafaeSMatthew G Knepley       for (k = 0; k < dof; ++k) indices[*loff+dof-k-1] = off+k;
3789552f7358SJed Brown     }
3790552f7358SJed Brown   } else {
3791552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
3792552f7358SJed Brown     if (orientation >= 0) {
3793552f7358SJed Brown       for (k = 0; k < dof; ++k) {
3794552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
3795552f7358SJed Brown           /* Insert check for returning constrained indices */
3796e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
3797552f7358SJed Brown           ++cind;
3798552f7358SJed Brown         } else {
3799e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
3800552f7358SJed Brown         }
3801552f7358SJed Brown       }
3802552f7358SJed Brown     } else {
3803552f7358SJed Brown       for (k = 0; k < dof; ++k) {
3804552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
3805552f7358SJed Brown           /* Insert check for returning constrained indices */
3806e6ccafaeSMatthew G Knepley           indices[*loff+dof-k-1] = -(off+k+1);
3807552f7358SJed Brown           ++cind;
3808552f7358SJed Brown         } else {
3809e6ccafaeSMatthew G Knepley           indices[*loff+dof-k-1] = off+k-cind;
3810552f7358SJed Brown         }
3811552f7358SJed Brown       }
3812552f7358SJed Brown     }
3813552f7358SJed Brown   }
3814e6ccafaeSMatthew G Knepley   *loff += dof;
3815552f7358SJed Brown   PetscFunctionReturn(0);
3816552f7358SJed Brown }
3817552f7358SJed Brown 
3818552f7358SJed Brown #undef __FUNCT__
3819552f7358SJed Brown #define __FUNCT__ "indicesPointFields_private"
3820552f7358SJed Brown /* . off - The global offset of this point */
3821a6dfd86eSKarl Rupp PetscErrorCode indicesPointFields_private(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, PetscInt orientation, PetscInt indices[])
3822a6dfd86eSKarl Rupp {
3823552f7358SJed Brown   PetscInt       numFields, foff, f;
3824552f7358SJed Brown   PetscErrorCode ierr;
3825552f7358SJed Brown 
3826552f7358SJed Brown   PetscFunctionBegin;
3827552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3828552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
3829552f7358SJed Brown     PetscInt        fdof, fcomp, cfdof;
3830552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
3831552f7358SJed Brown     PetscInt        cind = 0, k, c;
3832552f7358SJed Brown 
3833552f7358SJed Brown     ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
3834552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
3835552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
3836552f7358SJed Brown     if (!cfdof || setBC) {
3837552f7358SJed Brown       if (orientation >= 0) {
38380d644c17SKarl Rupp         for (k = 0; k < fdof; ++k) indices[foffs[f]+k] = off+foff+k;
3839552f7358SJed Brown       } else {
3840552f7358SJed Brown         for (k = fdof/fcomp-1; k >= 0; --k) {
3841552f7358SJed Brown           for (c = 0; c < fcomp; ++c) {
3842552f7358SJed Brown             indices[foffs[f]+k*fcomp+c] = off+foff+(fdof/fcomp-1-k)*fcomp+c;
3843552f7358SJed Brown           }
3844552f7358SJed Brown         }
3845552f7358SJed Brown       }
3846552f7358SJed Brown     } else {
3847552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
3848552f7358SJed Brown       if (orientation >= 0) {
3849552f7358SJed Brown         for (k = 0; k < fdof; ++k) {
3850552f7358SJed Brown           if ((cind < cfdof) && (k == fcdofs[cind])) {
3851552f7358SJed Brown             indices[foffs[f]+k] = -(off+foff+k+1);
3852552f7358SJed Brown             ++cind;
3853552f7358SJed Brown           } else {
3854552f7358SJed Brown             indices[foffs[f]+k] = off+foff+k-cind;
3855552f7358SJed Brown           }
3856552f7358SJed Brown         }
3857552f7358SJed Brown       } else {
3858552f7358SJed Brown         for (k = fdof/fcomp-1; k >= 0; --k) {
3859552f7358SJed Brown           for (c = 0; c < fcomp; ++c) {
3860552f7358SJed Brown             if ((cind < cfdof) && ((fdof/fcomp-1-k)*fcomp+c == fcdofs[cind])) {
3861552f7358SJed Brown               indices[foffs[f]+k*fcomp+c] = -(off+foff+(fdof/fcomp-1-k)*fcomp+c+1);
3862552f7358SJed Brown               ++cind;
3863552f7358SJed Brown             } else {
3864552f7358SJed Brown               indices[foffs[f]+k*fcomp+c] = off+foff+(fdof/fcomp-1-k)*fcomp+c-cind;
3865552f7358SJed Brown             }
3866552f7358SJed Brown           }
3867552f7358SJed Brown         }
3868552f7358SJed Brown       }
3869552f7358SJed Brown     }
3870552f7358SJed Brown     foff     += fdof - cfdof;
3871552f7358SJed Brown     foffs[f] += fdof;
3872552f7358SJed Brown   }
3873552f7358SJed Brown   PetscFunctionReturn(0);
3874552f7358SJed Brown }
3875552f7358SJed Brown 
3876552f7358SJed Brown #undef __FUNCT__
3877f7c74593SToby Isaac #define __FUNCT__ "DMPlexAnchorsModifyMat"
3878f7c74593SToby Isaac static PetscErrorCode DMPlexAnchorsModifyMat(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[])
3879d3d1a6afSToby Isaac {
3880d3d1a6afSToby Isaac   Mat             cMat;
3881d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
3882d3d1a6afSToby Isaac   IS              aIS;
3883d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
3884d3d1a6afSToby Isaac   const PetscInt  *anchors;
3885e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
3886d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
3887d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
3888d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
3889d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
3890d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
3891d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
3892d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
3893d3d1a6afSToby Isaac   PetscScalar     *newValues,*tmpValues;
3894d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
3895d3d1a6afSToby Isaac   PetscErrorCode  ierr;
3896d3d1a6afSToby Isaac 
3897d3d1a6afSToby Isaac   PetscFunctionBegin;
3898d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3899d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
3900d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3901d3d1a6afSToby Isaac 
3902a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
3903d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
3904d3d1a6afSToby Isaac   if (aSec) {
3905d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
3906d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
3907d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
3908d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
3909d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
3910d3d1a6afSToby Isaac      * into the global matrix anyway) */
3911d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
3912d3d1a6afSToby Isaac       PetscInt b    = points[p];
3913d3d1a6afSToby Isaac       PetscInt bDof = 0;
3914d3d1a6afSToby Isaac 
3915d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
3916d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
3917d3d1a6afSToby Isaac       }
3918d3d1a6afSToby Isaac       if (bDof) {
3919d3d1a6afSToby Isaac         /* this point is constrained */
3920d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
3921d3d1a6afSToby Isaac         PetscInt bOff, q;
3922d3d1a6afSToby Isaac 
3923d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
3924d3d1a6afSToby Isaac         newNumPoints  += bDof;
3925d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
3926d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
3927d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
3928d3d1a6afSToby Isaac           PetscInt aDof;
3929d3d1a6afSToby Isaac 
3930d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
3931d3d1a6afSToby Isaac           newNumIndices += aDof;
3932d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
3933d3d1a6afSToby Isaac             PetscInt fDof;
3934d3d1a6afSToby Isaac 
3935d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
3936d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
3937d3d1a6afSToby Isaac           }
3938d3d1a6afSToby Isaac         }
3939d3d1a6afSToby Isaac       }
3940d3d1a6afSToby Isaac       else {
3941d3d1a6afSToby Isaac         /* this point is not constrained */
3942d3d1a6afSToby Isaac         newNumPoints++;
3943d3d1a6afSToby Isaac         ierr           = PetscSectionGetDof(section,b,&bDof);CHKERRQ(ierr);
3944d3d1a6afSToby Isaac         newNumIndices += bDof;
3945d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
3946d3d1a6afSToby Isaac           PetscInt fDof;
3947d3d1a6afSToby Isaac 
3948d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
3949d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
3950d3d1a6afSToby Isaac         }
3951d3d1a6afSToby Isaac       }
3952d3d1a6afSToby Isaac     }
3953d3d1a6afSToby Isaac   }
3954d3d1a6afSToby Isaac   if (!anyConstrained) {
3955d3d1a6afSToby Isaac     *outNumPoints  = 0;
3956d3d1a6afSToby Isaac     *outNumIndices = 0;
3957d3d1a6afSToby Isaac     *outPoints     = NULL;
3958d3d1a6afSToby Isaac     *outValues     = NULL;
3959d3d1a6afSToby Isaac     if (aSec) {
3960d3d1a6afSToby Isaac       ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
3961d3d1a6afSToby Isaac     }
3962d3d1a6afSToby Isaac     PetscFunctionReturn(0);
3963d3d1a6afSToby Isaac   }
3964d3d1a6afSToby Isaac 
3965d3d1a6afSToby Isaac   for (f = 1; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
3966d3d1a6afSToby Isaac 
3967d3d1a6afSToby Isaac   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", newOffsets[numFields], newNumIndices);
3968d3d1a6afSToby Isaac 
3969f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
3970d3d1a6afSToby Isaac 
3971d3d1a6afSToby Isaac   /* output arrays */
3972d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
3973d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
3974d3d1a6afSToby Isaac 
3975d3d1a6afSToby Isaac   /* workspaces */
3976d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
3977d3d1a6afSToby Isaac   if (numFields) {
3978d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
3979d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
3980d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
3981d3d1a6afSToby Isaac     }
3982d3d1a6afSToby Isaac   }
3983d3d1a6afSToby Isaac   else {
3984d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
3985d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
3986d3d1a6afSToby Isaac   }
3987d3d1a6afSToby Isaac 
3988d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
3989d3d1a6afSToby Isaac   if (numFields) {
3990d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
3991d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
3992d3d1a6afSToby Isaac       PetscInt bDof = 0;
3993d3d1a6afSToby Isaac 
3994d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
3995d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
3996d3d1a6afSToby Isaac       }
3997d3d1a6afSToby Isaac       if (bDof) {
3998d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
3999d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4000d3d1a6afSToby Isaac 
4001d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4002d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4003d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4004d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4005d3d1a6afSToby Isaac             PetscInt aFDof;
4006d3d1a6afSToby Isaac 
4007d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4008d3d1a6afSToby Isaac             allFDof += aFDof;
4009d3d1a6afSToby Isaac           }
4010d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4011d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4012d3d1a6afSToby Isaac         }
4013d3d1a6afSToby Isaac       }
4014d3d1a6afSToby Isaac       else {
4015d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4016d3d1a6afSToby Isaac           PetscInt fDof;
4017d3d1a6afSToby Isaac 
4018d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4019d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4020d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4021d3d1a6afSToby Isaac         }
4022d3d1a6afSToby Isaac       }
4023d3d1a6afSToby Isaac     }
4024d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4025d3d1a6afSToby Isaac       newPointOffsets[f][0] = 0;
4026d3d1a6afSToby Isaac       pointMatOffsets[f][0] = 0;
4027d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4028d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4029d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4030d3d1a6afSToby Isaac       }
4031d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4032d3d1a6afSToby Isaac     }
4033d3d1a6afSToby Isaac   }
4034d3d1a6afSToby Isaac   else {
4035d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4036d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4037d3d1a6afSToby Isaac       PetscInt bDof = 0;
4038d3d1a6afSToby Isaac 
4039d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4040d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4041d3d1a6afSToby Isaac       }
4042d3d1a6afSToby Isaac       if (bDof) {
4043d3d1a6afSToby Isaac         PetscInt dof, bOff, q, allDof = 0;
4044d3d1a6afSToby Isaac 
4045d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section, b, &dof);CHKERRQ(ierr);
4046d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4047d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4048d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4049d3d1a6afSToby Isaac 
4050d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4051d3d1a6afSToby Isaac           allDof += aDof;
4052d3d1a6afSToby Isaac         }
4053d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
4054d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = dof * allDof;
4055d3d1a6afSToby Isaac       }
4056d3d1a6afSToby Isaac       else {
4057d3d1a6afSToby Isaac         PetscInt dof;
4058d3d1a6afSToby Isaac 
4059d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section, b, &dof);CHKERRQ(ierr);
4060d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = dof;
4061d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4062d3d1a6afSToby Isaac       }
4063d3d1a6afSToby Isaac     }
4064d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4065d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4066d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4067d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4068d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4069d3d1a6afSToby Isaac     }
4070d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4071d3d1a6afSToby Isaac   }
4072d3d1a6afSToby Isaac 
4073d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4074d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4075d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4076d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4077d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4078d3d1a6afSToby Isaac   if (numFields) {
4079d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4080d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4081d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
4082d3d1a6afSToby Isaac       PetscInt bDof = 0;
4083d3d1a6afSToby Isaac 
4084d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4085d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4086d3d1a6afSToby Isaac       }
4087d3d1a6afSToby Isaac       if (bDof) {
4088d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4089d3d1a6afSToby Isaac 
4090d3d1a6afSToby Isaac         fStart[0] = 0;
4091d3d1a6afSToby Isaac         fEnd[0]   = 0;
4092d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4093d3d1a6afSToby Isaac           PetscInt fDof;
4094d3d1a6afSToby Isaac 
4095d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4096d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4097d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4098d3d1a6afSToby Isaac         }
4099d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
4100d3d1a6afSToby Isaac         ierr = indicesPointFields_private(cSec, b, bOff, fEnd, PETSC_TRUE, o, indices);CHKERRQ(ierr);
4101d3d1a6afSToby Isaac 
4102d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4103d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4104d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4105d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4106d3d1a6afSToby Isaac 
4107d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4108d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4109d3d1a6afSToby Isaac         }
4110d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4111d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4112d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4113d3d1a6afSToby Isaac 
4114d3d1a6afSToby 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 */
4115d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4116d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4117d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(section, a, &aOff);
4118d3d1a6afSToby Isaac           ierr = indicesPointFields_private(section, a, aOff, fAnchorEnd, PETSC_TRUE, 0, newIndices);CHKERRQ(ierr);
4119d3d1a6afSToby Isaac         }
4120d3d1a6afSToby Isaac         newP += bDof;
4121d3d1a6afSToby Isaac 
4122d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
4123d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4124d3d1a6afSToby 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);
4125d3d1a6afSToby Isaac         }
4126d3d1a6afSToby Isaac       }
4127d3d1a6afSToby Isaac       else {
4128d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4129d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4130d3d1a6afSToby Isaac         newP++;
4131d3d1a6afSToby Isaac       }
4132d3d1a6afSToby Isaac     }
4133d3d1a6afSToby Isaac   } else {
4134d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4135d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4136d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
4137d3d1a6afSToby Isaac       PetscInt bDof = 0;
4138d3d1a6afSToby Isaac 
4139d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4140d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4141d3d1a6afSToby Isaac       }
4142d3d1a6afSToby Isaac       if (bDof) {
4143d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
4144d3d1a6afSToby Isaac 
4145d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
4146d3d1a6afSToby Isaac         ierr = indicesPoint_private(cSec, b, bOff, &bEnd, PETSC_TRUE, o, indices);CHKERRQ(ierr);
4147d3d1a6afSToby Isaac 
4148d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4149d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4150d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4151d3d1a6afSToby Isaac 
4152d3d1a6afSToby 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 */
4153d3d1a6afSToby Isaac 
4154d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4155d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4156d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(section, a, &aOff);
4157d3d1a6afSToby Isaac           ierr = indicesPoint_private(section, a, aOff, &bAnchorEnd, PETSC_TRUE, 0, newIndices);CHKERRQ(ierr);
4158d3d1a6afSToby Isaac         }
4159d3d1a6afSToby Isaac         newP += bDof;
4160d3d1a6afSToby Isaac 
4161d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
4162d3d1a6afSToby Isaac         ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
4163d3d1a6afSToby Isaac       }
4164d3d1a6afSToby Isaac       else {
4165d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4166d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4167d3d1a6afSToby Isaac         newP++;
4168d3d1a6afSToby Isaac       }
4169d3d1a6afSToby Isaac     }
4170d3d1a6afSToby Isaac   }
4171d3d1a6afSToby Isaac 
4172d3d1a6afSToby Isaac   ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
4173d3d1a6afSToby Isaac   /* multiply constraints on the right */
4174d3d1a6afSToby Isaac   if (numFields) {
4175d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4176d3d1a6afSToby Isaac       PetscInt oldOff = offsets[f];
4177d3d1a6afSToby Isaac 
4178d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4179d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[f][p];
4180d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
4181d3d1a6afSToby Isaac         PetscInt c, r, k;
4182d3d1a6afSToby Isaac         PetscInt dof;
4183d3d1a6afSToby Isaac 
4184d3d1a6afSToby Isaac         ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
4185d3d1a6afSToby Isaac         if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4186d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
4187d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
4188d3d1a6afSToby Isaac 
4189d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
4190d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
4191d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
4192d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
4193d3d1a6afSToby Isaac               }
4194d3d1a6afSToby Isaac             }
4195d3d1a6afSToby Isaac           }
4196d3d1a6afSToby Isaac         }
4197d3d1a6afSToby Isaac         else {
4198d3d1a6afSToby Isaac           /* copy this column as is */
4199d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
4200d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
4201d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4202d3d1a6afSToby Isaac             }
4203d3d1a6afSToby Isaac           }
4204d3d1a6afSToby Isaac         }
4205d3d1a6afSToby Isaac         oldOff += dof;
4206d3d1a6afSToby Isaac       }
4207d3d1a6afSToby Isaac     }
4208d3d1a6afSToby Isaac   }
4209d3d1a6afSToby Isaac   else {
4210d3d1a6afSToby Isaac     PetscInt oldOff = 0;
4211d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4212d3d1a6afSToby Isaac       PetscInt cStart = newPointOffsets[0][p];
4213d3d1a6afSToby Isaac       PetscInt b      = points[2 * p];
4214d3d1a6afSToby Isaac       PetscInt c, r, k;
4215d3d1a6afSToby Isaac       PetscInt dof;
4216d3d1a6afSToby Isaac 
4217d3d1a6afSToby Isaac       ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
4218d3d1a6afSToby Isaac       if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
4219d3d1a6afSToby Isaac         PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
4220d3d1a6afSToby Isaac         const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
4221d3d1a6afSToby Isaac 
4222d3d1a6afSToby Isaac         for (r = 0; r < numIndices; r++) {
4223d3d1a6afSToby Isaac           for (c = 0; c < nCols; c++) {
4224d3d1a6afSToby Isaac             for (k = 0; k < dof; k++) {
4225d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
4226d3d1a6afSToby Isaac             }
4227d3d1a6afSToby Isaac           }
4228d3d1a6afSToby Isaac         }
4229d3d1a6afSToby Isaac       }
4230d3d1a6afSToby Isaac       else {
4231d3d1a6afSToby Isaac         /* copy this column as is */
4232d3d1a6afSToby Isaac         for (r = 0; r < numIndices; r++) {
4233d3d1a6afSToby Isaac           for (c = 0; c < dof; c++) {
4234d3d1a6afSToby Isaac             tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4235d3d1a6afSToby Isaac           }
4236d3d1a6afSToby Isaac         }
4237d3d1a6afSToby Isaac       }
4238d3d1a6afSToby Isaac       oldOff += dof;
4239d3d1a6afSToby Isaac     }
4240d3d1a6afSToby Isaac   }
4241d3d1a6afSToby Isaac 
4242d3d1a6afSToby Isaac   ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
4243d3d1a6afSToby Isaac   /* multiply constraints transpose on the left */
4244d3d1a6afSToby Isaac   if (numFields) {
4245d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4246d3d1a6afSToby Isaac       PetscInt oldOff = offsets[f];
4247d3d1a6afSToby Isaac 
4248d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4249d3d1a6afSToby Isaac         PetscInt rStart = newPointOffsets[f][p];
4250d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
4251d3d1a6afSToby Isaac         PetscInt c, r, k;
4252d3d1a6afSToby Isaac         PetscInt dof;
4253d3d1a6afSToby Isaac 
4254d3d1a6afSToby Isaac         ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
4255d3d1a6afSToby Isaac         if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4256d3d1a6afSToby Isaac           PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
4257d3d1a6afSToby Isaac           const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
4258d3d1a6afSToby Isaac 
4259d3d1a6afSToby Isaac           for (r = 0; r < nRows; r++) {
4260d3d1a6afSToby Isaac             for (c = 0; c < newNumIndices; c++) {
4261d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
4262d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
4263d3d1a6afSToby Isaac               }
4264d3d1a6afSToby Isaac             }
4265d3d1a6afSToby Isaac           }
4266d3d1a6afSToby Isaac         }
4267d3d1a6afSToby Isaac         else {
4268d3d1a6afSToby Isaac           /* copy this row as is */
4269d3d1a6afSToby Isaac           for (r = 0; r < dof; r++) {
4270d3d1a6afSToby Isaac             for (c = 0; c < newNumIndices; c++) {
4271d3d1a6afSToby Isaac               newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
4272d3d1a6afSToby Isaac             }
4273d3d1a6afSToby Isaac           }
4274d3d1a6afSToby Isaac         }
4275d3d1a6afSToby Isaac         oldOff += dof;
4276d3d1a6afSToby Isaac       }
4277d3d1a6afSToby Isaac     }
4278d3d1a6afSToby Isaac   }
4279d3d1a6afSToby Isaac   else {
4280d3d1a6afSToby Isaac     PetscInt oldOff = 0;
4281d3d1a6afSToby Isaac 
4282d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4283d3d1a6afSToby Isaac       PetscInt rStart = newPointOffsets[0][p];
4284d3d1a6afSToby Isaac       PetscInt b      = points[2 * p];
4285d3d1a6afSToby Isaac       PetscInt c, r, k;
4286d3d1a6afSToby Isaac       PetscInt dof;
4287d3d1a6afSToby Isaac 
4288d3d1a6afSToby Isaac       ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
4289d3d1a6afSToby Isaac       if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
4290d3d1a6afSToby Isaac         PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
4291d3d1a6afSToby Isaac         const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
4292d3d1a6afSToby Isaac 
4293d3d1a6afSToby Isaac         for (r = 0; r < nRows; r++) {
4294d3d1a6afSToby Isaac           for (c = 0; c < newNumIndices; c++) {
4295d3d1a6afSToby Isaac             for (k = 0; k < dof; k++) {
4296d3d1a6afSToby Isaac               newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
4297d3d1a6afSToby Isaac             }
4298d3d1a6afSToby Isaac           }
4299d3d1a6afSToby Isaac         }
4300d3d1a6afSToby Isaac       }
4301d3d1a6afSToby Isaac       else {
4302d3d1a6afSToby Isaac         /* copy this row as is */
4303d3d1a6afSToby Isaac         for (r = 0; r < dof; c++) {
4304d3d1a6afSToby Isaac           for (c = 0; c < newNumIndices; c++) {
4305d3d1a6afSToby Isaac             newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
4306d3d1a6afSToby Isaac           }
4307d3d1a6afSToby Isaac         }
4308d3d1a6afSToby Isaac       }
4309d3d1a6afSToby Isaac       oldOff += dof;
4310d3d1a6afSToby Isaac     }
4311d3d1a6afSToby Isaac   }
4312d3d1a6afSToby Isaac 
4313d3d1a6afSToby Isaac   /* clean up */
4314d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4315d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4316d3d1a6afSToby Isaac   if (numFields) {
4317d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4318d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4319d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4320d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4321d3d1a6afSToby Isaac     }
4322d3d1a6afSToby Isaac   }
4323d3d1a6afSToby Isaac   else {
4324d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4325d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4326d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4327d3d1a6afSToby Isaac   }
4328d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
4329d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
4330d3d1a6afSToby Isaac 
4331d3d1a6afSToby Isaac   /* output */
4332d3d1a6afSToby Isaac   *outNumPoints  = newNumPoints;
4333d3d1a6afSToby Isaac   *outNumIndices = newNumIndices;
4334d3d1a6afSToby Isaac   *outPoints     = newPoints;
4335d3d1a6afSToby Isaac   *outValues     = newValues;
4336d3d1a6afSToby Isaac   for (f = 0; f < numFields; f++) {
4337d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
4338d3d1a6afSToby Isaac   }
4339d3d1a6afSToby Isaac   PetscFunctionReturn(0);
4340d3d1a6afSToby Isaac }
4341d3d1a6afSToby Isaac 
4342d3d1a6afSToby Isaac #undef __FUNCT__
4343552f7358SJed Brown #define __FUNCT__ "DMPlexMatSetClosure"
43447f5d1fdeSMatthew G. Knepley /*@C
43457f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
43467f5d1fdeSMatthew G. Knepley 
43477f5d1fdeSMatthew G. Knepley   Not collective
43487f5d1fdeSMatthew G. Knepley 
43497f5d1fdeSMatthew G. Knepley   Input Parameters:
43507f5d1fdeSMatthew G. Knepley + dm - The DM
4351ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4352ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
43537f5d1fdeSMatthew G. Knepley . A - The matrix
43547f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
43557f5d1fdeSMatthew G. Knepley . values - The array of values
43567f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
43577f5d1fdeSMatthew G. Knepley 
43587f5d1fdeSMatthew G. Knepley   Fortran Notes:
43597f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
43607f5d1fdeSMatthew G. Knepley 
43617f5d1fdeSMatthew G. Knepley   Level: intermediate
43627f5d1fdeSMatthew G. Knepley 
43637f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
43647f5d1fdeSMatthew G. Knepley @*/
43657c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
4366552f7358SJed Brown {
4367552f7358SJed Brown   DM_Plex        *mesh   = (DM_Plex*) dm->data;
43681b406b76SMatthew G. Knepley   PetscSection    clSection;
43691b406b76SMatthew G. Knepley   IS              clPoints;
4370d3d1a6afSToby Isaac   PetscInt       *points = NULL, *newPoints;
43711b406b76SMatthew G. Knepley   const PetscInt *clp;
4372552f7358SJed Brown   PetscInt       *indices;
4373552f7358SJed Brown   PetscInt        offsets[32];
4374d3d1a6afSToby Isaac   PetscInt        numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, pStart, pEnd, p, q, f;
4375d3d1a6afSToby Isaac   PetscScalar    *newValues;
4376552f7358SJed Brown   PetscErrorCode  ierr;
4377552f7358SJed Brown 
4378552f7358SJed Brown   PetscFunctionBegin;
4379552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4380ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
43813dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4382ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
43833dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
43843dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
4385552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
438682f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
4387552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
43881b406b76SMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);CHKERRQ(ierr);
43891b406b76SMatthew G. Knepley   if (!clPoints) {
4390552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
4391552f7358SJed Brown     /* Compress out points not in the section */
4392552f7358SJed Brown     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4393552f7358SJed Brown     for (p = 0, q = 0; p < numPoints*2; p += 2) {
4394552f7358SJed Brown       if ((points[p] >= pStart) && (points[p] < pEnd)) {
4395552f7358SJed Brown         points[q*2]   = points[p];
4396552f7358SJed Brown         points[q*2+1] = points[p+1];
4397552f7358SJed Brown         ++q;
4398552f7358SJed Brown       }
4399552f7358SJed Brown     }
4400552f7358SJed Brown     numPoints = q;
44011b406b76SMatthew G. Knepley   } else {
44021b406b76SMatthew G. Knepley     PetscInt dof, off;
44031b406b76SMatthew G. Knepley 
44041b406b76SMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
44051b406b76SMatthew G. Knepley     numPoints = dof/2;
44061b406b76SMatthew G. Knepley     ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
44071b406b76SMatthew G. Knepley     ierr = ISGetIndices(clPoints, &clp);CHKERRQ(ierr);
44081b406b76SMatthew G. Knepley     points = (PetscInt *) &clp[off];
44091b406b76SMatthew G. Knepley   }
4410552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
4411552f7358SJed Brown     PetscInt fdof;
4412552f7358SJed Brown 
4413552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
4414552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
4415552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4416552f7358SJed Brown       offsets[f+1] += fdof;
4417552f7358SJed Brown     }
4418552f7358SJed Brown     numIndices += dof;
4419552f7358SJed Brown   }
44200d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
44210d644c17SKarl Rupp 
442282f516ccSBarry Smith   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", offsets[numFields], numIndices);
4423f7c74593SToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets);CHKERRQ(ierr);
4424d3d1a6afSToby Isaac   if (newNumPoints) {
4425d3d1a6afSToby Isaac     if (!clPoints) {
4426d3d1a6afSToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
4427d3d1a6afSToby Isaac     } else {
4428d3d1a6afSToby Isaac       ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);
4429d3d1a6afSToby Isaac     }
4430d3d1a6afSToby Isaac     numPoints  = newNumPoints;
4431d3d1a6afSToby Isaac     numIndices = newNumIndices;
4432d3d1a6afSToby Isaac     points     = newPoints;
4433d3d1a6afSToby Isaac     values     = newValues;
4434d3d1a6afSToby Isaac   }
4435552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
4436552f7358SJed Brown   if (numFields) {
4437552f7358SJed Brown     for (p = 0; p < numPoints*2; p += 2) {
4438552f7358SJed Brown       PetscInt o = points[p+1];
4439552f7358SJed Brown       ierr = PetscSectionGetOffset(globalSection, points[p], &globalOff);CHKERRQ(ierr);
4440552f7358SJed Brown       indicesPointFields_private(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, o, indices);
4441552f7358SJed Brown     }
4442552f7358SJed Brown   } else {
4443e6ccafaeSMatthew G Knepley     for (p = 0, off = 0; p < numPoints*2; p += 2) {
4444552f7358SJed Brown       PetscInt o = points[p+1];
4445552f7358SJed Brown       ierr = PetscSectionGetOffset(globalSection, points[p], &globalOff);CHKERRQ(ierr);
4446e6ccafaeSMatthew G Knepley       indicesPoint_private(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, indices);
4447552f7358SJed Brown     }
4448552f7358SJed Brown   }
4449b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
4450552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
4451552f7358SJed Brown   if (ierr) {
4452552f7358SJed Brown     PetscMPIInt    rank;
4453552f7358SJed Brown     PetscErrorCode ierr2;
4454552f7358SJed Brown 
445582f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
445677a6746dSMatthew G Knepley     ierr2 = (*PetscErrorPrintf)("[%D]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
4457b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
44586a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
4459552f7358SJed Brown     CHKERRQ(ierr);
4460552f7358SJed Brown   }
4461d3d1a6afSToby Isaac   if (newNumPoints) {
4462d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
4463d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
4464d3d1a6afSToby Isaac   }
4465d3d1a6afSToby Isaac   else {
44661b406b76SMatthew G. Knepley     if (!clPoints) {
4467552f7358SJed Brown       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
44681b406b76SMatthew G. Knepley     } else {
44691b406b76SMatthew G. Knepley       ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);
44701b406b76SMatthew G. Knepley     }
4471d3d1a6afSToby Isaac   }
4472552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
4473552f7358SJed Brown   PetscFunctionReturn(0);
4474552f7358SJed Brown }
4475552f7358SJed Brown 
4476552f7358SJed Brown #undef __FUNCT__
4477de41b84cSMatthew G. Knepley #define __FUNCT__ "DMPlexMatSetClosureRefined"
4478de41b84cSMatthew 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)
4479de41b84cSMatthew G. Knepley {
4480de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
4481de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
4482de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
4483de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
4484de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
4485de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
44864ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
4487de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
4488de41b84cSMatthew G. Knepley 
4489de41b84cSMatthew G. Knepley   PetscFunctionBegin;
4490de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
4491de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
4492de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
4493de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
4494de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
4495de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
4496de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
4497de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
4498de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
4499de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
4500de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
4501de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
4502de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
4503de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4504de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4505de41b84cSMatthew G. Knepley   /* Column indices */
4506de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
45074ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
4508de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
4509de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
4510de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
4511de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
4512de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
4513de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
4514de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
4515de41b84cSMatthew G. Knepley       ++q;
4516de41b84cSMatthew G. Knepley     }
4517de41b84cSMatthew G. Knepley   }
4518de41b84cSMatthew G. Knepley   numCPoints = q;
4519de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
4520de41b84cSMatthew G. Knepley     PetscInt fdof;
4521de41b84cSMatthew G. Knepley 
4522de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
45234ca5e9f5SMatthew G. Knepley     if (!dof) continue;
4524de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
4525de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
4526de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
4527de41b84cSMatthew G. Knepley     }
4528de41b84cSMatthew G. Knepley     numCIndices += dof;
4529de41b84cSMatthew G. Knepley   }
4530de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
4531de41b84cSMatthew G. Knepley   /* Row indices */
4532de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
4533de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
45341ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
4535de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
4536de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
4537de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
4538de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
4539de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
4540de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
4541de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
45424ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
45434ca5e9f5SMatthew G. Knepley         if (!dof) continue;
45444ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
45454ca5e9f5SMatthew G. Knepley         if (s < q) continue;
4546de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
4547de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
4548de41b84cSMatthew G. Knepley         ++q;
4549de41b84cSMatthew G. Knepley       }
4550de41b84cSMatthew G. Knepley     }
4551de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
4552de41b84cSMatthew G. Knepley   }
4553de41b84cSMatthew G. Knepley   numFPoints = q;
4554de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
4555de41b84cSMatthew G. Knepley     PetscInt fdof;
4556de41b84cSMatthew G. Knepley 
4557de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
45584ca5e9f5SMatthew G. Knepley     if (!dof) continue;
4559de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
4560de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
4561de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
4562de41b84cSMatthew G. Knepley     }
4563de41b84cSMatthew G. Knepley     numFIndices += dof;
4564de41b84cSMatthew G. Knepley   }
4565de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
4566de41b84cSMatthew G. Knepley 
4567de41b84cSMatthew G. Knepley   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", foffsets[numFields], numFIndices);
4568de41b84cSMatthew G. Knepley   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", coffsets[numFields], numCIndices);
4569de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
4570de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
4571de41b84cSMatthew G. Knepley   if (numFields) {
4572de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
4573de41b84cSMatthew G. Knepley       PetscInt o = ftotpoints[p+1];
4574de41b84cSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);CHKERRQ(ierr);
4575de41b84cSMatthew G. Knepley       indicesPointFields_private(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, o, findices);
4576de41b84cSMatthew G. Knepley     }
4577de41b84cSMatthew G. Knepley     for (p = 0; p < numCPoints*2; p += 2) {
4578de41b84cSMatthew G. Knepley       PetscInt o = cpoints[p+1];
4579de41b84cSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);CHKERRQ(ierr);
4580de41b84cSMatthew G. Knepley       indicesPointFields_private(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, o, cindices);
4581de41b84cSMatthew G. Knepley     }
4582de41b84cSMatthew G. Knepley   } else {
4583de41b84cSMatthew G. Knepley     for (p = 0, off = 0; p < numFPoints*2; p += 2) {
4584de41b84cSMatthew G. Knepley       PetscInt o = ftotpoints[p+1];
4585de41b84cSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);CHKERRQ(ierr);
4586de41b84cSMatthew G. Knepley       indicesPoint_private(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, findices);
4587de41b84cSMatthew G. Knepley     }
4588de41b84cSMatthew G. Knepley     for (p = 0, off = 0; p < numCPoints*2; p += 2) {
4589de41b84cSMatthew G. Knepley       PetscInt o = cpoints[p+1];
4590de41b84cSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);CHKERRQ(ierr);
4591de41b84cSMatthew G. Knepley       indicesPoint_private(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, cindices);
4592de41b84cSMatthew G. Knepley     }
4593de41b84cSMatthew G. Knepley   }
4594de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
4595de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
4596de41b84cSMatthew G. Knepley   if (ierr) {
4597de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
4598de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
4599de41b84cSMatthew G. Knepley 
4600de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
4601de41b84cSMatthew G. Knepley     ierr2 = (*PetscErrorPrintf)("[%D]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
4602de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
4603de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
4604de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
4605de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
4606de41b84cSMatthew G. Knepley   }
4607549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
4608de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
4609de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
4610de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
4611de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
4612de41b84cSMatthew G. Knepley }
4613de41b84cSMatthew G. Knepley 
4614de41b84cSMatthew G. Knepley #undef __FUNCT__
46157c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexMatGetClosureIndicesRefined"
46167c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
46177c927364SMatthew G. Knepley {
46187c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
46197c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
46207c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
46217c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
46227c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
46237c927364SMatthew G. Knepley   PetscErrorCode ierr;
46247c927364SMatthew G. Knepley 
46257c927364SMatthew G. Knepley   PetscFunctionBegin;
46267c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
46277c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
46287c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
46297c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
46307c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
46317c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
46327c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
46337c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
46347c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
46357c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
46367c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
46377c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
46387c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
46397c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
46407c927364SMatthew G. Knepley   /* Column indices */
46417c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
46427c927364SMatthew G. Knepley   maxFPoints = numCPoints;
46437c927364SMatthew G. Knepley   /* Compress out points not in the section */
46447c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
46457c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
46467c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
46477c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
46487c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
46497c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
46507c927364SMatthew G. Knepley       ++q;
46517c927364SMatthew G. Knepley     }
46527c927364SMatthew G. Knepley   }
46537c927364SMatthew G. Knepley   numCPoints = q;
46547c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
46557c927364SMatthew G. Knepley     PetscInt fdof;
46567c927364SMatthew G. Knepley 
46577c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
46587c927364SMatthew G. Knepley     if (!dof) continue;
46597c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
46607c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
46617c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
46627c927364SMatthew G. Knepley     }
46637c927364SMatthew G. Knepley     numCIndices += dof;
46647c927364SMatthew G. Knepley   }
46657c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
46667c927364SMatthew G. Knepley   /* Row indices */
46677c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
46687c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
46697c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
46707c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
46717c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
46727c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
46737c927364SMatthew G. Knepley     /* Compress out points not in the section */
46747c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
46757c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
46767c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
46777c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
46787c927364SMatthew G. Knepley         if (!dof) continue;
46797c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
46807c927364SMatthew G. Knepley         if (s < q) continue;
46817c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
46827c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
46837c927364SMatthew G. Knepley         ++q;
46847c927364SMatthew G. Knepley       }
46857c927364SMatthew G. Knepley     }
46867c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
46877c927364SMatthew G. Knepley   }
46887c927364SMatthew G. Knepley   numFPoints = q;
46897c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
46907c927364SMatthew G. Knepley     PetscInt fdof;
46917c927364SMatthew G. Knepley 
46927c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
46937c927364SMatthew G. Knepley     if (!dof) continue;
46947c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
46957c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
46967c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
46977c927364SMatthew G. Knepley     }
46987c927364SMatthew G. Knepley     numFIndices += dof;
46997c927364SMatthew G. Knepley   }
47007c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
47017c927364SMatthew G. Knepley 
47027c927364SMatthew G. Knepley   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", foffsets[numFields], numFIndices);
47037c927364SMatthew G. Knepley   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", coffsets[numFields], numCIndices);
47047c927364SMatthew G. Knepley   if (numFields) {
47057c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
47067c927364SMatthew G. Knepley       PetscInt o = ftotpoints[p+1];
47077c927364SMatthew G. Knepley       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);CHKERRQ(ierr);
47087c927364SMatthew G. Knepley       indicesPointFields_private(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, o, findices);
47097c927364SMatthew G. Knepley     }
47107c927364SMatthew G. Knepley     for (p = 0; p < numCPoints*2; p += 2) {
47117c927364SMatthew G. Knepley       PetscInt o = cpoints[p+1];
47127c927364SMatthew G. Knepley       ierr = PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);CHKERRQ(ierr);
47137c927364SMatthew G. Knepley       indicesPointFields_private(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, o, cindices);
47147c927364SMatthew G. Knepley     }
47157c927364SMatthew G. Knepley   } else {
47167c927364SMatthew G. Knepley     for (p = 0, off = 0; p < numFPoints*2; p += 2) {
47177c927364SMatthew G. Knepley       PetscInt o = ftotpoints[p+1];
47187c927364SMatthew G. Knepley       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);CHKERRQ(ierr);
47197c927364SMatthew G. Knepley       indicesPoint_private(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, findices);
47207c927364SMatthew G. Knepley     }
47217c927364SMatthew G. Knepley     for (p = 0, off = 0; p < numCPoints*2; p += 2) {
47227c927364SMatthew G. Knepley       PetscInt o = cpoints[p+1];
47237c927364SMatthew G. Knepley       ierr = PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);CHKERRQ(ierr);
47247c927364SMatthew G. Knepley       indicesPoint_private(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, cindices);
47257c927364SMatthew G. Knepley     }
47267c927364SMatthew G. Knepley   }
47277c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
47287c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
47297c927364SMatthew G. Knepley   PetscFunctionReturn(0);
47307c927364SMatthew G. Knepley }
47317c927364SMatthew G. Knepley 
47327c927364SMatthew G. Knepley #undef __FUNCT__
4733770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexGetHybridBounds"
4734f96281f8SMatthew G. Knepley /*@
4735f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
4736f96281f8SMatthew G. Knepley 
4737f96281f8SMatthew G. Knepley   Input Parameter:
4738f96281f8SMatthew G. Knepley . dm - The DMPlex object
4739f96281f8SMatthew G. Knepley 
4740f96281f8SMatthew G. Knepley   Output Parameters:
4741f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
4742dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
4743dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
4744dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
4745f96281f8SMatthew G. Knepley 
4746f96281f8SMatthew G. Knepley   Level: developer
4747f96281f8SMatthew G. Knepley 
4748dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
4749f96281f8SMatthew G. Knepley @*/
4750770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
4751552f7358SJed Brown {
4752552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
4753770b213bSMatthew G Knepley   PetscInt       dim;
4754770b213bSMatthew G Knepley   PetscErrorCode ierr;
4755552f7358SJed Brown 
4756552f7358SJed Brown   PetscFunctionBegin;
4757552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4758c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4759770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
4760770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
4761770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
4762770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
4763552f7358SJed Brown   PetscFunctionReturn(0);
4764552f7358SJed Brown }
4765552f7358SJed Brown 
4766552f7358SJed Brown #undef __FUNCT__
4767770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexSetHybridBounds"
4768dc5a3409SMatthew G. Knepley /*@
4769dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
4770dc5a3409SMatthew G. Knepley 
4771dc5a3409SMatthew G. Knepley   Input Parameters:
4772dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
4773dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
4774dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
4775dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
4776dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
4777dc5a3409SMatthew G. Knepley 
4778dc5a3409SMatthew G. Knepley   Level: developer
4779dc5a3409SMatthew G. Knepley 
4780dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
4781dc5a3409SMatthew G. Knepley @*/
4782770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
4783552f7358SJed Brown {
4784552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
4785770b213bSMatthew G Knepley   PetscInt       dim;
4786770b213bSMatthew G Knepley   PetscErrorCode ierr;
4787552f7358SJed Brown 
4788552f7358SJed Brown   PetscFunctionBegin;
4789552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4790c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4791770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
4792770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
4793770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
4794770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
4795552f7358SJed Brown   PetscFunctionReturn(0);
4796552f7358SJed Brown }
4797552f7358SJed Brown 
4798552f7358SJed Brown #undef __FUNCT__
4799552f7358SJed Brown #define __FUNCT__ "DMPlexGetVTKCellHeight"
4800552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
4801552f7358SJed Brown {
4802552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4803552f7358SJed Brown 
4804552f7358SJed Brown   PetscFunctionBegin;
4805552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4806552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
4807552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
4808552f7358SJed Brown   PetscFunctionReturn(0);
4809552f7358SJed Brown }
4810552f7358SJed Brown 
4811552f7358SJed Brown #undef __FUNCT__
4812552f7358SJed Brown #define __FUNCT__ "DMPlexSetVTKCellHeight"
4813552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
4814552f7358SJed Brown {
4815552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4816552f7358SJed Brown 
4817552f7358SJed Brown   PetscFunctionBegin;
4818552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4819552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
4820552f7358SJed Brown   PetscFunctionReturn(0);
4821552f7358SJed Brown }
4822552f7358SJed Brown 
4823552f7358SJed Brown #undef __FUNCT__
4824552f7358SJed Brown #define __FUNCT__ "DMPlexCreateNumbering_Private"
4825552f7358SJed Brown /* We can easily have a form that takes an IS instead */
4826ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
4827552f7358SJed Brown {
4828552f7358SJed Brown   PetscSection   section, globalSection;
4829552f7358SJed Brown   PetscInt      *numbers, p;
4830552f7358SJed Brown   PetscErrorCode ierr;
4831552f7358SJed Brown 
4832552f7358SJed Brown   PetscFunctionBegin;
483382f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
4834552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
4835552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
4836552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
4837552f7358SJed Brown   }
4838552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
4839552f7358SJed Brown   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
4840854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
4841552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
4842552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
4843ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
4844ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
4845552f7358SJed Brown   }
484682f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
4847ef48cebcSMatthew G. Knepley   if (globalSize) {
4848ef48cebcSMatthew G. Knepley     PetscLayout layout;
4849ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
4850ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
4851ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4852ef48cebcSMatthew G. Knepley   }
4853552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
4854552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
4855552f7358SJed Brown   PetscFunctionReturn(0);
4856552f7358SJed Brown }
4857552f7358SJed Brown 
4858552f7358SJed Brown #undef __FUNCT__
4859552f7358SJed Brown #define __FUNCT__ "DMPlexGetCellNumbering"
4860552f7358SJed Brown PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
4861552f7358SJed Brown {
4862552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
4863552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
4864552f7358SJed Brown   PetscErrorCode ierr;
4865552f7358SJed Brown 
4866552f7358SJed Brown   PetscFunctionBegin;
4867552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4868552f7358SJed Brown   if (!mesh->globalCellNumbers) {
4869552f7358SJed Brown     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
4870552f7358SJed Brown     ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
48710298fd71SBarry Smith     ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
48720d644c17SKarl Rupp     if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
4873ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, &mesh->globalCellNumbers);CHKERRQ(ierr);
4874552f7358SJed Brown   }
4875552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
4876552f7358SJed Brown   PetscFunctionReturn(0);
4877552f7358SJed Brown }
4878552f7358SJed Brown 
4879552f7358SJed Brown #undef __FUNCT__
4880552f7358SJed Brown #define __FUNCT__ "DMPlexGetVertexNumbering"
4881552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
4882552f7358SJed Brown {
4883552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
4884552f7358SJed Brown   PetscInt       vStart, vEnd, vMax;
4885552f7358SJed Brown   PetscErrorCode ierr;
4886552f7358SJed Brown 
4887552f7358SJed Brown   PetscFunctionBegin;
4888552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4889552f7358SJed Brown   if (!mesh->globalVertexNumbers) {
4890552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
48910298fd71SBarry Smith     ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
48920d644c17SKarl Rupp     if (vMax >= 0) vEnd = PetscMin(vEnd, vMax);
4893ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, &mesh->globalVertexNumbers);CHKERRQ(ierr);
4894552f7358SJed Brown   }
4895552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
4896552f7358SJed Brown   PetscFunctionReturn(0);
4897552f7358SJed Brown }
4898552f7358SJed Brown 
4899ef48cebcSMatthew G. Knepley #undef __FUNCT__
4900ef48cebcSMatthew G. Knepley #define __FUNCT__ "DMPlexCreatePointNumbering"
4901ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
4902ef48cebcSMatthew G. Knepley {
4903ef48cebcSMatthew G. Knepley   IS             nums[4];
4904ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
4905ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
4906ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
4907ef48cebcSMatthew G. Knepley 
4908ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
4909ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4910ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4911ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
4912ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
4913ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
4914ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
4915ef48cebcSMatthew G. Knepley 
4916ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
4917ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
4918ef48cebcSMatthew G. Knepley     shift += gsize;
4919ef48cebcSMatthew G. Knepley   }
4920ef48cebcSMatthew G. Knepley   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);
4921ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
4922ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
4923ef48cebcSMatthew G. Knepley }
4924ef48cebcSMatthew G. Knepley 
4925552f7358SJed Brown 
4926552f7358SJed Brown #undef __FUNCT__
4927552f7358SJed Brown #define __FUNCT__ "PetscSectionCreateGlobalSectionLabel"
4928552f7358SJed Brown /*@C
4929552f7358SJed Brown   PetscSectionCreateGlobalSectionLabel - Create a section describing the global field layout using
4930552f7358SJed Brown   the local section and an SF describing the section point overlap.
4931552f7358SJed Brown 
4932552f7358SJed Brown   Input Parameters:
4933552f7358SJed Brown   + s - The PetscSection for the local field layout
4934552f7358SJed Brown   . sf - The SF describing parallel layout of the section points
4935552f7358SJed Brown   . includeConstraints - By default this is PETSC_FALSE, meaning that the global field vector will not possess constrained dofs
4936552f7358SJed Brown   . label - The label specifying the points
4937552f7358SJed Brown   - labelValue - The label stratum specifying the points
4938552f7358SJed Brown 
4939552f7358SJed Brown   Output Parameter:
4940552f7358SJed Brown   . gsection - The PetscSection for the global field layout
4941552f7358SJed Brown 
4942552f7358SJed Brown   Note: This gives negative sizes and offsets to points not owned by this process
4943552f7358SJed Brown 
4944552f7358SJed Brown   Level: developer
4945552f7358SJed Brown 
4946552f7358SJed Brown .seealso: PetscSectionCreate()
4947552f7358SJed Brown @*/
4948552f7358SJed Brown PetscErrorCode PetscSectionCreateGlobalSectionLabel(PetscSection s, PetscSF sf, PetscBool includeConstraints, DMLabel label, PetscInt labelValue, PetscSection *gsection)
4949552f7358SJed Brown {
49507ec39499SMatthew G. Knepley   PetscInt      *neg = NULL, *tmpOff = NULL;
4951552f7358SJed Brown   PetscInt       pStart, pEnd, p, dof, cdof, off, globalOff = 0, nroots;
4952552f7358SJed Brown   PetscErrorCode ierr;
4953552f7358SJed Brown 
4954552f7358SJed Brown   PetscFunctionBegin;
49554c0d72c2SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) s), gsection);CHKERRQ(ierr);
4956552f7358SJed Brown   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
4957552f7358SJed Brown   ierr = PetscSectionSetChart(*gsection, pStart, pEnd);CHKERRQ(ierr);
49587ec39499SMatthew G. Knepley   ierr = PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
49597ec39499SMatthew G. Knepley   if (nroots >= 0) {
49607ec39499SMatthew G. Knepley     if (nroots < pEnd-pStart) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %d < %d section size", nroots, pEnd-pStart);
49611795a4d1SJed Brown     ierr = PetscCalloc1(nroots, &neg);CHKERRQ(ierr);
49627ec39499SMatthew G. Knepley     if (nroots > pEnd-pStart) {
49631795a4d1SJed Brown       ierr = PetscCalloc1(nroots, &tmpOff);CHKERRQ(ierr);
49647ec39499SMatthew G. Knepley     } else {
49657ec39499SMatthew G. Knepley       tmpOff = &(*gsection)->atlasDof[-pStart];
49667ec39499SMatthew G. Knepley     }
49677ec39499SMatthew G. Knepley   }
4968552f7358SJed Brown   /* Mark ghost points with negative dof */
4969552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
4970552f7358SJed Brown     PetscInt value;
4971552f7358SJed Brown 
4972552f7358SJed Brown     ierr = DMLabelGetValue(label, p, &value);CHKERRQ(ierr);
4973552f7358SJed Brown     if (value != labelValue) continue;
4974552f7358SJed Brown     ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
4975552f7358SJed Brown     ierr = PetscSectionSetDof(*gsection, p, dof);CHKERRQ(ierr);
4976552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
4977552f7358SJed Brown     if (!includeConstraints && cdof > 0) {ierr = PetscSectionSetConstraintDof(*gsection, p, cdof);CHKERRQ(ierr);}
49787ec39499SMatthew G. Knepley     if (neg) neg[p] = -(dof+1);
4979552f7358SJed Brown   }
4980552f7358SJed Brown   ierr = PetscSectionSetUpBC(*gsection);CHKERRQ(ierr);
4981552f7358SJed Brown   if (nroots >= 0) {
49827ec39499SMatthew G. Knepley     ierr = PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
49837ec39499SMatthew G. Knepley     ierr = PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
4984552f7358SJed Brown     if (nroots > pEnd-pStart) {
49857ec39499SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasDof[p-pStart] = tmpOff[p];}
4986552f7358SJed Brown     }
4987552f7358SJed Brown   }
4988552f7358SJed Brown   /* Calculate new sizes, get proccess offset, and calculate point offsets */
4989552f7358SJed Brown   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
4990552f7358SJed Brown     cdof = (!includeConstraints && s->bc) ? s->bc->atlasDof[p] : 0;
4991552f7358SJed Brown     (*gsection)->atlasOff[p] = off;
4992552f7358SJed Brown     off += (*gsection)->atlasDof[p] > 0 ? (*gsection)->atlasDof[p]-cdof : 0;
4993552f7358SJed Brown   }
49944c0d72c2SMatthew G. Knepley   ierr       = MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject) s));CHKERRQ(ierr);
4995552f7358SJed Brown   globalOff -= off;
4996552f7358SJed Brown   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
4997552f7358SJed Brown     (*gsection)->atlasOff[p] += globalOff;
49987ec39499SMatthew G. Knepley     if (neg) neg[p] = -((*gsection)->atlasOff[p]+1);
4999552f7358SJed Brown   }
5000552f7358SJed Brown   /* Put in negative offsets for ghost points */
5001552f7358SJed Brown   if (nroots >= 0) {
50027ec39499SMatthew G. Knepley     ierr = PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
50037ec39499SMatthew G. Knepley     ierr = PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
5004552f7358SJed Brown     if (nroots > pEnd-pStart) {
50057ec39499SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasOff[p-pStart] = tmpOff[p];}
5006552f7358SJed Brown     }
5007552f7358SJed Brown   }
50087ec39499SMatthew G. Knepley   if (nroots >= 0 && nroots > pEnd-pStart) {ierr = PetscFree(tmpOff);CHKERRQ(ierr);}
5009552f7358SJed Brown   ierr = PetscFree(neg);CHKERRQ(ierr);
5010552f7358SJed Brown   PetscFunctionReturn(0);
5011552f7358SJed Brown }
5012ca8062c8SMatthew G. Knepley 
5013ca8062c8SMatthew G. Knepley #undef __FUNCT__
5014ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSymmetry"
5015ca8062c8SMatthew G. Knepley /*@
5016ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
5017ca8062c8SMatthew G. Knepley 
5018ca8062c8SMatthew G. Knepley   Input Parameters:
5019ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
5020ca8062c8SMatthew G. Knepley 
5021ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5022ca8062c8SMatthew G. Knepley 
5023ca8062c8SMatthew G. Knepley   Level: developer
5024ca8062c8SMatthew G. Knepley 
50259bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
5026ca8062c8SMatthew G. Knepley @*/
5027ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
5028ca8062c8SMatthew G. Knepley {
5029ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
5030ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
5031ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
5032ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
5033ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
5034ca8062c8SMatthew G. Knepley 
5035ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5036ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5037ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
5038ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
5039ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
5040ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
5041ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
5042ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
5043ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
5044ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
504542e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
504642e66dfaSMatthew G. Knepley       PetscInt  d;
504742e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
504842e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
504942e66dfaSMatthew G. Knepley       }
5050ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
5051ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
5052ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
5053ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
5054ca8062c8SMatthew G. Knepley       }
505542e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
5056ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d cone: ", p);
5057ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
5058ca8062c8SMatthew G. Knepley           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", cone[s]);
5059ca8062c8SMatthew G. Knepley         }
5060ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");
5061ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d support: ", cone[c]);
5062ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
5063ca8062c8SMatthew G. Knepley           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", support[s]);
5064ca8062c8SMatthew G. Knepley         }
5065ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");
506642e66dfaSMatthew G. Knepley         if (dup) {
506742e66dfaSMatthew G. Knepley           SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not repeatedly found in support of repeated cone point %d", p, cone[c]);
506842e66dfaSMatthew G. Knepley         } else {
5069ca8062c8SMatthew G. Knepley           SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not found in support of cone point %d", p, cone[c]);
5070ca8062c8SMatthew G. Knepley         }
5071ca8062c8SMatthew G. Knepley       }
507242e66dfaSMatthew G. Knepley     }
5073ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
5074ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
5075ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
5076ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
5077ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
5078ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
5079ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
5080ca8062c8SMatthew G. Knepley       }
5081ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
5082ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d support: ", p);
5083ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
5084ca8062c8SMatthew G. Knepley           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", support[c]);
5085ca8062c8SMatthew G. Knepley         }
5086ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");
5087ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d cone: ", support[s]);
5088ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
5089ca8062c8SMatthew G. Knepley           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", cone[c]);
5090ca8062c8SMatthew G. Knepley         }
5091ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");
5092ca8062c8SMatthew G. Knepley         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not found in cone of support point %d", p, support[s]);
5093ca8062c8SMatthew G. Knepley       }
5094ca8062c8SMatthew G. Knepley     }
5095ca8062c8SMatthew G. Knepley   }
5096ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
5097ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
5098ca8062c8SMatthew G. Knepley   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %d != Total support size %d", csize, ssize);
5099ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
5100ca8062c8SMatthew G. Knepley }
5101ca8062c8SMatthew G. Knepley 
5102ca8062c8SMatthew G. Knepley #undef __FUNCT__
5103ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSkeleton"
5104ca8062c8SMatthew G. Knepley /*@
5105ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
5106ca8062c8SMatthew G. Knepley 
5107ca8062c8SMatthew G. Knepley   Input Parameters:
5108ca8062c8SMatthew G. Knepley + dm - The DMPlex object
510958723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
511058723a97SMatthew G. Knepley - cellHeight - Normally 0
5111ca8062c8SMatthew G. Knepley 
5112ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5113ca8062c8SMatthew G. Knepley 
5114ca8062c8SMatthew G. Knepley   Level: developer
5115ca8062c8SMatthew G. Knepley 
51169bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
5117ca8062c8SMatthew G. Knepley @*/
511858723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
5119ca8062c8SMatthew G. Knepley {
512042363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
5121ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
5122ca8062c8SMatthew G. Knepley 
5123ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5124ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5125c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5126ca8062c8SMatthew G. Knepley   switch (dim) {
512742363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
512842363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
512942363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
5130ca8062c8SMatthew G. Knepley   default:
5131ca8062c8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %d", dim);
5132ca8062c8SMatthew G. Knepley   }
513358723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
513458723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
5135ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
5136ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
5137ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
513858723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
513958723a97SMatthew G. Knepley 
514058723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
514158723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
514258723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
514358723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
514458723a97SMatthew G. Knepley     }
514558723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
5146ca8062c8SMatthew G. Knepley     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %d has  %d vertices != %d", c, coneSize, numCorners);
5147ca8062c8SMatthew G. Knepley   }
514842363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
514942363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
515042363296SMatthew G. Knepley 
515142363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
515242363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
515342363296SMatthew G. Knepley       const PetscInt p = closure[cl];
515442363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
515542363296SMatthew G. Knepley     }
515642363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
515742363296SMatthew G. Knepley     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %d has  %d vertices > %d", c, coneSize, numHybridCorners);
515842363296SMatthew G. Knepley   }
5159ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
5160ca8062c8SMatthew G. Knepley }
51619bf0dad6SMatthew G. Knepley 
51629bf0dad6SMatthew G. Knepley #undef __FUNCT__
51639bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckFaces"
51649bf0dad6SMatthew G. Knepley /*@
51659bf0dad6SMatthew 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
51669bf0dad6SMatthew G. Knepley 
51679bf0dad6SMatthew G. Knepley   Input Parameters:
51689bf0dad6SMatthew G. Knepley + dm - The DMPlex object
51699bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
51709bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
51719bf0dad6SMatthew G. Knepley 
51729bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
51739bf0dad6SMatthew G. Knepley 
51749bf0dad6SMatthew G. Knepley   Level: developer
51759bf0dad6SMatthew G. Knepley 
51769bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
51779bf0dad6SMatthew G. Knepley @*/
51789bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
51799bf0dad6SMatthew G. Knepley {
51803554e41dSMatthew G. Knepley   PetscInt       pMax[4];
51813554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
51829bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
51839bf0dad6SMatthew G. Knepley 
51849bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
51859bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5186c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
51879bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
518825abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
51893554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
51903554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
51913554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
51929bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
51939bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
51949bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
51959bf0dad6SMatthew G. Knepley 
519625abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
51979bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
51989bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
51999bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
52009bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
52019bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
52029bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
52039bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
52049bf0dad6SMatthew G. Knepley       }
52053554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
52069bf0dad6SMatthew G. Knepley       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %d has %d faces but should have %d", c, coneSize, numFaces);
52079bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
52089bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
52099bf0dad6SMatthew G. Knepley 
52109bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
52119bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
52129bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
52139bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
52149bf0dad6SMatthew G. Knepley         }
5215e003ed69SMatthew G. Knepley         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);
52169bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
5217e003ed69SMatthew G. Knepley           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]);
52189bf0dad6SMatthew G. Knepley         }
52199bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
52209bf0dad6SMatthew G. Knepley       }
52219bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
52229bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
52239bf0dad6SMatthew G. Knepley     }
52243554e41dSMatthew G. Knepley   }
5225552f7358SJed Brown   PetscFunctionReturn(0);
5226552f7358SJed Brown }
52273913d7c8SMatthew G. Knepley 
52283913d7c8SMatthew G. Knepley #undef __FUNCT__
5229bceba477SMatthew G. Knepley #define __FUNCT__ "DMCreateInterpolation_Plex"
5230bceba477SMatthew G. Knepley /* Pointwise interpolation
5231bceba477SMatthew G. Knepley      Just code FEM for now
5232bceba477SMatthew G. Knepley      u^f = I u^c
52334ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
52344ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
52354ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
5236bceba477SMatthew G. Knepley */
5237bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
5238bceba477SMatthew G. Knepley {
5239bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
5240bceba477SMatthew G. Knepley   PetscInt       m, n;
5241a063dac3SMatthew G. Knepley   void          *ctx;
5242bceba477SMatthew G. Knepley   PetscErrorCode ierr;
5243bceba477SMatthew G. Knepley 
5244bceba477SMatthew G. Knepley   PetscFunctionBegin;
5245bceba477SMatthew G. Knepley   /*
5246bceba477SMatthew G. Knepley   Loop over coarse cells
5247bceba477SMatthew G. Knepley     Loop over coarse basis functions
5248bceba477SMatthew G. Knepley       Loop over fine cells in coarse cell
5249bceba477SMatthew G. Knepley         Loop over fine dual basis functions
5250bceba477SMatthew G. Knepley           Evaluate coarse basis on fine dual basis quad points
5251bceba477SMatthew G. Knepley           Sum
5252bceba477SMatthew G. Knepley           Update local element matrix
5253bceba477SMatthew G. Knepley     Accumulate to interpolation matrix
5254bceba477SMatthew G. Knepley 
5255bceba477SMatthew G. Knepley    Can extend PetscFEIntegrateJacobian_Basic() to do a specialized cell loop
5256bceba477SMatthew G. Knepley   */
5257bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
5258bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
5259bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
5260bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
5261bceba477SMatthew G. Knepley   /* We need to preallocate properly */
5262bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
5263bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
5264bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
5265a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
5266a063dac3SMatthew G. Knepley   ierr = DMPlexComputeInterpolatorFEM(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);
52675d1c2e58SMatthew G. Knepley   /* Use naive scaling */
52685d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
5269a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
5270a063dac3SMatthew G. Knepley }
5271bceba477SMatthew G. Knepley 
5272a063dac3SMatthew G. Knepley #undef __FUNCT__
5273a063dac3SMatthew G. Knepley #define __FUNCT__ "DMCreateInjection_Plex"
5274a063dac3SMatthew G. Knepley PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, VecScatter *ctx)
5275a063dac3SMatthew G. Knepley {
527690748bafSMatthew G. Knepley   PetscErrorCode ierr;
527790748bafSMatthew G. Knepley 
5278a063dac3SMatthew G. Knepley   PetscFunctionBegin;
52797c927364SMatthew G. Knepley   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, ctx, NULL);CHKERRQ(ierr);
5280bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
5281bceba477SMatthew G. Knepley }
5282bceba477SMatthew G. Knepley 
5283bceba477SMatthew G. Knepley #undef __FUNCT__
5284fd59a867SMatthew G. Knepley #define __FUNCT__ "DMCreateDefaultSection_Plex"
5285fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
5286fd59a867SMatthew G. Knepley {
5287fd59a867SMatthew G. Knepley   PetscSection   section;
5288fd59a867SMatthew G. Knepley   IS            *bcPoints;
5289ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
5290286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
5291ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
5292ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
5293fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
5294fd59a867SMatthew G. Knepley 
5295fd59a867SMatthew G. Knepley   PetscFunctionBegin;
5296ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
5297ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
5298ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
5299ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
5300ebb3236fSMatthew G. Knepley     PetscObject  obj;
5301ebb3236fSMatthew G. Knepley     PetscClassId id;
5302ebb3236fSMatthew G. Knepley 
5303ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
5304ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5305ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
5306ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
5307ebb3236fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
5308ebb3236fSMatthew G. Knepley   }
5309ebb3236fSMatthew G. Knepley   /* Allocate boundary point storage */
5310286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
5311c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5312ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5313ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
5314fd59a867SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
5315fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
5316fd59a867SMatthew G. Knepley     PetscBool isEssential;
531763d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5318fd59a867SMatthew G. Knepley     if (isEssential) ++numBC;
5319fd59a867SMatthew G. Knepley   }
5320ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
5321fd59a867SMatthew G. Knepley   ierr = PetscMalloc2(numBC,&bcFields,numBC,&bcPoints);CHKERRQ(ierr);
5322ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
5323ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
5324ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
5325ebb3236fSMatthew G. Knepley 
5326ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
5327ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
5328ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
5329ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
5330ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
5331ebb3236fSMatthew G. Knepley   }
5332ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
5333ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
5334fd59a867SMatthew G. Knepley     const char     *bdLabel;
5335fd59a867SMatthew G. Knepley     DMLabel         label;
5336fd59a867SMatthew G. Knepley     const PetscInt *values;
53377391c1cbSMatthew G. Knepley     PetscInt        bd2, field, numValues;
5338c945be8dSMatthew G. Knepley     PetscBool       isEssential, duplicate = PETSC_FALSE;
5339fd59a867SMatthew G. Knepley 
534063d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
5341fd59a867SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
5342ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
53437391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
53447391c1cbSMatthew G. Knepley       const char *bdname;
53452f81efd2SMatthew G. Knepley       ierr = DMPlexGetBoundary(dm, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
53467391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
53477391c1cbSMatthew G. Knepley       if (duplicate) break;
53487391c1cbSMatthew G. Knepley     }
5349ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
5350fd59a867SMatthew G. Knepley       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
535132720fa8SMatthew G. Knepley       ierr = DMPlexLabelAddCells(dm, label);CHKERRQ(ierr);
53527391c1cbSMatthew G. Knepley     }
5353ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
5354fd59a867SMatthew G. Knepley     if (isEssential) {
535593a5981aSMatthew G. Knepley       PetscInt       *newidx;
5356ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
535793a5981aSMatthew G. Knepley 
5358fd59a867SMatthew G. Knepley       bcFields[bc] = field;
5359ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
5360ebb3236fSMatthew G. Knepley         IS              tmp;
5361ebb3236fSMatthew G. Knepley         const PetscInt *idx;
5362ebb3236fSMatthew G. Knepley 
5363ebb3236fSMatthew G. Knepley         ierr = DMPlexGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
5364ebb3236fSMatthew G. Knepley         if (!tmp) continue;
536593a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
536693a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
5367ebb3236fSMatthew G. Knepley         if (isFE[field]) {
536893a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
5369ebb3236fSMatthew G. Knepley         } else {
5370ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
5371ebb3236fSMatthew G. Knepley         }
537293a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
537393a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
5374fd59a867SMatthew G. Knepley       }
5375ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
5376ebb3236fSMatthew G. Knepley       newn = 0;
5377ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
5378ebb3236fSMatthew G. Knepley         IS              tmp;
5379ebb3236fSMatthew G. Knepley         const PetscInt *idx;
5380ebb3236fSMatthew G. Knepley 
5381ebb3236fSMatthew G. Knepley         ierr = DMPlexGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
5382ebb3236fSMatthew G. Knepley         if (!tmp) continue;
5383ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
5384ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
5385ebb3236fSMatthew G. Knepley         if (isFE[field]) {
5386ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
5387ebb3236fSMatthew G. Knepley         } else {
5388ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
5389ebb3236fSMatthew G. Knepley         }
5390ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
5391ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
5392ebb3236fSMatthew G. Knepley       }
5393ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
5394ebb3236fSMatthew G. Knepley     }
5395fd59a867SMatthew G. Knepley   }
5396fd59a867SMatthew G. Knepley   /* Handle discretization */
5397ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
5398fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
53990bacfa0aSMatthew G. Knepley     PetscObject obj;
54000bacfa0aSMatthew G. Knepley 
54010bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
5402ebb3236fSMatthew G. Knepley     if (isFE[f]) {
54030bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
5404286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
5405fd59a867SMatthew G. Knepley       PetscInt        d;
5406fd59a867SMatthew G. Knepley 
5407fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
5408286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
5409286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
5410ebb3236fSMatthew G. Knepley     } else {
54110bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
54120bacfa0aSMatthew G. Knepley 
54130bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
54140bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
5415ebb3236fSMatthew G. Knepley     }
5416fd59a867SMatthew G. Knepley   }
5417286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
5418286d8613SMatthew G. Knepley     PetscInt d;
5419286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
5420286d8613SMatthew 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.");
5421286d8613SMatthew G. Knepley     }
5422286d8613SMatthew G. Knepley   }
5423f8f126e8SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcPoints, NULL, &section);CHKERRQ(ierr);
5424fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
5425fd59a867SMatthew G. Knepley     PetscFE     fe;
5426fd59a867SMatthew G. Knepley     const char *name;
542718abd763SMatthew G. Knepley 
542818abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
5429fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
5430fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
5431fd59a867SMatthew G. Knepley   }
5432fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
5433fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5434fd59a867SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);}
5435fd59a867SMatthew G. Knepley   ierr = PetscFree2(bcFields,bcPoints);CHKERRQ(ierr);
5436286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
5437ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
5438bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
5439bceba477SMatthew G. Knepley }
5440bceba477SMatthew G. Knepley 
5441bceba477SMatthew G. Knepley #undef __FUNCT__
54423913d7c8SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCoarseDM"
54433913d7c8SMatthew G. Knepley /*@
54443913d7c8SMatthew G. Knepley   DMPlexGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
54453913d7c8SMatthew G. Knepley 
54463913d7c8SMatthew G. Knepley   Input Parameter:
54473913d7c8SMatthew G. Knepley . dm - The DMPlex object
54483913d7c8SMatthew G. Knepley 
54493913d7c8SMatthew G. Knepley   Output Parameter:
54503913d7c8SMatthew G. Knepley . cdm - The coarse DM
54513913d7c8SMatthew G. Knepley 
54523913d7c8SMatthew G. Knepley   Level: intermediate
54533913d7c8SMatthew G. Knepley 
54543913d7c8SMatthew G. Knepley .seealso: DMPlexSetCoarseDM()
54553913d7c8SMatthew G. Knepley @*/
54563913d7c8SMatthew G. Knepley PetscErrorCode DMPlexGetCoarseDM(DM dm, DM *cdm)
54573913d7c8SMatthew G. Knepley {
54583913d7c8SMatthew G. Knepley   PetscFunctionBegin;
54593913d7c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54603913d7c8SMatthew G. Knepley   PetscValidPointer(cdm, 2);
54613913d7c8SMatthew G. Knepley   *cdm = ((DM_Plex *) dm->data)->coarseMesh;
54623913d7c8SMatthew G. Knepley   PetscFunctionReturn(0);
54633913d7c8SMatthew G. Knepley }
54643913d7c8SMatthew G. Knepley 
54653913d7c8SMatthew G. Knepley #undef __FUNCT__
54663913d7c8SMatthew G. Knepley #define __FUNCT__ "DMPlexSetCoarseDM"
54673913d7c8SMatthew G. Knepley /*@
54683913d7c8SMatthew G. Knepley   DMPlexSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
54693913d7c8SMatthew G. Knepley 
54703913d7c8SMatthew G. Knepley   Input Parameters:
54713913d7c8SMatthew G. Knepley + dm - The DMPlex object
54723913d7c8SMatthew G. Knepley - cdm - The coarse DM
54733913d7c8SMatthew G. Knepley 
54743913d7c8SMatthew G. Knepley   Level: intermediate
54753913d7c8SMatthew G. Knepley 
54763913d7c8SMatthew G. Knepley .seealso: DMPlexGetCoarseDM()
54773913d7c8SMatthew G. Knepley @*/
54783913d7c8SMatthew G. Knepley PetscErrorCode DMPlexSetCoarseDM(DM dm, DM cdm)
54793913d7c8SMatthew G. Knepley {
54803913d7c8SMatthew G. Knepley   DM_Plex       *mesh;
54813913d7c8SMatthew G. Knepley   PetscErrorCode ierr;
54823913d7c8SMatthew G. Knepley 
54833913d7c8SMatthew G. Knepley   PetscFunctionBegin;
54843913d7c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54853913d7c8SMatthew G. Knepley   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
54863913d7c8SMatthew G. Knepley   mesh = (DM_Plex *) dm->data;
54873913d7c8SMatthew G. Knepley   ierr = DMDestroy(&mesh->coarseMesh);CHKERRQ(ierr);
54883913d7c8SMatthew G. Knepley   mesh->coarseMesh = cdm;
54893913d7c8SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) mesh->coarseMesh);CHKERRQ(ierr);
54903913d7c8SMatthew G. Knepley   PetscFunctionReturn(0);
54913913d7c8SMatthew G. Knepley }
5492a68b90caSToby Isaac 
5493f7c74593SToby Isaac /* anchors */
5494a68b90caSToby Isaac #undef __FUNCT__
5495a17985deSToby Isaac #define __FUNCT__ "DMPlexGetAnchors"
5496a68b90caSToby Isaac /*@
5497f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
5498f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
5499a68b90caSToby Isaac 
5500e228b242SToby Isaac   not collective
5501a68b90caSToby Isaac 
5502a68b90caSToby Isaac   Input Parameters:
5503a68b90caSToby Isaac . dm - The DMPlex object
5504a68b90caSToby Isaac 
5505a68b90caSToby Isaac   Output Parameters:
5506a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
5507a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
5508a68b90caSToby Isaac 
5509a68b90caSToby Isaac 
5510a68b90caSToby Isaac   Level: intermediate
5511a68b90caSToby Isaac 
5512f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
5513a68b90caSToby Isaac @*/
5514a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
5515a68b90caSToby Isaac {
5516a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
551741e6d900SToby Isaac   PetscErrorCode ierr;
5518a68b90caSToby Isaac 
5519a68b90caSToby Isaac   PetscFunctionBegin;
5520a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
552141e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
5522a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
5523a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
5524a68b90caSToby Isaac   PetscFunctionReturn(0);
5525a68b90caSToby Isaac }
5526a68b90caSToby Isaac 
5527a68b90caSToby Isaac #undef __FUNCT__
5528a17985deSToby Isaac #define __FUNCT__ "DMPlexSetAnchors"
5529a68b90caSToby Isaac /*@
5530f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
5531f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
5532a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
5533a68b90caSToby Isaac 
5534a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
5535f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
5536a68b90caSToby Isaac 
5537e228b242SToby Isaac   collective on dm
5538a68b90caSToby Isaac 
5539a68b90caSToby Isaac   Input Parameters:
5540a68b90caSToby Isaac + dm - The DMPlex object
5541e228b242SToby 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).
5542e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
5543a68b90caSToby Isaac 
5544a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
5545a68b90caSToby Isaac 
5546a68b90caSToby Isaac   Level: intermediate
5547a68b90caSToby Isaac 
5548f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
5549a68b90caSToby Isaac @*/
5550a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
5551a68b90caSToby Isaac {
5552a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
5553e228b242SToby Isaac   PetscMPIInt result;
5554a68b90caSToby Isaac   PetscErrorCode ierr;
5555a68b90caSToby Isaac 
5556a68b90caSToby Isaac   PetscFunctionBegin;
5557a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5558e228b242SToby Isaac   if (anchorSection) {
5559e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
5560e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
5561e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
5562e228b242SToby Isaac   }
5563e228b242SToby Isaac   if (anchorIS) {
5564e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
5565e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
5566e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
5567e228b242SToby Isaac   }
5568a68b90caSToby Isaac 
5569a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
5570a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
5571a68b90caSToby Isaac   plex->anchorSection = anchorSection;
5572a68b90caSToby Isaac 
5573a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
5574a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
5575a68b90caSToby Isaac   plex->anchorIS = anchorIS;
5576a68b90caSToby Isaac 
5577a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
5578a68b90caSToby Isaac   if (anchorIS && anchorSection) {
5579a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
5580a68b90caSToby Isaac     const PetscInt *anchors;
5581a68b90caSToby Isaac 
5582a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
5583a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
5584a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
5585a68b90caSToby Isaac     for (a = 0; a < size; a++) {
5586a68b90caSToby Isaac       PetscInt p;
5587a68b90caSToby Isaac 
5588a68b90caSToby Isaac       p = anchors[a];
5589a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
5590a68b90caSToby Isaac         PetscInt dof;
5591a68b90caSToby Isaac 
5592a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
5593a68b90caSToby Isaac         if (dof) {
5594a68b90caSToby Isaac           PetscErrorCode ierr2;
5595a68b90caSToby Isaac 
5596a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
5597a68b90caSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %d cannot be constrained and an anchor",p);
5598a68b90caSToby Isaac         }
5599a68b90caSToby Isaac       }
5600a68b90caSToby Isaac     }
5601a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
5602a68b90caSToby Isaac   }
5603a68b90caSToby Isaac #endif
5604f7c74593SToby Isaac   /* reset the generic constraints */
5605f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
5606a68b90caSToby Isaac   PetscFunctionReturn(0);
5607a68b90caSToby Isaac }
5608a68b90caSToby Isaac 
5609a68b90caSToby Isaac #undef __FUNCT__
5610f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintSection_Anchors"
5611f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
5612a68b90caSToby Isaac {
5613f7c74593SToby Isaac   PetscSection anchorSection;
5614a68b90caSToby Isaac   PetscInt pStart, pEnd, p, dof, numFields, f;
5615a68b90caSToby Isaac   PetscErrorCode ierr;
5616a68b90caSToby Isaac 
5617a68b90caSToby Isaac   PetscFunctionBegin;
5618a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5619a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
5620e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
5621a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
5622a68b90caSToby Isaac   ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
5623a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
5624a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
5625a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
5626a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
5627a68b90caSToby Isaac     if (dof) {
5628a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
5629a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
5630a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
5631a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
5632a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
5633a68b90caSToby Isaac       }
5634a68b90caSToby Isaac     }
5635a68b90caSToby Isaac   }
5636a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
5637a68b90caSToby Isaac   PetscFunctionReturn(0);
5638a68b90caSToby Isaac }
5639a68b90caSToby Isaac 
5640a68b90caSToby Isaac #undef __FUNCT__
5641f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintMatrix_Anchors"
5642f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
5643a68b90caSToby Isaac {
5644f7c74593SToby Isaac   PetscSection aSec;
56450ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
56460ac89760SToby Isaac   const PetscInt *anchors;
56470ac89760SToby Isaac   PetscInt numFields, f;
564866ad2231SToby Isaac   IS aIS;
56490ac89760SToby Isaac   PetscErrorCode ierr;
56500ac89760SToby Isaac 
56510ac89760SToby Isaac   PetscFunctionBegin;
56520ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56530ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
56540ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
56550ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
56560ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
56570ac89760SToby Isaac   ierr = MatSetType(*cMat,MATSEQAIJ);
5658a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
565966ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
56600ac89760SToby Isaac   ierr = PetscSectionGetChart(aSec,&pStart,&pEnd);CHKERRQ(ierr);
56610ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
56620ac89760SToby Isaac   i[0] = 0;
56630ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
56640ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
56650ac89760SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&dof);CHKERRQ(ierr);
56660ac89760SToby Isaac     if (!dof) continue;
56670ac89760SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&off);CHKERRQ(ierr);
56680ac89760SToby Isaac     if (numFields) {
56690ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
56700ac89760SToby Isaac         annz = 0;
56710ac89760SToby Isaac         for (q = 0; q < dof; q++) {
56720ac89760SToby Isaac           a = anchors[off + q];
56730ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
56740ac89760SToby Isaac           annz += aDof;
56750ac89760SToby Isaac         }
56760ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
56770ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
56780ac89760SToby Isaac         for (q = 0; q < dof; q++) {
56790ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
56800ac89760SToby Isaac         }
56810ac89760SToby Isaac       }
56820ac89760SToby Isaac     }
56830ac89760SToby Isaac     else {
56840ac89760SToby Isaac       annz = 0;
56850ac89760SToby Isaac       for (q = 0; q < dof; q++) {
56860ac89760SToby Isaac         a = anchors[off + q];
56870ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
56880ac89760SToby Isaac         annz += aDof;
56890ac89760SToby Isaac       }
56900ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
56910ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
56920ac89760SToby Isaac       for (q = 0; q < dof; q++) {
56930ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
56940ac89760SToby Isaac       }
56950ac89760SToby Isaac     }
56960ac89760SToby Isaac   }
56970ac89760SToby Isaac   nnz = i[m];
56980ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
56990ac89760SToby Isaac   offset = 0;
57000ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
57010ac89760SToby Isaac     if (numFields) {
57020ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
57030ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
57040ac89760SToby Isaac         for (q = 0; q < dof; q++) {
57050ac89760SToby Isaac           PetscInt rDof, rOff, r;
57060ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
57070ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
57080ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
57090ac89760SToby Isaac             PetscInt s;
57100ac89760SToby Isaac 
57110ac89760SToby Isaac             a = anchors[rOff + r];
57120ac89760SToby Isaac 
57130ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
57140ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
57150ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
57160ac89760SToby Isaac               j[offset++] = aOff + s;
57170ac89760SToby Isaac             }
57180ac89760SToby Isaac           }
57190ac89760SToby Isaac         }
57200ac89760SToby Isaac       }
57210ac89760SToby Isaac     }
57220ac89760SToby Isaac     else {
57230ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
57240ac89760SToby Isaac       for (q = 0; q < dof; q++) {
57250ac89760SToby Isaac         PetscInt rDof, rOff, r;
57260ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
57270ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
57280ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
57290ac89760SToby Isaac           PetscInt s;
57300ac89760SToby Isaac 
57310ac89760SToby Isaac           a = anchors[rOff + r];
57320ac89760SToby Isaac 
57330ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
57340ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
57350ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
57360ac89760SToby Isaac             j[offset++] = aOff + s;
57370ac89760SToby Isaac           }
57380ac89760SToby Isaac         }
57390ac89760SToby Isaac       }
57400ac89760SToby Isaac     }
57410ac89760SToby Isaac   }
57420ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
57430ac89760SToby Isaac   ierr = PetscFree2(i,j);CHKERRQ(ierr);
574466ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
57450ac89760SToby Isaac   PetscFunctionReturn(0);
57460ac89760SToby Isaac }
57470ac89760SToby Isaac 
57480ac89760SToby Isaac #undef __FUNCT__
574966ad2231SToby Isaac #define __FUNCT__ "DMCreateDefaultConstraints_Plex"
575066ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
575166ad2231SToby Isaac {
5752f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
5753f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
575466ad2231SToby Isaac   Mat            cMat;
575566ad2231SToby Isaac   PetscErrorCode ierr;
575666ad2231SToby Isaac 
575766ad2231SToby Isaac   PetscFunctionBegin;
575866ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5759a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
576066ad2231SToby Isaac   if (anchorSection) {
5761e228b242SToby Isaac     PetscDS  ds;
5762e228b242SToby Isaac     PetscInt nf;
5763e228b242SToby Isaac 
5764f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
5765f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
5766f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
5767e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
5768e228b242SToby Isaac     ierr = PetscDSGetNumFields(ds,&nf);
5769e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
577066ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
577166ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
577266ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
577366ad2231SToby Isaac   }
577466ad2231SToby Isaac   PetscFunctionReturn(0);
577566ad2231SToby Isaac }
5776