Actual source code: plex.c
1: #include <petsc/private/dmpleximpl.h>
2: #include <petsc/private/dmlabelimpl.h>
3: #include <petsc/private/isimpl.h>
4: #include <petsc/private/vecimpl.h>
5: #include <petsc/private/glvisvecimpl.h>
6: #include <petscsf.h>
7: #include <petscds.h>
8: #include <petscdraw.h>
9: #include <petscdmfield.h>
10: #include <petscdmplextransform.h>
12: /* Logging support */
13: PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_InterpolateSF, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Symmetrize, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh, DMPLEX_RebalanceSharedPoints, DMPLEX_PartSelf, DMPLEX_PartLabelInvert, DMPLEX_PartLabelCreateSF, DMPLEX_PartStratSF, DMPLEX_CreatePointSF, DMPLEX_LocatePoints, DMPLEX_TopologyView, DMPLEX_LabelsView, DMPLEX_CoordinatesView, DMPLEX_SectionView, DMPLEX_GlobalVectorView, DMPLEX_LocalVectorView, DMPLEX_TopologyLoad, DMPLEX_LabelsLoad, DMPLEX_CoordinatesLoad, DMPLEX_SectionLoad, DMPLEX_GlobalVectorLoad, DMPLEX_LocalVectorLoad;
14: PetscLogEvent DMPLEX_RebalBuildGraph, DMPLEX_RebalRewriteSF, DMPLEX_RebalGatherGraph, DMPLEX_RebalPartition, DMPLEX_RebalScatterPart;
16: PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
18: /*@
19: DMPlexIsSimplex - Is the first cell in this mesh a simplex?
21: Input Parameter:
22: . dm - The `DMPLEX` object
24: Output Parameter:
25: . simplex - Flag checking for a simplex
27: Level: intermediate
29: Note:
30: This just gives the first range of cells found. If the mesh has several cell types, it will only give the first.
31: If the mesh has no cells, this returns `PETSC_FALSE`.
33: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetSimplexOrBoxCells()`, `DMPlexGetCellType()`, `DMPlexGetHeightStratum()`, `DMPolytopeTypeGetNumVertices()`
34: @*/
35: PetscErrorCode DMPlexIsSimplex(DM dm, PetscBool *simplex)
36: {
37: DMPolytopeType ct;
38: PetscInt cStart, cEnd;
40: DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
41: if (cEnd <= cStart) {
42: *simplex = PETSC_FALSE;
43: return 0;
44: }
45: DMPlexGetCellType(dm, cStart, &ct);
46: *simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
47: return 0;
48: }
50: /*@
51: DMPlexGetSimplexOrBoxCells - Get the range of cells which are neither prisms nor ghost FV cells
53: Input Parameters:
54: + dm - The `DMPLEX` object
55: - height - The cell height in the Plex, 0 is the default
57: Output Parameters:
58: + cStart - The first "normal" cell
59: - cEnd - The upper bound on "normal"" cells
61: Level: developer
63: Note:
64: This just gives the first range of cells found. If the mesh has several cell types, it will only give the first.
66: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexConstructGhostCells()`, `DMPlexGetGhostCellStratum()`
67: @*/
68: PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PetscInt *cStart, PetscInt *cEnd)
69: {
70: DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
71: PetscInt cS, cE, c;
73: DMPlexGetHeightStratum(dm, PetscMax(height, 0), &cS, &cE);
74: for (c = cS; c < cE; ++c) {
75: DMPolytopeType cct;
77: DMPlexGetCellType(dm, c, &cct);
78: if ((PetscInt)cct < 0) break;
79: switch (cct) {
80: case DM_POLYTOPE_POINT:
81: case DM_POLYTOPE_SEGMENT:
82: case DM_POLYTOPE_TRIANGLE:
83: case DM_POLYTOPE_QUADRILATERAL:
84: case DM_POLYTOPE_TETRAHEDRON:
85: case DM_POLYTOPE_HEXAHEDRON:
86: ct = cct;
87: break;
88: default:
89: break;
90: }
91: if (ct != DM_POLYTOPE_UNKNOWN) break;
92: }
93: if (ct != DM_POLYTOPE_UNKNOWN) {
94: DMLabel ctLabel;
96: DMPlexGetCellTypeLabel(dm, &ctLabel);
97: DMLabelGetStratumBounds(ctLabel, ct, &cS, &cE);
98: // Reset label for fast lookup
99: DMLabelMakeAllInvalid_Internal(ctLabel);
100: }
101: if (cStart) *cStart = cS;
102: if (cEnd) *cEnd = cE;
103: return 0;
104: }
106: PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
107: {
108: PetscInt cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd;
109: PetscInt vcdof[2] = {0, 0}, globalvcdof[2];
111: *ft = PETSC_VTK_INVALID;
112: DMGetCoordinateDim(dm, &cdim);
113: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
114: DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);
115: PetscSectionGetChart(section, &pStart, &pEnd);
116: if (field >= 0) {
117: if ((vStart >= pStart) && (vStart < pEnd)) PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);
118: if ((cStart >= pStart) && (cStart < pEnd)) PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);
119: } else {
120: if ((vStart >= pStart) && (vStart < pEnd)) PetscSectionGetDof(section, vStart, &vcdof[0]);
121: if ((cStart >= pStart) && (cStart < pEnd)) PetscSectionGetDof(section, cStart, &vcdof[1]);
122: }
123: MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
124: if (globalvcdof[0]) {
125: *sStart = vStart;
126: *sEnd = vEnd;
127: if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
128: else *ft = PETSC_VTK_POINT_FIELD;
129: } else if (globalvcdof[1]) {
130: *sStart = cStart;
131: *sEnd = cEnd;
132: if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
133: else *ft = PETSC_VTK_CELL_FIELD;
134: } else {
135: if (field >= 0) {
136: const char *fieldname;
138: PetscSectionGetFieldName(section, field, &fieldname);
139: PetscInfo((PetscObject)dm, "Could not classify VTK output type of section field %" PetscInt_FMT " \"%s\"\n", field, fieldname);
140: } else {
141: PetscInfo((PetscObject)dm, "Could not classify VTK output type of section\n");
142: }
143: }
144: return 0;
145: }
147: /*@
148: DMPlexVecView1D - Plot many 1D solutions on the same line graph
150: Collective on dm
152: Input Parameters:
153: + dm - The `DMPLEX` object
154: . n - The number of vectors
155: . u - The array of local vectors
156: - viewer - The `PetscViewer`
158: Level: advanced
160: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `VecViewFromOptions()`, `VecView()`
161: @*/
162: PetscErrorCode DMPlexVecView1D(DM dm, PetscInt n, Vec u[], PetscViewer viewer)
163: {
164: PetscDS ds;
165: PetscDraw draw = NULL;
166: PetscDrawLG lg;
167: Vec coordinates;
168: const PetscScalar *coords, **sol;
169: PetscReal *vals;
170: PetscInt *Nc;
171: PetscInt Nf, f, c, Nl, l, i, vStart, vEnd, v;
172: char **names;
174: DMGetDS(dm, &ds);
175: PetscDSGetNumFields(ds, &Nf);
176: PetscDSGetTotalComponents(ds, &Nl);
177: PetscDSGetComponents(ds, &Nc);
179: PetscViewerDrawGetDraw(viewer, 0, &draw);
180: if (!draw) return 0;
181: PetscDrawLGCreate(draw, n * Nl, &lg);
183: PetscMalloc3(n, &sol, n * Nl, &names, n * Nl, &vals);
184: for (i = 0, l = 0; i < n; ++i) {
185: const char *vname;
187: PetscObjectGetName((PetscObject)u[i], &vname);
188: for (f = 0; f < Nf; ++f) {
189: PetscObject disc;
190: const char *fname;
191: char tmpname[PETSC_MAX_PATH_LEN];
193: PetscDSGetDiscretization(ds, f, &disc);
194: /* TODO Create names for components */
195: for (c = 0; c < Nc[f]; ++c, ++l) {
196: PetscObjectGetName(disc, &fname);
197: PetscStrcpy(tmpname, vname);
198: PetscStrlcat(tmpname, ":", PETSC_MAX_PATH_LEN);
199: PetscStrlcat(tmpname, fname, PETSC_MAX_PATH_LEN);
200: PetscStrallocpy(tmpname, &names[l]);
201: }
202: }
203: }
204: PetscDrawLGSetLegend(lg, (const char *const *)names);
205: /* Just add P_1 support for now */
206: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
207: DMGetCoordinatesLocal(dm, &coordinates);
208: VecGetArrayRead(coordinates, &coords);
209: for (i = 0; i < n; ++i) VecGetArrayRead(u[i], &sol[i]);
210: for (v = vStart; v < vEnd; ++v) {
211: PetscScalar *x, *svals;
213: DMPlexPointLocalRead(dm, v, coords, &x);
214: for (i = 0; i < n; ++i) {
215: DMPlexPointLocalRead(dm, v, sol[i], &svals);
216: for (l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
217: }
218: PetscDrawLGAddCommonPoint(lg, PetscRealPart(x[0]), vals);
219: }
220: VecRestoreArrayRead(coordinates, &coords);
221: for (i = 0; i < n; ++i) VecRestoreArrayRead(u[i], &sol[i]);
222: for (l = 0; l < n * Nl; ++l) PetscFree(names[l]);
223: PetscFree3(sol, names, vals);
225: PetscDrawLGDraw(lg);
226: PetscDrawLGDestroy(&lg);
227: return 0;
228: }
230: static PetscErrorCode VecView_Plex_Local_Draw_1D(Vec u, PetscViewer viewer)
231: {
232: DM dm;
234: VecGetDM(u, &dm);
235: DMPlexVecView1D(dm, 1, &u, viewer);
236: return 0;
237: }
239: static PetscErrorCode VecView_Plex_Local_Draw_2D(Vec v, PetscViewer viewer)
240: {
241: DM dm;
242: PetscSection s;
243: PetscDraw draw, popup;
244: DM cdm;
245: PetscSection coordSection;
246: Vec coordinates;
247: const PetscScalar *coords, *array;
248: PetscReal bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
249: PetscReal vbound[2], time;
250: PetscBool flg;
251: PetscInt dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
252: const char *name;
253: char title[PETSC_MAX_PATH_LEN];
255: PetscViewerDrawGetDraw(viewer, 0, &draw);
256: VecGetDM(v, &dm);
257: DMGetCoordinateDim(dm, &dim);
258: DMGetLocalSection(dm, &s);
259: PetscSectionGetNumFields(s, &Nf);
260: DMGetCoarsenLevel(dm, &level);
261: DMGetCoordinateDM(dm, &cdm);
262: DMGetLocalSection(cdm, &coordSection);
263: DMGetCoordinatesLocal(dm, &coordinates);
264: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
265: DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
267: PetscObjectGetName((PetscObject)v, &name);
268: DMGetOutputSequenceNumber(dm, &step, &time);
270: VecGetLocalSize(coordinates, &N);
271: VecGetArrayRead(coordinates, &coords);
272: for (c = 0; c < N; c += dim) {
273: bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));
274: bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
275: bound[1] = PetscMin(bound[1], PetscRealPart(coords[c + 1]));
276: bound[3] = PetscMax(bound[3], PetscRealPart(coords[c + 1]));
277: }
278: VecRestoreArrayRead(coordinates, &coords);
279: PetscDrawClear(draw);
281: /* Could implement something like DMDASelectFields() */
282: for (f = 0; f < Nf; ++f) {
283: DM fdm = dm;
284: Vec fv = v;
285: IS fis;
286: char prefix[PETSC_MAX_PATH_LEN];
287: const char *fname;
289: PetscSectionGetFieldComponents(s, f, &Nc);
290: PetscSectionGetFieldName(s, f, &fname);
292: if (v->hdr.prefix) PetscStrncpy(prefix, v->hdr.prefix, sizeof(prefix));
293: else prefix[0] = '\0';
294: if (Nf > 1) {
295: DMCreateSubDM(dm, 1, &f, &fis, &fdm);
296: VecGetSubVector(v, fis, &fv);
297: PetscStrlcat(prefix, fname, sizeof(prefix));
298: PetscStrlcat(prefix, "_", sizeof(prefix));
299: }
300: for (comp = 0; comp < Nc; ++comp, ++w) {
301: PetscInt nmax = 2;
303: PetscViewerDrawGetDraw(viewer, w, &draw);
304: if (Nc > 1) PetscSNPrintf(title, sizeof(title), "%s:%s_%" PetscInt_FMT " Step: %" PetscInt_FMT " Time: %.4g", name, fname, comp, step, (double)time);
305: else PetscSNPrintf(title, sizeof(title), "%s:%s Step: %" PetscInt_FMT " Time: %.4g", name, fname, step, (double)time);
306: PetscDrawSetTitle(draw, title);
308: /* TODO Get max and min only for this component */
309: PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);
310: if (!flg) {
311: VecMin(fv, NULL, &vbound[0]);
312: VecMax(fv, NULL, &vbound[1]);
313: if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
314: }
315: PetscDrawGetPopup(draw, &popup);
316: PetscDrawScalePopup(popup, vbound[0], vbound[1]);
317: PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);
319: VecGetArrayRead(fv, &array);
320: for (c = cStart; c < cEnd; ++c) {
321: PetscScalar *coords = NULL, *a = NULL;
322: PetscInt numCoords, color[4] = {-1, -1, -1, -1};
324: DMPlexPointLocalRead(fdm, c, array, &a);
325: if (a) {
326: color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
327: color[1] = color[2] = color[3] = color[0];
328: } else {
329: PetscScalar *vals = NULL;
330: PetscInt numVals, va;
332: DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);
334: switch (numVals / Nc) {
335: case 3: /* P1 Triangle */
336: case 4: /* P1 Quadrangle */
337: for (va = 0; va < numVals / Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp]), vbound[0], vbound[1]);
338: break;
339: case 6: /* P2 Triangle */
340: case 8: /* P2 Quadrangle */
341: for (va = 0; va < numVals / (Nc * 2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp + numVals / (Nc * 2)]), vbound[0], vbound[1]);
342: break;
343: default:
344: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %" PetscInt_FMT " cannot be handled", numVals / Nc);
345: }
346: DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);
347: }
348: DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
349: switch (numCoords) {
350: case 6:
351: case 12: /* Localized triangle */
352: PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);
353: break;
354: case 8:
355: case 16: /* Localized quadrilateral */
356: PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);
357: PetscDrawTriangle(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), color[2], color[3], color[0]);
358: break;
359: default:
360: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %" PetscInt_FMT " coordinates", numCoords);
361: }
362: DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
363: }
364: VecRestoreArrayRead(fv, &array);
365: PetscDrawFlush(draw);
366: PetscDrawPause(draw);
367: PetscDrawSave(draw);
368: }
369: if (Nf > 1) {
370: VecRestoreSubVector(v, fis, &fv);
371: ISDestroy(&fis);
372: DMDestroy(&fdm);
373: }
374: }
375: return 0;
376: }
378: static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
379: {
380: DM dm;
381: PetscDraw draw;
382: PetscInt dim;
383: PetscBool isnull;
385: PetscViewerDrawGetDraw(viewer, 0, &draw);
386: PetscDrawIsNull(draw, &isnull);
387: if (isnull) return 0;
389: VecGetDM(v, &dm);
390: DMGetCoordinateDim(dm, &dim);
391: switch (dim) {
392: case 1:
393: VecView_Plex_Local_Draw_1D(v, viewer);
394: break;
395: case 2:
396: VecView_Plex_Local_Draw_2D(v, viewer);
397: break;
398: default:
399: SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_SUP, "Cannot draw meshes of dimension %" PetscInt_FMT ". Try PETSCVIEWERGLVIS", dim);
400: }
401: return 0;
402: }
404: static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
405: {
406: DM dm;
407: Vec locv;
408: const char *name;
409: PetscSection section;
410: PetscInt pStart, pEnd;
411: PetscInt numFields;
412: PetscViewerVTKFieldType ft;
414: VecGetDM(v, &dm);
415: DMCreateLocalVector(dm, &locv); /* VTK viewer requires exclusive ownership of the vector */
416: PetscObjectGetName((PetscObject)v, &name);
417: PetscObjectSetName((PetscObject)locv, name);
418: VecCopy(v, locv);
419: DMGetLocalSection(dm, §ion);
420: PetscSectionGetNumFields(section, &numFields);
421: if (!numFields) {
422: DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);
423: PetscViewerVTKAddField(viewer, (PetscObject)dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE, (PetscObject)locv);
424: } else {
425: PetscInt f;
427: for (f = 0; f < numFields; f++) {
428: DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);
429: if (ft == PETSC_VTK_INVALID) continue;
430: PetscObjectReference((PetscObject)locv);
431: PetscViewerVTKAddField(viewer, (PetscObject)dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE, (PetscObject)locv);
432: }
433: VecDestroy(&locv);
434: }
435: return 0;
436: }
438: PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
439: {
440: DM dm;
441: PetscBool isvtk, ishdf5, isdraw, isglvis, iscgns;
443: VecGetDM(v, &dm);
445: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk);
446: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
447: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw);
448: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis);
449: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns);
450: if (isvtk || ishdf5 || isdraw || isglvis || iscgns) {
451: PetscInt i, numFields;
452: PetscObject fe;
453: PetscBool fem = PETSC_FALSE;
454: Vec locv = v;
455: const char *name;
456: PetscInt step;
457: PetscReal time;
459: DMGetNumFields(dm, &numFields);
460: for (i = 0; i < numFields; i++) {
461: DMGetField(dm, i, NULL, &fe);
462: if (fe->classid == PETSCFE_CLASSID) {
463: fem = PETSC_TRUE;
464: break;
465: }
466: }
467: if (fem) {
468: PetscObject isZero;
470: DMGetLocalVector(dm, &locv);
471: PetscObjectGetName((PetscObject)v, &name);
472: PetscObjectSetName((PetscObject)locv, name);
473: PetscObjectQuery((PetscObject)v, "__Vec_bc_zero__", &isZero);
474: PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", isZero);
475: VecCopy(v, locv);
476: DMGetOutputSequenceNumber(dm, NULL, &time);
477: DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);
478: }
479: if (isvtk) {
480: VecView_Plex_Local_VTK(locv, viewer);
481: } else if (ishdf5) {
482: #if defined(PETSC_HAVE_HDF5)
483: VecView_Plex_Local_HDF5_Internal(locv, viewer);
484: #else
485: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
486: #endif
487: } else if (isdraw) {
488: VecView_Plex_Local_Draw(locv, viewer);
489: } else if (isglvis) {
490: DMGetOutputSequenceNumber(dm, &step, NULL);
491: PetscViewerGLVisSetSnapId(viewer, step);
492: VecView_GLVis(locv, viewer);
493: } else if (iscgns) {
494: #if defined(PETSC_HAVE_CGNS)
495: VecView_Plex_Local_CGNS(locv, viewer);
496: #else
497: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "CGNS not supported in this build.\nPlease reconfigure using --download-cgns");
498: #endif
499: }
500: if (fem) {
501: PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", NULL);
502: DMRestoreLocalVector(dm, &locv);
503: }
504: } else {
505: PetscBool isseq;
507: PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq);
508: if (isseq) VecView_Seq(v, viewer);
509: else VecView_MPI(v, viewer);
510: }
511: return 0;
512: }
514: PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
515: {
516: DM dm;
517: PetscBool isvtk, ishdf5, isdraw, isglvis, isexodusii, iscgns;
519: VecGetDM(v, &dm);
521: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk);
522: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
523: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw);
524: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis);
525: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns);
526: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodusii);
527: if (isvtk || isdraw || isglvis || iscgns) {
528: Vec locv;
529: PetscObject isZero;
530: const char *name;
532: DMGetLocalVector(dm, &locv);
533: PetscObjectGetName((PetscObject)v, &name);
534: PetscObjectSetName((PetscObject)locv, name);
535: DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);
536: DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);
537: PetscObjectQuery((PetscObject)v, "__Vec_bc_zero__", &isZero);
538: PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", isZero);
539: VecView_Plex_Local(locv, viewer);
540: PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", NULL);
541: DMRestoreLocalVector(dm, &locv);
542: } else if (ishdf5) {
543: #if defined(PETSC_HAVE_HDF5)
544: VecView_Plex_HDF5_Internal(v, viewer);
545: #else
546: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
547: #endif
548: } else if (isexodusii) {
549: #if defined(PETSC_HAVE_EXODUSII)
550: VecView_PlexExodusII_Internal(v, viewer);
551: #else
552: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
553: #endif
554: } else {
555: PetscBool isseq;
557: PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq);
558: if (isseq) VecView_Seq(v, viewer);
559: else VecView_MPI(v, viewer);
560: }
561: return 0;
562: }
564: PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
565: {
566: DM dm;
567: MPI_Comm comm;
568: PetscViewerFormat format;
569: Vec v;
570: PetscBool isvtk, ishdf5;
572: VecGetDM(originalv, &dm);
573: PetscObjectGetComm((PetscObject)originalv, &comm);
575: PetscViewerGetFormat(viewer, &format);
576: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
577: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk);
578: if (format == PETSC_VIEWER_NATIVE) {
579: /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
580: /* this need a better fix */
581: if (dm->useNatural) {
582: if (dm->sfNatural) {
583: const char *vecname;
584: PetscInt n, nroots;
586: VecGetLocalSize(originalv, &n);
587: PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);
588: if (n == nroots) {
589: DMGetGlobalVector(dm, &v);
590: DMPlexGlobalToNaturalBegin(dm, originalv, v);
591: DMPlexGlobalToNaturalEnd(dm, originalv, v);
592: PetscObjectGetName((PetscObject)originalv, &vecname);
593: PetscObjectSetName((PetscObject)v, vecname);
594: } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
595: } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
596: } else v = originalv;
597: } else v = originalv;
599: if (ishdf5) {
600: #if defined(PETSC_HAVE_HDF5)
601: VecView_Plex_HDF5_Native_Internal(v, viewer);
602: #else
603: SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
604: #endif
605: } else if (isvtk) {
606: SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
607: } else {
608: PetscBool isseq;
610: PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq);
611: if (isseq) VecView_Seq(v, viewer);
612: else VecView_MPI(v, viewer);
613: }
614: if (v != originalv) DMRestoreGlobalVector(dm, &v);
615: return 0;
616: }
618: PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
619: {
620: DM dm;
621: PetscBool ishdf5;
623: VecGetDM(v, &dm);
625: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
626: if (ishdf5) {
627: DM dmBC;
628: Vec gv;
629: const char *name;
631: DMGetOutputDM(dm, &dmBC);
632: DMGetGlobalVector(dmBC, &gv);
633: PetscObjectGetName((PetscObject)v, &name);
634: PetscObjectSetName((PetscObject)gv, name);
635: VecLoad_Default(gv, viewer);
636: DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);
637: DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);
638: DMRestoreGlobalVector(dmBC, &gv);
639: } else VecLoad_Default(v, viewer);
640: return 0;
641: }
643: PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
644: {
645: DM dm;
646: PetscBool ishdf5, isexodusii;
648: VecGetDM(v, &dm);
650: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
651: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodusii);
652: if (ishdf5) {
653: #if defined(PETSC_HAVE_HDF5)
654: VecLoad_Plex_HDF5_Internal(v, viewer);
655: #else
656: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
657: #endif
658: } else if (isexodusii) {
659: #if defined(PETSC_HAVE_EXODUSII)
660: VecLoad_PlexExodusII_Internal(v, viewer);
661: #else
662: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
663: #endif
664: } else VecLoad_Default(v, viewer);
665: return 0;
666: }
668: PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
669: {
670: DM dm;
671: PetscViewerFormat format;
672: PetscBool ishdf5;
674: VecGetDM(originalv, &dm);
676: PetscViewerGetFormat(viewer, &format);
677: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
678: if (format == PETSC_VIEWER_NATIVE) {
679: if (dm->useNatural) {
680: if (dm->sfNatural) {
681: if (ishdf5) {
682: #if defined(PETSC_HAVE_HDF5)
683: Vec v;
684: const char *vecname;
686: DMGetGlobalVector(dm, &v);
687: PetscObjectGetName((PetscObject)originalv, &vecname);
688: PetscObjectSetName((PetscObject)v, vecname);
689: VecLoad_Plex_HDF5_Native_Internal(v, viewer);
690: DMPlexNaturalToGlobalBegin(dm, v, originalv);
691: DMPlexNaturalToGlobalEnd(dm, v, originalv);
692: DMRestoreGlobalVector(dm, &v);
693: #else
694: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
695: #endif
696: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
697: }
698: } else VecLoad_Default(originalv, viewer);
699: }
700: return 0;
701: }
703: PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
704: {
705: PetscSection coordSection;
706: Vec coordinates;
707: DMLabel depthLabel, celltypeLabel;
708: const char *name[4];
709: const PetscScalar *a;
710: PetscInt dim, pStart, pEnd, cStart, cEnd, c;
712: DMGetDimension(dm, &dim);
713: DMGetCoordinatesLocal(dm, &coordinates);
714: DMGetCoordinateSection(dm, &coordSection);
715: DMPlexGetDepthLabel(dm, &depthLabel);
716: DMPlexGetCellTypeLabel(dm, &celltypeLabel);
717: DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
718: PetscSectionGetChart(coordSection, &pStart, &pEnd);
719: VecGetArrayRead(coordinates, &a);
720: name[0] = "vertex";
721: name[1] = "edge";
722: name[dim - 1] = "face";
723: name[dim] = "cell";
724: for (c = cStart; c < cEnd; ++c) {
725: PetscInt *closure = NULL;
726: PetscInt closureSize, cl, ct;
728: DMLabelGetValue(celltypeLabel, c, &ct);
729: PetscViewerASCIIPrintf(viewer, "Geometry for cell %" PetscInt_FMT " polytope type %s:\n", c, DMPolytopeTypes[ct]);
730: DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
731: PetscViewerASCIIPushTab(viewer);
732: for (cl = 0; cl < closureSize * 2; cl += 2) {
733: PetscInt point = closure[cl], depth, dof, off, d, p;
735: if ((point < pStart) || (point >= pEnd)) continue;
736: PetscSectionGetDof(coordSection, point, &dof);
737: if (!dof) continue;
738: DMLabelGetValue(depthLabel, point, &depth);
739: PetscSectionGetOffset(coordSection, point, &off);
740: PetscViewerASCIIPrintf(viewer, "%s %" PetscInt_FMT " coords:", name[depth], point);
741: for (p = 0; p < dof / dim; ++p) {
742: PetscViewerASCIIPrintf(viewer, " (");
743: for (d = 0; d < dim; ++d) {
744: if (d > 0) PetscViewerASCIIPrintf(viewer, ", ");
745: PetscViewerASCIIPrintf(viewer, "%g", (double)PetscRealPart(a[off + p * dim + d]));
746: }
747: PetscViewerASCIIPrintf(viewer, ")");
748: }
749: PetscViewerASCIIPrintf(viewer, "\n");
750: }
751: DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
752: PetscViewerASCIIPopTab(viewer);
753: }
754: VecRestoreArrayRead(coordinates, &a);
755: return 0;
756: }
758: typedef enum {
759: CS_CARTESIAN,
760: CS_POLAR,
761: CS_CYLINDRICAL,
762: CS_SPHERICAL
763: } CoordSystem;
764: const char *CoordSystems[] = {"cartesian", "polar", "cylindrical", "spherical", "CoordSystem", "CS_", NULL};
766: static PetscErrorCode DMPlexView_Ascii_Coordinates(PetscViewer viewer, CoordSystem cs, PetscInt dim, const PetscScalar x[])
767: {
768: PetscInt i;
770: if (dim > 3) {
771: for (i = 0; i < dim; ++i) PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)PetscRealPart(x[i]));
772: } else {
773: PetscReal coords[3], trcoords[3] = {0., 0., 0.};
775: for (i = 0; i < dim; ++i) coords[i] = PetscRealPart(x[i]);
776: switch (cs) {
777: case CS_CARTESIAN:
778: for (i = 0; i < dim; ++i) trcoords[i] = coords[i];
779: break;
780: case CS_POLAR:
782: trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
783: trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
784: break;
785: case CS_CYLINDRICAL:
787: trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
788: trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
789: trcoords[2] = coords[2];
790: break;
791: case CS_SPHERICAL:
793: trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]) + PetscSqr(coords[2]));
794: trcoords[1] = PetscAtan2Real(PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1])), coords[2]);
795: trcoords[2] = PetscAtan2Real(coords[1], coords[0]);
796: break;
797: }
798: for (i = 0; i < dim; ++i) PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)trcoords[i]);
799: }
800: return 0;
801: }
803: static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
804: {
805: DM_Plex *mesh = (DM_Plex *)dm->data;
806: DM cdm, cdmCell;
807: PetscSection coordSection, coordSectionCell;
808: Vec coordinates, coordinatesCell;
809: PetscViewerFormat format;
811: DMGetCoordinateDM(dm, &cdm);
812: DMGetCoordinateSection(dm, &coordSection);
813: DMGetCoordinatesLocal(dm, &coordinates);
814: DMGetCellCoordinateDM(dm, &cdmCell);
815: DMGetCellCoordinateSection(dm, &coordSectionCell);
816: DMGetCellCoordinatesLocal(dm, &coordinatesCell);
817: PetscViewerGetFormat(viewer, &format);
818: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
819: const char *name;
820: PetscInt dim, cellHeight, maxConeSize, maxSupportSize;
821: PetscInt pStart, pEnd, p, numLabels, l;
822: PetscMPIInt rank, size;
824: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
825: MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
826: PetscObjectGetName((PetscObject)dm, &name);
827: DMPlexGetChart(dm, &pStart, &pEnd);
828: DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
829: DMGetDimension(dm, &dim);
830: DMPlexGetVTKCellHeight(dm, &cellHeight);
831: if (name) PetscViewerASCIIPrintf(viewer, "%s in %" PetscInt_FMT " dimension%s:\n", name, dim, dim == 1 ? "" : "s");
832: else PetscViewerASCIIPrintf(viewer, "Mesh in %" PetscInt_FMT " dimension%s:\n", dim, dim == 1 ? "" : "s");
833: if (cellHeight) PetscViewerASCIIPrintf(viewer, " Cells are at height %" PetscInt_FMT "\n", cellHeight);
834: PetscViewerASCIIPrintf(viewer, "Supports:\n");
835: PetscViewerASCIIPushSynchronized(viewer);
836: PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %" PetscInt_FMT "\n", rank, maxSupportSize);
837: for (p = pStart; p < pEnd; ++p) {
838: PetscInt dof, off, s;
840: PetscSectionGetDof(mesh->supportSection, p, &dof);
841: PetscSectionGetOffset(mesh->supportSection, p, &off);
842: for (s = off; s < off + dof; ++s) PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %" PetscInt_FMT " ----> %" PetscInt_FMT "\n", rank, p, mesh->supports[s]);
843: }
844: PetscViewerFlush(viewer);
845: PetscViewerASCIIPrintf(viewer, "Cones:\n");
846: PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %" PetscInt_FMT "\n", rank, maxConeSize);
847: for (p = pStart; p < pEnd; ++p) {
848: PetscInt dof, off, c;
850: PetscSectionGetDof(mesh->coneSection, p, &dof);
851: PetscSectionGetOffset(mesh->coneSection, p, &off);
852: for (c = off; c < off + dof; ++c) PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %" PetscInt_FMT " <---- %" PetscInt_FMT " (%" PetscInt_FMT ")\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);
853: }
854: PetscViewerFlush(viewer);
855: PetscViewerASCIIPopSynchronized(viewer);
856: if (coordSection && coordinates) {
857: CoordSystem cs = CS_CARTESIAN;
858: const PetscScalar *array, *arrayCell = NULL;
859: PetscInt Nf, Nc, pvStart, pvEnd, pcStart = PETSC_MAX_INT, pcEnd = PETSC_MIN_INT, pStart, pEnd, p;
860: PetscMPIInt rank;
861: const char *name;
863: PetscOptionsGetEnum(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_coord_system", CoordSystems, (PetscEnum *)&cs, NULL);
864: MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);
865: PetscSectionGetNumFields(coordSection, &Nf);
867: PetscSectionGetFieldComponents(coordSection, 0, &Nc);
868: PetscSectionGetChart(coordSection, &pvStart, &pvEnd);
869: if (coordSectionCell) PetscSectionGetChart(coordSectionCell, &pcStart, &pcEnd);
870: pStart = PetscMin(pvStart, pcStart);
871: pEnd = PetscMax(pvEnd, pcEnd);
872: PetscObjectGetName((PetscObject)coordinates, &name);
873: PetscViewerASCIIPrintf(viewer, "%s with %" PetscInt_FMT " fields\n", name, Nf);
874: PetscViewerASCIIPrintf(viewer, " field 0 with %" PetscInt_FMT " components\n", Nc);
875: if (cs != CS_CARTESIAN) PetscViewerASCIIPrintf(viewer, " output coordinate system: %s\n", CoordSystems[cs]);
877: VecGetArrayRead(coordinates, &array);
878: if (coordinatesCell) VecGetArrayRead(coordinatesCell, &arrayCell);
879: PetscViewerASCIIPushSynchronized(viewer);
880: PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank);
881: for (p = pStart; p < pEnd; ++p) {
882: PetscInt dof, off;
884: if (p >= pvStart && p < pvEnd) {
885: PetscSectionGetDof(coordSection, p, &dof);
886: PetscSectionGetOffset(coordSection, p, &off);
887: if (dof) {
888: PetscViewerASCIISynchronizedPrintf(viewer, " (%4" PetscInt_FMT ") dim %2" PetscInt_FMT " offset %3" PetscInt_FMT, p, dof, off);
889: DMPlexView_Ascii_Coordinates(viewer, cs, dof, &array[off]);
890: PetscViewerASCIISynchronizedPrintf(viewer, "\n");
891: }
892: }
893: if (cdmCell && p >= pcStart && p < pcEnd) {
894: PetscSectionGetDof(coordSectionCell, p, &dof);
895: PetscSectionGetOffset(coordSectionCell, p, &off);
896: if (dof) {
897: PetscViewerASCIISynchronizedPrintf(viewer, " (%4" PetscInt_FMT ") dim %2" PetscInt_FMT " offset %3" PetscInt_FMT, p, dof, off);
898: DMPlexView_Ascii_Coordinates(viewer, cs, dof, &arrayCell[off]);
899: PetscViewerASCIISynchronizedPrintf(viewer, "\n");
900: }
901: }
902: }
903: PetscViewerFlush(viewer);
904: PetscViewerASCIIPopSynchronized(viewer);
905: VecRestoreArrayRead(coordinates, &array);
906: if (coordinatesCell) VecRestoreArrayRead(coordinatesCell, &arrayCell);
907: }
908: DMGetNumLabels(dm, &numLabels);
909: if (numLabels) PetscViewerASCIIPrintf(viewer, "Labels:\n");
910: for (l = 0; l < numLabels; ++l) {
911: DMLabel label;
912: PetscBool isdepth;
913: const char *name;
915: DMGetLabelName(dm, l, &name);
916: PetscStrcmp(name, "depth", &isdepth);
917: if (isdepth) continue;
918: DMGetLabel(dm, name, &label);
919: DMLabelView(label, viewer);
920: }
921: if (size > 1) {
922: PetscSF sf;
924: DMGetPointSF(dm, &sf);
925: PetscSFView(sf, viewer);
926: }
927: PetscViewerFlush(viewer);
928: } else if (format == PETSC_VIEWER_ASCII_LATEX) {
929: const char *name, *color;
930: const char *defcolors[3] = {"gray", "orange", "green"};
931: const char *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
932: char lname[PETSC_MAX_PATH_LEN];
933: PetscReal scale = 2.0;
934: PetscReal tikzscale = 1.0;
935: PetscBool useNumbers = PETSC_TRUE, drawNumbers[4], drawColors[4], useLabels, useColors, plotEdges, drawHasse = PETSC_FALSE;
936: double tcoords[3];
937: PetscScalar *coords;
938: PetscInt numLabels, l, numColors, numLColors, dim, d, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p, n;
939: PetscMPIInt rank, size;
940: char **names, **colors, **lcolors;
941: PetscBool flg, lflg;
942: PetscBT wp = NULL;
943: PetscInt pEnd, pStart;
945: DMGetDimension(dm, &dim);
946: DMPlexGetDepth(dm, &depth);
947: DMGetNumLabels(dm, &numLabels);
948: numLabels = PetscMax(numLabels, 10);
949: numColors = 10;
950: numLColors = 10;
951: PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);
952: PetscOptionsGetReal(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);
953: PetscOptionsGetReal(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);
954: PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);
955: for (d = 0; d < 4; ++d) drawNumbers[d] = useNumbers;
956: for (d = 0; d < 4; ++d) drawColors[d] = PETSC_TRUE;
957: n = 4;
958: PetscOptionsGetBoolArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_numbers_depth", drawNumbers, &n, &flg);
960: PetscOptionsGetBoolArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_colors_depth", drawColors, &n, &flg);
962: PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);
963: if (!useLabels) numLabels = 0;
964: PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);
965: if (!useColors) {
966: numColors = 3;
967: for (c = 0; c < numColors; ++c) PetscStrallocpy(defcolors[c], &colors[c]);
968: }
969: PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);
970: if (!useColors) {
971: numLColors = 4;
972: for (c = 0; c < numLColors; ++c) PetscStrallocpy(deflcolors[c], &lcolors[c]);
973: }
974: PetscOptionsGetString(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg);
975: plotEdges = (PetscBool)(depth > 1 && drawNumbers[1] && dim < 3);
976: PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);
978: if (depth < dim) plotEdges = PETSC_FALSE;
979: PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_hasse", &drawHasse, NULL);
981: /* filter points with labelvalue != labeldefaultvalue */
982: DMPlexGetChart(dm, &pStart, &pEnd);
983: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
984: DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);
985: DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
986: if (lflg) {
987: DMLabel lbl;
989: DMGetLabel(dm, lname, &lbl);
990: if (lbl) {
991: PetscInt val, defval;
993: DMLabelGetDefaultValue(lbl, &defval);
994: PetscBTCreate(pEnd - pStart, &wp);
995: for (c = pStart; c < pEnd; c++) {
996: PetscInt *closure = NULL;
997: PetscInt closureSize;
999: DMLabelGetValue(lbl, c, &val);
1000: if (val == defval) continue;
1002: DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1003: for (p = 0; p < closureSize * 2; p += 2) PetscBTSet(wp, closure[p] - pStart);
1004: DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1005: }
1006: }
1007: }
1009: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
1010: MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
1011: PetscObjectGetName((PetscObject)dm, &name);
1012: PetscCall(PetscViewerASCIIPrintf(viewer, "\
1013: \\documentclass[tikz]{standalone}\n\n\
1014: \\usepackage{pgflibraryshapes}\n\
1015: \\usetikzlibrary{backgrounds}\n\
1016: \\usetikzlibrary{arrows}\n\
1017: \\begin{document}\n"));
1018: if (size > 1) {
1019: PetscViewerASCIIPrintf(viewer, "%s for process ", name);
1020: for (p = 0; p < size; ++p) {
1021: if (p) PetscViewerASCIIPrintf(viewer, (p == size - 1) ? ", and " : ", ");
1022: PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%" PetscInt_FMT "}", colors[p % numColors], p);
1023: }
1024: PetscViewerASCIIPrintf(viewer, ".\n\n\n");
1025: }
1026: if (drawHasse) {
1027: PetscInt maxStratum = PetscMax(vEnd - vStart, PetscMax(eEnd - eStart, cEnd - cStart));
1029: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vStart}{%" PetscInt_FMT "}\n", vStart);
1030: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vEnd}{%" PetscInt_FMT "}\n", vEnd - 1);
1031: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numVertices}{%" PetscInt_FMT "}\n", vEnd - vStart);
1032: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vShift}{%.2f}\n", 3 + (maxStratum - (vEnd - vStart)) / 2.);
1033: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eStart}{%" PetscInt_FMT "}\n", eStart);
1034: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eEnd}{%" PetscInt_FMT "}\n", eEnd - 1);
1035: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eShift}{%.2f}\n", 3 + (maxStratum - (eEnd - eStart)) / 2.);
1036: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numEdges}{%" PetscInt_FMT "}\n", eEnd - eStart);
1037: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cStart}{%" PetscInt_FMT "}\n", cStart);
1038: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cEnd}{%" PetscInt_FMT "}\n", cEnd - 1);
1039: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numCells}{%" PetscInt_FMT "}\n", cEnd - cStart);
1040: PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cShift}{%.2f}\n", 3 + (maxStratum - (cEnd - cStart)) / 2.);
1041: }
1042: PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double)tikzscale);
1044: /* Plot vertices */
1045: VecGetArray(coordinates, &coords);
1046: PetscViewerASCIIPushSynchronized(viewer);
1047: for (v = vStart; v < vEnd; ++v) {
1048: PetscInt off, dof, d;
1049: PetscBool isLabeled = PETSC_FALSE;
1051: if (wp && !PetscBTLookup(wp, v - pStart)) continue;
1052: PetscSectionGetDof(coordSection, v, &dof);
1053: PetscSectionGetOffset(coordSection, v, &off);
1054: PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");
1056: for (d = 0; d < dof; ++d) {
1057: tcoords[d] = (double)(scale * PetscRealPart(coords[off + d]));
1058: tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1059: }
1060: /* Rotate coordinates since PGF makes z point out of the page instead of up */
1061: if (dim == 3) {
1062: PetscReal tmp = tcoords[1];
1063: tcoords[1] = tcoords[2];
1064: tcoords[2] = -tmp;
1065: }
1066: for (d = 0; d < dof; ++d) {
1067: if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ",");
1068: PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);
1069: }
1070: if (drawHasse) color = colors[0 % numColors];
1071: else color = colors[rank % numColors];
1072: for (l = 0; l < numLabels; ++l) {
1073: PetscInt val;
1074: DMGetLabelValue(dm, names[l], v, &val);
1075: if (val >= 0) {
1076: color = lcolors[l % numLColors];
1077: isLabeled = PETSC_TRUE;
1078: break;
1079: }
1080: }
1081: if (drawNumbers[0]) {
1082: PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "};\n", v, rank, color, v);
1083: } else if (drawColors[0]) {
1084: PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);
1085: } else PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [] {};\n", v, rank);
1086: }
1087: VecRestoreArray(coordinates, &coords);
1088: PetscViewerFlush(viewer);
1089: /* Plot edges */
1090: if (plotEdges) {
1091: VecGetArray(coordinates, &coords);
1092: PetscViewerASCIIPrintf(viewer, "\\path\n");
1093: for (e = eStart; e < eEnd; ++e) {
1094: const PetscInt *cone;
1095: PetscInt coneSize, offA, offB, dof, d;
1097: if (wp && !PetscBTLookup(wp, e - pStart)) continue;
1098: DMPlexGetConeSize(dm, e, &coneSize);
1100: DMPlexGetCone(dm, e, &cone);
1101: PetscSectionGetDof(coordSection, cone[0], &dof);
1102: PetscSectionGetOffset(coordSection, cone[0], &offA);
1103: PetscSectionGetOffset(coordSection, cone[1], &offB);
1104: PetscViewerASCIISynchronizedPrintf(viewer, "(");
1105: for (d = 0; d < dof; ++d) {
1106: tcoords[d] = (double)(0.5 * scale * PetscRealPart(coords[offA + d] + coords[offB + d]));
1107: tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1108: }
1109: /* Rotate coordinates since PGF makes z point out of the page instead of up */
1110: if (dim == 3) {
1111: PetscReal tmp = tcoords[1];
1112: tcoords[1] = tcoords[2];
1113: tcoords[2] = -tmp;
1114: }
1115: for (d = 0; d < dof; ++d) {
1116: if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ",");
1117: PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);
1118: }
1119: if (drawHasse) color = colors[1 % numColors];
1120: else color = colors[rank % numColors];
1121: for (l = 0; l < numLabels; ++l) {
1122: PetscInt val;
1123: DMGetLabelValue(dm, names[l], v, &val);
1124: if (val >= 0) {
1125: color = lcolors[l % numLColors];
1126: break;
1127: }
1128: }
1129: PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "} --\n", e, rank, color, e);
1130: }
1131: VecRestoreArray(coordinates, &coords);
1132: PetscViewerFlush(viewer);
1133: PetscViewerASCIIPrintf(viewer, "(0,0);\n");
1134: }
1135: /* Plot cells */
1136: if (dim == 3 || !drawNumbers[1]) {
1137: for (e = eStart; e < eEnd; ++e) {
1138: const PetscInt *cone;
1140: if (wp && !PetscBTLookup(wp, e - pStart)) continue;
1141: color = colors[rank % numColors];
1142: for (l = 0; l < numLabels; ++l) {
1143: PetscInt val;
1144: DMGetLabelValue(dm, names[l], e, &val);
1145: if (val >= 0) {
1146: color = lcolors[l % numLColors];
1147: break;
1148: }
1149: }
1150: DMPlexGetCone(dm, e, &cone);
1151: PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", color, cone[0], rank, cone[1], rank);
1152: }
1153: } else {
1154: DMPolytopeType ct;
1156: /* Drawing a 2D polygon */
1157: for (c = cStart; c < cEnd; ++c) {
1158: if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1159: DMPlexGetCellType(dm, c, &ct);
1160: if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR || ct == DM_POLYTOPE_TRI_PRISM_TENSOR || ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) {
1161: const PetscInt *cone;
1162: PetscInt coneSize, e;
1164: DMPlexGetCone(dm, c, &cone);
1165: DMPlexGetConeSize(dm, c, &coneSize);
1166: for (e = 0; e < coneSize; ++e) {
1167: const PetscInt *econe;
1169: DMPlexGetCone(dm, cone[e], &econe);
1170: PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", colors[rank % numColors], econe[0], rank, cone[e], rank, econe[1], rank);
1171: }
1172: } else {
1173: PetscInt *closure = NULL;
1174: PetscInt closureSize, Nv = 0, v;
1176: DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1177: for (p = 0; p < closureSize * 2; p += 2) {
1178: const PetscInt point = closure[p];
1180: if ((point >= vStart) && (point < vEnd)) closure[Nv++] = point;
1181: }
1182: PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank % numColors]);
1183: for (v = 0; v <= Nv; ++v) {
1184: const PetscInt vertex = closure[v % Nv];
1186: if (v > 0) {
1187: if (plotEdges) {
1188: const PetscInt *edge;
1189: PetscInt endpoints[2], ne;
1191: endpoints[0] = closure[v - 1];
1192: endpoints[1] = vertex;
1193: DMPlexGetJoin(dm, 2, endpoints, &ne, &edge);
1195: PetscViewerASCIISynchronizedPrintf(viewer, " -- (%" PetscInt_FMT "_%d) -- ", edge[0], rank);
1196: DMPlexRestoreJoin(dm, 2, endpoints, &ne, &edge);
1197: } else PetscViewerASCIISynchronizedPrintf(viewer, " -- ");
1198: }
1199: PetscViewerASCIISynchronizedPrintf(viewer, "(%" PetscInt_FMT "_%d)", vertex, rank);
1200: }
1201: PetscViewerASCIISynchronizedPrintf(viewer, ";\n");
1202: DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1203: }
1204: }
1205: }
1206: for (c = cStart; c < cEnd; ++c) {
1207: double ccoords[3] = {0.0, 0.0, 0.0};
1208: PetscBool isLabeled = PETSC_FALSE;
1209: PetscScalar *cellCoords = NULL;
1210: const PetscScalar *array;
1211: PetscInt numCoords, cdim, d;
1212: PetscBool isDG;
1214: if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1215: DMGetCoordinateDim(dm, &cdim);
1216: DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &cellCoords);
1218: PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");
1219: for (p = 0; p < numCoords / cdim; ++p) {
1220: for (d = 0; d < cdim; ++d) {
1221: tcoords[d] = (double)(scale * PetscRealPart(cellCoords[p * cdim + d]));
1222: tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1223: }
1224: /* Rotate coordinates since PGF makes z point out of the page instead of up */
1225: if (cdim == 3) {
1226: PetscReal tmp = tcoords[1];
1227: tcoords[1] = tcoords[2];
1228: tcoords[2] = -tmp;
1229: }
1230: for (d = 0; d < dim; ++d) ccoords[d] += tcoords[d];
1231: }
1232: for (d = 0; d < cdim; ++d) ccoords[d] /= (numCoords / cdim);
1233: DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &cellCoords);
1234: for (d = 0; d < cdim; ++d) {
1235: if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ",");
1236: PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)ccoords[d]);
1237: }
1238: if (drawHasse) color = colors[depth % numColors];
1239: else color = colors[rank % numColors];
1240: for (l = 0; l < numLabels; ++l) {
1241: PetscInt val;
1242: DMGetLabelValue(dm, names[l], c, &val);
1243: if (val >= 0) {
1244: color = lcolors[l % numLColors];
1245: isLabeled = PETSC_TRUE;
1246: break;
1247: }
1248: }
1249: if (drawNumbers[dim]) {
1250: PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "};\n", c, rank, color, c);
1251: } else if (drawColors[dim]) {
1252: PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", c, rank, !isLabeled ? 1 : 2, color);
1253: } else PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [] {};\n", c, rank);
1254: }
1255: if (drawHasse) {
1256: color = colors[depth % numColors];
1257: PetscViewerASCIIPrintf(viewer, "%% Cells\n");
1258: PetscViewerASCIIPrintf(viewer, "\\foreach \\c in {\\cStart,...,\\cEnd}\n");
1259: PetscViewerASCIIPrintf(viewer, "{\n");
1260: PetscViewerASCIIPrintf(viewer, " \\node(\\c_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\cShift+\\c-\\cStart,0) {\\c};\n", rank, color);
1261: PetscViewerASCIIPrintf(viewer, "}\n");
1263: color = colors[1 % numColors];
1264: PetscViewerASCIIPrintf(viewer, "%% Edges\n");
1265: PetscViewerASCIIPrintf(viewer, "\\foreach \\e in {\\eStart,...,\\eEnd}\n");
1266: PetscViewerASCIIPrintf(viewer, "{\n");
1267: PetscViewerASCIIPrintf(viewer, " \\node(\\e_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\eShift+\\e-\\eStart,1) {\\e};\n", rank, color);
1268: PetscViewerASCIIPrintf(viewer, "}\n");
1270: color = colors[0 % numColors];
1271: PetscViewerASCIIPrintf(viewer, "%% Vertices\n");
1272: PetscViewerASCIIPrintf(viewer, "\\foreach \\v in {\\vStart,...,\\vEnd}\n");
1273: PetscViewerASCIIPrintf(viewer, "{\n");
1274: PetscViewerASCIIPrintf(viewer, " \\node(\\v_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\vShift+\\v-\\vStart,2) {\\v};\n", rank, color);
1275: PetscViewerASCIIPrintf(viewer, "}\n");
1277: for (p = pStart; p < pEnd; ++p) {
1278: const PetscInt *cone;
1279: PetscInt coneSize, cp;
1281: DMPlexGetCone(dm, p, &cone);
1282: DMPlexGetConeSize(dm, p, &coneSize);
1283: for (cp = 0; cp < coneSize; ++cp) PetscViewerASCIIPrintf(viewer, "\\draw[->, shorten >=1pt] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", cone[cp], rank, p, rank);
1284: }
1285: }
1286: PetscViewerFlush(viewer);
1287: PetscViewerASCIIPopSynchronized(viewer);
1288: PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");
1289: PetscViewerASCIIPrintf(viewer, "\\end{document}\n");
1290: for (l = 0; l < numLabels; ++l) PetscFree(names[l]);
1291: for (c = 0; c < numColors; ++c) PetscFree(colors[c]);
1292: for (c = 0; c < numLColors; ++c) PetscFree(lcolors[c]);
1293: PetscFree3(names, colors, lcolors);
1294: PetscBTDestroy(&wp);
1295: } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
1296: Vec cown, acown;
1297: VecScatter sct;
1298: ISLocalToGlobalMapping g2l;
1299: IS gid, acis;
1300: MPI_Comm comm, ncomm = MPI_COMM_NULL;
1301: MPI_Group ggroup, ngroup;
1302: PetscScalar *array, nid;
1303: const PetscInt *idxs;
1304: PetscInt *idxs2, *start, *adjacency, *work;
1305: PetscInt64 lm[3], gm[3];
1306: PetscInt i, c, cStart, cEnd, cum, numVertices, ect, ectn, cellHeight;
1307: PetscMPIInt d1, d2, rank;
1309: PetscObjectGetComm((PetscObject)dm, &comm);
1310: MPI_Comm_rank(comm, &rank);
1311: #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1312: MPI_Comm_split_type(comm, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &ncomm);
1313: #endif
1314: if (ncomm != MPI_COMM_NULL) {
1315: MPI_Comm_group(comm, &ggroup);
1316: MPI_Comm_group(ncomm, &ngroup);
1317: d1 = 0;
1318: MPI_Group_translate_ranks(ngroup, 1, &d1, ggroup, &d2);
1319: nid = d2;
1320: MPI_Group_free(&ggroup);
1321: MPI_Group_free(&ngroup);
1322: MPI_Comm_free(&ncomm);
1323: } else nid = 0.0;
1325: /* Get connectivity */
1326: DMPlexGetVTKCellHeight(dm, &cellHeight);
1327: DMPlexCreatePartitionerGraph(dm, cellHeight, &numVertices, &start, &adjacency, &gid);
1329: /* filter overlapped local cells */
1330: DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
1331: ISGetIndices(gid, &idxs);
1332: ISGetLocalSize(gid, &cum);
1333: PetscMalloc1(cum, &idxs2);
1334: for (c = cStart, cum = 0; c < cEnd; c++) {
1335: if (idxs[c - cStart] < 0) continue;
1336: idxs2[cum++] = idxs[c - cStart];
1337: }
1338: ISRestoreIndices(gid, &idxs);
1340: ISDestroy(&gid);
1341: ISCreateGeneral(comm, numVertices, idxs2, PETSC_OWN_POINTER, &gid);
1343: /* support for node-aware cell locality */
1344: ISCreateGeneral(comm, start[numVertices], adjacency, PETSC_USE_POINTER, &acis);
1345: VecCreateSeq(PETSC_COMM_SELF, start[numVertices], &acown);
1346: VecCreateMPI(comm, numVertices, PETSC_DECIDE, &cown);
1347: VecGetArray(cown, &array);
1348: for (c = 0; c < numVertices; c++) array[c] = nid;
1349: VecRestoreArray(cown, &array);
1350: VecScatterCreate(cown, acis, acown, NULL, &sct);
1351: VecScatterBegin(sct, cown, acown, INSERT_VALUES, SCATTER_FORWARD);
1352: VecScatterEnd(sct, cown, acown, INSERT_VALUES, SCATTER_FORWARD);
1353: ISDestroy(&acis);
1354: VecScatterDestroy(&sct);
1355: VecDestroy(&cown);
1357: /* compute edgeCut */
1358: for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum, start[c + 1] - start[c]);
1359: PetscMalloc1(cum, &work);
1360: ISLocalToGlobalMappingCreateIS(gid, &g2l);
1361: ISLocalToGlobalMappingSetType(g2l, ISLOCALTOGLOBALMAPPINGHASH);
1362: ISDestroy(&gid);
1363: VecGetArray(acown, &array);
1364: for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
1365: PetscInt totl;
1367: totl = start[c + 1] - start[c];
1368: ISGlobalToLocalMappingApply(g2l, IS_GTOLM_MASK, totl, adjacency + start[c], NULL, work);
1369: for (i = 0; i < totl; i++) {
1370: if (work[i] < 0) {
1371: ect += 1;
1372: ectn += (array[i + start[c]] != nid) ? 0 : 1;
1373: }
1374: }
1375: }
1376: PetscFree(work);
1377: VecRestoreArray(acown, &array);
1378: lm[0] = numVertices > 0 ? numVertices : PETSC_MAX_INT;
1379: lm[1] = -numVertices;
1380: MPIU_Allreduce(lm, gm, 2, MPIU_INT64, MPI_MIN, comm);
1381: PetscViewerASCIIPrintf(viewer, " Cell balance: %.2f (max %" PetscInt_FMT ", min %" PetscInt_FMT, -((double)gm[1]) / ((double)gm[0]), -(PetscInt)gm[1], (PetscInt)gm[0]);
1382: lm[0] = ect; /* edgeCut */
1383: lm[1] = ectn; /* node-aware edgeCut */
1384: lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
1385: MPIU_Allreduce(lm, gm, 3, MPIU_INT64, MPI_SUM, comm);
1386: PetscViewerASCIIPrintf(viewer, ", empty %" PetscInt_FMT ")\n", (PetscInt)gm[2]);
1387: #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1388: PetscViewerASCIIPrintf(viewer, " Edge Cut: %" PetscInt_FMT " (on node %.3f)\n", (PetscInt)(gm[0] / 2), gm[0] ? ((double)(gm[1])) / ((double)gm[0]) : 1.);
1389: #else
1390: PetscViewerASCIIPrintf(viewer, " Edge Cut: %" PetscInt_FMT " (on node %.3f)\n", (PetscInt)(gm[0] / 2), 0.0);
1391: #endif
1392: ISLocalToGlobalMappingDestroy(&g2l);
1393: PetscFree(start);
1394: PetscFree(adjacency);
1395: VecDestroy(&acown);
1396: } else {
1397: const char *name;
1398: PetscInt *sizes, *hybsizes, *ghostsizes;
1399: PetscInt locDepth, depth, cellHeight, dim, d;
1400: PetscInt pStart, pEnd, p, gcStart, gcEnd, gcNum;
1401: PetscInt numLabels, l, maxSize = 17;
1402: DMPolytopeType ct0 = DM_POLYTOPE_UNKNOWN;
1403: MPI_Comm comm;
1404: PetscMPIInt size, rank;
1406: PetscObjectGetComm((PetscObject)dm, &comm);
1407: MPI_Comm_size(comm, &size);
1408: MPI_Comm_rank(comm, &rank);
1409: DMGetDimension(dm, &dim);
1410: DMPlexGetVTKCellHeight(dm, &cellHeight);
1411: PetscObjectGetName((PetscObject)dm, &name);
1412: if (name) PetscViewerASCIIPrintf(viewer, "%s in %" PetscInt_FMT " dimension%s:\n", name, dim, dim == 1 ? "" : "s");
1413: else PetscViewerASCIIPrintf(viewer, "Mesh in %" PetscInt_FMT " dimension%s:\n", dim, dim == 1 ? "" : "s");
1414: if (cellHeight) PetscViewerASCIIPrintf(viewer, " Cells are at height %" PetscInt_FMT "\n", cellHeight);
1415: DMPlexGetDepth(dm, &locDepth);
1416: MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);
1417: DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);
1418: gcNum = gcEnd - gcStart;
1419: if (size < maxSize) PetscCalloc3(size, &sizes, size, &hybsizes, size, &ghostsizes);
1420: else PetscCalloc3(3, &sizes, 3, &hybsizes, 3, &ghostsizes);
1421: for (d = 0; d <= depth; d++) {
1422: PetscInt Nc[2] = {0, 0}, ict;
1424: DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
1425: if (pStart < pEnd) DMPlexGetCellType(dm, pStart, &ct0);
1426: ict = ct0;
1427: MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);
1428: ct0 = (DMPolytopeType)ict;
1429: for (p = pStart; p < pEnd; ++p) {
1430: DMPolytopeType ct;
1432: DMPlexGetCellType(dm, p, &ct);
1433: if (ct == ct0) ++Nc[0];
1434: else ++Nc[1];
1435: }
1436: if (size < maxSize) {
1437: MPI_Gather(&Nc[0], 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);
1438: MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);
1439: if (d == depth) MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);
1440: PetscViewerASCIIPrintf(viewer, " Number of %" PetscInt_FMT "-cells per rank:", (depth == 1) && d ? dim : d);
1441: for (p = 0; p < size; ++p) {
1442: if (rank == 0) {
1443: PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, sizes[p] + hybsizes[p]);
1444: if (hybsizes[p] > 0) PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ")", hybsizes[p]);
1445: if (ghostsizes[p] > 0) PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "]", ghostsizes[p]);
1446: }
1447: }
1448: } else {
1449: PetscInt locMinMax[2];
1451: locMinMax[0] = Nc[0] + Nc[1];
1452: locMinMax[1] = Nc[0] + Nc[1];
1453: PetscGlobalMinMaxInt(comm, locMinMax, sizes);
1454: locMinMax[0] = Nc[1];
1455: locMinMax[1] = Nc[1];
1456: PetscGlobalMinMaxInt(comm, locMinMax, hybsizes);
1457: if (d == depth) {
1458: locMinMax[0] = gcNum;
1459: locMinMax[1] = gcNum;
1460: PetscGlobalMinMaxInt(comm, locMinMax, ghostsizes);
1461: }
1462: PetscViewerASCIIPrintf(viewer, " Min/Max of %" PetscInt_FMT "-cells per rank:", (depth == 1) && d ? dim : d);
1463: PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT "/%" PetscInt_FMT, sizes[0], sizes[1]);
1464: if (hybsizes[0] > 0) PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT "/%" PetscInt_FMT ")", hybsizes[0], hybsizes[1]);
1465: if (ghostsizes[0] > 0) PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "/%" PetscInt_FMT "]", ghostsizes[0], ghostsizes[1]);
1466: }
1467: PetscViewerASCIIPrintf(viewer, "\n");
1468: }
1469: PetscFree3(sizes, hybsizes, ghostsizes);
1470: {
1471: const PetscReal *maxCell;
1472: const PetscReal *L;
1473: PetscBool localized;
1475: DMGetPeriodicity(dm, &maxCell, NULL, &L);
1476: DMGetCoordinatesLocalized(dm, &localized);
1477: if (L || localized) {
1478: PetscViewerASCIIPrintf(viewer, "Periodic mesh");
1479: PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
1480: if (L) {
1481: PetscViewerASCIIPrintf(viewer, " (");
1482: for (d = 0; d < dim; ++d) {
1483: if (d > 0) PetscViewerASCIIPrintf(viewer, ", ");
1484: PetscViewerASCIIPrintf(viewer, "%s", L[d] > 0.0 ? "PERIODIC" : "NONE");
1485: }
1486: PetscViewerASCIIPrintf(viewer, ")");
1487: }
1488: PetscViewerASCIIPrintf(viewer, " coordinates %s\n", localized ? "localized" : "not localized");
1489: PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
1490: }
1491: }
1492: DMGetNumLabels(dm, &numLabels);
1493: if (numLabels) PetscViewerASCIIPrintf(viewer, "Labels:\n");
1494: for (l = 0; l < numLabels; ++l) {
1495: DMLabel label;
1496: const char *name;
1497: IS valueIS;
1498: const PetscInt *values;
1499: PetscInt numValues, v;
1501: DMGetLabelName(dm, l, &name);
1502: DMGetLabel(dm, name, &label);
1503: DMLabelGetNumValues(label, &numValues);
1504: PetscViewerASCIIPrintf(viewer, " %s: %" PetscInt_FMT " strata with value/size (", name, numValues);
1505: DMLabelGetValueIS(label, &valueIS);
1506: ISGetIndices(valueIS, &values);
1507: PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
1508: for (v = 0; v < numValues; ++v) {
1509: PetscInt size;
1511: DMLabelGetStratumSize(label, values[v], &size);
1512: if (v > 0) PetscViewerASCIIPrintf(viewer, ", ");
1513: PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " (%" PetscInt_FMT ")", values[v], size);
1514: }
1515: PetscViewerASCIIPrintf(viewer, ")\n");
1516: PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
1517: ISRestoreIndices(valueIS, &values);
1518: ISDestroy(&valueIS);
1519: }
1520: {
1521: char **labelNames;
1522: PetscInt Nl = numLabels;
1523: PetscBool flg;
1525: PetscMalloc1(Nl, &labelNames);
1526: PetscOptionsGetStringArray(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_labels", labelNames, &Nl, &flg);
1527: for (l = 0; l < Nl; ++l) {
1528: DMLabel label;
1530: DMHasLabel(dm, labelNames[l], &flg);
1531: if (flg) {
1532: DMGetLabel(dm, labelNames[l], &label);
1533: DMLabelView(label, viewer);
1534: }
1535: PetscFree(labelNames[l]);
1536: }
1537: PetscFree(labelNames);
1538: }
1539: /* If no fields are specified, people do not want to see adjacency */
1540: if (dm->Nf) {
1541: PetscInt f;
1543: for (f = 0; f < dm->Nf; ++f) {
1544: const char *name;
1546: PetscObjectGetName(dm->fields[f].disc, &name);
1547: if (numLabels) PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);
1548: PetscViewerASCIIPushTab(viewer);
1549: if (dm->fields[f].label) DMLabelView(dm->fields[f].label, viewer);
1550: if (dm->fields[f].adjacency[0]) {
1551: if (dm->fields[f].adjacency[1]) PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");
1552: else PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");
1553: } else {
1554: if (dm->fields[f].adjacency[1]) PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");
1555: else PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");
1556: }
1557: PetscViewerASCIIPopTab(viewer);
1558: }
1559: }
1560: DMGetCoarseDM(dm, &cdm);
1561: if (cdm) {
1562: PetscViewerASCIIPushTab(viewer);
1563: DMPlexView_Ascii(cdm, viewer);
1564: PetscViewerASCIIPopTab(viewer);
1565: }
1566: }
1567: return 0;
1568: }
1570: static PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[])
1571: {
1572: DMPolytopeType ct;
1573: PetscMPIInt rank;
1574: PetscInt cdim;
1576: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
1577: DMPlexGetCellType(dm, cell, &ct);
1578: DMGetCoordinateDim(dm, &cdim);
1579: switch (ct) {
1580: case DM_POLYTOPE_SEGMENT:
1581: case DM_POLYTOPE_POINT_PRISM_TENSOR:
1582: switch (cdim) {
1583: case 1: {
1584: const PetscReal y = 0.5; /* TODO Put it in the middle of the viewport */
1585: const PetscReal dy = 0.05; /* TODO Make it a fraction of the total length */
1587: PetscDrawLine(draw, PetscRealPart(coords[0]), y, PetscRealPart(coords[1]), y, PETSC_DRAW_BLACK);
1588: PetscDrawLine(draw, PetscRealPart(coords[0]), y + dy, PetscRealPart(coords[0]), y - dy, PETSC_DRAW_BLACK);
1589: PetscDrawLine(draw, PetscRealPart(coords[1]), y + dy, PetscRealPart(coords[1]), y - dy, PETSC_DRAW_BLACK);
1590: } break;
1591: case 2: {
1592: const PetscReal dx = (PetscRealPart(coords[3]) - PetscRealPart(coords[1]));
1593: const PetscReal dy = (PetscRealPart(coords[2]) - PetscRealPart(coords[0]));
1594: const PetscReal l = 0.1 / PetscSqrtReal(dx * dx + dy * dy);
1596: PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1597: PetscDrawLine(draw, PetscRealPart(coords[0]) + l * dx, PetscRealPart(coords[1]) + l * dy, PetscRealPart(coords[0]) - l * dx, PetscRealPart(coords[1]) - l * dy, PETSC_DRAW_BLACK);
1598: PetscDrawLine(draw, PetscRealPart(coords[2]) + l * dx, PetscRealPart(coords[3]) + l * dy, PetscRealPart(coords[2]) - l * dx, PetscRealPart(coords[3]) - l * dy, PETSC_DRAW_BLACK);
1599: } break;
1600: default:
1601: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of dimension %" PetscInt_FMT, cdim);
1602: }
1603: break;
1604: case DM_POLYTOPE_TRIANGLE:
1605: PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2);
1606: PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1607: PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1608: PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1609: break;
1610: case DM_POLYTOPE_QUADRILATERAL:
1611: PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2);
1612: PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2);
1613: PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1614: PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1615: PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);
1616: PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1617: break;
1618: case DM_POLYTOPE_FV_GHOST:
1619: break;
1620: default:
1621: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1622: }
1623: return 0;
1624: }
1626: static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
1627: {
1628: DMPolytopeType ct;
1629: PetscReal centroid[2] = {0., 0.};
1630: PetscMPIInt rank;
1631: PetscInt fillColor, v, e, d;
1633: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
1634: DMPlexGetCellType(dm, cell, &ct);
1635: fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2;
1636: switch (ct) {
1637: case DM_POLYTOPE_TRIANGLE: {
1638: PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};
1640: for (v = 0; v < 3; ++v) {
1641: centroid[0] += PetscRealPart(coords[v * 2 + 0]) / 3.;
1642: centroid[1] += PetscRealPart(coords[v * 2 + 1]) / 3.;
1643: }
1644: for (e = 0; e < 3; ++e) {
1645: refCoords[0] = refVertices[e * 2 + 0];
1646: refCoords[1] = refVertices[e * 2 + 1];
1647: for (d = 1; d <= edgeDiv; ++d) {
1648: refCoords[d * 2 + 0] = refCoords[0] + (refVertices[(e + 1) % 3 * 2 + 0] - refCoords[0]) * d / edgeDiv;
1649: refCoords[d * 2 + 1] = refCoords[1] + (refVertices[(e + 1) % 3 * 2 + 1] - refCoords[1]) * d / edgeDiv;
1650: }
1651: DMPlexReferenceToCoordinates(dm, cell, edgeDiv + 1, refCoords, edgeCoords);
1652: for (d = 0; d < edgeDiv; ++d) {
1653: PetscDrawTriangle(draw, centroid[0], centroid[1], edgeCoords[d * 2 + 0], edgeCoords[d * 2 + 1], edgeCoords[(d + 1) * 2 + 0], edgeCoords[(d + 1) * 2 + 1], fillColor, fillColor, fillColor);
1654: PetscDrawLine(draw, edgeCoords[d * 2 + 0], edgeCoords[d * 2 + 1], edgeCoords[(d + 1) * 2 + 0], edgeCoords[(d + 1) * 2 + 1], PETSC_DRAW_BLACK);
1655: }
1656: }
1657: } break;
1658: default:
1659: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1660: }
1661: return 0;
1662: }
1664: static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1665: {
1666: PetscDraw draw;
1667: DM cdm;
1668: PetscSection coordSection;
1669: Vec coordinates;
1670: const PetscScalar *coords;
1671: PetscReal xyl[2], xyr[2], bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1672: PetscReal *refCoords, *edgeCoords;
1673: PetscBool isnull, drawAffine = PETSC_TRUE;
1674: PetscInt dim, vStart, vEnd, cStart, cEnd, c, N, edgeDiv = 4;
1676: DMGetCoordinateDim(dm, &dim);
1678: PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL);
1679: if (!drawAffine) PetscMalloc2((edgeDiv + 1) * dim, &refCoords, (edgeDiv + 1) * dim, &edgeCoords);
1680: DMGetCoordinateDM(dm, &cdm);
1681: DMGetLocalSection(cdm, &coordSection);
1682: DMGetCoordinatesLocal(dm, &coordinates);
1683: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
1684: DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
1686: PetscViewerDrawGetDraw(viewer, 0, &draw);
1687: PetscDrawIsNull(draw, &isnull);
1688: if (isnull) return 0;
1689: PetscDrawSetTitle(draw, "Mesh");
1691: VecGetLocalSize(coordinates, &N);
1692: VecGetArrayRead(coordinates, &coords);
1693: for (c = 0; c < N; c += dim) {
1694: bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));
1695: bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1696: bound[1] = PetscMin(bound[1], PetscRealPart(coords[c + 1]));
1697: bound[3] = PetscMax(bound[3], PetscRealPart(coords[c + 1]));
1698: }
1699: VecRestoreArrayRead(coordinates, &coords);
1700: MPIU_Allreduce(&bound[0], xyl, 2, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));
1701: MPIU_Allreduce(&bound[2], xyr, 2, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)dm));
1702: PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);
1703: PetscDrawClear(draw);
1705: for (c = cStart; c < cEnd; ++c) {
1706: PetscScalar *coords = NULL;
1707: PetscInt numCoords;
1709: DMPlexVecGetClosureAtDepth_Internal(dm, coordSection, coordinates, c, 0, &numCoords, &coords);
1710: if (drawAffine) DMPlexDrawCell(dm, draw, c, coords);
1711: else DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords);
1712: DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
1713: }
1714: if (!drawAffine) PetscFree2(refCoords, edgeCoords);
1715: PetscDrawFlush(draw);
1716: PetscDrawPause(draw);
1717: PetscDrawSave(draw);
1718: return 0;
1719: }
1721: #if defined(PETSC_HAVE_EXODUSII)
1722: #include <exodusII.h>
1723: #include <petscviewerexodusii.h>
1724: #endif
1726: PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1727: {
1728: PetscBool iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus, iscgns;
1729: char name[PETSC_MAX_PATH_LEN];
1733: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii);
1734: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk);
1735: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1736: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw);
1737: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis);
1738: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodus);
1739: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns);
1740: if (iascii) {
1741: PetscViewerFormat format;
1742: PetscViewerGetFormat(viewer, &format);
1743: if (format == PETSC_VIEWER_ASCII_GLVIS) DMPlexView_GLVis(dm, viewer);
1744: else DMPlexView_Ascii(dm, viewer);
1745: } else if (ishdf5) {
1746: #if defined(PETSC_HAVE_HDF5)
1747: DMPlexView_HDF5_Internal(dm, viewer);
1748: #else
1749: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1750: #endif
1751: } else if (isvtk) {
1752: DMPlexVTKWriteAll((PetscObject)dm, viewer);
1753: } else if (isdraw) {
1754: DMPlexView_Draw(dm, viewer);
1755: } else if (isglvis) {
1756: DMPlexView_GLVis(dm, viewer);
1757: #if defined(PETSC_HAVE_EXODUSII)
1758: } else if (isexodus) {
1759: /*
1760: exodusII requires that all sets be part of exactly one cell set.
1761: If the dm does not have a "Cell Sets" label defined, we create one
1762: with ID 1, containing all cells.
1763: Note that if the Cell Sets label is defined but does not cover all cells,
1764: we may still have a problem. This should probably be checked here or in the viewer;
1765: */
1766: PetscInt numCS;
1767: DMGetLabelSize(dm, "Cell Sets", &numCS);
1768: if (!numCS) {
1769: PetscInt cStart, cEnd, c;
1770: DMCreateLabel(dm, "Cell Sets");
1771: DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
1772: for (c = cStart; c < cEnd; ++c) DMSetLabelValue(dm, "Cell Sets", c, 1);
1773: }
1774: DMView_PlexExodusII(dm, viewer);
1775: #endif
1776: #if defined(PETSC_HAVE_CGNS)
1777: } else if (iscgns) {
1778: DMView_PlexCGNS(dm, viewer);
1779: #endif
1780: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1781: /* Optionally view the partition */
1782: PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_partition_view", &flg);
1783: if (flg) {
1784: Vec ranks;
1785: DMPlexCreateRankField(dm, &ranks);
1786: VecView(ranks, viewer);
1787: VecDestroy(&ranks);
1788: }
1789: /* Optionally view a label */
1790: PetscOptionsGetString(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_label_view", name, sizeof(name), &flg);
1791: if (flg) {
1792: DMLabel label;
1793: Vec val;
1795: DMGetLabel(dm, name, &label);
1797: DMPlexCreateLabelField(dm, label, &val);
1798: VecView(val, viewer);
1799: VecDestroy(&val);
1800: }
1801: return 0;
1802: }
1804: /*@
1805: DMPlexTopologyView - Saves a `DMPLEX` topology into a file
1807: Collective on dm
1809: Input Parameters:
1810: + dm - The `DM` whose topology is to be saved
1811: - viewer - The `PetscViewer` to save it in
1813: Level: advanced
1815: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsView()`, `DMPlexTopologyLoad()`, `PetscViewer`
1816: @*/
1817: PetscErrorCode DMPlexTopologyView(DM dm, PetscViewer viewer)
1818: {
1819: PetscBool ishdf5;
1823: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1824: PetscLogEventBegin(DMPLEX_TopologyView, viewer, 0, 0, 0);
1825: if (ishdf5) {
1826: #if defined(PETSC_HAVE_HDF5)
1827: PetscViewerFormat format;
1828: PetscViewerGetFormat(viewer, &format);
1829: if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1830: IS globalPointNumbering;
1832: DMPlexCreatePointNumbering(dm, &globalPointNumbering);
1833: DMPlexTopologyView_HDF5_Internal(dm, globalPointNumbering, viewer);
1834: ISDestroy(&globalPointNumbering);
1835: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
1836: #else
1837: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1838: #endif
1839: }
1840: PetscLogEventEnd(DMPLEX_TopologyView, viewer, 0, 0, 0);
1841: return 0;
1842: }
1844: /*@
1845: DMPlexCoordinatesView - Saves `DMPLEX` coordinates into a file
1847: Collective on dm
1849: Input Parameters:
1850: + dm - The `DM` whose coordinates are to be saved
1851: - viewer - The `PetscViewer` for saving
1853: Level: advanced
1855: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexLabelsView()`, `DMPlexCoordinatesLoad()`, `PetscViewer`
1856: @*/
1857: PetscErrorCode DMPlexCoordinatesView(DM dm, PetscViewer viewer)
1858: {
1859: PetscBool ishdf5;
1863: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1864: PetscLogEventBegin(DMPLEX_CoordinatesView, viewer, 0, 0, 0);
1865: if (ishdf5) {
1866: #if defined(PETSC_HAVE_HDF5)
1867: PetscViewerFormat format;
1868: PetscViewerGetFormat(viewer, &format);
1869: if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1870: DMPlexCoordinatesView_HDF5_Internal(dm, viewer);
1871: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
1872: #else
1873: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1874: #endif
1875: }
1876: PetscLogEventEnd(DMPLEX_CoordinatesView, viewer, 0, 0, 0);
1877: return 0;
1878: }
1880: /*@
1881: DMPlexLabelsView - Saves `DMPLEX` labels into a file
1883: Collective on dm
1885: Input Parameters:
1886: + dm - The `DM` whose labels are to be saved
1887: - viewer - The `PetscViewer` for saving
1889: Level: advanced
1891: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsLoad()`, `PetscViewer`
1892: @*/
1893: PetscErrorCode DMPlexLabelsView(DM dm, PetscViewer viewer)
1894: {
1895: PetscBool ishdf5;
1899: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1900: PetscLogEventBegin(DMPLEX_LabelsView, viewer, 0, 0, 0);
1901: if (ishdf5) {
1902: #if defined(PETSC_HAVE_HDF5)
1903: IS globalPointNumbering;
1904: PetscViewerFormat format;
1906: PetscViewerGetFormat(viewer, &format);
1907: if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1908: DMPlexCreatePointNumbering(dm, &globalPointNumbering);
1909: DMPlexLabelsView_HDF5_Internal(dm, globalPointNumbering, viewer);
1910: ISDestroy(&globalPointNumbering);
1911: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1912: #else
1913: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1914: #endif
1915: }
1916: PetscLogEventEnd(DMPLEX_LabelsView, viewer, 0, 0, 0);
1917: return 0;
1918: }
1920: /*@
1921: DMPlexSectionView - Saves a section associated with a `DMPLEX`
1923: Collective on dm
1925: Input Parameters:
1926: + dm - The `DM` that contains the topology on which the section to be saved is defined
1927: . viewer - The `PetscViewer` for saving
1928: - sectiondm - The `DM` that contains the section to be saved
1930: Level: advanced
1932: Notes:
1933: This function is a wrapper around `PetscSectionView()`; in addition to the raw section, it saves information that associates the section points to the topology (dm) points. When the topology (dm) and the section are later loaded with `DMPlexTopologyLoad()` and `DMPlexSectionLoad()`, respectively, this information is used to match section points with topology points.
1935: In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
1937: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsView()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`, `PetscSectionView()`, `DMPlexSectionLoad()`, `PetscViewer`
1938: @*/
1939: PetscErrorCode DMPlexSectionView(DM dm, PetscViewer viewer, DM sectiondm)
1940: {
1941: PetscBool ishdf5;
1946: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1947: PetscLogEventBegin(DMPLEX_SectionView, viewer, 0, 0, 0);
1948: if (ishdf5) {
1949: #if defined(PETSC_HAVE_HDF5)
1950: DMPlexSectionView_HDF5_Internal(dm, viewer, sectiondm);
1951: #else
1952: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1953: #endif
1954: }
1955: PetscLogEventEnd(DMPLEX_SectionView, viewer, 0, 0, 0);
1956: return 0;
1957: }
1959: /*@
1960: DMPlexGlobalVectorView - Saves a global vector
1962: Collective on dm
1964: Input Parameters:
1965: + dm - The `DM` that represents the topology
1966: . viewer - The `PetscViewer` to save data with
1967: . sectiondm - The `DM` that contains the global section on which vec is defined
1968: - vec - The global vector to be saved
1970: Level: advanced
1972: Notes:
1973: In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
1975: Typical calling sequence:
1976: .vb
1977: DMCreate(PETSC_COMM_WORLD, &dm);
1978: DMSetType(dm, DMPLEX);
1979: PetscObjectSetName((PetscObject)dm, "topologydm_name");
1980: DMClone(dm, §iondm);
1981: PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
1982: PetscSectionCreate(PETSC_COMM_WORLD, §ion);
1983: DMPlexGetChart(sectiondm, &pStart, &pEnd);
1984: PetscSectionSetChart(section, pStart, pEnd);
1985: PetscSectionSetUp(section);
1986: DMSetLocalSection(sectiondm, section);
1987: PetscSectionDestroy(§ion);
1988: DMGetGlobalVector(sectiondm, &vec);
1989: PetscObjectSetName((PetscObject)vec, "vec_name");
1990: DMPlexTopologyView(dm, viewer);
1991: DMPlexSectionView(dm, viewer, sectiondm);
1992: DMPlexGlobalVectorView(dm, viewer, sectiondm, vec);
1993: DMRestoreGlobalVector(sectiondm, &vec);
1994: DMDestroy(§iondm);
1995: DMDestroy(&dm);
1996: .ve
1998: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyView()`, `DMPlexSectionView()`, `DMPlexLocalVectorView()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`
1999: @*/
2000: PetscErrorCode DMPlexGlobalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
2001: {
2002: PetscBool ishdf5;
2008: /* Check consistency */
2009: {
2010: PetscSection section;
2011: PetscBool includesConstraints;
2012: PetscInt m, m1;
2014: VecGetLocalSize(vec, &m1);
2015: DMGetGlobalSection(sectiondm, §ion);
2016: PetscSectionGetIncludesConstraints(section, &includesConstraints);
2017: if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2018: else PetscSectionGetConstrainedStorageSize(section, &m);
2020: }
2021: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2022: PetscLogEventBegin(DMPLEX_GlobalVectorView, viewer, 0, 0, 0);
2023: if (ishdf5) {
2024: #if defined(PETSC_HAVE_HDF5)
2025: DMPlexGlobalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec);
2026: #else
2027: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2028: #endif
2029: }
2030: PetscLogEventEnd(DMPLEX_GlobalVectorView, viewer, 0, 0, 0);
2031: return 0;
2032: }
2034: /*@
2035: DMPlexLocalVectorView - Saves a local vector
2037: Collective on dm
2039: Input Parameters:
2040: + dm - The `DM` that represents the topology
2041: . viewer - The `PetscViewer` to save data with
2042: . sectiondm - The `DM` that contains the local section on which vec is defined; may be the same as dm
2043: - vec - The local vector to be saved
2045: Level: advanced
2047: Note:
2048: In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
2050: Typical calling sequence:
2051: .vb
2052: DMCreate(PETSC_COMM_WORLD, &dm);
2053: DMSetType(dm, DMPLEX);
2054: PetscObjectSetName((PetscObject)dm, "topologydm_name");
2055: DMClone(dm, §iondm);
2056: PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2057: PetscSectionCreate(PETSC_COMM_WORLD, §ion);
2058: DMPlexGetChart(sectiondm, &pStart, &pEnd);
2059: PetscSectionSetChart(section, pStart, pEnd);
2060: PetscSectionSetUp(section);
2061: DMSetLocalSection(sectiondm, section);
2062: DMGetLocalVector(sectiondm, &vec);
2063: PetscObjectSetName((PetscObject)vec, "vec_name");
2064: DMPlexTopologyView(dm, viewer);
2065: DMPlexSectionView(dm, viewer, sectiondm);
2066: DMPlexLocalVectorView(dm, viewer, sectiondm, vec);
2067: DMRestoreLocalVector(sectiondm, &vec);
2068: DMDestroy(§iondm);
2069: DMDestroy(&dm);
2070: .ve
2072: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyView()`, `DMPlexSectionView()`, `DMPlexGlobalVectorView()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`
2073: @*/
2074: PetscErrorCode DMPlexLocalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
2075: {
2076: PetscBool ishdf5;
2082: /* Check consistency */
2083: {
2084: PetscSection section;
2085: PetscBool includesConstraints;
2086: PetscInt m, m1;
2088: VecGetLocalSize(vec, &m1);
2089: DMGetLocalSection(sectiondm, §ion);
2090: PetscSectionGetIncludesConstraints(section, &includesConstraints);
2091: if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2092: else PetscSectionGetConstrainedStorageSize(section, &m);
2094: }
2095: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2096: PetscLogEventBegin(DMPLEX_LocalVectorView, viewer, 0, 0, 0);
2097: if (ishdf5) {
2098: #if defined(PETSC_HAVE_HDF5)
2099: DMPlexLocalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec);
2100: #else
2101: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2102: #endif
2103: }
2104: PetscLogEventEnd(DMPLEX_LocalVectorView, viewer, 0, 0, 0);
2105: return 0;
2106: }
2108: PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
2109: {
2110: PetscBool ishdf5;
2114: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2115: if (ishdf5) {
2116: #if defined(PETSC_HAVE_HDF5)
2117: PetscViewerFormat format;
2118: PetscViewerGetFormat(viewer, &format);
2119: if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
2120: DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);
2121: } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2122: DMPlexLoad_HDF5_Internal(dm, viewer);
2123: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2124: return 0;
2125: #else
2126: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2127: #endif
2128: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
2129: }
2131: /*@
2132: DMPlexTopologyLoad - Loads a topology into a `DMPLEX`
2134: Collective on dm
2136: Input Parameters:
2137: + dm - The `DM` into which the topology is loaded
2138: - viewer - The `PetscViewer` for the saved topology
2140: Output Parameters:
2141: . globalToLocalPointSF - The `PetscSF` that pushes points in [0, N) to the associated points in the loaded plex, where N is the global number of points; NULL if unneeded
2143: Level: advanced
2145: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexCoordinatesLoad()`, `DMPlexLabelsLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2146: `PetscViewer`, `PetscSF`
2147: @*/
2148: PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *globalToLocalPointSF)
2149: {
2150: PetscBool ishdf5;
2155: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2156: PetscLogEventBegin(DMPLEX_TopologyLoad, viewer, 0, 0, 0);
2157: if (ishdf5) {
2158: #if defined(PETSC_HAVE_HDF5)
2159: PetscViewerFormat format;
2160: PetscViewerGetFormat(viewer, &format);
2161: if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2162: DMPlexTopologyLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);
2163: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2164: #else
2165: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2166: #endif
2167: }
2168: PetscLogEventEnd(DMPLEX_TopologyLoad, viewer, 0, 0, 0);
2169: return 0;
2170: }
2172: /*@
2173: DMPlexCoordinatesLoad - Loads coordinates into a `DMPLEX`
2175: Collective on dm
2177: Input Parameters:
2178: + dm - The `DM` into which the coordinates are loaded
2179: . viewer - The `PetscViewer` for the saved coordinates
2180: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad()` when loading dm from viewer
2182: Level: advanced
2184: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexLabelsLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2185: `PetscSF`, `PetscViewer`
2186: @*/
2187: PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2188: {
2189: PetscBool ishdf5;
2194: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2195: PetscLogEventBegin(DMPLEX_CoordinatesLoad, viewer, 0, 0, 0);
2196: if (ishdf5) {
2197: #if defined(PETSC_HAVE_HDF5)
2198: PetscViewerFormat format;
2199: PetscViewerGetFormat(viewer, &format);
2200: if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2201: DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);
2202: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2203: #else
2204: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2205: #endif
2206: }
2207: PetscLogEventEnd(DMPLEX_CoordinatesLoad, viewer, 0, 0, 0);
2208: return 0;
2209: }
2211: /*@
2212: DMPlexLabelsLoad - Loads labels into a `DMPLEX`
2214: Collective on dm
2216: Input Parameters:
2217: + dm - The `DM` into which the labels are loaded
2218: . viewer - The `PetscViewer` for the saved labels
2219: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad()` when loading dm from viewer
2221: Level: advanced
2223: Note:
2224: The `PetscSF` argument must not be NULL if the `DM` is distributed, otherwise an error occurs.
2226: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexCoordinatesLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2227: `PetscSF`, `PetscViewer`
2228: @*/
2229: PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2230: {
2231: PetscBool ishdf5;
2236: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2237: PetscLogEventBegin(DMPLEX_LabelsLoad, viewer, 0, 0, 0);
2238: if (ishdf5) {
2239: #if defined(PETSC_HAVE_HDF5)
2240: PetscViewerFormat format;
2242: PetscViewerGetFormat(viewer, &format);
2243: if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2244: DMPlexLabelsLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);
2245: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2246: #else
2247: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2248: #endif
2249: }
2250: PetscLogEventEnd(DMPLEX_LabelsLoad, viewer, 0, 0, 0);
2251: return 0;
2252: }
2254: /*@
2255: DMPlexSectionLoad - Loads section into a `DMPLEX`
2257: Collective on dm
2259: Input Parameters:
2260: + dm - The `DM` that represents the topology
2261: . viewer - The `PetscViewer` that represents the on-disk section (sectionA)
2262: . sectiondm - The `DM` into which the on-disk section (sectionA) is migrated
2263: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad(`) when loading dm from viewer
2265: Output Parameters
2266: + globalDofSF - The SF that migrates any on-disk Vec data associated with sectionA into a global Vec associated with the sectiondm's global section (NULL if not needed)
2267: - localDofSF - The SF that migrates any on-disk Vec data associated with sectionA into a local Vec associated with the sectiondm's local section (NULL if not needed)
2269: Level: advanced
2271: Notes:
2272: This function is a wrapper around `PetscSectionLoad()`; it loads, in addition to the raw section, a list of global point numbers that associates each on-disk section point with a global point number in [0, NX), where NX is the number of topology points in dm. Noting that globalToLocalPointSF associates each topology point in dm with a global number in [0, NX), one can readily establish an association of the on-disk section points with the topology points.
2274: In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
2276: The output parameter, globalDofSF (localDofSF), can later be used with `DMPlexGlobalVectorLoad()` (`DMPlexLocalVectorLoad()`) to load on-disk vectors into global (local) vectors associated with sectiondm's global (local) section.
2278: Example using 2 processes:
2279: .vb
2280: NX (number of points on dm): 4
2281: sectionA : the on-disk section
2282: vecA : a vector associated with sectionA
2283: sectionB : sectiondm's local section constructed in this function
2284: vecB (local) : a vector associated with sectiondm's local section
2285: vecB (global) : a vector associated with sectiondm's global section
2287: rank 0 rank 1
2288: vecA (global) : [.0 .4 .1 | .2 .3] <- to be loaded in DMPlexGlobalVectorLoad() or DMPlexLocalVectorLoad()
2289: sectionA->atlasOff : 0 2 | 1 <- loaded in PetscSectionLoad()
2290: sectionA->atlasDof : 1 3 | 1 <- loaded in PetscSectionLoad()
2291: sectionA's global point numbers: 0 2 | 3 <- loaded in DMPlexSectionLoad()
2292: [0, NX) : 0 1 | 2 3 <- conceptual partition used in globalToLocalPointSF
2293: sectionB's global point numbers: 0 1 3 | 3 2 <- associated with [0, NX) by globalToLocalPointSF
2294: sectionB->atlasDof : 1 0 1 | 1 3
2295: sectionB->atlasOff (no perm) : 0 1 1 | 0 1
2296: vecB (local) : [.0 .4] | [.4 .1 .2 .3] <- to be constructed by calling DMPlexLocalVectorLoad() with localDofSF
2297: vecB (global) : [.0 .4 | .1 .2 .3] <- to be constructed by calling DMPlexGlobalVectorLoad() with globalDofSF
2298: .ve
2299: where "|" represents a partition of loaded data, and global point 3 is assumed to be owned by rank 0.
2301: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexCoordinatesLoad()`, `DMPlexLabelsLoad()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`, `PetscSectionLoad()`, `DMPlexSectionView()`, `PetscSF`, `PetscViewer`
2302: @*/
2303: PetscErrorCode DMPlexSectionLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF globalToLocalPointSF, PetscSF *globalDofSF, PetscSF *localDofSF)
2304: {
2305: PetscBool ishdf5;
2313: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2314: PetscLogEventBegin(DMPLEX_SectionLoad, viewer, 0, 0, 0);
2315: if (ishdf5) {
2316: #if defined(PETSC_HAVE_HDF5)
2317: DMPlexSectionLoad_HDF5_Internal(dm, viewer, sectiondm, globalToLocalPointSF, globalDofSF, localDofSF);
2318: #else
2319: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2320: #endif
2321: }
2322: PetscLogEventEnd(DMPLEX_SectionLoad, viewer, 0, 0, 0);
2323: return 0;
2324: }
2326: /*@
2327: DMPlexGlobalVectorLoad - Loads on-disk vector data into a global vector
2329: Collective on dm
2331: Input Parameters:
2332: + dm - The `DM` that represents the topology
2333: . viewer - The `PetscViewer` that represents the on-disk vector data
2334: . sectiondm - The `DM` that contains the global section on which vec is defined
2335: . sf - The `PetscSF` that migrates the on-disk vector data into vec
2336: - vec - The global vector to set values of
2338: Level: advanced
2340: Notes:
2341: In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
2343: Typical calling sequence:
2344: .vb
2345: DMCreate(PETSC_COMM_WORLD, &dm);
2346: DMSetType(dm, DMPLEX);
2347: PetscObjectSetName((PetscObject)dm, "topologydm_name");
2348: DMPlexTopologyLoad(dm, viewer, &sfX);
2349: DMClone(dm, §iondm);
2350: PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2351: DMPlexSectionLoad(dm, viewer, sectiondm, sfX, &gsf, NULL);
2352: DMGetGlobalVector(sectiondm, &vec);
2353: PetscObjectSetName((PetscObject)vec, "vec_name");
2354: DMPlexGlobalVectorLoad(dm, viewer, sectiondm, gsf, vec);
2355: DMRestoreGlobalVector(sectiondm, &vec);
2356: PetscSFDestroy(&gsf);
2357: PetscSFDestroy(&sfX);
2358: DMDestroy(§iondm);
2359: DMDestroy(&dm);
2360: .ve
2362: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyLoad()`, `DMPlexSectionLoad()`, `DMPlexLocalVectorLoad()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`,
2363: `PetscSF`, `PetscViewer`
2364: @*/
2365: PetscErrorCode DMPlexGlobalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2366: {
2367: PetscBool ishdf5;
2374: /* Check consistency */
2375: {
2376: PetscSection section;
2377: PetscBool includesConstraints;
2378: PetscInt m, m1;
2380: VecGetLocalSize(vec, &m1);
2381: DMGetGlobalSection(sectiondm, §ion);
2382: PetscSectionGetIncludesConstraints(section, &includesConstraints);
2383: if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2384: else PetscSectionGetConstrainedStorageSize(section, &m);
2386: }
2387: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2388: PetscLogEventBegin(DMPLEX_GlobalVectorLoad, viewer, 0, 0, 0);
2389: if (ishdf5) {
2390: #if defined(PETSC_HAVE_HDF5)
2391: DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec);
2392: #else
2393: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2394: #endif
2395: }
2396: PetscLogEventEnd(DMPLEX_GlobalVectorLoad, viewer, 0, 0, 0);
2397: return 0;
2398: }
2400: /*@
2401: DMPlexLocalVectorLoad - Loads on-disk vector data into a local vector
2403: Collective on dm
2405: Input Parameters:
2406: + dm - The `DM` that represents the topology
2407: . viewer - The `PetscViewer` that represents the on-disk vector data
2408: . sectiondm - The `DM` that contains the local section on which vec is defined
2409: . sf - The `PetscSF` that migrates the on-disk vector data into vec
2410: - vec - The local vector to set values of
2412: Level: advanced
2414: Notes:
2415: In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.
2417: Typical calling sequence:
2418: .vb
2419: DMCreate(PETSC_COMM_WORLD, &dm);
2420: DMSetType(dm, DMPLEX);
2421: PetscObjectSetName((PetscObject)dm, "topologydm_name");
2422: DMPlexTopologyLoad(dm, viewer, &sfX);
2423: DMClone(dm, §iondm);
2424: PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2425: DMPlexSectionLoad(dm, viewer, sectiondm, sfX, NULL, &lsf);
2426: DMGetLocalVector(sectiondm, &vec);
2427: PetscObjectSetName((PetscObject)vec, "vec_name");
2428: DMPlexLocalVectorLoad(dm, viewer, sectiondm, lsf, vec);
2429: DMRestoreLocalVector(sectiondm, &vec);
2430: PetscSFDestroy(&lsf);
2431: PetscSFDestroy(&sfX);
2432: DMDestroy(§iondm);
2433: DMDestroy(&dm);
2434: .ve
2436: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyLoad()`, `DMPlexSectionLoad()`, `DMPlexGlobalVectorLoad()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`,
2437: `PetscSF`, `PetscViewer`
2438: @*/
2439: PetscErrorCode DMPlexLocalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2440: {
2441: PetscBool ishdf5;
2448: /* Check consistency */
2449: {
2450: PetscSection section;
2451: PetscBool includesConstraints;
2452: PetscInt m, m1;
2454: VecGetLocalSize(vec, &m1);
2455: DMGetLocalSection(sectiondm, §ion);
2456: PetscSectionGetIncludesConstraints(section, &includesConstraints);
2457: if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2458: else PetscSectionGetConstrainedStorageSize(section, &m);
2460: }
2461: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2462: PetscLogEventBegin(DMPLEX_LocalVectorLoad, viewer, 0, 0, 0);
2463: if (ishdf5) {
2464: #if defined(PETSC_HAVE_HDF5)
2465: DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec);
2466: #else
2467: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2468: #endif
2469: }
2470: PetscLogEventEnd(DMPLEX_LocalVectorLoad, viewer, 0, 0, 0);
2471: return 0;
2472: }
2474: PetscErrorCode DMDestroy_Plex(DM dm)
2475: {
2476: DM_Plex *mesh = (DM_Plex *)dm->data;
2478: PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", NULL);
2479: PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", NULL);
2480: PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", NULL);
2481: PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", NULL);
2482: PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerviativeBoundaryValues_C", NULL);
2483: PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", NULL);
2484: PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", NULL);
2485: PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", NULL);
2486: PetscObjectComposeFunction((PetscObject)dm, "MatComputeNeumannOverlap_C", NULL);
2487: PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", NULL);
2488: PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", NULL);
2489: PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", NULL);
2490: PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", NULL);
2491: if (--mesh->refct > 0) return 0;
2492: PetscSectionDestroy(&mesh->coneSection);
2493: PetscFree(mesh->cones);
2494: PetscFree(mesh->coneOrientations);
2495: PetscSectionDestroy(&mesh->supportSection);
2496: PetscSectionDestroy(&mesh->subdomainSection);
2497: PetscFree(mesh->supports);
2498: PetscFree(mesh->facesTmp);
2499: PetscFree(mesh->tetgenOpts);
2500: PetscFree(mesh->triangleOpts);
2501: PetscFree(mesh->transformType);
2502: PetscFree(mesh->distributionName);
2503: PetscPartitionerDestroy(&mesh->partitioner);
2504: DMLabelDestroy(&mesh->subpointMap);
2505: ISDestroy(&mesh->subpointIS);
2506: ISDestroy(&mesh->globalVertexNumbers);
2507: ISDestroy(&mesh->globalCellNumbers);
2508: PetscSectionDestroy(&mesh->anchorSection);
2509: ISDestroy(&mesh->anchorIS);
2510: PetscSectionDestroy(&mesh->parentSection);
2511: PetscFree(mesh->parents);
2512: PetscFree(mesh->childIDs);
2513: PetscSectionDestroy(&mesh->childSection);
2514: PetscFree(mesh->children);
2515: DMDestroy(&mesh->referenceTree);
2516: PetscGridHashDestroy(&mesh->lbox);
2517: PetscFree(mesh->neighbors);
2518: if (mesh->metricCtx) PetscFree(mesh->metricCtx);
2519: /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
2520: PetscFree(mesh);
2521: return 0;
2522: }
2524: PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
2525: {
2526: PetscSection sectionGlobal;
2527: PetscInt bs = -1, mbs;
2528: PetscInt localSize, localStart = 0;
2529: PetscBool isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
2530: MatType mtype;
2531: ISLocalToGlobalMapping ltog;
2533: MatInitializePackage();
2534: mtype = dm->mattype;
2535: DMGetGlobalSection(dm, §ionGlobal);
2536: /* PetscSectionGetStorageSize(sectionGlobal, &localSize); */
2537: PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);
2538: MPI_Exscan(&localSize, &localStart, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)dm));
2539: MatCreate(PetscObjectComm((PetscObject)dm), J);
2540: MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);
2541: MatSetType(*J, mtype);
2542: MatSetFromOptions(*J);
2543: MatGetBlockSize(*J, &mbs);
2544: if (mbs > 1) bs = mbs;
2545: PetscStrcmp(mtype, MATSHELL, &isShell);
2546: PetscStrcmp(mtype, MATBAIJ, &isBlock);
2547: PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);
2548: PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);
2549: PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);
2550: PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);
2551: PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);
2552: PetscStrcmp(mtype, MATIS, &isMatIS);
2553: if (!isShell) {
2554: PetscBool fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
2555: PetscInt *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *pblocks;
2556: PetscInt pStart, pEnd, p, dof, cdof;
2558: DMGetLocalToGlobalMapping(dm, <og);
2560: PetscCalloc1(localSize, &pblocks);
2561: PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);
2562: for (p = pStart; p < pEnd; ++p) {
2563: PetscInt bdof, offset;
2565: PetscSectionGetDof(sectionGlobal, p, &dof);
2566: PetscSectionGetOffset(sectionGlobal, p, &offset);
2567: PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);
2568: for (PetscInt i = 0; i < dof - cdof; i++) pblocks[offset - localStart + i] = dof - cdof;
2569: dof = dof < 0 ? -(dof + 1) : dof;
2570: bdof = cdof && (dof - cdof) ? 1 : dof;
2571: if (dof) {
2572: if (bs < 0) {
2573: bs = bdof;
2574: } else if (bs != bdof) {
2575: bs = 1;
2576: }
2577: }
2578: }
2579: /* Must have same blocksize on all procs (some might have no points) */
2580: bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
2581: bsLocal[1] = bs;
2582: PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax);
2583: if (bsMinMax[0] != bsMinMax[1]) bs = 1;
2584: else bs = bsMinMax[0];
2585: bs = PetscMax(1, bs);
2586: MatSetLocalToGlobalMapping(*J, ltog, ltog);
2587: if (dm->prealloc_skip) { // User will likely use MatSetPreallocationCOO(), but still set structural parameters
2588: MatSetBlockSize(*J, bs);
2589: MatSetUp(*J);
2590: } else {
2591: PetscCalloc4(localSize / bs, &dnz, localSize / bs, &onz, localSize / bs, &dnzu, localSize / bs, &onzu);
2592: DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);
2593: PetscFree4(dnz, onz, dnzu, onzu);
2594: }
2595: { // Consolidate blocks
2596: PetscInt nblocks = 0;
2597: for (PetscInt i = 0; i < localSize; i += PetscMax(1, pblocks[i])) {
2598: if (pblocks[i] == 0) continue;
2599: pblocks[nblocks++] = pblocks[i]; // nblocks always <= i
2601: }
2602: MatSetVariableBlockSizes(*J, nblocks, pblocks);
2603: }
2604: PetscFree(pblocks);
2605: }
2606: MatSetDM(*J, dm);
2607: return 0;
2608: }
2610: /*@
2611: DMPlexGetSubdomainSection - Returns the section associated with the subdomain
2613: Not Collective
2615: Input Parameter:
2616: . mesh - The `DMPLEX`
2618: Output Parameters:
2619: . subsection - The subdomain section
2621: Level: developer
2623: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `PetscSection`
2624: @*/
2625: PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
2626: {
2627: DM_Plex *mesh = (DM_Plex *)dm->data;
2630: if (!mesh->subdomainSection) {
2631: PetscSection section;
2632: PetscSF sf;
2634: PetscSFCreate(PETSC_COMM_SELF, &sf);
2635: DMGetLocalSection(dm, §ion);
2636: PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_TRUE, &mesh->subdomainSection);
2637: PetscSFDestroy(&sf);
2638: }
2639: *subsection = mesh->subdomainSection;
2640: return 0;
2641: }
2643: /*@
2644: DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
2646: Not Collective
2648: Input Parameter:
2649: . mesh - The `DMPLEX`
2651: Output Parameters:
2652: + pStart - The first mesh point
2653: - pEnd - The upper bound for mesh points
2655: Level: beginner
2657: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetChart()`
2658: @*/
2659: PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
2660: {
2661: DM_Plex *mesh = (DM_Plex *)dm->data;
2664: PetscSectionGetChart(mesh->coneSection, pStart, pEnd);
2665: return 0;
2666: }
2668: /*@
2669: DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
2671: Not Collective
2673: Input Parameters:
2674: + mesh - The `DMPLEX`
2675: . pStart - The first mesh point
2676: - pEnd - The upper bound for mesh points
2678: Level: beginner
2680: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetChart()`
2681: @*/
2682: PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
2683: {
2684: DM_Plex *mesh = (DM_Plex *)dm->data;
2687: PetscSectionSetChart(mesh->coneSection, pStart, pEnd);
2688: PetscSectionSetChart(mesh->supportSection, pStart, pEnd);
2689: return 0;
2690: }
2692: /*@
2693: DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
2695: Not Collective
2697: Input Parameters:
2698: + mesh - The `DMPLEX`
2699: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
2701: Output Parameter:
2702: . size - The cone size for point p
2704: Level: beginner
2706: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`
2707: @*/
2708: PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
2709: {
2710: DM_Plex *mesh = (DM_Plex *)dm->data;
2714: PetscSectionGetDof(mesh->coneSection, p, size);
2715: return 0;
2716: }
2718: /*@
2719: DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
2721: Not Collective
2723: Input Parameters:
2724: + mesh - The `DMPLEX`
2725: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
2726: - size - The cone size for point p
2728: Level: beginner
2730: Note:
2731: This should be called after `DMPlexSetChart()`.
2733: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetConeSize()`, `DMPlexSetChart()`
2734: @*/
2735: PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
2736: {
2737: DM_Plex *mesh = (DM_Plex *)dm->data;
2740: PetscSectionSetDof(mesh->coneSection, p, size);
2741: return 0;
2742: }
2744: /*@
2745: DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
2747: Not Collective
2749: Input Parameters:
2750: + mesh - The `DMPLEX`
2751: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
2752: - size - The additional cone size for point p
2754: Level: beginner
2756: Note:
2757: This should be called after `DMPlexSetChart()`.
2759: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexGetConeSize()`, `DMPlexSetChart()`
2760: @*/
2761: PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
2762: {
2763: DM_Plex *mesh = (DM_Plex *)dm->data;
2765: PetscSectionAddDof(mesh->coneSection, p, size);
2766: return 0;
2767: }
2769: /*@C
2770: DMPlexGetCone - Return the points on the in-edges for this point in the DAG
2772: Not Collective
2774: Input Parameters:
2775: + dm - The `DMPLEX`
2776: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
2778: Output Parameter:
2779: . cone - An array of points which are on the in-edges for point p
2781: Level: beginner
2783: Fortran Note:
2784: You must also call `DMPlexRestoreCone()` after you finish using the returned array.
2785: `DMPlexRestoreCone()` is not needed/available in C.
2787: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSize()`, `DMPlexSetCone()`, `DMPlexGetConeTuple()`, `DMPlexSetChart()`, `DMPlexRestoreCone()`
2788: @*/
2789: PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
2790: {
2791: DM_Plex *mesh = (DM_Plex *)dm->data;
2792: PetscInt off;
2796: PetscSectionGetOffset(mesh->coneSection, p, &off);
2797: *cone = &mesh->cones[off];
2798: return 0;
2799: }
2801: /*@C
2802: DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
2804: Not Collective
2806: Input Parameters:
2807: + dm - The `DMPLEX`
2808: - p - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`
2810: Output Parameters:
2811: + pConesSection - `PetscSection` describing the layout of pCones
2812: - pCones - An array of points which are on the in-edges for the point set p
2814: Level: intermediate
2816: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeRecursive()`, `DMPlexSetChart()`, `PetscSection`, `IS`
2817: @*/
2818: PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
2819: {
2820: PetscSection cs, newcs;
2821: PetscInt *cones;
2822: PetscInt *newarr = NULL;
2823: PetscInt n;
2825: DMPlexGetCones(dm, &cones);
2826: DMPlexGetConeSection(dm, &cs);
2827: PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void **)&newarr) : NULL);
2828: if (pConesSection) *pConesSection = newcs;
2829: if (pCones) {
2830: PetscSectionGetStorageSize(newcs, &n);
2831: ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);
2832: }
2833: return 0;
2834: }
2836: /*@
2837: DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
2839: Not Collective
2841: Input Parameters:
2842: + dm - The `DMPLEX`
2843: - points - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`
2845: Output Parameter:
2846: . expandedPoints - An array of vertices recursively expanded from input points
2848: Level: advanced
2850: Notes:
2851: Like `DMPlexGetConeRecursive()` but returns only the 0-depth IS (i.e. vertices only) and no sections.
2853: There is no corresponding Restore function, just call `ISDestroy()` on the returned `IS` to deallocate.
2855: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexGetConeRecursive()`, `DMPlexRestoreConeRecursive()`,
2856: `DMPlexGetDepth()`, `IS`
2857: @*/
2858: PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
2859: {
2860: IS *expandedPointsAll;
2861: PetscInt depth;
2866: DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
2867: *expandedPoints = expandedPointsAll[0];
2868: PetscObjectReference((PetscObject)expandedPointsAll[0]);
2869: DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
2870: return 0;
2871: }
2873: /*@
2874: DMPlexGetConeRecursive - Expand each given point into its cone points and do that recursively until we end up just with vertices (DAG points of depth 0, i.e. without cones).
2876: Not Collective
2878: Input Parameters:
2879: + dm - The `DMPLEX`
2880: - points - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`
2882: Output Parameters:
2883: + depth - (optional) Size of the output arrays, equal to `DMPLEX` depth, returned by `DMPlexGetDepth()`
2884: . expandedPoints - (optional) An array of index sets with recursively expanded cones
2885: - sections - (optional) An array of sections which describe mappings from points to their cone points
2887: Level: advanced
2889: Notes:
2890: Like `DMPlexGetConeTuple()` but recursive.
2892: Array expandedPoints has size equal to depth. Each expandedPoints[d] contains DAG points with maximum depth d, recursively cone-wise expanded from the input points.
2893: For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
2895: Array section has size equal to depth. Each `PetscSection` sections[d] realizes mapping from expandedPoints[d+1] (section points) to expandedPoints[d] (section dofs) as follows:
2896: (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
2897: (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
2899: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexRestoreConeRecursive()`, `DMPlexGetConeRecursiveVertices()`,
2900: `DMPlexGetDepth()`, `PetscSection`, `IS`
2901: @*/
2902: PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2903: {
2904: const PetscInt *arr0 = NULL, *cone = NULL;
2905: PetscInt *arr = NULL, *newarr = NULL;
2906: PetscInt d, depth_, i, n, newn, cn, co, start, end;
2907: IS *expandedPoints_;
2908: PetscSection *sections_;
2915: ISGetLocalSize(points, &n);
2916: ISGetIndices(points, &arr0);
2917: DMPlexGetDepth(dm, &depth_);
2918: PetscCalloc1(depth_, &expandedPoints_);
2919: PetscCalloc1(depth_, §ions_);
2920: arr = (PetscInt *)arr0; /* this is ok because first generation of arr is not modified */
2921: for (d = depth_ - 1; d >= 0; d--) {
2922: PetscSectionCreate(PETSC_COMM_SELF, §ions_[d]);
2923: PetscSectionSetChart(sections_[d], 0, n);
2924: for (i = 0; i < n; i++) {
2925: DMPlexGetDepthStratum(dm, d + 1, &start, &end);
2926: if (arr[i] >= start && arr[i] < end) {
2927: DMPlexGetConeSize(dm, arr[i], &cn);
2928: PetscSectionSetDof(sections_[d], i, cn);
2929: } else {
2930: PetscSectionSetDof(sections_[d], i, 1);
2931: }
2932: }
2933: PetscSectionSetUp(sections_[d]);
2934: PetscSectionGetStorageSize(sections_[d], &newn);
2935: PetscMalloc1(newn, &newarr);
2936: for (i = 0; i < n; i++) {
2937: PetscSectionGetDof(sections_[d], i, &cn);
2938: PetscSectionGetOffset(sections_[d], i, &co);
2939: if (cn > 1) {
2940: DMPlexGetCone(dm, arr[i], &cone);
2941: PetscMemcpy(&newarr[co], cone, cn * sizeof(PetscInt));
2942: } else {
2943: newarr[co] = arr[i];
2944: }
2945: }
2946: ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);
2947: arr = newarr;
2948: n = newn;
2949: }
2950: ISRestoreIndices(points, &arr0);
2951: *depth = depth_;
2952: if (expandedPoints) *expandedPoints = expandedPoints_;
2953: else {
2954: for (d = 0; d < depth_; d++) ISDestroy(&expandedPoints_[d]);
2955: PetscFree(expandedPoints_);
2956: }
2957: if (sections) *sections = sections_;
2958: else {
2959: for (d = 0; d < depth_; d++) PetscSectionDestroy(§ions_[d]);
2960: PetscFree(sections_);
2961: }
2962: return 0;
2963: }
2965: /*@
2966: DMPlexRestoreConeRecursive - Deallocates arrays created by `DMPlexGetConeRecursive()`
2968: Not Collective
2970: Input Parameters:
2971: + dm - The `DMPLEX`
2972: - points - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`
2974: Output Parameters:
2975: + depth - (optional) Size of the output arrays, equal to `DMPLEX` depth, returned by `DMPlexGetDepth()`
2976: . expandedPoints - (optional) An array of recursively expanded cones
2977: - sections - (optional) An array of sections which describe mappings from points to their cone points
2979: Level: advanced
2981: Note:
2982: See `DMPlexGetConeRecursive()`
2984: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexGetConeRecursive()`, `DMPlexGetConeRecursiveVertices()`,
2985: `DMPlexGetDepth()`, `IS`, `PetscSection`
2986: @*/
2987: PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2988: {
2989: PetscInt d, depth_;
2991: DMPlexGetDepth(dm, &depth_);
2993: if (depth) *depth = 0;
2994: if (expandedPoints) {
2995: for (d = 0; d < depth_; d++) ISDestroy(&((*expandedPoints)[d]));
2996: PetscFree(*expandedPoints);
2997: }
2998: if (sections) {
2999: for (d = 0; d < depth_; d++) PetscSectionDestroy(&((*sections)[d]));
3000: PetscFree(*sections);
3001: }
3002: return 0;
3003: }
3005: /*@
3006: DMPlexSetCone - Set the points on the in-edges for this point in the DAG; that is these are the points that cover the specific point
3008: Not Collective
3010: Input Parameters:
3011: + mesh - The `DMPLEX`
3012: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3013: - cone - An array of points which are on the in-edges for point p
3015: Level: beginner
3017: Note:
3018: This should be called after all calls to `DMPlexSetConeSize()` and `DMSetUp()`.
3020: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`, `DMPlexSetSupport()`, `DMPlexSetSupportSize()`
3021: @*/
3022: PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
3023: {
3024: DM_Plex *mesh = (DM_Plex *)dm->data;
3025: PetscInt pStart, pEnd;
3026: PetscInt dof, off, c;
3029: PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3030: PetscSectionGetDof(mesh->coneSection, p, &dof);
3032: PetscSectionGetOffset(mesh->coneSection, p, &off);
3034: for (c = 0; c < dof; ++c) {
3036: mesh->cones[off + c] = cone[c];
3037: }
3038: return 0;
3039: }
3041: /*@C
3042: DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
3044: Not Collective
3046: Input Parameters:
3047: + mesh - The `DMPLEX`
3048: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
3050: Output Parameter:
3051: . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
3052: integer giving the prescription for cone traversal.
3054: Level: beginner
3056: Note:
3057: The number indexes the symmetry transformations for the cell type (see manual). Orientation 0 is always
3058: the identity transformation. Negative orientation indicates reflection so that -(o+1) is the reflection
3059: of o, however it is not necessarily the inverse. To get the inverse, use `DMPolytopeTypeComposeOrientationInv()`
3060: with the identity.
3062: Fortran Note:
3063: You must also call `DMPlexRestoreConeOrientation()` after you finish using the returned array.
3064: `DMPlexRestoreConeOrientation()` is not needed/available in C.
3066: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPolytopeTypeComposeOrientation()`, `DMPolytopeTypeComposeOrientationInv()`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetCone()`, `DMPlexSetChart()`
3067: @*/
3068: PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
3069: {
3070: DM_Plex *mesh = (DM_Plex *)dm->data;
3071: PetscInt off;
3074: if (PetscDefined(USE_DEBUG)) {
3075: PetscInt dof;
3076: PetscSectionGetDof(mesh->coneSection, p, &dof);
3078: }
3079: PetscSectionGetOffset(mesh->coneSection, p, &off);
3081: *coneOrientation = &mesh->coneOrientations[off];
3082: return 0;
3083: }
3085: /*@
3086: DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
3088: Not Collective
3090: Input Parameters:
3091: + mesh - The `DMPLEX`
3092: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3093: - coneOrientation - An array of orientations
3095: Level: beginner
3097: Notes:
3098: This should be called after all calls to `DMPlexSetConeSize()` and `DMSetUp()`.
3100: The meaning of coneOrientation is detailed in `DMPlexGetConeOrientation()`.
3102: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetConeOrientation()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3103: @*/
3104: PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
3105: {
3106: DM_Plex *mesh = (DM_Plex *)dm->data;
3107: PetscInt pStart, pEnd;
3108: PetscInt dof, off, c;
3111: PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3112: PetscSectionGetDof(mesh->coneSection, p, &dof);
3114: PetscSectionGetOffset(mesh->coneSection, p, &off);
3116: for (c = 0; c < dof; ++c) {
3117: PetscInt cdof, o = coneOrientation[c];
3119: PetscSectionGetDof(mesh->coneSection, mesh->cones[off + c], &cdof);
3121: mesh->coneOrientations[off + c] = o;
3122: }
3123: return 0;
3124: }
3126: /*@
3127: DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
3129: Not Collective
3131: Input Parameters:
3132: + mesh - The `DMPLEX`
3133: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3134: . conePos - The local index in the cone where the point should be put
3135: - conePoint - The mesh point to insert
3137: Level: beginner
3139: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3140: @*/
3141: PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
3142: {
3143: DM_Plex *mesh = (DM_Plex *)dm->data;
3144: PetscInt pStart, pEnd;
3145: PetscInt dof, off;
3148: PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3151: PetscSectionGetDof(mesh->coneSection, p, &dof);
3152: PetscSectionGetOffset(mesh->coneSection, p, &off);
3154: mesh->cones[off + conePos] = conePoint;
3155: return 0;
3156: }
3158: /*@
3159: DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
3161: Not Collective
3163: Input Parameters:
3164: + mesh - The `DMPLEX`
3165: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3166: . conePos - The local index in the cone where the point should be put
3167: - coneOrientation - The point orientation to insert
3169: Level: beginner
3171: Note:
3172: The meaning of coneOrientation values is detailed in `DMPlexGetConeOrientation()`.
3174: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3175: @*/
3176: PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
3177: {
3178: DM_Plex *mesh = (DM_Plex *)dm->data;
3179: PetscInt pStart, pEnd;
3180: PetscInt dof, off;
3183: PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3185: PetscSectionGetDof(mesh->coneSection, p, &dof);
3186: PetscSectionGetOffset(mesh->coneSection, p, &off);
3188: mesh->coneOrientations[off + conePos] = coneOrientation;
3189: return 0;
3190: }
3192: /*@
3193: DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
3195: Not Collective
3197: Input Parameters:
3198: + mesh - The `DMPLEX`
3199: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
3201: Output Parameter:
3202: . size - The support size for point p
3204: Level: beginner
3206: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`, `DMPlexGetConeSize()`
3207: @*/
3208: PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
3209: {
3210: DM_Plex *mesh = (DM_Plex *)dm->data;
3214: PetscSectionGetDof(mesh->supportSection, p, size);
3215: return 0;
3216: }
3218: /*@
3219: DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
3221: Not Collective
3223: Input Parameters:
3224: + mesh - The `DMPLEX`
3225: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3226: - size - The support size for point p
3228: Level: beginner
3230: Note:
3231: This should be called after DMPlexSetChart().
3233: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetSupportSize()`, `DMPlexSetChart()`
3234: @*/
3235: PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
3236: {
3237: DM_Plex *mesh = (DM_Plex *)dm->data;
3240: PetscSectionSetDof(mesh->supportSection, p, size);
3241: return 0;
3242: }
3244: /*@C
3245: DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
3247: Not Collective
3249: Input Parameters:
3250: + mesh - The `DMPLEX`
3251: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
3253: Output Parameter:
3254: . support - An array of points which are on the out-edges for point p
3256: Level: beginner
3258: Fortran Note:
3259: You must also call `DMPlexRestoreSupport()` after you finish using the returned array.
3260: `DMPlexRestoreSupport()` is not needed/available in C.
3262: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetSupportSize()`, `DMPlexSetSupport()`, `DMPlexGetCone()`, `DMPlexSetChart()`
3263: @*/
3264: PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
3265: {
3266: DM_Plex *mesh = (DM_Plex *)dm->data;
3267: PetscInt off;
3271: PetscSectionGetOffset(mesh->supportSection, p, &off);
3272: *support = &mesh->supports[off];
3273: return 0;
3274: }
3276: /*@
3277: DMPlexSetSupport - Set the points on the out-edges for this point in the DAG, that is the list of points that this point covers
3279: Not Collective
3281: Input Parameters:
3282: + mesh - The `DMPLEX`
3283: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3284: - support - An array of points which are on the out-edges for point p
3286: Level: beginner
3288: Note:
3289: This should be called after all calls to `DMPlexSetSupportSize()` and `DMSetUp()`.
3291: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexSetCone()`, `DMPlexSetConeSize()`, `DMPlexCreate()`, `DMPlexGetSupport()`, `DMPlexSetChart()`, `DMPlexSetSupportSize()`, `DMSetUp()`
3292: @*/
3293: PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
3294: {
3295: DM_Plex *mesh = (DM_Plex *)dm->data;
3296: PetscInt pStart, pEnd;
3297: PetscInt dof, off, c;
3300: PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
3301: PetscSectionGetDof(mesh->supportSection, p, &dof);
3303: PetscSectionGetOffset(mesh->supportSection, p, &off);
3305: for (c = 0; c < dof; ++c) {
3307: mesh->supports[off + c] = support[c];
3308: }
3309: return 0;
3310: }
3312: /*@
3313: DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
3315: Not Collective
3317: Input Parameters:
3318: + mesh - The `DMPLEX`
3319: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3320: . supportPos - The local index in the cone where the point should be put
3321: - supportPoint - The mesh point to insert
3323: Level: beginner
3325: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3326: @*/
3327: PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
3328: {
3329: DM_Plex *mesh = (DM_Plex *)dm->data;
3330: PetscInt pStart, pEnd;
3331: PetscInt dof, off;
3334: PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
3335: PetscSectionGetDof(mesh->supportSection, p, &dof);
3336: PetscSectionGetOffset(mesh->supportSection, p, &off);
3340: mesh->supports[off + supportPos] = supportPoint;
3341: return 0;
3342: }
3344: /* Converts an orientation o in the current numbering to the previous scheme used in Plex */
3345: PetscInt DMPolytopeConvertNewOrientation_Internal(DMPolytopeType ct, PetscInt o)
3346: {
3347: switch (ct) {
3348: case DM_POLYTOPE_SEGMENT:
3349: if (o == -1) return -2;
3350: break;
3351: case DM_POLYTOPE_TRIANGLE:
3352: if (o == -3) return -1;
3353: if (o == -2) return -3;
3354: if (o == -1) return -2;
3355: break;
3356: case DM_POLYTOPE_QUADRILATERAL:
3357: if (o == -4) return -2;
3358: if (o == -3) return -1;
3359: if (o == -2) return -4;
3360: if (o == -1) return -3;
3361: break;
3362: default:
3363: return o;
3364: }
3365: return o;
3366: }
3368: /* Converts an orientation o in the previous scheme used in Plex to the current numbering */
3369: PetscInt DMPolytopeConvertOldOrientation_Internal(DMPolytopeType ct, PetscInt o)
3370: {
3371: switch (ct) {
3372: case DM_POLYTOPE_SEGMENT:
3373: if ((o == -2) || (o == 1)) return -1;
3374: if (o == -1) return 0;
3375: break;
3376: case DM_POLYTOPE_TRIANGLE:
3377: if (o == -3) return -2;
3378: if (o == -2) return -1;
3379: if (o == -1) return -3;
3380: break;
3381: case DM_POLYTOPE_QUADRILATERAL:
3382: if (o == -4) return -2;
3383: if (o == -3) return -1;
3384: if (o == -2) return -4;
3385: if (o == -1) return -3;
3386: break;
3387: default:
3388: return o;
3389: }
3390: return o;
3391: }
3393: /* Takes in a mesh whose orientations are in the previous scheme and converts them all to the current numbering */
3394: PetscErrorCode DMPlexConvertOldOrientations_Internal(DM dm)
3395: {
3396: PetscInt pStart, pEnd, p;
3398: DMPlexGetChart(dm, &pStart, &pEnd);
3399: for (p = pStart; p < pEnd; ++p) {
3400: const PetscInt *cone, *ornt;
3401: PetscInt coneSize, c;
3403: DMPlexGetConeSize(dm, p, &coneSize);
3404: DMPlexGetCone(dm, p, &cone);
3405: DMPlexGetConeOrientation(dm, p, &ornt);
3406: for (c = 0; c < coneSize; ++c) {
3407: DMPolytopeType ct;
3408: const PetscInt o = ornt[c];
3410: DMPlexGetCellType(dm, cone[c], &ct);
3411: switch (ct) {
3412: case DM_POLYTOPE_SEGMENT:
3413: if ((o == -2) || (o == 1)) DMPlexInsertConeOrientation(dm, p, c, -1);
3414: if (o == -1) DMPlexInsertConeOrientation(dm, p, c, 0);
3415: break;
3416: case DM_POLYTOPE_TRIANGLE:
3417: if (o == -3) DMPlexInsertConeOrientation(dm, p, c, -2);
3418: if (o == -2) DMPlexInsertConeOrientation(dm, p, c, -1);
3419: if (o == -1) DMPlexInsertConeOrientation(dm, p, c, -3);
3420: break;
3421: case DM_POLYTOPE_QUADRILATERAL:
3422: if (o == -4) DMPlexInsertConeOrientation(dm, p, c, -2);
3423: if (o == -3) DMPlexInsertConeOrientation(dm, p, c, -1);
3424: if (o == -2) DMPlexInsertConeOrientation(dm, p, c, -4);
3425: if (o == -1) DMPlexInsertConeOrientation(dm, p, c, -3);
3426: break;
3427: default:
3428: break;
3429: }
3430: }
3431: }
3432: return 0;
3433: }
3435: static PetscErrorCode DMPlexGetTransitiveClosure_Depth1_Private(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3436: {
3437: DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3438: PetscInt *closure;
3439: const PetscInt *tmp = NULL, *tmpO = NULL;
3440: PetscInt off = 0, tmpSize, t;
3443: if (ornt) {
3444: DMPlexGetCellType(dm, p, &ct);
3445: if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN) ct = DM_POLYTOPE_UNKNOWN;
3446: }
3447: if (*points) {
3448: closure = *points;
3449: } else {
3450: PetscInt maxConeSize, maxSupportSize;
3451: DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
3452: DMGetWorkArray(dm, 2 * (PetscMax(maxConeSize, maxSupportSize) + 1), MPIU_INT, &closure);
3453: }
3454: if (useCone) {
3455: DMPlexGetConeSize(dm, p, &tmpSize);
3456: DMPlexGetCone(dm, p, &tmp);
3457: DMPlexGetConeOrientation(dm, p, &tmpO);
3458: } else {
3459: DMPlexGetSupportSize(dm, p, &tmpSize);
3460: DMPlexGetSupport(dm, p, &tmp);
3461: }
3462: if (ct == DM_POLYTOPE_UNKNOWN) {
3463: closure[off++] = p;
3464: closure[off++] = 0;
3465: for (t = 0; t < tmpSize; ++t) {
3466: closure[off++] = tmp[t];
3467: closure[off++] = tmpO ? tmpO[t] : 0;
3468: }
3469: } else {
3470: const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, ornt);
3472: /* We assume that cells with a valid type have faces with a valid type */
3473: closure[off++] = p;
3474: closure[off++] = ornt;
3475: for (t = 0; t < tmpSize; ++t) {
3476: DMPolytopeType ft;
3478: DMPlexGetCellType(dm, tmp[t], &ft);
3479: closure[off++] = tmp[arr[t]];
3480: closure[off++] = tmpO ? DMPolytopeTypeComposeOrientation(ft, ornt, tmpO[t]) : 0;
3481: }
3482: }
3483: if (numPoints) *numPoints = tmpSize + 1;
3484: if (points) *points = closure;
3485: return 0;
3486: }
3488: /* We need a special tensor version because we want to allow duplicate points in the endcaps for hybrid cells */
3489: static PetscErrorCode DMPlexTransitiveClosure_Tensor_Internal(DM dm, PetscInt point, DMPolytopeType ct, PetscInt o, PetscBool useCone, PetscInt *numPoints, PetscInt **points)
3490: {
3491: const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
3492: const PetscInt *cone, *ornt;
3493: PetscInt *pts, *closure = NULL;
3494: DMPolytopeType ft;
3495: PetscInt maxConeSize, maxSupportSize, coneSeries, supportSeries, maxSize;
3496: PetscInt dim, coneSize, c, d, clSize, cl;
3499: DMGetDimension(dm, &dim);
3500: DMPlexGetConeSize(dm, point, &coneSize);
3501: DMPlexGetCone(dm, point, &cone);
3502: DMPlexGetConeOrientation(dm, point, &ornt);
3503: DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
3504: coneSeries = (maxConeSize > 1) ? ((PetscPowInt(maxConeSize, dim + 1) - 1) / (maxConeSize - 1)) : dim + 1;
3505: supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, dim + 1) - 1) / (maxSupportSize - 1)) : dim + 1;
3506: maxSize = PetscMax(coneSeries, supportSeries);
3507: if (*points) {
3508: pts = *points;
3509: } else DMGetWorkArray(dm, 2 * maxSize, MPIU_INT, &pts);
3510: c = 0;
3511: pts[c++] = point;
3512: pts[c++] = o;
3513: DMPlexGetCellType(dm, cone[arr[0 * 2 + 0]], &ft);
3514: DMPlexGetTransitiveClosure_Internal(dm, cone[arr[0 * 2 + 0]], DMPolytopeTypeComposeOrientation(ft, arr[0 * 2 + 1], ornt[0]), useCone, &clSize, &closure);
3515: for (cl = 0; cl < clSize * 2; cl += 2) {
3516: pts[c++] = closure[cl];
3517: pts[c++] = closure[cl + 1];
3518: }
3519: DMPlexGetTransitiveClosure_Internal(dm, cone[arr[1 * 2 + 0]], DMPolytopeTypeComposeOrientation(ft, arr[1 * 2 + 1], ornt[1]), useCone, &clSize, &closure);
3520: for (cl = 0; cl < clSize * 2; cl += 2) {
3521: pts[c++] = closure[cl];
3522: pts[c++] = closure[cl + 1];
3523: }
3524: DMPlexRestoreTransitiveClosure(dm, cone[0], useCone, &clSize, &closure);
3525: for (d = 2; d < coneSize; ++d) {
3526: DMPlexGetCellType(dm, cone[arr[d * 2 + 0]], &ft);
3527: pts[c++] = cone[arr[d * 2 + 0]];
3528: pts[c++] = DMPolytopeTypeComposeOrientation(ft, arr[d * 2 + 1], ornt[d]);
3529: }
3530: if (dim >= 3) {
3531: for (d = 2; d < coneSize; ++d) {
3532: const PetscInt fpoint = cone[arr[d * 2 + 0]];
3533: const PetscInt *fcone, *fornt;
3534: PetscInt fconeSize, fc, i;
3536: DMPlexGetCellType(dm, fpoint, &ft);
3537: const PetscInt *farr = DMPolytopeTypeGetArrangment(ft, DMPolytopeTypeComposeOrientation(ft, arr[d * 2 + 1], ornt[d]));
3538: DMPlexGetConeSize(dm, fpoint, &fconeSize);
3539: DMPlexGetCone(dm, fpoint, &fcone);
3540: DMPlexGetConeOrientation(dm, fpoint, &fornt);
3541: for (fc = 0; fc < fconeSize; ++fc) {
3542: const PetscInt cp = fcone[farr[fc * 2 + 0]];
3543: const PetscInt co = farr[fc * 2 + 1];
3545: for (i = 0; i < c; i += 2)
3546: if (pts[i] == cp) break;
3547: if (i == c) {
3548: DMPlexGetCellType(dm, cp, &ft);
3549: pts[c++] = cp;
3550: pts[c++] = DMPolytopeTypeComposeOrientation(ft, co, fornt[farr[fc * 2 + 0]]);
3551: }
3552: }
3553: }
3554: }
3555: *numPoints = c / 2;
3556: *points = pts;
3557: return 0;
3558: }
3560: PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3561: {
3562: DMPolytopeType ct;
3563: PetscInt *closure, *fifo;
3564: PetscInt closureSize = 0, fifoStart = 0, fifoSize = 0;
3565: PetscInt maxConeSize, maxSupportSize, coneSeries, supportSeries;
3566: PetscInt depth, maxSize;
3569: DMPlexGetDepth(dm, &depth);
3570: if (depth == 1) {
3571: DMPlexGetTransitiveClosure_Depth1_Private(dm, p, ornt, useCone, numPoints, points);
3572: return 0;
3573: }
3574: DMPlexGetCellType(dm, p, &ct);
3575: if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN) ct = DM_POLYTOPE_UNKNOWN;
3576: if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR || ct == DM_POLYTOPE_TRI_PRISM_TENSOR || ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) {
3577: DMPlexTransitiveClosure_Tensor_Internal(dm, p, ct, ornt, useCone, numPoints, points);
3578: return 0;
3579: }
3580: DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
3581: coneSeries = (maxConeSize > 1) ? ((PetscPowInt(maxConeSize, depth + 1) - 1) / (maxConeSize - 1)) : depth + 1;
3582: supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, depth + 1) - 1) / (maxSupportSize - 1)) : depth + 1;
3583: maxSize = PetscMax(coneSeries, supportSeries);
3584: DMGetWorkArray(dm, 3 * maxSize, MPIU_INT, &fifo);
3585: if (*points) {
3586: closure = *points;
3587: } else DMGetWorkArray(dm, 2 * maxSize, MPIU_INT, &closure);
3588: closure[closureSize++] = p;
3589: closure[closureSize++] = ornt;
3590: fifo[fifoSize++] = p;
3591: fifo[fifoSize++] = ornt;
3592: fifo[fifoSize++] = ct;
3593: /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
3594: while (fifoSize - fifoStart) {
3595: const PetscInt q = fifo[fifoStart++];
3596: const PetscInt o = fifo[fifoStart++];
3597: const DMPolytopeType qt = (DMPolytopeType)fifo[fifoStart++];
3598: const PetscInt *qarr = DMPolytopeTypeGetArrangment(qt, o);
3599: const PetscInt *tmp, *tmpO;
3600: PetscInt tmpSize, t;
3602: if (PetscDefined(USE_DEBUG)) {
3603: PetscInt nO = DMPolytopeTypeGetNumArrangments(qt) / 2;
3605: }
3606: if (useCone) {
3607: DMPlexGetConeSize(dm, q, &tmpSize);
3608: DMPlexGetCone(dm, q, &tmp);
3609: DMPlexGetConeOrientation(dm, q, &tmpO);
3610: } else {
3611: DMPlexGetSupportSize(dm, q, &tmpSize);
3612: DMPlexGetSupport(dm, q, &tmp);
3613: tmpO = NULL;
3614: }
3615: for (t = 0; t < tmpSize; ++t) {
3616: const PetscInt ip = useCone && qarr ? qarr[t * 2] : t;
3617: const PetscInt io = useCone && qarr ? qarr[t * 2 + 1] : 0;
3618: const PetscInt cp = tmp[ip];
3619: DMPlexGetCellType(dm, cp, &ct);
3620: const PetscInt co = tmpO ? DMPolytopeTypeComposeOrientation(ct, io, tmpO[ip]) : 0;
3621: PetscInt c;
3623: /* Check for duplicate */
3624: for (c = 0; c < closureSize; c += 2) {
3625: if (closure[c] == cp) break;
3626: }
3627: if (c == closureSize) {
3628: closure[closureSize++] = cp;
3629: closure[closureSize++] = co;
3630: fifo[fifoSize++] = cp;
3631: fifo[fifoSize++] = co;
3632: fifo[fifoSize++] = ct;
3633: }
3634: }
3635: }
3636: DMRestoreWorkArray(dm, 3 * maxSize, MPIU_INT, &fifo);
3637: if (numPoints) *numPoints = closureSize / 2;
3638: if (points) *points = closure;
3639: return 0;
3640: }
3642: /*@C
3643: DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
3645: Not Collective
3647: Input Parameters:
3648: + dm - The `DMPLEX`
3649: . p - The mesh point
3650: - useCone - `PETSC_TRUE` for the closure, otherwise return the star
3652: Input/Output Parameter:
3653: . points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...];
3654: if NULL on input, internal storage will be returned, otherwise the provided array is used
3656: Output Parameter:
3657: . numPoints - The number of points in the closure, so points[] is of size 2*numPoints
3659: Level: beginner
3661: Note:
3662: If using internal storage (points is NULL on input), each call overwrites the last output.
3664: Fortran Note:
3665: The numPoints argument is not present in the Fortran binding since it is internal to the array.
3667: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreTransitiveClosure()`, `DMPlexCreate()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexGetCone()`
3668: @*/
3669: PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3670: {
3675: DMPlexGetTransitiveClosure_Internal(dm, p, 0, useCone, numPoints, points);
3676: return 0;
3677: }
3679: /*@C
3680: DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
3682: Not Collective
3684: Input Parameters:
3685: + dm - The `DMPLEX`
3686: . p - The mesh point
3687: . useCone - `PETSC_TRUE` for the closure, otherwise return the star
3688: . numPoints - The number of points in the closure, so points[] is of size 2*numPoints
3689: - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
3691: Level: beginner
3693: Note:
3694: If not using internal storage (points is not NULL on input), this call is unnecessary
3696: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetTransitiveClosure()`, `DMPlexCreate()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexGetCone()`
3697: @*/
3698: PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3699: {
3702: if (numPoints) *numPoints = 0;
3703: DMRestoreWorkArray(dm, 0, MPIU_INT, points);
3704: return 0;
3705: }
3707: /*@
3708: DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
3710: Not Collective
3712: Input Parameter:
3713: . mesh - The `DMPLEX`
3715: Output Parameters:
3716: + maxConeSize - The maximum number of in-edges
3717: - maxSupportSize - The maximum number of out-edges
3719: Level: beginner
3721: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`
3722: @*/
3723: PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
3724: {
3725: DM_Plex *mesh = (DM_Plex *)dm->data;
3728: if (maxConeSize) PetscSectionGetMaxDof(mesh->coneSection, maxConeSize);
3729: if (maxSupportSize) PetscSectionGetMaxDof(mesh->supportSection, maxSupportSize);
3730: return 0;
3731: }
3733: PetscErrorCode DMSetUp_Plex(DM dm)
3734: {
3735: DM_Plex *mesh = (DM_Plex *)dm->data;
3736: PetscInt size, maxSupportSize;
3739: PetscSectionSetUp(mesh->coneSection);
3740: PetscSectionGetStorageSize(mesh->coneSection, &size);
3741: PetscMalloc1(size, &mesh->cones);
3742: PetscCalloc1(size, &mesh->coneOrientations);
3743: PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize);
3744: if (maxSupportSize) {
3745: PetscSectionSetUp(mesh->supportSection);
3746: PetscSectionGetStorageSize(mesh->supportSection, &size);
3747: PetscMalloc1(size, &mesh->supports);
3748: }
3749: return 0;
3750: }
3752: PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
3753: {
3754: if (subdm) DMClone(dm, subdm);
3755: DMCreateSectionSubDM(dm, numFields, fields, is, subdm);
3756: if (subdm) (*subdm)->useNatural = dm->useNatural;
3757: if (dm->useNatural && dm->sfMigration) {
3758: PetscSF sfNatural;
3760: (*subdm)->sfMigration = dm->sfMigration;
3761: PetscObjectReference((PetscObject)dm->sfMigration);
3762: DMPlexCreateGlobalToNaturalSF(*subdm, NULL, (*subdm)->sfMigration, &sfNatural);
3763: (*subdm)->sfNatural = sfNatural;
3764: }
3765: return 0;
3766: }
3768: PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
3769: {
3770: PetscInt i = 0;
3772: DMClone(dms[0], superdm);
3773: DMCreateSectionSuperDM(dms, len, is, superdm);
3774: (*superdm)->useNatural = PETSC_FALSE;
3775: for (i = 0; i < len; i++) {
3776: if (dms[i]->useNatural && dms[i]->sfMigration) {
3777: PetscSF sfNatural;
3779: (*superdm)->sfMigration = dms[i]->sfMigration;
3780: PetscObjectReference((PetscObject)dms[i]->sfMigration);
3781: (*superdm)->useNatural = PETSC_TRUE;
3782: DMPlexCreateGlobalToNaturalSF(*superdm, NULL, (*superdm)->sfMigration, &sfNatural);
3783: (*superdm)->sfNatural = sfNatural;
3784: break;
3785: }
3786: }
3787: return 0;
3788: }
3790: /*@
3791: DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
3793: Not Collective
3795: Input Parameter:
3796: . mesh - The `DMPLEX`
3798: Level: beginner
3800: Note:
3801: This should be called after all calls to `DMPlexSetCone()`
3803: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMPlexSetCone()`
3804: @*/
3805: PetscErrorCode DMPlexSymmetrize(DM dm)
3806: {
3807: DM_Plex *mesh = (DM_Plex *)dm->data;
3808: PetscInt *offsets;
3809: PetscInt supportSize;
3810: PetscInt pStart, pEnd, p;
3814: PetscLogEventBegin(DMPLEX_Symmetrize, dm, 0, 0, 0);
3815: /* Calculate support sizes */
3816: DMPlexGetChart(dm, &pStart, &pEnd);
3817: for (p = pStart; p < pEnd; ++p) {
3818: PetscInt dof, off, c;
3820: PetscSectionGetDof(mesh->coneSection, p, &dof);
3821: PetscSectionGetOffset(mesh->coneSection, p, &off);
3822: for (c = off; c < off + dof; ++c) PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);
3823: }
3824: PetscSectionSetUp(mesh->supportSection);
3825: /* Calculate supports */
3826: PetscSectionGetStorageSize(mesh->supportSection, &supportSize);
3827: PetscMalloc1(supportSize, &mesh->supports);
3828: PetscCalloc1(pEnd - pStart, &offsets);
3829: for (p = pStart; p < pEnd; ++p) {
3830: PetscInt dof, off, c;
3832: PetscSectionGetDof(mesh->coneSection, p, &dof);
3833: PetscSectionGetOffset(mesh->coneSection, p, &off);
3834: for (c = off; c < off + dof; ++c) {
3835: const PetscInt q = mesh->cones[c];
3836: PetscInt offS;
3838: PetscSectionGetOffset(mesh->supportSection, q, &offS);
3840: mesh->supports[offS + offsets[q]] = p;
3841: ++offsets[q];
3842: }
3843: }
3844: PetscFree(offsets);
3845: PetscLogEventEnd(DMPLEX_Symmetrize, dm, 0, 0, 0);
3846: return 0;
3847: }
3849: static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
3850: {
3851: IS stratumIS;
3853: if (pStart >= pEnd) return 0;
3854: if (PetscDefined(USE_DEBUG)) {
3855: PetscInt qStart, qEnd, numLevels, level;
3856: PetscBool overlap = PETSC_FALSE;
3857: DMLabelGetNumValues(label, &numLevels);
3858: for (level = 0; level < numLevels; level++) {
3859: DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
3860: if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {
3861: overlap = PETSC_TRUE;
3862: break;
3863: }
3864: }
3866: }
3867: ISCreateStride(PETSC_COMM_SELF, pEnd - pStart, pStart, 1, &stratumIS);
3868: DMLabelSetStratumIS(label, depth, stratumIS);
3869: ISDestroy(&stratumIS);
3870: return 0;
3871: }
3873: /*@
3874: DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
3875: can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
3876: same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
3877: the DAG.
3879: Collective on dm
3881: Input Parameter:
3882: . mesh - The `DMPLEX`
3884: Level: beginner
3886: Notes:
3887: Concretely, `DMPlexStratify()` creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
3888: meshes, vertices are depth 0 and cells are depth 1. For fully interpolated meshes, depth 0 for vertices, 1 for edges, and so on
3889: until cells have depth equal to the dimension of the mesh. The depth label can be accessed through `DMPlexGetDepthLabel()` or `DMPlexGetDepthStratum()`, or
3890: manually via `DMGetLabel()`. The height is defined implicitly by height = maxDimension - depth, and can be accessed
3891: via `DMPlexGetHeightStratum()`. For example, cells have height 0 and faces have height 1.
3893: The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
3894: if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
3895: we had a mesh consisting of one triangle (c0) and three vertices (v0, v1, v2), and only one edge is on the boundary so we choose
3896: to interpolate only that one (e0), so that
3897: .vb
3898: cone(c0) = {e0, v2}
3899: cone(e0) = {v0, v1}
3900: .ve
3901: If `DMPlexStratify()` is run on this mesh, it will give depths
3902: .vb
3903: depth 0 = {v0, v1, v2}
3904: depth 1 = {e0, c0}
3905: .ve
3906: where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
3908: `DMPlexStratify()` should be called after all calls to `DMPlexSymmetrize()`
3910: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSymmetrize()`, `DMPlexComputeCellTypes()`
3911: @*/
3912: PetscErrorCode DMPlexStratify(DM dm)
3913: {
3914: DM_Plex *mesh = (DM_Plex *)dm->data;
3915: DMLabel label;
3916: PetscInt pStart, pEnd, p;
3917: PetscInt numRoots = 0, numLeaves = 0;
3920: PetscLogEventBegin(DMPLEX_Stratify, dm, 0, 0, 0);
3922: /* Create depth label */
3923: DMPlexGetChart(dm, &pStart, &pEnd);
3924: DMCreateLabel(dm, "depth");
3925: DMPlexGetDepthLabel(dm, &label);
3927: {
3928: /* Initialize roots and count leaves */
3929: PetscInt sMin = PETSC_MAX_INT;
3930: PetscInt sMax = PETSC_MIN_INT;
3931: PetscInt coneSize, supportSize;
3933: for (p = pStart; p < pEnd; ++p) {
3934: DMPlexGetConeSize(dm, p, &coneSize);
3935: DMPlexGetSupportSize(dm, p, &supportSize);
3936: if (!coneSize && supportSize) {
3937: sMin = PetscMin(p, sMin);
3938: sMax = PetscMax(p, sMax);
3939: ++numRoots;
3940: } else if (!supportSize && coneSize) {
3941: ++numLeaves;
3942: } else if (!supportSize && !coneSize) {
3943: /* Isolated points */
3944: sMin = PetscMin(p, sMin);
3945: sMax = PetscMax(p, sMax);
3946: }
3947: }
3948: DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax + 1);
3949: }
3951: if (numRoots + numLeaves == (pEnd - pStart)) {
3952: PetscInt sMin = PETSC_MAX_INT;
3953: PetscInt sMax = PETSC_MIN_INT;
3954: PetscInt coneSize, supportSize;
3956: for (p = pStart; p < pEnd; ++p) {
3957: DMPlexGetConeSize(dm, p, &coneSize);
3958: DMPlexGetSupportSize(dm, p, &supportSize);
3959: if (!supportSize && coneSize) {
3960: sMin = PetscMin(p, sMin);
3961: sMax = PetscMax(p, sMax);
3962: }
3963: }
3964: DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax + 1);
3965: } else {
3966: PetscInt level = 0;
3967: PetscInt qStart, qEnd, q;
3969: DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
3970: while (qEnd > qStart) {
3971: PetscInt sMin = PETSC_MAX_INT;
3972: PetscInt sMax = PETSC_MIN_INT;
3974: for (q = qStart; q < qEnd; ++q) {
3975: const PetscInt *support;
3976: PetscInt supportSize, s;
3978: DMPlexGetSupportSize(dm, q, &supportSize);
3979: DMPlexGetSupport(dm, q, &support);
3980: for (s = 0; s < supportSize; ++s) {
3981: sMin = PetscMin(support[s], sMin);
3982: sMax = PetscMax(support[s], sMax);
3983: }
3984: }
3985: DMLabelGetNumValues(label, &level);
3986: DMPlexCreateDepthStratum(dm, label, level, sMin, sMax + 1);
3987: DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
3988: }
3989: }
3990: { /* just in case there is an empty process */
3991: PetscInt numValues, maxValues = 0, v;
3993: DMLabelGetNumValues(label, &numValues);
3994: MPI_Allreduce(&numValues, &maxValues, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
3995: for (v = numValues; v < maxValues; v++) DMLabelAddStratum(label, v);
3996: }
3997: PetscObjectStateGet((PetscObject)label, &mesh->depthState);
3998: PetscLogEventEnd(DMPLEX_Stratify, dm, 0, 0, 0);
3999: return 0;
4000: }
4002: PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
4003: {
4004: DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4005: PetscInt dim, depth, pheight, coneSize;
4008: DMGetDimension(dm, &dim);
4009: DMPlexGetDepth(dm, &depth);
4010: DMPlexGetConeSize(dm, p, &coneSize);
4011: pheight = depth - pdepth;
4012: if (depth <= 1) {
4013: switch (pdepth) {
4014: case 0:
4015: ct = DM_POLYTOPE_POINT;
4016: break;
4017: case 1:
4018: switch (coneSize) {
4019: case 2:
4020: ct = DM_POLYTOPE_SEGMENT;
4021: break;
4022: case 3:
4023: ct = DM_POLYTOPE_TRIANGLE;
4024: break;
4025: case 4:
4026: switch (dim) {
4027: case 2:
4028: ct = DM_POLYTOPE_QUADRILATERAL;
4029: break;
4030: case 3:
4031: ct = DM_POLYTOPE_TETRAHEDRON;
4032: break;
4033: default:
4034: break;
4035: }
4036: break;
4037: case 5:
4038: ct = DM_POLYTOPE_PYRAMID;
4039: break;
4040: case 6:
4041: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;
4042: break;
4043: case 8:
4044: ct = DM_POLYTOPE_HEXAHEDRON;
4045: break;
4046: default:
4047: break;
4048: }
4049: }
4050: } else {
4051: if (pdepth == 0) {
4052: ct = DM_POLYTOPE_POINT;
4053: } else if (pheight == 0) {
4054: switch (dim) {
4055: case 1:
4056: switch (coneSize) {
4057: case 2:
4058: ct = DM_POLYTOPE_SEGMENT;
4059: break;
4060: default:
4061: break;
4062: }
4063: break;
4064: case 2:
4065: switch (coneSize) {
4066: case 3:
4067: ct = DM_POLYTOPE_TRIANGLE;
4068: break;
4069: case 4:
4070: ct = DM_POLYTOPE_QUADRILATERAL;
4071: break;
4072: default:
4073: break;
4074: }
4075: break;
4076: case 3:
4077: switch (coneSize) {
4078: case 4:
4079: ct = DM_POLYTOPE_TETRAHEDRON;
4080: break;
4081: case 5: {
4082: const PetscInt *cone;
4083: PetscInt faceConeSize;
4085: DMPlexGetCone(dm, p, &cone);
4086: DMPlexGetConeSize(dm, cone[0], &faceConeSize);
4087: switch (faceConeSize) {
4088: case 3:
4089: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;
4090: break;
4091: case 4:
4092: ct = DM_POLYTOPE_PYRAMID;
4093: break;
4094: }
4095: } break;
4096: case 6:
4097: ct = DM_POLYTOPE_HEXAHEDRON;
4098: break;
4099: default:
4100: break;
4101: }
4102: break;
4103: default:
4104: break;
4105: }
4106: } else if (pheight > 0) {
4107: switch (coneSize) {
4108: case 2:
4109: ct = DM_POLYTOPE_SEGMENT;
4110: break;
4111: case 3:
4112: ct = DM_POLYTOPE_TRIANGLE;
4113: break;
4114: case 4:
4115: ct = DM_POLYTOPE_QUADRILATERAL;
4116: break;
4117: default:
4118: break;
4119: }
4120: }
4121: }
4122: *pt = ct;
4123: return 0;
4124: }
4126: /*@
4127: DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
4129: Collective on dm
4131: Input Parameter:
4132: . mesh - The `DMPLEX`
4134: Level: developer
4136: Note:
4137: This function is normally called automatically when a cell type is requested. It creates an
4138: internal `DMLabel` named "celltype" which can be directly accessed using `DMGetLabel()`. A user may disable
4139: automatic creation by creating the label manually, using `DMCreateLabel`(dm, "celltype").
4141: `DMPlexComputeCellTypes()` should be called after all calls to `DMPlexSymmetrize()` and `DMPlexStratify()`
4143: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSymmetrize()`, `DMPlexStratify()`, `DMGetLabel()`, `DMCreateLabel()`
4144: @*/
4145: PetscErrorCode DMPlexComputeCellTypes(DM dm)
4146: {
4147: DM_Plex *mesh;
4148: DMLabel ctLabel;
4149: PetscInt pStart, pEnd, p;
4152: mesh = (DM_Plex *)dm->data;
4153: DMCreateLabel(dm, "celltype");
4154: DMPlexGetCellTypeLabel(dm, &ctLabel);
4155: DMPlexGetChart(dm, &pStart, &pEnd);
4156: for (p = pStart; p < pEnd; ++p) {
4157: DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4158: PetscInt pdepth;
4160: DMPlexGetPointDepth(dm, p, &pdepth);
4161: DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);
4163: DMLabelSetValue(ctLabel, p, ct);
4164: }
4165: PetscObjectStateGet((PetscObject)ctLabel, &mesh->celltypeState);
4166: PetscObjectViewFromOptions((PetscObject)ctLabel, NULL, "-dm_plex_celltypes_view");
4167: return 0;
4168: }
4170: /*@C
4171: DMPlexGetJoin - Get an array for the join of the set of points
4173: Not Collective
4175: Input Parameters:
4176: + dm - The `DMPLEX` object
4177: . numPoints - The number of input points for the join
4178: - points - The input points
4180: Output Parameters:
4181: + numCoveredPoints - The number of points in the join
4182: - coveredPoints - The points in the join
4184: Level: intermediate
4186: Note:
4187: Currently, this is restricted to a single level join
4189: Fortran Note:
4190: The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.
4192: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreJoin()`, `DMPlexGetMeet()`
4193: @*/
4194: PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4195: {
4196: DM_Plex *mesh = (DM_Plex *)dm->data;
4197: PetscInt *join[2];
4198: PetscInt joinSize, i = 0;
4199: PetscInt dof, off, p, c, m;
4200: PetscInt maxSupportSize;
4206: PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize);
4207: DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[0]);
4208: DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[1]);
4209: /* Copy in support of first point */
4210: PetscSectionGetDof(mesh->supportSection, points[0], &dof);
4211: PetscSectionGetOffset(mesh->supportSection, points[0], &off);
4212: for (joinSize = 0; joinSize < dof; ++joinSize) join[i][joinSize] = mesh->supports[off + joinSize];
4213: /* Check each successive support */
4214: for (p = 1; p < numPoints; ++p) {
4215: PetscInt newJoinSize = 0;
4217: PetscSectionGetDof(mesh->supportSection, points[p], &dof);
4218: PetscSectionGetOffset(mesh->supportSection, points[p], &off);
4219: for (c = 0; c < dof; ++c) {
4220: const PetscInt point = mesh->supports[off + c];
4222: for (m = 0; m < joinSize; ++m) {
4223: if (point == join[i][m]) {
4224: join[1 - i][newJoinSize++] = point;
4225: break;
4226: }
4227: }
4228: }
4229: joinSize = newJoinSize;
4230: i = 1 - i;
4231: }
4232: *numCoveredPoints = joinSize;
4233: *coveredPoints = join[i];
4234: DMRestoreWorkArray(dm, maxSupportSize, MPIU_INT, &join[1 - i]);
4235: return 0;
4236: }
4238: /*@C
4239: DMPlexRestoreJoin - Restore an array for the join of the set of points
4241: Not Collective
4243: Input Parameters:
4244: + dm - The `DMPLEX` object
4245: . numPoints - The number of input points for the join
4246: - points - The input points
4248: Output Parameters:
4249: + numCoveredPoints - The number of points in the join
4250: - coveredPoints - The points in the join
4252: Level: intermediate
4254: Fortran Note:
4255: The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.
4257: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetJoin()`, `DMPlexGetFullJoin()`, `DMPlexGetMeet()`
4258: @*/
4259: PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4260: {
4265: DMRestoreWorkArray(dm, 0, MPIU_INT, (void *)coveredPoints);
4266: if (numCoveredPoints) *numCoveredPoints = 0;
4267: return 0;
4268: }
4270: /*@C
4271: DMPlexGetFullJoin - Get an array for the join of the set of points
4273: Not Collective
4275: Input Parameters:
4276: + dm - The `DMPLEX` object
4277: . numPoints - The number of input points for the join
4278: - points - The input points
4280: Output Parameters:
4281: + numCoveredPoints - The number of points in the join
4282: - coveredPoints - The points in the join
4284: Level: intermediate
4286: Fortran Note:
4287: The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.
4289: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetJoin()`, `DMPlexRestoreJoin()`, `DMPlexGetMeet()`
4290: @*/
4291: PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4292: {
4293: PetscInt *offsets, **closures;
4294: PetscInt *join[2];
4295: PetscInt depth = 0, maxSize, joinSize = 0, i = 0;
4296: PetscInt p, d, c, m, ms;
4303: DMPlexGetDepth(dm, &depth);
4304: PetscCalloc1(numPoints, &closures);
4305: DMGetWorkArray(dm, numPoints * (depth + 2), MPIU_INT, &offsets);
4306: DMPlexGetMaxSizes(dm, NULL, &ms);
4307: maxSize = (ms > 1) ? ((PetscPowInt(ms, depth + 1) - 1) / (ms - 1)) : depth + 1;
4308: DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);
4309: DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);
4311: for (p = 0; p < numPoints; ++p) {
4312: PetscInt closureSize;
4314: DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);
4316: offsets[p * (depth + 2) + 0] = 0;
4317: for (d = 0; d < depth + 1; ++d) {
4318: PetscInt pStart, pEnd, i;
4320: DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
4321: for (i = offsets[p * (depth + 2) + d]; i < closureSize; ++i) {
4322: if ((pStart > closures[p][i * 2]) || (pEnd <= closures[p][i * 2])) {
4323: offsets[p * (depth + 2) + d + 1] = i;
4324: break;
4325: }
4326: }
4327: if (i == closureSize) offsets[p * (depth + 2) + d + 1] = i;
4328: }
4330: }
4331: for (d = 0; d < depth + 1; ++d) {
4332: PetscInt dof;
4334: /* Copy in support of first point */
4335: dof = offsets[d + 1] - offsets[d];
4336: for (joinSize = 0; joinSize < dof; ++joinSize) join[i][joinSize] = closures[0][(offsets[d] + joinSize) * 2];
4337: /* Check each successive cone */
4338: for (p = 1; p < numPoints && joinSize; ++p) {
4339: PetscInt newJoinSize = 0;
4341: dof = offsets[p * (depth + 2) + d + 1] - offsets[p * (depth + 2) + d];
4342: for (c = 0; c < dof; ++c) {
4343: const PetscInt point = closures[p][(offsets[p * (depth + 2) + d] + c) * 2];
4345: for (m = 0; m < joinSize; ++m) {
4346: if (point == join[i][m]) {
4347: join[1 - i][newJoinSize++] = point;
4348: break;
4349: }
4350: }
4351: }
4352: joinSize = newJoinSize;
4353: i = 1 - i;
4354: }
4355: if (joinSize) break;
4356: }
4357: *numCoveredPoints = joinSize;
4358: *coveredPoints = join[i];
4359: for (p = 0; p < numPoints; ++p) DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);
4360: PetscFree(closures);
4361: DMRestoreWorkArray(dm, numPoints * (depth + 2), MPIU_INT, &offsets);
4362: DMRestoreWorkArray(dm, ms, MPIU_INT, &join[1 - i]);
4363: return 0;
4364: }
4366: /*@C
4367: DMPlexGetMeet - Get an array for the meet of the set of points
4369: Not Collective
4371: Input Parameters:
4372: + dm - The `DMPLEX` object
4373: . numPoints - The number of input points for the meet
4374: - points - The input points
4376: Output Parameters:
4377: + numCoveredPoints - The number of points in the meet
4378: - coveredPoints - The points in the meet
4380: Level: intermediate
4382: Note:
4383: Currently, this is restricted to a single level meet
4385: Fortran Notes:
4386: The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.
4388: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreMeet()`, `DMPlexGetJoin()`
4389: @*/
4390: PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
4391: {
4392: DM_Plex *mesh = (DM_Plex *)dm->data;
4393: PetscInt *meet[2];
4394: PetscInt meetSize, i = 0;
4395: PetscInt dof, off, p, c, m;
4396: PetscInt maxConeSize;
4402: PetscSectionGetMaxDof(mesh->coneSection, &maxConeSize);
4403: DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[0]);
4404: DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[1]);
4405: /* Copy in cone of first point */
4406: PetscSectionGetDof(mesh->coneSection, points[0], &dof);
4407: PetscSectionGetOffset(mesh->coneSection, points[0], &off);
4408: for (meetSize = 0; meetSize < dof; ++meetSize) meet[i][meetSize] = mesh->cones[off + meetSize];
4409: /* Check each successive cone */
4410: for (p = 1; p < numPoints; ++p) {
4411: PetscInt newMeetSize = 0;
4413: PetscSectionGetDof(mesh->coneSection, points[p], &dof);
4414: PetscSectionGetOffset(mesh->coneSection, points[p], &off);
4415: for (c = 0; c < dof; ++c) {
4416: const PetscInt point = mesh->cones[off + c];
4418: for (m = 0; m < meetSize; ++m) {
4419: if (point == meet[i][m]) {
4420: meet[1 - i][newMeetSize++] = point;
4421: break;
4422: }
4423: }
4424: }
4425: meetSize = newMeetSize;
4426: i = 1 - i;
4427: }
4428: *numCoveringPoints = meetSize;
4429: *coveringPoints = meet[i];
4430: DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &meet[1 - i]);
4431: return 0;
4432: }
4434: /*@C
4435: DMPlexRestoreMeet - Restore an array for the meet of the set of points
4437: Not Collective
4439: Input Parameters:
4440: + dm - The `DMPLEX` object
4441: . numPoints - The number of input points for the meet
4442: - points - The input points
4444: Output Parameters:
4445: + numCoveredPoints - The number of points in the meet
4446: - coveredPoints - The points in the meet
4448: Level: intermediate
4450: Fortran Note:
4451: The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.
4453: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetMeet()`, `DMPlexGetFullMeet()`, `DMPlexGetJoin()`
4454: @*/
4455: PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4456: {
4461: DMRestoreWorkArray(dm, 0, MPIU_INT, (void *)coveredPoints);
4462: if (numCoveredPoints) *numCoveredPoints = 0;
4463: return 0;
4464: }
4466: /*@C
4467: DMPlexGetFullMeet - Get an array for the meet of the set of points
4469: Not Collective
4471: Input Parameters:
4472: + dm - The `DMPLEX` object
4473: . numPoints - The number of input points for the meet
4474: - points - The input points
4476: Output Parameters:
4477: + numCoveredPoints - The number of points in the meet
4478: - coveredPoints - The points in the meet
4480: Level: intermediate
4482: Fortran Note:
4483: The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.
4485: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetMeet()`, `DMPlexRestoreMeet()`, `DMPlexGetJoin()`
4486: @*/
4487: PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4488: {
4489: PetscInt *offsets, **closures;
4490: PetscInt *meet[2];
4491: PetscInt height = 0, maxSize, meetSize = 0, i = 0;
4492: PetscInt p, h, c, m, mc;
4499: DMPlexGetDepth(dm, &height);
4500: PetscMalloc1(numPoints, &closures);
4501: DMGetWorkArray(dm, numPoints * (height + 2), MPIU_INT, &offsets);
4502: DMPlexGetMaxSizes(dm, &mc, NULL);
4503: maxSize = (mc > 1) ? ((PetscPowInt(mc, height + 1) - 1) / (mc - 1)) : height + 1;
4504: DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);
4505: DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);
4507: for (p = 0; p < numPoints; ++p) {
4508: PetscInt closureSize;
4510: DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);
4512: offsets[p * (height + 2) + 0] = 0;
4513: for (h = 0; h < height + 1; ++h) {
4514: PetscInt pStart, pEnd, i;
4516: DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);
4517: for (i = offsets[p * (height + 2) + h]; i < closureSize; ++i) {
4518: if ((pStart > closures[p][i * 2]) || (pEnd <= closures[p][i * 2])) {
4519: offsets[p * (height + 2) + h + 1] = i;
4520: break;
4521: }
4522: }
4523: if (i == closureSize) offsets[p * (height + 2) + h + 1] = i;
4524: }
4526: }
4527: for (h = 0; h < height + 1; ++h) {
4528: PetscInt dof;
4530: /* Copy in cone of first point */
4531: dof = offsets[h + 1] - offsets[h];
4532: for (meetSize = 0; meetSize < dof; ++meetSize) meet[i][meetSize] = closures[0][(offsets[h] + meetSize) * 2];
4533: /* Check each successive cone */
4534: for (p = 1; p < numPoints && meetSize; ++p) {
4535: PetscInt newMeetSize = 0;
4537: dof = offsets[p * (height + 2) + h + 1] - offsets[p * (height + 2) + h];
4538: for (c = 0; c < dof; ++c) {
4539: const PetscInt point = closures[p][(offsets[p * (height + 2) + h] + c) * 2];
4541: for (m = 0; m < meetSize; ++m) {
4542: if (point == meet[i][m]) {
4543: meet[1 - i][newMeetSize++] = point;
4544: break;
4545: }
4546: }
4547: }
4548: meetSize = newMeetSize;
4549: i = 1 - i;
4550: }
4551: if (meetSize) break;
4552: }
4553: *numCoveredPoints = meetSize;
4554: *coveredPoints = meet[i];
4555: for (p = 0; p < numPoints; ++p) DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);
4556: PetscFree(closures);
4557: DMRestoreWorkArray(dm, numPoints * (height + 2), MPIU_INT, &offsets);
4558: DMRestoreWorkArray(dm, mc, MPIU_INT, &meet[1 - i]);
4559: return 0;
4560: }
4562: /*@C
4563: DMPlexEqual - Determine if two `DM` have the same topology
4565: Not Collective
4567: Input Parameters:
4568: + dmA - A `DMPLEX` object
4569: - dmB - A `DMPLEX` object
4571: Output Parameters:
4572: . equal - `PETSC_TRUE` if the topologies are identical
4574: Level: intermediate
4576: Note:
4577: We are not solving graph isomorphism, so we do not permute.
4579: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
4580: @*/
4581: PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
4582: {
4583: PetscInt depth, depthB, pStart, pEnd, pStartB, pEndB, p;
4589: *equal = PETSC_FALSE;
4590: DMPlexGetDepth(dmA, &depth);
4591: DMPlexGetDepth(dmB, &depthB);
4592: if (depth != depthB) return 0;
4593: DMPlexGetChart(dmA, &pStart, &pEnd);
4594: DMPlexGetChart(dmB, &pStartB, &pEndB);
4595: if ((pStart != pStartB) || (pEnd != pEndB)) return 0;
4596: for (p = pStart; p < pEnd; ++p) {
4597: const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
4598: PetscInt coneSize, coneSizeB, c, supportSize, supportSizeB, s;
4600: DMPlexGetConeSize(dmA, p, &coneSize);
4601: DMPlexGetCone(dmA, p, &cone);
4602: DMPlexGetConeOrientation(dmA, p, &ornt);
4603: DMPlexGetConeSize(dmB, p, &coneSizeB);
4604: DMPlexGetCone(dmB, p, &coneB);
4605: DMPlexGetConeOrientation(dmB, p, &orntB);
4606: if (coneSize != coneSizeB) return 0;
4607: for (c = 0; c < coneSize; ++c) {
4608: if (cone[c] != coneB[c]) return 0;
4609: if (ornt[c] != orntB[c]) return 0;
4610: }
4611: DMPlexGetSupportSize(dmA, p, &supportSize);
4612: DMPlexGetSupport(dmA, p, &support);
4613: DMPlexGetSupportSize(dmB, p, &supportSizeB);
4614: DMPlexGetSupport(dmB, p, &supportB);
4615: if (supportSize != supportSizeB) return 0;
4616: for (s = 0; s < supportSize; ++s) {
4617: if (support[s] != supportB[s]) return 0;
4618: }
4619: }
4620: *equal = PETSC_TRUE;
4621: return 0;
4622: }
4624: /*@C
4625: DMPlexGetNumFaceVertices - Returns the number of vertices on a face
4627: Not Collective
4629: Input Parameters:
4630: + dm - The `DMPLEX`
4631: . cellDim - The cell dimension
4632: - numCorners - The number of vertices on a cell
4634: Output Parameters:
4635: . numFaceVertices - The number of vertices on a face
4637: Level: developer
4639: Note:
4640: Of course this can only work for a restricted set of symmetric shapes
4642: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
4643: @*/
4644: PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
4645: {
4646: MPI_Comm comm;
4648: PetscObjectGetComm((PetscObject)dm, &comm);
4650: switch (cellDim) {
4651: case 0:
4652: *numFaceVertices = 0;
4653: break;
4654: case 1:
4655: *numFaceVertices = 1;
4656: break;
4657: case 2:
4658: switch (numCorners) {
4659: case 3: /* triangle */
4660: *numFaceVertices = 2; /* Edge has 2 vertices */
4661: break;
4662: case 4: /* quadrilateral */
4663: *numFaceVertices = 2; /* Edge has 2 vertices */
4664: break;
4665: case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
4666: *numFaceVertices = 3; /* Edge has 3 vertices */
4667: break;
4668: case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
4669: *numFaceVertices = 3; /* Edge has 3 vertices */
4670: break;
4671: default:
4672: SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %" PetscInt_FMT " for dimension %" PetscInt_FMT, numCorners, cellDim);
4673: }
4674: break;
4675: case 3:
4676: switch (numCorners) {
4677: case 4: /* tetradehdron */
4678: *numFaceVertices = 3; /* Face has 3 vertices */
4679: break;
4680: case 6: /* tet cohesive cells */
4681: *numFaceVertices = 4; /* Face has 4 vertices */
4682: break;
4683: case 8: /* hexahedron */
4684: *numFaceVertices = 4; /* Face has 4 vertices */
4685: break;
4686: case 9: /* tet cohesive Lagrange cells */
4687: *numFaceVertices = 6; /* Face has 6 vertices */
4688: break;
4689: case 10: /* quadratic tetrahedron */
4690: *numFaceVertices = 6; /* Face has 6 vertices */
4691: break;
4692: case 12: /* hex cohesive Lagrange cells */
4693: *numFaceVertices = 6; /* Face has 6 vertices */
4694: break;
4695: case 18: /* quadratic tet cohesive Lagrange cells */
4696: *numFaceVertices = 6; /* Face has 6 vertices */
4697: break;
4698: case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
4699: *numFaceVertices = 9; /* Face has 9 vertices */
4700: break;
4701: default:
4702: SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %" PetscInt_FMT " for dimension %" PetscInt_FMT, numCorners, cellDim);
4703: }
4704: break;
4705: default:
4706: SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %" PetscInt_FMT, cellDim);
4707: }
4708: return 0;
4709: }
4711: /*@
4712: DMPlexGetDepthLabel - Get the `DMLabel` recording the depth of each point
4714: Not Collective
4716: Input Parameter:
4717: . dm - The `DMPLEX` object
4719: Output Parameter:
4720: . depthLabel - The `DMLabel` recording point depth
4722: Level: developer
4724: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepth()`, `DMPlexGetHeightStratum()`, `DMPlexGetDepthStratum()`, `DMPlexGetPointDepth()`,
4725: @*/
4726: PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
4727: {
4730: *depthLabel = dm->depthLabel;
4731: return 0;
4732: }
4734: /*@
4735: DMPlexGetDepth - Get the depth of the DAG representing this mesh
4737: Not Collective
4739: Input Parameter:
4740: . dm - The `DMPLEX` object
4742: Output Parameter:
4743: . depth - The number of strata (breadth first levels) in the DAG
4745: Level: developer
4747: Notes:
4748: This returns maximum of point depths over all points, i.e. maximum value of the label returned by `DMPlexGetDepthLabel()`.
4750: The point depth is described more in detail in `DMPlexGetDepthStratum()`.
4752: An empty mesh gives -1.
4754: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepthLabel()`, `DMPlexGetDepthStratum()`, `DMPlexGetPointDepth()`, `DMPlexSymmetrize()`
4755: @*/
4756: PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
4757: {
4758: DMLabel label;
4759: PetscInt d = 0;
4763: DMPlexGetDepthLabel(dm, &label);
4764: if (label) DMLabelGetNumValues(label, &d);
4765: *depth = d - 1;
4766: return 0;
4767: }
4769: /*@
4770: DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
4772: Not Collective
4774: Input Parameters:
4775: + dm - The `DMPLEX` object
4776: - depth - The requested depth
4778: Output Parameters:
4779: + start - The first point at this depth
4780: - end - One beyond the last point at this depth
4782: Level: developer
4784: Notes:
4785: Depth indexing is related to topological dimension. Depth stratum 0 contains the lowest topological dimension points,
4786: often "vertices". If the mesh is "interpolated" (see `DMPlexInterpolate()`), then depth stratum 1 contains the next
4787: higher dimension, e.g., "edges".
4789: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetHeightStratum()`, `DMPlexGetDepth()`, `DMPlexGetDepthLabel()`, `DMPlexGetPointDepth()`, `DMPlexSymmetrize()`, `DMPlexInterpolate()`
4790: @*/
4791: PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt depth, PetscInt *start, PetscInt *end)
4792: {
4793: DMLabel label;
4794: PetscInt pStart, pEnd;
4797: if (start) {
4799: *start = 0;
4800: }
4801: if (end) {
4803: *end = 0;
4804: }
4805: DMPlexGetChart(dm, &pStart, &pEnd);
4806: if (pStart == pEnd) return 0;
4807: if (depth < 0) {
4808: if (start) *start = pStart;
4809: if (end) *end = pEnd;
4810: return 0;
4811: }
4812: DMPlexGetDepthLabel(dm, &label);
4814: DMLabelGetStratumBounds(label, depth, start, end);
4815: return 0;
4816: }
4818: /*@
4819: DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
4821: Not Collective
4823: Input Parameters:
4824: + dm - The `DMPLEX` object
4825: - height - The requested height
4827: Output Parameters:
4828: + start - The first point at this height
4829: - end - One beyond the last point at this height
4831: Level: developer
4833: Notes:
4834: Height indexing is related to topological codimension. Height stratum 0 contains the highest topological dimension
4835: points, often called "cells" or "elements". If the mesh is "interpolated" (see `DMPlexInterpolate()`), then height
4836: stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
4838: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetDepth()`, `DMPlexGetPointHeight()`
4839: @*/
4840: PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt height, PetscInt *start, PetscInt *end)
4841: {
4842: DMLabel label;
4843: PetscInt depth, pStart, pEnd;
4846: if (start) {
4848: *start = 0;
4849: }
4850: if (end) {
4852: *end = 0;
4853: }
4854: DMPlexGetChart(dm, &pStart, &pEnd);
4855: if (pStart == pEnd) return 0;
4856: if (height < 0) {
4857: if (start) *start = pStart;
4858: if (end) *end = pEnd;
4859: return 0;
4860: }
4861: DMPlexGetDepthLabel(dm, &label);
4863: DMLabelGetNumValues(label, &depth);
4864: DMLabelGetStratumBounds(label, depth - 1 - height, start, end);
4865: return 0;
4866: }
4868: /*@
4869: DMPlexGetPointDepth - Get the depth of a given point
4871: Not Collective
4873: Input Parameters:
4874: + dm - The `DMPLEX` object
4875: - point - The point
4877: Output Parameter:
4878: . depth - The depth of the point
4880: Level: intermediate
4882: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexGetPointHeight()`
4883: @*/
4884: PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
4885: {
4888: DMLabelGetValue(dm->depthLabel, point, depth);
4889: return 0;
4890: }
4892: /*@
4893: DMPlexGetPointHeight - Get the height of a given point
4895: Not Collective
4897: Input Parameters:
4898: + dm - The `DMPLEX` object
4899: - point - The point
4901: Output Parameter:
4902: . height - The height of the point
4904: Level: intermediate
4906: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexGetPointDepth()`
4907: @*/
4908: PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
4909: {
4910: PetscInt n, pDepth;
4914: DMLabelGetNumValues(dm->depthLabel, &n);
4915: DMLabelGetValue(dm->depthLabel, point, &pDepth);
4916: *height = n - 1 - pDepth; /* DAG depth is n-1 */
4917: return 0;
4918: }
4920: /*@
4921: DMPlexGetCellTypeLabel - Get the `DMLabel` recording the polytope type of each cell
4923: Not Collective
4925: Input Parameter:
4926: . dm - The `DMPLEX` object
4928: Output Parameter:
4929: . celltypeLabel - The `DMLabel` recording cell polytope type
4931: Level: developer
4933: Note:
4934: This function will trigger automatica computation of cell types. This can be disabled by calling
4935: `DMCreateLabel`(dm, "celltype") beforehand.
4937: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMCreateLabel()`
4938: @*/
4939: PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
4940: {
4943: if (!dm->celltypeLabel) DMPlexComputeCellTypes(dm);
4944: *celltypeLabel = dm->celltypeLabel;
4945: return 0;
4946: }
4948: /*@
4949: DMPlexGetCellType - Get the polytope type of a given cell
4951: Not Collective
4953: Input Parameters:
4954: + dm - The `DMPLEX` object
4955: - cell - The cell
4957: Output Parameter:
4958: . celltype - The polytope type of the cell
4960: Level: intermediate
4962: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellTypeLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`
4963: @*/
4964: PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
4965: {
4966: DMLabel label;
4967: PetscInt ct;
4971: DMPlexGetCellTypeLabel(dm, &label);
4972: DMLabelGetValue(label, cell, &ct);
4974: *celltype = (DMPolytopeType)ct;
4975: return 0;
4976: }
4978: /*@
4979: DMPlexSetCellType - Set the polytope type of a given cell
4981: Not Collective
4983: Input Parameters:
4984: + dm - The `DMPLEX` object
4985: . cell - The cell
4986: - celltype - The polytope type of the cell
4988: Level: advanced
4990: Note:
4991: By default, cell types will be automatically computed using `DMPlexComputeCellTypes()` before this function
4992: is executed. This function will override the computed type. However, if automatic classification will not succeed
4993: and a user wants to manually specify all types, the classification must be disabled by calling
4994: DMCreaateLabel(dm, "celltype") before getting or setting any cell types.
4996: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellTypeLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexComputeCellTypes()`, `DMCreateLabel()`
4997: @*/
4998: PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
4999: {
5000: DMLabel label;
5003: DMPlexGetCellTypeLabel(dm, &label);
5004: DMLabelSetValue(label, cell, celltype);
5005: return 0;
5006: }
5008: PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
5009: {
5010: PetscSection section, s;
5011: Mat m;
5012: PetscInt maxHeight;
5014: DMClone(dm, cdm);
5015: DMPlexGetMaxProjectionHeight(dm, &maxHeight);
5016: DMPlexSetMaxProjectionHeight(*cdm, maxHeight);
5017: PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ion);
5018: DMSetLocalSection(*cdm, section);
5019: PetscSectionDestroy(§ion);
5020: PetscSectionCreate(PETSC_COMM_SELF, &s);
5021: MatCreate(PETSC_COMM_SELF, &m);
5022: DMSetDefaultConstraints(*cdm, s, m, NULL);
5023: PetscSectionDestroy(&s);
5024: MatDestroy(&m);
5026: DMSetNumFields(*cdm, 1);
5027: DMCreateDS(*cdm);
5028: return 0;
5029: }
5031: PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
5032: {
5033: Vec coordsLocal, cellCoordsLocal;
5034: DM coordsDM, cellCoordsDM;
5036: *field = NULL;
5037: DMGetCoordinatesLocal(dm, &coordsLocal);
5038: DMGetCoordinateDM(dm, &coordsDM);
5039: DMGetCellCoordinatesLocal(dm, &cellCoordsLocal);
5040: DMGetCellCoordinateDM(dm, &cellCoordsDM);
5041: if (coordsLocal && coordsDM) {
5042: if (cellCoordsLocal && cellCoordsDM) DMFieldCreateDSWithDG(coordsDM, cellCoordsDM, 0, coordsLocal, cellCoordsLocal, field);
5043: else DMFieldCreateDS(coordsDM, 0, coordsLocal, field);
5044: }
5045: return 0;
5046: }
5048: /*@C
5049: DMPlexGetConeSection - Return a section which describes the layout of cone data
5051: Not Collective
5053: Input Parameters:
5054: . dm - The `DMPLEX` object
5056: Output Parameter:
5057: . section - The `PetscSection` object
5059: Level: developer
5061: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetSupportSection()`, `DMPlexGetCones()`, `DMPlexGetConeOrientations()`, `PetscSection`
5062: @*/
5063: PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
5064: {
5065: DM_Plex *mesh = (DM_Plex *)dm->data;
5068: if (section) *section = mesh->coneSection;
5069: return 0;
5070: }
5072: /*@C
5073: DMPlexGetSupportSection - Return a section which describes the layout of support data
5075: Not Collective
5077: Input Parameters:
5078: . dm - The `DMPLEX` object
5080: Output Parameter:
5081: . section - The `PetscSection` object
5083: Level: developer
5085: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`, `PetscSection`
5086: @*/
5087: PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
5088: {
5089: DM_Plex *mesh = (DM_Plex *)dm->data;
5092: if (section) *section = mesh->supportSection;
5093: return 0;
5094: }
5096: /*@C
5097: DMPlexGetCones - Return cone data
5099: Not Collective
5101: Input Parameters:
5102: . dm - The `DMPLEX` object
5104: Output Parameter:
5105: . cones - The cone for each point
5107: Level: developer
5109: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`
5110: @*/
5111: PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
5112: {
5113: DM_Plex *mesh = (DM_Plex *)dm->data;
5116: if (cones) *cones = mesh->cones;
5117: return 0;
5118: }
5120: /*@C
5121: DMPlexGetConeOrientations - Return cone orientation data
5123: Not Collective
5125: Input Parameters:
5126: . dm - The `DMPLEX` object
5128: Output Parameter:
5129: . coneOrientations - The array of cone orientations for all points
5131: Level: developer
5133: Notes:
5134: The `PetscSection` returned by `DMPlexGetConeSection()` partitions coneOrientations into cone orientations of particular points as returned by `DMPlexGetConeOrientation()`.
5136: The meaning of coneOrientations values is detailed in `DMPlexGetConeOrientation()`.
5138: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`, `DMPlexGetConeOrientation()`, `PetscSection`
5139: @*/
5140: PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
5141: {
5142: DM_Plex *mesh = (DM_Plex *)dm->data;
5145: if (coneOrientations) *coneOrientations = mesh->coneOrientations;
5146: return 0;
5147: }
5149: /******************************** FEM Support **********************************/
5151: /*
5152: Returns number of components and tensor degree for the field. For interpolated meshes, line should be a point
5153: representing a line in the section.
5154: */
5155: static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section, PetscInt field, PetscInt line, PetscBool vertexchart, PetscInt *Nc, PetscInt *k)
5156: {
5158: PetscSectionGetFieldComponents(section, field, Nc);
5159: if (line < 0) {
5160: *k = 0;
5161: *Nc = 0;
5162: } else if (vertexchart) { /* If we only have a vertex chart, we must have degree k=1 */
5163: *k = 1;
5164: } else { /* Assume the full interpolated mesh is in the chart; lines in particular */
5165: /* An order k SEM disc has k-1 dofs on an edge */
5166: PetscSectionGetFieldDof(section, line, field, k);
5167: *k = *k / *Nc + 1;
5168: }
5169: return 0;
5170: }
5172: /*@
5174: DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
5175: lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
5176: section provided (or the section of the DM).
5178: Input Parameters:
5179: + dm - The DM
5180: . point - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
5181: - section - The PetscSection to reorder, or NULL for the default section
5183: Example:
5184: A typical interpolated single-quad mesh might order points as
5185: .vb
5186: [c0, v1, v2, v3, v4, e5, e6, e7, e8]
5188: v4 -- e6 -- v3
5189: | |
5190: e7 c0 e8
5191: | |
5192: v1 -- e5 -- v2
5193: .ve
5195: (There is no significance to the ordering described here.) The default section for a Q3 quad might typically assign
5196: dofs in the order of points, e.g.,
5197: .vb
5198: c0 -> [0,1,2,3]
5199: v1 -> [4]
5200: ...
5201: e5 -> [8, 9]
5202: .ve
5204: which corresponds to the dofs
5205: .vb
5206: 6 10 11 7
5207: 13 2 3 15
5208: 12 0 1 14
5209: 4 8 9 5
5210: .ve
5212: The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
5213: .vb
5214: 0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
5215: .ve
5217: After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
5218: .vb
5219: 4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
5220: .ve
5222: Level: developer
5224: Note:
5225: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
5226: degree of the basis.
5228: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMGetLocalSection()`, `PetscSectionSetClosurePermutation()`, `DMSetGlobalSection()`
5229: @*/
5230: PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
5231: {
5232: DMLabel label;
5233: PetscInt dim, depth = -1, eStart = -1, Nf;
5234: PetscBool vertexchart;
5236: DMGetDimension(dm, &dim);
5237: if (dim < 1) return 0;
5238: if (point < 0) {
5239: PetscInt sStart, sEnd;
5241: DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);
5242: point = sEnd - sStart ? sStart : point;
5243: }
5244: DMPlexGetDepthLabel(dm, &label);
5245: if (point >= 0) DMLabelGetValue(label, point, &depth);
5246: if (!section) DMGetLocalSection(dm, §ion);
5247: if (depth == 1) {
5248: eStart = point;
5249: } else if (depth == dim) {
5250: const PetscInt *cone;
5252: DMPlexGetCone(dm, point, &cone);
5253: if (dim == 2) eStart = cone[0];
5254: else if (dim == 3) {
5255: const PetscInt *cone2;
5256: DMPlexGetCone(dm, cone[0], &cone2);
5257: eStart = cone2[0];
5258: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " of depth %" PetscInt_FMT " cannot be used to bootstrap spectral ordering for dim %" PetscInt_FMT, point, depth, dim);
5260: { /* Determine whether the chart covers all points or just vertices. */
5261: PetscInt pStart, pEnd, cStart, cEnd;
5262: DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);
5263: PetscSectionGetChart(section, &cStart, &cEnd);
5264: if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Only vertices are in the chart */
5265: else if (cStart <= point && point < cEnd) vertexchart = PETSC_FALSE; /* Some interpolated points exist in the chart */
5266: else vertexchart = PETSC_TRUE; /* Some interpolated points are not in chart; assume dofs only at cells and vertices */
5267: }
5268: PetscSectionGetNumFields(section, &Nf);
5269: for (PetscInt d = 1; d <= dim; d++) {
5270: PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
5271: PetscInt *perm;
5273: for (f = 0; f < Nf; ++f) {
5274: PetscSectionFieldGetTensorDegree_Private(section, f, eStart, vertexchart, &Nc, &k);
5275: size += PetscPowInt(k + 1, d) * Nc;
5276: }
5277: PetscMalloc1(size, &perm);
5278: for (f = 0; f < Nf; ++f) {
5279: switch (d) {
5280: case 1:
5281: PetscSectionFieldGetTensorDegree_Private(section, f, eStart, vertexchart, &Nc, &k);
5282: /*
5283: Original ordering is [ edge of length k-1; vtx0; vtx1 ]
5284: We want [ vtx0; edge of length k-1; vtx1 ]
5285: */
5286: for (c = 0; c < Nc; c++, offset++) perm[offset] = (k - 1) * Nc + c + foffset;
5287: for (i = 0; i < k - 1; i++)
5288: for (c = 0; c < Nc; c++, offset++) perm[offset] = i * Nc + c + foffset;
5289: for (c = 0; c < Nc; c++, offset++) perm[offset] = k * Nc + c + foffset;
5290: foffset = offset;
5291: break;
5292: case 2:
5293: /* The original quad closure is oriented clockwise, {f, e_b, e_r, e_t, e_l, v_lb, v_rb, v_tr, v_tl} */
5294: PetscSectionFieldGetTensorDegree_Private(section, f, eStart, vertexchart, &Nc, &k);
5295: /* The SEM order is
5297: v_lb, {e_b}, v_rb,
5298: e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
5299: v_lt, reverse {e_t}, v_rt
5300: */
5301: {
5302: const PetscInt of = 0;
5303: const PetscInt oeb = of + PetscSqr(k - 1);
5304: const PetscInt oer = oeb + (k - 1);
5305: const PetscInt oet = oer + (k - 1);
5306: const PetscInt oel = oet + (k - 1);
5307: const PetscInt ovlb = oel + (k - 1);
5308: const PetscInt ovrb = ovlb + 1;
5309: const PetscInt ovrt = ovrb + 1;
5310: const PetscInt ovlt = ovrt + 1;
5311: PetscInt o;
5313: /* bottom */
5314: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb * Nc + c + foffset;
5315: for (o = oeb; o < oer; ++o)
5316: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5317: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb * Nc + c + foffset;
5318: /* middle */
5319: for (i = 0; i < k - 1; ++i) {
5320: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel + (k - 2) - i) * Nc + c + foffset;
5321: for (o = of + (k - 1) * i; o < of + (k - 1) * (i + 1); ++o)
5322: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5323: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer + i) * Nc + c + foffset;
5324: }
5325: /* top */
5326: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt * Nc + c + foffset;
5327: for (o = oel - 1; o >= oet; --o)
5328: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5329: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt * Nc + c + foffset;
5330: foffset = offset;
5331: }
5332: break;
5333: case 3:
5334: /* The original hex closure is
5336: {c,
5337: f_b, f_t, f_f, f_b, f_r, f_l,
5338: e_bl, e_bb, e_br, e_bf, e_tf, e_tr, e_tb, e_tl, e_rf, e_lf, e_lb, e_rb,
5339: v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
5340: */
5341: PetscSectionFieldGetTensorDegree_Private(section, f, eStart, vertexchart, &Nc, &k);
5342: /* The SEM order is
5343: Bottom Slice
5344: v_blf, {e^{(k-1)-n}_bf}, v_brf,
5345: e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
5346: v_blb, {e_bb}, v_brb,
5348: Middle Slice (j)
5349: {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
5350: f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
5351: e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
5353: Top Slice
5354: v_tlf, {e_tf}, v_trf,
5355: e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
5356: v_tlb, {e^{(k-1)-n}_tb}, v_trb,
5357: */
5358: {
5359: const PetscInt oc = 0;
5360: const PetscInt ofb = oc + PetscSqr(k - 1) * (k - 1);
5361: const PetscInt oft = ofb + PetscSqr(k - 1);
5362: const PetscInt off = oft + PetscSqr(k - 1);
5363: const PetscInt ofk = off + PetscSqr(k - 1);
5364: const PetscInt ofr = ofk + PetscSqr(k - 1);
5365: const PetscInt ofl = ofr + PetscSqr(k - 1);
5366: const PetscInt oebl = ofl + PetscSqr(k - 1);
5367: const PetscInt oebb = oebl + (k - 1);
5368: const PetscInt oebr = oebb + (k - 1);
5369: const PetscInt oebf = oebr + (k - 1);
5370: const PetscInt oetf = oebf + (k - 1);
5371: const PetscInt oetr = oetf + (k - 1);
5372: const PetscInt oetb = oetr + (k - 1);
5373: const PetscInt oetl = oetb + (k - 1);
5374: const PetscInt oerf = oetl + (k - 1);
5375: const PetscInt oelf = oerf + (k - 1);
5376: const PetscInt oelb = oelf + (k - 1);
5377: const PetscInt oerb = oelb + (k - 1);
5378: const PetscInt ovblf = oerb + (k - 1);
5379: const PetscInt ovblb = ovblf + 1;
5380: const PetscInt ovbrb = ovblb + 1;
5381: const PetscInt ovbrf = ovbrb + 1;
5382: const PetscInt ovtlf = ovbrf + 1;
5383: const PetscInt ovtrf = ovtlf + 1;
5384: const PetscInt ovtrb = ovtrf + 1;
5385: const PetscInt ovtlb = ovtrb + 1;
5386: PetscInt o, n;
5388: /* Bottom Slice */
5389: /* bottom */
5390: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf * Nc + c + foffset;
5391: for (o = oetf - 1; o >= oebf; --o)
5392: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5393: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf * Nc + c + foffset;
5394: /* middle */
5395: for (i = 0; i < k - 1; ++i) {
5396: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl + i) * Nc + c + foffset;
5397: for (n = 0; n < k - 1; ++n) {
5398: o = ofb + n * (k - 1) + i;
5399: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5400: }
5401: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr + (k - 2) - i) * Nc + c + foffset;
5402: }
5403: /* top */
5404: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb * Nc + c + foffset;
5405: for (o = oebb; o < oebr; ++o)
5406: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5407: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb * Nc + c + foffset;
5409: /* Middle Slice */
5410: for (j = 0; j < k - 1; ++j) {
5411: /* bottom */
5412: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf + (k - 2) - j) * Nc + c + foffset;
5413: for (o = off + j * (k - 1); o < off + (j + 1) * (k - 1); ++o)
5414: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5415: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf + j) * Nc + c + foffset;
5416: /* middle */
5417: for (i = 0; i < k - 1; ++i) {
5418: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl + i * (k - 1) + j) * Nc + c + foffset;
5419: for (n = 0; n < k - 1; ++n)
5420: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oc + (j * (k - 1) + i) * (k - 1) + n) * Nc + c + foffset;
5421: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr + j * (k - 1) + i) * Nc + c + foffset;
5422: }
5423: /* top */
5424: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb + j) * Nc + c + foffset;
5425: for (o = ofk + j * (k - 1) + (k - 2); o >= ofk + j * (k - 1); --o)
5426: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5427: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb + (k - 2) - j) * Nc + c + foffset;
5428: }
5430: /* Top Slice */
5431: /* bottom */
5432: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf * Nc + c + foffset;
5433: for (o = oetf; o < oetr; ++o)
5434: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5435: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf * Nc + c + foffset;
5436: /* middle */
5437: for (i = 0; i < k - 1; ++i) {
5438: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl + (k - 2) - i) * Nc + c + foffset;
5439: for (n = 0; n < k - 1; ++n)
5440: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oft + i * (k - 1) + n) * Nc + c + foffset;
5441: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr + i) * Nc + c + foffset;
5442: }
5443: /* top */
5444: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb * Nc + c + foffset;
5445: for (o = oetl - 1; o >= oetb; --o)
5446: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5447: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb * Nc + c + foffset;
5449: foffset = offset;
5450: }
5451: break;
5452: default:
5453: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %" PetscInt_FMT, d);
5454: }
5455: }
5457: /* Check permutation */
5458: {
5459: PetscInt *check;
5461: PetscMalloc1(size, &check);
5462: for (i = 0; i < size; ++i) {
5463: check[i] = -1;
5465: }
5466: for (i = 0; i < size; ++i) check[perm[i]] = i;
5468: PetscFree(check);
5469: }
5470: PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size, PETSC_OWN_POINTER, perm);
5471: if (d == dim) { // Add permutation for localized (in case this is a coordinate DM)
5472: PetscInt *loc_perm;
5473: PetscMalloc1(size * 2, &loc_perm);
5474: for (PetscInt i = 0; i < size; i++) {
5475: loc_perm[i] = perm[i];
5476: loc_perm[size + i] = size + perm[i];
5477: }
5478: PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size * 2, PETSC_OWN_POINTER, loc_perm);
5479: }
5480: }
5481: return 0;
5482: }
5484: PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
5485: {
5486: PetscDS prob;
5487: PetscInt depth, Nf, h;
5488: DMLabel label;
5491: DMGetDS(dm, &prob);
5492: Nf = prob->Nf;
5493: label = dm->depthLabel;
5494: *dspace = NULL;
5495: if (field < Nf) {
5496: PetscObject disc = prob->disc[field];
5498: if (disc->classid == PETSCFE_CLASSID) {
5499: PetscDualSpace dsp;
5501: PetscFEGetDualSpace((PetscFE)disc, &dsp);
5502: DMLabelGetNumValues(label, &depth);
5503: DMLabelGetValue(label, point, &h);
5504: h = depth - 1 - h;
5505: if (h) {
5506: PetscDualSpaceGetHeightSubspace(dsp, h, dspace);
5507: } else {
5508: *dspace = dsp;
5509: }
5510: }
5511: }
5512: return 0;
5513: }
5515: static inline PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5516: {
5517: PetscScalar *array;
5518: const PetscScalar *vArray;
5519: const PetscInt *cone, *coneO;
5520: PetscInt pStart, pEnd, p, numPoints, size = 0, offset = 0;
5523: PetscSectionGetChart(section, &pStart, &pEnd);
5524: DMPlexGetConeSize(dm, point, &numPoints);
5525: DMPlexGetCone(dm, point, &cone);
5526: DMPlexGetConeOrientation(dm, point, &coneO);
5527: if (!values || !*values) {
5528: if ((point >= pStart) && (point < pEnd)) {
5529: PetscInt dof;
5531: PetscSectionGetDof(section, point, &dof);
5532: size += dof;
5533: }
5534: for (p = 0; p < numPoints; ++p) {
5535: const PetscInt cp = cone[p];
5536: PetscInt dof;
5538: if ((cp < pStart) || (cp >= pEnd)) continue;
5539: PetscSectionGetDof(section, cp, &dof);
5540: size += dof;
5541: }
5542: if (!values) {
5543: if (csize) *csize = size;
5544: return 0;
5545: }
5546: DMGetWorkArray(dm, size, MPIU_SCALAR, &array);
5547: } else {
5548: array = *values;
5549: }
5550: size = 0;
5551: VecGetArrayRead(v, &vArray);
5552: if ((point >= pStart) && (point < pEnd)) {
5553: PetscInt dof, off, d;
5554: const PetscScalar *varr;
5556: PetscSectionGetDof(section, point, &dof);
5557: PetscSectionGetOffset(section, point, &off);
5558: varr = &vArray[off];
5559: for (d = 0; d < dof; ++d, ++offset) array[offset] = varr[d];
5560: size += dof;
5561: }
5562: for (p = 0; p < numPoints; ++p) {
5563: const PetscInt cp = cone[p];
5564: PetscInt o = coneO[p];
5565: PetscInt dof, off, d;
5566: const PetscScalar *varr;
5568: if ((cp < pStart) || (cp >= pEnd)) continue;
5569: PetscSectionGetDof(section, cp, &dof);
5570: PetscSectionGetOffset(section, cp, &off);
5571: varr = &vArray[off];
5572: if (o >= 0) {
5573: for (d = 0; d < dof; ++d, ++offset) array[offset] = varr[d];
5574: } else {
5575: for (d = dof - 1; d >= 0; --d, ++offset) array[offset] = varr[d];
5576: }
5577: size += dof;
5578: }
5579: VecRestoreArrayRead(v, &vArray);
5580: if (!*values) {
5581: if (csize) *csize = size;
5582: *values = array;
5583: } else {
5585: *csize = size;
5586: }
5587: return 0;
5588: }
5590: /* Compress out points not in the section */
5591: static inline PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
5592: {
5593: const PetscInt np = *numPoints;
5594: PetscInt pStart, pEnd, p, q;
5596: PetscSectionGetChart(section, &pStart, &pEnd);
5597: for (p = 0, q = 0; p < np; ++p) {
5598: const PetscInt r = points[p * 2];
5599: if ((r >= pStart) && (r < pEnd)) {
5600: points[q * 2] = r;
5601: points[q * 2 + 1] = points[p * 2 + 1];
5602: ++q;
5603: }
5604: }
5605: *numPoints = q;
5606: return 0;
5607: }
5609: /* Compressed closure does not apply closure permutation */
5610: PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
5611: {
5612: const PetscInt *cla = NULL;
5613: PetscInt np, *pts = NULL;
5616: PetscSectionGetClosureIndex(section, (PetscObject)dm, clSec, clPoints);
5617: if (*clPoints) {
5618: PetscInt dof, off;
5620: PetscSectionGetDof(*clSec, point, &dof);
5621: PetscSectionGetOffset(*clSec, point, &off);
5622: ISGetIndices(*clPoints, &cla);
5623: np = dof / 2;
5624: pts = (PetscInt *)&cla[off];
5625: } else {
5626: DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);
5627: CompressPoints_Private(section, &np, pts);
5628: }
5629: *numPoints = np;
5630: *points = pts;
5631: *clp = cla;
5632: return 0;
5633: }
5635: PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
5636: {
5638: if (!*clPoints) {
5639: DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);
5640: } else {
5641: ISRestoreIndices(*clPoints, clp);
5642: }
5643: *numPoints = 0;
5644: *points = NULL;
5645: *clSec = NULL;
5646: *clPoints = NULL;
5647: *clp = NULL;
5648: return 0;
5649: }
5651: static inline PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
5652: {
5653: PetscInt offset = 0, p;
5654: const PetscInt **perms = NULL;
5655: const PetscScalar **flips = NULL;
5658: *size = 0;
5659: PetscSectionGetPointSyms(section, numPoints, points, &perms, &flips);
5660: for (p = 0; p < numPoints; p++) {
5661: const PetscInt point = points[2 * p];
5662: const PetscInt *perm = perms ? perms[p] : NULL;
5663: const PetscScalar *flip = flips ? flips[p] : NULL;
5664: PetscInt dof, off, d;
5665: const PetscScalar *varr;
5667: PetscSectionGetDof(section, point, &dof);
5668: PetscSectionGetOffset(section, point, &off);
5669: varr = &vArray[off];
5670: if (clperm) {
5671: if (perm) {
5672: for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]] = varr[d];
5673: } else {
5674: for (d = 0; d < dof; d++) array[clperm[offset + d]] = varr[d];
5675: }
5676: if (flip) {
5677: for (d = 0; d < dof; d++) array[clperm[offset + d]] *= flip[d];
5678: }
5679: } else {
5680: if (perm) {
5681: for (d = 0; d < dof; d++) array[offset + perm[d]] = varr[d];
5682: } else {
5683: for (d = 0; d < dof; d++) array[offset + d] = varr[d];
5684: }
5685: if (flip) {
5686: for (d = 0; d < dof; d++) array[offset + d] *= flip[d];
5687: }
5688: }
5689: offset += dof;
5690: }
5691: PetscSectionRestorePointSyms(section, numPoints, points, &perms, &flips);
5692: *size = offset;
5693: return 0;
5694: }
5696: static inline PetscErrorCode DMPlexVecGetClosure_Fields_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], PetscInt numFields, const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
5697: {
5698: PetscInt offset = 0, f;
5701: *size = 0;
5702: for (f = 0; f < numFields; ++f) {
5703: PetscInt p;
5704: const PetscInt **perms = NULL;
5705: const PetscScalar **flips = NULL;
5707: PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips);
5708: for (p = 0; p < numPoints; p++) {
5709: const PetscInt point = points[2 * p];
5710: PetscInt fdof, foff, b;
5711: const PetscScalar *varr;
5712: const PetscInt *perm = perms ? perms[p] : NULL;
5713: const PetscScalar *flip = flips ? flips[p] : NULL;
5715: PetscSectionGetFieldDof(section, point, f, &fdof);
5716: PetscSectionGetFieldOffset(section, point, f, &foff);
5717: varr = &vArray[foff];
5718: if (clperm) {
5719: if (perm) {
5720: for (b = 0; b < fdof; b++) array[clperm[offset + perm[b]]] = varr[b];
5721: } else {
5722: for (b = 0; b < fdof; b++) array[clperm[offset + b]] = varr[b];
5723: }
5724: if (flip) {
5725: for (b = 0; b < fdof; b++) array[clperm[offset + b]] *= flip[b];
5726: }
5727: } else {
5728: if (perm) {
5729: for (b = 0; b < fdof; b++) array[offset + perm[b]] = varr[b];
5730: } else {
5731: for (b = 0; b < fdof; b++) array[offset + b] = varr[b];
5732: }
5733: if (flip) {
5734: for (b = 0; b < fdof; b++) array[offset + b] *= flip[b];
5735: }
5736: }
5737: offset += fdof;
5738: }
5739: PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips);
5740: }
5741: *size = offset;
5742: return 0;
5743: }
5745: /*@C
5746: DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
5748: Not collective
5750: Input Parameters:
5751: + dm - The `DM`
5752: . section - The section describing the layout in v, or NULL to use the default section
5753: . v - The local vector
5754: - point - The point in the `DM`
5756: Input/Output Parameters:
5757: + csize - The size of the input values array, or NULL; on output the number of values in the closure
5758: - values - An array to use for the values, or NULL to have it allocated automatically;
5759: if the user provided NULL, it is a borrowed array and should not be freed
5761: Level: intermediate
5763: Notes:
5764: `DMPlexVecGetClosure()`/`DMPlexVecRestoreClosure()` only allocates the values array if it set to NULL in the
5765: calling function. This is because `DMPlexVecGetClosure()` is typically called in the inner loop of a `Vec` or `Mat`
5766: assembly function, and a user may already have allocated storage for this operation.
5768: A typical use could be
5769: .vb
5770: values = NULL;
5771: DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
5772: for (cl = 0; cl < clSize; ++cl) {
5773: <Compute on closure>
5774: }
5775: DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);
5776: .ve
5777: or
5778: .vb
5779: PetscMalloc1(clMaxSize, &values);
5780: for (p = pStart; p < pEnd; ++p) {
5781: clSize = clMaxSize;
5782: DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
5783: for (cl = 0; cl < clSize; ++cl) {
5784: <Compute on closure>
5785: }
5786: }
5787: PetscFree(values);
5788: .ve
5790: Fortran Note:
5791: The csize argument is not present in the Fortran binding since it is internal to the array.
5793: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexVecRestoreClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
5794: @*/
5795: PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5796: {
5797: PetscSection clSection;
5798: IS clPoints;
5799: PetscInt *points = NULL;
5800: const PetscInt *clp, *perm;
5801: PetscInt depth, numFields, numPoints, asize;
5805: if (!section) DMGetLocalSection(dm, §ion);
5808: DMPlexGetDepth(dm, &depth);
5809: PetscSectionGetNumFields(section, &numFields);
5810: if (depth == 1 && numFields < 2) {
5811: DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
5812: return 0;
5813: }
5814: /* Get points */
5815: DMPlexGetCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5816: /* Get sizes */
5817: asize = 0;
5818: for (PetscInt p = 0; p < numPoints * 2; p += 2) {
5819: PetscInt dof;
5820: PetscSectionGetDof(section, points[p], &dof);
5821: asize += dof;
5822: }
5823: if (values) {
5824: const PetscScalar *vArray;
5825: PetscInt size;
5827: if (*values) {
5829: } else DMGetWorkArray(dm, asize, MPIU_SCALAR, values);
5830: PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, asize, &perm);
5831: VecGetArrayRead(v, &vArray);
5832: /* Get values */
5833: if (numFields > 0) DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);
5834: else DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);
5836: /* Cleanup array */
5837: VecRestoreArrayRead(v, &vArray);
5838: }
5839: if (csize) *csize = asize;
5840: /* Cleanup points */
5841: DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5842: return 0;
5843: }
5845: PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
5846: {
5847: DMLabel depthLabel;
5848: PetscSection clSection;
5849: IS clPoints;
5850: PetscScalar *array;
5851: const PetscScalar *vArray;
5852: PetscInt *points = NULL;
5853: const PetscInt *clp, *perm = NULL;
5854: PetscInt mdepth, numFields, numPoints, Np = 0, p, clsize, size;
5858: if (!section) DMGetLocalSection(dm, §ion);
5861: DMPlexGetDepth(dm, &mdepth);
5862: DMPlexGetDepthLabel(dm, &depthLabel);
5863: PetscSectionGetNumFields(section, &numFields);
5864: if (mdepth == 1 && numFields < 2) {
5865: DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
5866: return 0;
5867: }
5868: /* Get points */
5869: DMPlexGetCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5870: for (clsize = 0, p = 0; p < Np; p++) {
5871: PetscInt dof;
5872: PetscSectionGetDof(section, points[2 * p], &dof);
5873: clsize += dof;
5874: }
5875: PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &perm);
5876: /* Filter points */
5877: for (p = 0; p < numPoints * 2; p += 2) {
5878: PetscInt dep;
5880: DMLabelGetValue(depthLabel, points[p], &dep);
5881: if (dep != depth) continue;
5882: points[Np * 2 + 0] = points[p];
5883: points[Np * 2 + 1] = points[p + 1];
5884: ++Np;
5885: }
5886: /* Get array */
5887: if (!values || !*values) {
5888: PetscInt asize = 0, dof;
5890: for (p = 0; p < Np * 2; p += 2) {
5891: PetscSectionGetDof(section, points[p], &dof);
5892: asize += dof;
5893: }
5894: if (!values) {
5895: DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5896: if (csize) *csize = asize;
5897: return 0;
5898: }
5899: DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);
5900: } else {
5901: array = *values;
5902: }
5903: VecGetArrayRead(v, &vArray);
5904: /* Get values */
5905: if (numFields > 0) DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);
5906: else DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);
5907: /* Cleanup points */
5908: DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5909: /* Cleanup array */
5910: VecRestoreArrayRead(v, &vArray);
5911: if (!*values) {
5912: if (csize) *csize = size;
5913: *values = array;
5914: } else {
5916: *csize = size;
5917: }
5918: return 0;
5919: }
5921: /*@C
5922: DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
5924: Not collective
5926: Input Parameters:
5927: + dm - The `DM`
5928: . section - The section describing the layout in v, or NULL to use the default section
5929: . v - The local vector
5930: . point - The point in the `DM`
5931: . csize - The number of values in the closure, or NULL
5932: - values - The array of values, which is a borrowed array and should not be freed
5934: Level: intermediate
5936: Note:
5937: The array values are discarded and not copied back into v. In order to copy values back to v, use `DMPlexVecSetClosure()`
5939: Fortran Note:
5940: The csize argument is not present in the Fortran binding since it is internal to the array.
5942: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
5943: @*/
5944: PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5945: {
5946: PetscInt size = 0;
5948: /* Should work without recalculating size */
5949: DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void *)values);
5950: *values = NULL;
5951: return 0;
5952: }
5954: static inline void add(PetscScalar *x, PetscScalar y)
5955: {
5956: *x += y;
5957: }
5958: static inline void insert(PetscScalar *x, PetscScalar y)
5959: {
5960: *x = y;
5961: }
5963: static inline PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar *, PetscScalar), PetscBool setBC, const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
5964: {
5965: PetscInt cdof; /* The number of constraints on this point */
5966: const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5967: PetscScalar *a;
5968: PetscInt off, cind = 0, k;
5970: PetscSectionGetConstraintDof(section, point, &cdof);
5971: PetscSectionGetOffset(section, point, &off);
5972: a = &array[off];
5973: if (!cdof || setBC) {
5974: if (clperm) {
5975: if (perm) {
5976: for (k = 0; k < dof; ++k) fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
5977: } else {
5978: for (k = 0; k < dof; ++k) fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
5979: }
5980: } else {
5981: if (perm) {
5982: for (k = 0; k < dof; ++k) fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
5983: } else {
5984: for (k = 0; k < dof; ++k) fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
5985: }
5986: }
5987: } else {
5988: PetscSectionGetConstraintIndices(section, point, &cdofs);
5989: if (clperm) {
5990: if (perm) {
5991: for (k = 0; k < dof; ++k) {
5992: if ((cind < cdof) && (k == cdofs[cind])) {
5993: ++cind;
5994: continue;
5995: }
5996: fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
5997: }
5998: } else {
5999: for (k = 0; k < dof; ++k) {
6000: if ((cind < cdof) && (k == cdofs[cind])) {
6001: ++cind;
6002: continue;
6003: }
6004: fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
6005: }
6006: }
6007: } else {
6008: if (perm) {
6009: for (k = 0; k < dof; ++k) {
6010: if ((cind < cdof) && (k == cdofs[cind])) {
6011: ++cind;
6012: continue;
6013: }
6014: fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
6015: }
6016: } else {
6017: for (k = 0; k < dof; ++k) {
6018: if ((cind < cdof) && (k == cdofs[cind])) {
6019: ++cind;
6020: continue;
6021: }
6022: fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
6023: }
6024: }
6025: }
6026: }
6027: return 0;
6028: }
6030: static inline PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar *, PetscScalar), const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
6031: {
6032: PetscInt cdof; /* The number of constraints on this point */
6033: const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6034: PetscScalar *a;
6035: PetscInt off, cind = 0, k;
6037: PetscSectionGetConstraintDof(section, point, &cdof);
6038: PetscSectionGetOffset(section, point, &off);
6039: a = &array[off];
6040: if (cdof) {
6041: PetscSectionGetConstraintIndices(section, point, &cdofs);
6042: if (clperm) {
6043: if (perm) {
6044: for (k = 0; k < dof; ++k) {
6045: if ((cind < cdof) && (k == cdofs[cind])) {
6046: fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
6047: cind++;
6048: }
6049: }
6050: } else {
6051: for (k = 0; k < dof; ++k) {
6052: if ((cind < cdof) && (k == cdofs[cind])) {
6053: fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
6054: cind++;
6055: }
6056: }
6057: }
6058: } else {
6059: if (perm) {
6060: for (k = 0; k < dof; ++k) {
6061: if ((cind < cdof) && (k == cdofs[cind])) {
6062: fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
6063: cind++;
6064: }
6065: }
6066: } else {
6067: for (k = 0; k < dof; ++k) {
6068: if ((cind < cdof) && (k == cdofs[cind])) {
6069: fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
6070: cind++;
6071: }
6072: }
6073: }
6074: }
6075: }
6076: return 0;
6077: }
6079: static inline PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, const PetscInt *perm, const PetscScalar *flip, PetscInt f, void (*fuse)(PetscScalar *, PetscScalar), PetscBool setBC, const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
6080: {
6081: PetscScalar *a;
6082: PetscInt fdof, foff, fcdof, foffset = *offset;
6083: const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6084: PetscInt cind = 0, b;
6086: PetscSectionGetFieldDof(section, point, f, &fdof);
6087: PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
6088: PetscSectionGetFieldOffset(section, point, f, &foff);
6089: a = &array[foff];
6090: if (!fcdof || setBC) {
6091: if (clperm) {
6092: if (perm) {
6093: for (b = 0; b < fdof; b++) fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
6094: } else {
6095: for (b = 0; b < fdof; b++) fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
6096: }
6097: } else {
6098: if (perm) {
6099: for (b = 0; b < fdof; b++) fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
6100: } else {
6101: for (b = 0; b < fdof; b++) fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
6102: }
6103: }
6104: } else {
6105: PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6106: if (clperm) {
6107: if (perm) {
6108: for (b = 0; b < fdof; b++) {
6109: if ((cind < fcdof) && (b == fcdofs[cind])) {
6110: ++cind;
6111: continue;
6112: }
6113: fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
6114: }
6115: } else {
6116: for (b = 0; b < fdof; b++) {
6117: if ((cind < fcdof) && (b == fcdofs[cind])) {
6118: ++cind;
6119: continue;
6120: }
6121: fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
6122: }
6123: }
6124: } else {
6125: if (perm) {
6126: for (b = 0; b < fdof; b++) {
6127: if ((cind < fcdof) && (b == fcdofs[cind])) {
6128: ++cind;
6129: continue;
6130: }
6131: fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
6132: }
6133: } else {
6134: for (b = 0; b < fdof; b++) {
6135: if ((cind < fcdof) && (b == fcdofs[cind])) {
6136: ++cind;
6137: continue;
6138: }
6139: fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
6140: }
6141: }
6142: }
6143: }
6144: *offset += fdof;
6145: return 0;
6146: }
6148: static inline PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, const PetscInt perm[], const PetscScalar flip[], PetscInt f, PetscInt Ncc, const PetscInt comps[], void (*fuse)(PetscScalar *, PetscScalar), const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
6149: {
6150: PetscScalar *a;
6151: PetscInt fdof, foff, fcdof, foffset = *offset;
6152: const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6153: PetscInt Nc, cind = 0, ncind = 0, b;
6154: PetscBool ncSet, fcSet;
6156: PetscSectionGetFieldComponents(section, f, &Nc);
6157: PetscSectionGetFieldDof(section, point, f, &fdof);
6158: PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
6159: PetscSectionGetFieldOffset(section, point, f, &foff);
6160: a = &array[foff];
6161: if (fcdof) {
6162: /* We just override fcdof and fcdofs with Ncc and comps */
6163: PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6164: if (clperm) {
6165: if (perm) {
6166: if (comps) {
6167: for (b = 0; b < fdof; b++) {
6168: ncSet = fcSet = PETSC_FALSE;
6169: if (b % Nc == comps[ncind]) {
6170: ncind = (ncind + 1) % Ncc;
6171: ncSet = PETSC_TRUE;
6172: }
6173: if ((cind < fcdof) && (b == fcdofs[cind])) {
6174: ++cind;
6175: fcSet = PETSC_TRUE;
6176: }
6177: if (ncSet && fcSet) fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
6178: }
6179: } else {
6180: for (b = 0; b < fdof; b++) {
6181: if ((cind < fcdof) && (b == fcdofs[cind])) {
6182: fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
6183: ++cind;
6184: }
6185: }
6186: }
6187: } else {
6188: if (comps) {
6189: for (b = 0; b < fdof; b++) {
6190: ncSet = fcSet = PETSC_FALSE;
6191: if (b % Nc == comps[ncind]) {
6192: ncind = (ncind + 1) % Ncc;
6193: ncSet = PETSC_TRUE;
6194: }
6195: if ((cind < fcdof) && (b == fcdofs[cind])) {
6196: ++cind;
6197: fcSet = PETSC_TRUE;
6198: }
6199: if (ncSet && fcSet) fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
6200: }
6201: } else {
6202: for (b = 0; b < fdof; b++) {
6203: if ((cind < fcdof) && (b == fcdofs[cind])) {
6204: fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
6205: ++cind;
6206: }
6207: }
6208: }
6209: }
6210: } else {
6211: if (perm) {
6212: if (comps) {
6213: for (b = 0; b < fdof; b++) {
6214: ncSet = fcSet = PETSC_FALSE;
6215: if (b % Nc == comps[ncind]) {
6216: ncind = (ncind + 1) % Ncc;
6217: ncSet = PETSC_TRUE;
6218: }
6219: if ((cind < fcdof) && (b == fcdofs[cind])) {
6220: ++cind;
6221: fcSet = PETSC_TRUE;
6222: }
6223: if (ncSet && fcSet) fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
6224: }
6225: } else {
6226: for (b = 0; b < fdof; b++) {
6227: if ((cind < fcdof) && (b == fcdofs[cind])) {
6228: fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
6229: ++cind;
6230: }
6231: }
6232: }
6233: } else {
6234: if (comps) {
6235: for (b = 0; b < fdof; b++) {
6236: ncSet = fcSet = PETSC_FALSE;
6237: if (b % Nc == comps[ncind]) {
6238: ncind = (ncind + 1) % Ncc;
6239: ncSet = PETSC_TRUE;
6240: }
6241: if ((cind < fcdof) && (b == fcdofs[cind])) {
6242: ++cind;
6243: fcSet = PETSC_TRUE;
6244: }
6245: if (ncSet && fcSet) fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
6246: }
6247: } else {
6248: for (b = 0; b < fdof; b++) {
6249: if ((cind < fcdof) && (b == fcdofs[cind])) {
6250: fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
6251: ++cind;
6252: }
6253: }
6254: }
6255: }
6256: }
6257: }
6258: *offset += fdof;
6259: return 0;
6260: }
6262: static inline PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
6263: {
6264: PetscScalar *array;
6265: const PetscInt *cone, *coneO;
6266: PetscInt pStart, pEnd, p, numPoints, off, dof;
6269: PetscSectionGetChart(section, &pStart, &pEnd);
6270: DMPlexGetConeSize(dm, point, &numPoints);
6271: DMPlexGetCone(dm, point, &cone);
6272: DMPlexGetConeOrientation(dm, point, &coneO);
6273: VecGetArray(v, &array);
6274: for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
6275: const PetscInt cp = !p ? point : cone[p - 1];
6276: const PetscInt o = !p ? 0 : coneO[p - 1];
6278: if ((cp < pStart) || (cp >= pEnd)) {
6279: dof = 0;
6280: continue;
6281: }
6282: PetscSectionGetDof(section, cp, &dof);
6283: /* ADD_VALUES */
6284: {
6285: const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6286: PetscScalar *a;
6287: PetscInt cdof, coff, cind = 0, k;
6289: PetscSectionGetConstraintDof(section, cp, &cdof);
6290: PetscSectionGetOffset(section, cp, &coff);
6291: a = &array[coff];
6292: if (!cdof) {
6293: if (o >= 0) {
6294: for (k = 0; k < dof; ++k) a[k] += values[off + k];
6295: } else {
6296: for (k = 0; k < dof; ++k) a[k] += values[off + dof - k - 1];
6297: }
6298: } else {
6299: PetscSectionGetConstraintIndices(section, cp, &cdofs);
6300: if (o >= 0) {
6301: for (k = 0; k < dof; ++k) {
6302: if ((cind < cdof) && (k == cdofs[cind])) {
6303: ++cind;
6304: continue;
6305: }
6306: a[k] += values[off + k];
6307: }
6308: } else {
6309: for (k = 0; k < dof; ++k) {
6310: if ((cind < cdof) && (k == cdofs[cind])) {
6311: ++cind;
6312: continue;
6313: }
6314: a[k] += values[off + dof - k - 1];
6315: }
6316: }
6317: }
6318: }
6319: }
6320: VecRestoreArray(v, &array);
6321: return 0;
6322: }
6324: /*@C
6325: DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
6327: Not collective
6329: Input Parameters:
6330: + dm - The `DM`
6331: . section - The section describing the layout in v, or NULL to use the default section
6332: . v - The local vector
6333: . point - The point in the DM
6334: . values - The array of values
6335: - mode - The insert mode. One of `INSERT_ALL_VALUES`, `ADD_ALL_VALUES`, `INSERT_VALUES`, `ADD_VALUES`, `INSERT_BC_VALUES`, and `ADD_BC_VALUES`,
6336: where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions.
6338: Level: intermediate
6340: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`
6341: @*/
6342: PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
6343: {
6344: PetscSection clSection;
6345: IS clPoints;
6346: PetscScalar *array;
6347: PetscInt *points = NULL;
6348: const PetscInt *clp, *clperm = NULL;
6349: PetscInt depth, numFields, numPoints, p, clsize;
6353: if (!section) DMGetLocalSection(dm, §ion);
6356: DMPlexGetDepth(dm, &depth);
6357: PetscSectionGetNumFields(section, &numFields);
6358: if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
6359: DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);
6360: return 0;
6361: }
6362: /* Get points */
6363: DMPlexGetCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
6364: for (clsize = 0, p = 0; p < numPoints; p++) {
6365: PetscInt dof;
6366: PetscSectionGetDof(section, points[2 * p], &dof);
6367: clsize += dof;
6368: }
6369: PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &clperm);
6370: /* Get array */
6371: VecGetArray(v, &array);
6372: /* Get values */
6373: if (numFields > 0) {
6374: PetscInt offset = 0, f;
6375: for (f = 0; f < numFields; ++f) {
6376: const PetscInt **perms = NULL;
6377: const PetscScalar **flips = NULL;
6379: PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips);
6380: switch (mode) {
6381: case INSERT_VALUES:
6382: for (p = 0; p < numPoints; p++) {
6383: const PetscInt point = points[2 * p];
6384: const PetscInt *perm = perms ? perms[p] : NULL;
6385: const PetscScalar *flip = flips ? flips[p] : NULL;
6386: updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
6387: }
6388: break;
6389: case INSERT_ALL_VALUES:
6390: for (p = 0; p < numPoints; p++) {
6391: const PetscInt point = points[2 * p];
6392: const PetscInt *perm = perms ? perms[p] : NULL;
6393: const PetscScalar *flip = flips ? flips[p] : NULL;
6394: updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
6395: }
6396: break;
6397: case INSERT_BC_VALUES:
6398: for (p = 0; p < numPoints; p++) {
6399: const PetscInt point = points[2 * p];
6400: const PetscInt *perm = perms ? perms[p] : NULL;
6401: const PetscScalar *flip = flips ? flips[p] : NULL;
6402: updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
6403: }
6404: break;
6405: case ADD_VALUES:
6406: for (p = 0; p < numPoints; p++) {
6407: const PetscInt point = points[2 * p];
6408: const PetscInt *perm = perms ? perms[p] : NULL;
6409: const PetscScalar *flip = flips ? flips[p] : NULL;
6410: updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
6411: }
6412: break;
6413: case ADD_ALL_VALUES:
6414: for (p = 0; p < numPoints; p++) {
6415: const PetscInt point = points[2 * p];
6416: const PetscInt *perm = perms ? perms[p] : NULL;
6417: const PetscScalar *flip = flips ? flips[p] : NULL;
6418: updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
6419: }
6420: break;
6421: case ADD_BC_VALUES:
6422: for (p = 0; p < numPoints; p++) {
6423: const PetscInt point = points[2 * p];
6424: const PetscInt *perm = perms ? perms[p] : NULL;
6425: const PetscScalar *flip = flips ? flips[p] : NULL;
6426: updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
6427: }
6428: break;
6429: default:
6430: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6431: }
6432: PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips);
6433: }
6434: } else {
6435: PetscInt dof, off;
6436: const PetscInt **perms = NULL;
6437: const PetscScalar **flips = NULL;
6439: PetscSectionGetPointSyms(section, numPoints, points, &perms, &flips);
6440: switch (mode) {
6441: case INSERT_VALUES:
6442: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6443: const PetscInt point = points[2 * p];
6444: const PetscInt *perm = perms ? perms[p] : NULL;
6445: const PetscScalar *flip = flips ? flips[p] : NULL;
6446: PetscSectionGetDof(section, point, &dof);
6447: updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
6448: }
6449: break;
6450: case INSERT_ALL_VALUES:
6451: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6452: const PetscInt point = points[2 * p];
6453: const PetscInt *perm = perms ? perms[p] : NULL;
6454: const PetscScalar *flip = flips ? flips[p] : NULL;
6455: PetscSectionGetDof(section, point, &dof);
6456: updatePoint_private(section, point, dof, insert, PETSC_TRUE, perm, flip, clperm, values, off, array);
6457: }
6458: break;
6459: case INSERT_BC_VALUES:
6460: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6461: const PetscInt point = points[2 * p];
6462: const PetscInt *perm = perms ? perms[p] : NULL;
6463: const PetscScalar *flip = flips ? flips[p] : NULL;
6464: PetscSectionGetDof(section, point, &dof);
6465: updatePointBC_private(section, point, dof, insert, perm, flip, clperm, values, off, array);
6466: }
6467: break;
6468: case ADD_VALUES:
6469: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6470: const PetscInt point = points[2 * p];
6471: const PetscInt *perm = perms ? perms[p] : NULL;
6472: const PetscScalar *flip = flips ? flips[p] : NULL;
6473: PetscSectionGetDof(section, point, &dof);
6474: updatePoint_private(section, point, dof, add, PETSC_FALSE, perm, flip, clperm, values, off, array);
6475: }
6476: break;
6477: case ADD_ALL_VALUES:
6478: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6479: const PetscInt point = points[2 * p];
6480: const PetscInt *perm = perms ? perms[p] : NULL;
6481: const PetscScalar *flip = flips ? flips[p] : NULL;
6482: PetscSectionGetDof(section, point, &dof);
6483: updatePoint_private(section, point, dof, add, PETSC_TRUE, perm, flip, clperm, values, off, array);
6484: }
6485: break;
6486: case ADD_BC_VALUES:
6487: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6488: const PetscInt point = points[2 * p];
6489: const PetscInt *perm = perms ? perms[p] : NULL;
6490: const PetscScalar *flip = flips ? flips[p] : NULL;
6491: PetscSectionGetDof(section, point, &dof);
6492: updatePointBC_private(section, point, dof, add, perm, flip, clperm, values, off, array);
6493: }
6494: break;
6495: default:
6496: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6497: }
6498: PetscSectionRestorePointSyms(section, numPoints, points, &perms, &flips);
6499: }
6500: /* Cleanup points */
6501: DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
6502: /* Cleanup array */
6503: VecRestoreArray(v, &array);
6504: return 0;
6505: }
6507: /* Check whether the given point is in the label. If not, update the offset to skip this point */
6508: static inline PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset, PetscBool *contains)
6509: {
6510: *contains = PETSC_TRUE;
6511: if (label) {
6512: PetscInt fdof;
6514: DMLabelStratumHasPoint(label, labelId, point, contains);
6515: if (!*contains) {
6516: PetscSectionGetFieldDof(section, point, f, &fdof);
6517: *offset += fdof;
6518: return 0;
6519: }
6520: }
6521: return 0;
6522: }
6524: /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
6525: PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, PetscInt Ncc, const PetscInt comps[], DMLabel label, PetscInt labelId, const PetscScalar values[], InsertMode mode)
6526: {
6527: PetscSection clSection;
6528: IS clPoints;
6529: PetscScalar *array;
6530: PetscInt *points = NULL;
6531: const PetscInt *clp;
6532: PetscInt numFields, numPoints, p;
6533: PetscInt offset = 0, f;
6537: if (!section) DMGetLocalSection(dm, §ion);
6540: PetscSectionGetNumFields(section, &numFields);
6541: /* Get points */
6542: DMPlexGetCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
6543: /* Get array */
6544: VecGetArray(v, &array);
6545: /* Get values */
6546: for (f = 0; f < numFields; ++f) {
6547: const PetscInt **perms = NULL;
6548: const PetscScalar **flips = NULL;
6549: PetscBool contains;
6551: if (!fieldActive[f]) {
6552: for (p = 0; p < numPoints * 2; p += 2) {
6553: PetscInt fdof;
6554: PetscSectionGetFieldDof(section, points[p], f, &fdof);
6555: offset += fdof;
6556: }
6557: continue;
6558: }
6559: PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips);
6560: switch (mode) {
6561: case INSERT_VALUES:
6562: for (p = 0; p < numPoints; p++) {
6563: const PetscInt point = points[2 * p];
6564: const PetscInt *perm = perms ? perms[p] : NULL;
6565: const PetscScalar *flip = flips ? flips[p] : NULL;
6566: CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6567: if (!contains) continue;
6568: updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
6569: }
6570: break;
6571: case INSERT_ALL_VALUES:
6572: for (p = 0; p < numPoints; p++) {
6573: const PetscInt point = points[2 * p];
6574: const PetscInt *perm = perms ? perms[p] : NULL;
6575: const PetscScalar *flip = flips ? flips[p] : NULL;
6576: CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6577: if (!contains) continue;
6578: updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
6579: }
6580: break;
6581: case INSERT_BC_VALUES:
6582: for (p = 0; p < numPoints; p++) {
6583: const PetscInt point = points[2 * p];
6584: const PetscInt *perm = perms ? perms[p] : NULL;
6585: const PetscScalar *flip = flips ? flips[p] : NULL;
6586: CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6587: if (!contains) continue;
6588: updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
6589: }
6590: break;
6591: case ADD_VALUES:
6592: for (p = 0; p < numPoints; p++) {
6593: const PetscInt point = points[2 * p];
6594: const PetscInt *perm = perms ? perms[p] : NULL;
6595: const PetscScalar *flip = flips ? flips[p] : NULL;
6596: CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6597: if (!contains) continue;
6598: updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
6599: }
6600: break;
6601: case ADD_ALL_VALUES:
6602: for (p = 0; p < numPoints; p++) {
6603: const PetscInt point = points[2 * p];
6604: const PetscInt *perm = perms ? perms[p] : NULL;
6605: const PetscScalar *flip = flips ? flips[p] : NULL;
6606: CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6607: if (!contains) continue;
6608: updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
6609: }
6610: break;
6611: default:
6612: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6613: }
6614: PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips);
6615: }
6616: /* Cleanup points */
6617: DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
6618: /* Cleanup array */
6619: VecRestoreArray(v, &array);
6620: return 0;
6621: }
6623: static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
6624: {
6625: PetscMPIInt rank;
6626: PetscInt i, j;
6628: MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
6629: PetscViewerASCIIPrintf(viewer, "[%d]mat for point %" PetscInt_FMT "\n", rank, point);
6630: for (i = 0; i < numRIndices; i++) PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%" PetscInt_FMT "] = %" PetscInt_FMT "\n", rank, i, rindices[i]);
6631: for (i = 0; i < numCIndices; i++) PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%" PetscInt_FMT "] = %" PetscInt_FMT "\n", rank, i, cindices[i]);
6632: numCIndices = numCIndices ? numCIndices : numRIndices;
6633: if (!values) return 0;
6634: for (i = 0; i < numRIndices; i++) {
6635: PetscViewerASCIIPrintf(viewer, "[%d]", rank);
6636: for (j = 0; j < numCIndices; j++) {
6637: #if defined(PETSC_USE_COMPLEX)
6638: PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i * numCIndices + j]), (double)PetscImaginaryPart(values[i * numCIndices + j]));
6639: #else
6640: PetscViewerASCIIPrintf(viewer, " %g", (double)values[i * numCIndices + j]);
6641: #endif
6642: }
6643: PetscViewerASCIIPrintf(viewer, "\n");
6644: }
6645: return 0;
6646: }
6648: /*
6649: DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
6651: Input Parameters:
6652: + section - The section for this data layout
6653: . islocal - Is the section (and thus indices being requested) local or global?
6654: . point - The point contributing dofs with these indices
6655: . off - The global offset of this point
6656: . loff - The local offset of each field
6657: . setBC - The flag determining whether to include indices of boundary values
6658: . perm - A permutation of the dofs on this point, or NULL
6659: - indperm - A permutation of the entire indices array, or NULL
6661: Output Parameter:
6662: . indices - Indices for dofs on this point
6664: Level: developer
6666: Note: The indices could be local or global, depending on the value of 'off'.
6667: */
6668: PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
6669: {
6670: PetscInt dof; /* The number of unknowns on this point */
6671: PetscInt cdof; /* The number of constraints on this point */
6672: const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6673: PetscInt cind = 0, k;
6676: PetscSectionGetDof(section, point, &dof);
6677: PetscSectionGetConstraintDof(section, point, &cdof);
6678: if (!cdof || setBC) {
6679: for (k = 0; k < dof; ++k) {
6680: const PetscInt preind = perm ? *loff + perm[k] : *loff + k;
6681: const PetscInt ind = indperm ? indperm[preind] : preind;
6683: indices[ind] = off + k;
6684: }
6685: } else {
6686: PetscSectionGetConstraintIndices(section, point, &cdofs);
6687: for (k = 0; k < dof; ++k) {
6688: const PetscInt preind = perm ? *loff + perm[k] : *loff + k;
6689: const PetscInt ind = indperm ? indperm[preind] : preind;
6691: if ((cind < cdof) && (k == cdofs[cind])) {
6692: /* Insert check for returning constrained indices */
6693: indices[ind] = -(off + k + 1);
6694: ++cind;
6695: } else {
6696: indices[ind] = off + k - (islocal ? 0 : cind);
6697: }
6698: }
6699: }
6700: *loff += dof;
6701: return 0;
6702: }
6704: /*
6705: DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
6707: Input Parameters:
6708: + section - a section (global or local)
6709: - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
6710: . point - point within section
6711: . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
6712: . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
6713: . setBC - identify constrained (boundary condition) points via involution.
6714: . perms - perms[f][permsoff][:] is a permutation of dofs within each field
6715: . permsoff - offset
6716: - indperm - index permutation
6718: Output Parameter:
6719: . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
6720: . indices - array to hold indices (as defined by section) of each dof associated with point
6722: Notes:
6723: If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
6724: If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
6725: in the local vector.
6727: If section is global and setBC=false, the indices for constrained points are negative (and their value is not
6728: significant). It is invalid to call with a global section and setBC=true.
6730: Developer Note:
6731: The section is only used for field layout, so islocal is technically a statement about the offset (off). At some point
6732: in the future, global sections may have fields set, in which case we could pass the global section and obtain the
6733: offset could be obtained from the section instead of passing it explicitly as we do now.
6735: Example:
6736: Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
6737: When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
6738: Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
6739: The global vector does not store constrained dofs, so when this function returns global indices, say {110, -112, 111}, the value of -112 is an arbitrary flag that should not be interpreted beyond its sign.
6741: Level: developer
6742: */
6743: PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscBool islocal, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
6744: {
6745: PetscInt numFields, foff, f;
6748: PetscSectionGetNumFields(section, &numFields);
6749: for (f = 0, foff = 0; f < numFields; ++f) {
6750: PetscInt fdof, cfdof;
6751: const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6752: PetscInt cind = 0, b;
6753: const PetscInt *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
6755: PetscSectionGetFieldDof(section, point, f, &fdof);
6756: PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
6757: if (!cfdof || setBC) {
6758: for (b = 0; b < fdof; ++b) {
6759: const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
6760: const PetscInt ind = indperm ? indperm[preind] : preind;
6762: indices[ind] = off + foff + b;
6763: }
6764: } else {
6765: PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6766: for (b = 0; b < fdof; ++b) {
6767: const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
6768: const PetscInt ind = indperm ? indperm[preind] : preind;
6770: if ((cind < cfdof) && (b == fcdofs[cind])) {
6771: indices[ind] = -(off + foff + b + 1);
6772: ++cind;
6773: } else {
6774: indices[ind] = off + foff + b - (islocal ? 0 : cind);
6775: }
6776: }
6777: }
6778: foff += (setBC || islocal ? fdof : (fdof - cfdof));
6779: foffs[f] += fdof;
6780: }
6781: return 0;
6782: }
6784: /*
6785: This version believes the globalSection offsets for each field, rather than just the point offset
6787: . foffs - The offset into 'indices' for each field, since it is segregated by field
6789: Notes:
6790: The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
6791: Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
6792: */
6793: static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
6794: {
6795: PetscInt numFields, foff, f;
6797: PetscSectionGetNumFields(section, &numFields);
6798: for (f = 0; f < numFields; ++f) {
6799: PetscInt fdof, cfdof;
6800: const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6801: PetscInt cind = 0, b;
6802: const PetscInt *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
6804: PetscSectionGetFieldDof(section, point, f, &fdof);
6805: PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
6806: PetscSectionGetFieldOffset(globalSection, point, f, &foff);
6807: if (!cfdof) {
6808: for (b = 0; b < fdof; ++b) {
6809: const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
6810: const PetscInt ind = indperm ? indperm[preind] : preind;
6812: indices[ind] = foff + b;
6813: }
6814: } else {
6815: PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6816: for (b = 0; b < fdof; ++b) {
6817: const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
6818: const PetscInt ind = indperm ? indperm[preind] : preind;
6820: if ((cind < cfdof) && (b == fcdofs[cind])) {
6821: indices[ind] = -(foff + b + 1);
6822: ++cind;
6823: } else {
6824: indices[ind] = foff + b - cind;
6825: }
6826: }
6827: }
6828: foffs[f] += fdof;
6829: }
6830: return 0;
6831: }
6833: PetscErrorCode DMPlexAnchorsModifyMat(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscInt ***perms, const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[], PetscBool multiplyLeft)
6834: {
6835: Mat cMat;
6836: PetscSection aSec, cSec;
6837: IS aIS;
6838: PetscInt aStart = -1, aEnd = -1;
6839: const PetscInt *anchors;
6840: PetscInt numFields, f, p, q, newP = 0;
6841: PetscInt newNumPoints = 0, newNumIndices = 0;
6842: PetscInt *newPoints, *indices, *newIndices;
6843: PetscInt maxAnchor, maxDof;
6844: PetscInt newOffsets[32];
6845: PetscInt *pointMatOffsets[32];
6846: PetscInt *newPointOffsets[32];
6847: PetscScalar *pointMat[32];
6848: PetscScalar *newValues = NULL, *tmpValues;
6849: PetscBool anyConstrained = PETSC_FALSE;
6853: PetscSectionGetNumFields(section, &numFields);
6855: DMPlexGetAnchors(dm, &aSec, &aIS);
6856: /* if there are point-to-point constraints */
6857: if (aSec) {
6858: PetscArrayzero(newOffsets, 32);
6859: ISGetIndices(aIS, &anchors);
6860: PetscSectionGetChart(aSec, &aStart, &aEnd);
6861: /* figure out how many points are going to be in the new element matrix
6862: * (we allow double counting, because it's all just going to be summed
6863: * into the global matrix anyway) */
6864: for (p = 0; p < 2 * numPoints; p += 2) {
6865: PetscInt b = points[p];
6866: PetscInt bDof = 0, bSecDof;
6868: PetscSectionGetDof(section, b, &bSecDof);
6869: if (!bSecDof) continue;
6870: if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
6871: if (bDof) {
6872: /* this point is constrained */
6873: /* it is going to be replaced by its anchors */
6874: PetscInt bOff, q;
6876: anyConstrained = PETSC_TRUE;
6877: newNumPoints += bDof;
6878: PetscSectionGetOffset(aSec, b, &bOff);
6879: for (q = 0; q < bDof; q++) {
6880: PetscInt a = anchors[bOff + q];
6881: PetscInt aDof;
6883: PetscSectionGetDof(section, a, &aDof);
6884: newNumIndices += aDof;
6885: for (f = 0; f < numFields; ++f) {
6886: PetscInt fDof;
6888: PetscSectionGetFieldDof(section, a, f, &fDof);
6889: newOffsets[f + 1] += fDof;
6890: }
6891: }
6892: } else {
6893: /* this point is not constrained */
6894: newNumPoints++;
6895: newNumIndices += bSecDof;
6896: for (f = 0; f < numFields; ++f) {
6897: PetscInt fDof;
6899: PetscSectionGetFieldDof(section, b, f, &fDof);
6900: newOffsets[f + 1] += fDof;
6901: }
6902: }
6903: }
6904: }
6905: if (!anyConstrained) {
6906: if (outNumPoints) *outNumPoints = 0;
6907: if (outNumIndices) *outNumIndices = 0;
6908: if (outPoints) *outPoints = NULL;
6909: if (outValues) *outValues = NULL;
6910: if (aSec) ISRestoreIndices(aIS, &anchors);
6911: return 0;
6912: }
6914: if (outNumPoints) *outNumPoints = newNumPoints;
6915: if (outNumIndices) *outNumIndices = newNumIndices;
6917: for (f = 0; f < numFields; ++f) newOffsets[f + 1] += newOffsets[f];
6919: if (!outPoints && !outValues) {
6920: if (offsets) {
6921: for (f = 0; f <= numFields; f++) offsets[f] = newOffsets[f];
6922: }
6923: if (aSec) ISRestoreIndices(aIS, &anchors);
6924: return 0;
6925: }
6929: DMGetDefaultConstraints(dm, &cSec, &cMat, NULL);
6931: /* workspaces */
6932: if (numFields) {
6933: for (f = 0; f < numFields; f++) {
6934: DMGetWorkArray(dm, numPoints + 1, MPIU_INT, &pointMatOffsets[f]);
6935: DMGetWorkArray(dm, numPoints + 1, MPIU_INT, &newPointOffsets[f]);
6936: }
6937: } else {
6938: DMGetWorkArray(dm, numPoints + 1, MPIU_INT, &pointMatOffsets[0]);
6939: DMGetWorkArray(dm, numPoints, MPIU_INT, &newPointOffsets[0]);
6940: }
6942: /* get workspaces for the point-to-point matrices */
6943: if (numFields) {
6944: PetscInt totalOffset, totalMatOffset;
6946: for (p = 0; p < numPoints; p++) {
6947: PetscInt b = points[2 * p];
6948: PetscInt bDof = 0, bSecDof;
6950: PetscSectionGetDof(section, b, &bSecDof);
6951: if (!bSecDof) {
6952: for (f = 0; f < numFields; f++) {
6953: newPointOffsets[f][p + 1] = 0;
6954: pointMatOffsets[f][p + 1] = 0;
6955: }
6956: continue;
6957: }
6958: if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
6959: if (bDof) {
6960: for (f = 0; f < numFields; f++) {
6961: PetscInt fDof, q, bOff, allFDof = 0;
6963: PetscSectionGetFieldDof(section, b, f, &fDof);
6964: PetscSectionGetOffset(aSec, b, &bOff);
6965: for (q = 0; q < bDof; q++) {
6966: PetscInt a = anchors[bOff + q];
6967: PetscInt aFDof;
6969: PetscSectionGetFieldDof(section, a, f, &aFDof);
6970: allFDof += aFDof;
6971: }
6972: newPointOffsets[f][p + 1] = allFDof;
6973: pointMatOffsets[f][p + 1] = fDof * allFDof;
6974: }
6975: } else {
6976: for (f = 0; f < numFields; f++) {
6977: PetscInt fDof;
6979: PetscSectionGetFieldDof(section, b, f, &fDof);
6980: newPointOffsets[f][p + 1] = fDof;
6981: pointMatOffsets[f][p + 1] = 0;
6982: }
6983: }
6984: }
6985: for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
6986: newPointOffsets[f][0] = totalOffset;
6987: pointMatOffsets[f][0] = totalMatOffset;
6988: for (p = 0; p < numPoints; p++) {
6989: newPointOffsets[f][p + 1] += newPointOffsets[f][p];
6990: pointMatOffsets[f][p + 1] += pointMatOffsets[f][p];
6991: }
6992: totalOffset = newPointOffsets[f][numPoints];
6993: totalMatOffset = pointMatOffsets[f][numPoints];
6994: DMGetWorkArray(dm, pointMatOffsets[f][numPoints], MPIU_SCALAR, &pointMat[f]);
6995: }
6996: } else {
6997: for (p = 0; p < numPoints; p++) {
6998: PetscInt b = points[2 * p];
6999: PetscInt bDof = 0, bSecDof;
7001: PetscSectionGetDof(section, b, &bSecDof);
7002: if (!bSecDof) {
7003: newPointOffsets[0][p + 1] = 0;
7004: pointMatOffsets[0][p + 1] = 0;
7005: continue;
7006: }
7007: if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
7008: if (bDof) {
7009: PetscInt bOff, q, allDof = 0;
7011: PetscSectionGetOffset(aSec, b, &bOff);
7012: for (q = 0; q < bDof; q++) {
7013: PetscInt a = anchors[bOff + q], aDof;
7015: PetscSectionGetDof(section, a, &aDof);
7016: allDof += aDof;
7017: }
7018: newPointOffsets[0][p + 1] = allDof;
7019: pointMatOffsets[0][p + 1] = bSecDof * allDof;
7020: } else {
7021: newPointOffsets[0][p + 1] = bSecDof;
7022: pointMatOffsets[0][p + 1] = 0;
7023: }
7024: }
7025: newPointOffsets[0][0] = 0;
7026: pointMatOffsets[0][0] = 0;
7027: for (p = 0; p < numPoints; p++) {
7028: newPointOffsets[0][p + 1] += newPointOffsets[0][p];
7029: pointMatOffsets[0][p + 1] += pointMatOffsets[0][p];
7030: }
7031: DMGetWorkArray(dm, pointMatOffsets[0][numPoints], MPIU_SCALAR, &pointMat[0]);
7032: }
7034: /* output arrays */
7035: DMGetWorkArray(dm, 2 * newNumPoints, MPIU_INT, &newPoints);
7037: /* get the point-to-point matrices; construct newPoints */
7038: PetscSectionGetMaxDof(aSec, &maxAnchor);
7039: PetscSectionGetMaxDof(section, &maxDof);
7040: DMGetWorkArray(dm, maxDof, MPIU_INT, &indices);
7041: DMGetWorkArray(dm, maxAnchor * maxDof, MPIU_INT, &newIndices);
7042: if (numFields) {
7043: for (p = 0, newP = 0; p < numPoints; p++) {
7044: PetscInt b = points[2 * p];
7045: PetscInt o = points[2 * p + 1];
7046: PetscInt bDof = 0, bSecDof;
7048: PetscSectionGetDof(section, b, &bSecDof);
7049: if (!bSecDof) continue;
7050: if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
7051: if (bDof) {
7052: PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
7054: fStart[0] = 0;
7055: fEnd[0] = 0;
7056: for (f = 0; f < numFields; f++) {
7057: PetscInt fDof;
7059: PetscSectionGetFieldDof(cSec, b, f, &fDof);
7060: fStart[f + 1] = fStart[f] + fDof;
7061: fEnd[f + 1] = fStart[f + 1];
7062: }
7063: PetscSectionGetOffset(cSec, b, &bOff);
7064: DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);
7066: fAnchorStart[0] = 0;
7067: fAnchorEnd[0] = 0;
7068: for (f = 0; f < numFields; f++) {
7069: PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
7071: fAnchorStart[f + 1] = fAnchorStart[f] + fDof;
7072: fAnchorEnd[f + 1] = fAnchorStart[f + 1];
7073: }
7074: PetscSectionGetOffset(aSec, b, &bOff);
7075: for (q = 0; q < bDof; q++) {
7076: PetscInt a = anchors[bOff + q], aOff;
7078: /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */
7079: newPoints[2 * (newP + q)] = a;
7080: newPoints[2 * (newP + q) + 1] = 0;
7081: PetscSectionGetOffset(section, a, &aOff);
7082: DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);
7083: }
7084: newP += bDof;
7086: if (outValues) {
7087: /* get the point-to-point submatrix */
7088: for (f = 0; f < numFields; f++) MatGetValues(cMat, fEnd[f] - fStart[f], indices + fStart[f], fAnchorEnd[f] - fAnchorStart[f], newIndices + fAnchorStart[f], pointMat[f] + pointMatOffsets[f][p]);
7089: }
7090: } else {
7091: newPoints[2 * newP] = b;
7092: newPoints[2 * newP + 1] = o;
7093: newP++;
7094: }
7095: }
7096: } else {
7097: for (p = 0; p < numPoints; p++) {
7098: PetscInt b = points[2 * p];
7099: PetscInt o = points[2 * p + 1];
7100: PetscInt bDof = 0, bSecDof;
7102: PetscSectionGetDof(section, b, &bSecDof);
7103: if (!bSecDof) continue;
7104: if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
7105: if (bDof) {
7106: PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
7108: PetscSectionGetOffset(cSec, b, &bOff);
7109: DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);
7111: PetscSectionGetOffset(aSec, b, &bOff);
7112: for (q = 0; q < bDof; q++) {
7113: PetscInt a = anchors[bOff + q], aOff;
7115: /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */
7117: newPoints[2 * (newP + q)] = a;
7118: newPoints[2 * (newP + q) + 1] = 0;
7119: PetscSectionGetOffset(section, a, &aOff);
7120: DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);
7121: }
7122: newP += bDof;
7124: /* get the point-to-point submatrix */
7125: if (outValues) MatGetValues(cMat, bEnd, indices, bAnchorEnd, newIndices, pointMat[0] + pointMatOffsets[0][p]);
7126: } else {
7127: newPoints[2 * newP] = b;
7128: newPoints[2 * newP + 1] = o;
7129: newP++;
7130: }
7131: }
7132: }
7134: if (outValues) {
7135: DMGetWorkArray(dm, newNumIndices * numIndices, MPIU_SCALAR, &tmpValues);
7136: PetscArrayzero(tmpValues, newNumIndices * numIndices);
7137: /* multiply constraints on the right */
7138: if (numFields) {
7139: for (f = 0; f < numFields; f++) {
7140: PetscInt oldOff = offsets[f];
7142: for (p = 0; p < numPoints; p++) {
7143: PetscInt cStart = newPointOffsets[f][p];
7144: PetscInt b = points[2 * p];
7145: PetscInt c, r, k;
7146: PetscInt dof;
7148: PetscSectionGetFieldDof(section, b, f, &dof);
7149: if (!dof) continue;
7150: if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
7151: PetscInt nCols = newPointOffsets[f][p + 1] - cStart;
7152: const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
7154: for (r = 0; r < numIndices; r++) {
7155: for (c = 0; c < nCols; c++) {
7156: for (k = 0; k < dof; k++) tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
7157: }
7158: }
7159: } else {
7160: /* copy this column as is */
7161: for (r = 0; r < numIndices; r++) {
7162: for (c = 0; c < dof; c++) tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
7163: }
7164: }
7165: oldOff += dof;
7166: }
7167: }
7168: } else {
7169: PetscInt oldOff = 0;
7170: for (p = 0; p < numPoints; p++) {
7171: PetscInt cStart = newPointOffsets[0][p];
7172: PetscInt b = points[2 * p];
7173: PetscInt c, r, k;
7174: PetscInt dof;
7176: PetscSectionGetDof(section, b, &dof);
7177: if (!dof) continue;
7178: if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
7179: PetscInt nCols = newPointOffsets[0][p + 1] - cStart;
7180: const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
7182: for (r = 0; r < numIndices; r++) {
7183: for (c = 0; c < nCols; c++) {
7184: for (k = 0; k < dof; k++) tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
7185: }
7186: }
7187: } else {
7188: /* copy this column as is */
7189: for (r = 0; r < numIndices; r++) {
7190: for (c = 0; c < dof; c++) tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
7191: }
7192: }
7193: oldOff += dof;
7194: }
7195: }
7197: if (multiplyLeft) {
7198: DMGetWorkArray(dm, newNumIndices * newNumIndices, MPIU_SCALAR, &newValues);
7199: PetscArrayzero(newValues, newNumIndices * newNumIndices);
7200: /* multiply constraints transpose on the left */
7201: if (numFields) {
7202: for (f = 0; f < numFields; f++) {
7203: PetscInt oldOff = offsets[f];
7205: for (p = 0; p < numPoints; p++) {
7206: PetscInt rStart = newPointOffsets[f][p];
7207: PetscInt b = points[2 * p];
7208: PetscInt c, r, k;
7209: PetscInt dof;
7211: PetscSectionGetFieldDof(section, b, f, &dof);
7212: if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
7213: PetscInt nRows = newPointOffsets[f][p + 1] - rStart;
7214: const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
7216: for (r = 0; r < nRows; r++) {
7217: for (c = 0; c < newNumIndices; c++) {
7218: for (k = 0; k < dof; k++) newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
7219: }
7220: }
7221: } else {
7222: /* copy this row as is */
7223: for (r = 0; r < dof; r++) {
7224: for (c = 0; c < newNumIndices; c++) newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
7225: }
7226: }
7227: oldOff += dof;
7228: }
7229: }
7230: } else {
7231: PetscInt oldOff = 0;
7233: for (p = 0; p < numPoints; p++) {
7234: PetscInt rStart = newPointOffsets[0][p];
7235: PetscInt b = points[2 * p];
7236: PetscInt c, r, k;
7237: PetscInt dof;
7239: PetscSectionGetDof(section, b, &dof);
7240: if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
7241: PetscInt nRows = newPointOffsets[0][p + 1] - rStart;
7242: const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
7244: for (r = 0; r < nRows; r++) {
7245: for (c = 0; c < newNumIndices; c++) {
7246: for (k = 0; k < dof; k++) newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
7247: }
7248: }
7249: } else {
7250: /* copy this row as is */
7251: for (r = 0; r < dof; r++) {
7252: for (c = 0; c < newNumIndices; c++) newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
7253: }
7254: }
7255: oldOff += dof;
7256: }
7257: }
7259: DMRestoreWorkArray(dm, newNumIndices * numIndices, MPIU_SCALAR, &tmpValues);
7260: } else {
7261: newValues = tmpValues;
7262: }
7263: }
7265: /* clean up */
7266: DMRestoreWorkArray(dm, maxDof, MPIU_INT, &indices);
7267: DMRestoreWorkArray(dm, maxAnchor * maxDof, MPIU_INT, &newIndices);
7269: if (numFields) {
7270: for (f = 0; f < numFields; f++) {
7271: DMRestoreWorkArray(dm, pointMatOffsets[f][numPoints], MPIU_SCALAR, &pointMat[f]);
7272: DMRestoreWorkArray(dm, numPoints + 1, MPIU_INT, &pointMatOffsets[f]);
7273: DMRestoreWorkArray(dm, numPoints + 1, MPIU_INT, &newPointOffsets[f]);
7274: }
7275: } else {
7276: DMRestoreWorkArray(dm, pointMatOffsets[0][numPoints], MPIU_SCALAR, &pointMat[0]);
7277: DMRestoreWorkArray(dm, numPoints + 1, MPIU_INT, &pointMatOffsets[0]);
7278: DMRestoreWorkArray(dm, numPoints + 1, MPIU_INT, &newPointOffsets[0]);
7279: }
7280: ISRestoreIndices(aIS, &anchors);
7282: /* output */
7283: if (outPoints) {
7284: *outPoints = newPoints;
7285: } else {
7286: DMRestoreWorkArray(dm, 2 * newNumPoints, MPIU_INT, &newPoints);
7287: }
7288: if (outValues) *outValues = newValues;
7289: for (f = 0; f <= numFields; f++) offsets[f] = newOffsets[f];
7290: return 0;
7291: }
7293: /*@C
7294: DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.
7296: Not collective
7298: Input Parameters:
7299: + dm - The `DM`
7300: . section - The `PetscSection` describing the points (a local section)
7301: . idxSection - The `PetscSection` from which to obtain indices (may be local or global)
7302: . point - The point defining the closure
7303: - useClPerm - Use the closure point permutation if available
7305: Output Parameters:
7306: + numIndices - The number of dof indices in the closure of point with the input sections
7307: . indices - The dof indices
7308: . outOffsets - Array to write the field offsets into, or NULL
7309: - values - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
7311: Level: advanced
7313: Notes:
7314: Must call `DMPlexRestoreClosureIndices()` to free allocated memory
7316: If idxSection is global, any constrained dofs (see `DMAddBoundary()`, for example) will get negative indices. The value
7317: of those indices is not significant. If idxSection is local, the constrained dofs will yield the involution -(idx+1)
7318: of their index in a local vector. A caller who does not wish to distinguish those points may recover the nonnegative
7319: indices via involution, -(-(idx+1)+1)==idx. Local indices are provided when idxSection == section, otherwise global
7320: indices (with the above semantics) are implied.
7322: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreClosureIndices()`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`, `DMGetLocalSection()`,
7323: `PetscSection`, `DMGetGlobalSection()`
7324: @*/
7325: PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
7326: {
7327: /* Closure ordering */
7328: PetscSection clSection;
7329: IS clPoints;
7330: const PetscInt *clp;
7331: PetscInt *points;
7332: const PetscInt *clperm = NULL;
7333: /* Dof permutation and sign flips */
7334: const PetscInt **perms[32] = {NULL};
7335: const PetscScalar **flips[32] = {NULL};
7336: PetscScalar *valCopy = NULL;
7337: /* Hanging node constraints */
7338: PetscInt *pointsC = NULL;
7339: PetscScalar *valuesC = NULL;
7340: PetscInt NclC, NiC;
7342: PetscInt *idx;
7343: PetscInt Nf, Ncl, Ni = 0, offsets[32], p, f;
7344: PetscBool isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
7354: PetscSectionGetNumFields(section, &Nf);
7356: PetscArrayzero(offsets, 32);
7357: /* 1) Get points in closure */
7358: DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
7359: if (useClPerm) {
7360: PetscInt depth, clsize;
7361: DMPlexGetPointDepth(dm, point, &depth);
7362: for (clsize = 0, p = 0; p < Ncl; p++) {
7363: PetscInt dof;
7364: PetscSectionGetDof(section, points[2 * p], &dof);
7365: clsize += dof;
7366: }
7367: PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &clperm);
7368: }
7369: /* 2) Get number of indices on these points and field offsets from section */
7370: for (p = 0; p < Ncl * 2; p += 2) {
7371: PetscInt dof, fdof;
7373: PetscSectionGetDof(section, points[p], &dof);
7374: for (f = 0; f < Nf; ++f) {
7375: PetscSectionGetFieldDof(section, points[p], f, &fdof);
7376: offsets[f + 1] += fdof;
7377: }
7378: Ni += dof;
7379: }
7380: for (f = 1; f < Nf; ++f) offsets[f + 1] += offsets[f];
7382: /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
7383: for (f = 0; f < PetscMax(1, Nf); ++f) {
7384: if (Nf) PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);
7385: else PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);
7386: /* may need to apply sign changes to the element matrix */
7387: if (values && flips[f]) {
7388: PetscInt foffset = offsets[f];
7390: for (p = 0; p < Ncl; ++p) {
7391: PetscInt pnt = points[2 * p], fdof;
7392: const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
7394: if (!Nf) PetscSectionGetDof(section, pnt, &fdof);
7395: else PetscSectionGetFieldDof(section, pnt, f, &fdof);
7396: if (flip) {
7397: PetscInt i, j, k;
7399: if (!valCopy) {
7400: DMGetWorkArray(dm, Ni * Ni, MPIU_SCALAR, &valCopy);
7401: for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
7402: *values = valCopy;
7403: }
7404: for (i = 0; i < fdof; ++i) {
7405: PetscScalar fval = flip[i];
7407: for (k = 0; k < Ni; ++k) {
7408: valCopy[Ni * (foffset + i) + k] *= fval;
7409: valCopy[Ni * k + (foffset + i)] *= fval;
7410: }
7411: }
7412: }
7413: foffset += fdof;
7414: }
7415: }
7416: }
7417: /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
7418: DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);
7419: if (NclC) {
7420: if (valCopy) DMRestoreWorkArray(dm, Ni * Ni, MPIU_SCALAR, &valCopy);
7421: for (f = 0; f < PetscMax(1, Nf); ++f) {
7422: if (Nf) PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);
7423: else PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);
7424: }
7425: for (f = 0; f < PetscMax(1, Nf); ++f) {
7426: if (Nf) PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);
7427: else PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);
7428: }
7429: DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
7430: Ncl = NclC;
7431: Ni = NiC;
7432: points = pointsC;
7433: if (values) *values = valuesC;
7434: }
7435: /* 5) Calculate indices */
7436: DMGetWorkArray(dm, Ni, MPIU_INT, &idx);
7437: if (Nf) {
7438: PetscInt idxOff;
7439: PetscBool useFieldOffsets;
7441: if (outOffsets) {
7442: for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];
7443: }
7444: PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);
7445: if (useFieldOffsets) {
7446: for (p = 0; p < Ncl; ++p) {
7447: const PetscInt pnt = points[p * 2];
7449: DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);
7450: }
7451: } else {
7452: for (p = 0; p < Ncl; ++p) {
7453: const PetscInt pnt = points[p * 2];
7455: PetscSectionGetOffset(idxSection, pnt, &idxOff);
7456: /* Note that we pass a local section even though we're using global offsets. This is because global sections do
7457: * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
7458: * global section. */
7459: DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff + 1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);
7460: }
7461: }
7462: } else {
7463: PetscInt off = 0, idxOff;
7465: for (p = 0; p < Ncl; ++p) {
7466: const PetscInt pnt = points[p * 2];
7467: const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
7469: PetscSectionGetOffset(idxSection, pnt, &idxOff);
7470: /* Note that we pass a local section even though we're using global offsets. This is because global sections do
7471: * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
7472: DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff + 1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);
7473: }
7474: }
7475: /* 6) Cleanup */
7476: for (f = 0; f < PetscMax(1, Nf); ++f) {
7477: if (Nf) PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);
7478: else PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);
7479: }
7480: if (NclC) {
7481: DMRestoreWorkArray(dm, NclC * 2, MPIU_INT, &pointsC);
7482: } else {
7483: DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
7484: }
7486: if (numIndices) *numIndices = Ni;
7487: if (indices) *indices = idx;
7488: return 0;
7489: }
7491: /*@C
7492: DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.
7494: Not collective
7496: Input Parameters:
7497: + dm - The `DM`
7498: . section - The `PetscSection` describing the points (a local section)
7499: . idxSection - The `PetscSection` from which to obtain indices (may be local or global)
7500: . point - The point defining the closure
7501: - useClPerm - Use the closure point permutation if available
7503: Output Parameters:
7504: + numIndices - The number of dof indices in the closure of point with the input sections
7505: . indices - The dof indices
7506: . outOffsets - Array to write the field offsets into, or NULL
7507: - values - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL
7509: Level: advanced
7511: Notes:
7512: If values were modified, the user is responsible for calling `DMRestoreWorkArray`(dm, 0, `MPIU_SCALAR`, &values).
7514: If idxSection is global, any constrained dofs (see `DMAddBoundary()`, for example) will get negative indices. The value
7515: of those indices is not significant. If idxSection is local, the constrained dofs will yield the involution -(idx+1)
7516: of their index in a local vector. A caller who does not wish to distinguish those points may recover the nonnegative
7517: indices via involution, -(-(idx+1)+1)==idx. Local indices are provided when idxSection == section, otherwise global
7518: indices (with the above semantics) are implied.
7520: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetClosureIndices()`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
7521: @*/
7522: PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
7523: {
7526: DMRestoreWorkArray(dm, 0, MPIU_INT, indices);
7527: return 0;
7528: }
7530: /*@C
7531: DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
7533: Not collective
7535: Input Parameters:
7536: + dm - The `DM`
7537: . section - The section describing the layout in v, or NULL to use the default section
7538: . globalSection - The section describing the layout in v, or NULL to use the default global section
7539: . A - The matrix
7540: . point - The point in the `DM`
7541: . values - The array of values
7542: - mode - The insert mode, where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions
7544: Level: intermediate
7546: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosureGeneral()`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`
7547: @*/
7548: PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7549: {
7550: DM_Plex *mesh = (DM_Plex *)dm->data;
7551: PetscInt *indices;
7552: PetscInt numIndices;
7553: const PetscScalar *valuesOrig = values;
7554: PetscErrorCode ierr;
7557: if (!section) DMGetLocalSection(dm, §ion);
7559: if (!globalSection) DMGetGlobalSection(dm, &globalSection);
7563: DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values);
7565: if (mesh->printSetValues) DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);
7566: /* TODO: fix this code to not use error codes as handle-able exceptions! */
7567: MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
7568: if (ierr) {
7569: PetscMPIInt rank;
7571: MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
7572: (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);
7573: DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);
7574: DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values);
7575: if (values != valuesOrig) DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);
7576: SETERRQ(PetscObjectComm((PetscObject)dm), ierr, "Not possible to set matrix values");
7577: }
7578: if (mesh->printFEM > 1) {
7579: PetscInt i;
7580: PetscPrintf(PETSC_COMM_SELF, " Indices:");
7581: for (i = 0; i < numIndices; ++i) PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, indices[i]);
7582: PetscPrintf(PETSC_COMM_SELF, "\n");
7583: }
7585: DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values);
7586: if (values != valuesOrig) DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);
7587: return 0;
7588: }
7590: /*@C
7591: DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section
7593: Not collective
7595: Input Parameters:
7596: + dmRow - The `DM` for the row fields
7597: . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
7598: . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
7599: . dmCol - The `DM` for the column fields
7600: . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
7601: . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
7602: . A - The matrix
7603: . point - The point in the `DM`
7604: . values - The array of values
7605: - mode - The insert mode, where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions
7607: Level: intermediate
7609: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosure()`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`
7610: @*/
7611: PetscErrorCode DMPlexMatSetClosureGeneral(DM dmRow, PetscSection sectionRow, PetscSection globalSectionRow, DM dmCol, PetscSection sectionCol, PetscSection globalSectionCol, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7612: {
7613: DM_Plex *mesh = (DM_Plex *)dmRow->data;
7614: PetscInt *indicesRow, *indicesCol;
7615: PetscInt numIndicesRow, numIndicesCol;
7616: const PetscScalar *valuesOrig = values;
7617: PetscErrorCode ierr;
7620: if (!sectionRow) DMGetLocalSection(dmRow, §ionRow);
7622: if (!globalSectionRow) DMGetGlobalSection(dmRow, &globalSectionRow);
7625: if (!sectionCol) DMGetLocalSection(dmCol, §ionCol);
7627: if (!globalSectionCol) DMGetGlobalSection(dmCol, &globalSectionCol);
7631: DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&values);
7632: DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **)&values);
7634: if (mesh->printSetValues) DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);
7635: /* TODO: fix this code to not use error codes as handle-able exceptions! */
7636: MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
7637: if (ierr) {
7638: PetscMPIInt rank;
7640: MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
7641: (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);
7642: DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);
7643: DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&values);
7644: DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **)&values);
7645: if (values != valuesOrig) DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);
7646: }
7648: DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&values);
7649: DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **)&values);
7650: if (values != valuesOrig) DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);
7651: return 0;
7652: }
7654: PetscErrorCode DMPlexMatSetClosureRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7655: {
7656: DM_Plex *mesh = (DM_Plex *)dmf->data;
7657: PetscInt *fpoints = NULL, *ftotpoints = NULL;
7658: PetscInt *cpoints = NULL;
7659: PetscInt *findices, *cindices;
7660: const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7661: PetscInt foffsets[32], coffsets[32];
7662: DMPolytopeType ct;
7663: PetscInt numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
7664: PetscErrorCode ierr;
7668: if (!fsection) DMGetLocalSection(dmf, &fsection);
7670: if (!csection) DMGetLocalSection(dmc, &csection);
7672: if (!globalFSection) DMGetGlobalSection(dmf, &globalFSection);
7674: if (!globalCSection) DMGetGlobalSection(dmc, &globalCSection);
7677: PetscSectionGetNumFields(fsection, &numFields);
7679: PetscArrayzero(foffsets, 32);
7680: PetscArrayzero(coffsets, 32);
7681: /* Column indices */
7682: DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7683: maxFPoints = numCPoints;
7684: /* Compress out points not in the section */
7685: /* TODO: Squeeze out points with 0 dof as well */
7686: PetscSectionGetChart(csection, &pStart, &pEnd);
7687: for (p = 0, q = 0; p < numCPoints * 2; p += 2) {
7688: if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
7689: cpoints[q * 2] = cpoints[p];
7690: cpoints[q * 2 + 1] = cpoints[p + 1];
7691: ++q;
7692: }
7693: }
7694: numCPoints = q;
7695: for (p = 0, numCIndices = 0; p < numCPoints * 2; p += 2) {
7696: PetscInt fdof;
7698: PetscSectionGetDof(csection, cpoints[p], &dof);
7699: if (!dof) continue;
7700: for (f = 0; f < numFields; ++f) {
7701: PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
7702: coffsets[f + 1] += fdof;
7703: }
7704: numCIndices += dof;
7705: }
7706: for (f = 1; f < numFields; ++f) coffsets[f + 1] += coffsets[f];
7707: /* Row indices */
7708: DMPlexGetCellType(dmc, point, &ct);
7709: {
7710: DMPlexTransform tr;
7711: DMPolytopeType *rct;
7712: PetscInt *rsize, *rcone, *rornt, Nt;
7714: DMPlexTransformCreate(PETSC_COMM_SELF, &tr);
7715: DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR);
7716: DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt);
7717: numSubcells = rsize[Nt - 1];
7718: DMPlexTransformDestroy(&tr);
7719: }
7720: DMGetWorkArray(dmf, maxFPoints * 2 * numSubcells, MPIU_INT, &ftotpoints);
7721: for (r = 0, q = 0; r < numSubcells; ++r) {
7722: /* TODO Map from coarse to fine cells */
7723: DMPlexGetTransitiveClosure(dmf, point * numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
7724: /* Compress out points not in the section */
7725: PetscSectionGetChart(fsection, &pStart, &pEnd);
7726: for (p = 0; p < numFPoints * 2; p += 2) {
7727: if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
7728: PetscSectionGetDof(fsection, fpoints[p], &dof);
7729: if (!dof) continue;
7730: for (s = 0; s < q; ++s)
7731: if (fpoints[p] == ftotpoints[s * 2]) break;
7732: if (s < q) continue;
7733: ftotpoints[q * 2] = fpoints[p];
7734: ftotpoints[q * 2 + 1] = fpoints[p + 1];
7735: ++q;
7736: }
7737: }
7738: DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
7739: }
7740: numFPoints = q;
7741: for (p = 0, numFIndices = 0; p < numFPoints * 2; p += 2) {
7742: PetscInt fdof;
7744: PetscSectionGetDof(fsection, ftotpoints[p], &dof);
7745: if (!dof) continue;
7746: for (f = 0; f < numFields; ++f) {
7747: PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
7748: foffsets[f + 1] += fdof;
7749: }
7750: numFIndices += dof;
7751: }
7752: for (f = 1; f < numFields; ++f) foffsets[f + 1] += foffsets[f];
7756: DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);
7757: DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
7758: if (numFields) {
7759: const PetscInt **permsF[32] = {NULL};
7760: const PetscInt **permsC[32] = {NULL};
7762: for (f = 0; f < numFields; f++) {
7763: PetscSectionGetFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL);
7764: PetscSectionGetFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL);
7765: }
7766: for (p = 0; p < numFPoints; p++) {
7767: PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff);
7768: DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
7769: }
7770: for (p = 0; p < numCPoints; p++) {
7771: PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff);
7772: DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
7773: }
7774: for (f = 0; f < numFields; f++) {
7775: PetscSectionRestoreFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL);
7776: PetscSectionRestoreFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL);
7777: }
7778: } else {
7779: const PetscInt **permsF = NULL;
7780: const PetscInt **permsC = NULL;
7782: PetscSectionGetPointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL);
7783: PetscSectionGetPointSyms(csection, numCPoints, cpoints, &permsC, NULL);
7784: for (p = 0, off = 0; p < numFPoints; p++) {
7785: const PetscInt *perm = permsF ? permsF[p] : NULL;
7787: PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff);
7788: DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
7789: }
7790: for (p = 0, off = 0; p < numCPoints; p++) {
7791: const PetscInt *perm = permsC ? permsC[p] : NULL;
7793: PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff);
7794: DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
7795: }
7796: PetscSectionRestorePointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL);
7797: PetscSectionRestorePointSyms(csection, numCPoints, cpoints, &permsC, NULL);
7798: }
7799: if (mesh->printSetValues) DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);
7800: /* TODO: flips */
7801: /* TODO: fix this code to not use error codes as handle-able exceptions! */
7802: MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
7803: if (ierr) {
7804: PetscMPIInt rank;
7806: MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
7807: (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);
7808: DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);
7809: DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);
7810: DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
7811: }
7812: DMRestoreWorkArray(dmf, numCPoints * 2 * 4, MPIU_INT, &ftotpoints);
7813: DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7814: DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);
7815: DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
7816: return 0;
7817: }
7819: PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
7820: {
7821: PetscInt *fpoints = NULL, *ftotpoints = NULL;
7822: PetscInt *cpoints = NULL;
7823: PetscInt foffsets[32], coffsets[32];
7824: const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7825: DMPolytopeType ct;
7826: PetscInt numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
7830: if (!fsection) DMGetLocalSection(dmf, &fsection);
7832: if (!csection) DMGetLocalSection(dmc, &csection);
7834: if (!globalFSection) DMGetGlobalSection(dmf, &globalFSection);
7836: if (!globalCSection) DMGetGlobalSection(dmc, &globalCSection);
7838: PetscSectionGetNumFields(fsection, &numFields);
7840: PetscArrayzero(foffsets, 32);
7841: PetscArrayzero(coffsets, 32);
7842: /* Column indices */
7843: DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7844: maxFPoints = numCPoints;
7845: /* Compress out points not in the section */
7846: /* TODO: Squeeze out points with 0 dof as well */
7847: PetscSectionGetChart(csection, &pStart, &pEnd);
7848: for (p = 0, q = 0; p < numCPoints * 2; p += 2) {
7849: if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
7850: cpoints[q * 2] = cpoints[p];
7851: cpoints[q * 2 + 1] = cpoints[p + 1];
7852: ++q;
7853: }
7854: }
7855: numCPoints = q;
7856: for (p = 0, numCIndices = 0; p < numCPoints * 2; p += 2) {
7857: PetscInt fdof;
7859: PetscSectionGetDof(csection, cpoints[p], &dof);
7860: if (!dof) continue;
7861: for (f = 0; f < numFields; ++f) {
7862: PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
7863: coffsets[f + 1] += fdof;
7864: }
7865: numCIndices += dof;
7866: }
7867: for (f = 1; f < numFields; ++f) coffsets[f + 1] += coffsets[f];
7868: /* Row indices */
7869: DMPlexGetCellType(dmc, point, &ct);
7870: {
7871: DMPlexTransform tr;
7872: DMPolytopeType *rct;
7873: PetscInt *rsize, *rcone, *rornt, Nt;
7875: DMPlexTransformCreate(PETSC_COMM_SELF, &tr);
7876: DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR);
7877: DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt);
7878: numSubcells = rsize[Nt - 1];
7879: DMPlexTransformDestroy(&tr);
7880: }
7881: DMGetWorkArray(dmf, maxFPoints * 2 * numSubcells, MPIU_INT, &ftotpoints);
7882: for (r = 0, q = 0; r < numSubcells; ++r) {
7883: /* TODO Map from coarse to fine cells */
7884: DMPlexGetTransitiveClosure(dmf, point * numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
7885: /* Compress out points not in the section */
7886: PetscSectionGetChart(fsection, &pStart, &pEnd);
7887: for (p = 0; p < numFPoints * 2; p += 2) {
7888: if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
7889: PetscSectionGetDof(fsection, fpoints[p], &dof);
7890: if (!dof) continue;
7891: for (s = 0; s < q; ++s)
7892: if (fpoints[p] == ftotpoints[s * 2]) break;
7893: if (s < q) continue;
7894: ftotpoints[q * 2] = fpoints[p];
7895: ftotpoints[q * 2 + 1] = fpoints[p + 1];
7896: ++q;
7897: }
7898: }
7899: DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
7900: }
7901: numFPoints = q;
7902: for (p = 0, numFIndices = 0; p < numFPoints * 2; p += 2) {
7903: PetscInt fdof;
7905: PetscSectionGetDof(fsection, ftotpoints[p], &dof);
7906: if (!dof) continue;
7907: for (f = 0; f < numFields; ++f) {
7908: PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
7909: foffsets[f + 1] += fdof;
7910: }
7911: numFIndices += dof;
7912: }
7913: for (f = 1; f < numFields; ++f) foffsets[f + 1] += foffsets[f];
7917: if (numFields) {
7918: const PetscInt **permsF[32] = {NULL};
7919: const PetscInt **permsC[32] = {NULL};
7921: for (f = 0; f < numFields; f++) {
7922: PetscSectionGetFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL);
7923: PetscSectionGetFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL);
7924: }
7925: for (p = 0; p < numFPoints; p++) {
7926: PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff);
7927: DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
7928: }
7929: for (p = 0; p < numCPoints; p++) {
7930: PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff);
7931: DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
7932: }
7933: for (f = 0; f < numFields; f++) {
7934: PetscSectionRestoreFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL);
7935: PetscSectionRestoreFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL);
7936: }
7937: } else {
7938: const PetscInt **permsF = NULL;
7939: const PetscInt **permsC = NULL;
7941: PetscSectionGetPointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL);
7942: PetscSectionGetPointSyms(csection, numCPoints, cpoints, &permsC, NULL);
7943: for (p = 0, off = 0; p < numFPoints; p++) {
7944: const PetscInt *perm = permsF ? permsF[p] : NULL;
7946: PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff);
7947: DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
7948: }
7949: for (p = 0, off = 0; p < numCPoints; p++) {
7950: const PetscInt *perm = permsC ? permsC[p] : NULL;
7952: PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff);
7953: DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
7954: }
7955: PetscSectionRestorePointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL);
7956: PetscSectionRestorePointSyms(csection, numCPoints, cpoints, &permsC, NULL);
7957: }
7958: DMRestoreWorkArray(dmf, numCPoints * 2 * 4, MPIU_INT, &ftotpoints);
7959: DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7960: return 0;
7961: }
7963: /*@C
7964: DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
7966: Input Parameter:
7967: . dm - The `DMPLEX` object
7969: Output Parameter:
7970: . cellHeight - The height of a cell
7972: Level: developer
7974: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexSetVTKCellHeight()`
7975: @*/
7976: PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7977: {
7978: DM_Plex *mesh = (DM_Plex *)dm->data;
7982: *cellHeight = mesh->vtkCellHeight;
7983: return 0;
7984: }
7986: /*@C
7987: DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
7989: Input Parameters:
7990: + dm - The `DMPLEX` object
7991: - cellHeight - The height of a cell
7993: Level: developer
7995: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetVTKCellHeight()`
7996: @*/
7997: PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7998: {
7999: DM_Plex *mesh = (DM_Plex *)dm->data;
8002: mesh->vtkCellHeight = cellHeight;
8003: return 0;
8004: }
8006: /*@
8007: DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
8009: Input Parameter:
8010: . dm - The `DMPLEX` object
8012: Output Parameters:
8013: + gcStart - The first ghost cell, or NULL
8014: - gcEnd - The upper bound on ghost cells, or NULL
8016: Level: advanced
8018: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexConstructGhostCells()`, `DMPlexGetGhostCellStratum()`
8019: @*/
8020: PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
8021: {
8022: DMLabel ctLabel;
8025: DMPlexGetCellTypeLabel(dm, &ctLabel);
8026: DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);
8027: // Reset label for fast lookup
8028: DMLabelMakeAllInvalid_Internal(ctLabel);
8029: return 0;
8030: }
8032: PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
8033: {
8034: PetscSection section, globalSection;
8035: PetscInt *numbers, p;
8037: if (PetscDefined(USE_DEBUG)) DMPlexCheckPointSF(dm, sf, PETSC_TRUE);
8038: PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ion);
8039: PetscSectionSetChart(section, pStart, pEnd);
8040: for (p = pStart; p < pEnd; ++p) PetscSectionSetDof(section, p, 1);
8041: PetscSectionSetUp(section);
8042: PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);
8043: PetscMalloc1(pEnd - pStart, &numbers);
8044: for (p = pStart; p < pEnd; ++p) {
8045: PetscSectionGetOffset(globalSection, p, &numbers[p - pStart]);
8046: if (numbers[p - pStart] < 0) numbers[p - pStart] -= shift;
8047: else numbers[p - pStart] += shift;
8048: }
8049: ISCreateGeneral(PetscObjectComm((PetscObject)dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);
8050: if (globalSize) {
8051: PetscLayout layout;
8052: PetscSectionGetPointLayout(PetscObjectComm((PetscObject)dm), globalSection, &layout);
8053: PetscLayoutGetSize(layout, globalSize);
8054: PetscLayoutDestroy(&layout);
8055: }
8056: PetscSectionDestroy(§ion);
8057: PetscSectionDestroy(&globalSection);
8058: return 0;
8059: }
8061: PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
8062: {
8063: PetscInt cellHeight, cStart, cEnd;
8065: DMPlexGetVTKCellHeight(dm, &cellHeight);
8066: if (includeHybrid) DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
8067: else DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);
8068: DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);
8069: return 0;
8070: }
8072: /*@
8073: DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
8075: Input Parameter:
8076: . dm - The `DMPLEX` object
8078: Output Parameter:
8079: . globalCellNumbers - Global cell numbers for all cells on this process
8081: Level: developer
8083: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetVertexNumbering()`
8084: @*/
8085: PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
8086: {
8087: DM_Plex *mesh = (DM_Plex *)dm->data;
8090: if (!mesh->globalCellNumbers) DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);
8091: *globalCellNumbers = mesh->globalCellNumbers;
8092: return 0;
8093: }
8095: PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
8096: {
8097: PetscInt vStart, vEnd;
8100: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
8101: DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);
8102: return 0;
8103: }
8105: /*@
8106: DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
8108: Input Parameter:
8109: . dm - The `DMPLEX` object
8111: Output Parameter:
8112: . globalVertexNumbers - Global vertex numbers for all vertices on this process
8114: Level: developer
8116: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`
8117: @*/
8118: PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
8119: {
8120: DM_Plex *mesh = (DM_Plex *)dm->data;
8123: if (!mesh->globalVertexNumbers) DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);
8124: *globalVertexNumbers = mesh->globalVertexNumbers;
8125: return 0;
8126: }
8128: /*@
8129: DMPlexCreatePointNumbering - Create a global numbering for all points.
8131: Collective on dm
8133: Input Parameter:
8134: . dm - The `DMPLEX` object
8136: Output Parameter:
8137: . globalPointNumbers - Global numbers for all points on this process
8139: Level: developer
8141: Notes:
8142: The point numbering `IS` is parallel, with local portion indexed by local points (see `DMGetLocalSection()`). The global
8143: points are taken as stratified, with each MPI rank owning a contiguous subset of each stratum. In the IS, owned points
8144: will have their non-negative value while points owned by different ranks will be involuted -(idx+1). As an example,
8145: consider a parallel mesh in which the first two elements and first two vertices are owned by rank 0.
8147: The partitioned mesh is
8148: ```
8149: (2)--0--(3)--1--(4) (1)--0--(2)
8150: ```
8151: and its global numbering is
8152: ```
8153: (3)--0--(4)--1--(5)--2--(6)
8154: ```
8155: Then the global numbering is provided as
8156: ```
8157: [0] Number of indices in set 5
8158: [0] 0 0
8159: [0] 1 1
8160: [0] 2 3
8161: [0] 3 4
8162: [0] 4 -6
8163: [1] Number of indices in set 3
8164: [1] 0 2
8165: [1] 1 5
8166: [1] 2 6
8167: ```
8169: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`
8170: @*/
8171: PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
8172: {
8173: IS nums[4];
8174: PetscInt depths[4], gdepths[4], starts[4];
8175: PetscInt depth, d, shift = 0;
8176: PetscBool empty = PETSC_FALSE;
8179: DMPlexGetDepth(dm, &depth);
8180: // For unstratified meshes use dim instead of depth
8181: if (depth < 0) DMGetDimension(dm, &depth);
8182: // If any stratum is empty, we must mark all empty
8183: for (d = 0; d <= depth; ++d) {
8184: PetscInt end;
8186: depths[d] = depth - d;
8187: DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);
8188: if (!(starts[d] - end)) empty = PETSC_TRUE;
8189: }
8190: if (empty)
8191: for (d = 0; d <= depth; ++d) {
8192: depths[d] = -1;
8193: starts[d] = -1;
8194: }
8195: else PetscSortIntWithArray(depth + 1, starts, depths);
8196: MPIU_Allreduce(depths, gdepths, depth + 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
8198: // Note here that 'shift' is collective, so that the numbering is stratified by depth
8199: for (d = 0; d <= depth; ++d) {
8200: PetscInt pStart, pEnd, gsize;
8202: DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);
8203: DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);
8204: shift += gsize;
8205: }
8206: ISConcatenate(PetscObjectComm((PetscObject)dm), depth + 1, nums, globalPointNumbers);
8207: for (d = 0; d <= depth; ++d) ISDestroy(&nums[d]);
8208: return 0;
8209: }
8211: /*@
8212: DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
8214: Input Parameter:
8215: . dm - The `DMPLEX` object
8217: Output Parameter:
8218: . ranks - The rank field
8220: Options Database Key:
8221: . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
8223: Level: intermediate
8225: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`
8226: @*/
8227: PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
8228: {
8229: DM rdm;
8230: PetscFE fe;
8231: PetscScalar *r;
8232: PetscMPIInt rank;
8233: DMPolytopeType ct;
8234: PetscInt dim, cStart, cEnd, c;
8235: PetscBool simplex;
8240: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
8241: DMClone(dm, &rdm);
8242: DMGetDimension(rdm, &dim);
8243: DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
8244: DMPlexGetCellType(dm, cStart, &ct);
8245: simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
8246: PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);
8247: PetscObjectSetName((PetscObject)fe, "rank");
8248: DMSetField(rdm, 0, NULL, (PetscObject)fe);
8249: PetscFEDestroy(&fe);
8250: DMCreateDS(rdm);
8251: DMCreateGlobalVector(rdm, ranks);
8252: PetscObjectSetName((PetscObject)*ranks, "partition");
8253: VecGetArray(*ranks, &r);
8254: for (c = cStart; c < cEnd; ++c) {
8255: PetscScalar *lr;
8257: DMPlexPointGlobalRef(rdm, c, r, &lr);
8258: if (lr) *lr = rank;
8259: }
8260: VecRestoreArray(*ranks, &r);
8261: DMDestroy(&rdm);
8262: return 0;
8263: }
8265: /*@
8266: DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
8268: Input Parameters:
8269: + dm - The DMPlex
8270: - label - The DMLabel
8272: Output Parameter:
8273: . val - The label value field
8275: Options Database Keys:
8276: . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
8278: Level: intermediate
8280: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`
8281: @*/
8282: PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
8283: {
8284: DM rdm;
8285: PetscFE fe;
8286: PetscScalar *v;
8287: PetscInt dim, cStart, cEnd, c;
8293: DMClone(dm, &rdm);
8294: DMGetDimension(rdm, &dim);
8295: PetscFECreateDefault(PetscObjectComm((PetscObject)rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);
8296: PetscObjectSetName((PetscObject)fe, "label_value");
8297: DMSetField(rdm, 0, NULL, (PetscObject)fe);
8298: PetscFEDestroy(&fe);
8299: DMCreateDS(rdm);
8300: DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
8301: DMCreateGlobalVector(rdm, val);
8302: PetscObjectSetName((PetscObject)*val, "label_value");
8303: VecGetArray(*val, &v);
8304: for (c = cStart; c < cEnd; ++c) {
8305: PetscScalar *lv;
8306: PetscInt cval;
8308: DMPlexPointGlobalRef(rdm, c, v, &lv);
8309: DMLabelGetValue(label, c, &cval);
8310: *lv = cval;
8311: }
8312: VecRestoreArray(*val, &v);
8313: DMDestroy(&rdm);
8314: return 0;
8315: }
8317: /*@
8318: DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
8320: Input Parameter:
8321: . dm - The `DMPLEX` object
8323: Level: developer
8325: Notes:
8326: This is a useful diagnostic when creating meshes programmatically.
8328: For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8330: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
8331: @*/
8332: PetscErrorCode DMPlexCheckSymmetry(DM dm)
8333: {
8334: PetscSection coneSection, supportSection;
8335: const PetscInt *cone, *support;
8336: PetscInt coneSize, c, supportSize, s;
8337: PetscInt pStart, pEnd, p, pp, csize, ssize;
8338: PetscBool storagecheck = PETSC_TRUE;
8341: DMViewFromOptions(dm, NULL, "-sym_dm_view");
8342: DMPlexGetConeSection(dm, &coneSection);
8343: DMPlexGetSupportSection(dm, &supportSection);
8344: /* Check that point p is found in the support of its cone points, and vice versa */
8345: DMPlexGetChart(dm, &pStart, &pEnd);
8346: for (p = pStart; p < pEnd; ++p) {
8347: DMPlexGetConeSize(dm, p, &coneSize);
8348: DMPlexGetCone(dm, p, &cone);
8349: for (c = 0; c < coneSize; ++c) {
8350: PetscBool dup = PETSC_FALSE;
8351: PetscInt d;
8352: for (d = c - 1; d >= 0; --d) {
8353: if (cone[c] == cone[d]) {
8354: dup = PETSC_TRUE;
8355: break;
8356: }
8357: }
8358: DMPlexGetSupportSize(dm, cone[c], &supportSize);
8359: DMPlexGetSupport(dm, cone[c], &support);
8360: for (s = 0; s < supportSize; ++s) {
8361: if (support[s] == p) break;
8362: }
8363: if ((s >= supportSize) || (dup && (support[s + 1] != p))) {
8364: PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " cone: ", p);
8365: for (s = 0; s < coneSize; ++s) PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", cone[s]);
8366: PetscPrintf(PETSC_COMM_SELF, "\n");
8367: PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " support: ", cone[c]);
8368: for (s = 0; s < supportSize; ++s) PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", support[s]);
8369: PetscPrintf(PETSC_COMM_SELF, "\n");
8371: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not found in support of cone point %" PetscInt_FMT, p, cone[c]);
8372: }
8373: }
8374: DMPlexGetTreeParent(dm, p, &pp, NULL);
8375: if (p != pp) {
8376: storagecheck = PETSC_FALSE;
8377: continue;
8378: }
8379: DMPlexGetSupportSize(dm, p, &supportSize);
8380: DMPlexGetSupport(dm, p, &support);
8381: for (s = 0; s < supportSize; ++s) {
8382: DMPlexGetConeSize(dm, support[s], &coneSize);
8383: DMPlexGetCone(dm, support[s], &cone);
8384: for (c = 0; c < coneSize; ++c) {
8385: DMPlexGetTreeParent(dm, cone[c], &pp, NULL);
8386: if (cone[c] != pp) {
8387: c = 0;
8388: break;
8389: }
8390: if (cone[c] == p) break;
8391: }
8392: if (c >= coneSize) {
8393: PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " support: ", p);
8394: for (c = 0; c < supportSize; ++c) PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", support[c]);
8395: PetscPrintf(PETSC_COMM_SELF, "\n");
8396: PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " cone: ", support[s]);
8397: for (c = 0; c < coneSize; ++c) PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", cone[c]);
8398: PetscPrintf(PETSC_COMM_SELF, "\n");
8399: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not found in cone of support point %" PetscInt_FMT, p, support[s]);
8400: }
8401: }
8402: }
8403: if (storagecheck) {
8404: PetscSectionGetStorageSize(coneSection, &csize);
8405: PetscSectionGetStorageSize(supportSection, &ssize);
8407: }
8408: return 0;
8409: }
8411: /*
8412: For submeshes with cohesive cells (see DMPlexConstructCohesiveCells()), we allow a special case where some of the boundary of a face (edges and vertices) are not duplicated. We call these special boundary points "unsplit", since the same edge or vertex appears in both copies of the face. These unsplit points throw off our counting, so we have to explicitly account for them here.
8413: */
8414: static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
8415: {
8416: DMPolytopeType cct;
8417: PetscInt ptpoints[4];
8418: const PetscInt *cone, *ccone, *ptcone;
8419: PetscInt coneSize, cp, cconeSize, ccp, npt = 0, pt;
8421: *unsplit = 0;
8422: switch (ct) {
8423: case DM_POLYTOPE_POINT_PRISM_TENSOR:
8424: ptpoints[npt++] = c;
8425: break;
8426: case DM_POLYTOPE_SEG_PRISM_TENSOR:
8427: DMPlexGetCone(dm, c, &cone);
8428: DMPlexGetConeSize(dm, c, &coneSize);
8429: for (cp = 0; cp < coneSize; ++cp) {
8430: DMPlexGetCellType(dm, cone[cp], &cct);
8431: if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
8432: }
8433: break;
8434: case DM_POLYTOPE_TRI_PRISM_TENSOR:
8435: case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8436: DMPlexGetCone(dm, c, &cone);
8437: DMPlexGetConeSize(dm, c, &coneSize);
8438: for (cp = 0; cp < coneSize; ++cp) {
8439: DMPlexGetCone(dm, cone[cp], &ccone);
8440: DMPlexGetConeSize(dm, cone[cp], &cconeSize);
8441: for (ccp = 0; ccp < cconeSize; ++ccp) {
8442: DMPlexGetCellType(dm, ccone[ccp], &cct);
8443: if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
8444: PetscInt p;
8445: for (p = 0; p < npt; ++p)
8446: if (ptpoints[p] == ccone[ccp]) break;
8447: if (p == npt) ptpoints[npt++] = ccone[ccp];
8448: }
8449: }
8450: }
8451: break;
8452: default:
8453: break;
8454: }
8455: for (pt = 0; pt < npt; ++pt) {
8456: DMPlexGetCone(dm, ptpoints[pt], &ptcone);
8457: if (ptcone[0] == ptcone[1]) ++(*unsplit);
8458: }
8459: return 0;
8460: }
8462: /*@
8463: DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
8465: Input Parameters:
8466: + dm - The `DMPLEX` object
8467: - cellHeight - Normally 0
8469: Level: developer
8471: Notes:
8472: This is a useful diagnostic when creating meshes programmatically.
8473: Currently applicable only to homogeneous simplex or tensor meshes.
8475: For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8477: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
8478: @*/
8479: PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
8480: {
8481: DMPlexInterpolatedFlag interp;
8482: DMPolytopeType ct;
8483: PetscInt vStart, vEnd, cStart, cEnd, c;
8486: DMPlexIsInterpolated(dm, &interp);
8487: DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
8488: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
8489: for (c = cStart; c < cEnd; ++c) {
8490: PetscInt *closure = NULL;
8491: PetscInt coneSize, closureSize, cl, Nv = 0;
8493: DMPlexGetCellType(dm, c, &ct);
8495: if (ct == DM_POLYTOPE_UNKNOWN) continue;
8496: if (interp == DMPLEX_INTERPOLATED_FULL) {
8497: DMPlexGetConeSize(dm, c, &coneSize);
8499: }
8500: DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8501: for (cl = 0; cl < closureSize * 2; cl += 2) {
8502: const PetscInt p = closure[cl];
8503: if ((p >= vStart) && (p < vEnd)) ++Nv;
8504: }
8505: DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8506: /* Special Case: Tensor faces with identified vertices */
8507: if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
8508: PetscInt unsplit;
8510: DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
8511: if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
8512: }
8514: }
8515: return 0;
8516: }
8518: /*@
8519: DMPlexCheckFaces - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type
8521: Collective on dm
8523: Input Parameters:
8524: + dm - The `DMPLEX` object
8525: - cellHeight - Normally 0
8527: Level: developer
8529: Notes:
8530: This is a useful diagnostic when creating meshes programmatically.
8531: This routine is only relevant for meshes that are fully interpolated across all ranks.
8532: It will error out if a partially interpolated mesh is given on some rank.
8533: It will do nothing for locally uninterpolated mesh (as there is nothing to check).
8535: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
8537: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexGetVTKCellHeight()`, `DMSetFromOptions()`
8538: @*/
8539: PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
8540: {
8541: PetscInt dim, depth, vStart, vEnd, cStart, cEnd, c, h;
8542: DMPlexInterpolatedFlag interpEnum;
8545: DMPlexIsInterpolatedCollective(dm, &interpEnum);
8546: if (interpEnum == DMPLEX_INTERPOLATED_NONE) return 0;
8547: if (interpEnum != DMPLEX_INTERPOLATED_FULL) {
8548: PetscPrintf(PetscObjectComm((PetscObject)dm), "DMPlexCheckFaces() warning: Mesh is only partially interpolated, this is currently not supported");
8549: return 0;
8550: }
8552: DMGetDimension(dm, &dim);
8553: DMPlexGetDepth(dm, &depth);
8554: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
8555: for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
8556: DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);
8557: for (c = cStart; c < cEnd; ++c) {
8558: const PetscInt *cone, *ornt, *faceSizes, *faces;
8559: const DMPolytopeType *faceTypes;
8560: DMPolytopeType ct;
8561: PetscInt numFaces, coneSize, f;
8562: PetscInt *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
8564: DMPlexGetCellType(dm, c, &ct);
8565: DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
8566: if (unsplit) continue;
8567: DMPlexGetConeSize(dm, c, &coneSize);
8568: DMPlexGetCone(dm, c, &cone);
8569: DMPlexGetConeOrientation(dm, c, &ornt);
8570: DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8571: for (cl = 0; cl < closureSize * 2; cl += 2) {
8572: const PetscInt p = closure[cl];
8573: if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
8574: }
8575: DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
8577: for (f = 0; f < numFaces; ++f) {
8578: DMPolytopeType fct;
8579: PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
8581: DMPlexGetCellType(dm, cone[f], &fct);
8582: DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);
8583: for (cl = 0; cl < fclosureSize * 2; cl += 2) {
8584: const PetscInt p = fclosure[cl];
8585: if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
8586: }
8588: for (v = 0; v < fnumCorners; ++v) {
8589: if (fclosure[v] != faces[fOff + v]) {
8590: PetscInt v1;
8592: PetscPrintf(PETSC_COMM_SELF, "face closure:");
8593: for (v1 = 0; v1 < fnumCorners; ++v1) PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, fclosure[v1]);
8594: PetscPrintf(PETSC_COMM_SELF, "\ncell face:");
8595: for (v1 = 0; v1 < fnumCorners; ++v1) PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, faces[fOff + v1]);
8596: PetscPrintf(PETSC_COMM_SELF, "\n");
8597: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " of type %s (cone idx %" PetscInt_FMT ", ornt %" PetscInt_FMT ") of cell %" PetscInt_FMT " of type %s vertex %" PetscInt_FMT ", %" PetscInt_FMT " != %" PetscInt_FMT, cone[f], DMPolytopeTypes[fct], f, ornt[f], c, DMPolytopeTypes[ct], v, fclosure[v], faces[fOff + v]);
8598: }
8599: }
8600: DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);
8601: fOff += faceSizes[f];
8602: }
8603: DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
8604: DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8605: }
8606: }
8607: return 0;
8608: }
8610: /*@
8611: DMPlexCheckGeometry - Check the geometry of mesh cells
8613: Input Parameter:
8614: . dm - The `DMPLEX` object
8616: Level: developer
8618: Notes:
8619: This is a useful diagnostic when creating meshes programmatically.
8621: For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8623: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
8624: @*/
8625: PetscErrorCode DMPlexCheckGeometry(DM dm)
8626: {
8627: Vec coordinates;
8628: PetscReal detJ, J[9], refVol = 1.0;
8629: PetscReal vol;
8630: PetscInt dim, depth, dE, d, cStart, cEnd, c;
8632: DMGetDimension(dm, &dim);
8633: DMGetCoordinateDim(dm, &dE);
8634: if (dim != dE) return 0;
8635: DMPlexGetDepth(dm, &depth);
8636: for (d = 0; d < dim; ++d) refVol *= 2.0;
8637: DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
8638: /* Make sure local coordinates are created, because that step is collective */
8639: DMGetCoordinatesLocal(dm, &coordinates);
8640: for (c = cStart; c < cEnd; ++c) {
8641: DMPolytopeType ct;
8642: PetscInt unsplit;
8643: PetscBool ignoreZeroVol = PETSC_FALSE;
8645: DMPlexGetCellType(dm, c, &ct);
8646: switch (ct) {
8647: case DM_POLYTOPE_SEG_PRISM_TENSOR:
8648: case DM_POLYTOPE_TRI_PRISM_TENSOR:
8649: case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8650: ignoreZeroVol = PETSC_TRUE;
8651: break;
8652: default:
8653: break;
8654: }
8655: switch (ct) {
8656: case DM_POLYTOPE_TRI_PRISM:
8657: case DM_POLYTOPE_TRI_PRISM_TENSOR:
8658: case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8659: case DM_POLYTOPE_PYRAMID:
8660: continue;
8661: default:
8662: break;
8663: }
8664: DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
8665: if (unsplit) continue;
8666: DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);
8668: PetscInfo(dm, "Cell %" PetscInt_FMT " FEM Volume %g\n", c, (double)(detJ * refVol));
8669: /* This should work with periodicity since DG coordinates should be used */
8670: if (depth > 1) {
8671: DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);
8673: PetscInfo(dm, "Cell %" PetscInt_FMT " FVM Volume %g\n", c, (double)vol);
8674: }
8675: }
8676: return 0;
8677: }
8679: /*@
8680: DMPlexCheckPointSF - Check that several necessary conditions are met for the Point SF of this plex.
8682: Collective on dm
8684: Input Parameters:
8685: + dm - The `DMPLEX` object
8686: . pointSF - The `PetscSF`, or NULL for `PointSF` attached to `DM`
8687: - allowExtraRoots - Flag to allow extra points not present in the `DM`
8689: Level: developer
8691: Notes:
8692: This is mainly intended for debugging/testing purposes.
8694: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
8696: Extra roots can come from priodic cuts, where additional points appear on the boundary
8698: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMGetPointSF()`, `DMSetFromOptions()`
8699: @*/
8700: PetscErrorCode DMPlexCheckPointSF(DM dm, PetscSF pointSF, PetscBool allowExtraRoots)
8701: {
8702: PetscInt l, nleaves, nroots, overlap;
8703: const PetscInt *locals;
8704: const PetscSFNode *remotes;
8705: PetscBool distributed;
8706: MPI_Comm comm;
8707: PetscMPIInt rank;
8711: else pointSF = dm->sf;
8712: PetscObjectGetComm((PetscObject)dm, &comm);
8714: MPI_Comm_rank(comm, &rank);
8715: {
8716: PetscMPIInt mpiFlag;
8718: MPI_Comm_compare(comm, PetscObjectComm((PetscObject)pointSF), &mpiFlag);
8720: }
8721: PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, &remotes);
8722: DMPlexIsDistributed(dm, &distributed);
8723: if (!distributed) {
8725: return 0;
8726: }
8728: DMPlexGetOverlap(dm, &overlap);
8730: /* Check SF graph is compatible with DMPlex chart */
8731: {
8732: PetscInt pStart, pEnd, maxLeaf;
8734: DMPlexGetChart(dm, &pStart, &pEnd);
8735: PetscSFGetLeafRange(pointSF, NULL, &maxLeaf);
8738: }
8740: /* Check Point SF has no local points referenced */
8741: for (l = 0; l < nleaves; l++) {
8742: PetscAssert(remotes[l].rank != (PetscInt)rank, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains local point %" PetscInt_FMT " <- (%" PetscInt_FMT ",%" PetscInt_FMT ")", locals ? locals[l] : l, remotes[l].rank, remotes[l].index);
8743: }
8745: /* Check there are no cells in interface */
8746: if (!overlap) {
8747: PetscInt cellHeight, cStart, cEnd;
8749: DMPlexGetVTKCellHeight(dm, &cellHeight);
8750: DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
8751: for (l = 0; l < nleaves; ++l) {
8752: const PetscInt point = locals ? locals[l] : l;
8755: }
8756: }
8758: /* If some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
8759: {
8760: const PetscInt *rootdegree;
8762: PetscSFComputeDegreeBegin(pointSF, &rootdegree);
8763: PetscSFComputeDegreeEnd(pointSF, &rootdegree);
8764: for (l = 0; l < nleaves; ++l) {
8765: const PetscInt point = locals ? locals[l] : l;
8766: const PetscInt *cone;
8767: PetscInt coneSize, c, idx;
8769: DMPlexGetConeSize(dm, point, &coneSize);
8770: DMPlexGetCone(dm, point, &cone);
8771: for (c = 0; c < coneSize; ++c) {
8772: if (!rootdegree[cone[c]]) {
8773: if (locals) {
8774: PetscFindInt(cone[c], nleaves, locals, &idx);
8775: } else {
8776: idx = (cone[c] < nleaves) ? cone[c] : -1;
8777: }
8779: }
8780: }
8781: }
8782: }
8783: return 0;
8784: }
8786: /*@
8787: DMPlexCheck - Perform various checks of Plex sanity
8789: Input Parameter:
8790: . dm - The `DMPLEX` object
8792: Level: developer
8794: Notes:
8795: This is a useful diagnostic when creating meshes programmatically.
8797: For the complete list of DMPlexCheck* functions, see DMSetFromOptions().
8799: Currently does not include DMPlexCheckCellShape().
8801: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, DMCreate(), DMSetFromOptions()
8802: @*/
8803: PetscErrorCode DMPlexCheck(DM dm)
8804: {
8805: PetscInt cellHeight;
8807: DMPlexGetVTKCellHeight(dm, &cellHeight);
8808: DMPlexCheckSymmetry(dm);
8809: DMPlexCheckSkeleton(dm, cellHeight);
8810: DMPlexCheckFaces(dm, cellHeight);
8811: DMPlexCheckGeometry(dm);
8812: DMPlexCheckPointSF(dm, NULL, PETSC_FALSE);
8813: DMPlexCheckInterfaceCones(dm);
8814: return 0;
8815: }
8817: typedef struct cell_stats {
8818: PetscReal min, max, sum, squaresum;
8819: PetscInt count;
8820: } cell_stats_t;
8822: static void MPIAPI cell_stats_reduce(void *a, void *b, int *len, MPI_Datatype *datatype)
8823: {
8824: PetscInt i, N = *len;
8826: for (i = 0; i < N; i++) {
8827: cell_stats_t *A = (cell_stats_t *)a;
8828: cell_stats_t *B = (cell_stats_t *)b;
8830: B->min = PetscMin(A->min, B->min);
8831: B->max = PetscMax(A->max, B->max);
8832: B->sum += A->sum;
8833: B->squaresum += A->squaresum;
8834: B->count += A->count;
8835: }
8836: }
8838: /*@
8839: DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
8841: Collective on dm
8843: Input Parameters:
8844: + dm - The `DMPLEX` object
8845: . output - If true, statistics will be displayed on stdout
8846: - condLimit - Display all cells above this condition number, or `PETSC_DETERMINE` for no cell output
8848: Level: developer
8850: Notes:
8851: This is mainly intended for debugging/testing purposes.
8853: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
8855: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexComputeOrthogonalQuality()`
8856: @*/
8857: PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
8858: {
8859: DM dmCoarse;
8860: cell_stats_t stats, globalStats;
8861: MPI_Comm comm = PetscObjectComm((PetscObject)dm);
8862: PetscReal *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
8863: PetscReal limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
8864: PetscInt cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
8865: PetscMPIInt rank, size;
8868: stats.min = PETSC_MAX_REAL;
8869: stats.max = PETSC_MIN_REAL;
8870: stats.sum = stats.squaresum = 0.;
8871: stats.count = 0;
8873: MPI_Comm_size(comm, &size);
8874: MPI_Comm_rank(comm, &rank);
8875: DMGetCoordinateDim(dm, &cdim);
8876: PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);
8877: DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);
8878: DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);
8879: for (c = cStart; c < cEnd; c++) {
8880: PetscInt i;
8881: PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
8883: DMPlexComputeCellGeometryAffineFEM(dm, c, NULL, J, invJ, &detJ);
8885: for (i = 0; i < PetscSqr(cdim); ++i) {
8886: frobJ += J[i] * J[i];
8887: frobInvJ += invJ[i] * invJ[i];
8888: }
8889: cond2 = frobJ * frobInvJ;
8890: cond = PetscSqrtReal(cond2);
8892: stats.min = PetscMin(stats.min, cond);
8893: stats.max = PetscMax(stats.max, cond);
8894: stats.sum += cond;
8895: stats.squaresum += cond2;
8896: stats.count++;
8897: if (output && cond > limit) {
8898: PetscSection coordSection;
8899: Vec coordsLocal;
8900: PetscScalar *coords = NULL;
8901: PetscInt Nv, d, clSize, cl, *closure = NULL;
8903: DMGetCoordinatesLocal(dm, &coordsLocal);
8904: DMGetCoordinateSection(dm, &coordSection);
8905: DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
8906: PetscSynchronizedPrintf(comm, "[%d] Cell %" PetscInt_FMT " cond %g\n", rank, c, (double)cond);
8907: for (i = 0; i < Nv / cdim; ++i) {
8908: PetscSynchronizedPrintf(comm, " Vertex %" PetscInt_FMT ": (", i);
8909: for (d = 0; d < cdim; ++d) {
8910: if (d > 0) PetscSynchronizedPrintf(comm, ", ");
8911: PetscSynchronizedPrintf(comm, "%g", (double)PetscRealPart(coords[i * cdim + d]));
8912: }
8913: PetscSynchronizedPrintf(comm, ")\n");
8914: }
8915: DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
8916: for (cl = 0; cl < clSize * 2; cl += 2) {
8917: const PetscInt edge = closure[cl];
8919: if ((edge >= eStart) && (edge < eEnd)) {
8920: PetscReal len;
8922: DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);
8923: PetscSynchronizedPrintf(comm, " Edge %" PetscInt_FMT ": length %g\n", edge, (double)len);
8924: }
8925: }
8926: DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
8927: DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
8928: }
8929: }
8930: if (output) PetscSynchronizedFlush(comm, NULL);
8932: if (size > 1) {
8933: PetscMPIInt blockLengths[2] = {4, 1};
8934: MPI_Aint blockOffsets[2] = {offsetof(cell_stats_t, min), offsetof(cell_stats_t, count)};
8935: MPI_Datatype blockTypes[2] = {MPIU_REAL, MPIU_INT}, statType;
8936: MPI_Op statReduce;
8938: MPI_Type_create_struct(2, blockLengths, blockOffsets, blockTypes, &statType);
8939: MPI_Type_commit(&statType);
8940: MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);
8941: MPI_Reduce(&stats, &globalStats, 1, statType, statReduce, 0, comm);
8942: MPI_Op_free(&statReduce);
8943: MPI_Type_free(&statType);
8944: } else {
8945: PetscArraycpy(&globalStats, &stats, 1);
8946: }
8947: if (rank == 0) {
8948: count = globalStats.count;
8949: min = globalStats.min;
8950: max = globalStats.max;
8951: mean = globalStats.sum / globalStats.count;
8952: stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1), 0)) : 0.0;
8953: }
8955: if (output) PetscPrintf(comm, "Mesh with %" PetscInt_FMT " cells, shape condition numbers: min = %g, max = %g, mean = %g, stddev = %g\n", count, (double)min, (double)max, (double)mean, (double)stdev);
8956: PetscFree2(J, invJ);
8958: DMGetCoarseDM(dm, &dmCoarse);
8959: if (dmCoarse) {
8960: PetscBool isplex;
8962: PetscObjectTypeCompare((PetscObject)dmCoarse, DMPLEX, &isplex);
8963: if (isplex) DMPlexCheckCellShape(dmCoarse, output, condLimit);
8964: }
8965: return 0;
8966: }
8968: /*@
8969: DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
8970: orthogonal quality below given tolerance.
8972: Collective on dm
8974: Input Parameters:
8975: + dm - The `DMPLEX` object
8976: . fv - Optional `PetscFV` object for pre-computed cell/face centroid information
8977: - atol - [0, 1] Absolute tolerance for tagging cells.
8979: Output Parameters:
8980: + OrthQual - Vec containing orthogonal quality per cell
8981: - OrthQualLabel - `DMLabel` tagging cells below atol with `DM_ADAPT_REFINE`
8983: Options Database Keys:
8984: + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only `PETSCVIEWERASCII` is supported.
8985: - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.
8987: Level: intermediate
8989: Notes:
8990: Orthogonal quality is given by the following formula:
8992: $ \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]$
8994: Where A_i is the i'th face-normal vector, f_i is the vector from the cell centroid to the i'th face centroid, and c_i
8995: is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
8996: current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
8997: calculating the cosine of the angle between these vectors.
8999: Orthogonal quality ranges from 1 (best) to 0 (worst).
9001: This routine is mainly useful for FVM, however is not restricted to only FVM. The `PetscFV` object is optionally used to check for
9002: pre-computed FVM cell data, but if it is not passed in then this data will be computed.
9004: Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.
9006: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCheckCellShape()`, `DMCreateLabel()`, `PetscFV`, `DMLabel`, `Vec`
9007: @*/
9008: PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
9009: {
9010: PetscInt nc, cellHeight, cStart, cEnd, cell, cellIter = 0;
9011: PetscInt *idx;
9012: PetscScalar *oqVals;
9013: const PetscScalar *cellGeomArr, *faceGeomArr;
9014: PetscReal *ci, *fi, *Ai;
9015: MPI_Comm comm;
9016: Vec cellgeom, facegeom;
9017: DM dmFace, dmCell;
9018: IS glob;
9019: ISLocalToGlobalMapping ltog;
9020: PetscViewer vwr;
9026: PetscObjectGetComm((PetscObject)dm, &comm);
9027: DMGetDimension(dm, &nc);
9029: {
9030: DMPlexInterpolatedFlag interpFlag;
9032: DMPlexIsInterpolated(dm, &interpFlag);
9033: if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
9034: PetscMPIInt rank;
9036: MPI_Comm_rank(comm, &rank);
9037: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
9038: }
9039: }
9040: if (OrthQualLabel) {
9042: DMCreateLabel(dm, "Orthogonal_Quality");
9043: DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);
9044: } else {
9045: *OrthQualLabel = NULL;
9046: }
9047: DMPlexGetVTKCellHeight(dm, &cellHeight);
9048: DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
9049: DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);
9050: ISLocalToGlobalMappingCreateIS(glob, <og);
9051: ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);
9052: VecCreate(comm, OrthQual);
9053: VecSetType(*OrthQual, VECSTANDARD);
9054: VecSetSizes(*OrthQual, cEnd - cStart, PETSC_DETERMINE);
9055: VecSetLocalToGlobalMapping(*OrthQual, ltog);
9056: VecSetUp(*OrthQual);
9057: ISDestroy(&glob);
9058: ISLocalToGlobalMappingDestroy(<og);
9059: DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);
9060: VecGetArrayRead(cellgeom, &cellGeomArr);
9061: VecGetArrayRead(facegeom, &faceGeomArr);
9062: VecGetDM(cellgeom, &dmCell);
9063: VecGetDM(facegeom, &dmFace);
9064: PetscMalloc5(cEnd - cStart, &idx, cEnd - cStart, &oqVals, nc, &ci, nc, &fi, nc, &Ai);
9065: for (cell = cStart; cell < cEnd; cellIter++, cell++) {
9066: PetscInt cellneigh, cellneighiter = 0, adjSize = PETSC_DETERMINE;
9067: PetscInt cellarr[2], *adj = NULL;
9068: PetscScalar *cArr, *fArr;
9069: PetscReal minvalc = 1.0, minvalf = 1.0;
9070: PetscFVCellGeom *cg;
9072: idx[cellIter] = cell - cStart;
9073: cellarr[0] = cell;
9074: /* Make indexing into cellGeom easier */
9075: DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);
9076: DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);
9077: /* Technically 1 too big, but easier than fiddling with empty adjacency array */
9078: PetscCalloc2(adjSize, &cArr, adjSize, &fArr);
9079: for (cellneigh = 0; cellneigh < adjSize; cellneighiter++, cellneigh++) {
9080: PetscInt i;
9081: const PetscInt neigh = adj[cellneigh];
9082: PetscReal normci = 0, normfi = 0, normai = 0;
9083: PetscFVCellGeom *cgneigh;
9084: PetscFVFaceGeom *fg;
9086: /* Don't count ourselves in the neighbor list */
9087: if (neigh == cell) continue;
9088: DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);
9089: cellarr[1] = neigh;
9090: {
9091: PetscInt numcovpts;
9092: const PetscInt *covpts;
9094: DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);
9095: DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);
9096: DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);
9097: }
9099: /* Compute c_i, f_i and their norms */
9100: for (i = 0; i < nc; i++) {
9101: ci[i] = cgneigh->centroid[i] - cg->centroid[i];
9102: fi[i] = fg->centroid[i] - cg->centroid[i];
9103: Ai[i] = fg->normal[i];
9104: normci += PetscPowReal(ci[i], 2);
9105: normfi += PetscPowReal(fi[i], 2);
9106: normai += PetscPowReal(Ai[i], 2);
9107: }
9108: normci = PetscSqrtReal(normci);
9109: normfi = PetscSqrtReal(normfi);
9110: normai = PetscSqrtReal(normai);
9112: /* Normalize and compute for each face-cell-normal pair */
9113: for (i = 0; i < nc; i++) {
9114: ci[i] = ci[i] / normci;
9115: fi[i] = fi[i] / normfi;
9116: Ai[i] = Ai[i] / normai;
9117: /* PetscAbs because I don't know if normals are guaranteed to point out */
9118: cArr[cellneighiter] += PetscAbs(Ai[i] * ci[i]);
9119: fArr[cellneighiter] += PetscAbs(Ai[i] * fi[i]);
9120: }
9121: if (PetscRealPart(cArr[cellneighiter]) < minvalc) minvalc = PetscRealPart(cArr[cellneighiter]);
9122: if (PetscRealPart(fArr[cellneighiter]) < minvalf) minvalf = PetscRealPart(fArr[cellneighiter]);
9123: }
9124: PetscFree(adj);
9125: PetscFree2(cArr, fArr);
9126: /* Defer to cell if they're equal */
9127: oqVals[cellIter] = PetscMin(minvalf, minvalc);
9128: if (OrthQualLabel) {
9129: if (PetscRealPart(oqVals[cellIter]) <= atol) DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);
9130: }
9131: }
9132: VecSetValuesLocal(*OrthQual, cEnd - cStart, idx, oqVals, INSERT_VALUES);
9133: VecAssemblyBegin(*OrthQual);
9134: VecAssemblyEnd(*OrthQual);
9135: VecRestoreArrayRead(cellgeom, &cellGeomArr);
9136: VecRestoreArrayRead(facegeom, &faceGeomArr);
9137: PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);
9138: if (OrthQualLabel) {
9139: if (vwr) DMLabelView(*OrthQualLabel, vwr);
9140: }
9141: PetscFree5(idx, oqVals, ci, fi, Ai);
9142: PetscViewerDestroy(&vwr);
9143: VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");
9144: return 0;
9145: }
9147: /* this is here instead of DMGetOutputDM because output DM still has constraints in the local indices that affect
9148: * interpolator construction */
9149: static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
9150: {
9151: PetscSection section, newSection, gsection;
9152: PetscSF sf;
9153: PetscBool hasConstraints, ghasConstraints;
9157: DMGetLocalSection(dm, §ion);
9158: PetscSectionHasConstraints(section, &hasConstraints);
9159: MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm));
9160: if (!ghasConstraints) {
9161: PetscObjectReference((PetscObject)dm);
9162: *odm = dm;
9163: return 0;
9164: }
9165: DMClone(dm, odm);
9166: DMCopyFields(dm, *odm);
9167: DMGetLocalSection(*odm, &newSection);
9168: DMGetPointSF(*odm, &sf);
9169: PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);
9170: DMSetGlobalSection(*odm, gsection);
9171: PetscSectionDestroy(&gsection);
9172: return 0;
9173: }
9175: static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
9176: {
9177: DM dmco, dmfo;
9178: Mat interpo;
9179: Vec rscale;
9180: Vec cglobalo, clocal;
9181: Vec fglobal, fglobalo, flocal;
9182: PetscBool regular;
9184: DMGetFullDM(dmc, &dmco);
9185: DMGetFullDM(dmf, &dmfo);
9186: DMSetCoarseDM(dmfo, dmco);
9187: DMPlexGetRegularRefinement(dmf, ®ular);
9188: DMPlexSetRegularRefinement(dmfo, regular);
9189: DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);
9190: DMCreateGlobalVector(dmco, &cglobalo);
9191: DMCreateLocalVector(dmc, &clocal);
9192: VecSet(cglobalo, 0.);
9193: VecSet(clocal, 0.);
9194: DMCreateGlobalVector(dmf, &fglobal);
9195: DMCreateGlobalVector(dmfo, &fglobalo);
9196: DMCreateLocalVector(dmf, &flocal);
9197: VecSet(fglobal, 0.);
9198: VecSet(fglobalo, 0.);
9199: VecSet(flocal, 0.);
9200: DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);
9201: DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);
9202: DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);
9203: MatMult(interpo, cglobalo, fglobalo);
9204: DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);
9205: DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);
9206: DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);
9207: DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);
9208: *shift = fglobal;
9209: VecDestroy(&flocal);
9210: VecDestroy(&fglobalo);
9211: VecDestroy(&clocal);
9212: VecDestroy(&cglobalo);
9213: VecDestroy(&rscale);
9214: MatDestroy(&interpo);
9215: DMDestroy(&dmfo);
9216: DMDestroy(&dmco);
9217: return 0;
9218: }
9220: PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
9221: {
9222: PetscObject shifto;
9223: Vec shift;
9225: if (!interp) {
9226: Vec rscale;
9228: DMCreateInterpolation(coarse, fine, &interp, &rscale);
9229: VecDestroy(&rscale);
9230: } else {
9231: PetscObjectReference((PetscObject)interp);
9232: }
9233: PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto);
9234: if (!shifto) {
9235: DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift);
9236: PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject)shift);
9237: shifto = (PetscObject)shift;
9238: VecDestroy(&shift);
9239: }
9240: shift = (Vec)shifto;
9241: MatInterpolate(interp, coarseSol, fineSol);
9242: VecAXPY(fineSol, 1.0, shift);
9243: MatDestroy(&interp);
9244: return 0;
9245: }
9247: /* Pointwise interpolation
9248: Just code FEM for now
9249: u^f = I u^c
9250: sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
9251: u^f_i = sum_j psi^f_i I phi^c_j u^c_j
9252: I_{ij} = psi^f_i phi^c_j
9253: */
9254: PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
9255: {
9256: PetscSection gsc, gsf;
9257: PetscInt m, n;
9258: void *ctx;
9259: DM cdm;
9260: PetscBool regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;
9262: DMGetGlobalSection(dmFine, &gsf);
9263: PetscSectionGetConstrainedStorageSize(gsf, &m);
9264: DMGetGlobalSection(dmCoarse, &gsc);
9265: PetscSectionGetConstrainedStorageSize(gsc, &n);
9267: PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);
9268: MatCreate(PetscObjectComm((PetscObject)dmCoarse), interpolation);
9269: MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
9270: MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);
9271: DMGetApplicationContext(dmFine, &ctx);
9273: DMGetCoarseDM(dmFine, &cdm);
9274: DMPlexGetRegularRefinement(dmFine, ®ular);
9275: if (!isRefined || (regular && cdm == dmCoarse)) DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);
9276: else DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);
9277: MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");
9278: if (scaling) {
9279: /* Use naive scaling */
9280: DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);
9281: }
9282: return 0;
9283: }
9285: PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
9286: {
9287: VecScatter ctx;
9289: DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);
9290: MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);
9291: VecScatterDestroy(&ctx);
9292: return 0;
9293: }
9295: static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
9296: {
9297: const PetscInt Nc = uOff[1] - uOff[0];
9298: PetscInt c;
9299: for (c = 0; c < Nc; ++c) g0[c * Nc + c] = 1.0;
9300: }
9302: PetscErrorCode DMCreateMassMatrixLumped_Plex(DM dm, Vec *mass)
9303: {
9304: DM dmc;
9305: PetscDS ds;
9306: Vec ones, locmass;
9307: IS cellIS;
9308: PetscFormKey key;
9309: PetscInt depth;
9311: DMClone(dm, &dmc);
9312: DMCopyDisc(dm, dmc);
9313: DMGetDS(dmc, &ds);
9314: PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);
9315: DMCreateGlobalVector(dmc, mass);
9316: DMGetLocalVector(dmc, &ones);
9317: DMGetLocalVector(dmc, &locmass);
9318: DMPlexGetDepth(dmc, &depth);
9319: DMGetStratumIS(dmc, "depth", depth, &cellIS);
9320: VecSet(locmass, 0.0);
9321: VecSet(ones, 1.0);
9322: key.label = NULL;
9323: key.value = 0;
9324: key.field = 0;
9325: key.part = 0;
9326: DMPlexComputeJacobian_Action_Internal(dmc, key, cellIS, 0.0, 0.0, ones, NULL, ones, locmass, NULL);
9327: ISDestroy(&cellIS);
9328: VecSet(*mass, 0.0);
9329: DMLocalToGlobalBegin(dmc, locmass, ADD_VALUES, *mass);
9330: DMLocalToGlobalEnd(dmc, locmass, ADD_VALUES, *mass);
9331: DMRestoreLocalVector(dmc, &ones);
9332: DMRestoreLocalVector(dmc, &locmass);
9333: DMDestroy(&dmc);
9334: return 0;
9335: }
9337: PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
9338: {
9339: PetscSection gsc, gsf;
9340: PetscInt m, n;
9341: void *ctx;
9342: DM cdm;
9343: PetscBool regular;
9345: if (dmFine == dmCoarse) {
9346: DM dmc;
9347: PetscDS ds;
9348: PetscWeakForm wf;
9349: Vec u;
9350: IS cellIS;
9351: PetscFormKey key;
9352: PetscInt depth;
9354: DMClone(dmFine, &dmc);
9355: DMCopyDisc(dmFine, dmc);
9356: DMGetDS(dmc, &ds);
9357: PetscDSGetWeakForm(ds, &wf);
9358: PetscWeakFormClear(wf);
9359: PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);
9360: DMCreateMatrix(dmc, mass);
9361: DMGetLocalVector(dmc, &u);
9362: DMPlexGetDepth(dmc, &depth);
9363: DMGetStratumIS(dmc, "depth", depth, &cellIS);
9364: MatZeroEntries(*mass);
9365: key.label = NULL;
9366: key.value = 0;
9367: key.field = 0;
9368: key.part = 0;
9369: DMPlexComputeJacobian_Internal(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);
9370: ISDestroy(&cellIS);
9371: DMRestoreLocalVector(dmc, &u);
9372: DMDestroy(&dmc);
9373: } else {
9374: DMGetGlobalSection(dmFine, &gsf);
9375: PetscSectionGetConstrainedStorageSize(gsf, &m);
9376: DMGetGlobalSection(dmCoarse, &gsc);
9377: PetscSectionGetConstrainedStorageSize(gsc, &n);
9379: MatCreate(PetscObjectComm((PetscObject)dmCoarse), mass);
9380: MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
9381: MatSetType(*mass, dmCoarse->mattype);
9382: DMGetApplicationContext(dmFine, &ctx);
9384: DMGetCoarseDM(dmFine, &cdm);
9385: DMPlexGetRegularRefinement(dmFine, ®ular);
9386: if (regular && cdm == dmCoarse) DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);
9387: else DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);
9388: }
9389: MatViewFromOptions(*mass, NULL, "-mass_mat_view");
9390: return 0;
9391: }
9393: /*@
9394: DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
9396: Input Parameter:
9397: . dm - The `DMPLEX` object
9399: Output Parameter:
9400: . regular - The flag
9402: Level: intermediate
9404: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexSetRegularRefinement()`
9405: @*/
9406: PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
9407: {
9410: *regular = ((DM_Plex *)dm->data)->regularRefinement;
9411: return 0;
9412: }
9414: /*@
9415: DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
9417: Input Parameters:
9418: + dm - The `DMPLEX` object
9419: - regular - The flag
9421: Level: intermediate
9423: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetRegularRefinement()`
9424: @*/
9425: PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
9426: {
9428: ((DM_Plex *)dm->data)->regularRefinement = regular;
9429: return 0;
9430: }
9432: /*@
9433: DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints. Typically, the user will not have to
9434: call DMPlexGetAnchors() directly: if there are anchors, then `DMPlexGetAnchors()` is called during `DMGetDefaultConstraints()`.
9436: Not Collective
9438: Input Parameter:
9439: . dm - The `DMPLEX` object
9441: Output Parameters:
9442: + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
9443: - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
9445: Level: intermediate
9447: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexSetAnchors()`, `DMGetDefaultConstraints()`, `DMSetDefaultConstraints()`, `IS`, `PetscSection`
9448: @*/
9449: PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
9450: {
9451: DM_Plex *plex = (DM_Plex *)dm->data;
9454: if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) (*plex->createanchors)(dm);
9455: if (anchorSection) *anchorSection = plex->anchorSection;
9456: if (anchorIS) *anchorIS = plex->anchorIS;
9457: return 0;
9458: }
9460: /*@
9461: DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints. Unlike boundary conditions,
9462: when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
9463: point's degrees of freedom to be a linear combination of other points' degrees of freedom.
9465: Collective on dm
9467: Input Parameters:
9468: + dm - The `DMPLEX` object
9469: . anchorSection - The section that describes the mapping from constrained points to the anchor points listed in anchorIS.
9470: Must have a local communicator (`PETSC_COMM_SELF` or derivative).
9471: - anchorIS - The list of all anchor points. Must have a local communicator (`PETSC_COMM_SELF` or derivative).
9473: Level: intermediate
9475: Notes:
9476: After specifying the layout of constraints with `DMPlexSetAnchors()`, one specifies the constraints by calling
9477: `DMGetDefaultConstraints()` and filling in the entries in the constraint matrix.
9479: The reference counts of anchorSection and anchorIS are incremented.
9481: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetAnchors()`, `DMGetDefaultConstraints()`, `DMSetDefaultConstraints()`
9482: @*/
9483: PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
9484: {
9485: DM_Plex *plex = (DM_Plex *)dm->data;
9486: PetscMPIInt result;
9489: if (anchorSection) {
9491: MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)anchorSection), &result);
9493: }
9494: if (anchorIS) {
9496: MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)anchorIS), &result);
9498: }
9500: PetscObjectReference((PetscObject)anchorSection);
9501: PetscSectionDestroy(&plex->anchorSection);
9502: plex->anchorSection = anchorSection;
9504: PetscObjectReference((PetscObject)anchorIS);
9505: ISDestroy(&plex->anchorIS);
9506: plex->anchorIS = anchorIS;
9508: if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
9509: PetscInt size, a, pStart, pEnd;
9510: const PetscInt *anchors;
9512: PetscSectionGetChart(anchorSection, &pStart, &pEnd);
9513: ISGetLocalSize(anchorIS, &size);
9514: ISGetIndices(anchorIS, &anchors);
9515: for (a = 0; a < size; a++) {
9516: PetscInt p;
9518: p = anchors[a];
9519: if (p >= pStart && p < pEnd) {
9520: PetscInt dof;
9522: PetscSectionGetDof(anchorSection, p, &dof);
9523: if (dof) {
9524: ISRestoreIndices(anchorIS, &anchors);
9525: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Point %" PetscInt_FMT " cannot be constrained and an anchor", p);
9526: }
9527: }
9528: }
9529: ISRestoreIndices(anchorIS, &anchors);
9530: }
9531: /* reset the generic constraints */
9532: DMSetDefaultConstraints(dm, NULL, NULL, NULL);
9533: return 0;
9534: }
9536: static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
9537: {
9538: PetscSection anchorSection;
9539: PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
9542: DMPlexGetAnchors(dm, &anchorSection, NULL);
9543: PetscSectionCreate(PETSC_COMM_SELF, cSec);
9544: PetscSectionGetNumFields(section, &numFields);
9545: if (numFields) {
9546: PetscInt f;
9547: PetscSectionSetNumFields(*cSec, numFields);
9549: for (f = 0; f < numFields; f++) {
9550: PetscInt numComp;
9552: PetscSectionGetFieldComponents(section, f, &numComp);
9553: PetscSectionSetFieldComponents(*cSec, f, numComp);
9554: }
9555: }
9556: PetscSectionGetChart(anchorSection, &pStart, &pEnd);
9557: PetscSectionGetChart(section, &sStart, &sEnd);
9558: pStart = PetscMax(pStart, sStart);
9559: pEnd = PetscMin(pEnd, sEnd);
9560: pEnd = PetscMax(pStart, pEnd);
9561: PetscSectionSetChart(*cSec, pStart, pEnd);
9562: for (p = pStart; p < pEnd; p++) {
9563: PetscSectionGetDof(anchorSection, p, &dof);
9564: if (dof) {
9565: PetscSectionGetDof(section, p, &dof);
9566: PetscSectionSetDof(*cSec, p, dof);
9567: for (f = 0; f < numFields; f++) {
9568: PetscSectionGetFieldDof(section, p, f, &dof);
9569: PetscSectionSetFieldDof(*cSec, p, f, dof);
9570: }
9571: }
9572: }
9573: PetscSectionSetUp(*cSec);
9574: PetscObjectSetName((PetscObject)*cSec, "Constraint Section");
9575: return 0;
9576: }
9578: static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
9579: {
9580: PetscSection aSec;
9581: PetscInt pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
9582: const PetscInt *anchors;
9583: PetscInt numFields, f;
9584: IS aIS;
9585: MatType mtype;
9586: PetscBool iscuda, iskokkos;
9589: PetscSectionGetStorageSize(cSec, &m);
9590: PetscSectionGetStorageSize(section, &n);
9591: MatCreate(PETSC_COMM_SELF, cMat);
9592: MatSetSizes(*cMat, m, n, m, n);
9593: PetscStrcmp(dm->mattype, MATSEQAIJCUSPARSE, &iscuda);
9594: if (!iscuda) PetscStrcmp(dm->mattype, MATMPIAIJCUSPARSE, &iscuda);
9595: PetscStrcmp(dm->mattype, MATSEQAIJKOKKOS, &iskokkos);
9596: if (!iskokkos) PetscStrcmp(dm->mattype, MATMPIAIJKOKKOS, &iskokkos);
9597: if (iscuda) mtype = MATSEQAIJCUSPARSE;
9598: else if (iskokkos) mtype = MATSEQAIJKOKKOS;
9599: else mtype = MATSEQAIJ;
9600: MatSetType(*cMat, mtype);
9601: DMPlexGetAnchors(dm, &aSec, &aIS);
9602: ISGetIndices(aIS, &anchors);
9603: /* cSec will be a subset of aSec and section */
9604: PetscSectionGetChart(cSec, &pStart, &pEnd);
9605: PetscSectionGetChart(section, &sStart, &sEnd);
9606: PetscMalloc1(m + 1, &i);
9607: i[0] = 0;
9608: PetscSectionGetNumFields(section, &numFields);
9609: for (p = pStart; p < pEnd; p++) {
9610: PetscInt rDof, rOff, r;
9612: PetscSectionGetDof(aSec, p, &rDof);
9613: if (!rDof) continue;
9614: PetscSectionGetOffset(aSec, p, &rOff);
9615: if (numFields) {
9616: for (f = 0; f < numFields; f++) {
9617: annz = 0;
9618: for (r = 0; r < rDof; r++) {
9619: a = anchors[rOff + r];
9620: if (a < sStart || a >= sEnd) continue;
9621: PetscSectionGetFieldDof(section, a, f, &aDof);
9622: annz += aDof;
9623: }
9624: PetscSectionGetFieldDof(cSec, p, f, &dof);
9625: PetscSectionGetFieldOffset(cSec, p, f, &off);
9626: for (q = 0; q < dof; q++) i[off + q + 1] = i[off + q] + annz;
9627: }
9628: } else {
9629: annz = 0;
9630: PetscSectionGetDof(cSec, p, &dof);
9631: for (q = 0; q < dof; q++) {
9632: a = anchors[rOff + q];
9633: if (a < sStart || a >= sEnd) continue;
9634: PetscSectionGetDof(section, a, &aDof);
9635: annz += aDof;
9636: }
9637: PetscSectionGetDof(cSec, p, &dof);
9638: PetscSectionGetOffset(cSec, p, &off);
9639: for (q = 0; q < dof; q++) i[off + q + 1] = i[off + q] + annz;
9640: }
9641: }
9642: nnz = i[m];
9643: PetscMalloc1(nnz, &j);
9644: offset = 0;
9645: for (p = pStart; p < pEnd; p++) {
9646: if (numFields) {
9647: for (f = 0; f < numFields; f++) {
9648: PetscSectionGetFieldDof(cSec, p, f, &dof);
9649: for (q = 0; q < dof; q++) {
9650: PetscInt rDof, rOff, r;
9651: PetscSectionGetDof(aSec, p, &rDof);
9652: PetscSectionGetOffset(aSec, p, &rOff);
9653: for (r = 0; r < rDof; r++) {
9654: PetscInt s;
9656: a = anchors[rOff + r];
9657: if (a < sStart || a >= sEnd) continue;
9658: PetscSectionGetFieldDof(section, a, f, &aDof);
9659: PetscSectionGetFieldOffset(section, a, f, &aOff);
9660: for (s = 0; s < aDof; s++) j[offset++] = aOff + s;
9661: }
9662: }
9663: }
9664: } else {
9665: PetscSectionGetDof(cSec, p, &dof);
9666: for (q = 0; q < dof; q++) {
9667: PetscInt rDof, rOff, r;
9668: PetscSectionGetDof(aSec, p, &rDof);
9669: PetscSectionGetOffset(aSec, p, &rOff);
9670: for (r = 0; r < rDof; r++) {
9671: PetscInt s;
9673: a = anchors[rOff + r];
9674: if (a < sStart || a >= sEnd) continue;
9675: PetscSectionGetDof(section, a, &aDof);
9676: PetscSectionGetOffset(section, a, &aOff);
9677: for (s = 0; s < aDof; s++) j[offset++] = aOff + s;
9678: }
9679: }
9680: }
9681: }
9682: MatSeqAIJSetPreallocationCSR(*cMat, i, j, NULL);
9683: PetscFree(i);
9684: PetscFree(j);
9685: ISRestoreIndices(aIS, &anchors);
9686: return 0;
9687: }
9689: PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
9690: {
9691: DM_Plex *plex = (DM_Plex *)dm->data;
9692: PetscSection anchorSection, section, cSec;
9693: Mat cMat;
9696: DMPlexGetAnchors(dm, &anchorSection, NULL);
9697: if (anchorSection) {
9698: PetscInt Nf;
9700: DMGetLocalSection(dm, §ion);
9701: DMPlexCreateConstraintSection_Anchors(dm, section, &cSec);
9702: DMPlexCreateConstraintMatrix_Anchors(dm, section, cSec, &cMat);
9703: DMGetNumFields(dm, &Nf);
9704: if (Nf && plex->computeanchormatrix) (*plex->computeanchormatrix)(dm, section, cSec, cMat);
9705: DMSetDefaultConstraints(dm, cSec, cMat, NULL);
9706: PetscSectionDestroy(&cSec);
9707: MatDestroy(&cMat);
9708: }
9709: return 0;
9710: }
9712: PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
9713: {
9714: IS subis;
9715: PetscSection section, subsection;
9717: DMGetLocalSection(dm, §ion);
9720: /* Create subdomain */
9721: DMPlexFilter(dm, label, value, subdm);
9722: /* Create submodel */
9723: DMPlexGetSubpointIS(*subdm, &subis);
9724: PetscSectionCreateSubmeshSection(section, subis, &subsection);
9725: DMSetLocalSection(*subdm, subsection);
9726: PetscSectionDestroy(&subsection);
9727: DMCopyDisc(dm, *subdm);
9728: /* Create map from submodel to global model */
9729: if (is) {
9730: PetscSection sectionGlobal, subsectionGlobal;
9731: IS spIS;
9732: const PetscInt *spmap;
9733: PetscInt *subIndices;
9734: PetscInt subSize = 0, subOff = 0, pStart, pEnd, p;
9735: PetscInt Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
9737: DMPlexGetSubpointIS(*subdm, &spIS);
9738: ISGetIndices(spIS, &spmap);
9739: PetscSectionGetNumFields(section, &Nf);
9740: DMGetGlobalSection(dm, §ionGlobal);
9741: DMGetGlobalSection(*subdm, &subsectionGlobal);
9742: PetscSectionGetChart(subsection, &pStart, &pEnd);
9743: for (p = pStart; p < pEnd; ++p) {
9744: PetscInt gdof, pSubSize = 0;
9746: PetscSectionGetDof(sectionGlobal, p, &gdof);
9747: if (gdof > 0) {
9748: for (f = 0; f < Nf; ++f) {
9749: PetscInt fdof, fcdof;
9751: PetscSectionGetFieldDof(subsection, p, f, &fdof);
9752: PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);
9753: pSubSize += fdof - fcdof;
9754: }
9755: subSize += pSubSize;
9756: if (pSubSize) {
9757: if (bs < 0) {
9758: bs = pSubSize;
9759: } else if (bs != pSubSize) {
9760: /* Layout does not admit a pointwise block size */
9761: bs = 1;
9762: }
9763: }
9764: }
9765: }
9766: /* Must have same blocksize on all procs (some might have no points) */
9767: bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
9768: bsLocal[1] = bs;
9769: PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax);
9770: if (bsMinMax[0] != bsMinMax[1]) {
9771: bs = 1;
9772: } else {
9773: bs = bsMinMax[0];
9774: }
9775: PetscMalloc1(subSize, &subIndices);
9776: for (p = pStart; p < pEnd; ++p) {
9777: PetscInt gdof, goff;
9779: PetscSectionGetDof(subsectionGlobal, p, &gdof);
9780: if (gdof > 0) {
9781: const PetscInt point = spmap[p];
9783: PetscSectionGetOffset(sectionGlobal, point, &goff);
9784: for (f = 0; f < Nf; ++f) {
9785: PetscInt fdof, fcdof, fc, f2, poff = 0;
9787: /* Can get rid of this loop by storing field information in the global section */
9788: for (f2 = 0; f2 < f; ++f2) {
9789: PetscSectionGetFieldDof(section, p, f2, &fdof);
9790: PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);
9791: poff += fdof - fcdof;
9792: }
9793: PetscSectionGetFieldDof(section, p, f, &fdof);
9794: PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);
9795: for (fc = 0; fc < fdof - fcdof; ++fc, ++subOff) subIndices[subOff] = goff + poff + fc;
9796: }
9797: }
9798: }
9799: ISRestoreIndices(spIS, &spmap);
9800: ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);
9801: if (bs > 1) {
9802: /* We need to check that the block size does not come from non-contiguous fields */
9803: PetscInt i, j, set = 1;
9804: for (i = 0; i < subSize; i += bs) {
9805: for (j = 0; j < bs; ++j) {
9806: if (subIndices[i + j] != subIndices[i] + j) {
9807: set = 0;
9808: break;
9809: }
9810: }
9811: }
9812: if (set) ISSetBlockSize(*is, bs);
9813: }
9814: /* Attach nullspace */
9815: for (f = 0; f < Nf; ++f) {
9816: (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
9817: if ((*subdm)->nullspaceConstructors[f]) break;
9818: }
9819: if (f < Nf) {
9820: MatNullSpace nullSpace;
9821: (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);
9823: PetscObjectCompose((PetscObject)*is, "nullspace", (PetscObject)nullSpace);
9824: MatNullSpaceDestroy(&nullSpace);
9825: }
9826: }
9827: return 0;
9828: }
9830: /*@
9831: DMPlexMonitorThroughput - Report the cell throughput of FE integration
9833: Input Parameters:
9834: + dm - The `DM`
9835: - dummy - unused argument
9837: Options Database Key:
9838: . -dm_plex_monitor_throughput - Activate the monitor
9840: Level: developer
9842: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreate()`
9843: @*/
9844: PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
9845: {
9846: #if defined(PETSC_USE_LOG)
9847: PetscStageLog stageLog;
9848: PetscLogEvent event;
9849: PetscLogStage stage;
9850: PetscEventPerfInfo eventInfo;
9851: PetscReal cellRate, flopRate;
9852: PetscInt cStart, cEnd, Nf, N;
9853: const char *name;
9854: #endif
9857: #if defined(PETSC_USE_LOG)
9858: PetscObjectGetName((PetscObject)dm, &name);
9859: DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
9860: DMGetNumFields(dm, &Nf);
9861: PetscLogGetStageLog(&stageLog);
9862: PetscStageLogGetCurrent(stageLog, &stage);
9863: PetscLogEventGetId("DMPlexResidualFE", &event);
9864: PetscLogEventGetPerfInfo(stage, event, &eventInfo);
9865: N = (cEnd - cStart) * Nf * eventInfo.count;
9866: flopRate = eventInfo.flops / eventInfo.time;
9867: cellRate = N / eventInfo.time;
9868: PetscPrintf(PetscObjectComm((PetscObject)dm), "DM (%s) FE Residual Integration: %" PetscInt_FMT " integrals %d reps\n Cell rate: %.2g/s flop rate: %.2g MF/s\n", name ? name : "unknown", N, eventInfo.count, (double)cellRate, (double)(flopRate / 1.e6));
9869: #else
9870: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
9871: #endif
9872: return 0;
9873: }