xref: /petsc/src/dm/impls/plex/plex.c (revision 1795a4d16c893ec2fc06bbbc6c5ce592a2de75d4)
134541f0dSBarry Smith #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2552f7358SJed Brown #include <../src/sys/utils/hash.h>
3d53a3d6fSBarry Smith #include <petsc-private/isimpl.h>
40c312b8eSJed Brown #include <petscsf.h>
5552f7358SJed Brown 
6552f7358SJed Brown /* Logging support */
71337e6e5SMatthew G. Knepley PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Stratify, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM;
8552f7358SJed Brown 
95a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_Seq(Vec, PetscViewer);
105a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
11552f7358SJed Brown 
12552f7358SJed Brown #undef __FUNCT__
13552f7358SJed Brown #define __FUNCT__ "VecView_Plex_Local"
14552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
15552f7358SJed Brown {
16552f7358SJed Brown   DM             dm;
17552f7358SJed Brown   PetscBool      isvtk;
18552f7358SJed Brown   PetscErrorCode ierr;
19552f7358SJed Brown 
20552f7358SJed Brown   PetscFunctionBegin;
21552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
2282f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
23552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK, &isvtk);CHKERRQ(ierr);
24552f7358SJed Brown   if (isvtk) {
25552f7358SJed Brown     PetscViewerVTKFieldType ft = PETSC_VTK_POINT_FIELD;
26552f7358SJed Brown     PetscSection            section;
27552f7358SJed Brown     PetscInt                dim, pStart, pEnd, cStart, fStart, vStart, cdof = 0, fdof = 0, vdof = 0;
28552f7358SJed Brown 
29552f7358SJed Brown     ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
30552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
310298fd71SBarry Smith     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
320298fd71SBarry Smith     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, NULL);CHKERRQ(ierr);
330298fd71SBarry Smith     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, NULL);CHKERRQ(ierr);
34552f7358SJed Brown     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
35552f7358SJed Brown     /* Assumes that numer of dofs per point of each stratum is constant, natural for VTK */
36552f7358SJed Brown     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &cdof);CHKERRQ(ierr);}
37552f7358SJed Brown     if ((fStart >= pStart) && (fStart < pEnd)) {ierr = PetscSectionGetDof(section, fStart, &fdof);CHKERRQ(ierr);}
38552f7358SJed Brown     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vdof);CHKERRQ(ierr);}
39552f7358SJed Brown     if (cdof && fdof && vdof) { /* Actually Q2 or some such, but visualize as Q1 */
40552f7358SJed Brown       ft = (cdof == dim) ? PETSC_VTK_POINT_VECTOR_FIELD : PETSC_VTK_POINT_FIELD;
41552f7358SJed Brown     } else if (cdof && vdof) {
42ce94432eSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"No support for viewing mixed space with dofs at both vertices and cells");
43552f7358SJed Brown     } else if (cdof) {
44552f7358SJed Brown       /* TODO: This assumption should be removed when there is a way of identifying whether a space is conceptually a
45552f7358SJed Brown        * vector or just happens to have the same number of dofs as the dimension. */
46552f7358SJed Brown       if (cdof == dim) {
47552f7358SJed Brown         ft = PETSC_VTK_CELL_VECTOR_FIELD;
48552f7358SJed Brown       } else {
49552f7358SJed Brown         ft = PETSC_VTK_CELL_FIELD;
50552f7358SJed Brown       }
51552f7358SJed Brown     } else if (vdof) {
52552f7358SJed Brown       if (vdof == dim) {
53552f7358SJed Brown         ft = PETSC_VTK_POINT_VECTOR_FIELD;
54552f7358SJed Brown       } else {
55552f7358SJed Brown         ft = PETSC_VTK_POINT_FIELD;
56552f7358SJed Brown       }
5782f516ccSBarry Smith     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
58552f7358SJed Brown 
59552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr); /* viewer drops reference */
60552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
61552f7358SJed Brown     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
62552f7358SJed Brown   } else {
63552f7358SJed Brown     PetscBool isseq;
64552f7358SJed Brown 
65552f7358SJed Brown     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
66552f7358SJed Brown     if (isseq) {
67552f7358SJed Brown       ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);
68552f7358SJed Brown     } else {
69552f7358SJed Brown       ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);
70552f7358SJed Brown     }
71552f7358SJed Brown   }
72552f7358SJed Brown   PetscFunctionReturn(0);
73552f7358SJed Brown }
74552f7358SJed Brown 
75552f7358SJed Brown #undef __FUNCT__
76552f7358SJed Brown #define __FUNCT__ "VecView_Plex"
77552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
78552f7358SJed Brown {
79552f7358SJed Brown   DM             dm;
80552f7358SJed Brown   PetscBool      isvtk;
81552f7358SJed Brown   PetscErrorCode ierr;
82552f7358SJed Brown 
83552f7358SJed Brown   PetscFunctionBegin;
84552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
8582f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
86552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK, &isvtk);CHKERRQ(ierr);
87552f7358SJed Brown   if (isvtk) {
88552f7358SJed Brown     Vec         locv;
89552f7358SJed Brown     const char *name;
90552f7358SJed Brown 
91552f7358SJed Brown     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
92552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
93552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
94552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
95552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
96552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
97552f7358SJed Brown     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
98552f7358SJed Brown   } else {
99552f7358SJed Brown     PetscBool isseq;
100552f7358SJed Brown 
101552f7358SJed Brown     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
102552f7358SJed Brown     if (isseq) {
103552f7358SJed Brown       ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);
104552f7358SJed Brown     } else {
105552f7358SJed Brown       ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);
106552f7358SJed Brown     }
107552f7358SJed Brown   }
108552f7358SJed Brown   PetscFunctionReturn(0);
109552f7358SJed Brown }
110552f7358SJed Brown 
111552f7358SJed Brown #undef __FUNCT__
112552f7358SJed Brown #define __FUNCT__ "DMPlexView_Ascii"
113552f7358SJed Brown PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
114552f7358SJed Brown {
115552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
116552f7358SJed Brown   DM                cdm;
117552f7358SJed Brown   DMLabel           markers;
118552f7358SJed Brown   PetscSection      coordSection;
119552f7358SJed Brown   Vec               coordinates;
120552f7358SJed Brown   PetscViewerFormat format;
121552f7358SJed Brown   PetscErrorCode    ierr;
122552f7358SJed Brown 
123552f7358SJed Brown   PetscFunctionBegin;
124552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
125552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
126552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
127552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
128552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
129552f7358SJed Brown     const char *name;
130552f7358SJed Brown     PetscInt    maxConeSize, maxSupportSize;
131552f7358SJed Brown     PetscInt    pStart, pEnd, p;
132552f7358SJed Brown     PetscMPIInt rank, size;
133552f7358SJed Brown 
13482f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
13582f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
136552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
137552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
138552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
139552f7358SJed Brown     ierr = PetscViewerASCIISynchronizedAllow(viewer, PETSC_TRUE);CHKERRQ(ierr);
140552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);CHKERRQ(ierr);
141552f7358SJed Brown     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "Max sizes cone: %D support: %D\n", maxConeSize, maxSupportSize);CHKERRQ(ierr);
142552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
143552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
144552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
145552f7358SJed Brown       PetscInt dof, off, s;
146552f7358SJed Brown 
147552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
148552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
149552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
150552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%D]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
151552f7358SJed Brown       }
152552f7358SJed Brown     }
153552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
154552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
155552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
156552f7358SJed Brown       PetscInt dof, off, c;
157552f7358SJed Brown 
158552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
159552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
160552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
161552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%D]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
162552f7358SJed Brown       }
163552f7358SJed Brown     }
164552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1650298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
166552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
167552f7358SJed Brown     ierr = DMPlexGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
168552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
169552f7358SJed Brown     if (size > 1) {
170552f7358SJed Brown       PetscSF sf;
171552f7358SJed Brown 
172552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
173552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
174552f7358SJed Brown     }
175552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
176552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
177552f7358SJed Brown     const char  *name;
178552f7358SJed Brown     const char  *colors[3] = {"red", "blue", "green"};
179552f7358SJed Brown     const int    numColors  = 3;
180552f7358SJed Brown     PetscReal    scale      = 2.0;
181552f7358SJed Brown     PetscScalar *coords;
182552f7358SJed Brown     PetscInt     depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
183552f7358SJed Brown     PetscMPIInt  rank, size;
184552f7358SJed Brown 
185552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
18682f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
18782f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
188552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
189552f7358SJed Brown     ierr = PetscViewerASCIISynchronizedAllow(viewer, PETSC_TRUE);CHKERRQ(ierr);
190770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
191770b213bSMatthew G Knepley \\documentclass[crop,multi=false]{standalone}\n\n\
192552f7358SJed Brown \\usepackage{tikz}\n\
193552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
194552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
195552f7358SJed Brown \\usetikzlibrary{arrows}\n\
196552f7358SJed Brown \\begin{document}\n\
197552f7358SJed Brown \\section{%s}\n\
198770b213bSMatthew G Knepley \\begin{center}\n", name, 8.0/scale);CHKERRQ(ierr);
199770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Mesh for process ");CHKERRQ(ierr);
200770b213bSMatthew G Knepley     for (p = 0; p < size; ++p) {
201770b213bSMatthew G Knepley       if (p > 0 && p == size-1) {
202770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
203770b213bSMatthew G Knepley       } else if (p > 0) {
204770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
205770b213bSMatthew G Knepley       }
206770b213bSMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
207770b213bSMatthew G Knepley     }
208770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n\
209770b213bSMatthew G Knepley \\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n");CHKERRQ(ierr);
210552f7358SJed Brown     /* Plot vertices */
211552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
212552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
213552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
214552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
215552f7358SJed Brown       PetscInt off, dof, d;
216552f7358SJed Brown 
217552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
218552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
219552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
220552f7358SJed Brown       for (d = 0; d < dof; ++d) {
221552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
222552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%G", scale*PetscRealPart(coords[off+d]));CHKERRQ(ierr);
223552f7358SJed Brown       }
224552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%D) [draw,shape=circle,color=%s] {%D} --\n", v, rank, colors[rank%numColors], v);CHKERRQ(ierr);
225552f7358SJed Brown     }
226552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
227552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
228552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
229552f7358SJed Brown     /* Plot edges */
230552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
231552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
232552f7358SJed Brown     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
233552f7358SJed Brown     for (e = eStart; e < eEnd; ++e) {
234552f7358SJed Brown       const PetscInt *cone;
235552f7358SJed Brown       PetscInt        coneSize, offA, offB, dof, d;
236552f7358SJed Brown 
237552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
23882f516ccSBarry Smith       if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %d cone should have two vertices, not %d", e, coneSize);
239552f7358SJed Brown       ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
240552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
241552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
242552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
243552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
244552f7358SJed Brown       for (d = 0; d < dof; ++d) {
245552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
246552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%G", scale*0.5*PetscRealPart(coords[offA+d]+coords[offB+d]));CHKERRQ(ierr);
247552f7358SJed Brown       }
248552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%D) [draw,shape=circle,color=%s] {%D} --\n", e, rank, colors[rank%numColors], e);CHKERRQ(ierr);
249552f7358SJed Brown     }
250552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
251552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
252552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
253552f7358SJed Brown     /* Plot cells */
254552f7358SJed Brown     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
255552f7358SJed Brown     for (c = cStart; c < cEnd; ++c) {
2560298fd71SBarry Smith       PetscInt *closure = NULL;
257552f7358SJed Brown       PetscInt  closureSize, firstPoint = -1;
258552f7358SJed Brown 
259552f7358SJed Brown       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
260552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
261552f7358SJed Brown       for (p = 0; p < closureSize*2; p += 2) {
262552f7358SJed Brown         const PetscInt point = closure[p];
263552f7358SJed Brown 
264552f7358SJed Brown         if ((point < vStart) || (point >= vEnd)) continue;
265552f7358SJed Brown         if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
266552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%D)", point, rank);CHKERRQ(ierr);
267552f7358SJed Brown         if (firstPoint < 0) firstPoint = point;
268552f7358SJed Brown       }
269552f7358SJed Brown       /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
270552f7358SJed Brown       ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%D);\n", firstPoint, rank);CHKERRQ(ierr);
271552f7358SJed Brown       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
272552f7358SJed Brown     }
273552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
274552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n\\end{center}\n");CHKERRQ(ierr);
275770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
276552f7358SJed Brown   } else {
27782f516ccSBarry Smith     MPI_Comm    comm;
278834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
279834065abSMatthew G. Knepley     PetscInt    locDepth, depth, dim, d, pMax[4];
280552f7358SJed Brown     PetscInt    pStart, pEnd, p;
281a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
2821143a3c0SMatthew G. Knepley     const char *name;
283552f7358SJed Brown     PetscMPIInt size;
284552f7358SJed Brown 
28582f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
286552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
287552f7358SJed Brown     ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
2885f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
2891143a3c0SMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);CHKERRQ(ierr);}
2901143a3c0SMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);CHKERRQ(ierr);}
291552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
292552f7358SJed Brown     ierr = MPI_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
293834065abSMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], &pMax[depth-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
294dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
295552f7358SJed Brown     if (depth == 1) {
296552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
297552f7358SJed Brown       pEnd = pEnd - pStart;
298552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
299552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", 0);CHKERRQ(ierr);
300552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
301552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
302552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
303552f7358SJed Brown       pEnd = pEnd - pStart;
304552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
305552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
306552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
307552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
308552f7358SJed Brown     } else {
309552f7358SJed Brown       for (d = 0; d <= dim; d++) {
310552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
311834065abSMatthew G. Knepley         pEnd    -= pStart;
312834065abSMatthew G. Knepley         pMax[d] -= pStart;
313552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
314834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
315552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
316834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
317834065abSMatthew G. Knepley           if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
318834065abSMatthew G. Knepley           else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
319834065abSMatthew G. Knepley         }
320552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
321552f7358SJed Brown       }
322552f7358SJed Brown     }
323834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
324a57dd577SMatthew G Knepley     ierr = DMPlexGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
325a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
326a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
327a57dd577SMatthew G Knepley       DMLabel         label;
328a57dd577SMatthew G Knepley       const char     *name;
329a57dd577SMatthew G Knepley       IS              valueIS;
330a57dd577SMatthew G Knepley       const PetscInt *values;
331a57dd577SMatthew G Knepley       PetscInt        numValues, v;
332a57dd577SMatthew G Knepley 
333a57dd577SMatthew G Knepley       ierr = DMPlexGetLabelName(dm, l, &name);CHKERRQ(ierr);
334a57dd577SMatthew G Knepley       ierr = DMPlexGetLabel(dm, name, &label);CHKERRQ(ierr);
335a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
336a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %d strata of sizes (", name, numValues);CHKERRQ(ierr);
337a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
338a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
339a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
340a57dd577SMatthew G Knepley         PetscInt size;
341a57dd577SMatthew G Knepley 
342a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
343a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
344a57dd577SMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%d", size);CHKERRQ(ierr);
345a57dd577SMatthew G Knepley       }
346a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
347a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
3484d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
349a57dd577SMatthew G Knepley     }
350552f7358SJed Brown   }
351552f7358SJed Brown   PetscFunctionReturn(0);
352552f7358SJed Brown }
353552f7358SJed Brown 
354552f7358SJed Brown #undef __FUNCT__
355552f7358SJed Brown #define __FUNCT__ "DMView_Plex"
356552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
357552f7358SJed Brown {
358552f7358SJed Brown   PetscBool      iascii, isbinary;
359552f7358SJed Brown   PetscErrorCode ierr;
360552f7358SJed Brown 
361552f7358SJed Brown   PetscFunctionBegin;
362552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
363552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
364552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
365552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
366552f7358SJed Brown   if (iascii) {
367552f7358SJed Brown     ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
368552f7358SJed Brown #if 0
369552f7358SJed Brown   } else if (isbinary) {
370552f7358SJed Brown     ierr = DMPlexView_Binary(dm, viewer);CHKERRQ(ierr);
371552f7358SJed Brown #endif
37211aeaf0aSBarry Smith   }
373552f7358SJed Brown   PetscFunctionReturn(0);
374552f7358SJed Brown }
375552f7358SJed Brown 
376552f7358SJed Brown #undef __FUNCT__
377552f7358SJed Brown #define __FUNCT__ "DMDestroy_Plex"
378552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
379552f7358SJed Brown {
380552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
381552f7358SJed Brown   DMLabel        next  = mesh->labels;
382552f7358SJed Brown   PetscErrorCode ierr;
383552f7358SJed Brown 
384552f7358SJed Brown   PetscFunctionBegin;
3850d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
386552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
387552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
388552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
389552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
390552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
391552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
392552f7358SJed Brown   while (next) {
393552f7358SJed Brown     DMLabel tmp = next->next;
394552f7358SJed Brown 
395552f7358SJed Brown     ierr = DMLabelDestroy(&next);CHKERRQ(ierr);
396552f7358SJed Brown     next = tmp;
397552f7358SJed Brown   }
398a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
399552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
400552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
401552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
402552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
403552f7358SJed Brown   PetscFunctionReturn(0);
404552f7358SJed Brown }
405552f7358SJed Brown 
406552f7358SJed Brown #undef __FUNCT__
407552f7358SJed Brown #define __FUNCT__ "DMCreateMatrix_Plex"
408b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
409552f7358SJed Brown {
410552f7358SJed Brown   PetscSection   section, sectionGlobal;
411552f7358SJed Brown   PetscInt       bs = -1;
412552f7358SJed Brown   PetscInt       localSize;
41389545effSMatthew G. Knepley   PetscBool      isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock;
414552f7358SJed Brown   PetscErrorCode ierr;
415b412c318SBarry Smith   MatType        mtype;
416552f7358SJed Brown 
417552f7358SJed Brown   PetscFunctionBegin;
418607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
419b412c318SBarry Smith   mtype = dm->mattype;
420552f7358SJed Brown   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
421552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
422552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
423552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
42482f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
425552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
426552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
427552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
428552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
429552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
430552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
431552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
432552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
433552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
434552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
435552f7358SJed Brown   if (!isShell) {
436552f7358SJed Brown     PetscBool fillMatrix = (PetscBool) !dm->prealloc_only;
4372a28c762SMatthew G Knepley     PetscInt *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin;
438552f7358SJed Brown 
439552f7358SJed Brown     if (bs < 0) {
440552f7358SJed Brown       if (isBlock || isSeqBlock || isMPIBlock || isSymBlock || isSymSeqBlock || isSymMPIBlock) {
441fad22124SMatthew G Knepley         PetscInt pStart, pEnd, p, dof, cdof;
442552f7358SJed Brown 
443552f7358SJed Brown         ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
444552f7358SJed Brown         for (p = pStart; p < pEnd; ++p) {
445552f7358SJed Brown           ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
446fad22124SMatthew G Knepley           ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
447fad22124SMatthew G Knepley           if (dof-cdof) {
4482a28c762SMatthew G Knepley             if (bs < 0) {
449fad22124SMatthew G Knepley               bs = dof-cdof;
450fad22124SMatthew G Knepley             } else if (bs != dof-cdof) {
4512a28c762SMatthew G Knepley               /* Layout does not admit a pointwise block size */
4522a28c762SMatthew G Knepley               bs = 1;
453552f7358SJed Brown               break;
454552f7358SJed Brown             }
455552f7358SJed Brown           }
4562a28c762SMatthew G Knepley         }
4572a28c762SMatthew G Knepley         /* Must have same blocksize on all procs (some might have no points) */
4582a28c762SMatthew G Knepley         bsLocal = bs;
45982f516ccSBarry Smith         ierr = MPI_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
4602a28c762SMatthew G Knepley         bsLocal = bs < 0 ? bsMax : bs;
46182f516ccSBarry Smith         ierr = MPI_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
4622a28c762SMatthew G Knepley         if (bsMin != bsMax) {
4632a28c762SMatthew G Knepley           bs = 1;
4642a28c762SMatthew G Knepley         } else {
4652a28c762SMatthew G Knepley           bs = bsMax;
4662a28c762SMatthew G Knepley         }
467552f7358SJed Brown       } else {
468552f7358SJed Brown         bs = 1;
469552f7358SJed Brown       }
470552f7358SJed Brown     }
471*1795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
472552f7358SJed Brown     ierr = DMPlexPreallocateOperator(dm, bs, section, sectionGlobal, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
473552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
474552f7358SJed Brown   }
475552f7358SJed Brown   PetscFunctionReturn(0);
476552f7358SJed Brown }
477552f7358SJed Brown 
478552f7358SJed Brown #undef __FUNCT__
479552f7358SJed Brown #define __FUNCT__ "DMPlexGetDimension"
480552f7358SJed Brown /*@
481552f7358SJed Brown   DMPlexGetDimension - Return the topological mesh dimension
482552f7358SJed Brown 
483552f7358SJed Brown   Not collective
484552f7358SJed Brown 
485552f7358SJed Brown   Input Parameter:
486552f7358SJed Brown . mesh - The DMPlex
487552f7358SJed Brown 
488552f7358SJed Brown   Output Parameter:
489552f7358SJed Brown . dim - The topological mesh dimension
490552f7358SJed Brown 
491552f7358SJed Brown   Level: beginner
492552f7358SJed Brown 
493552f7358SJed Brown .seealso: DMPlexCreate()
494552f7358SJed Brown @*/
495552f7358SJed Brown PetscErrorCode DMPlexGetDimension(DM dm, PetscInt *dim)
496552f7358SJed Brown {
497552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
498552f7358SJed Brown 
499552f7358SJed Brown   PetscFunctionBegin;
500552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
501552f7358SJed Brown   PetscValidPointer(dim, 2);
502552f7358SJed Brown   *dim = mesh->dim;
503552f7358SJed Brown   PetscFunctionReturn(0);
504552f7358SJed Brown }
505552f7358SJed Brown 
506552f7358SJed Brown #undef __FUNCT__
507552f7358SJed Brown #define __FUNCT__ "DMPlexSetDimension"
508552f7358SJed Brown /*@
509552f7358SJed Brown   DMPlexSetDimension - Set the topological mesh dimension
510552f7358SJed Brown 
511552f7358SJed Brown   Collective on mesh
512552f7358SJed Brown 
513552f7358SJed Brown   Input Parameters:
514552f7358SJed Brown + mesh - The DMPlex
515552f7358SJed Brown - dim - The topological mesh dimension
516552f7358SJed Brown 
517552f7358SJed Brown   Level: beginner
518552f7358SJed Brown 
519552f7358SJed Brown .seealso: DMPlexCreate()
520552f7358SJed Brown @*/
521552f7358SJed Brown PetscErrorCode DMPlexSetDimension(DM dm, PetscInt dim)
522552f7358SJed Brown {
523552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
524552f7358SJed Brown 
525552f7358SJed Brown   PetscFunctionBegin;
526552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
527552f7358SJed Brown   PetscValidLogicalCollectiveInt(dm, dim, 2);
528552f7358SJed Brown   mesh->dim               = dim;
529552f7358SJed Brown   mesh->preallocCenterDim = dim;
530552f7358SJed Brown   PetscFunctionReturn(0);
531552f7358SJed Brown }
532552f7358SJed Brown 
533552f7358SJed Brown #undef __FUNCT__
534552f7358SJed Brown #define __FUNCT__ "DMPlexGetChart"
535552f7358SJed Brown /*@
536552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
537552f7358SJed Brown 
538552f7358SJed Brown   Not collective
539552f7358SJed Brown 
540552f7358SJed Brown   Input Parameter:
541552f7358SJed Brown . mesh - The DMPlex
542552f7358SJed Brown 
543552f7358SJed Brown   Output Parameters:
544552f7358SJed Brown + pStart - The first mesh point
545552f7358SJed Brown - pEnd   - The upper bound for mesh points
546552f7358SJed Brown 
547552f7358SJed Brown   Level: beginner
548552f7358SJed Brown 
549552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
550552f7358SJed Brown @*/
551552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
552552f7358SJed Brown {
553552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
554552f7358SJed Brown   PetscErrorCode ierr;
555552f7358SJed Brown 
556552f7358SJed Brown   PetscFunctionBegin;
557552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
558552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
559552f7358SJed Brown   PetscFunctionReturn(0);
560552f7358SJed Brown }
561552f7358SJed Brown 
562552f7358SJed Brown #undef __FUNCT__
563552f7358SJed Brown #define __FUNCT__ "DMPlexSetChart"
564552f7358SJed Brown /*@
565552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
566552f7358SJed Brown 
567552f7358SJed Brown   Not collective
568552f7358SJed Brown 
569552f7358SJed Brown   Input Parameters:
570552f7358SJed Brown + mesh - The DMPlex
571552f7358SJed Brown . pStart - The first mesh point
572552f7358SJed Brown - pEnd   - The upper bound for mesh points
573552f7358SJed Brown 
574552f7358SJed Brown   Output Parameters:
575552f7358SJed Brown 
576552f7358SJed Brown   Level: beginner
577552f7358SJed Brown 
578552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
579552f7358SJed Brown @*/
580552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
581552f7358SJed Brown {
582552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
583552f7358SJed Brown   PetscErrorCode ierr;
584552f7358SJed Brown 
585552f7358SJed Brown   PetscFunctionBegin;
586552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
587552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
588552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
589552f7358SJed Brown   PetscFunctionReturn(0);
590552f7358SJed Brown }
591552f7358SJed Brown 
592552f7358SJed Brown #undef __FUNCT__
593552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSize"
594552f7358SJed Brown /*@
595552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
596552f7358SJed Brown 
597552f7358SJed Brown   Not collective
598552f7358SJed Brown 
599552f7358SJed Brown   Input Parameters:
600552f7358SJed Brown + mesh - The DMPlex
601552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
602552f7358SJed Brown 
603552f7358SJed Brown   Output Parameter:
604552f7358SJed Brown . size - The cone size for point p
605552f7358SJed Brown 
606552f7358SJed Brown   Level: beginner
607552f7358SJed Brown 
608552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
609552f7358SJed Brown @*/
610552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
611552f7358SJed Brown {
612552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
613552f7358SJed Brown   PetscErrorCode ierr;
614552f7358SJed Brown 
615552f7358SJed Brown   PetscFunctionBegin;
616552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
617552f7358SJed Brown   PetscValidPointer(size, 3);
618552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
619552f7358SJed Brown   PetscFunctionReturn(0);
620552f7358SJed Brown }
621552f7358SJed Brown 
622552f7358SJed Brown #undef __FUNCT__
623552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeSize"
624552f7358SJed Brown /*@
625552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
626552f7358SJed Brown 
627552f7358SJed Brown   Not collective
628552f7358SJed Brown 
629552f7358SJed Brown   Input Parameters:
630552f7358SJed Brown + mesh - The DMPlex
631552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
632552f7358SJed Brown - size - The cone size for point p
633552f7358SJed Brown 
634552f7358SJed Brown   Output Parameter:
635552f7358SJed Brown 
636552f7358SJed Brown   Note:
637552f7358SJed Brown   This should be called after DMPlexSetChart().
638552f7358SJed Brown 
639552f7358SJed Brown   Level: beginner
640552f7358SJed Brown 
641552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
642552f7358SJed Brown @*/
643552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
644552f7358SJed Brown {
645552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
646552f7358SJed Brown   PetscErrorCode ierr;
647552f7358SJed Brown 
648552f7358SJed Brown   PetscFunctionBegin;
649552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
650552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
6510d644c17SKarl Rupp 
652552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
653552f7358SJed Brown   PetscFunctionReturn(0);
654552f7358SJed Brown }
655552f7358SJed Brown 
656552f7358SJed Brown #undef __FUNCT__
657552f7358SJed Brown #define __FUNCT__ "DMPlexGetCone"
658552f7358SJed Brown /*@C
659552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
660552f7358SJed Brown 
661552f7358SJed Brown   Not collective
662552f7358SJed Brown 
663552f7358SJed Brown   Input Parameters:
664552f7358SJed Brown + mesh - The DMPlex
665552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
666552f7358SJed Brown 
667552f7358SJed Brown   Output Parameter:
668552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
669552f7358SJed Brown 
670552f7358SJed Brown   Level: beginner
671552f7358SJed Brown 
6723813dfbdSMatthew G Knepley   Fortran Notes:
6733813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
6743813dfbdSMatthew G Knepley   include petsc.h90 in your code.
6753813dfbdSMatthew G Knepley 
6763813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
677552f7358SJed Brown 
678552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
679552f7358SJed Brown @*/
680552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
681552f7358SJed Brown {
682552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
683552f7358SJed Brown   PetscInt       off;
684552f7358SJed Brown   PetscErrorCode ierr;
685552f7358SJed Brown 
686552f7358SJed Brown   PetscFunctionBegin;
687552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
688552f7358SJed Brown   PetscValidPointer(cone, 3);
689552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
690552f7358SJed Brown   *cone = &mesh->cones[off];
691552f7358SJed Brown   PetscFunctionReturn(0);
692552f7358SJed Brown }
693552f7358SJed Brown 
694552f7358SJed Brown #undef __FUNCT__
695552f7358SJed Brown #define __FUNCT__ "DMPlexSetCone"
696552f7358SJed Brown /*@
697552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
698552f7358SJed Brown 
699552f7358SJed Brown   Not collective
700552f7358SJed Brown 
701552f7358SJed Brown   Input Parameters:
702552f7358SJed Brown + mesh - The DMPlex
703552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
704552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
705552f7358SJed Brown 
706552f7358SJed Brown   Output Parameter:
707552f7358SJed Brown 
708552f7358SJed Brown   Note:
709552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
710552f7358SJed Brown 
711552f7358SJed Brown   Level: beginner
712552f7358SJed Brown 
713552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
714552f7358SJed Brown @*/
715552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
716552f7358SJed Brown {
717552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
718552f7358SJed Brown   PetscInt       pStart, pEnd;
719552f7358SJed Brown   PetscInt       dof, off, c;
720552f7358SJed Brown   PetscErrorCode ierr;
721552f7358SJed Brown 
722552f7358SJed Brown   PetscFunctionBegin;
723552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
724552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
725552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
726552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
727552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
72882f516ccSBarry 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);
729552f7358SJed Brown   for (c = 0; c < dof; ++c) {
73082f516ccSBarry 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);
731552f7358SJed Brown     mesh->cones[off+c] = cone[c];
732552f7358SJed Brown   }
733552f7358SJed Brown   PetscFunctionReturn(0);
734552f7358SJed Brown }
735552f7358SJed Brown 
736552f7358SJed Brown #undef __FUNCT__
737552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientation"
738552f7358SJed Brown /*@C
739552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
740552f7358SJed Brown 
741552f7358SJed Brown   Not collective
742552f7358SJed Brown 
743552f7358SJed Brown   Input Parameters:
744552f7358SJed Brown + mesh - The DMPlex
745552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
746552f7358SJed Brown 
747552f7358SJed Brown   Output Parameter:
748552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
749552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
750552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
751552f7358SJed Brown                     the index of the cone point on which to start.
752552f7358SJed Brown 
753552f7358SJed Brown   Level: beginner
754552f7358SJed Brown 
7553813dfbdSMatthew G Knepley   Fortran Notes:
7563813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
7573813dfbdSMatthew G Knepley   include petsc.h90 in your code.
7583813dfbdSMatthew G Knepley 
7593813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
760552f7358SJed Brown 
761552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
762552f7358SJed Brown @*/
763552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
764552f7358SJed Brown {
765552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
766552f7358SJed Brown   PetscInt       off;
767552f7358SJed Brown   PetscErrorCode ierr;
768552f7358SJed Brown 
769552f7358SJed Brown   PetscFunctionBegin;
770552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
771552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
772552f7358SJed Brown   {
773552f7358SJed Brown     PetscInt dof;
774552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
775552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
776552f7358SJed Brown   }
777552f7358SJed Brown #endif
778552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
7790d644c17SKarl Rupp 
780552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
781552f7358SJed Brown   PetscFunctionReturn(0);
782552f7358SJed Brown }
783552f7358SJed Brown 
784552f7358SJed Brown #undef __FUNCT__
785552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeOrientation"
786552f7358SJed Brown /*@
787552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
788552f7358SJed Brown 
789552f7358SJed Brown   Not collective
790552f7358SJed Brown 
791552f7358SJed Brown   Input Parameters:
792552f7358SJed Brown + mesh - The DMPlex
793552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
794552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
795552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
796552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
797552f7358SJed Brown                     the index of the cone point on which to start.
798552f7358SJed Brown 
799552f7358SJed Brown   Output Parameter:
800552f7358SJed Brown 
801552f7358SJed Brown   Note:
802552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
803552f7358SJed Brown 
804552f7358SJed Brown   Level: beginner
805552f7358SJed Brown 
806552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
807552f7358SJed Brown @*/
808552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
809552f7358SJed Brown {
810552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
811552f7358SJed Brown   PetscInt       pStart, pEnd;
812552f7358SJed Brown   PetscInt       dof, off, c;
813552f7358SJed Brown   PetscErrorCode ierr;
814552f7358SJed Brown 
815552f7358SJed Brown   PetscFunctionBegin;
816552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
817552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
818552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
819552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
820552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
82182f516ccSBarry 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);
822552f7358SJed Brown   for (c = 0; c < dof; ++c) {
823552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
824552f7358SJed Brown 
825552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
82682f516ccSBarry 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);
827552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
828552f7358SJed Brown   }
829552f7358SJed Brown   PetscFunctionReturn(0);
830552f7358SJed Brown }
831552f7358SJed Brown 
832552f7358SJed Brown #undef __FUNCT__
833552f7358SJed Brown #define __FUNCT__ "DMPlexInsertCone"
834552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
835552f7358SJed Brown {
836552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
837552f7358SJed Brown   PetscInt       pStart, pEnd;
838552f7358SJed Brown   PetscInt       dof, off;
839552f7358SJed Brown   PetscErrorCode ierr;
840552f7358SJed Brown 
841552f7358SJed Brown   PetscFunctionBegin;
842552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
843552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
84482f516ccSBarry 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);
84582f516ccSBarry 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);
84677c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
84777c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
84877c88f5bSMatthew 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);
849552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
850552f7358SJed Brown   PetscFunctionReturn(0);
851552f7358SJed Brown }
852552f7358SJed Brown 
853552f7358SJed Brown #undef __FUNCT__
85477c88f5bSMatthew G Knepley #define __FUNCT__ "DMPlexInsertConeOrientation"
85577c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
85677c88f5bSMatthew G Knepley {
85777c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
85877c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
85977c88f5bSMatthew G Knepley   PetscInt       dof, off;
86077c88f5bSMatthew G Knepley   PetscErrorCode ierr;
86177c88f5bSMatthew G Knepley 
86277c88f5bSMatthew G Knepley   PetscFunctionBegin;
86377c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86477c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
86577c88f5bSMatthew 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);
86677c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
86777c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
86877c88f5bSMatthew 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);
86977c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
87077c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
87177c88f5bSMatthew G Knepley }
87277c88f5bSMatthew G Knepley 
87377c88f5bSMatthew G Knepley #undef __FUNCT__
874552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupportSize"
875552f7358SJed Brown /*@
876552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
877552f7358SJed Brown 
878552f7358SJed Brown   Not collective
879552f7358SJed Brown 
880552f7358SJed Brown   Input Parameters:
881552f7358SJed Brown + mesh - The DMPlex
882552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
883552f7358SJed Brown 
884552f7358SJed Brown   Output Parameter:
885552f7358SJed Brown . size - The support size for point p
886552f7358SJed Brown 
887552f7358SJed Brown   Level: beginner
888552f7358SJed Brown 
889552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
890552f7358SJed Brown @*/
891552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
892552f7358SJed Brown {
893552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
894552f7358SJed Brown   PetscErrorCode ierr;
895552f7358SJed Brown 
896552f7358SJed Brown   PetscFunctionBegin;
897552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
898552f7358SJed Brown   PetscValidPointer(size, 3);
899552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
900552f7358SJed Brown   PetscFunctionReturn(0);
901552f7358SJed Brown }
902552f7358SJed Brown 
903552f7358SJed Brown #undef __FUNCT__
904552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupportSize"
905552f7358SJed Brown /*@
906552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
907552f7358SJed Brown 
908552f7358SJed Brown   Not collective
909552f7358SJed Brown 
910552f7358SJed Brown   Input Parameters:
911552f7358SJed Brown + mesh - The DMPlex
912552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
913552f7358SJed Brown - size - The support size for point p
914552f7358SJed Brown 
915552f7358SJed Brown   Output Parameter:
916552f7358SJed Brown 
917552f7358SJed Brown   Note:
918552f7358SJed Brown   This should be called after DMPlexSetChart().
919552f7358SJed Brown 
920552f7358SJed Brown   Level: beginner
921552f7358SJed Brown 
922552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
923552f7358SJed Brown @*/
924552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
925552f7358SJed Brown {
926552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
927552f7358SJed Brown   PetscErrorCode ierr;
928552f7358SJed Brown 
929552f7358SJed Brown   PetscFunctionBegin;
930552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
931552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
9320d644c17SKarl Rupp 
933552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
934552f7358SJed Brown   PetscFunctionReturn(0);
935552f7358SJed Brown }
936552f7358SJed Brown 
937552f7358SJed Brown #undef __FUNCT__
938552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupport"
939552f7358SJed Brown /*@C
940552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
941552f7358SJed Brown 
942552f7358SJed Brown   Not collective
943552f7358SJed Brown 
944552f7358SJed Brown   Input Parameters:
945552f7358SJed Brown + mesh - The DMPlex
946552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
947552f7358SJed Brown 
948552f7358SJed Brown   Output Parameter:
949552f7358SJed Brown . support - An array of points which are on the out-edges for point p
950552f7358SJed Brown 
951552f7358SJed Brown   Level: beginner
952552f7358SJed Brown 
9533813dfbdSMatthew G Knepley   Fortran Notes:
9543813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
9553813dfbdSMatthew G Knepley   include petsc.h90 in your code.
9563813dfbdSMatthew G Knepley 
9573813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
9583813dfbdSMatthew G Knepley 
959552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
960552f7358SJed Brown @*/
961552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
962552f7358SJed Brown {
963552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
964552f7358SJed Brown   PetscInt       off;
965552f7358SJed Brown   PetscErrorCode ierr;
966552f7358SJed Brown 
967552f7358SJed Brown   PetscFunctionBegin;
968552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
969552f7358SJed Brown   PetscValidPointer(support, 3);
970552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
971552f7358SJed Brown   *support = &mesh->supports[off];
972552f7358SJed Brown   PetscFunctionReturn(0);
973552f7358SJed Brown }
974552f7358SJed Brown 
975552f7358SJed Brown #undef __FUNCT__
976552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupport"
977552f7358SJed Brown /*@
978552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
979552f7358SJed Brown 
980552f7358SJed Brown   Not collective
981552f7358SJed Brown 
982552f7358SJed Brown   Input Parameters:
983552f7358SJed Brown + mesh - The DMPlex
984552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
985552f7358SJed Brown - support - An array of points which are on the in-edges for point p
986552f7358SJed Brown 
987552f7358SJed Brown   Output Parameter:
988552f7358SJed Brown 
989552f7358SJed Brown   Note:
990552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
991552f7358SJed Brown 
992552f7358SJed Brown   Level: beginner
993552f7358SJed Brown 
994552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
995552f7358SJed Brown @*/
996552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
997552f7358SJed Brown {
998552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
999552f7358SJed Brown   PetscInt       pStart, pEnd;
1000552f7358SJed Brown   PetscInt       dof, off, c;
1001552f7358SJed Brown   PetscErrorCode ierr;
1002552f7358SJed Brown 
1003552f7358SJed Brown   PetscFunctionBegin;
1004552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1005552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1006552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1007552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1008552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
100982f516ccSBarry 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);
1010552f7358SJed Brown   for (c = 0; c < dof; ++c) {
101182f516ccSBarry 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);
1012552f7358SJed Brown     mesh->supports[off+c] = support[c];
1013552f7358SJed Brown   }
1014552f7358SJed Brown   PetscFunctionReturn(0);
1015552f7358SJed Brown }
1016552f7358SJed Brown 
1017552f7358SJed Brown #undef __FUNCT__
1018552f7358SJed Brown #define __FUNCT__ "DMPlexInsertSupport"
1019552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1020552f7358SJed Brown {
1021552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1022552f7358SJed Brown   PetscInt       pStart, pEnd;
1023552f7358SJed Brown   PetscInt       dof, off;
1024552f7358SJed Brown   PetscErrorCode ierr;
1025552f7358SJed Brown 
1026552f7358SJed Brown   PetscFunctionBegin;
1027552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1028552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1029552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1030552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
103182f516ccSBarry 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);
103282f516ccSBarry 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);
103382f516ccSBarry 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);
1034552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1035552f7358SJed Brown   PetscFunctionReturn(0);
1036552f7358SJed Brown }
1037552f7358SJed Brown 
1038552f7358SJed Brown #undef __FUNCT__
1039552f7358SJed Brown #define __FUNCT__ "DMPlexGetTransitiveClosure"
1040552f7358SJed Brown /*@C
1041552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1042552f7358SJed Brown 
1043552f7358SJed Brown   Not collective
1044552f7358SJed Brown 
1045552f7358SJed Brown   Input Parameters:
1046552f7358SJed Brown + mesh - The DMPlex
1047552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1048552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
10490298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1050552f7358SJed Brown 
1051552f7358SJed Brown   Output Parameters:
1052552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1053552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1054552f7358SJed Brown 
1055552f7358SJed Brown   Note:
10560298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1057552f7358SJed Brown 
10583813dfbdSMatthew G Knepley   Fortran Notes:
10593813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
10603813dfbdSMatthew G Knepley   include petsc.h90 in your code.
10613813dfbdSMatthew G Knepley 
10623813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
10633813dfbdSMatthew G Knepley 
1064552f7358SJed Brown   Level: beginner
1065552f7358SJed Brown 
1066552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1067552f7358SJed Brown @*/
1068552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1069552f7358SJed Brown {
1070552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1071552f7358SJed Brown   PetscInt       *closure, *fifo;
10720298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1073552f7358SJed Brown   PetscInt        tmpSize, t;
1074552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1075552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1076552f7358SJed Brown   PetscErrorCode  ierr;
1077552f7358SJed Brown 
1078552f7358SJed Brown   PetscFunctionBegin;
1079552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1080552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1081552f7358SJed Brown   /* This is only 1-level */
1082552f7358SJed Brown   if (useCone) {
1083552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1084552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1085552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1086552f7358SJed Brown   } else {
1087552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1088552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1089552f7358SJed Brown   }
1090bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1091bfbcdd7aSMatthew G. Knepley     if (*points) {
1092bfbcdd7aSMatthew G. Knepley       closure = *points;
1093bfbcdd7aSMatthew G. Knepley     } else {
1094bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1095bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1096bfbcdd7aSMatthew G. Knepley     }
1097bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1098bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1099bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1100bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1101bfbcdd7aSMatthew G. Knepley     }
1102bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1103bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1104bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1105bfbcdd7aSMatthew G. Knepley   }
1106bfbcdd7aSMatthew G. Knepley   maxSize = 2*PetscMax(PetscMax(PetscPowInt(mesh->maxConeSize,depth+1),PetscPowInt(mesh->maxSupportSize,depth+1)),depth+1);
1107bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1108bfbcdd7aSMatthew G. Knepley   if (*points) {
1109bfbcdd7aSMatthew G. Knepley     closure = *points;
1110bfbcdd7aSMatthew G. Knepley   } else {
1111bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1112bfbcdd7aSMatthew G. Knepley   }
1113bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1114552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1115552f7358SJed Brown     const PetscInt cp = tmp[t];
1116552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1117552f7358SJed Brown 
1118552f7358SJed Brown     closure[closureSize]   = cp;
1119552f7358SJed Brown     closure[closureSize+1] = co;
1120552f7358SJed Brown     fifo[fifoSize]         = cp;
1121552f7358SJed Brown     fifo[fifoSize+1]       = co;
1122552f7358SJed Brown   }
1123bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1124552f7358SJed Brown   while (fifoSize - fifoStart) {
1125552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1126552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1127552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1128552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1129552f7358SJed Brown 
1130552f7358SJed Brown     if (useCone) {
1131552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1132552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1133552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1134552f7358SJed Brown     } else {
1135552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1136552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
11370298fd71SBarry Smith       tmpO = NULL;
1138552f7358SJed Brown     }
1139552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1140552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1141552f7358SJed Brown       const PetscInt cp = tmp[i];
11422e1b13c2SMatthew 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. */
11432e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
11442e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
11452e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1146552f7358SJed Brown       PetscInt       c;
1147552f7358SJed Brown 
11482e1b13c2SMatthew G. Knepley       if (rev) {
11492e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
11502e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
11512e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
11522e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
11532e1b13c2SMatthew G. Knepley       }
1154552f7358SJed Brown       /* Check for duplicate */
1155552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1156552f7358SJed Brown         if (closure[c] == cp) break;
1157552f7358SJed Brown       }
1158552f7358SJed Brown       if (c == closureSize) {
1159552f7358SJed Brown         closure[closureSize]   = cp;
1160552f7358SJed Brown         closure[closureSize+1] = co;
1161552f7358SJed Brown         fifo[fifoSize]         = cp;
1162552f7358SJed Brown         fifo[fifoSize+1]       = co;
1163552f7358SJed Brown         closureSize           += 2;
1164552f7358SJed Brown         fifoSize              += 2;
1165552f7358SJed Brown       }
1166552f7358SJed Brown     }
1167552f7358SJed Brown     fifoStart += 2;
1168552f7358SJed Brown   }
1169552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1170552f7358SJed Brown   if (points)    *points    = closure;
1171552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1172552f7358SJed Brown   PetscFunctionReturn(0);
1173552f7358SJed Brown }
1174552f7358SJed Brown 
1175552f7358SJed Brown #undef __FUNCT__
1176552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreTransitiveClosure"
1177552f7358SJed Brown /*@C
1178552f7358SJed 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
1179552f7358SJed Brown 
1180552f7358SJed Brown   Not collective
1181552f7358SJed Brown 
1182552f7358SJed Brown   Input Parameters:
1183552f7358SJed Brown + mesh - The DMPlex
1184552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1185552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1186e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1187e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
1188552f7358SJed Brown 
1189552f7358SJed Brown   Note:
11900298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
1191552f7358SJed Brown 
11923813dfbdSMatthew G Knepley   Fortran Notes:
11933813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
11943813dfbdSMatthew G Knepley   include petsc.h90 in your code.
11953813dfbdSMatthew G Knepley 
11963813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
11973813dfbdSMatthew G Knepley 
1198552f7358SJed Brown   Level: beginner
1199552f7358SJed Brown 
1200552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1201552f7358SJed Brown @*/
1202552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1203552f7358SJed Brown {
1204552f7358SJed Brown   PetscErrorCode ierr;
1205552f7358SJed Brown 
1206552f7358SJed Brown   PetscFunctionBegin;
1207552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1208e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
1209e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
1210552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
12114ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
1212552f7358SJed Brown   PetscFunctionReturn(0);
1213552f7358SJed Brown }
1214552f7358SJed Brown 
1215552f7358SJed Brown #undef __FUNCT__
1216552f7358SJed Brown #define __FUNCT__ "DMPlexGetMaxSizes"
1217552f7358SJed Brown /*@
1218552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
1219552f7358SJed Brown 
1220552f7358SJed Brown   Not collective
1221552f7358SJed Brown 
1222552f7358SJed Brown   Input Parameter:
1223552f7358SJed Brown . mesh - The DMPlex
1224552f7358SJed Brown 
1225552f7358SJed Brown   Output Parameters:
1226552f7358SJed Brown + maxConeSize - The maximum number of in-edges
1227552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
1228552f7358SJed Brown 
1229552f7358SJed Brown   Level: beginner
1230552f7358SJed Brown 
1231552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1232552f7358SJed Brown @*/
1233552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1234552f7358SJed Brown {
1235552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
1236552f7358SJed Brown 
1237552f7358SJed Brown   PetscFunctionBegin;
1238552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1239552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1240552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1241552f7358SJed Brown   PetscFunctionReturn(0);
1242552f7358SJed Brown }
1243552f7358SJed Brown 
1244552f7358SJed Brown #undef __FUNCT__
1245552f7358SJed Brown #define __FUNCT__ "DMSetUp_Plex"
1246552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
1247552f7358SJed Brown {
1248552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1249552f7358SJed Brown   PetscInt       size;
1250552f7358SJed Brown   PetscErrorCode ierr;
1251552f7358SJed Brown 
1252552f7358SJed Brown   PetscFunctionBegin;
1253552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1254552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
1255552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
1256*1795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
1257*1795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
1258552f7358SJed Brown   if (mesh->maxSupportSize) {
1259552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
1260552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
1261*1795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
1262552f7358SJed Brown   }
1263552f7358SJed Brown   PetscFunctionReturn(0);
1264552f7358SJed Brown }
1265552f7358SJed Brown 
1266552f7358SJed Brown #undef __FUNCT__
1267552f7358SJed Brown #define __FUNCT__ "DMCreateSubDM_Plex"
1268552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1269552f7358SJed Brown {
1270552f7358SJed Brown   PetscErrorCode ierr;
1271552f7358SJed Brown 
1272552f7358SJed Brown   PetscFunctionBegin;
12734d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
12744d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1275552f7358SJed Brown   PetscFunctionReturn(0);
1276552f7358SJed Brown }
1277552f7358SJed Brown 
1278552f7358SJed Brown #undef __FUNCT__
1279552f7358SJed Brown #define __FUNCT__ "DMPlexSymmetrize"
1280552f7358SJed Brown /*@
1281552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
1282552f7358SJed Brown 
1283552f7358SJed Brown   Not collective
1284552f7358SJed Brown 
1285552f7358SJed Brown   Input Parameter:
1286552f7358SJed Brown . mesh - The DMPlex
1287552f7358SJed Brown 
1288552f7358SJed Brown   Output Parameter:
1289552f7358SJed Brown 
1290552f7358SJed Brown   Note:
1291552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
1292552f7358SJed Brown 
1293552f7358SJed Brown   Level: beginner
1294552f7358SJed Brown 
1295552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
1296552f7358SJed Brown @*/
1297552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
1298552f7358SJed Brown {
1299552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1300552f7358SJed Brown   PetscInt      *offsets;
1301552f7358SJed Brown   PetscInt       supportSize;
1302552f7358SJed Brown   PetscInt       pStart, pEnd, p;
1303552f7358SJed Brown   PetscErrorCode ierr;
1304552f7358SJed Brown 
1305552f7358SJed Brown   PetscFunctionBegin;
1306552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
130782f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
1308552f7358SJed Brown   /* Calculate support sizes */
1309552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1310552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
1311552f7358SJed Brown     PetscInt dof, off, c;
1312552f7358SJed Brown 
1313552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1314552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1315552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
1316552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
1317552f7358SJed Brown     }
1318552f7358SJed Brown   }
1319552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
1320552f7358SJed Brown     PetscInt dof;
1321552f7358SJed Brown 
1322552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
13230d644c17SKarl Rupp 
1324552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
1325552f7358SJed Brown   }
1326552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
1327552f7358SJed Brown   /* Calculate supports */
1328552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
1329*1795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
1330*1795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
1331552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
1332552f7358SJed Brown     PetscInt dof, off, c;
1333552f7358SJed Brown 
1334552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1335552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1336552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
1337552f7358SJed Brown       const PetscInt q = mesh->cones[c];
1338552f7358SJed Brown       PetscInt       offS;
1339552f7358SJed Brown 
1340552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
13410d644c17SKarl Rupp 
1342552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
1343552f7358SJed Brown       ++offsets[q];
1344552f7358SJed Brown     }
1345552f7358SJed Brown   }
1346552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
1347552f7358SJed Brown   PetscFunctionReturn(0);
1348552f7358SJed Brown }
1349552f7358SJed Brown 
1350552f7358SJed Brown #undef __FUNCT__
1351552f7358SJed Brown #define __FUNCT__ "DMPlexStratify"
1352552f7358SJed Brown /*@
1353552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
1354552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
1355552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
1356552f7358SJed Brown   the DAG.
1357552f7358SJed Brown 
1358552f7358SJed Brown   Not collective
1359552f7358SJed Brown 
1360552f7358SJed Brown   Input Parameter:
1361552f7358SJed Brown . mesh - The DMPlex
1362552f7358SJed Brown 
1363552f7358SJed Brown   Output Parameter:
1364552f7358SJed Brown 
1365552f7358SJed Brown   Notes:
1366552f7358SJed Brown   The normal association for the point grade is element dimension (or co-dimension). For instance, all vertices would
1367552f7358SJed Brown   have depth 0, and all edges depth 1. Likewise, all cells heights would have height 0, and all faces height 1.
1368552f7358SJed Brown 
1369552f7358SJed Brown   This should be called after all calls to DMPlexSymmetrize()
1370552f7358SJed Brown 
1371552f7358SJed Brown   Level: beginner
1372552f7358SJed Brown 
1373552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
1374552f7358SJed Brown @*/
1375552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
1376552f7358SJed Brown {
1377aa50250dSMatthew G. Knepley   DMLabel        label;
1378552f7358SJed Brown   PetscInt       pStart, pEnd, p;
1379552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
1380552f7358SJed Brown   PetscErrorCode ierr;
1381552f7358SJed Brown 
1382552f7358SJed Brown   PetscFunctionBegin;
1383552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1384552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
1385552f7358SJed Brown   /* Calculate depth */
1386aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1387aa50250dSMatthew G. Knepley   ierr = DMPlexCreateLabel(dm, "depth");CHKERRQ(ierr);
1388aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
1389552f7358SJed Brown   /* Initialize roots and count leaves */
1390552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
1391552f7358SJed Brown     PetscInt coneSize, supportSize;
1392552f7358SJed Brown 
1393552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1394552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
1395552f7358SJed Brown     if (!coneSize && supportSize) {
1396552f7358SJed Brown       ++numRoots;
1397aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
1398552f7358SJed Brown     } else if (!supportSize && coneSize) {
1399552f7358SJed Brown       ++numLeaves;
1400552f7358SJed Brown     } else if (!supportSize && !coneSize) {
1401552f7358SJed Brown       /* Isolated points */
1402aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
1403552f7358SJed Brown     }
1404552f7358SJed Brown   }
1405552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
1406552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
1407552f7358SJed Brown       PetscInt coneSize, supportSize;
1408552f7358SJed Brown 
1409552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
1410552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
1411552f7358SJed Brown       if (!supportSize && coneSize) {
1412aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
1413552f7358SJed Brown       }
1414552f7358SJed Brown     }
1415552f7358SJed Brown   } else {
141674ef644bSMatthew G. Knepley     IS       pointIS;
141774ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
1418552f7358SJed Brown 
141974ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
142074ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
142174ef644bSMatthew G. Knepley     while (numPoints) {
142274ef644bSMatthew G. Knepley       const PetscInt *points;
142374ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
142474ef644bSMatthew G. Knepley 
142574ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
142674ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
142774ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
142874ef644bSMatthew G. Knepley         const PetscInt *support;
142974ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
143074ef644bSMatthew G. Knepley 
143174ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
143274ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
143374ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
143474ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
1435552f7358SJed Brown         }
1436552f7358SJed Brown       }
14375edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
143874ef644bSMatthew G. Knepley       ++level;
143974ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
144074ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
144174ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
144274ef644bSMatthew G. Knepley       else         {numPoints = 0;}
144374ef644bSMatthew G. Knepley     }
144474ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
144574ef644bSMatthew G. Knepley   }
1446552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
1447552f7358SJed Brown   PetscFunctionReturn(0);
1448552f7358SJed Brown }
1449552f7358SJed Brown 
1450552f7358SJed Brown #undef __FUNCT__
1451552f7358SJed Brown #define __FUNCT__ "DMPlexGetJoin"
1452552f7358SJed Brown /*@C
1453552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
1454552f7358SJed Brown 
1455552f7358SJed Brown   Not Collective
1456552f7358SJed Brown 
1457552f7358SJed Brown   Input Parameters:
1458552f7358SJed Brown + dm - The DMPlex object
1459552f7358SJed Brown . numPoints - The number of input points for the join
1460552f7358SJed Brown - points - The input points
1461552f7358SJed Brown 
1462552f7358SJed Brown   Output Parameters:
1463552f7358SJed Brown + numCoveredPoints - The number of points in the join
1464552f7358SJed Brown - coveredPoints - The points in the join
1465552f7358SJed Brown 
1466552f7358SJed Brown   Level: intermediate
1467552f7358SJed Brown 
1468552f7358SJed Brown   Note: Currently, this is restricted to a single level join
1469552f7358SJed Brown 
14703813dfbdSMatthew G Knepley   Fortran Notes:
14713813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14723813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14733813dfbdSMatthew G Knepley 
14743813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
14753813dfbdSMatthew G Knepley 
1476552f7358SJed Brown .keywords: mesh
1477552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
1478552f7358SJed Brown @*/
1479552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
1480552f7358SJed Brown {
1481552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1482552f7358SJed Brown   PetscInt      *join[2];
1483552f7358SJed Brown   PetscInt       joinSize, i = 0;
1484552f7358SJed Brown   PetscInt       dof, off, p, c, m;
1485552f7358SJed Brown   PetscErrorCode ierr;
1486552f7358SJed Brown 
1487552f7358SJed Brown   PetscFunctionBegin;
1488552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1489552f7358SJed Brown   PetscValidPointer(points, 2);
1490552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
1491552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
1492552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
1493552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
1494552f7358SJed Brown   /* Copy in support of first point */
1495552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
1496552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
1497552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
1498552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
1499552f7358SJed Brown   }
1500552f7358SJed Brown   /* Check each successive support */
1501552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
1502552f7358SJed Brown     PetscInt newJoinSize = 0;
1503552f7358SJed Brown 
1504552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
1505552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
1506552f7358SJed Brown     for (c = 0; c < dof; ++c) {
1507552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
1508552f7358SJed Brown 
1509552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
1510552f7358SJed Brown         if (point == join[i][m]) {
1511552f7358SJed Brown           join[1-i][newJoinSize++] = point;
1512552f7358SJed Brown           break;
1513552f7358SJed Brown         }
1514552f7358SJed Brown       }
1515552f7358SJed Brown     }
1516552f7358SJed Brown     joinSize = newJoinSize;
1517552f7358SJed Brown     i        = 1-i;
1518552f7358SJed Brown   }
1519552f7358SJed Brown   *numCoveredPoints = joinSize;
1520552f7358SJed Brown   *coveredPoints    = join[i];
1521552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
1522552f7358SJed Brown   PetscFunctionReturn(0);
1523552f7358SJed Brown }
1524552f7358SJed Brown 
1525552f7358SJed Brown #undef __FUNCT__
1526552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreJoin"
1527552f7358SJed Brown /*@C
1528552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
1529552f7358SJed Brown 
1530552f7358SJed Brown   Not Collective
1531552f7358SJed Brown 
1532552f7358SJed Brown   Input Parameters:
1533552f7358SJed Brown + dm - The DMPlex object
1534552f7358SJed Brown . numPoints - The number of input points for the join
1535552f7358SJed Brown - points - The input points
1536552f7358SJed Brown 
1537552f7358SJed Brown   Output Parameters:
1538552f7358SJed Brown + numCoveredPoints - The number of points in the join
1539552f7358SJed Brown - coveredPoints - The points in the join
1540552f7358SJed Brown 
15413813dfbdSMatthew G Knepley   Fortran Notes:
15423813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
15433813dfbdSMatthew G Knepley   include petsc.h90 in your code.
15443813dfbdSMatthew G Knepley 
15453813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
15463813dfbdSMatthew G Knepley 
1547552f7358SJed Brown   Level: intermediate
1548552f7358SJed Brown 
1549552f7358SJed Brown .keywords: mesh
1550552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
1551552f7358SJed Brown @*/
1552552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
1553552f7358SJed Brown {
1554552f7358SJed Brown   PetscErrorCode ierr;
1555552f7358SJed Brown 
1556552f7358SJed Brown   PetscFunctionBegin;
1557552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1558d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
1559d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
1560d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
1561552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
1562d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
1563552f7358SJed Brown   PetscFunctionReturn(0);
1564552f7358SJed Brown }
1565552f7358SJed Brown 
1566552f7358SJed Brown #undef __FUNCT__
1567552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullJoin"
1568552f7358SJed Brown /*@C
1569552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
1570552f7358SJed Brown 
1571552f7358SJed Brown   Not Collective
1572552f7358SJed Brown 
1573552f7358SJed Brown   Input Parameters:
1574552f7358SJed Brown + dm - The DMPlex object
1575552f7358SJed Brown . numPoints - The number of input points for the join
1576552f7358SJed Brown - points - The input points
1577552f7358SJed Brown 
1578552f7358SJed Brown   Output Parameters:
1579552f7358SJed Brown + numCoveredPoints - The number of points in the join
1580552f7358SJed Brown - coveredPoints - The points in the join
1581552f7358SJed Brown 
15823813dfbdSMatthew G Knepley   Fortran Notes:
15833813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
15843813dfbdSMatthew G Knepley   include petsc.h90 in your code.
15853813dfbdSMatthew G Knepley 
15863813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
15873813dfbdSMatthew G Knepley 
1588552f7358SJed Brown   Level: intermediate
1589552f7358SJed Brown 
1590552f7358SJed Brown .keywords: mesh
1591552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
1592552f7358SJed Brown @*/
1593552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
1594552f7358SJed Brown {
1595552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1596552f7358SJed Brown   PetscInt      *offsets, **closures;
1597552f7358SJed Brown   PetscInt      *join[2];
1598552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
1599552f7358SJed Brown   PetscInt       p, d, c, m;
1600552f7358SJed Brown   PetscErrorCode ierr;
1601552f7358SJed Brown 
1602552f7358SJed Brown   PetscFunctionBegin;
1603552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1604552f7358SJed Brown   PetscValidPointer(points, 2);
1605552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
1606552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
1607552f7358SJed Brown 
1608552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1609*1795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
1610552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
1611e7b80042SMatthew G. Knepley   maxSize = PetscPowInt(mesh->maxSupportSize,depth+1);
1612552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
1613552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
1614552f7358SJed Brown 
1615552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
1616552f7358SJed Brown     PetscInt closureSize;
1617552f7358SJed Brown 
1618552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
16190d644c17SKarl Rupp 
1620552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
1621552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
1622552f7358SJed Brown       PetscInt pStart, pEnd, i;
1623552f7358SJed Brown 
1624552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1625552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
1626552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
1627552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
1628552f7358SJed Brown           break;
1629552f7358SJed Brown         }
1630552f7358SJed Brown       }
1631552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
1632552f7358SJed Brown     }
163382f516ccSBarry 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);
1634552f7358SJed Brown   }
1635552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
1636552f7358SJed Brown     PetscInt dof;
1637552f7358SJed Brown 
1638552f7358SJed Brown     /* Copy in support of first point */
1639552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
1640552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
1641552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
1642552f7358SJed Brown     }
1643552f7358SJed Brown     /* Check each successive cone */
1644552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
1645552f7358SJed Brown       PetscInt newJoinSize = 0;
1646552f7358SJed Brown 
1647552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
1648552f7358SJed Brown       for (c = 0; c < dof; ++c) {
1649552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
1650552f7358SJed Brown 
1651552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
1652552f7358SJed Brown           if (point == join[i][m]) {
1653552f7358SJed Brown             join[1-i][newJoinSize++] = point;
1654552f7358SJed Brown             break;
1655552f7358SJed Brown           }
1656552f7358SJed Brown         }
1657552f7358SJed Brown       }
1658552f7358SJed Brown       joinSize = newJoinSize;
1659552f7358SJed Brown       i        = 1-i;
1660552f7358SJed Brown     }
1661552f7358SJed Brown     if (joinSize) break;
1662552f7358SJed Brown   }
1663552f7358SJed Brown   *numCoveredPoints = joinSize;
1664552f7358SJed Brown   *coveredPoints    = join[i];
1665552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
16660298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
1667552f7358SJed Brown   }
1668552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
1669552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
1670552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
1671552f7358SJed Brown   PetscFunctionReturn(0);
1672552f7358SJed Brown }
1673552f7358SJed Brown 
1674552f7358SJed Brown #undef __FUNCT__
1675552f7358SJed Brown #define __FUNCT__ "DMPlexGetMeet"
1676552f7358SJed Brown /*@C
1677552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
1678552f7358SJed Brown 
1679552f7358SJed Brown   Not Collective
1680552f7358SJed Brown 
1681552f7358SJed Brown   Input Parameters:
1682552f7358SJed Brown + dm - The DMPlex object
1683552f7358SJed Brown . numPoints - The number of input points for the meet
1684552f7358SJed Brown - points - The input points
1685552f7358SJed Brown 
1686552f7358SJed Brown   Output Parameters:
1687552f7358SJed Brown + numCoveredPoints - The number of points in the meet
1688552f7358SJed Brown - coveredPoints - The points in the meet
1689552f7358SJed Brown 
1690552f7358SJed Brown   Level: intermediate
1691552f7358SJed Brown 
1692552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
1693552f7358SJed Brown 
16943813dfbdSMatthew G Knepley   Fortran Notes:
16953813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16963813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16973813dfbdSMatthew G Knepley 
16983813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
16993813dfbdSMatthew G Knepley 
1700552f7358SJed Brown .keywords: mesh
1701552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
1702552f7358SJed Brown @*/
1703552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
1704552f7358SJed Brown {
1705552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1706552f7358SJed Brown   PetscInt      *meet[2];
1707552f7358SJed Brown   PetscInt       meetSize, i = 0;
1708552f7358SJed Brown   PetscInt       dof, off, p, c, m;
1709552f7358SJed Brown   PetscErrorCode ierr;
1710552f7358SJed Brown 
1711552f7358SJed Brown   PetscFunctionBegin;
1712552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1713552f7358SJed Brown   PetscValidPointer(points, 2);
1714552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
1715552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
1716552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
1717552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
1718552f7358SJed Brown   /* Copy in cone of first point */
1719552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
1720552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
1721552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
1722552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
1723552f7358SJed Brown   }
1724552f7358SJed Brown   /* Check each successive cone */
1725552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
1726552f7358SJed Brown     PetscInt newMeetSize = 0;
1727552f7358SJed Brown 
1728552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
1729552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
1730552f7358SJed Brown     for (c = 0; c < dof; ++c) {
1731552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
1732552f7358SJed Brown 
1733552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
1734552f7358SJed Brown         if (point == meet[i][m]) {
1735552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
1736552f7358SJed Brown           break;
1737552f7358SJed Brown         }
1738552f7358SJed Brown       }
1739552f7358SJed Brown     }
1740552f7358SJed Brown     meetSize = newMeetSize;
1741552f7358SJed Brown     i        = 1-i;
1742552f7358SJed Brown   }
1743552f7358SJed Brown   *numCoveringPoints = meetSize;
1744552f7358SJed Brown   *coveringPoints    = meet[i];
1745552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
1746552f7358SJed Brown   PetscFunctionReturn(0);
1747552f7358SJed Brown }
1748552f7358SJed Brown 
1749552f7358SJed Brown #undef __FUNCT__
1750552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreMeet"
1751552f7358SJed Brown /*@C
1752552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
1753552f7358SJed Brown 
1754552f7358SJed Brown   Not Collective
1755552f7358SJed Brown 
1756552f7358SJed Brown   Input Parameters:
1757552f7358SJed Brown + dm - The DMPlex object
1758552f7358SJed Brown . numPoints - The number of input points for the meet
1759552f7358SJed Brown - points - The input points
1760552f7358SJed Brown 
1761552f7358SJed Brown   Output Parameters:
1762552f7358SJed Brown + numCoveredPoints - The number of points in the meet
1763552f7358SJed Brown - coveredPoints - The points in the meet
1764552f7358SJed Brown 
1765552f7358SJed Brown   Level: intermediate
1766552f7358SJed Brown 
17673813dfbdSMatthew G Knepley   Fortran Notes:
17683813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17693813dfbdSMatthew G Knepley   include petsc.h90 in your code.
17703813dfbdSMatthew G Knepley 
17713813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17723813dfbdSMatthew G Knepley 
1773552f7358SJed Brown .keywords: mesh
1774552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
1775552f7358SJed Brown @*/
1776552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
1777552f7358SJed Brown {
1778552f7358SJed Brown   PetscErrorCode ierr;
1779552f7358SJed Brown 
1780552f7358SJed Brown   PetscFunctionBegin;
1781552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1782d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
1783d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
1784d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
1785552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
1786d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
1787552f7358SJed Brown   PetscFunctionReturn(0);
1788552f7358SJed Brown }
1789552f7358SJed Brown 
1790552f7358SJed Brown #undef __FUNCT__
1791552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullMeet"
1792552f7358SJed Brown /*@C
1793552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
1794552f7358SJed Brown 
1795552f7358SJed Brown   Not Collective
1796552f7358SJed Brown 
1797552f7358SJed Brown   Input Parameters:
1798552f7358SJed Brown + dm - The DMPlex object
1799552f7358SJed Brown . numPoints - The number of input points for the meet
1800552f7358SJed Brown - points - The input points
1801552f7358SJed Brown 
1802552f7358SJed Brown   Output Parameters:
1803552f7358SJed Brown + numCoveredPoints - The number of points in the meet
1804552f7358SJed Brown - coveredPoints - The points in the meet
1805552f7358SJed Brown 
1806552f7358SJed Brown   Level: intermediate
1807552f7358SJed Brown 
18083813dfbdSMatthew G Knepley   Fortran Notes:
18093813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18103813dfbdSMatthew G Knepley   include petsc.h90 in your code.
18113813dfbdSMatthew G Knepley 
18123813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
18133813dfbdSMatthew G Knepley 
1814552f7358SJed Brown .keywords: mesh
1815552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
1816552f7358SJed Brown @*/
1817552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
1818552f7358SJed Brown {
1819552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1820552f7358SJed Brown   PetscInt      *offsets, **closures;
1821552f7358SJed Brown   PetscInt      *meet[2];
1822552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
1823552f7358SJed Brown   PetscInt       p, h, c, m;
1824552f7358SJed Brown   PetscErrorCode ierr;
1825552f7358SJed Brown 
1826552f7358SJed Brown   PetscFunctionBegin;
1827552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1828552f7358SJed Brown   PetscValidPointer(points, 2);
1829552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
1830552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
1831552f7358SJed Brown 
1832552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
1833785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
1834552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
1835e7b80042SMatthew G. Knepley   maxSize = PetscPowInt(mesh->maxConeSize,height+1);
1836552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
1837552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
1838552f7358SJed Brown 
1839552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
1840552f7358SJed Brown     PetscInt closureSize;
1841552f7358SJed Brown 
1842552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
18430d644c17SKarl Rupp 
1844552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
1845552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
1846552f7358SJed Brown       PetscInt pStart, pEnd, i;
1847552f7358SJed Brown 
1848552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
1849552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
1850552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
1851552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
1852552f7358SJed Brown           break;
1853552f7358SJed Brown         }
1854552f7358SJed Brown       }
1855552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
1856552f7358SJed Brown     }
185782f516ccSBarry 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);
1858552f7358SJed Brown   }
1859552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
1860552f7358SJed Brown     PetscInt dof;
1861552f7358SJed Brown 
1862552f7358SJed Brown     /* Copy in cone of first point */
1863552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
1864552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
1865552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
1866552f7358SJed Brown     }
1867552f7358SJed Brown     /* Check each successive cone */
1868552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
1869552f7358SJed Brown       PetscInt newMeetSize = 0;
1870552f7358SJed Brown 
1871552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
1872552f7358SJed Brown       for (c = 0; c < dof; ++c) {
1873552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
1874552f7358SJed Brown 
1875552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
1876552f7358SJed Brown           if (point == meet[i][m]) {
1877552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
1878552f7358SJed Brown             break;
1879552f7358SJed Brown           }
1880552f7358SJed Brown         }
1881552f7358SJed Brown       }
1882552f7358SJed Brown       meetSize = newMeetSize;
1883552f7358SJed Brown       i        = 1-i;
1884552f7358SJed Brown     }
1885552f7358SJed Brown     if (meetSize) break;
1886552f7358SJed Brown   }
1887552f7358SJed Brown   *numCoveredPoints = meetSize;
1888552f7358SJed Brown   *coveredPoints    = meet[i];
1889552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
18900298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
1891552f7358SJed Brown   }
1892552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
1893552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
1894552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
1895552f7358SJed Brown   PetscFunctionReturn(0);
1896552f7358SJed Brown }
1897552f7358SJed Brown 
1898552f7358SJed Brown #undef __FUNCT__
18994e3744c5SMatthew G. Knepley #define __FUNCT__ "DMPlexEqual"
19004e3744c5SMatthew G. Knepley /*@C
19014e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
19024e3744c5SMatthew G. Knepley 
19034e3744c5SMatthew G. Knepley   Not Collective
19044e3744c5SMatthew G. Knepley 
19054e3744c5SMatthew G. Knepley   Input Parameters:
19064e3744c5SMatthew G. Knepley + dmA - A DMPlex object
19074e3744c5SMatthew G. Knepley - dmB - A DMPlex object
19084e3744c5SMatthew G. Knepley 
19094e3744c5SMatthew G. Knepley   Output Parameters:
19104e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
19114e3744c5SMatthew G. Knepley 
19124e3744c5SMatthew G. Knepley   Level: intermediate
19134e3744c5SMatthew G. Knepley 
19144e3744c5SMatthew G. Knepley   Notes:
19154e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
19164e3744c5SMatthew G. Knepley 
19174e3744c5SMatthew G. Knepley .keywords: mesh
19184e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
19194e3744c5SMatthew G. Knepley @*/
19204e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
19214e3744c5SMatthew G. Knepley {
19224e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
19234e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
19244e3744c5SMatthew G. Knepley 
19254e3744c5SMatthew G. Knepley   PetscFunctionBegin;
19264e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
19274e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
19284e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
19294e3744c5SMatthew G. Knepley 
19304e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
19314e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
19324e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
19334e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
19344e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
19354e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
19364e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
19374e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
19384e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
19394e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
19404e3744c5SMatthew G. Knepley 
19414e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
19424e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
19434e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
19444e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
19454e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
19464e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
19474e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
19484e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
19494e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
19504e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
19514e3744c5SMatthew G. Knepley     }
19524e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
19534e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
19544e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
19554e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
19564e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
19574e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
19584e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
19594e3744c5SMatthew G. Knepley     }
19604e3744c5SMatthew G. Knepley   }
19614e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
19624e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
19634e3744c5SMatthew G. Knepley }
19644e3744c5SMatthew G. Knepley 
19654e3744c5SMatthew G. Knepley #undef __FUNCT__
196618ad9376SMatthew G. Knepley #define __FUNCT__ "DMPlexGetNumFaceVertices"
196718ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
1968a6dfd86eSKarl Rupp {
196982f516ccSBarry Smith   MPI_Comm       comm;
1970552f7358SJed Brown   PetscErrorCode ierr;
1971552f7358SJed Brown 
1972552f7358SJed Brown   PetscFunctionBegin;
197382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1974552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
1975552f7358SJed Brown   switch (cellDim) {
1976552f7358SJed Brown   case 0:
1977552f7358SJed Brown     *numFaceVertices = 0;
1978552f7358SJed Brown     break;
1979552f7358SJed Brown   case 1:
1980552f7358SJed Brown     *numFaceVertices = 1;
1981552f7358SJed Brown     break;
1982552f7358SJed Brown   case 2:
1983552f7358SJed Brown     switch (numCorners) {
198419436ca2SJed Brown     case 3: /* triangle */
198519436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
1986552f7358SJed Brown       break;
198719436ca2SJed Brown     case 4: /* quadrilateral */
198819436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
1989552f7358SJed Brown       break;
199019436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
199119436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
1992552f7358SJed Brown       break;
199319436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
199419436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
1995552f7358SJed Brown       break;
1996552f7358SJed Brown     default:
1997552f7358SJed Brown       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %d for dimension %d", numCorners, cellDim);
1998552f7358SJed Brown     }
1999552f7358SJed Brown     break;
2000552f7358SJed Brown   case 3:
2001552f7358SJed Brown     switch (numCorners) {
200219436ca2SJed Brown     case 4: /* tetradehdron */
200319436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2004552f7358SJed Brown       break;
200519436ca2SJed Brown     case 6: /* tet cohesive cells */
200619436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2007552f7358SJed Brown       break;
200819436ca2SJed Brown     case 8: /* hexahedron */
200919436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2010552f7358SJed Brown       break;
201119436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
201219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2013552f7358SJed Brown       break;
201419436ca2SJed Brown     case 10: /* quadratic tetrahedron */
201519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2016552f7358SJed Brown       break;
201719436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
201819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2019552f7358SJed Brown       break;
202019436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
202119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2022552f7358SJed Brown       break;
202319436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
202419436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2025552f7358SJed Brown       break;
2026552f7358SJed Brown     default:
2027552f7358SJed Brown       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %d for dimension %d", numCorners, cellDim);
2028552f7358SJed Brown     }
2029552f7358SJed Brown     break;
2030552f7358SJed Brown   default:
2031552f7358SJed Brown     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %d", cellDim);
2032552f7358SJed Brown   }
2033552f7358SJed Brown   PetscFunctionReturn(0);
2034552f7358SJed Brown }
2035552f7358SJed Brown 
2036552f7358SJed Brown #undef __FUNCT__
2037d27a0f52SMatthew G. Knepley #define __FUNCT__ "DMPlexOrient"
2038d27a0f52SMatthew G. Knepley /* Trys to give the mesh a consistent orientation */
2039d27a0f52SMatthew G. Knepley PetscErrorCode DMPlexOrient(DM dm)
2040d27a0f52SMatthew G. Knepley {
2041a1b4d4a2SMatthew G. Knepley   PetscBT        seenCells, flippedCells, seenFaces;
2042a1b4d4a2SMatthew G. Knepley   PetscInt      *faceFIFO, fTop, fBottom;
2043a1b4d4a2SMatthew G. Knepley   PetscInt       dim, h, cStart, cEnd, c, fStart, fEnd, face, maxConeSize, *revcone, *revconeO;
2044d27a0f52SMatthew G. Knepley   PetscErrorCode ierr;
2045d27a0f52SMatthew G. Knepley 
2046d27a0f52SMatthew G. Knepley   PetscFunctionBegin;
2047d27a0f52SMatthew G. Knepley   /* Truth Table
2048d27a0f52SMatthew G. Knepley      mismatch    flips   do action   mismatch   flipA ^ flipB   action
2049d27a0f52SMatthew G. Knepley          F       0 flips     no         F             F           F
2050d27a0f52SMatthew G. Knepley          F       1 flip      yes        F             T           T
2051d27a0f52SMatthew G. Knepley          F       2 flips     no         T             F           T
2052d27a0f52SMatthew G. Knepley          T       0 flips     yes        T             T           F
2053d27a0f52SMatthew G. Knepley          T       1 flip      no
2054d27a0f52SMatthew G. Knepley          T       2 flips     yes
2055d27a0f52SMatthew G. Knepley   */
2056a1b4d4a2SMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
2057d27a0f52SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &h);CHKERRQ(ierr);
2058a1b4d4a2SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, h,   &cStart, &cEnd);CHKERRQ(ierr);
2059d27a0f52SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, h+1, &fStart, &fEnd);CHKERRQ(ierr);
2060a1b4d4a2SMatthew G. Knepley   ierr = PetscBTCreate(cEnd - cStart, &seenCells);CHKERRQ(ierr);
2061a1b4d4a2SMatthew G. Knepley   ierr = PetscBTMemzero(cEnd - cStart, seenCells);CHKERRQ(ierr);
2062a1b4d4a2SMatthew G. Knepley   ierr = PetscBTCreate(cEnd - cStart, &flippedCells);CHKERRQ(ierr);
2063a1b4d4a2SMatthew G. Knepley   ierr = PetscBTMemzero(cEnd - cStart, flippedCells);CHKERRQ(ierr);
2064a1b4d4a2SMatthew G. Knepley   ierr = PetscBTCreate(fEnd - fStart, &seenFaces);CHKERRQ(ierr);
2065a1b4d4a2SMatthew G. Knepley   ierr = PetscBTMemzero(fEnd - fStart, seenFaces);CHKERRQ(ierr);
2066785e854fSJed Brown   ierr = PetscMalloc1((fEnd - fStart), &faceFIFO);CHKERRQ(ierr);
2067a1b4d4a2SMatthew G. Knepley   fTop = fBottom = 0;
2068a1b4d4a2SMatthew G. Knepley   /* Initialize FIFO with first cell */
2069defeeaeaSMatthew G. Knepley   if (cEnd > cStart) {
2070a1b4d4a2SMatthew G. Knepley     const PetscInt *cone;
2071a1b4d4a2SMatthew G. Knepley     PetscInt        coneSize;
2072a1b4d4a2SMatthew G. Knepley 
2073a1b4d4a2SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cStart, &coneSize);CHKERRQ(ierr);
2074a1b4d4a2SMatthew G. Knepley     ierr = DMPlexGetCone(dm, cStart, &cone);CHKERRQ(ierr);
2075a1b4d4a2SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
2076a1b4d4a2SMatthew G. Knepley       faceFIFO[fBottom++] = cone[c];
2077a1b4d4a2SMatthew G. Knepley       ierr = PetscBTSet(seenFaces, cone[c]-fStart);CHKERRQ(ierr);
2078a1b4d4a2SMatthew G. Knepley     }
2079a1b4d4a2SMatthew G. Knepley   }
2080a1b4d4a2SMatthew G. Knepley   /* Consider each face in FIFO */
2081a1b4d4a2SMatthew G. Knepley   while (fTop < fBottom) {
2082d27a0f52SMatthew G. Knepley     const PetscInt *support, *coneA, *coneB, *coneOA, *coneOB;
2083a1b4d4a2SMatthew G. Knepley     PetscInt        supportSize, coneSizeA, coneSizeB, posA = -1, posB = -1;
2084d27a0f52SMatthew G. Knepley     PetscInt        seenA, flippedA, seenB, flippedB, mismatch;
2085d27a0f52SMatthew G. Knepley 
2086a1b4d4a2SMatthew G. Knepley     face = faceFIFO[fTop++];
2087a1b4d4a2SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2088a1b4d4a2SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2089d27a0f52SMatthew G. Knepley     if (supportSize < 2) continue;
2090d27a0f52SMatthew G. Knepley     if (supportSize != 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Faces should separate only two cells, not %d", supportSize);
2091a1b4d4a2SMatthew G. Knepley     seenA    = PetscBTLookup(seenCells,    support[0]-cStart);
209286cff514SMatthew G. Knepley     flippedA = PetscBTLookup(flippedCells, support[0]-cStart) ? 1 : 0;
2093a1b4d4a2SMatthew G. Knepley     seenB    = PetscBTLookup(seenCells,    support[1]-cStart);
209486cff514SMatthew G. Knepley     flippedB = PetscBTLookup(flippedCells, support[1]-cStart) ? 1 : 0;
2095d27a0f52SMatthew G. Knepley 
2096d27a0f52SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, support[0], &coneSizeA);CHKERRQ(ierr);
2097d27a0f52SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, support[1], &coneSizeB);CHKERRQ(ierr);
2098d27a0f52SMatthew G. Knepley     ierr = DMPlexGetCone(dm, support[0], &coneA);CHKERRQ(ierr);
2099d27a0f52SMatthew G. Knepley     ierr = DMPlexGetCone(dm, support[1], &coneB);CHKERRQ(ierr);
2100d27a0f52SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, support[0], &coneOA);CHKERRQ(ierr);
2101d27a0f52SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, support[1], &coneOB);CHKERRQ(ierr);
2102a1b4d4a2SMatthew G. Knepley     for (c = 0; c < coneSizeA; ++c) {
2103a1b4d4a2SMatthew G. Knepley       if (!PetscBTLookup(seenFaces, coneA[c]-fStart)) {
2104a1b4d4a2SMatthew G. Knepley         faceFIFO[fBottom++] = coneA[c];
2105a1b4d4a2SMatthew G. Knepley         ierr = PetscBTSet(seenFaces, coneA[c]-fStart);CHKERRQ(ierr);
2106a1b4d4a2SMatthew G. Knepley       }
2107a1b4d4a2SMatthew G. Knepley       if (coneA[c] == face) posA = c;
2108a1b4d4a2SMatthew G. Knepley       if (fBottom > fEnd-fStart) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Face %d was pushed exceeding capacity %d > %d", coneA[c], fBottom, fEnd-fStart);
2109a1b4d4a2SMatthew G. Knepley     }
2110a1b4d4a2SMatthew G. Knepley     if (posA < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d could not be located in cell %d", face, support[0]);
2111a1b4d4a2SMatthew G. Knepley     for (c = 0; c < coneSizeB; ++c) {
2112a1b4d4a2SMatthew G. Knepley       if (!PetscBTLookup(seenFaces, coneB[c]-fStart)) {
2113a1b4d4a2SMatthew G. Knepley         faceFIFO[fBottom++] = coneB[c];
2114a1b4d4a2SMatthew G. Knepley         ierr = PetscBTSet(seenFaces, coneB[c]-fStart);CHKERRQ(ierr);
2115a1b4d4a2SMatthew G. Knepley       }
2116a1b4d4a2SMatthew G. Knepley       if (coneB[c] == face) posB = c;
2117a1b4d4a2SMatthew G. Knepley       if (fBottom > fEnd-fStart) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Face %d was pushed exceeding capacity %d > %d", coneA[c], fBottom, fEnd-fStart);
2118a1b4d4a2SMatthew G. Knepley     }
2119a1b4d4a2SMatthew G. Knepley     if (posB < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d could not be located in cell %d", face, support[1]);
2120d27a0f52SMatthew G. Knepley 
2121d27a0f52SMatthew G. Knepley     if (dim == 1) {
2122d27a0f52SMatthew G. Knepley       mismatch = posA == posB;
2123d27a0f52SMatthew G. Knepley     } else {
2124d27a0f52SMatthew G. Knepley       mismatch = coneOA[posA] == coneOB[posB];
2125d27a0f52SMatthew G. Knepley     }
2126d27a0f52SMatthew G. Knepley 
2127d27a0f52SMatthew G. Knepley     if (mismatch ^ (flippedA ^ flippedB)) {
2128d27a0f52SMatthew G. Knepley       if (seenA && seenB) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Previously seen cells %d and %d do not match: Fault mesh is non-orientable", support[0], support[1]);
2129d27a0f52SMatthew G. Knepley       if (!seenA && !flippedA) {
2130a1b4d4a2SMatthew G. Knepley         ierr = PetscBTSet(flippedCells, support[0]-cStart);CHKERRQ(ierr);
2131d27a0f52SMatthew G. Knepley       } else if (!seenB && !flippedB) {
2132a1b4d4a2SMatthew G. Knepley         ierr = PetscBTSet(flippedCells, support[1]-cStart);CHKERRQ(ierr);
2133d27a0f52SMatthew G. Knepley       } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Inconsistent mesh orientation: Fault mesh is non-orientable");
213486cff514SMatthew G. Knepley     } else if (mismatch && flippedA && flippedB) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Attempt to flip already flipped cell: Fault mesh is non-orientable");
2135a1b4d4a2SMatthew G. Knepley     ierr = PetscBTSet(seenCells, support[0]-cStart);CHKERRQ(ierr);
2136a1b4d4a2SMatthew G. Knepley     ierr = PetscBTSet(seenCells, support[1]-cStart);CHKERRQ(ierr);
2137d27a0f52SMatthew G. Knepley   }
2138a1b4d4a2SMatthew G. Knepley 
2139d27a0f52SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
2140d27a0f52SMatthew G. Knepley   ierr = DMGetWorkArray(dm, maxConeSize, PETSC_INT, &revcone);CHKERRQ(ierr);
2141d27a0f52SMatthew G. Knepley   ierr = DMGetWorkArray(dm, maxConeSize, PETSC_INT, &revconeO);CHKERRQ(ierr);
2142a1b4d4a2SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2143f4e909fcSMatthew G. Knepley     const PetscInt *cone, *coneO, *support;
2144f4e909fcSMatthew G. Knepley     PetscInt        coneSize, supportSize, faceSize, cp, sp;
2145d27a0f52SMatthew G. Knepley 
2146a1b4d4a2SMatthew G. Knepley     if (!PetscBTLookup(flippedCells, c-cStart)) continue;
2147a1b4d4a2SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2148a1b4d4a2SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2149a1b4d4a2SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, c, &coneO);CHKERRQ(ierr);
2150a1b4d4a2SMatthew G. Knepley     for (cp = 0; cp < coneSize; ++cp) {
2151a1b4d4a2SMatthew G. Knepley       const PetscInt rcp = coneSize-cp-1;
2152a1b4d4a2SMatthew G. Knepley 
2153a1b4d4a2SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cone[rcp], &faceSize);CHKERRQ(ierr);
2154a1b4d4a2SMatthew G. Knepley       revcone[cp]  = cone[rcp];
2155a1b4d4a2SMatthew G. Knepley       revconeO[cp] = coneO[rcp] >= 0 ? -(faceSize-coneO[rcp]) : faceSize+coneO[rcp];
2156d27a0f52SMatthew G. Knepley     }
2157a1b4d4a2SMatthew G. Knepley     ierr = DMPlexSetCone(dm, c, revcone);CHKERRQ(ierr);
2158a1b4d4a2SMatthew G. Knepley     ierr = DMPlexSetConeOrientation(dm, c, revconeO);CHKERRQ(ierr);
2159f4e909fcSMatthew G. Knepley     /* Reverse orientations of support */
2160f4e909fcSMatthew G. Knepley     faceSize = coneSize;
2161f4e909fcSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, c, &supportSize);CHKERRQ(ierr);
2162f4e909fcSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, c, &support);CHKERRQ(ierr);
2163f4e909fcSMatthew G. Knepley     for (sp = 0; sp < supportSize; ++sp) {
2164f4e909fcSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[sp], &coneSize);CHKERRQ(ierr);
2165f4e909fcSMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[sp], &cone);CHKERRQ(ierr);
2166f4e909fcSMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, support[sp], &coneO);CHKERRQ(ierr);
2167f4e909fcSMatthew G. Knepley       for (cp = 0; cp < coneSize; ++cp) {
2168f4e909fcSMatthew G. Knepley         if (cone[cp] != c) continue;
2169f4e909fcSMatthew G. Knepley         ierr = DMPlexInsertConeOrientation(dm, support[sp], cp, coneO[cp] >= 0 ? -(faceSize-coneO[cp]) : faceSize+coneO[cp]);CHKERRQ(ierr);
2170f4e909fcSMatthew G. Knepley       }
2171f4e909fcSMatthew G. Knepley     }
2172d27a0f52SMatthew G. Knepley   }
2173d27a0f52SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxConeSize, PETSC_INT, &revcone);CHKERRQ(ierr);
2174d27a0f52SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxConeSize, PETSC_INT, &revconeO);CHKERRQ(ierr);
2175d27a0f52SMatthew G. Knepley   ierr = PetscBTDestroy(&seenCells);CHKERRQ(ierr);
2176a1b4d4a2SMatthew G. Knepley   ierr = PetscBTDestroy(&flippedCells);CHKERRQ(ierr);
2177a1b4d4a2SMatthew G. Knepley   ierr = PetscBTDestroy(&seenFaces);CHKERRQ(ierr);
2178a1b4d4a2SMatthew G. Knepley   ierr = PetscFree(faceFIFO);CHKERRQ(ierr);
2179d27a0f52SMatthew G. Knepley   PetscFunctionReturn(0);
2180d27a0f52SMatthew G. Knepley }
2181d27a0f52SMatthew G. Knepley 
2182d27a0f52SMatthew G. Knepley #undef __FUNCT__
2183cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetAdjacencySingleLevel_Internal"
2184cb1e1211SMatthew G Knepley static PetscErrorCode DMPlexGetAdjacencySingleLevel_Internal(DM dm, PetscInt p, PetscBool useClosure, const PetscInt *tmpClosure, PetscInt *adjSize, PetscInt adj[])
2185cb1e1211SMatthew G Knepley {
2186cb1e1211SMatthew G Knepley   const PetscInt *support = NULL;
2187cb1e1211SMatthew G Knepley   PetscInt        numAdj   = 0, maxAdjSize = *adjSize, supportSize, s;
2188cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
2189cb1e1211SMatthew G Knepley 
2190cb1e1211SMatthew G Knepley   PetscFunctionBegin;
2191cb1e1211SMatthew G Knepley   if (useClosure) {
2192cb1e1211SMatthew G Knepley     ierr = DMPlexGetConeSize(dm, p, &supportSize);CHKERRQ(ierr);
2193cb1e1211SMatthew G Knepley     ierr = DMPlexGetCone(dm, p, &support);CHKERRQ(ierr);
2194cb1e1211SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
2195cb1e1211SMatthew G Knepley       const PetscInt *cone = NULL;
2196cb1e1211SMatthew G Knepley       PetscInt        coneSize, c, q;
2197cb1e1211SMatthew G Knepley 
2198cb1e1211SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, support[s], &coneSize);CHKERRQ(ierr);
2199cb1e1211SMatthew G Knepley       ierr = DMPlexGetSupport(dm, support[s], &cone);CHKERRQ(ierr);
2200cb1e1211SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2201cb1e1211SMatthew G Knepley         for (q = 0; q < numAdj || (adj[numAdj++] = cone[c],0); ++q) {
2202cb1e1211SMatthew G Knepley           if (cone[c] == adj[q]) break;
2203cb1e1211SMatthew G Knepley         }
2204cb1e1211SMatthew G Knepley         if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
2205cb1e1211SMatthew G Knepley       }
2206cb1e1211SMatthew G Knepley     }
2207cb1e1211SMatthew G Knepley   } else {
2208cb1e1211SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2209cb1e1211SMatthew G Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
2210cb1e1211SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
2211cb1e1211SMatthew G Knepley       const PetscInt *cone = NULL;
2212cb1e1211SMatthew G Knepley       PetscInt        coneSize, c, q;
2213cb1e1211SMatthew G Knepley 
2214cb1e1211SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
2215cb1e1211SMatthew G Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
2216cb1e1211SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2217cb1e1211SMatthew G Knepley         for (q = 0; q < numAdj || (adj[numAdj++] = cone[c],0); ++q) {
2218cb1e1211SMatthew G Knepley           if (cone[c] == adj[q]) break;
2219cb1e1211SMatthew G Knepley         }
2220cb1e1211SMatthew G Knepley         if (numAdj > maxAdjSize) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid mesh exceeded adjacency allocation (%D)", maxAdjSize);
2221cb1e1211SMatthew G Knepley       }
2222cb1e1211SMatthew G Knepley     }
2223cb1e1211SMatthew G Knepley   }
2224cb1e1211SMatthew G Knepley   *adjSize = numAdj;
2225cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2226cb1e1211SMatthew G Knepley }
2227cb1e1211SMatthew G Knepley 
2228cb1e1211SMatthew G Knepley #undef __FUNCT__
2229552f7358SJed Brown #define __FUNCT__ "DMPlexCreateNeighborCSR"
223077c88f5bSMatthew G Knepley PetscErrorCode DMPlexCreateNeighborCSR(DM dm, PetscInt cellHeight, PetscInt *numVertices, PetscInt **offsets, PetscInt **adjacency)
22310adebc6cSBarry Smith {
2232552f7358SJed Brown   const PetscInt maxFaceCases = 30;
2233552f7358SJed Brown   PetscInt       numFaceCases = 0;
2234552f7358SJed Brown   PetscInt       numFaceVertices[30]; /* maxFaceCases, C89 sucks sucks sucks */
2235552f7358SJed Brown   PetscInt      *off, *adj;
2236552f7358SJed Brown   PetscInt      *neighborCells, *tmpClosure;
2237552f7358SJed Brown   PetscInt       maxConeSize, maxSupportSize, maxClosure, maxNeighbors;
223877c88f5bSMatthew G Knepley   PetscInt       dim, cellDim, depth = 0, faceDepth, cStart, cEnd, c, numCells, cell;
2239552f7358SJed Brown   PetscErrorCode ierr;
2240552f7358SJed Brown 
2241552f7358SJed Brown   PetscFunctionBegin;
2242552f7358SJed Brown   /* For parallel partitioning, I think you have to communicate supports */
2243552f7358SJed Brown   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
224477c88f5bSMatthew G Knepley   cellDim = dim - cellHeight;
2245552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
224677c88f5bSMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
2247552f7358SJed Brown   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
2248552f7358SJed Brown   if (cEnd - cStart == 0) {
2249552f7358SJed Brown     if (numVertices) *numVertices = 0;
22500298fd71SBarry Smith     if (offsets)   *offsets   = NULL;
22510298fd71SBarry Smith     if (adjacency) *adjacency = NULL;
2252552f7358SJed Brown     PetscFunctionReturn(0);
2253552f7358SJed Brown   }
2254552f7358SJed Brown   numCells  = cEnd - cStart;
225577c88f5bSMatthew G Knepley   faceDepth = depth - cellHeight;
22560d957afbSMatthew G. Knepley   if (dim == depth) {
2257e430e843SMatthew G. Knepley     PetscInt f, fStart, fEnd;
2258e430e843SMatthew G. Knepley 
2259*1795a4d1SJed Brown     ierr = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
2260e430e843SMatthew G. Knepley     /* Count neighboring cells */
2261e430e843SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr);
2262e430e843SMatthew G. Knepley     for (f = fStart; f < fEnd; ++f) {
2263e430e843SMatthew G. Knepley       const PetscInt *support;
2264e430e843SMatthew G. Knepley       PetscInt        supportSize;
2265e430e843SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
2266e430e843SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
2267e430e843SMatthew G. Knepley       if (supportSize == 2) {
2268e430e843SMatthew G. Knepley         ++off[support[0]-cStart+1];
2269e430e843SMatthew G. Knepley         ++off[support[1]-cStart+1];
2270e430e843SMatthew G. Knepley       }
2271e430e843SMatthew G. Knepley     }
2272e430e843SMatthew G. Knepley     /* Prefix sum */
2273e430e843SMatthew G. Knepley     for (c = 1; c <= numCells; ++c) off[c] += off[c-1];
2274e430e843SMatthew G. Knepley     if (adjacency) {
2275e430e843SMatthew G. Knepley       PetscInt *tmp;
2276e430e843SMatthew G. Knepley 
2277785e854fSJed Brown       ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
2278785e854fSJed Brown       ierr = PetscMalloc1((numCells+1), &tmp);CHKERRQ(ierr);
2279e430e843SMatthew G. Knepley       ierr = PetscMemcpy(tmp, off, (numCells+1) * sizeof(PetscInt));CHKERRQ(ierr);
2280e430e843SMatthew G. Knepley       /* Get neighboring cells */
2281e430e843SMatthew G. Knepley       for (f = fStart; f < fEnd; ++f) {
2282e430e843SMatthew G. Knepley         const PetscInt *support;
2283e430e843SMatthew G. Knepley         PetscInt        supportSize;
2284e430e843SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
2285e430e843SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, f, &support);CHKERRQ(ierr);
2286e430e843SMatthew G. Knepley         if (supportSize == 2) {
2287e430e843SMatthew G. Knepley           adj[tmp[support[0]-cStart]++] = support[1];
2288e430e843SMatthew G. Knepley           adj[tmp[support[1]-cStart]++] = support[0];
2289e430e843SMatthew G. Knepley         }
2290e430e843SMatthew G. Knepley       }
2291e430e843SMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
22920d957afbSMatthew G. Knepley       for (c = 0; c < cEnd-cStart; ++c) if (tmp[c] != off[c+1]) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Offset %d != %d for cell %d", tmp[c], off[c], c+cStart);
2293e430e843SMatthew G. Knepley #endif
2294e430e843SMatthew G. Knepley       ierr = PetscFree(tmp);CHKERRQ(ierr);
2295e430e843SMatthew G. Knepley     }
2296e430e843SMatthew G. Knepley     if (numVertices) *numVertices = numCells;
2297e430e843SMatthew G. Knepley     if (offsets)   *offsets   = off;
2298e430e843SMatthew G. Knepley     if (adjacency) *adjacency = adj;
2299e430e843SMatthew G. Knepley     PetscFunctionReturn(0);
2300e430e843SMatthew G. Knepley   }
2301552f7358SJed Brown   /* Setup face recognition */
230277c88f5bSMatthew G Knepley   if (faceDepth == 1) {
2303552f7358SJed Brown     PetscInt cornersSeen[30] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* Could use PetscBT */
2304552f7358SJed Brown 
2305552f7358SJed Brown     for (c = cStart; c < cEnd; ++c) {
2306552f7358SJed Brown       PetscInt corners;
2307552f7358SJed Brown 
2308552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, c, &corners);CHKERRQ(ierr);
2309552f7358SJed Brown       if (!cornersSeen[corners]) {
2310552f7358SJed Brown         PetscInt nFV;
2311552f7358SJed Brown 
231282f516ccSBarry Smith         if (numFaceCases >= maxFaceCases) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Exceeded maximum number of face recognition cases");
2313552f7358SJed Brown         cornersSeen[corners] = 1;
23140d644c17SKarl Rupp 
231518ad9376SMatthew G. Knepley         ierr = DMPlexGetNumFaceVertices(dm, cellDim, corners, &nFV);CHKERRQ(ierr);
23160d644c17SKarl Rupp 
2317552f7358SJed Brown         numFaceVertices[numFaceCases++] = nFV;
2318552f7358SJed Brown       }
2319552f7358SJed Brown     }
2320552f7358SJed Brown   }
2321e7b80042SMatthew G. Knepley   maxClosure   = 2*PetscMax(PetscPowInt(maxConeSize,depth+1),PetscPowInt(maxSupportSize,depth+1));
2322e7b80042SMatthew G. Knepley   maxNeighbors = PetscPowInt(maxConeSize,depth+1)*PetscPowInt(maxSupportSize,depth+1);
2323dcca6d9dSJed Brown   ierr         = PetscMalloc2(maxNeighbors,&neighborCells,maxClosure,&tmpClosure);CHKERRQ(ierr);
2324*1795a4d1SJed Brown   ierr         = PetscCalloc1(numCells+1, &off);CHKERRQ(ierr);
2325552f7358SJed Brown   /* Count neighboring cells */
2326552f7358SJed Brown   for (cell = cStart; cell < cEnd; ++cell) {
2327552f7358SJed Brown     PetscInt numNeighbors = maxNeighbors, n;
2328552f7358SJed Brown 
2329cb1e1211SMatthew G Knepley     ierr = DMPlexGetAdjacencySingleLevel_Internal(dm, cell, PETSC_TRUE, tmpClosure, &numNeighbors, neighborCells);CHKERRQ(ierr);
2330552f7358SJed Brown     /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
2331552f7358SJed Brown     for (n = 0; n < numNeighbors; ++n) {
23324d25b1e8SKarl Rupp       PetscInt        cellPair[2];
233377c88f5bSMatthew G Knepley       PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
2334552f7358SJed Brown       PetscInt        meetSize = 0;
23350298fd71SBarry Smith       const PetscInt *meet    = NULL;
2336552f7358SJed Brown 
23374d25b1e8SKarl Rupp       cellPair[0] = cell; cellPair[1] = neighborCells[n];
2338552f7358SJed Brown       if (cellPair[0] == cellPair[1]) continue;
2339552f7358SJed Brown       if (!found) {
2340552f7358SJed Brown         ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
2341552f7358SJed Brown         if (meetSize) {
2342552f7358SJed Brown           PetscInt f;
2343552f7358SJed Brown 
2344552f7358SJed Brown           for (f = 0; f < numFaceCases; ++f) {
2345552f7358SJed Brown             if (numFaceVertices[f] == meetSize) {
2346552f7358SJed Brown               found = PETSC_TRUE;
2347552f7358SJed Brown               break;
2348552f7358SJed Brown             }
2349552f7358SJed Brown           }
2350552f7358SJed Brown         }
2351552f7358SJed Brown         ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
2352552f7358SJed Brown       }
23530d644c17SKarl Rupp       if (found) ++off[cell-cStart+1];
2354552f7358SJed Brown     }
2355552f7358SJed Brown   }
2356552f7358SJed Brown   /* Prefix sum */
23570d644c17SKarl Rupp   for (cell = 1; cell <= numCells; ++cell) off[cell] += off[cell-1];
23580d644c17SKarl Rupp 
2359552f7358SJed Brown   if (adjacency) {
2360785e854fSJed Brown     ierr = PetscMalloc1(off[numCells], &adj);CHKERRQ(ierr);
2361552f7358SJed Brown     /* Get neighboring cells */
2362552f7358SJed Brown     for (cell = cStart; cell < cEnd; ++cell) {
2363552f7358SJed Brown       PetscInt numNeighbors = maxNeighbors, n;
2364552f7358SJed Brown       PetscInt cellOffset   = 0;
2365552f7358SJed Brown 
2366cb1e1211SMatthew G Knepley       ierr = DMPlexGetAdjacencySingleLevel_Internal(dm, cell, PETSC_TRUE, tmpClosure, &numNeighbors, neighborCells);CHKERRQ(ierr);
2367552f7358SJed Brown       /* Get meet with each cell, and check with recognizer (could optimize to check each pair only once) */
2368552f7358SJed Brown       for (n = 0; n < numNeighbors; ++n) {
23694d25b1e8SKarl Rupp         PetscInt        cellPair[2];
237077c88f5bSMatthew G Knepley         PetscBool       found    = faceDepth > 1 ? PETSC_TRUE : PETSC_FALSE;
2371552f7358SJed Brown         PetscInt        meetSize = 0;
23720298fd71SBarry Smith         const PetscInt *meet    = NULL;
2373552f7358SJed Brown 
23744d25b1e8SKarl Rupp         cellPair[0] = cell; cellPair[1] = neighborCells[n];
2375552f7358SJed Brown         if (cellPair[0] == cellPair[1]) continue;
2376552f7358SJed Brown         if (!found) {
2377552f7358SJed Brown           ierr = DMPlexGetMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
2378552f7358SJed Brown           if (meetSize) {
2379552f7358SJed Brown             PetscInt f;
2380552f7358SJed Brown 
2381552f7358SJed Brown             for (f = 0; f < numFaceCases; ++f) {
2382552f7358SJed Brown               if (numFaceVertices[f] == meetSize) {
2383552f7358SJed Brown                 found = PETSC_TRUE;
2384552f7358SJed Brown                 break;
2385552f7358SJed Brown               }
2386552f7358SJed Brown             }
2387552f7358SJed Brown           }
2388552f7358SJed Brown           ierr = DMPlexRestoreMeet(dm, 2, cellPair, &meetSize, &meet);CHKERRQ(ierr);
2389552f7358SJed Brown         }
2390552f7358SJed Brown         if (found) {
2391552f7358SJed Brown           adj[off[cell-cStart]+cellOffset] = neighborCells[n];
2392552f7358SJed Brown           ++cellOffset;
2393552f7358SJed Brown         }
2394552f7358SJed Brown       }
2395552f7358SJed Brown     }
2396552f7358SJed Brown   }
2397552f7358SJed Brown   ierr = PetscFree2(neighborCells,tmpClosure);CHKERRQ(ierr);
2398552f7358SJed Brown   if (numVertices) *numVertices = numCells;
2399552f7358SJed Brown   if (offsets)   *offsets   = off;
2400552f7358SJed Brown   if (adjacency) *adjacency = adj;
2401552f7358SJed Brown   PetscFunctionReturn(0);
2402552f7358SJed Brown }
2403552f7358SJed Brown 
2404519f805aSKarl Rupp #if defined(PETSC_HAVE_CHACO)
2405519f805aSKarl Rupp #if defined(PETSC_HAVE_UNISTD_H)
2406552f7358SJed Brown #include <unistd.h>
2407552f7358SJed Brown #endif
2408552f7358SJed Brown /* Chaco does not have an include file */
240939d7646bSJed Brown PETSC_EXTERN int interface(int nvtxs, int *start, int *adjacency, int *vwgts,
2410552f7358SJed Brown                        float *ewgts, float *x, float *y, float *z, char *outassignname,
2411552f7358SJed Brown                        char *outfilename, short *assignment, int architecture, int ndims_tot,
2412552f7358SJed Brown                        int mesh_dims[3], double *goal, int global_method, int local_method,
2413552f7358SJed Brown                        int rqi_flag, int vmax, int ndims, double eigtol, long seed);
2414552f7358SJed Brown 
2415552f7358SJed Brown extern int FREE_GRAPH;
2416552f7358SJed Brown 
2417552f7358SJed Brown #undef __FUNCT__
2418552f7358SJed Brown #define __FUNCT__ "DMPlexPartition_Chaco"
2419552f7358SJed Brown PetscErrorCode DMPlexPartition_Chaco(DM dm, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection *partSection, IS *partition)
2420552f7358SJed Brown {
2421552f7358SJed Brown   enum {DEFAULT_METHOD = 1, INERTIAL_METHOD = 3};
242282f516ccSBarry Smith   MPI_Comm       comm;
2423552f7358SJed Brown   int            nvtxs          = numVertices; /* number of vertices in full graph */
2424552f7358SJed Brown   int           *vwgts          = NULL;   /* weights for all vertices */
2425552f7358SJed Brown   float         *ewgts          = NULL;   /* weights for all edges */
2426552f7358SJed Brown   float         *x              = NULL, *y = NULL, *z = NULL; /* coordinates for inertial method */
2427552f7358SJed Brown   char          *outassignname  = NULL;   /*  name of assignment output file */
2428552f7358SJed Brown   char          *outfilename    = NULL;   /* output file name */
2429552f7358SJed Brown   int            architecture   = 1;      /* 0 => hypercube, d => d-dimensional mesh */
2430552f7358SJed Brown   int            ndims_tot      = 0;      /* total number of cube dimensions to divide */
2431552f7358SJed Brown   int            mesh_dims[3];            /* dimensions of mesh of processors */
2432552f7358SJed Brown   double        *goal          = NULL;    /* desired set sizes for each set */
2433552f7358SJed Brown   int            global_method = 1;       /* global partitioning algorithm */
2434552f7358SJed Brown   int            local_method  = 1;       /* local partitioning algorithm */
2435552f7358SJed Brown   int            rqi_flag      = 0;       /* should I use RQI/Symmlq eigensolver? */
2436552f7358SJed Brown   int            vmax          = 200;     /* how many vertices to coarsen down to? */
2437552f7358SJed Brown   int            ndims         = 1;       /* number of eigenvectors (2^d sets) */
2438552f7358SJed Brown   double         eigtol        = 0.001;   /* tolerance on eigenvectors */
2439552f7358SJed Brown   long           seed          = 123636512; /* for random graph mutations */
2440552f7358SJed Brown   short int     *assignment;              /* Output partition */
2441552f7358SJed Brown   int            fd_stdout, fd_pipe[2];
2442552f7358SJed Brown   PetscInt      *points;
2443552f7358SJed Brown   PetscMPIInt    commSize;
2444552f7358SJed Brown   int            i, v, p;
2445552f7358SJed Brown   PetscErrorCode ierr;
2446552f7358SJed Brown 
2447552f7358SJed Brown   PetscFunctionBegin;
244882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2449552f7358SJed Brown   ierr = MPI_Comm_size(comm, &commSize);CHKERRQ(ierr);
2450552f7358SJed Brown   if (!numVertices) {
2451552f7358SJed Brown     ierr = PetscSectionCreate(comm, partSection);CHKERRQ(ierr);
2452552f7358SJed Brown     ierr = PetscSectionSetChart(*partSection, 0, commSize);CHKERRQ(ierr);
2453552f7358SJed Brown     ierr = PetscSectionSetUp(*partSection);CHKERRQ(ierr);
24540298fd71SBarry Smith     ierr = ISCreateGeneral(comm, 0, NULL, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
2455552f7358SJed Brown     PetscFunctionReturn(0);
2456552f7358SJed Brown   }
2457552f7358SJed Brown   FREE_GRAPH = 0;                         /* Do not let Chaco free my memory */
24580d644c17SKarl Rupp   for (i = 0; i < start[numVertices]; ++i) ++adjacency[i];
24590d644c17SKarl Rupp 
2460552f7358SJed Brown   if (global_method == INERTIAL_METHOD) {
2461552f7358SJed Brown     /* manager.createCellCoordinates(nvtxs, &x, &y, &z); */
2462552f7358SJed Brown     SETERRQ(comm, PETSC_ERR_SUP, "Inertial partitioning not yet supported");
2463552f7358SJed Brown   }
2464552f7358SJed Brown   mesh_dims[0] = commSize;
2465552f7358SJed Brown   mesh_dims[1] = 1;
2466552f7358SJed Brown   mesh_dims[2] = 1;
2467785e854fSJed Brown   ierr = PetscMalloc1(nvtxs, &assignment);CHKERRQ(ierr);
2468552f7358SJed Brown   /* Chaco outputs to stdout. We redirect this to a buffer. */
2469552f7358SJed Brown   /* TODO: check error codes for UNIX calls */
2470519f805aSKarl Rupp #if defined(PETSC_HAVE_UNISTD_H)
2471552f7358SJed Brown   {
24727995c745SJed Brown     int piperet;
24737995c745SJed Brown     piperet = pipe(fd_pipe);
24747995c745SJed Brown     if (piperet) SETERRQ(comm,PETSC_ERR_SYS,"Could not create pipe");
2475552f7358SJed Brown     fd_stdout = dup(1);
2476552f7358SJed Brown     close(1);
2477552f7358SJed Brown     dup2(fd_pipe[1], 1);
2478552f7358SJed Brown   }
2479552f7358SJed Brown #endif
2480552f7358SJed Brown   ierr = interface(nvtxs, (int*) start, (int*) adjacency, vwgts, ewgts, x, y, z, outassignname, outfilename,
2481552f7358SJed Brown                    assignment, architecture, ndims_tot, mesh_dims, goal, global_method, local_method, rqi_flag,
2482552f7358SJed Brown                    vmax, ndims, eigtol, seed);
2483519f805aSKarl Rupp #if defined(PETSC_HAVE_UNISTD_H)
2484552f7358SJed Brown   {
2485552f7358SJed Brown     char msgLog[10000];
2486552f7358SJed Brown     int  count;
2487552f7358SJed Brown 
2488552f7358SJed Brown     fflush(stdout);
2489552f7358SJed Brown     count = read(fd_pipe[0], msgLog, (10000-1)*sizeof(char));
2490552f7358SJed Brown     if (count < 0) count = 0;
2491552f7358SJed Brown     msgLog[count] = 0;
2492552f7358SJed Brown     close(1);
2493552f7358SJed Brown     dup2(fd_stdout, 1);
2494552f7358SJed Brown     close(fd_stdout);
2495552f7358SJed Brown     close(fd_pipe[0]);
2496552f7358SJed Brown     close(fd_pipe[1]);
2497552f7358SJed Brown     if (ierr) SETERRQ1(comm, PETSC_ERR_LIB, "Error in Chaco library: %s", msgLog);
2498552f7358SJed Brown   }
2499552f7358SJed Brown #endif
2500552f7358SJed Brown   /* Convert to PetscSection+IS */
2501552f7358SJed Brown   ierr = PetscSectionCreate(comm, partSection);CHKERRQ(ierr);
2502552f7358SJed Brown   ierr = PetscSectionSetChart(*partSection, 0, commSize);CHKERRQ(ierr);
2503552f7358SJed Brown   for (v = 0; v < nvtxs; ++v) {
2504552f7358SJed Brown     ierr = PetscSectionAddDof(*partSection, assignment[v], 1);CHKERRQ(ierr);
2505552f7358SJed Brown   }
2506552f7358SJed Brown   ierr = PetscSectionSetUp(*partSection);CHKERRQ(ierr);
2507785e854fSJed Brown   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
2508552f7358SJed Brown   for (p = 0, i = 0; p < commSize; ++p) {
2509552f7358SJed Brown     for (v = 0; v < nvtxs; ++v) {
2510552f7358SJed Brown       if (assignment[v] == p) points[i++] = v;
2511552f7358SJed Brown     }
2512552f7358SJed Brown   }
2513552f7358SJed Brown   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
2514552f7358SJed Brown   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
2515552f7358SJed Brown   if (global_method == INERTIAL_METHOD) {
2516552f7358SJed Brown     /* manager.destroyCellCoordinates(nvtxs, &x, &y, &z); */
2517552f7358SJed Brown   }
2518552f7358SJed Brown   ierr = PetscFree(assignment);CHKERRQ(ierr);
25190d644c17SKarl Rupp   for (i = 0; i < start[numVertices]; ++i) --adjacency[i];
2520552f7358SJed Brown   PetscFunctionReturn(0);
2521552f7358SJed Brown }
2522552f7358SJed Brown #endif
2523552f7358SJed Brown 
2524519f805aSKarl Rupp #if defined(PETSC_HAVE_PARMETIS)
252525cda350SMatthew G. Knepley #include <parmetis.h>
252625cda350SMatthew G. Knepley 
2527552f7358SJed Brown #undef __FUNCT__
2528552f7358SJed Brown #define __FUNCT__ "DMPlexPartition_ParMetis"
2529552f7358SJed Brown PetscErrorCode DMPlexPartition_ParMetis(DM dm, PetscInt numVertices, PetscInt start[], PetscInt adjacency[], PetscSection *partSection, IS *partition)
2530552f7358SJed Brown {
253125cda350SMatthew G. Knepley   MPI_Comm       comm;
253225cda350SMatthew G. Knepley   PetscInt       nvtxs      = numVertices; // The number of vertices in full graph
253325cda350SMatthew G. Knepley   PetscInt      *vtxdist;                  // Distribution of vertices across processes
253425cda350SMatthew G. Knepley   PetscInt      *xadj       = start;       // Start of edge list for each vertex
253525cda350SMatthew G. Knepley   PetscInt      *adjncy     = adjacency;   // Edge lists for all vertices
253625cda350SMatthew G. Knepley   PetscInt      *vwgt       = NULL;        // Vertex weights
253725cda350SMatthew G. Knepley   PetscInt      *adjwgt     = NULL;        // Edge weights
253825cda350SMatthew G. Knepley   PetscInt       wgtflag    = 0;           // Indicates which weights are present
253925cda350SMatthew G. Knepley   PetscInt       numflag    = 0;           // Indicates initial offset (0 or 1)
254025cda350SMatthew G. Knepley   PetscInt       ncon       = 1;           // The number of weights per vertex
254125cda350SMatthew G. Knepley   PetscInt       nparts;                   // The number of partitions
254225cda350SMatthew G. Knepley   PetscReal     *tpwgts;                   // The fraction of vertex weights assigned to each partition
254325cda350SMatthew G. Knepley   PetscReal     *ubvec;                    // The balance intolerance for vertex weights
254425cda350SMatthew G. Knepley   PetscInt       options[5];               // Options
254525cda350SMatthew G. Knepley   // Outputs
254625cda350SMatthew G. Knepley   PetscInt       edgeCut;                  // The number of edges cut by the partition
254725cda350SMatthew G. Knepley   PetscInt      *assignment, *points;
254825cda350SMatthew G. Knepley   PetscMPIInt    commSize, rank, p, v, i;
254925cda350SMatthew G. Knepley   PetscErrorCode ierr;
255025cda350SMatthew G. Knepley 
2551552f7358SJed Brown   PetscFunctionBegin;
255225cda350SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
255325cda350SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &commSize);CHKERRQ(ierr);
255425cda350SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
255525cda350SMatthew G. Knepley   nparts = commSize;
255625cda350SMatthew G. Knepley   options[0] = 0; /* Use all defaults */
255725cda350SMatthew G. Knepley   /* Calculate vertex distribution */
2558dcca6d9dSJed Brown   ierr = PetscMalloc4(nparts+1,&vtxdist,nparts*ncon,&tpwgts,ncon,&ubvec,nvtxs,&assignment);CHKERRQ(ierr);
255925cda350SMatthew G. Knepley   vtxdist[0] = 0;
256025cda350SMatthew G. Knepley   ierr = MPI_Allgather(&nvtxs, 1, MPIU_INT, &vtxdist[1], 1, MPIU_INT, comm);CHKERRQ(ierr);
256125cda350SMatthew G. Knepley   for (p = 2; p <= nparts; ++p) {
256225cda350SMatthew G. Knepley     vtxdist[p] += vtxdist[p-1];
256325cda350SMatthew G. Knepley   }
256425cda350SMatthew G. Knepley   /* Calculate weights */
256525cda350SMatthew G. Knepley   for (p = 0; p < nparts; ++p) {
256625cda350SMatthew G. Knepley     tpwgts[p] = 1.0/nparts;
256725cda350SMatthew G. Knepley   }
256825cda350SMatthew G. Knepley   ubvec[0] = 1.05;
256925cda350SMatthew G. Knepley 
257025cda350SMatthew G. Knepley   if (nparts == 1) {
257125cda350SMatthew G. Knepley     ierr = PetscMemzero(assignment, nvtxs * sizeof(PetscInt));
257225cda350SMatthew G. Knepley   } else {
257325cda350SMatthew G. Knepley     if (vtxdist[1] == vtxdist[nparts]) {
257425cda350SMatthew G. Knepley       if (!rank) {
2575e7ee2502SMatthew G. Knepley         PetscStackPush("METIS_PartGraphKway");
2576e7ee2502SMatthew G. Knepley         ierr = METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt, &nparts, tpwgts, ubvec, NULL, &edgeCut, assignment);
2577e7ee2502SMatthew G. Knepley         PetscStackPop;
2578e7ee2502SMatthew G. Knepley         if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in METIS_PartGraphKway()");
257925cda350SMatthew G. Knepley       }
258025cda350SMatthew G. Knepley     } else {
2581e7ee2502SMatthew G. Knepley       PetscStackPush("ParMETIS_V3_PartKway");
2582e7ee2502SMatthew G. Knepley       ierr = ParMETIS_V3_PartKway(vtxdist, xadj, adjncy, vwgt, adjwgt, &wgtflag, &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgeCut, assignment, &comm);
2583e7ee2502SMatthew G. Knepley       PetscStackPop;
2584e7ee2502SMatthew G. Knepley       if (ierr != METIS_OK) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in ParMETIS_V3_PartKway()");
258525cda350SMatthew G. Knepley     }
258625cda350SMatthew G. Knepley   }
258725cda350SMatthew G. Knepley   /* Convert to PetscSection+IS */
258825cda350SMatthew G. Knepley   ierr = PetscSectionCreate(comm, partSection);CHKERRQ(ierr);
258925cda350SMatthew G. Knepley   ierr = PetscSectionSetChart(*partSection, 0, commSize);CHKERRQ(ierr);
259025cda350SMatthew G. Knepley   for (v = 0; v < nvtxs; ++v) {
259125cda350SMatthew G. Knepley     ierr = PetscSectionAddDof(*partSection, assignment[v], 1);CHKERRQ(ierr);
259225cda350SMatthew G. Knepley   }
259325cda350SMatthew G. Knepley   ierr = PetscSectionSetUp(*partSection);CHKERRQ(ierr);
2594785e854fSJed Brown   ierr = PetscMalloc1(nvtxs, &points);CHKERRQ(ierr);
259525cda350SMatthew G. Knepley   for (p = 0, i = 0; p < commSize; ++p) {
259625cda350SMatthew G. Knepley     for (v = 0; v < nvtxs; ++v) {
259725cda350SMatthew G. Knepley       if (assignment[v] == p) points[i++] = v;
259825cda350SMatthew G. Knepley     }
259925cda350SMatthew G. Knepley   }
260025cda350SMatthew G. Knepley   if (i != nvtxs) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of points %D should be %D", i, nvtxs);
260125cda350SMatthew G. Knepley   ierr = ISCreateGeneral(comm, nvtxs, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
260225cda350SMatthew G. Knepley   ierr = PetscFree4(vtxdist,tpwgts,ubvec,assignment);CHKERRQ(ierr);
2603552f7358SJed Brown   PetscFunctionReturn(0);
2604552f7358SJed Brown }
2605552f7358SJed Brown #endif
2606552f7358SJed Brown 
2607552f7358SJed Brown #undef __FUNCT__
2608552f7358SJed Brown #define __FUNCT__ "DMPlexEnlargePartition"
2609552f7358SJed Brown /* Expand the partition by BFS on the adjacency graph */
2610a6dfd86eSKarl Rupp PetscErrorCode DMPlexEnlargePartition(DM dm, const PetscInt start[], const PetscInt adjacency[], PetscSection origPartSection, IS origPartition, PetscSection *partSection, IS *partition)
2611a6dfd86eSKarl Rupp {
2612552f7358SJed Brown   PetscHashI      h;
2613552f7358SJed Brown   const PetscInt *points;
2614552f7358SJed Brown   PetscInt      **tmpPoints, *newPoints, totPoints = 0;
2615552f7358SJed Brown   PetscInt        pStart, pEnd, part, q;
2616552f7358SJed Brown   PetscErrorCode  ierr;
2617552f7358SJed Brown 
2618552f7358SJed Brown   PetscFunctionBegin;
2619552f7358SJed Brown   PetscHashICreate(h);
262082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), partSection);CHKERRQ(ierr);
2621552f7358SJed Brown   ierr = PetscSectionGetChart(origPartSection, &pStart, &pEnd);CHKERRQ(ierr);
2622552f7358SJed Brown   ierr = PetscSectionSetChart(*partSection, pStart, pEnd);CHKERRQ(ierr);
2623552f7358SJed Brown   ierr = ISGetIndices(origPartition, &points);CHKERRQ(ierr);
2624785e854fSJed Brown   ierr = PetscMalloc1((pEnd - pStart), &tmpPoints);CHKERRQ(ierr);
2625552f7358SJed Brown   for (part = pStart; part < pEnd; ++part) {
2626552f7358SJed Brown     PetscInt numPoints, nP, numNewPoints, off, p, n = 0;
2627552f7358SJed Brown 
2628552f7358SJed Brown     PetscHashIClear(h);
2629552f7358SJed Brown     ierr = PetscSectionGetDof(origPartSection, part, &numPoints);CHKERRQ(ierr);
2630552f7358SJed Brown     ierr = PetscSectionGetOffset(origPartSection, part, &off);CHKERRQ(ierr);
2631552f7358SJed Brown     /* Add all existing points to h */
2632552f7358SJed Brown     for (p = 0; p < numPoints; ++p) {
2633552f7358SJed Brown       const PetscInt point = points[off+p];
2634552f7358SJed Brown       PetscHashIAdd(h, point, 1);
2635552f7358SJed Brown     }
2636552f7358SJed Brown     PetscHashISize(h, nP);
263782f516ccSBarry Smith     if (nP != numPoints) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid partition has %d points, but only %d were unique", numPoints, nP);
2638552f7358SJed Brown     /* Add all points in next BFS level */
2639552f7358SJed Brown     /*   TODO We are brute forcing here, but could check the adjacency size to find the boundary */
2640552f7358SJed Brown     for (p = 0; p < numPoints; ++p) {
2641552f7358SJed Brown       const PetscInt point = points[off+p];
2642552f7358SJed Brown       PetscInt       s     = start[point], e = start[point+1], a;
2643552f7358SJed Brown 
26440d644c17SKarl Rupp       for (a = s; a < e; ++a) PetscHashIAdd(h, adjacency[a], 1);
2645552f7358SJed Brown     }
2646552f7358SJed Brown     PetscHashISize(h, numNewPoints);
2647552f7358SJed Brown     ierr = PetscSectionSetDof(*partSection, part, numNewPoints);CHKERRQ(ierr);
2648785e854fSJed Brown     ierr = PetscMalloc1(numNewPoints, &tmpPoints[part]);CHKERRQ(ierr);
2649c9fc7396SMatthew G. Knepley     ierr =  PetscHashIGetKeys(h, &n, tmpPoints[part]);CHKERRQ(ierr);
2650552f7358SJed Brown     totPoints += numNewPoints;
2651552f7358SJed Brown   }
2652552f7358SJed Brown   ierr = ISRestoreIndices(origPartition, &points);CHKERRQ(ierr);
2653552f7358SJed Brown   PetscHashIDestroy(h);
2654552f7358SJed Brown   ierr = PetscSectionSetUp(*partSection);CHKERRQ(ierr);
2655785e854fSJed Brown   ierr = PetscMalloc1(totPoints, &newPoints);CHKERRQ(ierr);
2656552f7358SJed Brown   for (part = pStart, q = 0; part < pEnd; ++part) {
2657552f7358SJed Brown     PetscInt numPoints, p;
2658552f7358SJed Brown 
2659552f7358SJed Brown     ierr = PetscSectionGetDof(*partSection, part, &numPoints);CHKERRQ(ierr);
26600d644c17SKarl Rupp     for (p = 0; p < numPoints; ++p, ++q) newPoints[q] = tmpPoints[part][p];
2661552f7358SJed Brown     ierr = PetscFree(tmpPoints[part]);CHKERRQ(ierr);
2662552f7358SJed Brown   }
2663552f7358SJed Brown   ierr = PetscFree(tmpPoints);CHKERRQ(ierr);
266482f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), totPoints, newPoints, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
2665552f7358SJed Brown   PetscFunctionReturn(0);
2666552f7358SJed Brown }
2667552f7358SJed Brown 
2668552f7358SJed Brown #undef __FUNCT__
2669552f7358SJed Brown #define __FUNCT__ "DMPlexCreatePartition"
2670552f7358SJed Brown /*
2671552f7358SJed Brown   DMPlexCreatePartition - Create a non-overlapping partition of the points at the given height
2672552f7358SJed Brown 
2673552f7358SJed Brown   Collective on DM
2674552f7358SJed Brown 
2675552f7358SJed Brown   Input Parameters:
2676552f7358SJed Brown   + dm - The DM
2677552f7358SJed Brown   . height - The height for points in the partition
2678552f7358SJed Brown   - enlarge - Expand each partition with neighbors
2679552f7358SJed Brown 
2680552f7358SJed Brown   Output Parameters:
2681552f7358SJed Brown   + partSection - The PetscSection giving the division of points by partition
2682552f7358SJed Brown   . partition - The list of points by partition
26830298fd71SBarry Smith   . origPartSection - If enlarge is true, the PetscSection giving the division of points before enlarging by partition, otherwise NULL
26840298fd71SBarry Smith   - origPartition - If enlarge is true, the list of points before enlarging by partition, otherwise NULL
2685552f7358SJed Brown 
2686552f7358SJed Brown   Level: developer
2687552f7358SJed Brown 
2688552f7358SJed Brown .seealso DMPlexDistribute()
2689552f7358SJed Brown */
2690ac17c9edSMatthew G. Knepley PetscErrorCode DMPlexCreatePartition(DM dm, const char name[], PetscInt height, PetscBool enlarge, PetscSection *partSection, IS *partition, PetscSection *origPartSection, IS *origPartition)
26910adebc6cSBarry Smith {
2692ac17c9edSMatthew G. Knepley   char           partname[1024];
2693ac17c9edSMatthew G. Knepley   PetscBool      isChaco = PETSC_FALSE, isMetis = PETSC_FALSE, flg;
2694552f7358SJed Brown   PetscMPIInt    size;
2695552f7358SJed Brown   PetscErrorCode ierr;
2696552f7358SJed Brown 
2697552f7358SJed Brown   PetscFunctionBegin;
269882f516ccSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
26990d644c17SKarl Rupp 
27000298fd71SBarry Smith   *origPartSection = NULL;
27010298fd71SBarry Smith   *origPartition   = NULL;
2702552f7358SJed Brown   if (size == 1) {
2703552f7358SJed Brown     PetscInt *points;
2704552f7358SJed Brown     PetscInt  cStart, cEnd, c;
2705552f7358SJed Brown 
2706552f7358SJed Brown     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
270782f516ccSBarry Smith     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), partSection);CHKERRQ(ierr);
2708552f7358SJed Brown     ierr = PetscSectionSetChart(*partSection, 0, size);CHKERRQ(ierr);
2709552f7358SJed Brown     ierr = PetscSectionSetDof(*partSection, 0, cEnd-cStart);CHKERRQ(ierr);
2710552f7358SJed Brown     ierr = PetscSectionSetUp(*partSection);CHKERRQ(ierr);
2711785e854fSJed Brown     ierr = PetscMalloc1((cEnd - cStart), &points);CHKERRQ(ierr);
27120d644c17SKarl Rupp     for (c = cStart; c < cEnd; ++c) points[c] = c;
271382f516ccSBarry Smith     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), cEnd-cStart, points, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
2714552f7358SJed Brown     PetscFunctionReturn(0);
2715552f7358SJed Brown   }
2716ac17c9edSMatthew G. Knepley   ierr = PetscOptionsGetString(((PetscObject) dm)->prefix, "-dm_plex_partitioner", partname, 1024, &flg);CHKERRQ(ierr);
2717ac17c9edSMatthew G. Knepley   if (flg) name = partname;
2718ac17c9edSMatthew G. Knepley   if (name) {
2719ac17c9edSMatthew G. Knepley     ierr = PetscStrcmp(name, "chaco", &isChaco);CHKERRQ(ierr);
2720ac17c9edSMatthew G. Knepley     ierr = PetscStrcmp(name, "metis", &isMetis);CHKERRQ(ierr);
2721ac17c9edSMatthew G. Knepley   }
2722552f7358SJed Brown   if (height == 0) {
2723552f7358SJed Brown     PetscInt  numVertices;
27240298fd71SBarry Smith     PetscInt *start     = NULL;
27250298fd71SBarry Smith     PetscInt *adjacency = NULL;
2726552f7358SJed Brown 
272777c88f5bSMatthew G Knepley     ierr = DMPlexCreateNeighborCSR(dm, 0, &numVertices, &start, &adjacency);CHKERRQ(ierr);
2728ac17c9edSMatthew G. Knepley     if (!name || isChaco) {
2729519f805aSKarl Rupp #if defined(PETSC_HAVE_CHACO)
2730552f7358SJed Brown       ierr = DMPlexPartition_Chaco(dm, numVertices, start, adjacency, partSection, partition);CHKERRQ(ierr);
2731ac17c9edSMatthew G. Knepley #else
2732ac17c9edSMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh partitioning needs external package support.\nPlease reconfigure with --download-chaco.");
2733552f7358SJed Brown #endif
2734ac17c9edSMatthew G. Knepley     } else if (isMetis) {
2735519f805aSKarl Rupp #if defined(PETSC_HAVE_PARMETIS)
2736552f7358SJed Brown       ierr = DMPlexPartition_ParMetis(dm, numVertices, start, adjacency, partSection, partition);CHKERRQ(ierr);
2737552f7358SJed Brown #endif
2738ac17c9edSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Unknown mesh partitioning package %s", name);
2739552f7358SJed Brown     if (enlarge) {
2740552f7358SJed Brown       *origPartSection = *partSection;
2741552f7358SJed Brown       *origPartition   = *partition;
27420d644c17SKarl Rupp 
2743552f7358SJed Brown       ierr = DMPlexEnlargePartition(dm, start, adjacency, *origPartSection, *origPartition, partSection, partition);CHKERRQ(ierr);
2744552f7358SJed Brown     }
2745552f7358SJed Brown     ierr = PetscFree(start);CHKERRQ(ierr);
2746552f7358SJed Brown     ierr = PetscFree(adjacency);CHKERRQ(ierr);
2747552f7358SJed Brown # if 0
2748552f7358SJed Brown   } else if (height == 1) {
2749552f7358SJed Brown     /* Build the dual graph for faces and partition the hypergraph */
2750552f7358SJed Brown     PetscInt numEdges;
2751552f7358SJed Brown 
2752552f7358SJed Brown     buildFaceCSRV(mesh, mesh->getFactory()->getNumbering(mesh, mesh->depth()-1), &numEdges, &start, &adjacency, GraphPartitioner::zeroBase());
2753552f7358SJed Brown     GraphPartitioner().partition(numEdges, start, adjacency, partition, manager);
2754552f7358SJed Brown     destroyCSR(numEdges, start, adjacency);
2755552f7358SJed Brown #endif
275682f516ccSBarry Smith   } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid partition height %D", height);
2757552f7358SJed Brown   PetscFunctionReturn(0);
2758552f7358SJed Brown }
2759552f7358SJed Brown 
2760552f7358SJed Brown #undef __FUNCT__
2761552f7358SJed Brown #define __FUNCT__ "DMPlexCreatePartitionClosure"
27620adebc6cSBarry Smith PetscErrorCode DMPlexCreatePartitionClosure(DM dm, PetscSection pointSection, IS pointPartition, PetscSection *section, IS *partition)
27630adebc6cSBarry Smith {
2764552f7358SJed Brown   /* const PetscInt  height = 0; */
2765552f7358SJed Brown   const PetscInt *partArray;
2766cb3cb6d9SJed Brown   PetscInt       *allPoints, *packPoints;
2767cb3cb6d9SJed Brown   PetscInt        rStart, rEnd, rank, pStart, pEnd, newSize;
2768552f7358SJed Brown   PetscErrorCode  ierr;
2769cb3cb6d9SJed Brown   PetscBT         bt;
2770cb3cb6d9SJed Brown   PetscSegBuffer  segpack,segpart;
2771552f7358SJed Brown 
2772552f7358SJed Brown   PetscFunctionBegin;
2773552f7358SJed Brown   ierr = PetscSectionGetChart(pointSection, &rStart, &rEnd);CHKERRQ(ierr);
2774552f7358SJed Brown   ierr = ISGetIndices(pointPartition, &partArray);CHKERRQ(ierr);
277582f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
2776552f7358SJed Brown   ierr = PetscSectionSetChart(*section, rStart, rEnd);CHKERRQ(ierr);
2777cb3cb6d9SJed Brown   ierr = DMPlexGetChart(dm,&pStart,&pEnd);CHKERRQ(ierr);
2778cb3cb6d9SJed Brown   ierr = PetscBTCreate(pEnd-pStart,&bt);CHKERRQ(ierr);
2779cb3cb6d9SJed Brown   ierr = PetscSegBufferCreate(sizeof(PetscInt),1000,&segpack);CHKERRQ(ierr);
2780cb3cb6d9SJed Brown   ierr = PetscSegBufferCreate(sizeof(PetscInt),1000,&segpart);CHKERRQ(ierr);
2781552f7358SJed Brown   for (rank = rStart; rank < rEnd; ++rank) {
2782471ecdacSJed Brown     PetscInt partSize = 0, numPoints, offset, p, *PETSC_RESTRICT placePoints;
2783552f7358SJed Brown 
2784552f7358SJed Brown     ierr = PetscSectionGetDof(pointSection, rank, &numPoints);CHKERRQ(ierr);
2785552f7358SJed Brown     ierr = PetscSectionGetOffset(pointSection, rank, &offset);CHKERRQ(ierr);
2786552f7358SJed Brown     for (p = 0; p < numPoints; ++p) {
2787552f7358SJed Brown       PetscInt  point   = partArray[offset+p], closureSize, c;
27880298fd71SBarry Smith       PetscInt *closure = NULL;
2789552f7358SJed Brown 
2790552f7358SJed Brown       /* TODO Include support for height > 0 case */
2791552f7358SJed Brown       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2792cb3cb6d9SJed Brown       for (c=0; c<closureSize; c++) {
2793cb3cb6d9SJed Brown         PetscInt cpoint = closure[c*2];
2794cb3cb6d9SJed Brown         if (!PetscBTLookupSet(bt,cpoint-pStart)) {
2795471ecdacSJed Brown           PetscInt *PETSC_RESTRICT pt;
2796cb3cb6d9SJed Brown           partSize++;
2797471ecdacSJed Brown           ierr = PetscSegBufferGetInts(segpart,1,&pt);CHKERRQ(ierr);
2798cb3cb6d9SJed Brown           *pt = cpoint;
2799552f7358SJed Brown         }
2800cb3cb6d9SJed Brown       }
2801552f7358SJed Brown       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2802552f7358SJed Brown     }
2803552f7358SJed Brown     ierr = PetscSectionSetDof(*section, rank, partSize);CHKERRQ(ierr);
2804471ecdacSJed Brown     ierr = PetscSegBufferGetInts(segpack,partSize,&placePoints);CHKERRQ(ierr);
2805471ecdacSJed Brown     ierr = PetscSegBufferExtractTo(segpart,placePoints);CHKERRQ(ierr);
2806471ecdacSJed Brown     ierr = PetscSortInt(partSize,placePoints);CHKERRQ(ierr);
2807471ecdacSJed Brown     for (p=0; p<partSize; p++) {ierr = PetscBTClear(bt,placePoints[p]-pStart);CHKERRQ(ierr);}
2808552f7358SJed Brown   }
2809cb3cb6d9SJed Brown   ierr = PetscBTDestroy(&bt);CHKERRQ(ierr);
2810cb3cb6d9SJed Brown   ierr = PetscSegBufferDestroy(&segpart);CHKERRQ(ierr);
2811cb3cb6d9SJed Brown 
2812552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
2813552f7358SJed Brown   ierr = PetscSectionGetStorageSize(*section, &newSize);CHKERRQ(ierr);
2814785e854fSJed Brown   ierr = PetscMalloc1(newSize, &allPoints);CHKERRQ(ierr);
2815552f7358SJed Brown 
2816137cf7b6SJed Brown   ierr = PetscSegBufferExtractInPlace(segpack,&packPoints);CHKERRQ(ierr);
2817552f7358SJed Brown   for (rank = rStart; rank < rEnd; ++rank) {
2818cb3cb6d9SJed Brown     PetscInt numPoints, offset;
2819552f7358SJed Brown 
2820cb3cb6d9SJed Brown     ierr = PetscSectionGetDof(*section, rank, &numPoints);CHKERRQ(ierr);
2821cb3cb6d9SJed Brown     ierr = PetscSectionGetOffset(*section, rank, &offset);CHKERRQ(ierr);
2822cb3cb6d9SJed Brown     ierr = PetscMemcpy(&allPoints[offset], packPoints, numPoints * sizeof(PetscInt));CHKERRQ(ierr);
2823cb3cb6d9SJed Brown     packPoints += numPoints;
2824552f7358SJed Brown   }
2825cb3cb6d9SJed Brown 
2826cb3cb6d9SJed Brown   ierr = PetscSegBufferDestroy(&segpack);CHKERRQ(ierr);
2827552f7358SJed Brown   ierr = ISRestoreIndices(pointPartition, &partArray);CHKERRQ(ierr);
282882f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), newSize, allPoints, PETSC_OWN_POINTER, partition);CHKERRQ(ierr);
2829552f7358SJed Brown   PetscFunctionReturn(0);
2830552f7358SJed Brown }
2831552f7358SJed Brown 
2832552f7358SJed Brown #undef __FUNCT__
2833552f7358SJed Brown #define __FUNCT__ "DMPlexDistributeField"
28341858a729SMatthew G. Knepley /*@
28351858a729SMatthew G. Knepley   DMPlexDistributeField - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
28361858a729SMatthew G. Knepley 
28371858a729SMatthew G. Knepley   Collective on DM
28381858a729SMatthew G. Knepley 
2839552f7358SJed Brown   Input Parameters:
28401858a729SMatthew G. Knepley + dm - The DMPlex object
28411858a729SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
28421858a729SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
28431858a729SMatthew G. Knepley - originalVec - The existing data
2844552f7358SJed Brown 
2845552f7358SJed Brown   Output Parameters:
28461858a729SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
28471858a729SMatthew G. Knepley - newVec - The new data
28481858a729SMatthew G. Knepley 
28491858a729SMatthew G. Knepley   Level: developer
28501858a729SMatthew G. Knepley 
28511858a729SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeData()
28521858a729SMatthew G. Knepley @*/
2853552f7358SJed Brown PetscErrorCode DMPlexDistributeField(DM dm, PetscSF pointSF, PetscSection originalSection, Vec originalVec, PetscSection newSection, Vec newVec)
2854552f7358SJed Brown {
2855552f7358SJed Brown   PetscSF        fieldSF;
2856552f7358SJed Brown   PetscInt      *remoteOffsets, fieldSize;
2857552f7358SJed Brown   PetscScalar   *originalValues, *newValues;
2858552f7358SJed Brown   PetscErrorCode ierr;
2859552f7358SJed Brown 
2860552f7358SJed Brown   PetscFunctionBegin;
28611337e6e5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
2862552f7358SJed Brown   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
2863552f7358SJed Brown 
2864552f7358SJed Brown   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
2865552f7358SJed Brown   ierr = VecSetSizes(newVec, fieldSize, PETSC_DETERMINE);CHKERRQ(ierr);
2866c0dedaeaSBarry Smith   ierr = VecSetType(newVec,dm->vectype);CHKERRQ(ierr);
2867552f7358SJed Brown 
2868552f7358SJed Brown   ierr = VecGetArray(originalVec, &originalValues);CHKERRQ(ierr);
2869552f7358SJed Brown   ierr = VecGetArray(newVec, &newValues);CHKERRQ(ierr);
2870552f7358SJed Brown   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
2871552f7358SJed Brown   ierr = PetscSFBcastBegin(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
2872552f7358SJed Brown   ierr = PetscSFBcastEnd(fieldSF, MPIU_SCALAR, originalValues, newValues);CHKERRQ(ierr);
2873552f7358SJed Brown   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
2874552f7358SJed Brown   ierr = VecRestoreArray(newVec, &newValues);CHKERRQ(ierr);
2875552f7358SJed Brown   ierr = VecRestoreArray(originalVec, &originalValues);CHKERRQ(ierr);
28761337e6e5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeField,dm,0,0,0);CHKERRQ(ierr);
2877552f7358SJed Brown   PetscFunctionReturn(0);
2878552f7358SJed Brown }
2879552f7358SJed Brown 
2880552f7358SJed Brown #undef __FUNCT__
28811858a729SMatthew G. Knepley #define __FUNCT__ "DMPlexDistributeData"
28821858a729SMatthew G. Knepley /*@
28831858a729SMatthew G. Knepley   DMPlexDistributeData - Distribute field data to match a given PetscSF, usually the SF from mesh distribution
28841858a729SMatthew G. Knepley 
28851858a729SMatthew G. Knepley   Collective on DM
28861858a729SMatthew G. Knepley 
28871858a729SMatthew G. Knepley   Input Parameters:
28881858a729SMatthew G. Knepley + dm - The DMPlex object
28891858a729SMatthew G. Knepley . pointSF - The PetscSF describing the communication pattern
28901858a729SMatthew G. Knepley . originalSection - The PetscSection for existing data layout
28911858a729SMatthew G. Knepley . datatype - The type of data
28921858a729SMatthew G. Knepley - originalData - The existing data
28931858a729SMatthew G. Knepley 
28941858a729SMatthew G. Knepley   Output Parameters:
28951858a729SMatthew G. Knepley + newSection - The PetscSF describing the new data layout
28961858a729SMatthew G. Knepley - newData - The new data
28971858a729SMatthew G. Knepley 
28981858a729SMatthew G. Knepley   Level: developer
28991858a729SMatthew G. Knepley 
29001858a729SMatthew G. Knepley .seealso: DMPlexDistribute(), DMPlexDistributeField()
29011858a729SMatthew G. Knepley @*/
29021858a729SMatthew G. Knepley PetscErrorCode DMPlexDistributeData(DM dm, PetscSF pointSF, PetscSection originalSection, MPI_Datatype datatype, void *originalData, PetscSection newSection, void **newData)
29031858a729SMatthew G. Knepley {
29041858a729SMatthew G. Knepley   PetscSF        fieldSF;
29051858a729SMatthew G. Knepley   PetscInt      *remoteOffsets, fieldSize;
29061858a729SMatthew G. Knepley   PetscMPIInt    dataSize;
29071858a729SMatthew G. Knepley   PetscErrorCode ierr;
29081858a729SMatthew G. Knepley 
29091858a729SMatthew G. Knepley   PetscFunctionBegin;
29101337e6e5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
29111858a729SMatthew G. Knepley   ierr = PetscSFDistributeSection(pointSF, originalSection, &remoteOffsets, newSection);CHKERRQ(ierr);
29121858a729SMatthew G. Knepley 
29131858a729SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(newSection, &fieldSize);CHKERRQ(ierr);
29141858a729SMatthew G. Knepley   ierr = MPI_Type_size(datatype, &dataSize);CHKERRQ(ierr);
29151858a729SMatthew G. Knepley   ierr = PetscMalloc(fieldSize * dataSize, newData);CHKERRQ(ierr);
29161858a729SMatthew G. Knepley 
29171858a729SMatthew G. Knepley   ierr = PetscSFCreateSectionSF(pointSF, originalSection, remoteOffsets, newSection, &fieldSF);CHKERRQ(ierr);
29181858a729SMatthew G. Knepley   ierr = PetscSFBcastBegin(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
29191858a729SMatthew G. Knepley   ierr = PetscSFBcastEnd(fieldSF, datatype, originalData, *newData);CHKERRQ(ierr);
29201858a729SMatthew G. Knepley   ierr = PetscSFDestroy(&fieldSF);CHKERRQ(ierr);
29211337e6e5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeData,dm,0,0,0);CHKERRQ(ierr);
29221858a729SMatthew G. Knepley   PetscFunctionReturn(0);
29231858a729SMatthew G. Knepley }
29241858a729SMatthew G. Knepley 
29251858a729SMatthew G. Knepley #undef __FUNCT__
2926552f7358SJed Brown #define __FUNCT__ "DMPlexDistribute"
2927552f7358SJed Brown /*@C
2928552f7358SJed Brown   DMPlexDistribute - Distributes the mesh and any associated sections.
2929552f7358SJed Brown 
2930552f7358SJed Brown   Not Collective
2931552f7358SJed Brown 
2932552f7358SJed Brown   Input Parameter:
2933552f7358SJed Brown + dm  - The original DMPlex object
2934552f7358SJed Brown . partitioner - The partitioning package, or NULL for the default
2935552f7358SJed Brown - overlap - The overlap of partitions, 0 is the default
2936552f7358SJed Brown 
2937552f7358SJed Brown   Output Parameter:
2938e7ba9e7aSMatthew G. Knepley + sf - The PetscSF used for point distribution
2939e7ba9e7aSMatthew G. Knepley - parallelMesh - The distributed DMPlex object, or NULL
2940552f7358SJed Brown 
29410298fd71SBarry Smith   Note: If the mesh was not distributed, the return value is NULL
2942552f7358SJed Brown 
2943552f7358SJed Brown   Level: intermediate
2944552f7358SJed Brown 
2945552f7358SJed Brown .keywords: mesh, elements
2946552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexDistributeByFace()
2947552f7358SJed Brown @*/
2948e7ba9e7aSMatthew G. Knepley PetscErrorCode DMPlexDistribute(DM dm, const char partitioner[], PetscInt overlap, PetscSF *sf, DM *dmParallel)
2949552f7358SJed Brown {
2950552f7358SJed Brown   DM_Plex               *mesh   = (DM_Plex*) dm->data, *pmesh;
295182f516ccSBarry Smith   MPI_Comm               comm;
2952552f7358SJed Brown   const PetscInt         height = 0;
2953552f7358SJed Brown   PetscInt               dim, numRemoteRanks;
2954057e8d19SMichael Lange   IS                     origCellPart,        origPart,        cellPart,        part;
2955057e8d19SMichael Lange   PetscSection           origCellPartSection, origPartSection, cellPartSection, partSection;
2956552f7358SJed Brown   PetscSFNode           *remoteRanks;
2957552f7358SJed Brown   PetscSF                partSF, pointSF, coneSF;
2958552f7358SJed Brown   ISLocalToGlobalMapping renumbering;
2959552f7358SJed Brown   PetscSection           originalConeSection, newConeSection;
2960552f7358SJed Brown   PetscInt              *remoteOffsets;
2961552f7358SJed Brown   PetscInt              *cones, *newCones, newConesSize;
2962552f7358SJed Brown   PetscBool              flg;
2963552f7358SJed Brown   PetscMPIInt            rank, numProcs, p;
2964552f7358SJed Brown   PetscErrorCode         ierr;
2965552f7358SJed Brown 
2966552f7358SJed Brown   PetscFunctionBegin;
2967552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2968e7ba9e7aSMatthew G. Knepley   if (sf) PetscValidPointer(sf,4);
2969e7ba9e7aSMatthew G. Knepley   PetscValidPointer(dmParallel,5);
29700d644c17SKarl Rupp 
2971552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
297282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2973552f7358SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2974552f7358SJed Brown   ierr = MPI_Comm_size(comm, &numProcs);CHKERRQ(ierr);
29750d644c17SKarl Rupp 
29760298fd71SBarry Smith   *dmParallel = NULL;
2977552f7358SJed Brown   if (numProcs == 1) PetscFunctionReturn(0);
2978552f7358SJed Brown 
2979552f7358SJed Brown   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
2980552f7358SJed Brown   /* Create cell partition - We need to rewrite to use IS, use the MatPartition stuff */
2981b96ba592SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
298282f516ccSBarry Smith   if (overlap > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Overlap > 1 not yet implemented");
2983ac17c9edSMatthew G. Knepley   ierr = DMPlexCreatePartition(dm, partitioner, height, overlap > 0 ? PETSC_TRUE : PETSC_FALSE, &cellPartSection, &cellPart, &origCellPartSection, &origCellPart);CHKERRQ(ierr);
2984552f7358SJed Brown   /* Create SF assuming a serial partition for all processes: Could check for IS length here */
29850d644c17SKarl Rupp   if (!rank) numRemoteRanks = numProcs;
29860d644c17SKarl Rupp   else       numRemoteRanks = 0;
2987785e854fSJed Brown   ierr = PetscMalloc1(numRemoteRanks, &remoteRanks);CHKERRQ(ierr);
2988552f7358SJed Brown   for (p = 0; p < numRemoteRanks; ++p) {
2989552f7358SJed Brown     remoteRanks[p].rank  = p;
2990552f7358SJed Brown     remoteRanks[p].index = 0;
2991552f7358SJed Brown   }
2992552f7358SJed Brown   ierr = PetscSFCreate(comm, &partSF);CHKERRQ(ierr);
29930298fd71SBarry Smith   ierr = PetscSFSetGraph(partSF, 1, numRemoteRanks, NULL, PETSC_OWN_POINTER, remoteRanks, PETSC_OWN_POINTER);CHKERRQ(ierr);
2994552f7358SJed Brown   ierr = PetscOptionsHasName(((PetscObject) dm)->prefix, "-partition_view", &flg);CHKERRQ(ierr);
2995552f7358SJed Brown   if (flg) {
2996552f7358SJed Brown     ierr = PetscPrintf(comm, "Cell Partition:\n");CHKERRQ(ierr);
2997552f7358SJed Brown     ierr = PetscSectionView(cellPartSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
29980298fd71SBarry Smith     ierr = ISView(cellPart, NULL);CHKERRQ(ierr);
2999552f7358SJed Brown     if (origCellPart) {
3000552f7358SJed Brown       ierr = PetscPrintf(comm, "Original Cell Partition:\n");CHKERRQ(ierr);
3001552f7358SJed Brown       ierr = PetscSectionView(origCellPartSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
30020298fd71SBarry Smith       ierr = ISView(origCellPart, NULL);CHKERRQ(ierr);
3003552f7358SJed Brown     }
30040298fd71SBarry Smith     ierr = PetscSFView(partSF, NULL);CHKERRQ(ierr);
3005552f7358SJed Brown   }
3006552f7358SJed Brown   /* Close the partition over the mesh */
3007552f7358SJed Brown   ierr = DMPlexCreatePartitionClosure(dm, cellPartSection, cellPart, &partSection, &part);CHKERRQ(ierr);
3008552f7358SJed Brown   ierr = ISDestroy(&cellPart);CHKERRQ(ierr);
3009552f7358SJed Brown   ierr = PetscSectionDestroy(&cellPartSection);CHKERRQ(ierr);
3010552f7358SJed Brown   /* Create new mesh */
3011552f7358SJed Brown   ierr  = DMPlexCreate(comm, dmParallel);CHKERRQ(ierr);
3012552f7358SJed Brown   ierr  = DMPlexSetDimension(*dmParallel, dim);CHKERRQ(ierr);
3013552f7358SJed Brown   ierr  = PetscObjectSetName((PetscObject) *dmParallel, "Parallel Mesh");CHKERRQ(ierr);
3014552f7358SJed Brown   pmesh = (DM_Plex*) (*dmParallel)->data;
3015552f7358SJed Brown   /* Distribute sieve points and the global point numbering (replaces creating remote bases) */
3016552f7358SJed Brown   ierr = PetscSFConvertPartition(partSF, partSection, part, &renumbering, &pointSF);CHKERRQ(ierr);
3017552f7358SJed Brown   if (flg) {
3018552f7358SJed Brown     ierr = PetscPrintf(comm, "Point Partition:\n");CHKERRQ(ierr);
3019552f7358SJed Brown     ierr = PetscSectionView(partSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
30200298fd71SBarry Smith     ierr = ISView(part, NULL);CHKERRQ(ierr);
30210298fd71SBarry Smith     ierr = PetscSFView(pointSF, NULL);CHKERRQ(ierr);
3022552f7358SJed Brown     ierr = PetscPrintf(comm, "Point Renumbering after partition:\n");CHKERRQ(ierr);
30230298fd71SBarry Smith     ierr = ISLocalToGlobalMappingView(renumbering, NULL);CHKERRQ(ierr);
3024552f7358SJed Brown   }
3025b96ba592SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_Partition,dm,0,0,0);CHKERRQ(ierr);
30261337e6e5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
3027552f7358SJed Brown   /* Distribute cone section */
3028552f7358SJed Brown   ierr = DMPlexGetConeSection(dm, &originalConeSection);CHKERRQ(ierr);
3029552f7358SJed Brown   ierr = DMPlexGetConeSection(*dmParallel, &newConeSection);CHKERRQ(ierr);
3030552f7358SJed Brown   ierr = PetscSFDistributeSection(pointSF, originalConeSection, &remoteOffsets, newConeSection);CHKERRQ(ierr);
3031552f7358SJed Brown   ierr = DMSetUp(*dmParallel);CHKERRQ(ierr);
3032552f7358SJed Brown   {
3033552f7358SJed Brown     PetscInt pStart, pEnd, p;
3034552f7358SJed Brown 
3035552f7358SJed Brown     ierr = PetscSectionGetChart(newConeSection, &pStart, &pEnd);CHKERRQ(ierr);
3036552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3037552f7358SJed Brown       PetscInt coneSize;
3038552f7358SJed Brown       ierr               = PetscSectionGetDof(newConeSection, p, &coneSize);CHKERRQ(ierr);
3039552f7358SJed Brown       pmesh->maxConeSize = PetscMax(pmesh->maxConeSize, coneSize);
3040552f7358SJed Brown     }
3041552f7358SJed Brown   }
3042552f7358SJed Brown   /* Communicate and renumber cones */
3043552f7358SJed Brown   ierr = PetscSFCreateSectionSF(pointSF, originalConeSection, remoteOffsets, newConeSection, &coneSF);CHKERRQ(ierr);
3044552f7358SJed Brown   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
3045552f7358SJed Brown   ierr = DMPlexGetCones(*dmParallel, &newCones);CHKERRQ(ierr);
3046552f7358SJed Brown   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
3047552f7358SJed Brown   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
3048552f7358SJed Brown   ierr = PetscSectionGetStorageSize(newConeSection, &newConesSize);CHKERRQ(ierr);
30490298fd71SBarry Smith   ierr = ISGlobalToLocalMappingApply(renumbering, IS_GTOLM_MASK, newConesSize, newCones, NULL, newCones);CHKERRQ(ierr);
3050552f7358SJed Brown   ierr = PetscOptionsHasName(((PetscObject) dm)->prefix, "-cones_view", &flg);CHKERRQ(ierr);
3051552f7358SJed Brown   if (flg) {
3052552f7358SJed Brown     ierr = PetscPrintf(comm, "Serial Cone Section:\n");CHKERRQ(ierr);
3053552f7358SJed Brown     ierr = PetscSectionView(originalConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
3054552f7358SJed Brown     ierr = PetscPrintf(comm, "Parallel Cone Section:\n");CHKERRQ(ierr);
3055552f7358SJed Brown     ierr = PetscSectionView(newConeSection, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
30560298fd71SBarry Smith     ierr = PetscSFView(coneSF, NULL);CHKERRQ(ierr);
3057552f7358SJed Brown   }
3058552f7358SJed Brown   ierr = DMPlexGetConeOrientations(dm, &cones);CHKERRQ(ierr);
3059552f7358SJed Brown   ierr = DMPlexGetConeOrientations(*dmParallel, &newCones);CHKERRQ(ierr);
3060552f7358SJed Brown   ierr = PetscSFBcastBegin(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
3061552f7358SJed Brown   ierr = PetscSFBcastEnd(coneSF, MPIU_INT, cones, newCones);CHKERRQ(ierr);
3062552f7358SJed Brown   ierr = PetscSFDestroy(&coneSF);CHKERRQ(ierr);
30631337e6e5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeCones,dm,0,0,0);CHKERRQ(ierr);
3064552f7358SJed Brown   /* Create supports and stratify sieve */
3065552f7358SJed Brown   {
3066552f7358SJed Brown     PetscInt pStart, pEnd;
3067552f7358SJed Brown 
3068552f7358SJed Brown     ierr = PetscSectionGetChart(pmesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
3069552f7358SJed Brown     ierr = PetscSectionSetChart(pmesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
3070552f7358SJed Brown   }
3071552f7358SJed Brown   ierr = DMPlexSymmetrize(*dmParallel);CHKERRQ(ierr);
3072552f7358SJed Brown   ierr = DMPlexStratify(*dmParallel);CHKERRQ(ierr);
3073552f7358SJed Brown   /* Distribute Coordinates */
3074552f7358SJed Brown   {
3075552f7358SJed Brown     PetscSection originalCoordSection, newCoordSection;
3076552f7358SJed Brown     Vec          originalCoordinates, newCoordinates;
3077552f7358SJed Brown     const char  *name;
3078552f7358SJed Brown 
3079552f7358SJed Brown     ierr = DMPlexGetCoordinateSection(dm, &originalCoordSection);CHKERRQ(ierr);
3080552f7358SJed Brown     ierr = DMPlexGetCoordinateSection(*dmParallel, &newCoordSection);CHKERRQ(ierr);
3081552f7358SJed Brown     ierr = DMGetCoordinatesLocal(dm, &originalCoordinates);CHKERRQ(ierr);
3082552f7358SJed Brown     ierr = VecCreate(comm, &newCoordinates);CHKERRQ(ierr);
3083552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) originalCoordinates, &name);CHKERRQ(ierr);
3084552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) newCoordinates, name);CHKERRQ(ierr);
3085552f7358SJed Brown 
3086552f7358SJed Brown     ierr = DMPlexDistributeField(dm, pointSF, originalCoordSection, originalCoordinates, newCoordSection, newCoordinates);CHKERRQ(ierr);
3087552f7358SJed Brown     ierr = DMSetCoordinatesLocal(*dmParallel, newCoordinates);CHKERRQ(ierr);
3088552f7358SJed Brown     ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
3089552f7358SJed Brown   }
3090552f7358SJed Brown   /* Distribute labels */
3091b96ba592SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
3092552f7358SJed Brown   {
3093552f7358SJed Brown     DMLabel  next      = mesh->labels, newNext = pmesh->labels;
3094552f7358SJed Brown     PetscInt numLabels = 0, l;
3095552f7358SJed Brown 
3096552f7358SJed Brown     /* Bcast number of labels */
309720cb08bdSMatthew G. Knepley     while (next) {++numLabels; next = next->next;}
3098552f7358SJed Brown     ierr = MPI_Bcast(&numLabels, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
3099552f7358SJed Brown     next = mesh->labels;
3100552f7358SJed Brown     for (l = 0; l < numLabels; ++l) {
310120cb08bdSMatthew G. Knepley       DMLabel   labelNew;
3102552f7358SJed Brown       PetscBool isdepth;
3103552f7358SJed Brown 
310420cb08bdSMatthew G. Knepley       /* Skip "depth" because it is recreated */
310520cb08bdSMatthew G. Knepley       if (!rank) {ierr = PetscStrcmp(next->name, "depth", &isdepth);CHKERRQ(ierr);}
310620cb08bdSMatthew G. Knepley       ierr = MPI_Bcast(&isdepth, 1, MPIU_BOOL, 0, comm);CHKERRQ(ierr);
310720cb08bdSMatthew G. Knepley       if (isdepth) {if (!rank) next = next->next; continue;}
310820cb08bdSMatthew G. Knepley       ierr = DMLabelDistribute(next, partSection, part, renumbering, &labelNew);CHKERRQ(ierr);
3109552f7358SJed Brown       /* Insert into list */
311020cb08bdSMatthew G. Knepley       if (newNext) newNext->next = labelNew;
311120cb08bdSMatthew G. Knepley       else         pmesh->labels = labelNew;
311220cb08bdSMatthew G. Knepley       newNext = labelNew;
31130d644c17SKarl Rupp       if (!rank) next = next->next;
3114552f7358SJed Brown     }
3115552f7358SJed Brown   }
3116b96ba592SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeLabels,dm,0,0,0);CHKERRQ(ierr);
31175a9d0af9SMatthew G. Knepley   /* Setup hybrid structure */
31185a9d0af9SMatthew G. Knepley   {
31195a9d0af9SMatthew G. Knepley     const PetscInt *gpoints;
31205a9d0af9SMatthew G. Knepley     PetscInt        depth, n, d;
31215a9d0af9SMatthew G. Knepley 
31225a9d0af9SMatthew G. Knepley     for (d = 0; d <= dim; ++d) {pmesh->hybridPointMax[d] = mesh->hybridPointMax[d];}
31235a9d0af9SMatthew G. Knepley     ierr = MPI_Bcast(pmesh->hybridPointMax, dim+1, MPIU_INT, 0, comm);CHKERRQ(ierr);
31245a9d0af9SMatthew G. Knepley     ierr = ISLocalToGlobalMappingGetSize(renumbering, &n);CHKERRQ(ierr);
31255a9d0af9SMatthew G. Knepley     ierr = ISLocalToGlobalMappingGetIndices(renumbering, &gpoints);CHKERRQ(ierr);
31265a9d0af9SMatthew G. Knepley     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
31275a9d0af9SMatthew G. Knepley     for (d = 0; d <= dim; ++d) {
31285a9d0af9SMatthew G. Knepley       PetscInt pmax = pmesh->hybridPointMax[d], newmax = 0, pEnd, stratum[2], p;
31295a9d0af9SMatthew G. Knepley 
31305a9d0af9SMatthew G. Knepley       if (pmax < 0) continue;
31315a9d0af9SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, d > depth ? depth : d, &stratum[0], &stratum[1]);CHKERRQ(ierr);
31326dfbe1fcSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(*dmParallel, d, NULL, &pEnd);CHKERRQ(ierr);
31335a9d0af9SMatthew G. Knepley       ierr = MPI_Bcast(stratum, 2, MPIU_INT, 0, comm);CHKERRQ(ierr);
31345a9d0af9SMatthew G. Knepley       for (p = 0; p < n; ++p) {
31355a9d0af9SMatthew G. Knepley         const PetscInt point = gpoints[p];
31365a9d0af9SMatthew G. Knepley 
31375a9d0af9SMatthew G. Knepley         if ((point >= stratum[0]) && (point < stratum[1]) && (point >= pmax)) ++newmax;
31385a9d0af9SMatthew G. Knepley       }
31395a9d0af9SMatthew G. Knepley       if (newmax > 0) pmesh->hybridPointMax[d] = pEnd - newmax;
31405a9d0af9SMatthew G. Knepley       else            pmesh->hybridPointMax[d] = -1;
31415a9d0af9SMatthew G. Knepley     }
31425a9d0af9SMatthew G. Knepley     ierr = ISLocalToGlobalMappingRestoreIndices(renumbering, &gpoints);CHKERRQ(ierr);
31435a9d0af9SMatthew G. Knepley   }
3144552f7358SJed Brown   /* Cleanup Partition */
3145552f7358SJed Brown   ierr = ISLocalToGlobalMappingDestroy(&renumbering);CHKERRQ(ierr);
3146552f7358SJed Brown   ierr = PetscSFDestroy(&partSF);CHKERRQ(ierr);
3147552f7358SJed Brown   ierr = PetscSectionDestroy(&partSection);CHKERRQ(ierr);
3148552f7358SJed Brown   ierr = ISDestroy(&part);CHKERRQ(ierr);
3149552f7358SJed Brown   /* Create point SF for parallel mesh */
3150b96ba592SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
3151552f7358SJed Brown   {
3152552f7358SJed Brown     const PetscInt *leaves;
3153552f7358SJed Brown     PetscSFNode    *remotePoints, *rowners, *lowners;
3154552f7358SJed Brown     PetscInt        numRoots, numLeaves, numGhostPoints = 0, p, gp, *ghostPoints;
3155552f7358SJed Brown     PetscInt        pStart, pEnd;
3156552f7358SJed Brown 
3157552f7358SJed Brown     ierr = DMPlexGetChart(*dmParallel, &pStart, &pEnd);CHKERRQ(ierr);
31580298fd71SBarry Smith     ierr = PetscSFGetGraph(pointSF, &numRoots, &numLeaves, &leaves, NULL);CHKERRQ(ierr);
3159dcca6d9dSJed Brown     ierr = PetscMalloc2(numRoots,&rowners,numLeaves,&lowners);CHKERRQ(ierr);
3160552f7358SJed Brown     for (p=0; p<numRoots; p++) {
3161552f7358SJed Brown       rowners[p].rank  = -1;
3162552f7358SJed Brown       rowners[p].index = -1;
3163552f7358SJed Brown     }
3164552f7358SJed Brown     if (origCellPart) {
3165057e8d19SMichael Lange       /* Make sure points in the original partition are not assigned to other procs */
3166057e8d19SMichael Lange       const PetscInt *origPoints;
3167552f7358SJed Brown 
3168057e8d19SMichael Lange       ierr = DMPlexCreatePartitionClosure(dm, origCellPartSection, origCellPart, &origPartSection, &origPart);CHKERRQ(ierr);
3169057e8d19SMichael Lange       ierr = ISGetIndices(origPart, &origPoints);CHKERRQ(ierr);
3170552f7358SJed Brown       for (p = 0; p < numProcs; ++p) {
3171552f7358SJed Brown         PetscInt dof, off, d;
3172552f7358SJed Brown 
3173057e8d19SMichael Lange         ierr = PetscSectionGetDof(origPartSection, p, &dof);CHKERRQ(ierr);
3174057e8d19SMichael Lange         ierr = PetscSectionGetOffset(origPartSection, p, &off);CHKERRQ(ierr);
3175552f7358SJed Brown         for (d = off; d < off+dof; ++d) {
3176057e8d19SMichael Lange           rowners[origPoints[d]].rank = p;
3177552f7358SJed Brown         }
3178552f7358SJed Brown       }
3179057e8d19SMichael Lange       ierr = ISRestoreIndices(origPart, &origPoints);CHKERRQ(ierr);
3180057e8d19SMichael Lange       ierr = ISDestroy(&origPart);CHKERRQ(ierr);
3181057e8d19SMichael Lange       ierr = PetscSectionDestroy(&origPartSection);CHKERRQ(ierr);
3182552f7358SJed Brown     }
3183552f7358SJed Brown     ierr = ISDestroy(&origCellPart);CHKERRQ(ierr);
3184552f7358SJed Brown     ierr = PetscSectionDestroy(&origCellPartSection);CHKERRQ(ierr);
3185552f7358SJed Brown 
3186552f7358SJed Brown     ierr = PetscSFBcastBegin(pointSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
3187552f7358SJed Brown     ierr = PetscSFBcastEnd(pointSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
3188552f7358SJed Brown     for (p = 0; p < numLeaves; ++p) {
3189552f7358SJed Brown       if (lowners[p].rank < 0 || lowners[p].rank == rank) { /* Either put in a bid or we know we own it */
3190552f7358SJed Brown         lowners[p].rank  = rank;
3191552f7358SJed Brown         lowners[p].index = leaves ? leaves[p] : p;
3192552f7358SJed Brown       } else if (lowners[p].rank >= 0) { /* Point already claimed so flag so that MAXLOC does not listen to us */
3193552f7358SJed Brown         lowners[p].rank  = -2;
3194552f7358SJed Brown         lowners[p].index = -2;
3195552f7358SJed Brown       }
3196552f7358SJed Brown     }
3197552f7358SJed Brown     for (p=0; p<numRoots; p++) { /* Root must not participate in the rediction, flag so that MAXLOC does not use */
3198552f7358SJed Brown       rowners[p].rank  = -3;
3199552f7358SJed Brown       rowners[p].index = -3;
3200552f7358SJed Brown     }
3201552f7358SJed Brown     ierr = PetscSFReduceBegin(pointSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
3202552f7358SJed Brown     ierr = PetscSFReduceEnd(pointSF, MPIU_2INT, lowners, rowners, MPI_MAXLOC);CHKERRQ(ierr);
3203552f7358SJed Brown     ierr = PetscSFBcastBegin(pointSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
3204552f7358SJed Brown     ierr = PetscSFBcastEnd(pointSF, MPIU_2INT, rowners, lowners);CHKERRQ(ierr);
3205552f7358SJed Brown     for (p = 0; p < numLeaves; ++p) {
3206552f7358SJed Brown       if (lowners[p].rank < 0 || lowners[p].index < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cell partition corrupt: point not claimed");
3207552f7358SJed Brown       if (lowners[p].rank != rank) ++numGhostPoints;
3208552f7358SJed Brown     }
3209785e854fSJed Brown     ierr = PetscMalloc1(numGhostPoints,    &ghostPoints);CHKERRQ(ierr);
3210785e854fSJed Brown     ierr = PetscMalloc1(numGhostPoints, &remotePoints);CHKERRQ(ierr);
3211552f7358SJed Brown     for (p = 0, gp = 0; p < numLeaves; ++p) {
3212552f7358SJed Brown       if (lowners[p].rank != rank) {
3213552f7358SJed Brown         ghostPoints[gp]        = leaves ? leaves[p] : p;
3214552f7358SJed Brown         remotePoints[gp].rank  = lowners[p].rank;
3215552f7358SJed Brown         remotePoints[gp].index = lowners[p].index;
3216552f7358SJed Brown         ++gp;
3217552f7358SJed Brown       }
3218552f7358SJed Brown     }
3219552f7358SJed Brown     ierr = PetscFree2(rowners,lowners);CHKERRQ(ierr);
3220552f7358SJed Brown     ierr = PetscSFSetGraph((*dmParallel)->sf, pEnd - pStart, numGhostPoints, ghostPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3221552f7358SJed Brown     ierr = PetscSFSetFromOptions((*dmParallel)->sf);CHKERRQ(ierr);
3222552f7358SJed Brown   }
3223b96ba592SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_DistributeSF,dm,0,0,0);CHKERRQ(ierr);
3224552f7358SJed Brown   /* Cleanup */
3225e7ba9e7aSMatthew G. Knepley   if (sf) {*sf = pointSF;}
3226e7ba9e7aSMatthew G. Knepley   else    {ierr = PetscSFDestroy(&pointSF);CHKERRQ(ierr);}
3227552f7358SJed Brown   ierr = DMSetFromOptions(*dmParallel);CHKERRQ(ierr);
3228552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Distribute,dm,0,0,0);CHKERRQ(ierr);
3229552f7358SJed Brown   PetscFunctionReturn(0);
3230552f7358SJed Brown }
3231552f7358SJed Brown 
32322e1b13c2SMatthew G. Knepley #undef __FUNCT__
32339db3b87dSMatthew G. Knepley #define __FUNCT__ "DMPlexInvertCell"
32349db3b87dSMatthew G. Knepley /*@C
32359db3b87dSMatthew G. Knepley   DMPlexInvertCell - This flips tetrahedron and hexahedron orientation since Plex stores them internally with outward normals. Other cells are left untouched.
32369db3b87dSMatthew G. Knepley 
32379db3b87dSMatthew G. Knepley   Input Parameters:
32389db3b87dSMatthew G. Knepley + numCorners - The number of vertices in a cell
32399db3b87dSMatthew G. Knepley - cone - The incoming cone
32409db3b87dSMatthew G. Knepley 
32419db3b87dSMatthew G. Knepley   Output Parameter:
32429db3b87dSMatthew G. Knepley . cone - The inverted cone (in-place)
32439db3b87dSMatthew G. Knepley 
32449db3b87dSMatthew G. Knepley   Level: developer
32459db3b87dSMatthew G. Knepley 
32469db3b87dSMatthew G. Knepley .seealso: DMPlexGenerate()
32479db3b87dSMatthew G. Knepley @*/
32489db3b87dSMatthew G. Knepley PetscErrorCode DMPlexInvertCell(PetscInt dim, PetscInt numCorners, int cone[])
32492e1b13c2SMatthew G. Knepley {
32509db3b87dSMatthew G. Knepley   int tmpc;
32512e1b13c2SMatthew G. Knepley 
32522e1b13c2SMatthew G. Knepley   PetscFunctionBegin;
32539db3b87dSMatthew G. Knepley   if (dim != 3) PetscFunctionReturn(0);
32549db3b87dSMatthew G. Knepley   switch (numCorners) {
32559db3b87dSMatthew G. Knepley   case 4:
32561885f254SMatthew G. Knepley     tmpc    = cone[0];
32571885f254SMatthew G. Knepley     cone[0] = cone[1];
32581885f254SMatthew G. Knepley     cone[1] = tmpc;
32599db3b87dSMatthew G. Knepley     break;
32609db3b87dSMatthew G. Knepley   case 8:
32619db3b87dSMatthew G. Knepley     tmpc    = cone[1];
32629db3b87dSMatthew G. Knepley     cone[1] = cone[3];
32639db3b87dSMatthew G. Knepley     cone[3] = tmpc;
32649db3b87dSMatthew G. Knepley     break;
32659db3b87dSMatthew G. Knepley   default: break;
32669db3b87dSMatthew G. Knepley   }
32679db3b87dSMatthew G. Knepley   PetscFunctionReturn(0);
32689db3b87dSMatthew G. Knepley }
32699db3b87dSMatthew G. Knepley 
32709db3b87dSMatthew G. Knepley #undef __FUNCT__
32719db3b87dSMatthew G. Knepley #define __FUNCT__ "DMPlexInvertCells_Internal"
32729db3b87dSMatthew G. Knepley /* This is to fix the tetrahedron orientation from TetGen */
32739db3b87dSMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexInvertCells_Internal(PetscInt dim, PetscInt numCells, PetscInt numCorners, int cells[])
32749db3b87dSMatthew G. Knepley {
32759db3b87dSMatthew G. Knepley   PetscInt       bound = numCells*numCorners, coff;
32769db3b87dSMatthew G. Knepley   PetscErrorCode ierr;
32779db3b87dSMatthew G. Knepley 
32789db3b87dSMatthew G. Knepley   PetscFunctionBegin;
32799db3b87dSMatthew G. Knepley   for (coff = 0; coff < bound; coff += numCorners) {
32809db3b87dSMatthew G. Knepley     ierr = DMPlexInvertCell(dim, numCorners, &cells[coff]);CHKERRQ(ierr);
32812e1b13c2SMatthew G. Knepley   }
32822e1b13c2SMatthew G. Knepley   PetscFunctionReturn(0);
32832e1b13c2SMatthew G. Knepley }
32842e1b13c2SMatthew G. Knepley 
3285519f805aSKarl Rupp #if defined(PETSC_HAVE_TRIANGLE)
3286552f7358SJed Brown #include <triangle.h>
3287552f7358SJed Brown 
3288552f7358SJed Brown #undef __FUNCT__
3289552f7358SJed Brown #define __FUNCT__ "InitInput_Triangle"
32900adebc6cSBarry Smith PetscErrorCode InitInput_Triangle(struct triangulateio *inputCtx)
32910adebc6cSBarry Smith {
3292552f7358SJed Brown   PetscFunctionBegin;
3293552f7358SJed Brown   inputCtx->numberofpoints             = 0;
3294552f7358SJed Brown   inputCtx->numberofpointattributes    = 0;
32950298fd71SBarry Smith   inputCtx->pointlist                  = NULL;
32960298fd71SBarry Smith   inputCtx->pointattributelist         = NULL;
32970298fd71SBarry Smith   inputCtx->pointmarkerlist            = NULL;
3298552f7358SJed Brown   inputCtx->numberofsegments           = 0;
32990298fd71SBarry Smith   inputCtx->segmentlist                = NULL;
33000298fd71SBarry Smith   inputCtx->segmentmarkerlist          = NULL;
3301552f7358SJed Brown   inputCtx->numberoftriangleattributes = 0;
33020298fd71SBarry Smith   inputCtx->trianglelist               = NULL;
3303552f7358SJed Brown   inputCtx->numberofholes              = 0;
33040298fd71SBarry Smith   inputCtx->holelist                   = NULL;
3305552f7358SJed Brown   inputCtx->numberofregions            = 0;
33060298fd71SBarry Smith   inputCtx->regionlist                 = NULL;
3307552f7358SJed Brown   PetscFunctionReturn(0);
3308552f7358SJed Brown }
3309552f7358SJed Brown 
3310552f7358SJed Brown #undef __FUNCT__
3311552f7358SJed Brown #define __FUNCT__ "InitOutput_Triangle"
33120adebc6cSBarry Smith PetscErrorCode InitOutput_Triangle(struct triangulateio *outputCtx)
33130adebc6cSBarry Smith {
3314552f7358SJed Brown   PetscFunctionBegin;
3315552f7358SJed Brown   outputCtx->numberofpoints        = 0;
33160298fd71SBarry Smith   outputCtx->pointlist             = NULL;
33170298fd71SBarry Smith   outputCtx->pointattributelist    = NULL;
33180298fd71SBarry Smith   outputCtx->pointmarkerlist       = NULL;
3319552f7358SJed Brown   outputCtx->numberoftriangles     = 0;
33200298fd71SBarry Smith   outputCtx->trianglelist          = NULL;
33210298fd71SBarry Smith   outputCtx->triangleattributelist = NULL;
33220298fd71SBarry Smith   outputCtx->neighborlist          = NULL;
33230298fd71SBarry Smith   outputCtx->segmentlist           = NULL;
33240298fd71SBarry Smith   outputCtx->segmentmarkerlist     = NULL;
3325552f7358SJed Brown   outputCtx->numberofedges         = 0;
33260298fd71SBarry Smith   outputCtx->edgelist              = NULL;
33270298fd71SBarry Smith   outputCtx->edgemarkerlist        = NULL;
3328552f7358SJed Brown   PetscFunctionReturn(0);
3329552f7358SJed Brown }
3330552f7358SJed Brown 
3331552f7358SJed Brown #undef __FUNCT__
3332552f7358SJed Brown #define __FUNCT__ "FiniOutput_Triangle"
33330adebc6cSBarry Smith PetscErrorCode FiniOutput_Triangle(struct triangulateio *outputCtx)
33340adebc6cSBarry Smith {
3335552f7358SJed Brown   PetscFunctionBegin;
3336552f7358SJed Brown   free(outputCtx->pointmarkerlist);
3337552f7358SJed Brown   free(outputCtx->edgelist);
3338552f7358SJed Brown   free(outputCtx->edgemarkerlist);
3339552f7358SJed Brown   free(outputCtx->trianglelist);
3340552f7358SJed Brown   free(outputCtx->neighborlist);
3341552f7358SJed Brown   PetscFunctionReturn(0);
3342552f7358SJed Brown }
3343552f7358SJed Brown 
3344552f7358SJed Brown #undef __FUNCT__
3345552f7358SJed Brown #define __FUNCT__ "DMPlexGenerate_Triangle"
3346552f7358SJed Brown PetscErrorCode DMPlexGenerate_Triangle(DM boundary, PetscBool interpolate, DM *dm)
3347552f7358SJed Brown {
334882f516ccSBarry Smith   MPI_Comm             comm;
3349552f7358SJed Brown   PetscInt             dim              = 2;
3350552f7358SJed Brown   const PetscBool      createConvexHull = PETSC_FALSE;
3351552f7358SJed Brown   const PetscBool      constrained      = PETSC_FALSE;
3352552f7358SJed Brown   struct triangulateio in;
3353552f7358SJed Brown   struct triangulateio out;
3354552f7358SJed Brown   PetscInt             vStart, vEnd, v, eStart, eEnd, e;
3355552f7358SJed Brown   PetscMPIInt          rank;
3356552f7358SJed Brown   PetscErrorCode       ierr;
3357552f7358SJed Brown 
3358552f7358SJed Brown   PetscFunctionBegin;
335982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)boundary,&comm);CHKERRQ(ierr);
3360552f7358SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3361552f7358SJed Brown   ierr = InitInput_Triangle(&in);CHKERRQ(ierr);
3362552f7358SJed Brown   ierr = InitOutput_Triangle(&out);CHKERRQ(ierr);
3363552f7358SJed Brown   ierr = DMPlexGetDepthStratum(boundary, 0, &vStart, &vEnd);CHKERRQ(ierr);
33640d644c17SKarl Rupp 
3365552f7358SJed Brown   in.numberofpoints = vEnd - vStart;
3366552f7358SJed Brown   if (in.numberofpoints > 0) {
3367552f7358SJed Brown     PetscSection coordSection;
3368552f7358SJed Brown     Vec          coordinates;
3369552f7358SJed Brown     PetscScalar *array;
3370552f7358SJed Brown 
3371785e854fSJed Brown     ierr = PetscMalloc1(in.numberofpoints*dim, &in.pointlist);CHKERRQ(ierr);
3372785e854fSJed Brown     ierr = PetscMalloc1(in.numberofpoints, &in.pointmarkerlist);CHKERRQ(ierr);
3373552f7358SJed Brown     ierr = DMGetCoordinatesLocal(boundary, &coordinates);CHKERRQ(ierr);
3374552f7358SJed Brown     ierr = DMPlexGetCoordinateSection(boundary, &coordSection);CHKERRQ(ierr);
3375552f7358SJed Brown     ierr = VecGetArray(coordinates, &array);CHKERRQ(ierr);
3376552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
3377552f7358SJed Brown       const PetscInt idx = v - vStart;
3378552f7358SJed Brown       PetscInt       off, d;
3379552f7358SJed Brown 
3380552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
3381552f7358SJed Brown       for (d = 0; d < dim; ++d) {
3382552f7358SJed Brown         in.pointlist[idx*dim + d] = PetscRealPart(array[off+d]);
3383552f7358SJed Brown       }
3384552f7358SJed Brown       ierr = DMPlexGetLabelValue(boundary, "marker", v, &in.pointmarkerlist[idx]);CHKERRQ(ierr);
3385552f7358SJed Brown     }
3386552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &array);CHKERRQ(ierr);
3387552f7358SJed Brown   }
3388552f7358SJed Brown   ierr  = DMPlexGetHeightStratum(boundary, 0, &eStart, &eEnd);CHKERRQ(ierr);
3389552f7358SJed Brown   in.numberofsegments = eEnd - eStart;
3390552f7358SJed Brown   if (in.numberofsegments > 0) {
3391785e854fSJed Brown     ierr = PetscMalloc1(in.numberofsegments*2, &in.segmentlist);CHKERRQ(ierr);
3392785e854fSJed Brown     ierr = PetscMalloc1(in.numberofsegments, &in.segmentmarkerlist);CHKERRQ(ierr);
3393552f7358SJed Brown     for (e = eStart; e < eEnd; ++e) {
3394552f7358SJed Brown       const PetscInt  idx = e - eStart;
3395552f7358SJed Brown       const PetscInt *cone;
3396552f7358SJed Brown 
3397552f7358SJed Brown       ierr = DMPlexGetCone(boundary, e, &cone);CHKERRQ(ierr);
33980d644c17SKarl Rupp 
3399552f7358SJed Brown       in.segmentlist[idx*2+0] = cone[0] - vStart;
3400552f7358SJed Brown       in.segmentlist[idx*2+1] = cone[1] - vStart;
34010d644c17SKarl Rupp 
3402552f7358SJed Brown       ierr = DMPlexGetLabelValue(boundary, "marker", e, &in.segmentmarkerlist[idx]);CHKERRQ(ierr);
3403552f7358SJed Brown     }
3404552f7358SJed Brown   }
3405552f7358SJed Brown #if 0 /* Do not currently support holes */
3406552f7358SJed Brown   PetscReal *holeCoords;
3407552f7358SJed Brown   PetscInt   h, d;
3408552f7358SJed Brown 
3409552f7358SJed Brown   ierr = DMPlexGetHoles(boundary, &in.numberofholes, &holeCords);CHKERRQ(ierr);
3410552f7358SJed Brown   if (in.numberofholes > 0) {
3411785e854fSJed Brown     ierr = PetscMalloc1(in.numberofholes*dim, &in.holelist);CHKERRQ(ierr);
3412552f7358SJed Brown     for (h = 0; h < in.numberofholes; ++h) {
3413552f7358SJed Brown       for (d = 0; d < dim; ++d) {
3414552f7358SJed Brown         in.holelist[h*dim+d] = holeCoords[h*dim+d];
3415552f7358SJed Brown       }
3416552f7358SJed Brown     }
3417552f7358SJed Brown   }
3418552f7358SJed Brown #endif
3419552f7358SJed Brown   if (!rank) {
3420552f7358SJed Brown     char args[32];
3421552f7358SJed Brown 
3422552f7358SJed Brown     /* Take away 'Q' for verbose output */
3423552f7358SJed Brown     ierr = PetscStrcpy(args, "pqezQ");CHKERRQ(ierr);
3424552f7358SJed Brown     if (createConvexHull) {
3425552f7358SJed Brown       ierr = PetscStrcat(args, "c");CHKERRQ(ierr);
3426552f7358SJed Brown     }
3427552f7358SJed Brown     if (constrained) {
3428552f7358SJed Brown       ierr = PetscStrcpy(args, "zepDQ");CHKERRQ(ierr);
3429552f7358SJed Brown     }
34300298fd71SBarry Smith     triangulate(args, &in, &out, NULL);
3431552f7358SJed Brown   }
3432552f7358SJed Brown   ierr = PetscFree(in.pointlist);CHKERRQ(ierr);
3433552f7358SJed Brown   ierr = PetscFree(in.pointmarkerlist);CHKERRQ(ierr);
3434552f7358SJed Brown   ierr = PetscFree(in.segmentlist);CHKERRQ(ierr);
3435552f7358SJed Brown   ierr = PetscFree(in.segmentmarkerlist);CHKERRQ(ierr);
3436552f7358SJed Brown   ierr = PetscFree(in.holelist);CHKERRQ(ierr);
3437552f7358SJed Brown 
3438552f7358SJed Brown   {
3439552f7358SJed Brown     const PetscInt numCorners  = 3;
3440552f7358SJed Brown     const PetscInt numCells    = out.numberoftriangles;
3441552f7358SJed Brown     const PetscInt numVertices = out.numberofpoints;
3442552f7358SJed Brown     const int     *cells      = out.trianglelist;
3443552f7358SJed Brown     const double  *meshCoords = out.pointlist;
3444552f7358SJed Brown 
34450fde5641SMatthew G Knepley     ierr = DMPlexCreateFromCellList(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dm);CHKERRQ(ierr);
3446552f7358SJed Brown     /* Set labels */
3447552f7358SJed Brown     for (v = 0; v < numVertices; ++v) {
3448552f7358SJed Brown       if (out.pointmarkerlist[v]) {
3449552f7358SJed Brown         ierr = DMPlexSetLabelValue(*dm, "marker", v+numCells, out.pointmarkerlist[v]);CHKERRQ(ierr);
3450552f7358SJed Brown       }
3451552f7358SJed Brown     }
3452552f7358SJed Brown     if (interpolate) {
3453552f7358SJed Brown       for (e = 0; e < out.numberofedges; e++) {
3454552f7358SJed Brown         if (out.edgemarkerlist[e]) {
3455552f7358SJed Brown           const PetscInt  vertices[2] = {out.edgelist[e*2+0]+numCells, out.edgelist[e*2+1]+numCells};
3456552f7358SJed Brown           const PetscInt *edges;
3457552f7358SJed Brown           PetscInt        numEdges;
3458552f7358SJed Brown 
3459552f7358SJed Brown           ierr = DMPlexGetJoin(*dm, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3460552f7358SJed Brown           if (numEdges != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %D", numEdges);
3461552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dm, "marker", edges[0], out.edgemarkerlist[e]);CHKERRQ(ierr);
3462552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dm, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3463552f7358SJed Brown         }
3464552f7358SJed Brown       }
3465552f7358SJed Brown     }
3466552f7358SJed Brown     ierr = DMPlexSetRefinementUniform(*dm, PETSC_FALSE);CHKERRQ(ierr);
3467552f7358SJed Brown   }
3468552f7358SJed Brown #if 0 /* Do not currently support holes */
3469552f7358SJed Brown   ierr = DMPlexCopyHoles(*dm, boundary);CHKERRQ(ierr);
3470552f7358SJed Brown #endif
3471552f7358SJed Brown   ierr = FiniOutput_Triangle(&out);CHKERRQ(ierr);
3472552f7358SJed Brown   PetscFunctionReturn(0);
3473552f7358SJed Brown }
3474552f7358SJed Brown 
3475552f7358SJed Brown #undef __FUNCT__
3476552f7358SJed Brown #define __FUNCT__ "DMPlexRefine_Triangle"
3477552f7358SJed Brown PetscErrorCode DMPlexRefine_Triangle(DM dm, double *maxVolumes, DM *dmRefined)
3478552f7358SJed Brown {
347982f516ccSBarry Smith   MPI_Comm             comm;
3480552f7358SJed Brown   PetscInt             dim  = 2;
3481552f7358SJed Brown   struct triangulateio in;
3482552f7358SJed Brown   struct triangulateio out;
3483552f7358SJed Brown   PetscInt             vStart, vEnd, v, cStart, cEnd, c, depth, depthGlobal;
3484552f7358SJed Brown   PetscMPIInt          rank;
3485552f7358SJed Brown   PetscErrorCode       ierr;
3486552f7358SJed Brown 
3487552f7358SJed Brown   PetscFunctionBegin;
348882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3489552f7358SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3490552f7358SJed Brown   ierr = InitInput_Triangle(&in);CHKERRQ(ierr);
3491552f7358SJed Brown   ierr = InitOutput_Triangle(&out);CHKERRQ(ierr);
3492552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3493552f7358SJed Brown   ierr = MPI_Allreduce(&depth, &depthGlobal, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
3494552f7358SJed Brown   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
34950d644c17SKarl Rupp 
3496552f7358SJed Brown   in.numberofpoints = vEnd - vStart;
3497552f7358SJed Brown   if (in.numberofpoints > 0) {
3498552f7358SJed Brown     PetscSection coordSection;
3499552f7358SJed Brown     Vec          coordinates;
3500552f7358SJed Brown     PetscScalar *array;
3501552f7358SJed Brown 
3502785e854fSJed Brown     ierr = PetscMalloc1(in.numberofpoints*dim, &in.pointlist);CHKERRQ(ierr);
3503785e854fSJed Brown     ierr = PetscMalloc1(in.numberofpoints, &in.pointmarkerlist);CHKERRQ(ierr);
3504552f7358SJed Brown     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
3505552f7358SJed Brown     ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3506552f7358SJed Brown     ierr = VecGetArray(coordinates, &array);CHKERRQ(ierr);
3507552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
3508552f7358SJed Brown       const PetscInt idx = v - vStart;
3509552f7358SJed Brown       PetscInt       off, d;
3510552f7358SJed Brown 
3511552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
3512552f7358SJed Brown       for (d = 0; d < dim; ++d) {
3513552f7358SJed Brown         in.pointlist[idx*dim + d] = PetscRealPart(array[off+d]);
3514552f7358SJed Brown       }
3515552f7358SJed Brown       ierr = DMPlexGetLabelValue(dm, "marker", v, &in.pointmarkerlist[idx]);CHKERRQ(ierr);
3516552f7358SJed Brown     }
3517552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &array);CHKERRQ(ierr);
3518552f7358SJed Brown   }
3519552f7358SJed Brown   ierr  = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
35200d644c17SKarl Rupp 
3521552f7358SJed Brown   in.numberofcorners   = 3;
3522552f7358SJed Brown   in.numberoftriangles = cEnd - cStart;
35230d644c17SKarl Rupp 
3524552f7358SJed Brown   in.trianglearealist  = (double*) maxVolumes;
3525552f7358SJed Brown   if (in.numberoftriangles > 0) {
3526785e854fSJed Brown     ierr = PetscMalloc1(in.numberoftriangles*in.numberofcorners, &in.trianglelist);CHKERRQ(ierr);
3527552f7358SJed Brown     for (c = cStart; c < cEnd; ++c) {
3528552f7358SJed Brown       const PetscInt idx      = c - cStart;
35290298fd71SBarry Smith       PetscInt      *closure = NULL;
3530552f7358SJed Brown       PetscInt       closureSize;
3531552f7358SJed Brown 
3532552f7358SJed Brown       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3533552f7358SJed Brown       if ((closureSize != 4) && (closureSize != 7)) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Mesh has cell which is not a triangle, %D vertices in closure", closureSize);
3534552f7358SJed Brown       for (v = 0; v < 3; ++v) {
3535552f7358SJed Brown         in.trianglelist[idx*in.numberofcorners + v] = closure[(v+closureSize-3)*2] - vStart;
3536552f7358SJed Brown       }
3537552f7358SJed Brown       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3538552f7358SJed Brown     }
3539552f7358SJed Brown   }
3540552f7358SJed Brown   /* TODO: Segment markers are missing on input */
3541552f7358SJed Brown #if 0 /* Do not currently support holes */
3542552f7358SJed Brown   PetscReal *holeCoords;
3543552f7358SJed Brown   PetscInt   h, d;
3544552f7358SJed Brown 
3545552f7358SJed Brown   ierr = DMPlexGetHoles(boundary, &in.numberofholes, &holeCords);CHKERRQ(ierr);
3546552f7358SJed Brown   if (in.numberofholes > 0) {
3547785e854fSJed Brown     ierr = PetscMalloc1(in.numberofholes*dim, &in.holelist);CHKERRQ(ierr);
3548552f7358SJed Brown     for (h = 0; h < in.numberofholes; ++h) {
3549552f7358SJed Brown       for (d = 0; d < dim; ++d) {
3550552f7358SJed Brown         in.holelist[h*dim+d] = holeCoords[h*dim+d];
3551552f7358SJed Brown       }
3552552f7358SJed Brown     }
3553552f7358SJed Brown   }
3554552f7358SJed Brown #endif
3555552f7358SJed Brown   if (!rank) {
3556552f7358SJed Brown     char args[32];
3557552f7358SJed Brown 
3558552f7358SJed Brown     /* Take away 'Q' for verbose output */
3559552f7358SJed Brown     ierr = PetscStrcpy(args, "pqezQra");CHKERRQ(ierr);
35600298fd71SBarry Smith     triangulate(args, &in, &out, NULL);
3561552f7358SJed Brown   }
3562552f7358SJed Brown   ierr = PetscFree(in.pointlist);CHKERRQ(ierr);
3563552f7358SJed Brown   ierr = PetscFree(in.pointmarkerlist);CHKERRQ(ierr);
3564552f7358SJed Brown   ierr = PetscFree(in.segmentlist);CHKERRQ(ierr);
3565552f7358SJed Brown   ierr = PetscFree(in.segmentmarkerlist);CHKERRQ(ierr);
3566552f7358SJed Brown   ierr = PetscFree(in.trianglelist);CHKERRQ(ierr);
3567552f7358SJed Brown 
3568552f7358SJed Brown   {
3569552f7358SJed Brown     const PetscInt numCorners  = 3;
3570552f7358SJed Brown     const PetscInt numCells    = out.numberoftriangles;
3571552f7358SJed Brown     const PetscInt numVertices = out.numberofpoints;
3572552f7358SJed Brown     const int     *cells      = out.trianglelist;
3573552f7358SJed Brown     const double  *meshCoords = out.pointlist;
3574552f7358SJed Brown     PetscBool      interpolate = depthGlobal > 1 ? PETSC_TRUE : PETSC_FALSE;
3575552f7358SJed Brown 
35760fde5641SMatthew G Knepley     ierr = DMPlexCreateFromCellList(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dmRefined);CHKERRQ(ierr);
3577552f7358SJed Brown     /* Set labels */
3578552f7358SJed Brown     for (v = 0; v < numVertices; ++v) {
3579552f7358SJed Brown       if (out.pointmarkerlist[v]) {
3580552f7358SJed Brown         ierr = DMPlexSetLabelValue(*dmRefined, "marker", v+numCells, out.pointmarkerlist[v]);CHKERRQ(ierr);
3581552f7358SJed Brown       }
3582552f7358SJed Brown     }
3583552f7358SJed Brown     if (interpolate) {
3584552f7358SJed Brown       PetscInt e;
3585552f7358SJed Brown 
3586552f7358SJed Brown       for (e = 0; e < out.numberofedges; e++) {
3587552f7358SJed Brown         if (out.edgemarkerlist[e]) {
3588552f7358SJed Brown           const PetscInt  vertices[2] = {out.edgelist[e*2+0]+numCells, out.edgelist[e*2+1]+numCells};
3589552f7358SJed Brown           const PetscInt *edges;
3590552f7358SJed Brown           PetscInt        numEdges;
3591552f7358SJed Brown 
3592552f7358SJed Brown           ierr = DMPlexGetJoin(*dmRefined, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3593552f7358SJed Brown           if (numEdges != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %D", numEdges);
3594552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dmRefined, "marker", edges[0], out.edgemarkerlist[e]);CHKERRQ(ierr);
3595552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dmRefined, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3596552f7358SJed Brown         }
3597552f7358SJed Brown       }
3598552f7358SJed Brown     }
3599552f7358SJed Brown     ierr = DMPlexSetRefinementUniform(*dmRefined, PETSC_FALSE);CHKERRQ(ierr);
3600552f7358SJed Brown   }
3601552f7358SJed Brown #if 0 /* Do not currently support holes */
3602552f7358SJed Brown   ierr = DMPlexCopyHoles(*dm, boundary);CHKERRQ(ierr);
3603552f7358SJed Brown #endif
3604552f7358SJed Brown   ierr = FiniOutput_Triangle(&out);CHKERRQ(ierr);
3605552f7358SJed Brown   PetscFunctionReturn(0);
3606552f7358SJed Brown }
3607552f7358SJed Brown #endif
3608552f7358SJed Brown 
3609519f805aSKarl Rupp #if defined(PETSC_HAVE_TETGEN)
3610552f7358SJed Brown #include <tetgen.h>
3611552f7358SJed Brown #undef __FUNCT__
3612552f7358SJed Brown #define __FUNCT__ "DMPlexGenerate_Tetgen"
3613552f7358SJed Brown PetscErrorCode DMPlexGenerate_Tetgen(DM boundary, PetscBool interpolate, DM *dm)
3614552f7358SJed Brown {
361582f516ccSBarry Smith   MPI_Comm       comm;
3616552f7358SJed Brown   const PetscInt dim  = 3;
3617552f7358SJed Brown   ::tetgenio     in;
3618552f7358SJed Brown   ::tetgenio     out;
3619552f7358SJed Brown   PetscInt       vStart, vEnd, v, fStart, fEnd, f;
3620552f7358SJed Brown   PetscMPIInt    rank;
3621552f7358SJed Brown   PetscErrorCode ierr;
3622552f7358SJed Brown 
3623552f7358SJed Brown   PetscFunctionBegin;
362482f516ccSBarry Smith   ierr              = PetscObjectGetComm((PetscObject)boundary,&comm);CHKERRQ(ierr);
3625552f7358SJed Brown   ierr              = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3626552f7358SJed Brown   ierr              = DMPlexGetDepthStratum(boundary, 0, &vStart, &vEnd);CHKERRQ(ierr);
3627552f7358SJed Brown   in.numberofpoints = vEnd - vStart;
3628552f7358SJed Brown   if (in.numberofpoints > 0) {
3629552f7358SJed Brown     PetscSection coordSection;
3630552f7358SJed Brown     Vec          coordinates;
3631552f7358SJed Brown     PetscScalar *array;
3632552f7358SJed Brown 
3633552f7358SJed Brown     in.pointlist       = new double[in.numberofpoints*dim];
3634552f7358SJed Brown     in.pointmarkerlist = new int[in.numberofpoints];
36350d644c17SKarl Rupp 
3636552f7358SJed Brown     ierr = DMGetCoordinatesLocal(boundary, &coordinates);CHKERRQ(ierr);
3637552f7358SJed Brown     ierr = DMPlexGetCoordinateSection(boundary, &coordSection);CHKERRQ(ierr);
3638552f7358SJed Brown     ierr = VecGetArray(coordinates, &array);CHKERRQ(ierr);
3639552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
3640552f7358SJed Brown       const PetscInt idx = v - vStart;
3641552f7358SJed Brown       PetscInt       off, d;
3642552f7358SJed Brown 
3643552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
36440d644c17SKarl Rupp       for (d = 0; d < dim; ++d) in.pointlist[idx*dim + d] = array[off+d];
3645552f7358SJed Brown       ierr = DMPlexGetLabelValue(boundary, "marker", v, &in.pointmarkerlist[idx]);CHKERRQ(ierr);
3646552f7358SJed Brown     }
3647552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &array);CHKERRQ(ierr);
3648552f7358SJed Brown   }
3649552f7358SJed Brown   ierr  = DMPlexGetHeightStratum(boundary, 0, &fStart, &fEnd);CHKERRQ(ierr);
36500d644c17SKarl Rupp 
3651552f7358SJed Brown   in.numberoffacets = fEnd - fStart;
3652552f7358SJed Brown   if (in.numberoffacets > 0) {
3653552f7358SJed Brown     in.facetlist       = new tetgenio::facet[in.numberoffacets];
3654552f7358SJed Brown     in.facetmarkerlist = new int[in.numberoffacets];
3655552f7358SJed Brown     for (f = fStart; f < fEnd; ++f) {
3656552f7358SJed Brown       const PetscInt idx     = f - fStart;
36570298fd71SBarry Smith       PetscInt      *points = NULL, numPoints, p, numVertices = 0, v;
3658552f7358SJed Brown 
3659552f7358SJed Brown       in.facetlist[idx].numberofpolygons = 1;
3660552f7358SJed Brown       in.facetlist[idx].polygonlist      = new tetgenio::polygon[in.facetlist[idx].numberofpolygons];
3661552f7358SJed Brown       in.facetlist[idx].numberofholes    = 0;
3662552f7358SJed Brown       in.facetlist[idx].holelist         = NULL;
3663552f7358SJed Brown 
3664552f7358SJed Brown       ierr = DMPlexGetTransitiveClosure(boundary, f, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
3665552f7358SJed Brown       for (p = 0; p < numPoints*2; p += 2) {
3666552f7358SJed Brown         const PetscInt point = points[p];
36670d644c17SKarl Rupp         if ((point >= vStart) && (point < vEnd)) points[numVertices++] = point;
3668552f7358SJed Brown       }
3669552f7358SJed Brown 
3670552f7358SJed Brown       tetgenio::polygon *poly = in.facetlist[idx].polygonlist;
3671552f7358SJed Brown       poly->numberofvertices = numVertices;
3672552f7358SJed Brown       poly->vertexlist       = new int[poly->numberofvertices];
3673552f7358SJed Brown       for (v = 0; v < numVertices; ++v) {
3674552f7358SJed Brown         const PetscInt vIdx = points[v] - vStart;
3675552f7358SJed Brown         poly->vertexlist[v] = vIdx;
3676552f7358SJed Brown       }
3677552f7358SJed Brown       ierr = DMPlexGetLabelValue(boundary, "marker", f, &in.facetmarkerlist[idx]);CHKERRQ(ierr);
3678552f7358SJed Brown       ierr = DMPlexRestoreTransitiveClosure(boundary, f, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
3679552f7358SJed Brown     }
3680552f7358SJed Brown   }
3681552f7358SJed Brown   if (!rank) {
3682552f7358SJed Brown     char args[32];
3683552f7358SJed Brown 
3684552f7358SJed Brown     /* Take away 'Q' for verbose output */
3685552f7358SJed Brown     ierr = PetscStrcpy(args, "pqezQ");CHKERRQ(ierr);
3686552f7358SJed Brown     ::tetrahedralize(args, &in, &out);
3687552f7358SJed Brown   }
3688552f7358SJed Brown   {
3689552f7358SJed Brown     const PetscInt numCorners  = 4;
3690552f7358SJed Brown     const PetscInt numCells    = out.numberoftetrahedra;
3691552f7358SJed Brown     const PetscInt numVertices = out.numberofpoints;
3692552f7358SJed Brown     const double   *meshCoords = out.pointlist;
36931d4b4d7bSJed Brown     int            *cells      = out.tetrahedronlist;
3694552f7358SJed Brown 
36959db3b87dSMatthew G. Knepley     ierr = DMPlexInvertCells_Internal(dim, numCells, numCorners, cells);CHKERRQ(ierr);
36960fde5641SMatthew G Knepley     ierr = DMPlexCreateFromCellList(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dm);CHKERRQ(ierr);
3697552f7358SJed Brown     /* Set labels */
3698552f7358SJed Brown     for (v = 0; v < numVertices; ++v) {
3699552f7358SJed Brown       if (out.pointmarkerlist[v]) {
3700552f7358SJed Brown         ierr = DMPlexSetLabelValue(*dm, "marker", v+numCells, out.pointmarkerlist[v]);CHKERRQ(ierr);
3701552f7358SJed Brown       }
3702552f7358SJed Brown     }
3703552f7358SJed Brown     if (interpolate) {
3704552f7358SJed Brown       PetscInt e;
3705552f7358SJed Brown 
3706552f7358SJed Brown       for (e = 0; e < out.numberofedges; e++) {
3707552f7358SJed Brown         if (out.edgemarkerlist[e]) {
3708552f7358SJed Brown           const PetscInt  vertices[2] = {out.edgelist[e*2+0]+numCells, out.edgelist[e*2+1]+numCells};
3709552f7358SJed Brown           const PetscInt *edges;
3710552f7358SJed Brown           PetscInt        numEdges;
3711552f7358SJed Brown 
3712552f7358SJed Brown           ierr = DMPlexGetJoin(*dm, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3713552f7358SJed Brown           if (numEdges != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %D", numEdges);
3714552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dm, "marker", edges[0], out.edgemarkerlist[e]);CHKERRQ(ierr);
3715552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dm, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3716552f7358SJed Brown         }
3717552f7358SJed Brown       }
3718552f7358SJed Brown       for (f = 0; f < out.numberoftrifaces; f++) {
3719552f7358SJed Brown         if (out.trifacemarkerlist[f]) {
3720552f7358SJed Brown           const PetscInt  vertices[3] = {out.trifacelist[f*3+0]+numCells, out.trifacelist[f*3+1]+numCells, out.trifacelist[f*3+2]+numCells};
3721552f7358SJed Brown           const PetscInt *faces;
3722552f7358SJed Brown           PetscInt        numFaces;
3723552f7358SJed Brown 
3724552f7358SJed Brown           ierr = DMPlexGetJoin(*dm, 3, vertices, &numFaces, &faces);CHKERRQ(ierr);
3725552f7358SJed Brown           if (numFaces != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Three vertices must cover only one face, not %D", numFaces);
3726552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dm, "marker", faces[0], out.trifacemarkerlist[f]);CHKERRQ(ierr);
3727552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dm, 3, vertices, &numFaces, &faces);CHKERRQ(ierr);
3728552f7358SJed Brown         }
3729552f7358SJed Brown       }
3730552f7358SJed Brown     }
3731552f7358SJed Brown     ierr = DMPlexSetRefinementUniform(*dm, PETSC_FALSE);CHKERRQ(ierr);
3732552f7358SJed Brown   }
3733552f7358SJed Brown   PetscFunctionReturn(0);
3734552f7358SJed Brown }
3735552f7358SJed Brown 
3736552f7358SJed Brown #undef __FUNCT__
3737552f7358SJed Brown #define __FUNCT__ "DMPlexRefine_Tetgen"
3738552f7358SJed Brown PetscErrorCode DMPlexRefine_Tetgen(DM dm, double *maxVolumes, DM *dmRefined)
3739552f7358SJed Brown {
374082f516ccSBarry Smith   MPI_Comm       comm;
3741552f7358SJed Brown   const PetscInt dim  = 3;
3742552f7358SJed Brown   ::tetgenio     in;
3743552f7358SJed Brown   ::tetgenio     out;
3744552f7358SJed Brown   PetscInt       vStart, vEnd, v, cStart, cEnd, c, depth, depthGlobal;
3745552f7358SJed Brown   PetscMPIInt    rank;
3746552f7358SJed Brown   PetscErrorCode ierr;
3747552f7358SJed Brown 
3748552f7358SJed Brown   PetscFunctionBegin;
374982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3750552f7358SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3751552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3752552f7358SJed Brown   ierr = MPI_Allreduce(&depth, &depthGlobal, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
3753552f7358SJed Brown   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
37540d644c17SKarl Rupp 
3755552f7358SJed Brown   in.numberofpoints = vEnd - vStart;
3756552f7358SJed Brown   if (in.numberofpoints > 0) {
3757552f7358SJed Brown     PetscSection coordSection;
3758552f7358SJed Brown     Vec          coordinates;
3759552f7358SJed Brown     PetscScalar *array;
3760552f7358SJed Brown 
3761552f7358SJed Brown     in.pointlist       = new double[in.numberofpoints*dim];
3762552f7358SJed Brown     in.pointmarkerlist = new int[in.numberofpoints];
37630d644c17SKarl Rupp 
3764552f7358SJed Brown     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
3765552f7358SJed Brown     ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3766552f7358SJed Brown     ierr = VecGetArray(coordinates, &array);CHKERRQ(ierr);
3767552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
3768552f7358SJed Brown       const PetscInt idx = v - vStart;
3769552f7358SJed Brown       PetscInt       off, d;
3770552f7358SJed Brown 
3771552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
37720d644c17SKarl Rupp       for (d = 0; d < dim; ++d) in.pointlist[idx*dim + d] = array[off+d];
3773552f7358SJed Brown       ierr = DMPlexGetLabelValue(dm, "marker", v, &in.pointmarkerlist[idx]);CHKERRQ(ierr);
3774552f7358SJed Brown     }
3775552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &array);CHKERRQ(ierr);
3776552f7358SJed Brown   }
3777552f7358SJed Brown   ierr  = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
37780d644c17SKarl Rupp 
3779552f7358SJed Brown   in.numberofcorners       = 4;
3780552f7358SJed Brown   in.numberoftetrahedra    = cEnd - cStart;
3781552f7358SJed Brown   in.tetrahedronvolumelist = (double*) maxVolumes;
3782552f7358SJed Brown   if (in.numberoftetrahedra > 0) {
3783552f7358SJed Brown     in.tetrahedronlist = new int[in.numberoftetrahedra*in.numberofcorners];
3784552f7358SJed Brown     for (c = cStart; c < cEnd; ++c) {
3785552f7358SJed Brown       const PetscInt idx      = c - cStart;
37860298fd71SBarry Smith       PetscInt      *closure = NULL;
3787552f7358SJed Brown       PetscInt       closureSize;
3788552f7358SJed Brown 
3789552f7358SJed Brown       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3790552f7358SJed Brown       if ((closureSize != 5) && (closureSize != 15)) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Mesh has cell which is not a tetrahedron, %D vertices in closure", closureSize);
3791552f7358SJed Brown       for (v = 0; v < 4; ++v) {
3792552f7358SJed Brown         in.tetrahedronlist[idx*in.numberofcorners + v] = closure[(v+closureSize-4)*2] - vStart;
3793552f7358SJed Brown       }
3794552f7358SJed Brown       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3795552f7358SJed Brown     }
3796552f7358SJed Brown   }
379719436ca2SJed Brown   /* TODO: Put in boundary faces with markers */
3798552f7358SJed Brown   if (!rank) {
3799552f7358SJed Brown     char args[32];
3800552f7358SJed Brown 
3801552f7358SJed Brown     /* Take away 'Q' for verbose output */
380219436ca2SJed Brown     /*ierr = PetscStrcpy(args, "qezQra");CHKERRQ(ierr); */
3803552f7358SJed Brown     ierr = PetscStrcpy(args, "qezraVVVV");CHKERRQ(ierr);
3804552f7358SJed Brown     ::tetrahedralize(args, &in, &out);
3805552f7358SJed Brown   }
3806552f7358SJed Brown   in.tetrahedronvolumelist = NULL;
3807552f7358SJed Brown 
3808552f7358SJed Brown   {
3809552f7358SJed Brown     const PetscInt numCorners  = 4;
3810552f7358SJed Brown     const PetscInt numCells    = out.numberoftetrahedra;
3811552f7358SJed Brown     const PetscInt numVertices = out.numberofpoints;
3812552f7358SJed Brown     const double   *meshCoords = out.pointlist;
38131d4b4d7bSJed Brown     int            *cells      = out.tetrahedronlist;
38141d4b4d7bSJed Brown 
3815552f7358SJed Brown     PetscBool      interpolate = depthGlobal > 1 ? PETSC_TRUE : PETSC_FALSE;
3816552f7358SJed Brown 
38179db3b87dSMatthew G. Knepley     ierr = DMPlexInvertCells_Internal(dim, numCells, numCorners, cells);CHKERRQ(ierr);
38180fde5641SMatthew G Knepley     ierr = DMPlexCreateFromCellList(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dmRefined);CHKERRQ(ierr);
3819552f7358SJed Brown     /* Set labels */
3820552f7358SJed Brown     for (v = 0; v < numVertices; ++v) {
3821552f7358SJed Brown       if (out.pointmarkerlist[v]) {
3822552f7358SJed Brown         ierr = DMPlexSetLabelValue(*dmRefined, "marker", v+numCells, out.pointmarkerlist[v]);CHKERRQ(ierr);
3823552f7358SJed Brown       }
3824552f7358SJed Brown     }
3825552f7358SJed Brown     if (interpolate) {
3826552f7358SJed Brown       PetscInt e, f;
3827552f7358SJed Brown 
3828552f7358SJed Brown       for (e = 0; e < out.numberofedges; e++) {
3829552f7358SJed Brown         if (out.edgemarkerlist[e]) {
3830552f7358SJed Brown           const PetscInt  vertices[2] = {out.edgelist[e*2+0]+numCells, out.edgelist[e*2+1]+numCells};
3831552f7358SJed Brown           const PetscInt *edges;
3832552f7358SJed Brown           PetscInt        numEdges;
3833552f7358SJed Brown 
3834552f7358SJed Brown           ierr = DMPlexGetJoin(*dmRefined, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3835552f7358SJed Brown           if (numEdges != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %D", numEdges);
3836552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dmRefined, "marker", edges[0], out.edgemarkerlist[e]);CHKERRQ(ierr);
3837552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dmRefined, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3838552f7358SJed Brown         }
3839552f7358SJed Brown       }
3840552f7358SJed Brown       for (f = 0; f < out.numberoftrifaces; f++) {
3841552f7358SJed Brown         if (out.trifacemarkerlist[f]) {
3842552f7358SJed Brown           const PetscInt  vertices[3] = {out.trifacelist[f*3+0]+numCells, out.trifacelist[f*3+1]+numCells, out.trifacelist[f*3+2]+numCells};
3843552f7358SJed Brown           const PetscInt *faces;
3844552f7358SJed Brown           PetscInt        numFaces;
3845552f7358SJed Brown 
3846552f7358SJed Brown           ierr = DMPlexGetJoin(*dmRefined, 3, vertices, &numFaces, &faces);CHKERRQ(ierr);
3847552f7358SJed Brown           if (numFaces != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Three vertices must cover only one face, not %D", numFaces);
3848552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dmRefined, "marker", faces[0], out.trifacemarkerlist[f]);CHKERRQ(ierr);
3849552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dmRefined, 3, vertices, &numFaces, &faces);CHKERRQ(ierr);
3850552f7358SJed Brown         }
3851552f7358SJed Brown       }
3852552f7358SJed Brown     }
3853552f7358SJed Brown     ierr = DMPlexSetRefinementUniform(*dmRefined, PETSC_FALSE);CHKERRQ(ierr);
3854552f7358SJed Brown   }
3855552f7358SJed Brown   PetscFunctionReturn(0);
3856552f7358SJed Brown }
3857552f7358SJed Brown #endif
3858552f7358SJed Brown 
3859519f805aSKarl Rupp #if defined(PETSC_HAVE_CTETGEN)
3860552f7358SJed Brown #include "ctetgen.h"
3861552f7358SJed Brown 
3862552f7358SJed Brown #undef __FUNCT__
3863552f7358SJed Brown #define __FUNCT__ "DMPlexGenerate_CTetgen"
3864552f7358SJed Brown PetscErrorCode DMPlexGenerate_CTetgen(DM boundary, PetscBool interpolate, DM *dm)
3865552f7358SJed Brown {
386682f516ccSBarry Smith   MPI_Comm       comm;
3867552f7358SJed Brown   const PetscInt dim  = 3;
3868552f7358SJed Brown   PLC           *in, *out;
3869552f7358SJed Brown   PetscInt       verbose = 0, vStart, vEnd, v, fStart, fEnd, f;
3870552f7358SJed Brown   PetscMPIInt    rank;
3871552f7358SJed Brown   PetscErrorCode ierr;
3872552f7358SJed Brown 
3873552f7358SJed Brown   PetscFunctionBegin;
387482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)boundary,&comm);CHKERRQ(ierr);
38750298fd71SBarry Smith   ierr = PetscOptionsGetInt(((PetscObject) boundary)->prefix, "-ctetgen_verbose", &verbose, NULL);CHKERRQ(ierr);
3876552f7358SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3877552f7358SJed Brown   ierr = DMPlexGetDepthStratum(boundary, 0, &vStart, &vEnd);CHKERRQ(ierr);
3878552f7358SJed Brown   ierr = PLCCreate(&in);CHKERRQ(ierr);
3879552f7358SJed Brown   ierr = PLCCreate(&out);CHKERRQ(ierr);
38800d644c17SKarl Rupp 
3881552f7358SJed Brown   in->numberofpoints = vEnd - vStart;
3882552f7358SJed Brown   if (in->numberofpoints > 0) {
3883552f7358SJed Brown     PetscSection coordSection;
3884552f7358SJed Brown     Vec          coordinates;
3885552f7358SJed Brown     PetscScalar *array;
3886552f7358SJed Brown 
3887785e854fSJed Brown     ierr = PetscMalloc1(in->numberofpoints*dim, &in->pointlist);CHKERRQ(ierr);
3888785e854fSJed Brown     ierr = PetscMalloc1(in->numberofpoints,       &in->pointmarkerlist);CHKERRQ(ierr);
3889552f7358SJed Brown     ierr = DMGetCoordinatesLocal(boundary, &coordinates);CHKERRQ(ierr);
3890552f7358SJed Brown     ierr = DMPlexGetCoordinateSection(boundary, &coordSection);CHKERRQ(ierr);
3891552f7358SJed Brown     ierr = VecGetArray(coordinates, &array);CHKERRQ(ierr);
3892552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
3893552f7358SJed Brown       const PetscInt idx = v - vStart;
3894552f7358SJed Brown       PetscInt       off, d, m;
3895552f7358SJed Brown 
3896552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
3897552f7358SJed Brown       for (d = 0; d < dim; ++d) {
3898552f7358SJed Brown         in->pointlist[idx*dim + d] = PetscRealPart(array[off+d]);
3899552f7358SJed Brown       }
3900552f7358SJed Brown       ierr = DMPlexGetLabelValue(boundary, "marker", v, &m);CHKERRQ(ierr);
39010d644c17SKarl Rupp 
3902552f7358SJed Brown       in->pointmarkerlist[idx] = (int) m;
3903552f7358SJed Brown     }
3904552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &array);CHKERRQ(ierr);
3905552f7358SJed Brown   }
3906552f7358SJed Brown   ierr  = DMPlexGetHeightStratum(boundary, 0, &fStart, &fEnd);CHKERRQ(ierr);
39070d644c17SKarl Rupp 
3908552f7358SJed Brown   in->numberoffacets = fEnd - fStart;
3909552f7358SJed Brown   if (in->numberoffacets > 0) {
3910785e854fSJed Brown     ierr = PetscMalloc1(in->numberoffacets, &in->facetlist);CHKERRQ(ierr);
3911785e854fSJed Brown     ierr = PetscMalloc1(in->numberoffacets,   &in->facetmarkerlist);CHKERRQ(ierr);
3912552f7358SJed Brown     for (f = fStart; f < fEnd; ++f) {
3913552f7358SJed Brown       const PetscInt idx     = f - fStart;
39140298fd71SBarry Smith       PetscInt      *points = NULL, numPoints, p, numVertices = 0, v, m;
3915552f7358SJed Brown       polygon       *poly;
3916552f7358SJed Brown 
3917552f7358SJed Brown       in->facetlist[idx].numberofpolygons = 1;
39180d644c17SKarl Rupp 
3919785e854fSJed Brown       ierr = PetscMalloc1(in->facetlist[idx].numberofpolygons, &in->facetlist[idx].polygonlist);CHKERRQ(ierr);
39200d644c17SKarl Rupp 
3921552f7358SJed Brown       in->facetlist[idx].numberofholes    = 0;
39220298fd71SBarry Smith       in->facetlist[idx].holelist         = NULL;
3923552f7358SJed Brown 
3924552f7358SJed Brown       ierr = DMPlexGetTransitiveClosure(boundary, f, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
3925552f7358SJed Brown       for (p = 0; p < numPoints*2; p += 2) {
3926552f7358SJed Brown         const PetscInt point = points[p];
39270d644c17SKarl Rupp         if ((point >= vStart) && (point < vEnd)) points[numVertices++] = point;
3928552f7358SJed Brown       }
3929552f7358SJed Brown 
3930552f7358SJed Brown       poly                   = in->facetlist[idx].polygonlist;
3931552f7358SJed Brown       poly->numberofvertices = numVertices;
3932785e854fSJed Brown       ierr                   = PetscMalloc1(poly->numberofvertices, &poly->vertexlist);CHKERRQ(ierr);
3933552f7358SJed Brown       for (v = 0; v < numVertices; ++v) {
3934552f7358SJed Brown         const PetscInt vIdx = points[v] - vStart;
3935552f7358SJed Brown         poly->vertexlist[v] = vIdx;
3936552f7358SJed Brown       }
3937552f7358SJed Brown       ierr                     = DMPlexGetLabelValue(boundary, "marker", f, &m);CHKERRQ(ierr);
3938552f7358SJed Brown       in->facetmarkerlist[idx] = (int) m;
3939552f7358SJed Brown       ierr                     = DMPlexRestoreTransitiveClosure(boundary, f, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
3940552f7358SJed Brown     }
3941552f7358SJed Brown   }
3942552f7358SJed Brown   if (!rank) {
3943552f7358SJed Brown     TetGenOpts t;
3944552f7358SJed Brown 
3945552f7358SJed Brown     ierr        = TetGenOptsInitialize(&t);CHKERRQ(ierr);
3946552f7358SJed Brown     t.in        = boundary; /* Should go away */
3947552f7358SJed Brown     t.plc       = 1;
3948552f7358SJed Brown     t.quality   = 1;
3949552f7358SJed Brown     t.edgesout  = 1;
3950552f7358SJed Brown     t.zeroindex = 1;
3951552f7358SJed Brown     t.quiet     = 1;
3952552f7358SJed Brown     t.verbose   = verbose;
3953552f7358SJed Brown     ierr        = TetGenCheckOpts(&t);CHKERRQ(ierr);
3954552f7358SJed Brown     ierr        = TetGenTetrahedralize(&t, in, out);CHKERRQ(ierr);
3955552f7358SJed Brown   }
3956552f7358SJed Brown   {
3957552f7358SJed Brown     const PetscInt numCorners  = 4;
3958552f7358SJed Brown     const PetscInt numCells    = out->numberoftetrahedra;
3959552f7358SJed Brown     const PetscInt numVertices = out->numberofpoints;
3960552f7358SJed Brown     const double   *meshCoords = out->pointlist;
39611d4b4d7bSJed Brown     int            *cells      = out->tetrahedronlist;
3962552f7358SJed Brown 
39639db3b87dSMatthew G. Knepley     ierr = DMPlexInvertCells_Internal(dim, numCells, numCorners, cells);CHKERRQ(ierr);
39640fde5641SMatthew G Knepley     ierr = DMPlexCreateFromCellList(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dm);CHKERRQ(ierr);
3965552f7358SJed Brown     /* Set labels */
3966552f7358SJed Brown     for (v = 0; v < numVertices; ++v) {
3967552f7358SJed Brown       if (out->pointmarkerlist[v]) {
3968552f7358SJed Brown         ierr = DMPlexSetLabelValue(*dm, "marker", v+numCells, out->pointmarkerlist[v]);CHKERRQ(ierr);
3969552f7358SJed Brown       }
3970552f7358SJed Brown     }
3971552f7358SJed Brown     if (interpolate) {
3972552f7358SJed Brown       PetscInt e;
3973552f7358SJed Brown 
3974552f7358SJed Brown       for (e = 0; e < out->numberofedges; e++) {
3975552f7358SJed Brown         if (out->edgemarkerlist[e]) {
3976552f7358SJed Brown           const PetscInt  vertices[2] = {out->edgelist[e*2+0]+numCells, out->edgelist[e*2+1]+numCells};
3977552f7358SJed Brown           const PetscInt *edges;
3978552f7358SJed Brown           PetscInt        numEdges;
3979552f7358SJed Brown 
3980552f7358SJed Brown           ierr = DMPlexGetJoin(*dm, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3981552f7358SJed Brown           if (numEdges != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %D", numEdges);
3982552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dm, "marker", edges[0], out->edgemarkerlist[e]);CHKERRQ(ierr);
3983552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dm, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
3984552f7358SJed Brown         }
3985552f7358SJed Brown       }
3986552f7358SJed Brown       for (f = 0; f < out->numberoftrifaces; f++) {
3987552f7358SJed Brown         if (out->trifacemarkerlist[f]) {
3988552f7358SJed Brown           const PetscInt  vertices[3] = {out->trifacelist[f*3+0]+numCells, out->trifacelist[f*3+1]+numCells, out->trifacelist[f*3+2]+numCells};
3989552f7358SJed Brown           const PetscInt *faces;
3990552f7358SJed Brown           PetscInt        numFaces;
3991552f7358SJed Brown 
3992552f7358SJed Brown           ierr = DMPlexGetFullJoin(*dm, 3, vertices, &numFaces, &faces);CHKERRQ(ierr);
3993552f7358SJed Brown           if (numFaces != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Three vertices must cover only one face, not %D", numFaces);
3994552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dm, "marker", faces[0], out->trifacemarkerlist[f]);CHKERRQ(ierr);
3995552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dm, 3, vertices, &numFaces, &faces);CHKERRQ(ierr);
3996552f7358SJed Brown         }
3997552f7358SJed Brown       }
3998552f7358SJed Brown     }
3999552f7358SJed Brown     ierr = DMPlexSetRefinementUniform(*dm, PETSC_FALSE);CHKERRQ(ierr);
4000552f7358SJed Brown   }
4001552f7358SJed Brown 
4002552f7358SJed Brown   ierr = PLCDestroy(&in);CHKERRQ(ierr);
4003552f7358SJed Brown   ierr = PLCDestroy(&out);CHKERRQ(ierr);
4004552f7358SJed Brown   PetscFunctionReturn(0);
4005552f7358SJed Brown }
4006552f7358SJed Brown 
4007552f7358SJed Brown #undef __FUNCT__
4008552f7358SJed Brown #define __FUNCT__ "DMPlexRefine_CTetgen"
4009552f7358SJed Brown PetscErrorCode DMPlexRefine_CTetgen(DM dm, PetscReal *maxVolumes, DM *dmRefined)
4010552f7358SJed Brown {
401182f516ccSBarry Smith   MPI_Comm       comm;
4012552f7358SJed Brown   const PetscInt dim  = 3;
4013552f7358SJed Brown   PLC           *in, *out;
4014552f7358SJed Brown   PetscInt       verbose = 0, vStart, vEnd, v, cStart, cEnd, c, depth, depthGlobal;
4015552f7358SJed Brown   PetscMPIInt    rank;
4016552f7358SJed Brown   PetscErrorCode ierr;
4017552f7358SJed Brown 
4018552f7358SJed Brown   PetscFunctionBegin;
401982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
40200298fd71SBarry Smith   ierr = PetscOptionsGetInt(((PetscObject) dm)->prefix, "-ctetgen_verbose", &verbose, NULL);CHKERRQ(ierr);
4021552f7358SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4022552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4023552f7358SJed Brown   ierr = MPI_Allreduce(&depth, &depthGlobal, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
4024552f7358SJed Brown   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
4025552f7358SJed Brown   ierr = PLCCreate(&in);CHKERRQ(ierr);
4026552f7358SJed Brown   ierr = PLCCreate(&out);CHKERRQ(ierr);
40270d644c17SKarl Rupp 
4028552f7358SJed Brown   in->numberofpoints = vEnd - vStart;
4029552f7358SJed Brown   if (in->numberofpoints > 0) {
4030552f7358SJed Brown     PetscSection coordSection;
4031552f7358SJed Brown     Vec          coordinates;
4032552f7358SJed Brown     PetscScalar *array;
4033552f7358SJed Brown 
4034785e854fSJed Brown     ierr = PetscMalloc1(in->numberofpoints*dim, &in->pointlist);CHKERRQ(ierr);
4035785e854fSJed Brown     ierr = PetscMalloc1(in->numberofpoints,       &in->pointmarkerlist);CHKERRQ(ierr);
4036552f7358SJed Brown     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
4037552f7358SJed Brown     ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
4038552f7358SJed Brown     ierr = VecGetArray(coordinates, &array);CHKERRQ(ierr);
4039552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
4040552f7358SJed Brown       const PetscInt idx = v - vStart;
4041552f7358SJed Brown       PetscInt       off, d, m;
4042552f7358SJed Brown 
4043552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
4044552f7358SJed Brown       for (d = 0; d < dim; ++d) {
4045552f7358SJed Brown         in->pointlist[idx*dim + d] = PetscRealPart(array[off+d]);
4046552f7358SJed Brown       }
4047552f7358SJed Brown       ierr = DMPlexGetLabelValue(dm, "marker", v, &m);CHKERRQ(ierr);
40480d644c17SKarl Rupp 
4049552f7358SJed Brown       in->pointmarkerlist[idx] = (int) m;
4050552f7358SJed Brown     }
4051552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &array);CHKERRQ(ierr);
4052552f7358SJed Brown   }
4053552f7358SJed Brown   ierr  = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
40540d644c17SKarl Rupp 
4055552f7358SJed Brown   in->numberofcorners       = 4;
4056552f7358SJed Brown   in->numberoftetrahedra    = cEnd - cStart;
4057552f7358SJed Brown   in->tetrahedronvolumelist = maxVolumes;
4058552f7358SJed Brown   if (in->numberoftetrahedra > 0) {
4059785e854fSJed Brown     ierr = PetscMalloc1(in->numberoftetrahedra*in->numberofcorners, &in->tetrahedronlist);CHKERRQ(ierr);
4060552f7358SJed Brown     for (c = cStart; c < cEnd; ++c) {
4061552f7358SJed Brown       const PetscInt idx      = c - cStart;
40620298fd71SBarry Smith       PetscInt      *closure = NULL;
4063552f7358SJed Brown       PetscInt       closureSize;
4064552f7358SJed Brown 
4065552f7358SJed Brown       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
4066552f7358SJed Brown       if ((closureSize != 5) && (closureSize != 15)) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Mesh has cell which is not a tetrahedron, %D vertices in closure", closureSize);
4067552f7358SJed Brown       for (v = 0; v < 4; ++v) {
4068552f7358SJed Brown         in->tetrahedronlist[idx*in->numberofcorners + v] = closure[(v+closureSize-4)*2] - vStart;
4069552f7358SJed Brown       }
4070552f7358SJed Brown       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
4071552f7358SJed Brown     }
4072552f7358SJed Brown   }
4073552f7358SJed Brown   if (!rank) {
4074552f7358SJed Brown     TetGenOpts t;
4075552f7358SJed Brown 
4076552f7358SJed Brown     ierr = TetGenOptsInitialize(&t);CHKERRQ(ierr);
40770d644c17SKarl Rupp 
4078552f7358SJed Brown     t.in        = dm; /* Should go away */
4079552f7358SJed Brown     t.refine    = 1;
4080552f7358SJed Brown     t.varvolume = 1;
4081552f7358SJed Brown     t.quality   = 1;
4082552f7358SJed Brown     t.edgesout  = 1;
4083552f7358SJed Brown     t.zeroindex = 1;
4084552f7358SJed Brown     t.quiet     = 1;
4085552f7358SJed Brown     t.verbose   = verbose; /* Change this */
40860d644c17SKarl Rupp 
4087552f7358SJed Brown     ierr = TetGenCheckOpts(&t);CHKERRQ(ierr);
4088552f7358SJed Brown     ierr = TetGenTetrahedralize(&t, in, out);CHKERRQ(ierr);
4089552f7358SJed Brown   }
4090552f7358SJed Brown   {
4091552f7358SJed Brown     const PetscInt numCorners  = 4;
4092552f7358SJed Brown     const PetscInt numCells    = out->numberoftetrahedra;
4093552f7358SJed Brown     const PetscInt numVertices = out->numberofpoints;
4094552f7358SJed Brown     const double   *meshCoords = out->pointlist;
40951d4b4d7bSJed Brown     int            *cells      = out->tetrahedronlist;
4096552f7358SJed Brown     PetscBool      interpolate = depthGlobal > 1 ? PETSC_TRUE : PETSC_FALSE;
4097552f7358SJed Brown 
40989db3b87dSMatthew G. Knepley     ierr = DMPlexInvertCells_Internal(dim, numCells, numCorners, cells);CHKERRQ(ierr);
40990fde5641SMatthew G Knepley     ierr = DMPlexCreateFromCellList(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dmRefined);CHKERRQ(ierr);
4100552f7358SJed Brown     /* Set labels */
4101552f7358SJed Brown     for (v = 0; v < numVertices; ++v) {
4102552f7358SJed Brown       if (out->pointmarkerlist[v]) {
4103552f7358SJed Brown         ierr = DMPlexSetLabelValue(*dmRefined, "marker", v+numCells, out->pointmarkerlist[v]);CHKERRQ(ierr);
4104552f7358SJed Brown       }
4105552f7358SJed Brown     }
4106552f7358SJed Brown     if (interpolate) {
4107552f7358SJed Brown       PetscInt e, f;
4108552f7358SJed Brown 
4109552f7358SJed Brown       for (e = 0; e < out->numberofedges; e++) {
4110552f7358SJed Brown         if (out->edgemarkerlist[e]) {
4111552f7358SJed Brown           const PetscInt  vertices[2] = {out->edgelist[e*2+0]+numCells, out->edgelist[e*2+1]+numCells};
4112552f7358SJed Brown           const PetscInt *edges;
4113552f7358SJed Brown           PetscInt        numEdges;
4114552f7358SJed Brown 
4115552f7358SJed Brown           ierr = DMPlexGetJoin(*dmRefined, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
4116552f7358SJed Brown           if (numEdges != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %D", numEdges);
4117552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dmRefined, "marker", edges[0], out->edgemarkerlist[e]);CHKERRQ(ierr);
4118552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dmRefined, 2, vertices, &numEdges, &edges);CHKERRQ(ierr);
4119552f7358SJed Brown         }
4120552f7358SJed Brown       }
4121552f7358SJed Brown       for (f = 0; f < out->numberoftrifaces; f++) {
4122552f7358SJed Brown         if (out->trifacemarkerlist[f]) {
4123552f7358SJed Brown           const PetscInt  vertices[3] = {out->trifacelist[f*3+0]+numCells, out->trifacelist[f*3+1]+numCells, out->trifacelist[f*3+2]+numCells};
4124552f7358SJed Brown           const PetscInt *faces;
4125552f7358SJed Brown           PetscInt        numFaces;
4126552f7358SJed Brown 
4127552f7358SJed Brown           ierr = DMPlexGetFullJoin(*dmRefined, 3, vertices, &numFaces, &faces);CHKERRQ(ierr);
4128552f7358SJed Brown           if (numFaces != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Three vertices must cover only one face, not %D", numFaces);
4129552f7358SJed Brown           ierr = DMPlexSetLabelValue(*dmRefined, "marker", faces[0], out->trifacemarkerlist[f]);CHKERRQ(ierr);
4130552f7358SJed Brown           ierr = DMPlexRestoreJoin(*dmRefined, 3, vertices, &numFaces, &faces);CHKERRQ(ierr);
4131552f7358SJed Brown         }
4132552f7358SJed Brown       }
4133552f7358SJed Brown     }
4134552f7358SJed Brown     ierr = DMPlexSetRefinementUniform(*dmRefined, PETSC_FALSE);CHKERRQ(ierr);
4135552f7358SJed Brown   }
4136552f7358SJed Brown   ierr = PLCDestroy(&in);CHKERRQ(ierr);
4137552f7358SJed Brown   ierr = PLCDestroy(&out);CHKERRQ(ierr);
4138552f7358SJed Brown   PetscFunctionReturn(0);
4139552f7358SJed Brown }
4140552f7358SJed Brown #endif
4141552f7358SJed Brown 
4142552f7358SJed Brown #undef __FUNCT__
4143552f7358SJed Brown #define __FUNCT__ "DMPlexGenerate"
4144552f7358SJed Brown /*@C
4145552f7358SJed Brown   DMPlexGenerate - Generates a mesh.
4146552f7358SJed Brown 
4147552f7358SJed Brown   Not Collective
4148552f7358SJed Brown 
4149552f7358SJed Brown   Input Parameters:
4150552f7358SJed Brown + boundary - The DMPlex boundary object
4151552f7358SJed Brown . name - The mesh generation package name
4152552f7358SJed Brown - interpolate - Flag to create intermediate mesh elements
4153552f7358SJed Brown 
4154552f7358SJed Brown   Output Parameter:
4155552f7358SJed Brown . mesh - The DMPlex object
4156552f7358SJed Brown 
4157552f7358SJed Brown   Level: intermediate
4158552f7358SJed Brown 
4159552f7358SJed Brown .keywords: mesh, elements
4160552f7358SJed Brown .seealso: DMPlexCreate(), DMRefine()
4161552f7358SJed Brown @*/
4162552f7358SJed Brown PetscErrorCode DMPlexGenerate(DM boundary, const char name[], PetscBool interpolate, DM *mesh)
4163552f7358SJed Brown {
4164552f7358SJed Brown   PetscInt       dim;
4165552f7358SJed Brown   char           genname[1024];
4166552f7358SJed Brown   PetscBool      isTriangle = PETSC_FALSE, isTetgen = PETSC_FALSE, isCTetgen = PETSC_FALSE, flg;
4167552f7358SJed Brown   PetscErrorCode ierr;
4168552f7358SJed Brown 
4169552f7358SJed Brown   PetscFunctionBegin;
4170552f7358SJed Brown   PetscValidHeaderSpecific(boundary, DM_CLASSID, 1);
4171552f7358SJed Brown   PetscValidLogicalCollectiveBool(boundary, interpolate, 2);
4172552f7358SJed Brown   ierr = DMPlexGetDimension(boundary, &dim);CHKERRQ(ierr);
4173552f7358SJed Brown   ierr = PetscOptionsGetString(((PetscObject) boundary)->prefix, "-dm_plex_generator", genname, 1024, &flg);CHKERRQ(ierr);
41740d644c17SKarl Rupp   if (flg) name = genname;
4175552f7358SJed Brown   if (name) {
4176552f7358SJed Brown     ierr = PetscStrcmp(name, "triangle", &isTriangle);CHKERRQ(ierr);
4177552f7358SJed Brown     ierr = PetscStrcmp(name, "tetgen",   &isTetgen);CHKERRQ(ierr);
4178552f7358SJed Brown     ierr = PetscStrcmp(name, "ctetgen",  &isCTetgen);CHKERRQ(ierr);
4179552f7358SJed Brown   }
4180552f7358SJed Brown   switch (dim) {
4181552f7358SJed Brown   case 1:
4182552f7358SJed Brown     if (!name || isTriangle) {
4183519f805aSKarl Rupp #if defined(PETSC_HAVE_TRIANGLE)
4184552f7358SJed Brown       ierr = DMPlexGenerate_Triangle(boundary, interpolate, mesh);CHKERRQ(ierr);
4185552f7358SJed Brown #else
418682f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)boundary), PETSC_ERR_SUP, "Mesh generation needs external package support.\nPlease reconfigure with --download-triangle.");
4187552f7358SJed Brown #endif
418882f516ccSBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)boundary), PETSC_ERR_SUP, "Unknown 2D mesh generation package %s", name);
4189552f7358SJed Brown     break;
4190552f7358SJed Brown   case 2:
4191552f7358SJed Brown     if (!name || isCTetgen) {
4192519f805aSKarl Rupp #if defined(PETSC_HAVE_CTETGEN)
4193552f7358SJed Brown       ierr = DMPlexGenerate_CTetgen(boundary, interpolate, mesh);CHKERRQ(ierr);
4194552f7358SJed Brown #else
419582f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)boundary), PETSC_ERR_SUP, "CTetgen needs external package support.\nPlease reconfigure with --download-ctetgen.");
4196552f7358SJed Brown #endif
4197552f7358SJed Brown     } else if (isTetgen) {
4198519f805aSKarl Rupp #if defined(PETSC_HAVE_TETGEN)
4199552f7358SJed Brown       ierr = DMPlexGenerate_Tetgen(boundary, interpolate, mesh);CHKERRQ(ierr);
4200552f7358SJed Brown #else
420182f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)boundary), PETSC_ERR_SUP, "Tetgen needs external package support.\nPlease reconfigure with --with-c-language=cxx --download-tetgen.");
4202552f7358SJed Brown #endif
420382f516ccSBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)boundary), PETSC_ERR_SUP, "Unknown 3D mesh generation package %s", name);
4204552f7358SJed Brown     break;
4205552f7358SJed Brown   default:
420682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject)boundary), PETSC_ERR_SUP, "Mesh generation for a dimension %d boundary is not supported.", dim);
4207552f7358SJed Brown   }
4208552f7358SJed Brown   PetscFunctionReturn(0);
4209552f7358SJed Brown }
4210552f7358SJed Brown 
4211552f7358SJed Brown #undef __FUNCT__
4212552f7358SJed Brown #define __FUNCT__ "DMRefine_Plex"
4213552f7358SJed Brown PetscErrorCode DMRefine_Plex(DM dm, MPI_Comm comm, DM *dmRefined)
4214552f7358SJed Brown {
4215552f7358SJed Brown   PetscReal      refinementLimit;
4216552f7358SJed Brown   PetscInt       dim, cStart, cEnd;
42170298fd71SBarry Smith   char           genname[1024], *name = NULL;
4218552f7358SJed Brown   PetscBool      isUniform, isTriangle = PETSC_FALSE, isTetgen = PETSC_FALSE, isCTetgen = PETSC_FALSE, flg;
4219552f7358SJed Brown   PetscErrorCode ierr;
4220552f7358SJed Brown 
4221552f7358SJed Brown   PetscFunctionBegin;
4222552f7358SJed Brown   ierr = DMPlexGetRefinementUniform(dm, &isUniform);CHKERRQ(ierr);
4223552f7358SJed Brown   if (isUniform) {
4224552f7358SJed Brown     CellRefiner cellRefiner;
4225552f7358SJed Brown 
4226509c9b89SMatthew G. Knepley     ierr = DMPlexGetCellRefiner_Internal(dm, &cellRefiner);CHKERRQ(ierr);
4227509c9b89SMatthew G. Knepley     ierr = DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);CHKERRQ(ierr);
4228552f7358SJed Brown     PetscFunctionReturn(0);
4229552f7358SJed Brown   }
4230552f7358SJed Brown   ierr = DMPlexGetRefinementLimit(dm, &refinementLimit);CHKERRQ(ierr);
4231552f7358SJed Brown   if (refinementLimit == 0.0) PetscFunctionReturn(0);
4232552f7358SJed Brown   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
4233552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4234552f7358SJed Brown   ierr = PetscOptionsGetString(((PetscObject) dm)->prefix, "-dm_plex_generator", genname, 1024, &flg);CHKERRQ(ierr);
42350d644c17SKarl Rupp   if (flg) name = genname;
4236552f7358SJed Brown   if (name) {
4237552f7358SJed Brown     ierr = PetscStrcmp(name, "triangle", &isTriangle);CHKERRQ(ierr);
4238552f7358SJed Brown     ierr = PetscStrcmp(name, "tetgen",   &isTetgen);CHKERRQ(ierr);
4239552f7358SJed Brown     ierr = PetscStrcmp(name, "ctetgen",  &isCTetgen);CHKERRQ(ierr);
4240552f7358SJed Brown   }
4241552f7358SJed Brown   switch (dim) {
4242552f7358SJed Brown   case 2:
4243552f7358SJed Brown     if (!name || isTriangle) {
4244519f805aSKarl Rupp #if defined(PETSC_HAVE_TRIANGLE)
4245552f7358SJed Brown       double  *maxVolumes;
4246552f7358SJed Brown       PetscInt c;
4247552f7358SJed Brown 
4248785e854fSJed Brown       ierr = PetscMalloc1((cEnd - cStart), &maxVolumes);CHKERRQ(ierr);
42490d644c17SKarl Rupp       for (c = 0; c < cEnd-cStart; ++c) maxVolumes[c] = refinementLimit;
4250552f7358SJed Brown       ierr = DMPlexRefine_Triangle(dm, maxVolumes, dmRefined);CHKERRQ(ierr);
42516b0a3334SMatthew G. Knepley       ierr = PetscFree(maxVolumes);CHKERRQ(ierr);
4252552f7358SJed Brown #else
425382f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Mesh refinement needs external package support.\nPlease reconfigure with --download-triangle.");
4254552f7358SJed Brown #endif
425582f516ccSBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unknown 2D mesh generation package %s", name);
4256552f7358SJed Brown     break;
4257552f7358SJed Brown   case 3:
4258552f7358SJed Brown     if (!name || isCTetgen) {
4259519f805aSKarl Rupp #if defined(PETSC_HAVE_CTETGEN)
4260552f7358SJed Brown       PetscReal *maxVolumes;
4261552f7358SJed Brown       PetscInt   c;
4262552f7358SJed Brown 
4263785e854fSJed Brown       ierr = PetscMalloc1((cEnd - cStart), &maxVolumes);CHKERRQ(ierr);
42640d644c17SKarl Rupp       for (c = 0; c < cEnd-cStart; ++c) maxVolumes[c] = refinementLimit;
4265552f7358SJed Brown       ierr = DMPlexRefine_CTetgen(dm, maxVolumes, dmRefined);CHKERRQ(ierr);
4266552f7358SJed Brown #else
426782f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "CTetgen needs external package support.\nPlease reconfigure with --download-ctetgen.");
4268552f7358SJed Brown #endif
4269552f7358SJed Brown     } else if (isTetgen) {
4270519f805aSKarl Rupp #if defined(PETSC_HAVE_TETGEN)
4271552f7358SJed Brown       double  *maxVolumes;
4272552f7358SJed Brown       PetscInt c;
4273552f7358SJed Brown 
4274785e854fSJed Brown       ierr = PetscMalloc1((cEnd - cStart), &maxVolumes);CHKERRQ(ierr);
42750d644c17SKarl Rupp       for (c = 0; c < cEnd-cStart; ++c) maxVolumes[c] = refinementLimit;
4276552f7358SJed Brown       ierr = DMPlexRefine_Tetgen(dm, maxVolumes, dmRefined);CHKERRQ(ierr);
4277552f7358SJed Brown #else
427882f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Tetgen needs external package support.\nPlease reconfigure with --with-c-language=cxx --download-tetgen.");
4279552f7358SJed Brown #endif
428082f516ccSBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unknown 3D mesh generation package %s", name);
4281552f7358SJed Brown     break;
4282552f7358SJed Brown   default:
428382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Mesh refinement in dimension %d is not supported.", dim);
4284552f7358SJed Brown   }
4285552f7358SJed Brown   PetscFunctionReturn(0);
4286552f7358SJed Brown }
4287552f7358SJed Brown 
4288552f7358SJed Brown #undef __FUNCT__
4289aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepthLabel"
4290552f7358SJed Brown /*@
4291aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
4292552f7358SJed Brown 
4293552f7358SJed Brown   Not Collective
4294552f7358SJed Brown 
4295aa50250dSMatthew G. Knepley   Input Parameter:
4296552f7358SJed Brown . dm    - The DMPlex object
4297552f7358SJed Brown 
4298aa50250dSMatthew G. Knepley   Output Parameter:
4299aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
4300552f7358SJed Brown 
4301552f7358SJed Brown   Level: developer
4302552f7358SJed Brown 
4303aa50250dSMatthew G. Knepley .keywords: mesh, points
4304aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
4305aa50250dSMatthew G. Knepley @*/
4306aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
4307aa50250dSMatthew G. Knepley {
4308aa50250dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
4309aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
4310aa50250dSMatthew G. Knepley 
4311aa50250dSMatthew G. Knepley   PetscFunctionBegin;
4312aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4313aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
431463d1a920SMatthew G. Knepley   if (!mesh->depthLabel) {ierr = DMPlexGetLabel(dm, "depth", &mesh->depthLabel);CHKERRQ(ierr);}
4315aa50250dSMatthew G. Knepley   *depthLabel = mesh->depthLabel;
4316aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
4317aa50250dSMatthew G. Knepley }
4318aa50250dSMatthew G. Knepley 
4319aa50250dSMatthew G. Knepley #undef __FUNCT__
4320aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepth"
4321aa50250dSMatthew G. Knepley /*@
4322aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
4323aa50250dSMatthew G. Knepley 
4324aa50250dSMatthew G. Knepley   Not Collective
4325aa50250dSMatthew G. Knepley 
4326aa50250dSMatthew G. Knepley   Input Parameter:
4327aa50250dSMatthew G. Knepley . dm    - The DMPlex object
4328aa50250dSMatthew G. Knepley 
4329aa50250dSMatthew G. Knepley   Output Parameter:
4330aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
4331aa50250dSMatthew G. Knepley 
4332aa50250dSMatthew G. Knepley   Level: developer
4333552f7358SJed Brown 
4334552f7358SJed Brown .keywords: mesh, points
4335aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
4336552f7358SJed Brown @*/
4337552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
4338552f7358SJed Brown {
4339aa50250dSMatthew G. Knepley   DMLabel        label;
4340aa50250dSMatthew G. Knepley   PetscInt       d = 0;
4341552f7358SJed Brown   PetscErrorCode ierr;
4342552f7358SJed Brown 
4343552f7358SJed Brown   PetscFunctionBegin;
4344552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4345552f7358SJed Brown   PetscValidPointer(depth, 2);
4346aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4347aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
4348552f7358SJed Brown   *depth = d-1;
4349552f7358SJed Brown   PetscFunctionReturn(0);
4350552f7358SJed Brown }
4351552f7358SJed Brown 
4352552f7358SJed Brown #undef __FUNCT__
4353552f7358SJed Brown #define __FUNCT__ "DMPlexGetDepthStratum"
4354552f7358SJed Brown /*@
4355552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
4356552f7358SJed Brown 
4357552f7358SJed Brown   Not Collective
4358552f7358SJed Brown 
4359552f7358SJed Brown   Input Parameters:
4360552f7358SJed Brown + dm           - The DMPlex object
4361552f7358SJed Brown - stratumValue - The requested depth
4362552f7358SJed Brown 
4363552f7358SJed Brown   Output Parameters:
4364552f7358SJed Brown + start - The first point at this depth
4365552f7358SJed Brown - end   - One beyond the last point at this depth
4366552f7358SJed Brown 
4367552f7358SJed Brown   Level: developer
4368552f7358SJed Brown 
4369552f7358SJed Brown .keywords: mesh, points
4370552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
4371552f7358SJed Brown @*/
43720adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
43730adebc6cSBarry Smith {
4374aa50250dSMatthew G. Knepley   DMLabel        label;
437563d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
4376552f7358SJed Brown   PetscErrorCode ierr;
4377552f7358SJed Brown 
4378552f7358SJed Brown   PetscFunctionBegin;
4379552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
438063d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
438163d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4382552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
43830d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
438463d1a920SMatthew G. Knepley   if (stratumValue < 0) {
438563d1a920SMatthew G. Knepley     if (start) *start = pStart;
438663d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
438763d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4388552f7358SJed Brown   }
4389aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
439063d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
439163d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
4392552f7358SJed Brown   PetscFunctionReturn(0);
4393552f7358SJed Brown }
4394552f7358SJed Brown 
4395552f7358SJed Brown #undef __FUNCT__
4396552f7358SJed Brown #define __FUNCT__ "DMPlexGetHeightStratum"
4397552f7358SJed Brown /*@
4398552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
4399552f7358SJed Brown 
4400552f7358SJed Brown   Not Collective
4401552f7358SJed Brown 
4402552f7358SJed Brown   Input Parameters:
4403552f7358SJed Brown + dm           - The DMPlex object
4404552f7358SJed Brown - stratumValue - The requested height
4405552f7358SJed Brown 
4406552f7358SJed Brown   Output Parameters:
4407552f7358SJed Brown + start - The first point at this height
4408552f7358SJed Brown - end   - One beyond the last point at this height
4409552f7358SJed Brown 
4410552f7358SJed Brown   Level: developer
4411552f7358SJed Brown 
4412552f7358SJed Brown .keywords: mesh, points
4413552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
4414552f7358SJed Brown @*/
44150adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
44160adebc6cSBarry Smith {
4417aa50250dSMatthew G. Knepley   DMLabel        label;
441863d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
4419552f7358SJed Brown   PetscErrorCode ierr;
4420552f7358SJed Brown 
4421552f7358SJed Brown   PetscFunctionBegin;
4422552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
442363d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
442463d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
4425552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
44260d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
442763d1a920SMatthew G. Knepley   if (stratumValue < 0) {
442863d1a920SMatthew G. Knepley     if (start) *start = pStart;
442963d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
443063d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
4431552f7358SJed Brown   }
4432aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
4433aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
443463d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
443563d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
4436552f7358SJed Brown   PetscFunctionReturn(0);
4437552f7358SJed Brown }
4438552f7358SJed Brown 
4439552f7358SJed Brown #undef __FUNCT__
4440552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionInitial"
4441552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
4442a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
4443a6dfd86eSKarl Rupp {
4444552f7358SJed Brown   PetscInt      *numDofTot;
4445552f7358SJed Brown   PetscInt       pStart = 0, pEnd = 0;
4446552f7358SJed Brown   PetscInt       p, d, f;
4447552f7358SJed Brown   PetscErrorCode ierr;
4448552f7358SJed Brown 
4449552f7358SJed Brown   PetscFunctionBegin;
4450785e854fSJed Brown   ierr = PetscMalloc1((dim+1), &numDofTot);CHKERRQ(ierr);
4451552f7358SJed Brown   for (d = 0; d <= dim; ++d) {
4452552f7358SJed Brown     numDofTot[d] = 0;
44530d644c17SKarl Rupp     for (f = 0; f < numFields; ++f) numDofTot[d] += numDof[f*(dim+1)+d];
4454552f7358SJed Brown   }
445582f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
4456552f7358SJed Brown   if (numFields > 0) {
4457552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
4458552f7358SJed Brown     if (numComp) {
4459552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
4460552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
4461552f7358SJed Brown       }
4462552f7358SJed Brown     }
4463552f7358SJed Brown   }
4464552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
4465552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
4466552f7358SJed Brown   for (d = 0; d <= dim; ++d) {
4467552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
4468552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
4469552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
4470552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
4471552f7358SJed Brown       }
4472552f7358SJed Brown       ierr = PetscSectionSetDof(*section, p, numDofTot[d]);CHKERRQ(ierr);
4473552f7358SJed Brown     }
4474552f7358SJed Brown   }
4475552f7358SJed Brown   ierr = PetscFree(numDofTot);CHKERRQ(ierr);
4476552f7358SJed Brown   PetscFunctionReturn(0);
4477552f7358SJed Brown }
4478552f7358SJed Brown 
4479552f7358SJed Brown #undef __FUNCT__
4480552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCDof"
4481552f7358SJed Brown /* Set the number of dof on each point and separate by fields
4482552f7358SJed Brown    If constDof is PETSC_DETERMINE, constrain every dof on the point
4483552f7358SJed Brown */
4484a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC,const PetscInt bcField[],const IS bcPoints[], PetscInt constDof, PetscSection section)
4485a6dfd86eSKarl Rupp {
4486552f7358SJed Brown   PetscInt       numFields;
4487552f7358SJed Brown   PetscInt       bc;
4488552f7358SJed Brown   PetscErrorCode ierr;
4489552f7358SJed Brown 
4490552f7358SJed Brown   PetscFunctionBegin;
4491552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4492552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
4493552f7358SJed Brown     PetscInt        field = 0;
4494552f7358SJed Brown     const PetscInt *idx;
4495552f7358SJed Brown     PetscInt        n, i;
4496552f7358SJed Brown 
44970d644c17SKarl Rupp     if (numFields) field = bcField[bc];
4498552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
4499552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
4500552f7358SJed Brown     for (i = 0; i < n; ++i) {
4501552f7358SJed Brown       const PetscInt p        = idx[i];
4502552f7358SJed Brown       PetscInt       numConst = constDof;
4503552f7358SJed Brown 
4504552f7358SJed Brown       /* Constrain every dof on the point */
4505552f7358SJed Brown       if (numConst < 0) {
4506552f7358SJed Brown         if (numFields) {
4507552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
4508552f7358SJed Brown         } else {
4509552f7358SJed Brown           ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
4510552f7358SJed Brown         }
4511552f7358SJed Brown       }
4512552f7358SJed Brown       if (numFields) {
4513552f7358SJed Brown         ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);
4514552f7358SJed Brown       }
4515552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
4516552f7358SJed Brown     }
4517552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
4518552f7358SJed Brown   }
4519552f7358SJed Brown   PetscFunctionReturn(0);
4520552f7358SJed Brown }
4521552f7358SJed Brown 
4522552f7358SJed Brown #undef __FUNCT__
4523552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCIndicesAll"
4524552f7358SJed Brown /* Set the constrained indices on each point and separate by fields */
45250adebc6cSBarry Smith PetscErrorCode DMPlexCreateSectionBCIndicesAll(DM dm, PetscSection section)
45260adebc6cSBarry Smith {
4527552f7358SJed Brown   PetscInt      *maxConstraints;
4528552f7358SJed Brown   PetscInt       numFields, f, pStart = 0, pEnd = 0, p;
4529552f7358SJed Brown   PetscErrorCode ierr;
4530552f7358SJed Brown 
4531552f7358SJed Brown   PetscFunctionBegin;
4532552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4533552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4534785e854fSJed Brown   ierr = PetscMalloc1((numFields+1), &maxConstraints);CHKERRQ(ierr);
45350d644c17SKarl Rupp   for (f = 0; f <= numFields; ++f) maxConstraints[f] = 0;
4536552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
4537552f7358SJed Brown     PetscInt cdof;
4538552f7358SJed Brown 
4539552f7358SJed Brown     if (numFields) {
4540552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
4541552f7358SJed Brown         ierr              = PetscSectionGetFieldConstraintDof(section, p, f, &cdof);CHKERRQ(ierr);
4542552f7358SJed Brown         maxConstraints[f] = PetscMax(maxConstraints[f], cdof);
4543552f7358SJed Brown       }
4544552f7358SJed Brown     } else {
4545552f7358SJed Brown       ierr              = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
4546552f7358SJed Brown       maxConstraints[0] = PetscMax(maxConstraints[0], cdof);
4547552f7358SJed Brown     }
4548552f7358SJed Brown   }
4549552f7358SJed Brown   for (f = 0; f < numFields; ++f) {
4550552f7358SJed Brown     maxConstraints[numFields] += maxConstraints[f];
4551552f7358SJed Brown   }
4552552f7358SJed Brown   if (maxConstraints[numFields]) {
4553552f7358SJed Brown     PetscInt *indices;
4554552f7358SJed Brown 
4555785e854fSJed Brown     ierr = PetscMalloc1(maxConstraints[numFields], &indices);CHKERRQ(ierr);
4556552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
4557552f7358SJed Brown       PetscInt cdof, d;
4558552f7358SJed Brown 
4559552f7358SJed Brown       ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
4560552f7358SJed Brown       if (cdof) {
4561552f7358SJed 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]);
4562552f7358SJed Brown         if (numFields) {
4563552f7358SJed Brown           PetscInt numConst = 0, foff = 0;
4564552f7358SJed Brown 
4565552f7358SJed Brown           for (f = 0; f < numFields; ++f) {
4566552f7358SJed Brown             PetscInt cfdof, fdof;
4567552f7358SJed Brown 
4568552f7358SJed Brown             ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
4569552f7358SJed Brown             ierr = PetscSectionGetFieldConstraintDof(section, p, f, &cfdof);CHKERRQ(ierr);
4570552f7358SJed Brown             /* Change constraint numbering from absolute local dof number to field relative local dof number */
45710d644c17SKarl Rupp             for (d = 0; d < cfdof; ++d) indices[numConst+d] = d;
4572552f7358SJed Brown             ierr = PetscSectionSetFieldConstraintIndices(section, p, f, &indices[numConst]);CHKERRQ(ierr);
45730d644c17SKarl Rupp             for (d = 0; d < cfdof; ++d) indices[numConst+d] += foff;
4574552f7358SJed Brown             numConst += cfdof;
4575552f7358SJed Brown             foff     += fdof;
4576552f7358SJed Brown           }
4577552f7358SJed Brown           if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
4578552f7358SJed Brown         } else {
45790d644c17SKarl Rupp           for (d = 0; d < cdof; ++d) indices[d] = d;
4580552f7358SJed Brown         }
4581552f7358SJed Brown         ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
4582552f7358SJed Brown       }
4583552f7358SJed Brown     }
4584552f7358SJed Brown     ierr = PetscFree(indices);CHKERRQ(ierr);
4585552f7358SJed Brown   }
4586552f7358SJed Brown   ierr = PetscFree(maxConstraints);CHKERRQ(ierr);
4587552f7358SJed Brown   PetscFunctionReturn(0);
4588552f7358SJed Brown }
4589552f7358SJed Brown 
4590552f7358SJed Brown #undef __FUNCT__
4591552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCIndicesField"
4592552f7358SJed Brown /* Set the constrained field indices on each point */
45930adebc6cSBarry Smith PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt field, IS bcPoints, IS constraintIndices, PetscSection section)
45940adebc6cSBarry Smith {
4595552f7358SJed Brown   const PetscInt *points, *indices;
4596552f7358SJed Brown   PetscInt        numFields, maxDof, numPoints, p, numConstraints;
4597552f7358SJed Brown   PetscErrorCode  ierr;
4598552f7358SJed Brown 
4599552f7358SJed Brown   PetscFunctionBegin;
4600552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4601552f7358SJed 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);
4602552f7358SJed Brown 
4603552f7358SJed Brown   ierr = ISGetLocalSize(bcPoints, &numPoints);CHKERRQ(ierr);
4604552f7358SJed Brown   ierr = ISGetIndices(bcPoints, &points);CHKERRQ(ierr);
4605552f7358SJed Brown   if (!constraintIndices) {
4606552f7358SJed Brown     PetscInt *idx, i;
4607552f7358SJed Brown 
4608552f7358SJed Brown     ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4609785e854fSJed Brown     ierr = PetscMalloc1(maxDof, &idx);CHKERRQ(ierr);
46100d644c17SKarl Rupp     for (i = 0; i < maxDof; ++i) idx[i] = i;
4611552f7358SJed Brown     for (p = 0; p < numPoints; ++p) {
4612552f7358SJed Brown       ierr = PetscSectionSetFieldConstraintIndices(section, points[p], field, idx);CHKERRQ(ierr);
4613552f7358SJed Brown     }
4614552f7358SJed Brown     ierr = PetscFree(idx);CHKERRQ(ierr);
4615552f7358SJed Brown   } else {
4616552f7358SJed Brown     ierr = ISGetLocalSize(constraintIndices, &numConstraints);CHKERRQ(ierr);
4617552f7358SJed Brown     ierr = ISGetIndices(constraintIndices, &indices);CHKERRQ(ierr);
4618552f7358SJed Brown     for (p = 0; p < numPoints; ++p) {
4619552f7358SJed Brown       PetscInt fcdof;
4620552f7358SJed Brown 
4621552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintDof(section, points[p], field, &fcdof);CHKERRQ(ierr);
462282f516ccSBarry 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);
4623552f7358SJed Brown       ierr = PetscSectionSetFieldConstraintIndices(section, points[p], field, indices);CHKERRQ(ierr);
4624552f7358SJed Brown     }
4625552f7358SJed Brown     ierr = ISRestoreIndices(constraintIndices, &indices);CHKERRQ(ierr);
4626552f7358SJed Brown   }
4627552f7358SJed Brown   ierr = ISRestoreIndices(bcPoints, &points);CHKERRQ(ierr);
4628552f7358SJed Brown   PetscFunctionReturn(0);
4629552f7358SJed Brown }
4630552f7358SJed Brown 
4631552f7358SJed Brown #undef __FUNCT__
4632552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCIndices"
4633552f7358SJed Brown /* Set the constrained indices on each point and separate by fields */
46340adebc6cSBarry Smith PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
46350adebc6cSBarry Smith {
4636552f7358SJed Brown   PetscInt      *indices;
4637552f7358SJed Brown   PetscInt       numFields, maxDof, f, pStart = 0, pEnd = 0, p;
4638552f7358SJed Brown   PetscErrorCode ierr;
4639552f7358SJed Brown 
4640552f7358SJed Brown   PetscFunctionBegin;
4641552f7358SJed Brown   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4642785e854fSJed Brown   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
4643552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
464482f516ccSBarry Smith   if (!numFields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "This function only works after users have set field constraint indices.");
4645552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4646552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
4647552f7358SJed Brown     PetscInt cdof, d;
4648552f7358SJed Brown 
4649552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
4650552f7358SJed Brown     if (cdof) {
4651552f7358SJed Brown       PetscInt numConst = 0, foff = 0;
4652552f7358SJed Brown 
4653552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
4654552f7358SJed Brown         const PetscInt *fcind;
4655552f7358SJed Brown         PetscInt        fdof, fcdof;
4656552f7358SJed Brown 
4657552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
4658552f7358SJed Brown         ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
4659552f7358SJed Brown         if (fcdof) {ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &fcind);CHKERRQ(ierr);}
4660552f7358SJed Brown         /* Change constraint numbering from field relative local dof number to absolute local dof number */
46610d644c17SKarl Rupp         for (d = 0; d < fcdof; ++d) indices[numConst+d] = fcind[d]+foff;
4662552f7358SJed Brown         foff     += fdof;
4663552f7358SJed Brown         numConst += fcdof;
4664552f7358SJed Brown       }
466582f516ccSBarry Smith       if (cdof != numConst) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
4666552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
4667552f7358SJed Brown     }
4668552f7358SJed Brown   }
4669552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
4670552f7358SJed Brown   PetscFunctionReturn(0);
4671552f7358SJed Brown }
4672552f7358SJed Brown 
4673552f7358SJed Brown #undef __FUNCT__
4674552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSection"
4675552f7358SJed Brown /*@C
4676552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
4677552f7358SJed Brown 
4678552f7358SJed Brown   Not Collective
4679552f7358SJed Brown 
4680552f7358SJed Brown   Input Parameters:
4681552f7358SJed Brown + dm        - The DMPlex object
4682552f7358SJed Brown . dim       - The spatial dimension of the problem
4683552f7358SJed Brown . numFields - The number of fields in the problem
4684552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
4685552f7358SJed 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
4686552f7358SJed Brown . numBC     - The number of boundary conditions
4687552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
4688552f7358SJed Brown - bcPoints  - An array of size numBC giving an IS holding the sieve points to which each boundary condition applies
4689552f7358SJed Brown 
4690552f7358SJed Brown   Output Parameter:
4691552f7358SJed Brown . section - The PetscSection object
4692552f7358SJed Brown 
4693552f7358SJed 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
4694552f7358SJed Brown   nubmer of dof for field 0 on each edge.
4695552f7358SJed Brown 
4696552f7358SJed Brown   Level: developer
4697552f7358SJed Brown 
46983813dfbdSMatthew G Knepley   Fortran Notes:
46993813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
47003813dfbdSMatthew G Knepley 
4701552f7358SJed Brown .keywords: mesh, elements
4702552f7358SJed Brown .seealso: DMPlexCreate(), PetscSectionCreate()
4703552f7358SJed Brown @*/
4704a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSection(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscInt numBC,const PetscInt bcField[],const IS bcPoints[], PetscSection *section)
4705a6dfd86eSKarl Rupp {
4706552f7358SJed Brown   PetscErrorCode ierr;
4707552f7358SJed Brown 
4708552f7358SJed Brown   PetscFunctionBegin;
4709552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
4710552f7358SJed Brown   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcPoints, PETSC_DETERMINE, *section);CHKERRQ(ierr);
4711552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
4712552f7358SJed Brown   if (numBC) {ierr = DMPlexCreateSectionBCIndicesAll(dm, *section);CHKERRQ(ierr);}
4713552f7358SJed Brown   {
4714552f7358SJed Brown     PetscBool view = PETSC_FALSE;
4715552f7358SJed Brown 
4716552f7358SJed Brown     ierr = PetscOptionsHasName(((PetscObject) dm)->prefix, "-section_view", &view);CHKERRQ(ierr);
4717552f7358SJed Brown     if (view) {ierr = PetscSectionView(*section, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
4718552f7358SJed Brown   }
4719552f7358SJed Brown   PetscFunctionReturn(0);
4720552f7358SJed Brown }
4721552f7358SJed Brown 
4722552f7358SJed Brown #undef __FUNCT__
4723552f7358SJed Brown #define __FUNCT__ "DMCreateCoordinateDM_Plex"
47240adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
47250adebc6cSBarry Smith {
4726552f7358SJed Brown   PetscSection   section;
4727552f7358SJed Brown   PetscErrorCode ierr;
4728552f7358SJed Brown 
4729552f7358SJed Brown   PetscFunctionBegin;
473038221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
473182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
4732552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
47331d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
4734552f7358SJed Brown   PetscFunctionReturn(0);
4735552f7358SJed Brown }
4736552f7358SJed Brown 
4737552f7358SJed Brown #undef __FUNCT__
4738552f7358SJed Brown #define __FUNCT__ "DMPlexGetCoordinateSection"
4739552f7358SJed Brown /*@
4740552f7358SJed Brown   DMPlexGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4741552f7358SJed Brown 
4742552f7358SJed Brown   Not Collective
4743552f7358SJed Brown 
4744552f7358SJed Brown   Input Parameter:
4745552f7358SJed Brown . dm - The DMPlex object
4746552f7358SJed Brown 
4747552f7358SJed Brown   Output Parameter:
4748552f7358SJed Brown . section - The PetscSection object
4749552f7358SJed Brown 
4750552f7358SJed Brown   Level: intermediate
4751552f7358SJed Brown 
4752552f7358SJed Brown .keywords: mesh, coordinates
4753552f7358SJed Brown .seealso: DMGetCoordinateDM(), DMPlexGetDefaultSection(), DMPlexSetDefaultSection()
4754552f7358SJed Brown @*/
47550adebc6cSBarry Smith PetscErrorCode DMPlexGetCoordinateSection(DM dm, PetscSection *section)
47560adebc6cSBarry Smith {
4757552f7358SJed Brown   DM             cdm;
4758552f7358SJed Brown   PetscErrorCode ierr;
4759552f7358SJed Brown 
4760552f7358SJed Brown   PetscFunctionBegin;
4761552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4762552f7358SJed Brown   PetscValidPointer(section, 2);
4763552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4764552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4765552f7358SJed Brown   PetscFunctionReturn(0);
4766552f7358SJed Brown }
4767552f7358SJed Brown 
4768552f7358SJed Brown #undef __FUNCT__
4769552f7358SJed Brown #define __FUNCT__ "DMPlexSetCoordinateSection"
4770552f7358SJed Brown /*@
4771552f7358SJed Brown   DMPlexSetCoordinateSection - Set the layout of coordinate values over the mesh.
4772552f7358SJed Brown 
4773552f7358SJed Brown   Not Collective
4774552f7358SJed Brown 
4775552f7358SJed Brown   Input Parameters:
4776552f7358SJed Brown + dm      - The DMPlex object
4777552f7358SJed Brown - section - The PetscSection object
4778552f7358SJed Brown 
4779552f7358SJed Brown   Level: intermediate
4780552f7358SJed Brown 
4781552f7358SJed Brown .keywords: mesh, coordinates
4782552f7358SJed Brown .seealso: DMPlexGetCoordinateSection(), DMPlexGetDefaultSection(), DMPlexSetDefaultSection()
4783552f7358SJed Brown @*/
47840adebc6cSBarry Smith PetscErrorCode DMPlexSetCoordinateSection(DM dm, PetscSection section)
47850adebc6cSBarry Smith {
4786552f7358SJed Brown   DM             cdm;
4787552f7358SJed Brown   PetscErrorCode ierr;
4788552f7358SJed Brown 
4789552f7358SJed Brown   PetscFunctionBegin;
4790552f7358SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47911d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4792552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4793552f7358SJed Brown   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
4794552f7358SJed Brown   PetscFunctionReturn(0);
4795552f7358SJed Brown }
4796552f7358SJed Brown 
4797552f7358SJed Brown #undef __FUNCT__
4798552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSection"
47990adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
48000adebc6cSBarry Smith {
4801552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4802552f7358SJed Brown 
4803552f7358SJed Brown   PetscFunctionBegin;
4804552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4805552f7358SJed Brown   if (section) *section = mesh->coneSection;
4806552f7358SJed Brown   PetscFunctionReturn(0);
4807552f7358SJed Brown }
4808552f7358SJed Brown 
4809552f7358SJed Brown #undef __FUNCT__
48108cb4d582SMatthew G. Knepley #define __FUNCT__ "DMPlexGetSupportSection"
48118cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
48128cb4d582SMatthew G. Knepley {
48138cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
48148cb4d582SMatthew G. Knepley 
48158cb4d582SMatthew G. Knepley   PetscFunctionBegin;
48168cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48178cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
48188cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
48198cb4d582SMatthew G. Knepley }
48208cb4d582SMatthew G. Knepley 
48218cb4d582SMatthew G. Knepley #undef __FUNCT__
4822552f7358SJed Brown #define __FUNCT__ "DMPlexGetCones"
4823a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
4824a6dfd86eSKarl Rupp {
4825552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4826552f7358SJed Brown 
4827552f7358SJed Brown   PetscFunctionBegin;
4828552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4829552f7358SJed Brown   if (cones) *cones = mesh->cones;
4830552f7358SJed Brown   PetscFunctionReturn(0);
4831552f7358SJed Brown }
4832552f7358SJed Brown 
4833552f7358SJed Brown #undef __FUNCT__
4834552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientations"
4835a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
4836a6dfd86eSKarl Rupp {
4837552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
4838552f7358SJed Brown 
4839552f7358SJed Brown   PetscFunctionBegin;
4840552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4841552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
4842552f7358SJed Brown   PetscFunctionReturn(0);
4843552f7358SJed Brown }
4844552f7358SJed Brown 
4845552f7358SJed Brown /******************************** FEM Support **********************************/
4846552f7358SJed Brown 
4847552f7358SJed Brown #undef __FUNCT__
4848552f7358SJed Brown #define __FUNCT__ "DMPlexVecGetClosure"
4849552f7358SJed Brown /*@C
4850552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4851552f7358SJed Brown 
4852552f7358SJed Brown   Not collective
4853552f7358SJed Brown 
4854552f7358SJed Brown   Input Parameters:
4855552f7358SJed Brown + dm - The DM
48560298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4857552f7358SJed Brown . v - The local vector
4858552f7358SJed Brown - point - The sieve point in the DM
4859552f7358SJed Brown 
4860552f7358SJed Brown   Output Parameters:
48610298fd71SBarry Smith + csize - The number of values in the closure, or NULL
4862552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4863552f7358SJed Brown 
48643813dfbdSMatthew G Knepley   Fortran Notes:
48653813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
48663813dfbdSMatthew G Knepley   include petsc.h90 in your code.
48673813dfbdSMatthew G Knepley 
48683813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
48693813dfbdSMatthew G Knepley 
4870552f7358SJed Brown   Level: intermediate
4871552f7358SJed Brown 
4872552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4873552f7358SJed Brown @*/
48747c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4875a6dfd86eSKarl Rupp {
487652744b75SMatthew G. Knepley   PetscSection   clSection;
487752744b75SMatthew G. Knepley   IS             clIndices;
4878552f7358SJed Brown   PetscScalar   *array, *vArray;
48790298fd71SBarry Smith   PetscInt      *points = NULL;
4880552f7358SJed Brown   PetscInt       offsets[32];
48819df71ca4SMatthew G. Knepley   PetscInt       depth, numFields, size = 0, numPoints, pStart, pEnd, p, q, f;
4882552f7358SJed Brown   PetscErrorCode ierr;
4883552f7358SJed Brown 
4884552f7358SJed Brown   PetscFunctionBegin;
4885552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4886552f7358SJed Brown   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
488752744b75SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
488852744b75SMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clIndices);CHKERRQ(ierr);
488952744b75SMatthew G. Knepley   if (clSection) {
489052744b75SMatthew G. Knepley     const PetscInt *idx;
489152744b75SMatthew G. Knepley     PetscInt        dof, off;
489252744b75SMatthew G. Knepley 
489352744b75SMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
489452744b75SMatthew G. Knepley     if (csize) *csize = dof;
489552744b75SMatthew G. Knepley     if (values) {
489652744b75SMatthew G. Knepley       if (!*values) {
489752744b75SMatthew G. Knepley         ierr = DMGetWorkArray(dm, dof, PETSC_SCALAR, &array);CHKERRQ(ierr);
489852744b75SMatthew G. Knepley         *values = array;
489952744b75SMatthew G. Knepley       } else {
490052744b75SMatthew G. Knepley         array = *values;
490152744b75SMatthew G. Knepley       }
490252744b75SMatthew G. Knepley       ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
490352744b75SMatthew G. Knepley       ierr = ISGetIndices(clIndices, &idx);CHKERRQ(ierr);
490452744b75SMatthew G. Knepley       ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
490552744b75SMatthew G. Knepley       for (p = 0; p < dof; ++p) array[p] = vArray[idx[off+p]];
490652744b75SMatthew G. Knepley       ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
490752744b75SMatthew G. Knepley       ierr = ISRestoreIndices(clIndices, &idx);CHKERRQ(ierr);
490852744b75SMatthew G. Knepley     }
490952744b75SMatthew G. Knepley     PetscFunctionReturn(0);
4910552f7358SJed Brown   }
49115a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4912552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
49132a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
491482f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
4915552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
49165a1bb5cfSMatthew G. Knepley   if (depth == 1 && numFields < 2) {
49175a1bb5cfSMatthew G. Knepley     const PetscInt *cone, *coneO;
49185a1bb5cfSMatthew G. Knepley 
49195a1bb5cfSMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
49205a1bb5cfSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
49215a1bb5cfSMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
49223f7cbbe7SMatthew G. Knepley     if (!values || !*values) {
49239df71ca4SMatthew G. Knepley       if ((point >= pStart) && (point < pEnd)) {
49249df71ca4SMatthew G. Knepley         PetscInt dof;
49259df71ca4SMatthew G. Knepley         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
49269df71ca4SMatthew G. Knepley         size += dof;
49279df71ca4SMatthew G. Knepley       }
49289df71ca4SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
49299df71ca4SMatthew G. Knepley         const PetscInt cp = cone[p];
49302a3aaacfSMatthew G. Knepley         PetscInt       dof;
49315a1bb5cfSMatthew G. Knepley 
49325a1bb5cfSMatthew G. Knepley         if ((cp < pStart) || (cp >= pEnd)) continue;
49332a3aaacfSMatthew G. Knepley         ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
49345a1bb5cfSMatthew G. Knepley         size += dof;
49355a1bb5cfSMatthew G. Knepley       }
49363f7cbbe7SMatthew G. Knepley       if (!values) {
49373f7cbbe7SMatthew G. Knepley         if (csize) *csize = size;
49383f7cbbe7SMatthew G. Knepley         PetscFunctionReturn(0);
49393f7cbbe7SMatthew G. Knepley       }
49405a1bb5cfSMatthew G. Knepley       ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
4941982e9ed1SMatthew G. Knepley     } else {
4942982e9ed1SMatthew G. Knepley       array = *values;
4943982e9ed1SMatthew G. Knepley     }
49449df71ca4SMatthew G. Knepley     size = 0;
49455a1bb5cfSMatthew G. Knepley     ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
49469df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
49479df71ca4SMatthew G. Knepley       PetscInt     dof, off, d;
49489df71ca4SMatthew G. Knepley       PetscScalar *varr;
49499df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
49509df71ca4SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
49519df71ca4SMatthew G. Knepley       varr = &vArray[off];
49529df71ca4SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offsets[0]) {
49539df71ca4SMatthew G. Knepley         array[offsets[0]] = varr[d];
49549df71ca4SMatthew G. Knepley       }
49559df71ca4SMatthew G. Knepley       size += dof;
49569df71ca4SMatthew G. Knepley     }
49579df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
49589df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
49599df71ca4SMatthew G. Knepley       PetscInt       o  = coneO[p];
49605a1bb5cfSMatthew G. Knepley       PetscInt       dof, off, d;
49615a1bb5cfSMatthew G. Knepley       PetscScalar   *varr;
49625a1bb5cfSMatthew G. Knepley 
496352ed52e8SMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
49645a1bb5cfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
49655a1bb5cfSMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
49665a1bb5cfSMatthew G. Knepley       varr = &vArray[off];
49675a1bb5cfSMatthew G. Knepley       if (o >= 0) {
49685a1bb5cfSMatthew G. Knepley         for (d = 0; d < dof; ++d, ++offsets[0]) {
49695a1bb5cfSMatthew G. Knepley           array[offsets[0]] = varr[d];
49705a1bb5cfSMatthew G. Knepley         }
49715a1bb5cfSMatthew G. Knepley       } else {
49725a1bb5cfSMatthew G. Knepley         for (d = dof-1; d >= 0; --d, ++offsets[0]) {
49735a1bb5cfSMatthew G. Knepley           array[offsets[0]] = varr[d];
49745a1bb5cfSMatthew G. Knepley         }
49755a1bb5cfSMatthew G. Knepley       }
49769df71ca4SMatthew G. Knepley       size += dof;
49775a1bb5cfSMatthew G. Knepley     }
49785a1bb5cfSMatthew G. Knepley     ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
49799df71ca4SMatthew G. Knepley     if (!*values) {
49805a1bb5cfSMatthew G. Knepley       if (csize) *csize = size;
49815a1bb5cfSMatthew G. Knepley       *values = array;
49829df71ca4SMatthew G. Knepley     } else {
49839df71ca4SMatthew G. Knepley       if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %d < actual size %d", *csize, size);
49848c312ff3SMatthew G. Knepley       *csize = size;
49859df71ca4SMatthew G. Knepley     }
49865a1bb5cfSMatthew G. Knepley     PetscFunctionReturn(0);
49875a1bb5cfSMatthew G. Knepley   }
4988552f7358SJed Brown   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
4989552f7358SJed Brown   /* Compress out points not in the section */
4990552f7358SJed Brown   for (p = 0, q = 0; p < numPoints*2; p += 2) {
4991552f7358SJed Brown     if ((points[p] >= pStart) && (points[p] < pEnd)) {
4992552f7358SJed Brown       points[q*2]   = points[p];
4993552f7358SJed Brown       points[q*2+1] = points[p+1];
4994552f7358SJed Brown       ++q;
4995552f7358SJed Brown     }
4996552f7358SJed Brown   }
4997552f7358SJed Brown   numPoints = q;
4998bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
4999552f7358SJed Brown     for (p = 0, size = 0; p < numPoints*2; p += 2) {
5000552f7358SJed Brown       PetscInt dof, fdof;
5001552f7358SJed Brown 
5002552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5003552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
5004552f7358SJed Brown         ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5005552f7358SJed Brown         offsets[f+1] += fdof;
5006552f7358SJed Brown       }
5007552f7358SJed Brown       size += dof;
5008552f7358SJed Brown     }
5009bd6c0d32SMatthew G. Knepley     if (!values) {
5010648a61deSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5011bd6c0d32SMatthew G. Knepley       if (csize) *csize = size;
5012bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
5013bd6c0d32SMatthew G. Knepley     }
5014d0f6b257SMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
5015d0f6b257SMatthew G. Knepley   } else {
5016d0f6b257SMatthew G. Knepley     array = *values;
5017d0f6b257SMatthew G. Knepley   }
50180d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
501982f516ccSBarry Smith   if (numFields && offsets[numFields] != size) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", offsets[numFields], size);
5020552f7358SJed Brown   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
5021552f7358SJed Brown   for (p = 0; p < numPoints*2; p += 2) {
5022552f7358SJed Brown     PetscInt     o = points[p+1];
5023552f7358SJed Brown     PetscInt     dof, off, d;
5024552f7358SJed Brown     PetscScalar *varr;
5025552f7358SJed Brown 
5026552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5027552f7358SJed Brown     ierr = PetscSectionGetOffset(section, points[p], &off);CHKERRQ(ierr);
5028552f7358SJed Brown     varr = &vArray[off];
5029552f7358SJed Brown     if (numFields) {
5030552f7358SJed Brown       PetscInt fdof, foff, fcomp, f, c;
5031552f7358SJed Brown 
5032552f7358SJed Brown       for (f = 0, foff = 0; f < numFields; ++f) {
5033552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5034552f7358SJed Brown         if (o >= 0) {
5035552f7358SJed Brown           for (d = 0; d < fdof; ++d, ++offsets[f]) {
5036552f7358SJed Brown             array[offsets[f]] = varr[foff+d];
5037552f7358SJed Brown           }
5038552f7358SJed Brown         } else {
5039552f7358SJed Brown           ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
5040552f7358SJed Brown           for (d = fdof/fcomp-1; d >= 0; --d) {
5041552f7358SJed Brown             for (c = 0; c < fcomp; ++c, ++offsets[f]) {
5042552f7358SJed Brown               array[offsets[f]] = varr[foff+d*fcomp+c];
5043552f7358SJed Brown             }
5044552f7358SJed Brown           }
5045552f7358SJed Brown         }
5046552f7358SJed Brown         foff += fdof;
5047552f7358SJed Brown       }
5048552f7358SJed Brown     } else {
5049552f7358SJed Brown       if (o >= 0) {
5050552f7358SJed Brown         for (d = 0; d < dof; ++d, ++offsets[0]) {
5051552f7358SJed Brown           array[offsets[0]] = varr[d];
5052552f7358SJed Brown         }
5053552f7358SJed Brown       } else {
5054552f7358SJed Brown         for (d = dof-1; d >= 0; --d, ++offsets[0]) {
5055552f7358SJed Brown           array[offsets[0]] = varr[d];
5056552f7358SJed Brown         }
5057552f7358SJed Brown       }
5058552f7358SJed Brown     }
5059552f7358SJed Brown   }
5060552f7358SJed Brown   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5061552f7358SJed Brown   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
5062d0f6b257SMatthew G. Knepley   if (!*values) {
5063552f7358SJed Brown     if (csize) *csize = size;
5064552f7358SJed Brown     *values = array;
5065d0f6b257SMatthew G. Knepley   } else {
5066d0f6b257SMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %d < actual size %d", *csize, size);
5067d0f6b257SMatthew G. Knepley     *csize = size;
5068d0f6b257SMatthew G. Knepley   }
5069552f7358SJed Brown   PetscFunctionReturn(0);
5070552f7358SJed Brown }
5071552f7358SJed Brown 
5072552f7358SJed Brown #undef __FUNCT__
5073552f7358SJed Brown #define __FUNCT__ "DMPlexVecRestoreClosure"
5074552f7358SJed Brown /*@C
5075552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
5076552f7358SJed Brown 
5077552f7358SJed Brown   Not collective
5078552f7358SJed Brown 
5079552f7358SJed Brown   Input Parameters:
5080552f7358SJed Brown + dm - The DM
50810298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
5082552f7358SJed Brown . v - The local vector
5083552f7358SJed Brown . point - The sieve point in the DM
50840298fd71SBarry Smith . csize - The number of values in the closure, or NULL
5085552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
5086552f7358SJed Brown 
50873813dfbdSMatthew G Knepley   Fortran Notes:
50883813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
50893813dfbdSMatthew G Knepley   include petsc.h90 in your code.
50903813dfbdSMatthew G Knepley 
50913813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
50923813dfbdSMatthew G Knepley 
5093552f7358SJed Brown   Level: intermediate
5094552f7358SJed Brown 
5095552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5096552f7358SJed Brown @*/
50977c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5098a6dfd86eSKarl Rupp {
5099552f7358SJed Brown   PetscInt       size = 0;
5100552f7358SJed Brown   PetscErrorCode ierr;
5101552f7358SJed Brown 
5102552f7358SJed Brown   PetscFunctionBegin;
5103552f7358SJed Brown   /* Should work without recalculating size */
5104552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
5105552f7358SJed Brown   PetscFunctionReturn(0);
5106552f7358SJed Brown }
5107552f7358SJed Brown 
5108552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
5109552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
5110552f7358SJed Brown 
5111552f7358SJed Brown #undef __FUNCT__
5112552f7358SJed Brown #define __FUNCT__ "updatePoint_private"
5113552f7358SJed Brown PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, PetscInt orientation, const PetscScalar values[], PetscScalar array[])
5114552f7358SJed Brown {
5115552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
5116552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5117552f7358SJed Brown   PetscScalar    *a;
5118552f7358SJed Brown   PetscInt        off, cind = 0, k;
5119552f7358SJed Brown   PetscErrorCode  ierr;
5120552f7358SJed Brown 
5121552f7358SJed Brown   PetscFunctionBegin;
5122552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5123552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5124552f7358SJed Brown   a    = &array[off];
5125552f7358SJed Brown   if (!cdof || setBC) {
5126552f7358SJed Brown     if (orientation >= 0) {
5127552f7358SJed Brown       for (k = 0; k < dof; ++k) {
5128552f7358SJed Brown         fuse(&a[k], values[k]);
5129552f7358SJed Brown       }
5130552f7358SJed Brown     } else {
5131552f7358SJed Brown       for (k = 0; k < dof; ++k) {
5132552f7358SJed Brown         fuse(&a[k], values[dof-k-1]);
5133552f7358SJed Brown       }
5134552f7358SJed Brown     }
5135552f7358SJed Brown   } else {
5136552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
5137552f7358SJed Brown     if (orientation >= 0) {
5138552f7358SJed Brown       for (k = 0; k < dof; ++k) {
5139552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5140552f7358SJed Brown         fuse(&a[k], values[k]);
5141552f7358SJed Brown       }
5142552f7358SJed Brown     } else {
5143552f7358SJed Brown       for (k = 0; k < dof; ++k) {
5144552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5145552f7358SJed Brown         fuse(&a[k], values[dof-k-1]);
5146552f7358SJed Brown       }
5147552f7358SJed Brown     }
5148552f7358SJed Brown   }
5149552f7358SJed Brown   PetscFunctionReturn(0);
5150552f7358SJed Brown }
5151552f7358SJed Brown 
5152552f7358SJed Brown #undef __FUNCT__
5153a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointBC_private"
5154a5e93ea8SMatthew G. Knepley PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscInt orientation, const PetscScalar values[], PetscScalar array[])
5155a5e93ea8SMatthew G. Knepley {
5156a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
5157a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5158a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
5159a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
5160a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
5161a5e93ea8SMatthew G. Knepley 
5162a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
5163a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5164a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5165a5e93ea8SMatthew G. Knepley   a    = &array[off];
5166a5e93ea8SMatthew G. Knepley   if (cdof) {
5167a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
5168a5e93ea8SMatthew G. Knepley     if (orientation >= 0) {
5169a5e93ea8SMatthew G. Knepley       for (k = 0; k < dof; ++k) {
5170a5e93ea8SMatthew G. Knepley         if ((cind < cdof) && (k == cdofs[cind])) {
5171a5e93ea8SMatthew G. Knepley           fuse(&a[k], values[k]);
5172a5e93ea8SMatthew G. Knepley           ++cind;
5173a5e93ea8SMatthew G. Knepley         }
5174a5e93ea8SMatthew G. Knepley       }
5175a5e93ea8SMatthew G. Knepley     } else {
5176a5e93ea8SMatthew G. Knepley       for (k = 0; k < dof; ++k) {
5177a5e93ea8SMatthew G. Knepley         if ((cind < cdof) && (k == cdofs[cind])) {
5178a5e93ea8SMatthew G. Knepley           fuse(&a[k], values[dof-k-1]);
5179a5e93ea8SMatthew G. Knepley           ++cind;
5180a5e93ea8SMatthew G. Knepley         }
5181a5e93ea8SMatthew G. Knepley       }
5182a5e93ea8SMatthew G. Knepley     }
5183a5e93ea8SMatthew G. Knepley   }
5184a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5185a5e93ea8SMatthew G. Knepley }
5186a5e93ea8SMatthew G. Knepley 
5187a5e93ea8SMatthew G. Knepley #undef __FUNCT__
5188552f7358SJed Brown #define __FUNCT__ "updatePointFields_private"
5189a6dfd86eSKarl Rupp PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, PetscInt foffs[], void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, PetscInt orientation, const PetscScalar values[], PetscScalar array[])
5190a6dfd86eSKarl Rupp {
5191552f7358SJed Brown   PetscScalar   *a;
5192552f7358SJed Brown   PetscInt       numFields, off, foff, f;
5193552f7358SJed Brown   PetscErrorCode ierr;
5194552f7358SJed Brown 
5195552f7358SJed Brown   PetscFunctionBegin;
5196552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5197552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5198552f7358SJed Brown   a    = &array[off];
5199552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
5200552f7358SJed Brown     PetscInt        fdof, fcomp, fcdof;
5201552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5202552f7358SJed Brown     PetscInt        cind = 0, k, c;
5203552f7358SJed Brown 
5204552f7358SJed Brown     ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
5205552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5206552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
5207552f7358SJed Brown     if (!fcdof || setBC) {
5208552f7358SJed Brown       if (orientation >= 0) {
5209552f7358SJed Brown         for (k = 0; k < fdof; ++k) {
5210552f7358SJed Brown           fuse(&a[foff+k], values[foffs[f]+k]);
5211552f7358SJed Brown         }
5212552f7358SJed Brown       } else {
5213552f7358SJed Brown         for (k = fdof/fcomp-1; k >= 0; --k) {
5214552f7358SJed Brown           for (c = 0; c < fcomp; ++c) {
5215552f7358SJed Brown             fuse(&a[foff+(fdof/fcomp-1-k)*fcomp+c], values[foffs[f]+k*fcomp+c]);
5216552f7358SJed Brown           }
5217552f7358SJed Brown         }
5218552f7358SJed Brown       }
5219552f7358SJed Brown     } else {
5220552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
5221552f7358SJed Brown       if (orientation >= 0) {
5222552f7358SJed Brown         for (k = 0; k < fdof; ++k) {
5223552f7358SJed Brown           if ((cind < fcdof) && (k == fcdofs[cind])) {++cind; continue;}
5224552f7358SJed Brown           fuse(&a[foff+k], values[foffs[f]+k]);
5225552f7358SJed Brown         }
5226552f7358SJed Brown       } else {
5227552f7358SJed Brown         for (k = fdof/fcomp-1; k >= 0; --k) {
5228552f7358SJed Brown           for (c = 0; c < fcomp; ++c) {
5229552f7358SJed Brown             if ((cind < fcdof) && (k*fcomp+c == fcdofs[cind])) {++cind; continue;}
5230552f7358SJed Brown             fuse(&a[foff+(fdof/fcomp-1-k)*fcomp+c], values[foffs[f]+k*fcomp+c]);
5231552f7358SJed Brown           }
5232552f7358SJed Brown         }
5233552f7358SJed Brown       }
5234552f7358SJed Brown     }
5235552f7358SJed Brown     foff     += fdof;
5236552f7358SJed Brown     foffs[f] += fdof;
5237552f7358SJed Brown   }
5238552f7358SJed Brown   PetscFunctionReturn(0);
5239552f7358SJed Brown }
5240552f7358SJed Brown 
5241552f7358SJed Brown #undef __FUNCT__
5242a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointFieldsBC_private"
5243a5e93ea8SMatthew G. Knepley PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, PetscInt foffs[], void (*fuse)(PetscScalar*, PetscScalar), PetscInt orientation, const PetscScalar values[], PetscScalar array[])
5244a5e93ea8SMatthew G. Knepley {
5245a5e93ea8SMatthew G. Knepley   PetscScalar   *a;
5246a5e93ea8SMatthew G. Knepley   PetscInt       numFields, off, foff, f;
5247a5e93ea8SMatthew G. Knepley   PetscErrorCode ierr;
5248a5e93ea8SMatthew G. Knepley 
5249a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
5250a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5251a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
5252a5e93ea8SMatthew G. Knepley   a    = &array[off];
5253a5e93ea8SMatthew G. Knepley   for (f = 0, foff = 0; f < numFields; ++f) {
5254a5e93ea8SMatthew G. Knepley     PetscInt        fdof, fcomp, fcdof;
5255a5e93ea8SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5256a5e93ea8SMatthew G. Knepley     PetscInt        cind = 0, k, c;
5257a5e93ea8SMatthew G. Knepley 
5258a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
5259a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5260a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
5261a5e93ea8SMatthew G. Knepley     if (fcdof) {
5262a5e93ea8SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
5263a5e93ea8SMatthew G. Knepley       if (orientation >= 0) {
5264a5e93ea8SMatthew G. Knepley         for (k = 0; k < fdof; ++k) {
5265a5e93ea8SMatthew G. Knepley           if ((cind < fcdof) && (k == fcdofs[cind])) {
5266a5e93ea8SMatthew G. Knepley             fuse(&a[foff+k], values[foffs[f]+k]);
5267a5e93ea8SMatthew G. Knepley             ++cind;
5268a5e93ea8SMatthew G. Knepley           }
5269a5e93ea8SMatthew G. Knepley         }
5270a5e93ea8SMatthew G. Knepley       } else {
5271a5e93ea8SMatthew G. Knepley         for (k = fdof/fcomp-1; k >= 0; --k) {
5272a5e93ea8SMatthew G. Knepley           for (c = 0; c < fcomp; ++c) {
5273a5e93ea8SMatthew G. Knepley             if ((cind < fcdof) && (k*fcomp+c == fcdofs[cind])) {
5274a5e93ea8SMatthew G. Knepley               fuse(&a[foff+(fdof/fcomp-1-k)*fcomp+c], values[foffs[f]+k*fcomp+c]);
5275a5e93ea8SMatthew G. Knepley               ++cind;
5276a5e93ea8SMatthew G. Knepley             }
5277a5e93ea8SMatthew G. Knepley           }
5278a5e93ea8SMatthew G. Knepley         }
5279a5e93ea8SMatthew G. Knepley       }
5280a5e93ea8SMatthew G. Knepley     }
5281a5e93ea8SMatthew G. Knepley     foff     += fdof;
5282a5e93ea8SMatthew G. Knepley     foffs[f] += fdof;
5283a5e93ea8SMatthew G. Knepley   }
5284a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
5285a5e93ea8SMatthew G. Knepley }
5286a5e93ea8SMatthew G. Knepley 
5287a5e93ea8SMatthew G. Knepley #undef __FUNCT__
5288552f7358SJed Brown #define __FUNCT__ "DMPlexVecSetClosure"
5289552f7358SJed Brown /*@C
5290552f7358SJed Brown   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
5291552f7358SJed Brown 
5292552f7358SJed Brown   Not collective
5293552f7358SJed Brown 
5294552f7358SJed Brown   Input Parameters:
5295552f7358SJed Brown + dm - The DM
52967f5d1fdeSMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
5297552f7358SJed Brown . v - The local vector
5298552f7358SJed Brown . point - The sieve point in the DM
52997f5d1fdeSMatthew G. Knepley . values - The array of values
5300552f7358SJed Brown - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
5301552f7358SJed Brown 
53023813dfbdSMatthew G Knepley   Fortran Notes:
53037f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
53043813dfbdSMatthew G Knepley 
5305552f7358SJed Brown   Level: intermediate
5306552f7358SJed Brown 
5307552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
5308552f7358SJed Brown @*/
5309a6dfd86eSKarl Rupp PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
5310a6dfd86eSKarl Rupp {
5311552f7358SJed Brown   PetscScalar   *array;
53120298fd71SBarry Smith   PetscInt      *points = NULL;
5313552f7358SJed Brown   PetscInt       offsets[32];
5314b6ebb6e6SMatthew G. Knepley   PetscInt       depth, numFields, numPoints, off, dof, pStart, pEnd, p, q, f;
5315552f7358SJed Brown   PetscErrorCode ierr;
5316552f7358SJed Brown 
5317552f7358SJed Brown   PetscFunctionBegin;
5318552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5319552f7358SJed Brown   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5320552f7358SJed Brown   if (!section) {
5321552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
5322552f7358SJed Brown   }
5323b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
5324552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5325b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
532682f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5327552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5328b6ebb6e6SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
5329b6ebb6e6SMatthew G. Knepley     const PetscInt *cone, *coneO;
5330b6ebb6e6SMatthew G. Knepley 
5331b6ebb6e6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
5332b6ebb6e6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
5333b6ebb6e6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
5334b6ebb6e6SMatthew G. Knepley     ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5335b6ebb6e6SMatthew G. Knepley     for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
5336b6ebb6e6SMatthew G. Knepley       const PetscInt cp = !p ? point : cone[p-1];
5337b6ebb6e6SMatthew G. Knepley       const PetscInt o  = !p ? 0     : coneO[p-1];
5338b6ebb6e6SMatthew G. Knepley 
5339b6ebb6e6SMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
5340b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
5341b6ebb6e6SMatthew G. Knepley       /* ADD_VALUES */
5342b6ebb6e6SMatthew G. Knepley       {
5343b6ebb6e6SMatthew G. Knepley         const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5344b6ebb6e6SMatthew G. Knepley         PetscScalar    *a;
5345b6ebb6e6SMatthew G. Knepley         PetscInt        cdof, coff, cind = 0, k;
5346b6ebb6e6SMatthew G. Knepley 
5347b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
5348b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
5349b6ebb6e6SMatthew G. Knepley         a    = &array[coff];
5350b6ebb6e6SMatthew G. Knepley         if (!cdof) {
5351b6ebb6e6SMatthew G. Knepley           if (o >= 0) {
5352b6ebb6e6SMatthew G. Knepley             for (k = 0; k < dof; ++k) {
5353b6ebb6e6SMatthew G. Knepley               a[k] += values[off+k];
5354b6ebb6e6SMatthew G. Knepley             }
5355b6ebb6e6SMatthew G. Knepley           } else {
5356b6ebb6e6SMatthew G. Knepley             for (k = 0; k < dof; ++k) {
5357b6ebb6e6SMatthew G. Knepley               a[k] += values[off+dof-k-1];
5358b6ebb6e6SMatthew G. Knepley             }
5359b6ebb6e6SMatthew G. Knepley           }
5360b6ebb6e6SMatthew G. Knepley         } else {
5361b6ebb6e6SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
5362b6ebb6e6SMatthew G. Knepley           if (o >= 0) {
5363b6ebb6e6SMatthew G. Knepley             for (k = 0; k < dof; ++k) {
5364b6ebb6e6SMatthew G. Knepley               if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5365b6ebb6e6SMatthew G. Knepley               a[k] += values[off+k];
5366b6ebb6e6SMatthew G. Knepley             }
5367b6ebb6e6SMatthew G. Knepley           } else {
5368b6ebb6e6SMatthew G. Knepley             for (k = 0; k < dof; ++k) {
5369b6ebb6e6SMatthew G. Knepley               if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5370b6ebb6e6SMatthew G. Knepley               a[k] += values[off+dof-k-1];
5371b6ebb6e6SMatthew G. Knepley             }
5372b6ebb6e6SMatthew G. Knepley           }
5373b6ebb6e6SMatthew G. Knepley         }
5374b6ebb6e6SMatthew G. Knepley       }
5375b6ebb6e6SMatthew G. Knepley     }
5376b6ebb6e6SMatthew G. Knepley     ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5377b6ebb6e6SMatthew G. Knepley     PetscFunctionReturn(0);
5378b6ebb6e6SMatthew G. Knepley   }
5379552f7358SJed Brown   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5380552f7358SJed Brown   /* Compress out points not in the section */
5381552f7358SJed Brown   for (p = 0, q = 0; p < numPoints*2; p += 2) {
5382552f7358SJed Brown     if ((points[p] >= pStart) && (points[p] < pEnd)) {
5383552f7358SJed Brown       points[q*2]   = points[p];
5384552f7358SJed Brown       points[q*2+1] = points[p+1];
5385552f7358SJed Brown       ++q;
5386552f7358SJed Brown     }
5387552f7358SJed Brown   }
5388552f7358SJed Brown   numPoints = q;
5389552f7358SJed Brown   for (p = 0; p < numPoints*2; p += 2) {
5390552f7358SJed Brown     PetscInt fdof;
5391552f7358SJed Brown 
5392552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5393552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5394552f7358SJed Brown       offsets[f+1] += fdof;
5395552f7358SJed Brown     }
5396552f7358SJed Brown   }
53970d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
5398552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5399552f7358SJed Brown   if (numFields) {
5400552f7358SJed Brown     switch (mode) {
5401552f7358SJed Brown     case INSERT_VALUES:
5402552f7358SJed Brown       for (p = 0; p < numPoints*2; p += 2) {
5403552f7358SJed Brown         PetscInt o = points[p+1];
5404552f7358SJed Brown         updatePointFields_private(section, points[p], offsets, insert, PETSC_FALSE, o, values, array);
5405552f7358SJed Brown       } break;
5406552f7358SJed Brown     case INSERT_ALL_VALUES:
5407552f7358SJed Brown       for (p = 0; p < numPoints*2; p += 2) {
5408552f7358SJed Brown         PetscInt o = points[p+1];
5409552f7358SJed Brown         updatePointFields_private(section, points[p], offsets, insert, PETSC_TRUE,  o, values, array);
5410552f7358SJed Brown       } break;
5411a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
5412a5e93ea8SMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5413a5e93ea8SMatthew G. Knepley         PetscInt o = points[p+1];
5414a5e93ea8SMatthew G. Knepley         updatePointFieldsBC_private(section, points[p], offsets, insert,  o, values, array);
5415a5e93ea8SMatthew G. Knepley       } break;
5416552f7358SJed Brown     case ADD_VALUES:
5417552f7358SJed Brown       for (p = 0; p < numPoints*2; p += 2) {
5418552f7358SJed Brown         PetscInt o = points[p+1];
5419552f7358SJed Brown         updatePointFields_private(section, points[p], offsets, add,    PETSC_FALSE, o, values, array);
5420552f7358SJed Brown       } break;
5421552f7358SJed Brown     case ADD_ALL_VALUES:
5422552f7358SJed Brown       for (p = 0; p < numPoints*2; p += 2) {
5423552f7358SJed Brown         PetscInt o = points[p+1];
5424552f7358SJed Brown         updatePointFields_private(section, points[p], offsets, add,    PETSC_TRUE,  o, values, array);
5425552f7358SJed Brown       } break;
5426552f7358SJed Brown     default:
542782f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %D", mode);
5428552f7358SJed Brown     }
5429552f7358SJed Brown   } else {
5430552f7358SJed Brown     switch (mode) {
5431552f7358SJed Brown     case INSERT_VALUES:
5432552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
5433552f7358SJed Brown         PetscInt o = points[p+1];
5434552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5435552f7358SJed Brown         updatePoint_private(section, points[p], dof, insert, PETSC_FALSE, o, &values[off], array);
5436552f7358SJed Brown       } break;
5437552f7358SJed Brown     case INSERT_ALL_VALUES:
5438552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
5439552f7358SJed Brown         PetscInt o = points[p+1];
5440552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5441552f7358SJed Brown         updatePoint_private(section, points[p], dof, insert, PETSC_TRUE,  o, &values[off], array);
5442552f7358SJed Brown       } break;
5443a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
5444a5e93ea8SMatthew G. Knepley       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
5445a5e93ea8SMatthew G. Knepley         PetscInt o = points[p+1];
5446a5e93ea8SMatthew G. Knepley         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5447a5e93ea8SMatthew G. Knepley         updatePointBC_private(section, points[p], dof, insert,  o, &values[off], array);
5448a5e93ea8SMatthew G. Knepley       } break;
5449552f7358SJed Brown     case ADD_VALUES:
5450552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
5451552f7358SJed Brown         PetscInt o = points[p+1];
5452552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5453552f7358SJed Brown         updatePoint_private(section, points[p], dof, add,    PETSC_FALSE, o, &values[off], array);
5454552f7358SJed Brown       } break;
5455552f7358SJed Brown     case ADD_ALL_VALUES:
5456552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
5457552f7358SJed Brown         PetscInt o = points[p+1];
5458552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5459552f7358SJed Brown         updatePoint_private(section, points[p], dof, add,    PETSC_TRUE,  o, &values[off], array);
5460552f7358SJed Brown       } break;
5461552f7358SJed Brown     default:
546282f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %D", mode);
5463552f7358SJed Brown     }
5464552f7358SJed Brown   }
5465552f7358SJed Brown   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5466552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5467552f7358SJed Brown   PetscFunctionReturn(0);
5468552f7358SJed Brown }
5469552f7358SJed Brown 
5470552f7358SJed Brown #undef __FUNCT__
5471e159ff95SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateClosureIndex"
5472e159ff95SMatthew G. Knepley /*@
5473e159ff95SMatthew G. Knepley   DMPlexCreateClosureIndex - Calculate an index for the given PetscSection for the closure operation on the DM
5474e159ff95SMatthew G. Knepley 
5475e159ff95SMatthew G. Knepley   Not collective
5476e159ff95SMatthew G. Knepley 
5477e159ff95SMatthew G. Knepley   Input Parameters:
5478e159ff95SMatthew G. Knepley + dm - The DM
5479e159ff95SMatthew G. Knepley - section - The section describing the layout in v, or NULL to use the default section
5480e159ff95SMatthew G. Knepley 
5481e159ff95SMatthew G. Knepley   Note:
5482e159ff95SMatthew G. Knepley   This should greatly improve the performance of the closure operations, at the cost of additional memory.
5483e159ff95SMatthew G. Knepley 
5484e159ff95SMatthew G. Knepley   Level: intermediate
5485e159ff95SMatthew G. Knepley 
5486e159ff95SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5487e159ff95SMatthew G. Knepley @*/
5488e159ff95SMatthew G. Knepley PetscErrorCode DMPlexCreateClosureIndex(DM dm, PetscSection section)
5489e159ff95SMatthew G. Knepley {
5490e159ff95SMatthew G. Knepley   PetscSection   closureSection;
5491e159ff95SMatthew G. Knepley   IS             closureIS;
5492e159ff95SMatthew G. Knepley   PetscInt       offsets[32], *clIndices;
5493e159ff95SMatthew G. Knepley   PetscInt       depth, numFields, pStart, pEnd, point, clSize;
5494e159ff95SMatthew G. Knepley   PetscErrorCode ierr;
5495e159ff95SMatthew G. Knepley 
5496e159ff95SMatthew G. Knepley   PetscFunctionBegin;
5497e159ff95SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5498e159ff95SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
5499e159ff95SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
5500e159ff95SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5501e159ff95SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5502e159ff95SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5503e159ff95SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) section), &closureSection);CHKERRQ(ierr);
5504e159ff95SMatthew G. Knepley   ierr = PetscSectionSetChart(closureSection, pStart, pEnd);CHKERRQ(ierr);
5505e159ff95SMatthew G. Knepley   for (point = pStart; point < pEnd; ++point) {
5506e159ff95SMatthew G. Knepley     PetscInt *points = NULL, numPoints, p, dof, cldof = 0;
5507e159ff95SMatthew G. Knepley 
5508e159ff95SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5509e159ff95SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
5510e159ff95SMatthew G. Knepley       if ((points[p] >= pStart) && (points[p] < pEnd)) {
5511e159ff95SMatthew G. Knepley         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5512e159ff95SMatthew G. Knepley         cldof += dof;
5513e159ff95SMatthew G. Knepley       }
5514e159ff95SMatthew G. Knepley     }
5515e159ff95SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5516e159ff95SMatthew G. Knepley     ierr = PetscSectionSetDof(closureSection, point, cldof);CHKERRQ(ierr);
5517e159ff95SMatthew G. Knepley   }
5518e159ff95SMatthew G. Knepley   ierr = PetscSectionSetUp(closureSection);CHKERRQ(ierr);
5519e159ff95SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(closureSection, &clSize);CHKERRQ(ierr);
5520785e854fSJed Brown   ierr = PetscMalloc1(clSize, &clIndices);CHKERRQ(ierr);
5521e159ff95SMatthew G. Knepley   for (point = pStart; point < pEnd; ++point) {
5522e159ff95SMatthew G. Knepley     PetscInt *points = NULL, numPoints, p, q, cldof, cloff, fdof, f;
5523e159ff95SMatthew G. Knepley 
5524e159ff95SMatthew G. Knepley     ierr = PetscSectionGetDof(closureSection, point, &cldof);CHKERRQ(ierr);
5525e159ff95SMatthew G. Knepley     ierr = PetscSectionGetOffset(closureSection, point, &cloff);CHKERRQ(ierr);
5526e159ff95SMatthew G. Knepley     ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5527e159ff95SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5528e159ff95SMatthew G. Knepley     /* Compress out points not in the section, and create field offsets */
5529e159ff95SMatthew G. Knepley     for (p = 0, q = 0; p < numPoints*2; p += 2) {
5530e159ff95SMatthew G. Knepley       if ((points[p] >= pStart) && (points[p] < pEnd)) {
5531e159ff95SMatthew G. Knepley         points[q*2]   = points[p];
5532e159ff95SMatthew G. Knepley         points[q*2+1] = points[p+1];
5533e159ff95SMatthew G. Knepley         for (f = 0; f < numFields; ++f) {
5534e159ff95SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5535e159ff95SMatthew G. Knepley           offsets[f+1] += fdof;
5536e159ff95SMatthew G. Knepley         }
5537e159ff95SMatthew G. Knepley         ++q;
5538e159ff95SMatthew G. Knepley       }
5539e159ff95SMatthew G. Knepley     }
5540e159ff95SMatthew G. Knepley     numPoints = q;
5541e159ff95SMatthew G. Knepley     for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
5542e159ff95SMatthew G. Knepley     if (numFields && offsets[numFields] != cldof) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", offsets[numFields], cldof);
5543e159ff95SMatthew G. Knepley     /* Create indices */
5544e159ff95SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
5545e159ff95SMatthew G. Knepley       PetscInt o = points[p+1], dof, off, d;
5546e159ff95SMatthew G. Knepley 
5547e159ff95SMatthew G. Knepley       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5548e159ff95SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, points[p], &off);CHKERRQ(ierr);
5549e159ff95SMatthew G. Knepley       if (numFields) {
5550e159ff95SMatthew G. Knepley         PetscInt fdof, foff, fcomp, f, c;
5551e159ff95SMatthew G. Knepley 
5552e159ff95SMatthew G. Knepley         for (f = 0, foff = 0; f < numFields; ++f) {
5553e159ff95SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5554e159ff95SMatthew G. Knepley           if (o >= 0) {
5555e159ff95SMatthew G. Knepley             for (d = 0; d < fdof; ++d, ++offsets[f]) clIndices[cloff+offsets[f]] = off+foff+d;
5556e159ff95SMatthew G. Knepley           } else {
5557e159ff95SMatthew G. Knepley             ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
5558e159ff95SMatthew G. Knepley             for (d = fdof/fcomp-1; d >= 0; --d) {
5559e159ff95SMatthew G. Knepley               for (c = 0; c < fcomp; ++c, ++offsets[f]) clIndices[cloff+offsets[f]] = off+foff+d*fcomp+c;
5560e159ff95SMatthew G. Knepley             }
5561e159ff95SMatthew G. Knepley           }
5562e159ff95SMatthew G. Knepley           foff += fdof;
5563e159ff95SMatthew G. Knepley         }
5564e159ff95SMatthew G. Knepley       } else {
5565e159ff95SMatthew G. Knepley         if (o >= 0) for (d = 0;     d < dof; ++d) clIndices[cloff+d] = off+d;
5566e159ff95SMatthew G. Knepley         else        for (d = dof-1; d >= 0;  --d) clIndices[cloff+d] = off+d;
5567e159ff95SMatthew G. Knepley       }
5568e159ff95SMatthew G. Knepley     }
5569e159ff95SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5570e159ff95SMatthew G. Knepley   }
5571e159ff95SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, clSize, clIndices, PETSC_OWN_POINTER, &closureIS);CHKERRQ(ierr);
5572e159ff95SMatthew G. Knepley   ierr = PetscSectionSetClosureIndex(section, (PetscObject) dm, closureSection, closureIS);
5573e159ff95SMatthew G. Knepley   PetscFunctionReturn(0);
5574e159ff95SMatthew G. Knepley }
5575e159ff95SMatthew G. Knepley 
5576e159ff95SMatthew G. Knepley #undef __FUNCT__
5577552f7358SJed Brown #define __FUNCT__ "DMPlexPrintMatSetValues"
55787c1f9639SMatthew G Knepley PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numIndices, const PetscInt indices[], const PetscScalar values[])
5579552f7358SJed Brown {
5580552f7358SJed Brown   PetscMPIInt    rank;
5581552f7358SJed Brown   PetscInt       i, j;
5582552f7358SJed Brown   PetscErrorCode ierr;
5583552f7358SJed Brown 
5584552f7358SJed Brown   PetscFunctionBegin;
558582f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
558677a6746dSMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer, "[%D]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
5587552f7358SJed Brown   for (i = 0; i < numIndices; i++) {
558877a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "[%D]mat indices[%D] = %D\n", rank, i, indices[i]);CHKERRQ(ierr);
5589552f7358SJed Brown   }
5590552f7358SJed Brown   for (i = 0; i < numIndices; i++) {
559177a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "[%D]", rank);CHKERRQ(ierr);
5592552f7358SJed Brown     for (j = 0; j < numIndices; j++) {
5593519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
559477a6746dSMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%G,%G)", PetscRealPart(values[i*numIndices+j]), PetscImaginaryPart(values[i*numIndices+j]));CHKERRQ(ierr);
5595552f7358SJed Brown #else
559677a6746dSMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %G", values[i*numIndices+j]);CHKERRQ(ierr);
5597552f7358SJed Brown #endif
5598552f7358SJed Brown     }
559977a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5600552f7358SJed Brown   }
5601552f7358SJed Brown   PetscFunctionReturn(0);
5602552f7358SJed Brown }
5603552f7358SJed Brown 
5604552f7358SJed Brown #undef __FUNCT__
5605552f7358SJed Brown #define __FUNCT__ "indicesPoint_private"
5606552f7358SJed Brown /* . off - The global offset of this point */
5607e6ccafaeSMatthew G Knepley PetscErrorCode indicesPoint_private(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, PetscInt orientation, PetscInt indices[])
5608a6dfd86eSKarl Rupp {
5609e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
5610552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
5611552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5612552f7358SJed Brown   PetscInt        cind = 0, k;
5613552f7358SJed Brown   PetscErrorCode  ierr;
5614552f7358SJed Brown 
5615552f7358SJed Brown   PetscFunctionBegin;
5616552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5617552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5618552f7358SJed Brown   if (!cdof || setBC) {
5619552f7358SJed Brown     if (orientation >= 0) {
5620e6ccafaeSMatthew G Knepley       for (k = 0; k < dof; ++k) indices[*loff+k] = off+k;
5621552f7358SJed Brown     } else {
5622e6ccafaeSMatthew G Knepley       for (k = 0; k < dof; ++k) indices[*loff+dof-k-1] = off+k;
5623552f7358SJed Brown     }
5624552f7358SJed Brown   } else {
5625552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
5626552f7358SJed Brown     if (orientation >= 0) {
5627552f7358SJed Brown       for (k = 0; k < dof; ++k) {
5628552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
5629552f7358SJed Brown           /* Insert check for returning constrained indices */
5630e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
5631552f7358SJed Brown           ++cind;
5632552f7358SJed Brown         } else {
5633e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
5634552f7358SJed Brown         }
5635552f7358SJed Brown       }
5636552f7358SJed Brown     } else {
5637552f7358SJed Brown       for (k = 0; k < dof; ++k) {
5638552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
5639552f7358SJed Brown           /* Insert check for returning constrained indices */
5640e6ccafaeSMatthew G Knepley           indices[*loff+dof-k-1] = -(off+k+1);
5641552f7358SJed Brown           ++cind;
5642552f7358SJed Brown         } else {
5643e6ccafaeSMatthew G Knepley           indices[*loff+dof-k-1] = off+k-cind;
5644552f7358SJed Brown         }
5645552f7358SJed Brown       }
5646552f7358SJed Brown     }
5647552f7358SJed Brown   }
5648e6ccafaeSMatthew G Knepley   *loff += dof;
5649552f7358SJed Brown   PetscFunctionReturn(0);
5650552f7358SJed Brown }
5651552f7358SJed Brown 
5652552f7358SJed Brown #undef __FUNCT__
5653552f7358SJed Brown #define __FUNCT__ "indicesPointFields_private"
5654552f7358SJed Brown /* . off - The global offset of this point */
5655a6dfd86eSKarl Rupp PetscErrorCode indicesPointFields_private(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, PetscInt orientation, PetscInt indices[])
5656a6dfd86eSKarl Rupp {
5657552f7358SJed Brown   PetscInt       numFields, foff, f;
5658552f7358SJed Brown   PetscErrorCode ierr;
5659552f7358SJed Brown 
5660552f7358SJed Brown   PetscFunctionBegin;
5661552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5662552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
5663552f7358SJed Brown     PetscInt        fdof, fcomp, cfdof;
5664552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5665552f7358SJed Brown     PetscInt        cind = 0, k, c;
5666552f7358SJed Brown 
5667552f7358SJed Brown     ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
5668552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5669552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5670552f7358SJed Brown     if (!cfdof || setBC) {
5671552f7358SJed Brown       if (orientation >= 0) {
56720d644c17SKarl Rupp         for (k = 0; k < fdof; ++k) indices[foffs[f]+k] = off+foff+k;
5673552f7358SJed Brown       } else {
5674552f7358SJed Brown         for (k = fdof/fcomp-1; k >= 0; --k) {
5675552f7358SJed Brown           for (c = 0; c < fcomp; ++c) {
5676552f7358SJed Brown             indices[foffs[f]+k*fcomp+c] = off+foff+(fdof/fcomp-1-k)*fcomp+c;
5677552f7358SJed Brown           }
5678552f7358SJed Brown         }
5679552f7358SJed Brown       }
5680552f7358SJed Brown     } else {
5681552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
5682552f7358SJed Brown       if (orientation >= 0) {
5683552f7358SJed Brown         for (k = 0; k < fdof; ++k) {
5684552f7358SJed Brown           if ((cind < cfdof) && (k == fcdofs[cind])) {
5685552f7358SJed Brown             indices[foffs[f]+k] = -(off+foff+k+1);
5686552f7358SJed Brown             ++cind;
5687552f7358SJed Brown           } else {
5688552f7358SJed Brown             indices[foffs[f]+k] = off+foff+k-cind;
5689552f7358SJed Brown           }
5690552f7358SJed Brown         }
5691552f7358SJed Brown       } else {
5692552f7358SJed Brown         for (k = fdof/fcomp-1; k >= 0; --k) {
5693552f7358SJed Brown           for (c = 0; c < fcomp; ++c) {
5694552f7358SJed Brown             if ((cind < cfdof) && ((fdof/fcomp-1-k)*fcomp+c == fcdofs[cind])) {
5695552f7358SJed Brown               indices[foffs[f]+k*fcomp+c] = -(off+foff+(fdof/fcomp-1-k)*fcomp+c+1);
5696552f7358SJed Brown               ++cind;
5697552f7358SJed Brown             } else {
5698552f7358SJed Brown               indices[foffs[f]+k*fcomp+c] = off+foff+(fdof/fcomp-1-k)*fcomp+c-cind;
5699552f7358SJed Brown             }
5700552f7358SJed Brown           }
5701552f7358SJed Brown         }
5702552f7358SJed Brown       }
5703552f7358SJed Brown     }
5704552f7358SJed Brown     foff     += fdof - cfdof;
5705552f7358SJed Brown     foffs[f] += fdof;
5706552f7358SJed Brown   }
5707552f7358SJed Brown   PetscFunctionReturn(0);
5708552f7358SJed Brown }
5709552f7358SJed Brown 
5710552f7358SJed Brown #undef __FUNCT__
5711552f7358SJed Brown #define __FUNCT__ "DMPlexMatSetClosure"
57127f5d1fdeSMatthew G. Knepley /*@C
57137f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
57147f5d1fdeSMatthew G. Knepley 
57157f5d1fdeSMatthew G. Knepley   Not collective
57167f5d1fdeSMatthew G. Knepley 
57177f5d1fdeSMatthew G. Knepley   Input Parameters:
57187f5d1fdeSMatthew G. Knepley + dm - The DM
57193dc93601SMatthew G. Knepley . section - The section describing the layout in v
57203dc93601SMatthew G. Knepley . globalSection - The section describing the layout in v
57217f5d1fdeSMatthew G. Knepley . A - The matrix
57227f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
57237f5d1fdeSMatthew G. Knepley . values - The array of values
57247f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
57257f5d1fdeSMatthew G. Knepley 
57267f5d1fdeSMatthew G. Knepley   Fortran Notes:
57277f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
57287f5d1fdeSMatthew G. Knepley 
57297f5d1fdeSMatthew G. Knepley   Level: intermediate
57307f5d1fdeSMatthew G. Knepley 
57317f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
57327f5d1fdeSMatthew G. Knepley @*/
57337c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5734552f7358SJed Brown {
5735552f7358SJed Brown   DM_Plex       *mesh   = (DM_Plex*) dm->data;
57360298fd71SBarry Smith   PetscInt      *points = NULL;
5737552f7358SJed Brown   PetscInt      *indices;
5738552f7358SJed Brown   PetscInt       offsets[32];
5739552f7358SJed Brown   PetscInt       numFields, numPoints, numIndices, dof, off, globalOff, pStart, pEnd, p, q, f;
5740552f7358SJed Brown   PetscErrorCode ierr;
5741552f7358SJed Brown 
5742552f7358SJed Brown   PetscFunctionBegin;
5743552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57443dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
57453dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
57463dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5747552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
574882f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5749552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5750552f7358SJed Brown   ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5751552f7358SJed Brown   /* Compress out points not in the section */
5752552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5753552f7358SJed Brown   for (p = 0, q = 0; p < numPoints*2; p += 2) {
5754552f7358SJed Brown     if ((points[p] >= pStart) && (points[p] < pEnd)) {
5755552f7358SJed Brown       points[q*2]   = points[p];
5756552f7358SJed Brown       points[q*2+1] = points[p+1];
5757552f7358SJed Brown       ++q;
5758552f7358SJed Brown     }
5759552f7358SJed Brown   }
5760552f7358SJed Brown   numPoints = q;
5761552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5762552f7358SJed Brown     PetscInt fdof;
5763552f7358SJed Brown 
5764552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5765552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5766552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5767552f7358SJed Brown       offsets[f+1] += fdof;
5768552f7358SJed Brown     }
5769552f7358SJed Brown     numIndices += dof;
5770552f7358SJed Brown   }
57710d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
57720d644c17SKarl Rupp 
577382f516ccSBarry Smith   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", offsets[numFields], numIndices);
5774552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5775552f7358SJed Brown   if (numFields) {
5776552f7358SJed Brown     for (p = 0; p < numPoints*2; p += 2) {
5777552f7358SJed Brown       PetscInt o = points[p+1];
5778552f7358SJed Brown       ierr = PetscSectionGetOffset(globalSection, points[p], &globalOff);CHKERRQ(ierr);
5779552f7358SJed Brown       indicesPointFields_private(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, o, indices);
5780552f7358SJed Brown     }
5781552f7358SJed Brown   } else {
5782e6ccafaeSMatthew G Knepley     for (p = 0, off = 0; p < numPoints*2; p += 2) {
5783552f7358SJed Brown       PetscInt o = points[p+1];
5784552f7358SJed Brown       ierr = PetscSectionGetOffset(globalSection, points[p], &globalOff);CHKERRQ(ierr);
5785e6ccafaeSMatthew G Knepley       indicesPoint_private(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, indices);
5786552f7358SJed Brown     }
5787552f7358SJed Brown   }
578877a6746dSMatthew G Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, values);CHKERRQ(ierr);}
5789552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5790552f7358SJed Brown   if (ierr) {
5791552f7358SJed Brown     PetscMPIInt    rank;
5792552f7358SJed Brown     PetscErrorCode ierr2;
5793552f7358SJed Brown 
579482f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
579577a6746dSMatthew G Knepley     ierr2 = (*PetscErrorPrintf)("[%D]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
579677a6746dSMatthew G Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, values);CHKERRQ(ierr2);
57976a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5798552f7358SJed Brown     CHKERRQ(ierr);
5799552f7358SJed Brown   }
5800552f7358SJed Brown   ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5801552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5802552f7358SJed Brown   PetscFunctionReturn(0);
5803552f7358SJed Brown }
5804552f7358SJed Brown 
5805552f7358SJed Brown #undef __FUNCT__
5806770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexGetHybridBounds"
5807f96281f8SMatthew G. Knepley /*@
5808f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5809f96281f8SMatthew G. Knepley 
5810f96281f8SMatthew G. Knepley   Input Parameter:
5811f96281f8SMatthew G. Knepley . dm - The DMPlex object
5812f96281f8SMatthew G. Knepley 
5813f96281f8SMatthew G. Knepley   Output Parameters:
5814f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5815f96281f8SMatthew G. Knepley . cMax - The first hybrid face
5816f96281f8SMatthew G. Knepley . cMax - The first hybrid edge
5817f96281f8SMatthew G. Knepley - cMax - The first hybrid vertex
5818f96281f8SMatthew G. Knepley 
5819f96281f8SMatthew G. Knepley   Level: developer
5820f96281f8SMatthew G. Knepley 
5821f96281f8SMatthew G. Knepley .seealso DMPlexCreateHybridMesh()
5822f96281f8SMatthew G. Knepley @*/
5823770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5824552f7358SJed Brown {
5825552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5826770b213bSMatthew G Knepley   PetscInt       dim;
5827770b213bSMatthew G Knepley   PetscErrorCode ierr;
5828552f7358SJed Brown 
5829552f7358SJed Brown   PetscFunctionBegin;
5830552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5831770b213bSMatthew G Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
5832770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5833770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5834770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5835770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5836552f7358SJed Brown   PetscFunctionReturn(0);
5837552f7358SJed Brown }
5838552f7358SJed Brown 
5839552f7358SJed Brown #undef __FUNCT__
5840770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexSetHybridBounds"
5841770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5842552f7358SJed Brown {
5843552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5844770b213bSMatthew G Knepley   PetscInt       dim;
5845770b213bSMatthew G Knepley   PetscErrorCode ierr;
5846552f7358SJed Brown 
5847552f7358SJed Brown   PetscFunctionBegin;
5848552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5849770b213bSMatthew G Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
5850770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5851770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5852770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5853770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5854552f7358SJed Brown   PetscFunctionReturn(0);
5855552f7358SJed Brown }
5856552f7358SJed Brown 
5857552f7358SJed Brown #undef __FUNCT__
5858552f7358SJed Brown #define __FUNCT__ "DMPlexGetVTKCellHeight"
5859552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5860552f7358SJed Brown {
5861552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5862552f7358SJed Brown 
5863552f7358SJed Brown   PetscFunctionBegin;
5864552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5865552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5866552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5867552f7358SJed Brown   PetscFunctionReturn(0);
5868552f7358SJed Brown }
5869552f7358SJed Brown 
5870552f7358SJed Brown #undef __FUNCT__
5871552f7358SJed Brown #define __FUNCT__ "DMPlexSetVTKCellHeight"
5872552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5873552f7358SJed Brown {
5874552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5875552f7358SJed Brown 
5876552f7358SJed Brown   PetscFunctionBegin;
5877552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5878552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5879552f7358SJed Brown   PetscFunctionReturn(0);
5880552f7358SJed Brown }
5881552f7358SJed Brown 
5882552f7358SJed Brown #undef __FUNCT__
5883552f7358SJed Brown #define __FUNCT__ "DMPlexCreateNumbering_Private"
5884552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5885552f7358SJed Brown PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscSF sf, IS *numbering)
5886552f7358SJed Brown {
5887552f7358SJed Brown   PetscSection   section, globalSection;
5888552f7358SJed Brown   PetscInt      *numbers, p;
5889552f7358SJed Brown   PetscErrorCode ierr;
5890552f7358SJed Brown 
5891552f7358SJed Brown   PetscFunctionBegin;
589282f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5893552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5894552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5895552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5896552f7358SJed Brown   }
5897552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
5898552f7358SJed Brown   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5899785e854fSJed Brown   ierr = PetscMalloc1((pEnd - pStart), &numbers);CHKERRQ(ierr);
5900552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5901552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5902552f7358SJed Brown   }
590382f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5904552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5905552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
5906552f7358SJed Brown   PetscFunctionReturn(0);
5907552f7358SJed Brown }
5908552f7358SJed Brown 
5909552f7358SJed Brown #undef __FUNCT__
5910552f7358SJed Brown #define __FUNCT__ "DMPlexGetCellNumbering"
5911552f7358SJed Brown PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
5912552f7358SJed Brown {
5913552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5914552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
5915552f7358SJed Brown   PetscErrorCode ierr;
5916552f7358SJed Brown 
5917552f7358SJed Brown   PetscFunctionBegin;
5918552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5919552f7358SJed Brown   if (!mesh->globalCellNumbers) {
5920552f7358SJed Brown     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
5921552f7358SJed Brown     ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
59220298fd71SBarry Smith     ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
59230d644c17SKarl Rupp     if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
5924552f7358SJed Brown     ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, dm->sf, &mesh->globalCellNumbers);CHKERRQ(ierr);
5925552f7358SJed Brown   }
5926552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
5927552f7358SJed Brown   PetscFunctionReturn(0);
5928552f7358SJed Brown }
5929552f7358SJed Brown 
5930552f7358SJed Brown #undef __FUNCT__
5931552f7358SJed Brown #define __FUNCT__ "DMPlexGetVertexNumbering"
5932552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
5933552f7358SJed Brown {
5934552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5935552f7358SJed Brown   PetscInt       vStart, vEnd, vMax;
5936552f7358SJed Brown   PetscErrorCode ierr;
5937552f7358SJed Brown 
5938552f7358SJed Brown   PetscFunctionBegin;
5939552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5940552f7358SJed Brown   if (!mesh->globalVertexNumbers) {
5941552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
59420298fd71SBarry Smith     ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
59430d644c17SKarl Rupp     if (vMax >= 0) vEnd = PetscMin(vEnd, vMax);
5944552f7358SJed Brown     ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, dm->sf, &mesh->globalVertexNumbers);CHKERRQ(ierr);
5945552f7358SJed Brown   }
5946552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
5947552f7358SJed Brown   PetscFunctionReturn(0);
5948552f7358SJed Brown }
5949552f7358SJed Brown 
5950552f7358SJed Brown 
5951552f7358SJed Brown #undef __FUNCT__
5952552f7358SJed Brown #define __FUNCT__ "PetscSectionCreateGlobalSectionLabel"
5953552f7358SJed Brown /*@C
5954552f7358SJed Brown   PetscSectionCreateGlobalSectionLabel - Create a section describing the global field layout using
5955552f7358SJed Brown   the local section and an SF describing the section point overlap.
5956552f7358SJed Brown 
5957552f7358SJed Brown   Input Parameters:
5958552f7358SJed Brown   + s - The PetscSection for the local field layout
5959552f7358SJed Brown   . sf - The SF describing parallel layout of the section points
5960552f7358SJed Brown   . includeConstraints - By default this is PETSC_FALSE, meaning that the global field vector will not possess constrained dofs
5961552f7358SJed Brown   . label - The label specifying the points
5962552f7358SJed Brown   - labelValue - The label stratum specifying the points
5963552f7358SJed Brown 
5964552f7358SJed Brown   Output Parameter:
5965552f7358SJed Brown   . gsection - The PetscSection for the global field layout
5966552f7358SJed Brown 
5967552f7358SJed Brown   Note: This gives negative sizes and offsets to points not owned by this process
5968552f7358SJed Brown 
5969552f7358SJed Brown   Level: developer
5970552f7358SJed Brown 
5971552f7358SJed Brown .seealso: PetscSectionCreate()
5972552f7358SJed Brown @*/
5973552f7358SJed Brown PetscErrorCode PetscSectionCreateGlobalSectionLabel(PetscSection s, PetscSF sf, PetscBool includeConstraints, DMLabel label, PetscInt labelValue, PetscSection *gsection)
5974552f7358SJed Brown {
59757ec39499SMatthew G. Knepley   PetscInt      *neg = NULL, *tmpOff = NULL;
5976552f7358SJed Brown   PetscInt       pStart, pEnd, p, dof, cdof, off, globalOff = 0, nroots;
5977552f7358SJed Brown   PetscErrorCode ierr;
5978552f7358SJed Brown 
5979552f7358SJed Brown   PetscFunctionBegin;
5980552f7358SJed Brown   ierr = PetscSectionCreate(s->atlasLayout.comm, gsection);CHKERRQ(ierr);
5981552f7358SJed Brown   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
5982552f7358SJed Brown   ierr = PetscSectionSetChart(*gsection, pStart, pEnd);CHKERRQ(ierr);
59837ec39499SMatthew G. Knepley   ierr = PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
59847ec39499SMatthew G. Knepley   if (nroots >= 0) {
59857ec39499SMatthew G. Knepley     if (nroots < pEnd-pStart) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %d < %d section size", nroots, pEnd-pStart);
5986*1795a4d1SJed Brown     ierr = PetscCalloc1(nroots, &neg);CHKERRQ(ierr);
59877ec39499SMatthew G. Knepley     if (nroots > pEnd-pStart) {
5988*1795a4d1SJed Brown       ierr = PetscCalloc1(nroots, &tmpOff);CHKERRQ(ierr);
59897ec39499SMatthew G. Knepley     } else {
59907ec39499SMatthew G. Knepley       tmpOff = &(*gsection)->atlasDof[-pStart];
59917ec39499SMatthew G. Knepley     }
59927ec39499SMatthew G. Knepley   }
5993552f7358SJed Brown   /* Mark ghost points with negative dof */
5994552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5995552f7358SJed Brown     PetscInt value;
5996552f7358SJed Brown 
5997552f7358SJed Brown     ierr = DMLabelGetValue(label, p, &value);CHKERRQ(ierr);
5998552f7358SJed Brown     if (value != labelValue) continue;
5999552f7358SJed Brown     ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
6000552f7358SJed Brown     ierr = PetscSectionSetDof(*gsection, p, dof);CHKERRQ(ierr);
6001552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
6002552f7358SJed Brown     if (!includeConstraints && cdof > 0) {ierr = PetscSectionSetConstraintDof(*gsection, p, cdof);CHKERRQ(ierr);}
60037ec39499SMatthew G. Knepley     if (neg) neg[p] = -(dof+1);
6004552f7358SJed Brown   }
6005552f7358SJed Brown   ierr = PetscSectionSetUpBC(*gsection);CHKERRQ(ierr);
6006552f7358SJed Brown   if (nroots >= 0) {
60077ec39499SMatthew G. Knepley     ierr = PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
60087ec39499SMatthew G. Knepley     ierr = PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
6009552f7358SJed Brown     if (nroots > pEnd-pStart) {
60107ec39499SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasDof[p-pStart] = tmpOff[p];}
6011552f7358SJed Brown     }
6012552f7358SJed Brown   }
6013552f7358SJed Brown   /* Calculate new sizes, get proccess offset, and calculate point offsets */
6014552f7358SJed Brown   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
6015552f7358SJed Brown     cdof = (!includeConstraints && s->bc) ? s->bc->atlasDof[p] : 0;
6016552f7358SJed Brown     (*gsection)->atlasOff[p] = off;
6017552f7358SJed Brown     off += (*gsection)->atlasDof[p] > 0 ? (*gsection)->atlasDof[p]-cdof : 0;
6018552f7358SJed Brown   }
6019552f7358SJed Brown   ierr       = MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, s->atlasLayout.comm);CHKERRQ(ierr);
6020552f7358SJed Brown   globalOff -= off;
6021552f7358SJed Brown   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
6022552f7358SJed Brown     (*gsection)->atlasOff[p] += globalOff;
60237ec39499SMatthew G. Knepley     if (neg) neg[p] = -((*gsection)->atlasOff[p]+1);
6024552f7358SJed Brown   }
6025552f7358SJed Brown   /* Put in negative offsets for ghost points */
6026552f7358SJed Brown   if (nroots >= 0) {
60277ec39499SMatthew G. Knepley     ierr = PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
60287ec39499SMatthew G. Knepley     ierr = PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
6029552f7358SJed Brown     if (nroots > pEnd-pStart) {
60307ec39499SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasOff[p-pStart] = tmpOff[p];}
6031552f7358SJed Brown     }
6032552f7358SJed Brown   }
60337ec39499SMatthew G. Knepley   if (nroots >= 0 && nroots > pEnd-pStart) {ierr = PetscFree(tmpOff);CHKERRQ(ierr);}
6034552f7358SJed Brown   ierr = PetscFree(neg);CHKERRQ(ierr);
6035552f7358SJed Brown   PetscFunctionReturn(0);
6036552f7358SJed Brown }
6037ca8062c8SMatthew G. Knepley 
6038ca8062c8SMatthew G. Knepley #undef __FUNCT__
6039ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSymmetry"
6040ca8062c8SMatthew G. Knepley /*@
6041ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6042ca8062c8SMatthew G. Knepley 
6043ca8062c8SMatthew G. Knepley   Input Parameters:
6044ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
6045ca8062c8SMatthew G. Knepley 
6046ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6047ca8062c8SMatthew G. Knepley 
6048ca8062c8SMatthew G. Knepley   Level: developer
6049ca8062c8SMatthew G. Knepley 
6050ca8062c8SMatthew G. Knepley .seealso: DMCreate()
6051ca8062c8SMatthew G. Knepley @*/
6052ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6053ca8062c8SMatthew G. Knepley {
6054ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6055ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6056ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
6057ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
6058ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6059ca8062c8SMatthew G. Knepley 
6060ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6061ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6062ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6063ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6064ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6065ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6066ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6067ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6068ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6069ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
6070ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6071ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6072ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6073ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6074ca8062c8SMatthew G. Knepley       }
6075ca8062c8SMatthew G. Knepley       if (s >= supportSize) {
6076ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d cone: ", p);
6077ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
6078ca8062c8SMatthew G. Knepley           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", cone[s]);
6079ca8062c8SMatthew G. Knepley         }
6080ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");
6081ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d support: ", cone[c]);
6082ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
6083ca8062c8SMatthew G. Knepley           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", support[s]);
6084ca8062c8SMatthew G. Knepley         }
6085ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");
6086ca8062c8SMatthew G. Knepley         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not found in support of cone point %d", p, cone[c]);
6087ca8062c8SMatthew G. Knepley       }
6088ca8062c8SMatthew G. Knepley     }
6089ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6090ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6091ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6092ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6093ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6094ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6095ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6096ca8062c8SMatthew G. Knepley       }
6097ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
6098ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d support: ", p);
6099ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
6100ca8062c8SMatthew G. Knepley           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", support[c]);
6101ca8062c8SMatthew G. Knepley         }
6102ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");
6103ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d cone: ", support[s]);
6104ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
6105ca8062c8SMatthew G. Knepley           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", cone[c]);
6106ca8062c8SMatthew G. Knepley         }
6107ca8062c8SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");
6108ca8062c8SMatthew G. Knepley         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not found in cone of support point %d", p, support[s]);
6109ca8062c8SMatthew G. Knepley       }
6110ca8062c8SMatthew G. Knepley     }
6111ca8062c8SMatthew G. Knepley   }
6112ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6113ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
6114ca8062c8SMatthew G. Knepley   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %d != Total support size %d", csize, ssize);
6115ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6116ca8062c8SMatthew G. Knepley }
6117ca8062c8SMatthew G. Knepley 
6118ca8062c8SMatthew G. Knepley #undef __FUNCT__
6119ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSkeleton"
6120ca8062c8SMatthew G. Knepley /*@
6121ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6122ca8062c8SMatthew G. Knepley 
6123ca8062c8SMatthew G. Knepley   Input Parameters:
6124ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
6125ca8062c8SMatthew G. Knepley 
6126ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6127ca8062c8SMatthew G. Knepley 
6128ca8062c8SMatthew G. Knepley   Level: developer
6129ca8062c8SMatthew G. Knepley 
6130ca8062c8SMatthew G. Knepley .seealso: DMCreate()
6131ca8062c8SMatthew G. Knepley @*/
6132ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex)
6133ca8062c8SMatthew G. Knepley {
6134ca8062c8SMatthew G. Knepley   DM             udm;
6135ca8062c8SMatthew G. Knepley   PetscInt       dim, numCorners, coneSize, cStart, cEnd, cMax, c;
6136ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6137ca8062c8SMatthew G. Knepley 
6138ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6139ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6140ca8062c8SMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
6141ca8062c8SMatthew G. Knepley   switch (dim) {
6142f4e3e319SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; break;
6143ca8062c8SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; break;
6144ca8062c8SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; break;
6145ca8062c8SMatthew G. Knepley   default:
6146ca8062c8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %d", dim);
6147ca8062c8SMatthew G. Knepley   }
6148ca8062c8SMatthew G. Knepley   ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
6149ca8062c8SMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) udm, "un_");CHKERRQ(ierr);
6150ca8062c8SMatthew G. Knepley   ierr = DMSetFromOptions(udm);CHKERRQ(ierr);
6151ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6152ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6153ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6154ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
6155ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(udm, c, &coneSize);CHKERRQ(ierr);
6156ca8062c8SMatthew G. Knepley     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %d has  %d vertices != %d", c, coneSize, numCorners);
6157ca8062c8SMatthew G. Knepley   }
6158ca8062c8SMatthew G. Knepley   ierr = DMDestroy(&udm);CHKERRQ(ierr);
6159ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6160ca8062c8SMatthew G. Knepley }
6161