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>
11: #include <petscblaslapack.h>
13: /* Logging support */
14: PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeMultistage, 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_CreateBoxSFC, 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;
15: PetscLogEvent DMPLEX_RebalBuildGraph, DMPLEX_RebalRewriteSF, DMPLEX_RebalGatherGraph, DMPLEX_RebalPartition, DMPLEX_RebalScatterPart, DMPLEX_Generate, DMPLEX_GetLocalOffsets, DMPLEX_Uninterpolate;
16: PetscLogEvent DMPLEX_DistributionView, DMPLEX_DistributionLoad;
18: PetscBool Plexcite = PETSC_FALSE;
19: const char PlexCitation[] = "@article{LangeMitchellKnepleyGorman2015,\n"
20: "title = {Efficient mesh management in {Firedrake} using {PETSc-DMPlex}},\n"
21: "author = {Michael Lange and Lawrence Mitchell and Matthew G. Knepley and Gerard J. Gorman},\n"
22: "journal = {SIAM Journal on Scientific Computing},\n"
23: "volume = {38},\n"
24: "number = {5},\n"
25: "pages = {S143--S155},\n"
26: "eprint = {http://arxiv.org/abs/1506.07749},\n"
27: "doi = {10.1137/15M1026092},\n"
28: "year = {2016},\n"
29: "petsc_uses={DMPlex},\n}\n";
31: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
33: /*@
34: DMPlexIsSimplex - Is the first cell in this mesh a simplex?
36: Input Parameter:
37: . dm - The `DMPLEX` object
39: Output Parameter:
40: . simplex - Flag checking for a simplex
42: Level: intermediate
44: Note:
45: This just gives the first range of cells found. If the mesh has several cell types, it will only give the first.
46: If the mesh has no cells, this returns `PETSC_FALSE`.
48: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSimplexOrBoxCells()`, `DMPlexGetCellType()`, `DMPlexGetHeightStratum()`, `DMPolytopeTypeGetNumVertices()`
49: @*/
50: PetscErrorCode DMPlexIsSimplex(DM dm, PetscBool *simplex)
51: {
52: DMPolytopeType ct;
53: PetscInt cStart, cEnd;
55: PetscFunctionBegin;
56: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
57: if (cEnd <= cStart) {
58: *simplex = PETSC_FALSE;
59: PetscFunctionReturn(PETSC_SUCCESS);
60: }
61: PetscCall(DMPlexGetCellType(dm, cStart, &ct));
62: *simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
63: PetscFunctionReturn(PETSC_SUCCESS);
64: }
66: /*@
67: DMPlexGetSimplexOrBoxCells - Get the range of cells which are neither prisms nor ghost FV cells
69: Input Parameters:
70: + dm - The `DMPLEX` object
71: - height - The cell height in the Plex, 0 is the default
73: Output Parameters:
74: + cStart - The first "normal" cell, pass `NULL` if not needed
75: - cEnd - The upper bound on "normal" cells, pass `NULL` if not needed
77: Level: developer
79: Note:
80: This function requires that tensor cells are ordered last.
82: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructGhostCells()`, `DMPlexGetCellTypeStratum()`
83: @*/
84: PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PeOp PetscInt *cStart, PeOp PetscInt *cEnd)
85: {
86: DMLabel ctLabel;
87: IS valueIS;
88: const PetscInt *ctypes;
89: PetscBool found = PETSC_FALSE;
90: PetscInt Nct, cS = PETSC_INT_MAX, cE = 0;
92: PetscFunctionBegin;
93: PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
94: PetscCall(DMLabelGetValueIS(ctLabel, &valueIS));
95: PetscCall(ISGetLocalSize(valueIS, &Nct));
96: PetscCall(ISGetIndices(valueIS, &ctypes));
97: for (PetscInt t = 0; t < Nct; ++t) {
98: const DMPolytopeType ct = (DMPolytopeType)ctypes[t];
99: PetscInt ctS, ctE, ht;
101: if (ct == DM_POLYTOPE_UNKNOWN) {
102: // If any cells are not typed, just use all cells
103: PetscCall(DMPlexGetHeightStratum(dm, PetscMax(height, 0), cStart, cEnd));
104: break;
105: }
106: if (DMPolytopeTypeIsHybrid(ct) || ct == DM_POLYTOPE_FV_GHOST) continue;
107: PetscCall(DMLabelGetStratumBounds(ctLabel, ct, &ctS, &ctE));
108: if (ctS >= ctE) continue;
109: // Check that a point has the right height
110: PetscCall(DMPlexGetPointHeight(dm, ctS, &ht));
111: if (ht != height) continue;
112: cS = PetscMin(cS, ctS);
113: cE = PetscMax(cE, ctE);
114: found = PETSC_TRUE;
115: }
116: if (!Nct || !found) cS = cE = 0;
117: PetscCall(ISDestroy(&valueIS));
118: // Reset label for fast lookup
119: PetscCall(DMLabelMakeAllInvalid_Internal(ctLabel));
120: if (cStart) *cStart = cS;
121: if (cEnd) *cEnd = cE;
122: PetscFunctionReturn(PETSC_SUCCESS);
123: }
125: PetscErrorCode DMPlexGetFieldTypes_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *types, PetscInt **ssStart, PetscInt **ssEnd, PetscViewerVTKFieldType **sft)
126: {
127: PetscInt cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd, c, depth, cellHeight, t;
128: PetscInt *sStart, *sEnd;
129: PetscViewerVTKFieldType *ft;
130: PetscInt vcdof[DM_NUM_POLYTOPES + 1], globalvcdof[DM_NUM_POLYTOPES + 1];
131: DMLabel depthLabel, ctLabel;
133: PetscFunctionBegin;
134: /* the vcdof and globalvcdof are sized to allow every polytope type and simple vertex at DM_NUM_POLYTOPES */
135: PetscCall(PetscArrayzero(vcdof, DM_NUM_POLYTOPES + 1));
136: PetscCall(DMGetCoordinateDim(dm, &cdim));
137: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
138: PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
139: if (field >= 0) {
140: if ((vStart >= pStart) && (vStart < pEnd)) PetscCall(PetscSectionGetFieldDof(section, vStart, field, &vcdof[DM_NUM_POLYTOPES]));
141: } else {
142: if ((vStart >= pStart) && (vStart < pEnd)) PetscCall(PetscSectionGetDof(section, vStart, &vcdof[DM_NUM_POLYTOPES]));
143: }
145: PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
146: PetscCall(DMPlexGetDepth(dm, &depth));
147: PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
148: PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
149: for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
150: const DMPolytopeType ict = (DMPolytopeType)c;
151: PetscInt dep;
153: if (ict == DM_POLYTOPE_FV_GHOST) continue;
154: PetscCall(DMLabelGetStratumBounds(ctLabel, ict, &cStart, &cEnd));
155: if (pStart >= 0) {
156: PetscCall(DMLabelGetValue(depthLabel, cStart, &dep));
157: if (dep != depth - cellHeight) continue;
158: }
159: if (field >= 0) {
160: if ((cStart >= pStart) && (cStart < pEnd)) PetscCall(PetscSectionGetFieldDof(section, cStart, field, &vcdof[c]));
161: } else {
162: if ((cStart >= pStart) && (cStart < pEnd)) PetscCall(PetscSectionGetDof(section, cStart, &vcdof[c]));
163: }
164: }
166: PetscCallMPI(MPIU_Allreduce(vcdof, globalvcdof, DM_NUM_POLYTOPES + 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
167: *types = 0;
169: for (c = 0; c < DM_NUM_POLYTOPES + 1; ++c) {
170: if (globalvcdof[c]) ++(*types);
171: }
173: PetscCall(PetscMalloc3(*types, &sStart, *types, &sEnd, *types, &ft));
174: t = 0;
175: if (globalvcdof[DM_NUM_POLYTOPES]) {
176: sStart[t] = vStart;
177: sEnd[t] = vEnd;
178: ft[t] = (globalvcdof[t] == cdim) ? PETSC_VTK_POINT_VECTOR_FIELD : PETSC_VTK_POINT_FIELD;
179: ++t;
180: }
182: for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
183: if (globalvcdof[c]) {
184: const DMPolytopeType ict = (DMPolytopeType)c;
186: PetscCall(DMLabelGetStratumBounds(ctLabel, ict, &cStart, &cEnd));
187: sStart[t] = cStart;
188: sEnd[t] = cEnd;
189: ft[t] = (globalvcdof[c] == cdim) ? PETSC_VTK_CELL_VECTOR_FIELD : PETSC_VTK_CELL_FIELD;
190: ++t;
191: }
192: }
194: if (!*types) {
195: if (field >= 0) {
196: const char *fieldname;
198: PetscCall(PetscSectionGetFieldName(section, field, &fieldname));
199: PetscCall(PetscInfo((PetscObject)dm, "Could not classify VTK output type of section field %" PetscInt_FMT " \"%s\"\n", field, fieldname));
200: } else {
201: PetscCall(PetscInfo((PetscObject)dm, "Could not classify VTK output type of section\n"));
202: }
203: }
205: *ssStart = sStart;
206: *ssEnd = sEnd;
207: *sft = ft;
208: PetscFunctionReturn(PETSC_SUCCESS);
209: }
211: PetscErrorCode DMPlexRestoreFieldTypes_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *types, PetscInt **sStart, PetscInt **sEnd, PetscViewerVTKFieldType **ft)
212: {
213: PetscFunctionBegin;
214: PetscCall(PetscFree3(*sStart, *sEnd, *ft));
215: PetscFunctionReturn(PETSC_SUCCESS);
216: }
218: PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
219: {
220: PetscInt cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd;
221: PetscInt vcdof[2] = {0, 0}, globalvcdof[2];
223: PetscFunctionBegin;
224: *ft = PETSC_VTK_INVALID;
225: PetscCall(DMGetCoordinateDim(dm, &cdim));
226: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
227: PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
228: PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
229: if (field >= 0) {
230: if ((vStart >= pStart) && (vStart < pEnd)) PetscCall(PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]));
231: if ((cStart >= pStart) && (cStart < pEnd)) PetscCall(PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]));
232: } else {
233: if ((vStart >= pStart) && (vStart < pEnd)) PetscCall(PetscSectionGetDof(section, vStart, &vcdof[0]));
234: if ((cStart >= pStart) && (cStart < pEnd)) PetscCall(PetscSectionGetDof(section, cStart, &vcdof[1]));
235: }
236: PetscCallMPI(MPIU_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
237: if (globalvcdof[0]) {
238: *sStart = vStart;
239: *sEnd = vEnd;
240: if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
241: else *ft = PETSC_VTK_POINT_FIELD;
242: } else if (globalvcdof[1]) {
243: *sStart = cStart;
244: *sEnd = cEnd;
245: if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
246: else *ft = PETSC_VTK_CELL_FIELD;
247: } else {
248: if (field >= 0) {
249: const char *fieldname;
251: PetscCall(PetscSectionGetFieldName(section, field, &fieldname));
252: PetscCall(PetscInfo(dm, "Could not classify VTK output type of section field %" PetscInt_FMT " \"%s\"\n", field, fieldname));
253: } else {
254: PetscCall(PetscInfo(dm, "Could not classify VTK output type of section\n"));
255: }
256: }
257: PetscFunctionReturn(PETSC_SUCCESS);
258: }
260: static PetscErrorCode DMPlexVecFFT1D_Internal(DM dm, PetscBool removeDC, PetscInt n, Vec u, Vec uhat)
261: {
262: Mat FT;
263: Vec fftX, fftY;
264: IS fftReal;
265: PetscInt N;
267: PetscFunctionBegin;
268: PetscCall(VecDuplicate(u, &uhat));
269: PetscCall(VecGetSize(u, &N));
270: PetscCall(MatCreateFFT(PetscObjectComm((PetscObject)dm), 1, &N, MATFFTW, &FT));
271: PetscCall(MatCreateVecs(FT, &fftX, &fftY));
272: PetscCall(ISCreateStride(PETSC_COMM_SELF, N, 0, 1, &fftReal));
274: PetscCall(VecISCopy(fftX, fftReal, SCATTER_FORWARD, u));
275: PetscCall(MatMult(FT, fftX, fftY));
276: PetscCall(VecFilter(fftY, PETSC_SMALL));
277: PetscCall(VecISCopy(fftY, fftReal, SCATTER_REVERSE, uhat));
278: if (removeDC) PetscCall(VecSetValue(uhat, 0, 0., INSERT_VALUES)); // Remove DC component
280: PetscCall(MatDestroy(&FT));
281: PetscCall(VecDestroy(&fftX));
282: PetscCall(VecDestroy(&fftY));
283: PetscCall(ISDestroy(&fftReal));
284: PetscFunctionReturn(PETSC_SUCCESS);
285: }
287: /*@
288: DMPlexVecView1D - Plot many 1D solutions on the same line graph
290: Collective
292: Input Parameters:
293: + dm - The `DMPLEX` object
294: . n - The number of vectors
295: . u - The array of local vectors
296: - viewer - The `PetscViewer`
298: Level: advanced
300: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `VecViewFromOptions()`, `VecView()`
301: @*/
302: PetscErrorCode DMPlexVecView1D(DM dm, PetscInt n, Vec u[], PetscViewer viewer)
303: {
304: DM cdm;
305: PetscDS ds;
306: PetscSection s;
307: Vec *uhat; // Fourier transform of each vector u[i]
308: Vec coordinates; // Local vector of mesh coordinate
309: const PetscScalar *coords; // Coordinate values
310: const PetscScalar **sol; // Arrays from each vector u[i]
311: char **names; // Names for each component of each vector
312: PetscReal *vals; // Values at a point for each component of each vector
313: PetscInt *Nc; // Number of components for each field
314: PetscInt Ntc; // Total number of components across all fields
315: PetscInt Nw; // Number of plots
316: PetscInt cdof; // Total number of cell dofs
317: PetscInt vdof; // Total number of vertex dofs
318: PetscInt k; // The Lagrange degree, and drawing mode
319: PetscInt Nf, vStart, vEnd, eStart, eEnd;
320: PetscBool separateCmp = PETSC_TRUE; // Plot components of fields
321: PetscBool fft = PETSC_FALSE; // Fourier Transform the field before plotting
322: PetscBool removeDC = PETSC_FALSE; // Remove DC component before plotting
324: PetscFunctionBegin;
325: PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_1d_fft", &fft, NULL));
326: PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_1d_components", &separateCmp, NULL));
327: PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_1d_remove_dc", &removeDC, NULL));
328: if (!n) fft = PETSC_FALSE;
329: if (fft) {
330: PetscCall(PetscMalloc1(n, &uhat));
331: for (PetscInt i = 0; i < n; ++i) {
332: PetscCall(VecDuplicate(u[i], &uhat[i]));
333: PetscCall(DMPlexVecFFT1D_Internal(dm, removeDC, 1, u[i], uhat[i]));
334: }
335: }
336: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
337: PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
338: PetscCall(DMGetCoordinateDM(dm, &cdm));
339: PetscCall(DMGetDS(dm, &ds));
340: PetscCall(PetscDSGetNumFields(ds, &Nf));
341: PetscCall(PetscDSGetTotalComponents(ds, &Ntc));
342: PetscCall(PetscDSGetComponents(ds, &Nc));
344: PetscCall(DMGetLocalSection(dm, &s));
345: PetscCall(PetscSectionGetDof(s, eStart, &cdof));
346: PetscCall(PetscSectionGetDof(s, vStart, &vdof));
347: PetscCheck(cdof || vdof, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unsupported discretization");
348: if (cdof && vdof) k = 2;
349: else if (vdof) k = 1;
350: else if (cdof) k = 0;
352: PetscCall(PetscMalloc3(n, &sol, n * Ntc, &names, n * Ntc, &vals));
353: for (PetscInt i = 0, l = 0; i < n; ++i) {
354: const char *vname;
356: PetscCall(PetscObjectGetName((PetscObject)u[i], &vname));
357: for (PetscInt f = 0; f < Nf; ++f) {
358: PetscObject disc;
359: const char *fname;
360: char tmpname[PETSC_MAX_PATH_LEN];
362: PetscCall(PetscDSGetDiscretization(ds, f, &disc));
363: /* TODO Create names for components */
364: for (PetscInt c = 0; c < Nc[f]; ++c, ++l) {
365: PetscCall(PetscObjectGetName(disc, &fname));
366: PetscCall(PetscStrncpy(tmpname, vname, sizeof(tmpname)));
367: PetscCall(PetscStrlcat(tmpname, ":", sizeof(tmpname)));
368: PetscCall(PetscStrlcat(tmpname, fname, sizeof(tmpname)));
369: PetscCall(PetscStrallocpy(tmpname, &names[l]));
370: }
371: }
372: }
374: PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
375: PetscCall(VecGetArrayRead(coordinates, &coords));
376: for (PetscInt i = 0; i < n; ++i) PetscCall(VecGetArrayRead(fft ? uhat[i] : u[i], &sol[i]));
378: PetscDrawLG *lg;
380: Nw = separateCmp ? Ntc : 1;
381: PetscCall(PetscMalloc1(Nw, &lg));
382: for (PetscInt w = 0; w < Nw; ++w) {
383: PetscDraw draw = NULL;
384: const PetscInt Nl = separateCmp ? 1 : Ntc;
385: PetscInt vFirst = -1;
386: PetscInt field = 0;
387: PetscInt cmp = 0;
388: PetscInt tcmp = 0;
390: if (separateCmp) {
391: for (PetscInt f = 0; f < Nf; ++f) {
392: PetscInt c;
394: for (c = 0; c < Nc[f]; ++c, ++tcmp) {
395: if (tcmp == w) {
396: cmp = c;
397: break;
398: }
399: }
400: if (c < Nc[f]) {
401: field = f;
402: break;
403: }
404: }
405: }
406: PetscCall(PetscViewerDrawGetDraw(viewer, w, &draw));
407: if (!draw) PetscFunctionReturn(PETSC_SUCCESS);
408: PetscCall(PetscDrawLGCreate(draw, n * Nl, &lg[w]));
410: PetscCall(PetscDrawLGSetLegend(lg[w], (const char *const *)&names[w]));
411: switch (k) {
412: case 0:
413: for (PetscInt e = eStart; e < eEnd; ++e) {
414: PetscScalar *xa, *xb, *svals;
415: const PetscInt *cone;
417: PetscCall(DMPlexGetCone(dm, e, &cone));
418: PetscCall(DMPlexPointLocalRead(cdm, cone[0], coords, &xa));
419: PetscCall(DMPlexPointLocalRead(cdm, cone[1], coords, &xb));
420: for (PetscInt i = 0; i < n; ++i) {
421: if (separateCmp) {
422: PetscCall(DMPlexPointLocalFieldRead(dm, e, field, sol[i], &svals));
423: vals[i] = PetscRealPart(svals[cmp]);
424: } else {
425: PetscCall(DMPlexPointLocalRead(dm, e, sol[i], &svals));
426: for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
427: }
428: }
429: PetscCall(PetscDrawLGAddCommonPoint(lg[w], 0.5 * (PetscRealPart(xa[0]) + PetscRealPart(xb[0])), vals));
430: }
431: break;
432: case 1:
433: for (PetscInt v = vStart; v < vEnd; ++v) {
434: PetscScalar *x, *svals;
436: PetscCall(DMPlexPointLocalRead(cdm, v, coords, &x));
437: for (PetscInt i = 0; i < n; ++i) {
438: if (separateCmp) {
439: PetscCall(DMPlexPointLocalFieldRead(dm, v, field, sol[i], &svals));
440: vals[i] = PetscRealPart(svals[cmp]);
441: } else {
442: PetscCall(DMPlexPointLocalRead(dm, v, sol[i], &svals));
443: for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
444: }
445: }
446: PetscCall(PetscDrawLGAddCommonPoint(lg[w], PetscRealPart(x[0]), vals));
447: }
448: break;
449: case 2:
450: for (PetscInt e = eStart; e < eEnd; ++e) {
451: PetscScalar *xa, *xb, *svals;
452: const PetscInt *cone;
454: PetscCall(DMPlexGetCone(dm, e, &cone));
455: PetscCall(DMPlexPointLocalRead(cdm, cone[0], coords, &xa));
456: PetscCall(DMPlexPointLocalRead(cdm, cone[1], coords, &xb));
457: if (e == eStart) vFirst = cone[0];
458: for (PetscInt i = 0; i < n; ++i) {
459: if (separateCmp) {
460: PetscCall(DMPlexPointLocalFieldRead(dm, cone[0], field, sol[i], &svals));
461: vals[i] = PetscRealPart(svals[cmp]);
462: } else {
463: PetscCall(DMPlexPointLocalRead(dm, cone[0], sol[i], &svals));
464: for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
465: }
466: }
467: PetscCall(PetscDrawLGAddCommonPoint(lg[w], PetscRealPart(xa[0]), vals));
468: if (e == eEnd - 1 && cone[1] != vFirst) {
469: for (PetscInt i = 0; i < n; ++i) {
470: if (separateCmp) {
471: PetscCall(DMPlexPointLocalFieldRead(dm, e, field, sol[i], &svals));
472: vals[i] = PetscRealPart(svals[cmp]);
473: } else {
474: PetscCall(DMPlexPointLocalRead(dm, e, sol[i], &svals));
475: for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
476: }
477: }
478: PetscCall(PetscDrawLGAddCommonPoint(lg[w], 0.5 * (PetscRealPart(xa[0]) + PetscRealPart(xb[0])), vals));
479: for (PetscInt i = 0; i < n; ++i) {
480: if (separateCmp) {
481: PetscCall(DMPlexPointLocalFieldRead(dm, cone[1], field, sol[i], &svals));
482: vals[i] = PetscRealPart(svals[cmp]);
483: } else {
484: PetscCall(DMPlexPointLocalRead(dm, cone[1], sol[i], &svals));
485: for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
486: }
487: }
488: PetscCall(PetscDrawLGAddCommonPoint(lg[w], PetscRealPart(xb[0]), vals));
489: }
490: }
491: break;
492: default:
493: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid value of k: %" PetscInt_FMT, k);
494: }
495: }
496: PetscCall(VecRestoreArrayRead(coordinates, &coords));
497: for (PetscInt i = 0; i < n; ++i) PetscCall(VecRestoreArrayRead(fft ? uhat[i] : u[i], &sol[i]));
498: if (fft) {
499: for (PetscInt i = 0; i < n; ++i) PetscCall(VecDestroy(&uhat[i]));
500: PetscCall(PetscFree(uhat));
501: }
502: for (PetscInt l = 0; l < n * Ntc; ++l) PetscCall(PetscFree(names[l]));
503: PetscCall(PetscFree3(sol, names, vals));
504: for (PetscInt w = 0; w < Nw; ++w) {
505: PetscCall(PetscDrawLGDraw(lg[w]));
506: PetscCall(PetscDrawLGDestroy(&lg[w]));
507: }
508: PetscCall(PetscFree(lg));
509: PetscFunctionReturn(PETSC_SUCCESS);
510: }
512: static PetscErrorCode VecView_Plex_Local_Draw_1D(Vec u, PetscViewer viewer)
513: {
514: DM dm;
516: PetscFunctionBegin;
517: PetscCall(VecGetDM(u, &dm));
518: PetscCall(DMPlexVecView1D(dm, 1, &u, viewer));
519: PetscFunctionReturn(PETSC_SUCCESS);
520: }
522: static PetscErrorCode VecView_Plex_Local_Draw_2D(Vec v, PetscViewer viewer)
523: {
524: DM dm;
525: PetscSection s;
526: PetscDraw draw, popup;
527: DM cdm;
528: PetscSection coordSection;
529: Vec coordinates;
530: const PetscScalar *array;
531: PetscReal lbound[3], ubound[3];
532: PetscReal vbound[2], time;
533: PetscBool flg;
534: PetscInt dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
535: const char *name;
536: char title[PETSC_MAX_PATH_LEN];
538: PetscFunctionBegin;
539: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
540: PetscCall(VecGetDM(v, &dm));
541: PetscCall(DMGetCoordinateDim(dm, &dim));
542: PetscCall(DMGetLocalSection(dm, &s));
543: PetscCall(PetscSectionGetNumFields(s, &Nf));
544: PetscCall(DMGetCoarsenLevel(dm, &level));
545: PetscCall(DMGetCoordinateDM(dm, &cdm));
546: PetscCall(DMGetLocalSection(cdm, &coordSection));
547: PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
548: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
549: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
551: PetscCall(PetscObjectGetName((PetscObject)v, &name));
552: PetscCall(DMGetOutputSequenceNumber(dm, &step, &time));
554: PetscCall(VecGetLocalSize(coordinates, &N));
555: PetscCall(DMGetBoundingBox(dm, lbound, ubound));
556: PetscCall(PetscDrawClear(draw));
558: /* Could implement something like DMDASelectFields() */
559: for (f = 0; f < Nf; ++f) {
560: DM fdm = dm;
561: Vec fv = v;
562: IS fis;
563: char prefix[PETSC_MAX_PATH_LEN];
564: const char *fname;
566: PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
567: PetscCall(PetscSectionGetFieldName(s, f, &fname));
569: if (v->hdr.prefix) PetscCall(PetscStrncpy(prefix, v->hdr.prefix, sizeof(prefix)));
570: else prefix[0] = '\0';
571: if (Nf > 1) {
572: PetscCall(DMCreateSubDM(dm, 1, &f, &fis, &fdm));
573: PetscCall(VecGetSubVector(v, fis, &fv));
574: PetscCall(PetscStrlcat(prefix, fname, sizeof(prefix)));
575: PetscCall(PetscStrlcat(prefix, "_", sizeof(prefix)));
576: }
577: for (comp = 0; comp < Nc; ++comp, ++w) {
578: PetscInt nmax = 2;
580: PetscCall(PetscViewerDrawGetDraw(viewer, w, &draw));
581: if (Nc > 1) PetscCall(PetscSNPrintf(title, sizeof(title), "%s:%s_%" PetscInt_FMT " Step: %" PetscInt_FMT " Time: %.4g", name, fname, comp, step, (double)time));
582: else PetscCall(PetscSNPrintf(title, sizeof(title), "%s:%s Step: %" PetscInt_FMT " Time: %.4g", name, fname, step, (double)time));
583: PetscCall(PetscDrawSetTitle(draw, title));
585: /* TODO Get max and min only for this component */
586: PetscCall(PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg));
587: if (!flg) {
588: PetscCall(VecMin(fv, NULL, &vbound[0]));
589: PetscCall(VecMax(fv, NULL, &vbound[1]));
590: if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
591: }
593: PetscCall(PetscDrawGetPopup(draw, &popup));
594: PetscCall(PetscDrawScalePopup(popup, vbound[0], vbound[1]));
595: PetscCall(PetscDrawSetCoordinates(draw, lbound[0], lbound[1], ubound[0], ubound[1]));
596: PetscCall(VecGetArrayRead(fv, &array));
597: for (c = cStart; c < cEnd; ++c) {
598: DMPolytopeType ct;
599: PetscScalar *coords = NULL, *a = NULL;
600: const PetscScalar *coords_arr;
601: PetscBool isDG;
602: PetscInt numCoords;
603: int color[4] = {-1, -1, -1, -1};
605: PetscCall(DMPlexGetCellType(dm, c, &ct));
606: PetscCall(DMPlexPointLocalRead(fdm, c, array, &a));
607: if (a) {
608: color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
609: color[1] = color[2] = color[3] = color[0];
610: } else {
611: PetscScalar *vals = NULL;
612: PetscInt numVals, va;
614: PetscCall(DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals));
615: if (!numVals) {
616: PetscCall(DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals));
617: continue;
618: }
619: PetscCheck(numVals % Nc == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of components %" PetscInt_FMT " does not divide the number of values in the closure %" PetscInt_FMT, Nc, numVals);
620: switch (numVals / Nc) {
621: case 1: /* P1 Clamped Segment Prism */
622: case 2: /* P1 Segment Prism, P2 Clamped Segment Prism */
623: PetscCheck(ct == DM_POLYTOPE_SEG_PRISM_TENSOR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell should be a tensor segment, but it is a %s", DMPolytopeTypes[ct]);
624: for (va = 0; va < numVals / Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp]), vbound[0], vbound[1]);
625: break;
626: case 3: /* P1 Triangle */
627: case 4: /* P1 Quadrangle */
628: PetscCheck(ct == DM_POLYTOPE_TRIANGLE || ct == DM_POLYTOPE_QUADRILATERAL || ct == DM_POLYTOPE_SEG_PRISM_TENSOR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell should be a triangle or quad, but it is a %s", DMPolytopeTypes[ct]);
629: for (va = 0; va < numVals / Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp]), vbound[0], vbound[1]);
630: break;
631: case 6: /* P2 Triangle */
632: case 8: /* P2 Quadrangle */
633: PetscCheck(ct == DM_POLYTOPE_TRIANGLE || ct == DM_POLYTOPE_QUADRILATERAL || ct == DM_POLYTOPE_SEG_PRISM_TENSOR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell should be a triangle or quad, but it is a %s", DMPolytopeTypes[ct]);
634: for (va = 0; va < numVals / (Nc * 2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp + numVals / (Nc * 2)]), vbound[0], vbound[1]);
635: break;
636: default:
637: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %" PetscInt_FMT " cannot be handled", numVals / Nc);
638: }
639: PetscCall(DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals));
640: }
641: PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &coords_arr, &coords));
642: switch (numCoords) {
643: case 6:
644: case 12: /* Localized triangle */
645: PetscCall(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]));
646: break;
647: case 8:
648: case 16: /* Localized quadrilateral */
649: if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR) {
650: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscMax(color[0], color[1])));
651: } else {
652: PetscCall(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]));
653: PetscCall(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]));
654: }
655: break;
656: default:
657: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %" PetscInt_FMT " coordinates", numCoords);
658: }
659: PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &coords_arr, &coords));
660: }
661: PetscCall(VecRestoreArrayRead(fv, &array));
662: PetscCall(PetscDrawFlush(draw));
663: PetscCall(PetscDrawPause(draw));
664: PetscCall(PetscDrawSave(draw));
665: }
666: if (Nf > 1) {
667: PetscCall(VecRestoreSubVector(v, fis, &fv));
668: PetscCall(ISDestroy(&fis));
669: PetscCall(DMDestroy(&fdm));
670: }
671: }
672: PetscFunctionReturn(PETSC_SUCCESS);
673: }
675: static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
676: {
677: DM dm;
678: PetscDraw draw;
679: PetscInt dim;
680: PetscBool isnull;
682: PetscFunctionBegin;
683: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
684: PetscCall(PetscDrawIsNull(draw, &isnull));
685: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
687: PetscCall(VecGetDM(v, &dm));
688: PetscCall(DMGetCoordinateDim(dm, &dim));
689: switch (dim) {
690: case 1:
691: PetscCall(VecView_Plex_Local_Draw_1D(v, viewer));
692: break;
693: case 2:
694: PetscCall(VecView_Plex_Local_Draw_2D(v, viewer));
695: break;
696: default:
697: SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_SUP, "Cannot draw meshes of dimension %" PetscInt_FMT ". Try PETSCVIEWERGLVIS", dim);
698: }
699: PetscFunctionReturn(PETSC_SUCCESS);
700: }
702: static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
703: {
704: DM dm;
705: Vec locv;
706: const char *name;
707: PetscSection section;
708: PetscInt pStart, pEnd;
709: PetscInt numFields;
710: PetscViewerVTKFieldType ft;
712: PetscFunctionBegin;
713: PetscCall(VecGetDM(v, &dm));
714: PetscCall(DMCreateLocalVector(dm, &locv)); /* VTK viewer requires exclusive ownership of the vector */
715: PetscCall(PetscObjectGetName((PetscObject)v, &name));
716: PetscCall(PetscObjectSetName((PetscObject)locv, name));
717: PetscCall(VecCopy(v, locv));
718: PetscCall(DMGetLocalSection(dm, §ion));
719: PetscCall(PetscSectionGetNumFields(section, &numFields));
720: if (!numFields) {
721: PetscCall(DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft));
722: PetscCall(PetscViewerVTKAddField(viewer, (PetscObject)dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE, (PetscObject)locv));
723: } else {
724: PetscInt f;
726: for (f = 0; f < numFields; f++) {
727: PetscCall(DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft));
728: if (ft == PETSC_VTK_INVALID) continue;
729: PetscCall(PetscObjectReference((PetscObject)locv));
730: PetscCall(PetscViewerVTKAddField(viewer, (PetscObject)dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE, (PetscObject)locv));
731: }
732: PetscCall(VecDestroy(&locv));
733: }
734: PetscFunctionReturn(PETSC_SUCCESS);
735: }
737: PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
738: {
739: DM dm;
740: PetscBool isvtk, ishdf5, isdraw, isglvis, iscgns, ispython;
742: PetscFunctionBegin;
743: PetscCall(VecGetDM(v, &dm));
744: PetscCheck(dm, PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
745: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk));
746: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
747: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
748: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis));
749: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns));
750: PetscCall(PetscObjectHasFunction((PetscObject)viewer, "PetscViewerPythonViewObject_C", &ispython));
751: if (isvtk || ishdf5 || isdraw || isglvis || iscgns || ispython) {
752: PetscInt i, numFields;
753: PetscObject fe;
754: PetscBool fem = PETSC_FALSE;
755: Vec locv = v;
756: const char *name;
757: PetscInt step;
758: PetscReal time;
760: PetscCall(DMGetNumFields(dm, &numFields));
761: for (i = 0; i < numFields; i++) {
762: PetscCall(DMGetField(dm, i, NULL, &fe));
763: if (fe->classid == PETSCFE_CLASSID) {
764: fem = PETSC_TRUE;
765: break;
766: }
767: }
768: if (fem) {
769: PetscObject isZero;
771: PetscCall(DMGetLocalVector(dm, &locv));
772: PetscCall(PetscObjectGetName((PetscObject)v, &name));
773: PetscCall(PetscObjectSetName((PetscObject)locv, name));
774: PetscCall(PetscObjectQuery((PetscObject)v, "__Vec_bc_zero__", &isZero));
775: PetscCall(PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", isZero));
776: PetscCall(VecCopy(v, locv));
777: PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
778: PetscCall(DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL));
779: }
780: if (isvtk) {
781: PetscCall(VecView_Plex_Local_VTK(locv, viewer));
782: } else if (ishdf5) {
783: #if defined(PETSC_HAVE_HDF5)
784: PetscCall(VecView_Plex_Local_HDF5_Internal(locv, viewer));
785: #else
786: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
787: #endif
788: } else if (isdraw) {
789: PetscCall(VecView_Plex_Local_Draw(locv, viewer));
790: } else if (ispython) {
791: PetscCall(PetscViewerPythonViewObject(viewer, (PetscObject)locv));
792: } else if (isglvis) {
793: PetscCall(DMGetOutputSequenceNumber(dm, &step, NULL));
794: PetscCall(PetscViewerGLVisSetSnapId(viewer, step));
795: PetscCall(VecView_GLVis(locv, viewer));
796: } else if (iscgns) {
797: #if defined(PETSC_HAVE_CGNS)
798: PetscCall(VecView_Plex_Local_CGNS(locv, viewer));
799: #else
800: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "CGNS not supported in this build.\nPlease reconfigure using --download-cgns");
801: #endif
802: }
803: if (fem) {
804: PetscCall(PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", NULL));
805: PetscCall(DMRestoreLocalVector(dm, &locv));
806: }
807: } else {
808: PetscBool isseq;
810: PetscCall(PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq));
811: if (isseq) PetscCall(VecView_Seq(v, viewer));
812: else PetscCall(VecView_MPI(v, viewer));
813: }
814: PetscFunctionReturn(PETSC_SUCCESS);
815: }
817: PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
818: {
819: DM dm;
820: PetscBool isvtk, ishdf5, isdraw, isglvis, isexodusii, iscgns, ispython;
822: PetscFunctionBegin;
823: PetscCall(VecGetDM(v, &dm));
824: PetscCheck(dm, PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
825: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk));
826: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
827: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
828: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis));
829: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns));
830: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodusii));
831: PetscCall(PetscObjectHasFunction((PetscObject)viewer, "PetscViewerPythonViewObject_C", &ispython));
832: if (isvtk || isdraw || isglvis || iscgns || ispython) {
833: Vec locv;
834: PetscObject isZero;
835: const char *name;
837: PetscCall(DMGetLocalVector(dm, &locv));
838: PetscCall(PetscObjectGetName((PetscObject)v, &name));
839: PetscCall(PetscObjectSetName((PetscObject)locv, name));
840: PetscCall(DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv));
841: PetscCall(DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv));
842: PetscCall(PetscObjectQuery((PetscObject)v, "__Vec_bc_zero__", &isZero));
843: PetscCall(PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", isZero));
844: PetscCall(VecView_Plex_Local(locv, viewer));
845: PetscCall(PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", NULL));
846: PetscCall(DMRestoreLocalVector(dm, &locv));
847: } else if (ishdf5) {
848: #if defined(PETSC_HAVE_HDF5)
849: PetscCall(VecView_Plex_HDF5_Internal(v, viewer));
850: #else
851: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
852: #endif
853: } else if (isexodusii) {
854: #if defined(PETSC_HAVE_EXODUSII)
855: PetscCall(VecView_PlexExodusII_Internal(v, viewer));
856: #else
857: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
858: #endif
859: } else {
860: PetscBool isseq;
862: PetscCall(PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq));
863: if (isseq) PetscCall(VecView_Seq(v, viewer));
864: else PetscCall(VecView_MPI(v, viewer));
865: }
866: PetscFunctionReturn(PETSC_SUCCESS);
867: }
869: PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
870: {
871: DM dm;
872: MPI_Comm comm;
873: PetscViewerFormat format;
874: Vec v;
875: PetscBool isvtk, ishdf5;
877: PetscFunctionBegin;
878: PetscCall(VecGetDM(originalv, &dm));
879: PetscCall(PetscObjectGetComm((PetscObject)originalv, &comm));
880: PetscCheck(dm, comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
881: PetscCall(PetscViewerGetFormat(viewer, &format));
882: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
883: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk));
884: if (format == PETSC_VIEWER_NATIVE) {
885: /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
886: /* this need a better fix */
887: if (dm->useNatural) {
888: const char *vecname;
889: PetscInt n, nroots;
891: PetscCheck(dm->sfNatural, comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
892: PetscCall(VecGetLocalSize(originalv, &n));
893: PetscCall(PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL));
894: PetscCheck(n == nroots, comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
895: PetscCall(DMPlexCreateNaturalVector(dm, &v));
896: PetscCall(DMPlexGlobalToNaturalBegin(dm, originalv, v));
897: PetscCall(DMPlexGlobalToNaturalEnd(dm, originalv, v));
898: PetscCall(PetscObjectGetName((PetscObject)originalv, &vecname));
899: PetscCall(PetscObjectSetName((PetscObject)v, vecname));
900: } else v = originalv;
901: } else v = originalv;
903: if (ishdf5) {
904: #if defined(PETSC_HAVE_HDF5)
905: PetscCall(VecView_Plex_HDF5_Native_Internal(v, viewer));
906: #else
907: SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
908: #endif
909: } else if (isvtk) {
910: SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
911: } else {
912: PetscBool isseq;
914: PetscCall(PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq));
915: if (isseq) PetscCall(VecView_Seq(v, viewer));
916: else PetscCall(VecView_MPI(v, viewer));
917: }
918: if (v != originalv) PetscCall(VecDestroy(&v));
919: PetscFunctionReturn(PETSC_SUCCESS);
920: }
922: PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
923: {
924: DM dm;
925: PetscBool ishdf5;
927: PetscFunctionBegin;
928: PetscCall(VecGetDM(v, &dm));
929: PetscCheck(dm, PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
930: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
931: if (ishdf5) {
932: DM dmBC;
933: Vec gv;
934: const char *name;
936: PetscCall(DMGetOutputDM(dm, &dmBC));
937: PetscCall(DMGetGlobalVector(dmBC, &gv));
938: PetscCall(PetscObjectGetName((PetscObject)v, &name));
939: PetscCall(PetscObjectSetName((PetscObject)gv, name));
940: PetscCall(VecLoad_Default(gv, viewer));
941: PetscCall(DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v));
942: PetscCall(DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v));
943: PetscCall(DMRestoreGlobalVector(dmBC, &gv));
944: } else PetscCall(VecLoad_Default(v, viewer));
945: PetscFunctionReturn(PETSC_SUCCESS);
946: }
948: PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
949: {
950: DM dm;
951: PetscBool ishdf5, isexodusii, iscgns;
953: PetscFunctionBegin;
954: PetscCall(VecGetDM(v, &dm));
955: PetscCheck(dm, PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
956: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
957: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodusii));
958: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns));
959: if (ishdf5) {
960: #if defined(PETSC_HAVE_HDF5)
961: PetscCall(VecLoad_Plex_HDF5_Internal(v, viewer));
962: #else
963: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
964: #endif
965: } else if (isexodusii) {
966: #if defined(PETSC_HAVE_EXODUSII)
967: PetscCall(VecLoad_PlexExodusII_Internal(v, viewer));
968: #else
969: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
970: #endif
971: } else if (iscgns) {
972: #if defined(PETSC_HAVE_CGNS)
973: PetscCall(VecLoad_Plex_CGNS_Internal(v, viewer));
974: #else
975: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "CGNS not supported in this build.\nPlease reconfigure using --download-cgns");
976: #endif
977: } else PetscCall(VecLoad_Default(v, viewer));
978: PetscFunctionReturn(PETSC_SUCCESS);
979: }
981: PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
982: {
983: DM dm;
984: PetscViewerFormat format;
985: PetscBool ishdf5;
987: PetscFunctionBegin;
988: PetscCall(VecGetDM(originalv, &dm));
989: PetscCheck(dm, PetscObjectComm((PetscObject)originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
990: PetscCall(PetscViewerGetFormat(viewer, &format));
991: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
992: if (format == PETSC_VIEWER_NATIVE) {
993: if (dm->useNatural) {
994: if (dm->sfNatural) {
995: if (ishdf5) {
996: #if defined(PETSC_HAVE_HDF5)
997: Vec v;
998: const char *vecname;
1000: PetscCall(DMPlexCreateNaturalVector(dm, &v));
1001: PetscCall(PetscObjectGetName((PetscObject)originalv, &vecname));
1002: PetscCall(PetscObjectSetName((PetscObject)v, vecname));
1003: PetscCall(VecLoad_Plex_HDF5_Native_Internal(v, viewer));
1004: PetscCall(DMPlexNaturalToGlobalBegin(dm, v, originalv));
1005: PetscCall(DMPlexNaturalToGlobalEnd(dm, v, originalv));
1006: PetscCall(VecDestroy(&v));
1007: #else
1008: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1009: #endif
1010: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
1011: }
1012: } else PetscCall(VecLoad_Default(originalv, viewer));
1013: }
1014: PetscFunctionReturn(PETSC_SUCCESS);
1015: }
1017: PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
1018: {
1019: PetscSection coordSection;
1020: Vec coordinates;
1021: DMLabel depthLabel, celltypeLabel;
1022: const char *name[4];
1023: const PetscScalar *a;
1024: PetscInt dim, pStart, pEnd, cStart, cEnd, c;
1026: PetscFunctionBegin;
1027: PetscCall(DMGetDimension(dm, &dim));
1028: PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
1029: PetscCall(DMGetCoordinateSection(dm, &coordSection));
1030: PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
1031: PetscCall(DMPlexGetCellTypeLabel(dm, &celltypeLabel));
1032: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
1033: PetscCall(PetscSectionGetChart(coordSection, &pStart, &pEnd));
1034: PetscCall(VecGetArrayRead(coordinates, &a));
1035: name[0] = "vertex";
1036: name[1] = "edge";
1037: name[dim - 1] = "face";
1038: name[dim] = "cell";
1039: for (c = cStart; c < cEnd; ++c) {
1040: PetscInt *closure = NULL;
1041: PetscInt closureSize, cl, ct;
1043: PetscCall(DMLabelGetValue(celltypeLabel, c, &ct));
1044: PetscCall(PetscViewerASCIIPrintf(viewer, "Geometry for cell %" PetscInt_FMT " polytope type %s:\n", c, DMPolytopeTypes[ct]));
1045: PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1046: PetscCall(PetscViewerASCIIPushTab(viewer));
1047: for (cl = 0; cl < closureSize * 2; cl += 2) {
1048: PetscInt point = closure[cl], depth, dof, off, d, p;
1050: if ((point < pStart) || (point >= pEnd)) continue;
1051: PetscCall(PetscSectionGetDof(coordSection, point, &dof));
1052: if (!dof) continue;
1053: PetscCall(DMLabelGetValue(depthLabel, point, &depth));
1054: PetscCall(PetscSectionGetOffset(coordSection, point, &off));
1055: PetscCall(PetscViewerASCIIPrintf(viewer, "%s %" PetscInt_FMT " coords:", name[depth], point));
1056: for (p = 0; p < dof / dim; ++p) {
1057: PetscCall(PetscViewerASCIIPrintf(viewer, " ("));
1058: for (d = 0; d < dim; ++d) {
1059: if (d > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ", "));
1060: PetscCall(PetscViewerASCIIPrintf(viewer, "%g", (double)PetscRealPart(a[off + p * dim + d])));
1061: }
1062: PetscCall(PetscViewerASCIIPrintf(viewer, ")"));
1063: }
1064: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1065: }
1066: PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1067: PetscCall(PetscViewerASCIIPopTab(viewer));
1068: }
1069: PetscCall(VecRestoreArrayRead(coordinates, &a));
1070: PetscFunctionReturn(PETSC_SUCCESS);
1071: }
1073: typedef enum {
1074: CS_CARTESIAN,
1075: CS_POLAR,
1076: CS_CYLINDRICAL,
1077: CS_SPHERICAL
1078: } CoordSystem;
1079: const char *CoordSystems[] = {"cartesian", "polar", "cylindrical", "spherical", "CoordSystem", "CS_", NULL};
1081: static PetscErrorCode DMPlexView_Ascii_Coordinates(PetscViewer viewer, CoordSystem cs, PetscInt dim, const PetscScalar x[])
1082: {
1083: PetscInt i;
1085: PetscFunctionBegin;
1086: if (dim > 3) {
1087: for (i = 0; i < dim; ++i) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)PetscRealPart(x[i])));
1088: } else {
1089: PetscReal coords[3], trcoords[3] = {0., 0., 0.};
1091: for (i = 0; i < dim; ++i) coords[i] = PetscRealPart(x[i]);
1092: switch (cs) {
1093: case CS_CARTESIAN:
1094: for (i = 0; i < dim; ++i) trcoords[i] = coords[i];
1095: break;
1096: case CS_POLAR:
1097: PetscCheck(dim == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Polar coordinates are for 2 dimension, not %" PetscInt_FMT, dim);
1098: trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
1099: trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
1100: break;
1101: case CS_CYLINDRICAL:
1102: PetscCheck(dim == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cylindrical coordinates are for 3 dimension, not %" PetscInt_FMT, dim);
1103: trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
1104: trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
1105: trcoords[2] = coords[2];
1106: break;
1107: case CS_SPHERICAL:
1108: PetscCheck(dim == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Spherical coordinates are for 3 dimension, not %" PetscInt_FMT, dim);
1109: trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]) + PetscSqr(coords[2]));
1110: trcoords[1] = PetscAtan2Real(PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1])), coords[2]);
1111: trcoords[2] = PetscAtan2Real(coords[1], coords[0]);
1112: break;
1113: }
1114: for (i = 0; i < dim; ++i) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)trcoords[i]));
1115: }
1116: PetscFunctionReturn(PETSC_SUCCESS);
1117: }
1119: static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
1120: {
1121: DM_Plex *mesh = (DM_Plex *)dm->data;
1122: DM cdm, cdmCell;
1123: PetscSection coordSection, coordSectionCell;
1124: Vec coordinates, coordinatesCell;
1125: PetscViewerFormat format;
1127: PetscFunctionBegin;
1128: PetscCall(PetscViewerGetFormat(viewer, &format));
1129: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1130: const char *name;
1131: PetscInt dim, cellHeight, maxConeSize, maxSupportSize;
1132: PetscInt pStart, pEnd, p, numLabels, l;
1133: PetscMPIInt rank, size;
1135: PetscCall(DMGetCoordinateDM(dm, &cdm));
1136: PetscCall(DMGetCoordinateSection(dm, &coordSection));
1137: PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
1138: PetscCall(DMGetCellCoordinateDM(dm, &cdmCell));
1139: PetscCall(DMGetCellCoordinateSection(dm, &coordSectionCell));
1140: PetscCall(DMGetCellCoordinatesLocal(dm, &coordinatesCell));
1141: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1142: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
1143: PetscCall(PetscObjectGetName((PetscObject)dm, &name));
1144: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1145: PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
1146: PetscCall(DMGetDimension(dm, &dim));
1147: PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
1148: if (name) PetscCall(PetscViewerASCIIPrintf(viewer, "%s in %" PetscInt_FMT " dimension%s:\n", name, dim, dim == 1 ? "" : "s"));
1149: else PetscCall(PetscViewerASCIIPrintf(viewer, "Mesh in %" PetscInt_FMT " dimension%s:\n", dim, dim == 1 ? "" : "s"));
1150: if (cellHeight) PetscCall(PetscViewerASCIIPrintf(viewer, " Cells are at height %" PetscInt_FMT "\n", cellHeight));
1151: PetscCall(PetscViewerASCIIPrintf(viewer, "Supports:\n"));
1152: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1153: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %" PetscInt_FMT "\n", rank, maxSupportSize));
1154: for (p = pStart; p < pEnd; ++p) {
1155: PetscInt dof, off, s;
1157: PetscCall(PetscSectionGetDof(mesh->supportSection, p, &dof));
1158: PetscCall(PetscSectionGetOffset(mesh->supportSection, p, &off));
1159: for (s = off; s < off + dof; ++s) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %" PetscInt_FMT " ----> %" PetscInt_FMT "\n", rank, p, mesh->supports[s]));
1160: }
1161: PetscCall(PetscViewerFlush(viewer));
1162: PetscCall(PetscViewerASCIIPrintf(viewer, "Cones:\n"));
1163: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %" PetscInt_FMT "\n", rank, maxConeSize));
1164: for (p = pStart; p < pEnd; ++p) {
1165: PetscInt dof, off, c;
1167: PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
1168: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
1169: for (c = off; c < off + dof; ++c) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %" PetscInt_FMT " <---- %" PetscInt_FMT " (%" PetscInt_FMT ")\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]));
1170: }
1171: PetscCall(PetscViewerFlush(viewer));
1172: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1173: if (coordSection && coordinates) {
1174: CoordSystem cs = CS_CARTESIAN;
1175: const PetscScalar *array, *arrayCell = NULL;
1176: PetscInt Nf, Nc, pvStart, pvEnd, pcStart = PETSC_INT_MAX, pcEnd = PETSC_INT_MIN, pStart, pEnd, p;
1177: PetscMPIInt rank;
1178: const char *name;
1180: PetscCall(PetscOptionsGetEnum(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_coord_system", CoordSystems, (PetscEnum *)&cs, NULL));
1181: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
1182: PetscCall(PetscSectionGetNumFields(coordSection, &Nf));
1183: PetscCheck(Nf == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Coordinate section should have 1 field, not %" PetscInt_FMT, Nf);
1184: PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &Nc));
1185: PetscCall(PetscSectionGetChart(coordSection, &pvStart, &pvEnd));
1186: if (coordSectionCell) PetscCall(PetscSectionGetChart(coordSectionCell, &pcStart, &pcEnd));
1187: pStart = PetscMin(pvStart, pcStart);
1188: pEnd = PetscMax(pvEnd, pcEnd);
1189: PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
1190: PetscCall(PetscViewerASCIIPrintf(viewer, "%s with %" PetscInt_FMT " fields\n", name, Nf));
1191: PetscCall(PetscViewerASCIIPrintf(viewer, " field 0 with %" PetscInt_FMT " components\n", Nc));
1192: if (cs != CS_CARTESIAN) PetscCall(PetscViewerASCIIPrintf(viewer, " output coordinate system: %s\n", CoordSystems[cs]));
1194: PetscCall(VecGetArrayRead(coordinates, &array));
1195: if (coordinatesCell) PetscCall(VecGetArrayRead(coordinatesCell, &arrayCell));
1196: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1197: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank));
1198: for (p = pStart; p < pEnd; ++p) {
1199: PetscInt dof, off;
1201: if (p >= pvStart && p < pvEnd) {
1202: PetscCall(PetscSectionGetDof(coordSection, p, &dof));
1203: PetscCall(PetscSectionGetOffset(coordSection, p, &off));
1204: if (dof) {
1205: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " (%4" PetscInt_FMT ") dof %2" PetscInt_FMT " offset %3" PetscInt_FMT, p, dof, off));
1206: PetscCall(DMPlexView_Ascii_Coordinates(viewer, cs, dof, &array[off]));
1207: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
1208: }
1209: }
1210: if (cdmCell && p >= pcStart && p < pcEnd) {
1211: PetscCall(PetscSectionGetDof(coordSectionCell, p, &dof));
1212: PetscCall(PetscSectionGetOffset(coordSectionCell, p, &off));
1213: if (dof) {
1214: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " (%4" PetscInt_FMT ") dof %2" PetscInt_FMT " offset %3" PetscInt_FMT, p, dof, off));
1215: PetscCall(DMPlexView_Ascii_Coordinates(viewer, cs, dof, &arrayCell[off]));
1216: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
1217: }
1218: }
1219: }
1220: PetscCall(PetscViewerFlush(viewer));
1221: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1222: PetscCall(VecRestoreArrayRead(coordinates, &array));
1223: if (coordinatesCell) PetscCall(VecRestoreArrayRead(coordinatesCell, &arrayCell));
1224: }
1225: PetscCall(DMGetNumLabels(dm, &numLabels));
1226: if (numLabels) PetscCall(PetscViewerASCIIPrintf(viewer, "Labels:\n"));
1227: for (l = 0; l < numLabels; ++l) {
1228: DMLabel label;
1229: PetscBool isdepth;
1230: const char *name;
1232: PetscCall(DMGetLabelName(dm, l, &name));
1233: PetscCall(PetscStrcmp(name, "depth", &isdepth));
1234: if (isdepth) continue;
1235: PetscCall(DMGetLabel(dm, name, &label));
1236: PetscCall(DMLabelView(label, viewer));
1237: }
1238: if (size > 1) {
1239: PetscSF sf;
1241: PetscCall(DMGetPointSF(dm, &sf));
1242: PetscCall(PetscSFView(sf, viewer));
1243: }
1244: if (mesh->periodic.face_sfs)
1245: for (PetscInt i = 0; i < mesh->periodic.num_face_sfs; i++) PetscCall(PetscSFView(mesh->periodic.face_sfs[i], viewer));
1246: PetscCall(PetscViewerFlush(viewer));
1247: } else if (format == PETSC_VIEWER_ASCII_LATEX) {
1248: const char *name, *color;
1249: const char *defcolors[3] = {"gray", "orange", "green"};
1250: const char *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
1251: char lname[PETSC_MAX_PATH_LEN];
1252: PetscReal scale = 2.0;
1253: PetscReal tikzscale = 1.0;
1254: PetscBool useNumbers = PETSC_TRUE, drawNumbers[4], drawColors[4], useLabels, useColors, plotEdges, drawHasse = PETSC_FALSE;
1255: double tcoords[3];
1256: PetscScalar *coords;
1257: PetscInt numLabels, l, numColors, numLColors, dim, d, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, fStart = 0, fEnd = 0, e, p, n;
1258: PetscMPIInt rank, size;
1259: char **names, **colors, **lcolors;
1260: PetscBool flg, lflg;
1261: PetscBT wp = NULL;
1262: PetscInt pEnd, pStart;
1264: PetscCall(DMGetCoordinateDM(dm, &cdm));
1265: PetscCall(DMGetCoordinateSection(dm, &coordSection));
1266: PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
1267: PetscCall(DMGetCellCoordinateDM(dm, &cdmCell));
1268: PetscCall(DMGetCellCoordinateSection(dm, &coordSectionCell));
1269: PetscCall(DMGetCellCoordinatesLocal(dm, &coordinatesCell));
1270: PetscCall(DMGetDimension(dm, &dim));
1271: PetscCall(DMPlexGetDepth(dm, &depth));
1272: PetscCall(DMGetNumLabels(dm, &numLabels));
1273: numLabels = PetscMax(numLabels, 10);
1274: numColors = 10;
1275: numLColors = 10;
1276: PetscCall(PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors));
1277: PetscCall(PetscOptionsGetReal(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_scale", &scale, NULL));
1278: PetscCall(PetscOptionsGetReal(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL));
1279: PetscCall(PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL));
1280: for (d = 0; d < 4; ++d) drawNumbers[d] = useNumbers;
1281: for (d = 0; d < 4; ++d) drawColors[d] = PETSC_TRUE;
1282: n = 4;
1283: PetscCall(PetscOptionsGetBoolArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_numbers_depth", drawNumbers, &n, &flg));
1284: PetscCheck(!flg || n == dim + 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Number of flags %" PetscInt_FMT " != %" PetscInt_FMT " dim+1", n, dim + 1);
1285: n = 4;
1286: PetscCall(PetscOptionsGetBoolArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_colors_depth", drawColors, &n, &flg));
1287: PetscCheck(!flg || n == dim + 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Number of flags %" PetscInt_FMT " != %" PetscInt_FMT " dim+1", n, dim + 1);
1288: PetscCall(PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels));
1289: if (!useLabels) numLabels = 0;
1290: PetscCall(PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors));
1291: if (!useColors) {
1292: numColors = 3;
1293: for (c = 0; c < numColors; ++c) PetscCall(PetscStrallocpy(defcolors[c], &colors[c]));
1294: }
1295: PetscCall(PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors));
1296: if (!useColors) {
1297: numLColors = 4;
1298: for (c = 0; c < numLColors; ++c) PetscCall(PetscStrallocpy(deflcolors[c], &lcolors[c]));
1299: }
1300: PetscCall(PetscOptionsGetString(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg));
1301: plotEdges = (PetscBool)(depth > 1 && drawNumbers[1] && dim < 3);
1302: PetscCall(PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg));
1303: PetscCheck(!flg || !plotEdges || depth >= dim, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Mesh must be interpolated");
1304: if (depth < dim) plotEdges = PETSC_FALSE;
1305: PetscCall(PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_hasse", &drawHasse, NULL));
1307: /* filter points with labelvalue != labeldefaultvalue */
1308: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1309: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1310: PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
1311: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
1312: PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
1313: if (lflg) {
1314: DMLabel lbl;
1316: PetscCall(DMGetLabel(dm, lname, &lbl));
1317: if (lbl) {
1318: PetscInt val, defval;
1320: PetscCall(DMLabelGetDefaultValue(lbl, &defval));
1321: PetscCall(PetscBTCreate(pEnd - pStart, &wp));
1322: for (c = pStart; c < pEnd; c++) {
1323: PetscInt *closure = NULL;
1324: PetscInt closureSize;
1326: PetscCall(DMLabelGetValue(lbl, c, &val));
1327: if (val == defval) continue;
1329: PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1330: for (p = 0; p < closureSize * 2; p += 2) PetscCall(PetscBTSet(wp, closure[p] - pStart));
1331: PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1332: }
1333: }
1334: }
1336: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1337: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
1338: PetscCall(PetscObjectGetName((PetscObject)dm, &name));
1339: PetscCall(PetscViewerASCIIPrintf(viewer, "\
1340: \\documentclass[tikz]{standalone}\n\n\
1341: \\usepackage{pgflibraryshapes}\n\
1342: \\usetikzlibrary{backgrounds}\n\
1343: \\usetikzlibrary{arrows}\n\
1344: \\begin{document}\n"));
1345: if (size > 1) {
1346: PetscCall(PetscViewerASCIIPrintf(viewer, "%s for process ", name));
1347: for (p = 0; p < size; ++p) {
1348: if (p) PetscCall(PetscViewerASCIIPrintf(viewer, (p == size - 1) ? ", and " : ", "));
1349: PetscCall(PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%" PetscInt_FMT "}", colors[p % numColors], p));
1350: }
1351: PetscCall(PetscViewerASCIIPrintf(viewer, ".\n\n\n"));
1352: }
1353: if (drawHasse) {
1354: PetscInt maxStratum = PetscMax(vEnd - vStart, PetscMax(eEnd - eStart, PetscMax(fEnd - fStart, cEnd - cStart)));
1356: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vStart}{%" PetscInt_FMT "}\n", vStart));
1357: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vEnd}{%" PetscInt_FMT "}\n", vEnd - 1));
1358: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numVertices}{%" PetscInt_FMT "}\n", vEnd - vStart));
1359: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vShift}{%.2f}\n", 3 + (maxStratum - (vEnd - vStart)) / 2.));
1360: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eStart}{%" PetscInt_FMT "}\n", eStart));
1361: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eEnd}{%" PetscInt_FMT "}\n", eEnd - 1));
1362: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eShift}{%.2f}\n", 3 + (maxStratum - (eEnd - eStart)) / 2.));
1363: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numEdges}{%" PetscInt_FMT "}\n", eEnd - eStart));
1364: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\fStart}{%" PetscInt_FMT "}\n", fStart));
1365: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\fEnd}{%" PetscInt_FMT "}\n", fEnd - 1));
1366: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\fShift}{%.2f}\n", 3 + (maxStratum - (fEnd - fStart)) / 2.));
1367: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numFaces}{%" PetscInt_FMT "}\n", fEnd - fStart));
1368: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cStart}{%" PetscInt_FMT "}\n", cStart));
1369: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cEnd}{%" PetscInt_FMT "}\n", cEnd - 1));
1370: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numCells}{%" PetscInt_FMT "}\n", cEnd - cStart));
1371: PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cShift}{%.2f}\n", 3 + (maxStratum - (cEnd - cStart)) / 2.));
1372: }
1373: PetscCall(PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double)tikzscale));
1375: /* Plot vertices */
1376: PetscCall(VecGetArray(coordinates, &coords));
1377: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1378: for (v = vStart; v < vEnd; ++v) {
1379: PetscInt off, dof, d;
1380: PetscBool isLabeled = PETSC_FALSE;
1382: if (wp && !PetscBTLookup(wp, v - pStart)) continue;
1383: PetscCall(PetscSectionGetDof(coordSection, v, &dof));
1384: PetscCall(PetscSectionGetOffset(coordSection, v, &off));
1385: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\\path ("));
1386: PetscCheck(dof <= 3, PETSC_COMM_SELF, PETSC_ERR_PLIB, "coordSection vertex %" PetscInt_FMT " has dof %" PetscInt_FMT " > 3", v, dof);
1387: for (d = 0; d < dof; ++d) {
1388: tcoords[d] = (double)(scale * PetscRealPart(coords[off + d]));
1389: tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1390: }
1391: /* Rotate coordinates since PGF makes z point out of the page instead of up */
1392: if (dim == 3) {
1393: PetscReal tmp = tcoords[1];
1394: tcoords[1] = tcoords[2];
1395: tcoords[2] = -tmp;
1396: }
1397: for (d = 0; d < dof; ++d) {
1398: if (d > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ","));
1399: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]));
1400: }
1401: if (drawHasse) color = colors[0 % numColors];
1402: else color = colors[rank % numColors];
1403: for (l = 0; l < numLabels; ++l) {
1404: PetscInt val;
1405: PetscCall(DMGetLabelValue(dm, names[l], v, &val));
1406: if (val >= 0) {
1407: color = lcolors[l % numLColors];
1408: isLabeled = PETSC_TRUE;
1409: break;
1410: }
1411: }
1412: if (drawNumbers[0]) {
1413: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "};\n", v, rank, color, v));
1414: } else if (drawColors[0]) {
1415: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color));
1416: } else PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [] {};\n", v, rank));
1417: }
1418: PetscCall(VecRestoreArray(coordinates, &coords));
1419: PetscCall(PetscViewerFlush(viewer));
1420: /* Plot edges */
1421: if (plotEdges) {
1422: PetscCall(VecGetArray(coordinates, &coords));
1423: PetscCall(PetscViewerASCIIPrintf(viewer, "\\path\n"));
1424: for (e = eStart; e < eEnd; ++e) {
1425: const PetscInt *cone;
1426: PetscInt coneSize, offA, offB, dof, d;
1428: if (wp && !PetscBTLookup(wp, e - pStart)) continue;
1429: PetscCall(DMPlexGetConeSize(dm, e, &coneSize));
1430: PetscCheck(coneSize == 2, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " cone should have two vertices, not %" PetscInt_FMT, e, coneSize);
1431: PetscCall(DMPlexGetCone(dm, e, &cone));
1432: PetscCall(PetscSectionGetDof(coordSection, cone[0], &dof));
1433: PetscCall(PetscSectionGetOffset(coordSection, cone[0], &offA));
1434: PetscCall(PetscSectionGetOffset(coordSection, cone[1], &offB));
1435: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "("));
1436: for (d = 0; d < dof; ++d) {
1437: tcoords[d] = (double)(scale * PetscRealPart(coords[offA + d] + coords[offB + d]) / 2);
1438: tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1439: }
1440: /* Rotate coordinates since PGF makes z point out of the page instead of up */
1441: if (dim == 3) {
1442: PetscReal tmp = tcoords[1];
1443: tcoords[1] = tcoords[2];
1444: tcoords[2] = -tmp;
1445: }
1446: for (d = 0; d < dof; ++d) {
1447: if (d > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ","));
1448: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]));
1449: }
1450: if (drawHasse) color = colors[1 % numColors];
1451: else color = colors[rank % numColors];
1452: for (l = 0; l < numLabels; ++l) {
1453: PetscInt val;
1454: PetscCall(DMGetLabelValue(dm, names[l], e, &val));
1455: if (val >= 0) {
1456: color = lcolors[l % numLColors];
1457: break;
1458: }
1459: }
1460: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "} --\n", e, rank, color, e));
1461: }
1462: PetscCall(VecRestoreArray(coordinates, &coords));
1463: PetscCall(PetscViewerFlush(viewer));
1464: PetscCall(PetscViewerASCIIPrintf(viewer, "(0,0);\n"));
1465: }
1466: /* Plot cells */
1467: if (dim == 3 || !drawNumbers[1]) {
1468: for (e = eStart; e < eEnd; ++e) {
1469: const PetscInt *cone;
1471: if (wp && !PetscBTLookup(wp, e - pStart)) continue;
1472: color = colors[rank % numColors];
1473: for (l = 0; l < numLabels; ++l) {
1474: PetscInt val;
1475: PetscCall(DMGetLabelValue(dm, names[l], e, &val));
1476: if (val >= 0) {
1477: color = lcolors[l % numLColors];
1478: break;
1479: }
1480: }
1481: PetscCall(DMPlexGetCone(dm, e, &cone));
1482: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", color, cone[0], rank, cone[1], rank));
1483: }
1484: } else {
1485: DMPolytopeType ct;
1487: /* Drawing a 2D polygon */
1488: for (c = cStart; c < cEnd; ++c) {
1489: if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1490: PetscCall(DMPlexGetCellType(dm, c, &ct));
1491: if (DMPolytopeTypeIsHybrid(ct)) {
1492: const PetscInt *cone;
1493: PetscInt coneSize, e;
1495: PetscCall(DMPlexGetCone(dm, c, &cone));
1496: PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
1497: for (e = 0; e < coneSize; ++e) {
1498: const PetscInt *econe;
1500: PetscCall(DMPlexGetCone(dm, cone[e], &econe));
1501: PetscCall(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));
1502: }
1503: } else {
1504: PetscInt *closure = NULL;
1505: PetscInt closureSize, Nv = 0, v;
1507: PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1508: for (p = 0; p < closureSize * 2; p += 2) {
1509: const PetscInt point = closure[p];
1511: if ((point >= vStart) && (point < vEnd)) closure[Nv++] = point;
1512: }
1513: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank % numColors]));
1514: for (v = 0; v <= Nv; ++v) {
1515: const PetscInt vertex = closure[v % Nv];
1517: if (v > 0) {
1518: if (plotEdges) {
1519: const PetscInt *edge;
1520: PetscInt endpoints[2], ne;
1522: endpoints[0] = closure[v - 1];
1523: endpoints[1] = vertex;
1524: PetscCall(DMPlexGetJoin(dm, 2, endpoints, &ne, &edge));
1525: PetscCheck(ne == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find edge for vertices %" PetscInt_FMT ", %" PetscInt_FMT, endpoints[0], endpoints[1]);
1526: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " -- (%" PetscInt_FMT "_%d) -- ", edge[0], rank));
1527: PetscCall(DMPlexRestoreJoin(dm, 2, endpoints, &ne, &edge));
1528: } else PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " -- "));
1529: }
1530: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "(%" PetscInt_FMT "_%d)", vertex, rank));
1531: }
1532: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ";\n"));
1533: PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1534: }
1535: }
1536: }
1537: for (c = cStart; c < cEnd; ++c) {
1538: double ccoords[3] = {0.0, 0.0, 0.0};
1539: PetscBool isLabeled = PETSC_FALSE;
1540: PetscScalar *cellCoords = NULL;
1541: const PetscScalar *array;
1542: PetscInt numCoords, cdim, d;
1543: PetscBool isDG;
1545: if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1546: PetscCall(DMGetCoordinateDim(dm, &cdim));
1547: PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &cellCoords));
1548: PetscCheck(!(numCoords % cdim), PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "coordinate dim %" PetscInt_FMT " does not divide numCoords %" PetscInt_FMT, cdim, numCoords);
1549: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\\path ("));
1550: for (p = 0; p < numCoords / cdim; ++p) {
1551: for (d = 0; d < cdim; ++d) {
1552: tcoords[d] = (double)(scale * PetscRealPart(cellCoords[p * cdim + d]));
1553: tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1554: }
1555: /* Rotate coordinates since PGF makes z point out of the page instead of up */
1556: if (cdim == 3) {
1557: PetscReal tmp = tcoords[1];
1558: tcoords[1] = tcoords[2];
1559: tcoords[2] = -tmp;
1560: }
1561: for (d = 0; d < dim; ++d) ccoords[d] += tcoords[d];
1562: }
1563: for (d = 0; d < cdim; ++d) ccoords[d] /= (numCoords / cdim);
1564: PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &cellCoords));
1565: for (d = 0; d < cdim; ++d) {
1566: if (d > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ","));
1567: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", ccoords[d]));
1568: }
1569: if (drawHasse) color = colors[depth % numColors];
1570: else color = colors[rank % numColors];
1571: for (l = 0; l < numLabels; ++l) {
1572: PetscInt val;
1573: PetscCall(DMGetLabelValue(dm, names[l], c, &val));
1574: if (val >= 0) {
1575: color = lcolors[l % numLColors];
1576: isLabeled = PETSC_TRUE;
1577: break;
1578: }
1579: }
1580: if (drawNumbers[dim]) {
1581: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "};\n", c, rank, color, c));
1582: } else if (drawColors[dim]) {
1583: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", c, rank, !isLabeled ? 1 : 2, color));
1584: } else PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [] {};\n", c, rank));
1585: }
1586: if (drawHasse) {
1587: int height = 0;
1589: color = colors[depth % numColors];
1590: PetscCall(PetscViewerASCIIPrintf(viewer, "%% Cells\n"));
1591: PetscCall(PetscViewerASCIIPrintf(viewer, "\\foreach \\c in {\\cStart,...,\\cEnd}\n"));
1592: PetscCall(PetscViewerASCIIPrintf(viewer, "{\n"));
1593: PetscCall(PetscViewerASCIIPrintf(viewer, " \\node(\\c_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\cShift+\\c-\\cStart,%d) {\\c};\n", rank, color, height++));
1594: PetscCall(PetscViewerASCIIPrintf(viewer, "}\n"));
1596: if (depth > 2) {
1597: color = colors[1 % numColors];
1598: PetscCall(PetscViewerASCIIPrintf(viewer, "%% Faces\n"));
1599: PetscCall(PetscViewerASCIIPrintf(viewer, "\\foreach \\f in {\\fStart,...,\\fEnd}\n"));
1600: PetscCall(PetscViewerASCIIPrintf(viewer, "{\n"));
1601: PetscCall(PetscViewerASCIIPrintf(viewer, " \\node(\\f_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\fShift+\\f-\\fStart,%d) {\\f};\n", rank, color, height++));
1602: PetscCall(PetscViewerASCIIPrintf(viewer, "}\n"));
1603: }
1605: color = colors[1 % numColors];
1606: PetscCall(PetscViewerASCIIPrintf(viewer, "%% Edges\n"));
1607: PetscCall(PetscViewerASCIIPrintf(viewer, "\\foreach \\e in {\\eStart,...,\\eEnd}\n"));
1608: PetscCall(PetscViewerASCIIPrintf(viewer, "{\n"));
1609: PetscCall(PetscViewerASCIIPrintf(viewer, " \\node(\\e_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\eShift+\\e-\\eStart,%d) {\\e};\n", rank, color, height++));
1610: PetscCall(PetscViewerASCIIPrintf(viewer, "}\n"));
1612: color = colors[0 % numColors];
1613: PetscCall(PetscViewerASCIIPrintf(viewer, "%% Vertices\n"));
1614: PetscCall(PetscViewerASCIIPrintf(viewer, "\\foreach \\v in {\\vStart,...,\\vEnd}\n"));
1615: PetscCall(PetscViewerASCIIPrintf(viewer, "{\n"));
1616: PetscCall(PetscViewerASCIIPrintf(viewer, " \\node(\\v_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\vShift+\\v-\\vStart,%d) {\\v};\n", rank, color, height++));
1617: PetscCall(PetscViewerASCIIPrintf(viewer, "}\n"));
1619: for (p = pStart; p < pEnd; ++p) {
1620: const PetscInt *cone;
1621: PetscInt coneSize, cp;
1623: PetscCall(DMPlexGetCone(dm, p, &cone));
1624: PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
1625: for (cp = 0; cp < coneSize; ++cp) PetscCall(PetscViewerASCIIPrintf(viewer, "\\draw[->, shorten >=1pt] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", cone[cp], rank, p, rank));
1626: }
1627: }
1628: PetscCall(PetscViewerFlush(viewer));
1629: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1630: PetscCall(PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n"));
1631: PetscCall(PetscViewerASCIIPrintf(viewer, "\\end{document}\n"));
1632: for (l = 0; l < numLabels; ++l) PetscCall(PetscFree(names[l]));
1633: for (c = 0; c < numColors; ++c) PetscCall(PetscFree(colors[c]));
1634: for (c = 0; c < numLColors; ++c) PetscCall(PetscFree(lcolors[c]));
1635: PetscCall(PetscFree3(names, colors, lcolors));
1636: PetscCall(PetscBTDestroy(&wp));
1637: } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
1638: Vec cown, acown;
1639: VecScatter sct;
1640: ISLocalToGlobalMapping g2l;
1641: IS gid, acis;
1642: MPI_Comm comm, ncomm = MPI_COMM_NULL;
1643: MPI_Group ggroup, ngroup;
1644: PetscScalar *array, nid;
1645: const PetscInt *idxs;
1646: PetscInt *idxs2, *start, *adjacency, *work;
1647: PetscInt64 lm[3], gm[3];
1648: PetscInt i, c, cStart, cEnd, cum, numVertices, ect, ectn, cellHeight;
1649: PetscMPIInt d1, d2, rank;
1651: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
1652: PetscCallMPI(MPI_Comm_rank(comm, &rank));
1653: #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1654: PetscCallMPI(MPI_Comm_split_type(comm, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &ncomm));
1655: #endif
1656: if (ncomm != MPI_COMM_NULL) {
1657: PetscCallMPI(MPI_Comm_group(comm, &ggroup));
1658: PetscCallMPI(MPI_Comm_group(ncomm, &ngroup));
1659: d1 = 0;
1660: PetscCallMPI(MPI_Group_translate_ranks(ngroup, 1, &d1, ggroup, &d2));
1661: nid = d2;
1662: PetscCallMPI(MPI_Group_free(&ggroup));
1663: PetscCallMPI(MPI_Group_free(&ngroup));
1664: PetscCallMPI(MPI_Comm_free(&ncomm));
1665: } else nid = 0.0;
1667: /* Get connectivity */
1668: PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
1669: PetscCall(DMPlexCreatePartitionerGraph(dm, cellHeight, &numVertices, &start, &adjacency, &gid));
1671: /* filter overlapped local cells */
1672: PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
1673: PetscCall(ISGetIndices(gid, &idxs));
1674: PetscCall(ISGetLocalSize(gid, &cum));
1675: PetscCall(PetscMalloc1(cum, &idxs2));
1676: for (c = cStart, cum = 0; c < cEnd; c++) {
1677: if (idxs[c - cStart] < 0) continue;
1678: idxs2[cum++] = idxs[c - cStart];
1679: }
1680: PetscCall(ISRestoreIndices(gid, &idxs));
1681: PetscCheck(numVertices == cum, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected %" PetscInt_FMT " != %" PetscInt_FMT, numVertices, cum);
1682: PetscCall(ISDestroy(&gid));
1683: PetscCall(ISCreateGeneral(comm, numVertices, idxs2, PETSC_OWN_POINTER, &gid));
1685: /* support for node-aware cell locality */
1686: PetscCall(ISCreateGeneral(comm, start[numVertices], adjacency, PETSC_USE_POINTER, &acis));
1687: PetscCall(VecCreateSeq(PETSC_COMM_SELF, start[numVertices], &acown));
1688: PetscCall(VecCreateMPI(comm, numVertices, PETSC_DECIDE, &cown));
1689: PetscCall(VecGetArray(cown, &array));
1690: for (c = 0; c < numVertices; c++) array[c] = nid;
1691: PetscCall(VecRestoreArray(cown, &array));
1692: PetscCall(VecScatterCreate(cown, acis, acown, NULL, &sct));
1693: PetscCall(VecScatterBegin(sct, cown, acown, INSERT_VALUES, SCATTER_FORWARD));
1694: PetscCall(VecScatterEnd(sct, cown, acown, INSERT_VALUES, SCATTER_FORWARD));
1695: PetscCall(ISDestroy(&acis));
1696: PetscCall(VecScatterDestroy(&sct));
1697: PetscCall(VecDestroy(&cown));
1699: /* compute edgeCut */
1700: for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum, start[c + 1] - start[c]);
1701: PetscCall(PetscMalloc1(cum, &work));
1702: PetscCall(ISLocalToGlobalMappingCreateIS(gid, &g2l));
1703: PetscCall(ISLocalToGlobalMappingSetType(g2l, ISLOCALTOGLOBALMAPPINGHASH));
1704: PetscCall(ISDestroy(&gid));
1705: PetscCall(VecGetArray(acown, &array));
1706: for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
1707: PetscInt totl;
1709: totl = start[c + 1] - start[c];
1710: PetscCall(ISGlobalToLocalMappingApply(g2l, IS_GTOLM_MASK, totl, adjacency + start[c], NULL, work));
1711: for (i = 0; i < totl; i++) {
1712: if (work[i] < 0) {
1713: ect += 1;
1714: ectn += (array[i + start[c]] != nid) ? 0 : 1;
1715: }
1716: }
1717: }
1718: PetscCall(PetscFree(work));
1719: PetscCall(VecRestoreArray(acown, &array));
1720: lm[0] = numVertices > 0 ? numVertices : PETSC_INT_MAX;
1721: lm[1] = -numVertices;
1722: PetscCallMPI(MPIU_Allreduce(lm, gm, 2, MPIU_INT64, MPI_MIN, comm));
1723: PetscCall(PetscViewerASCIIPrintf(viewer, " Cell balance: %.2f (max %" PetscInt64_FMT ", min %" PetscInt64_FMT, -((double)gm[1]) / ((double)gm[0]), -gm[1], gm[0]));
1724: lm[0] = ect; /* edgeCut */
1725: lm[1] = ectn; /* node-aware edgeCut */
1726: lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
1727: PetscCallMPI(MPIU_Allreduce(lm, gm, 3, MPIU_INT64, MPI_SUM, comm));
1728: PetscCall(PetscViewerASCIIPrintf(viewer, ", empty %" PetscInt64_FMT ")\n", gm[2]));
1729: #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1730: PetscCall(PetscViewerASCIIPrintf(viewer, " Edge Cut: %" PetscInt64_FMT " (on node %.3f)\n", gm[0] / 2, gm[0] ? ((double)gm[1]) / ((double)gm[0]) : 1.));
1731: #else
1732: PetscCall(PetscViewerASCIIPrintf(viewer, " Edge Cut: %" PetscInt64_FMT " (on node %.3f)\n", gm[0] / 2, 0.0));
1733: #endif
1734: PetscCall(ISLocalToGlobalMappingDestroy(&g2l));
1735: PetscCall(PetscFree(start));
1736: PetscCall(PetscFree(adjacency));
1737: PetscCall(VecDestroy(&acown));
1738: } else {
1739: const char *name;
1740: PetscInt *sizes, *hybsizes, *ghostsizes;
1741: PetscInt locDepth, depth, cellHeight, dim, d;
1742: PetscInt pStart, pEnd, p, gcStart, gcEnd, gcNum;
1743: PetscInt numLabels, l, maxSize = 17;
1744: DMPolytopeType ct0 = DM_POLYTOPE_UNKNOWN;
1745: MPI_Comm comm;
1746: PetscMPIInt size, rank;
1748: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
1749: PetscCallMPI(MPI_Comm_size(comm, &size));
1750: PetscCallMPI(MPI_Comm_rank(comm, &rank));
1751: PetscCall(DMGetDimension(dm, &dim));
1752: PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
1753: PetscCall(PetscObjectGetName((PetscObject)dm, &name));
1754: if (name) PetscCall(PetscViewerASCIIPrintf(viewer, "%s in %" PetscInt_FMT " dimension%s:\n", name, dim, dim == 1 ? "" : "s"));
1755: else PetscCall(PetscViewerASCIIPrintf(viewer, "Mesh in %" PetscInt_FMT " dimension%s:\n", dim, dim == 1 ? "" : "s"));
1756: if (cellHeight) PetscCall(PetscViewerASCIIPrintf(viewer, " Cells are at height %" PetscInt_FMT "\n", cellHeight));
1757: PetscCall(DMPlexGetDepth(dm, &locDepth));
1758: PetscCallMPI(MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm));
1759: PetscCall(DMPlexGetCellTypeStratum(dm, DM_POLYTOPE_FV_GHOST, &gcStart, &gcEnd));
1760: gcNum = gcEnd - gcStart;
1761: if (size < maxSize) PetscCall(PetscCalloc3(size, &sizes, size, &hybsizes, size, &ghostsizes));
1762: else PetscCall(PetscCalloc3(3, &sizes, 3, &hybsizes, 3, &ghostsizes));
1763: for (d = 0; d <= depth; d++) {
1764: PetscInt Nc[2] = {0, 0}, ict;
1766: PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
1767: if (pStart < pEnd) PetscCall(DMPlexGetCellType(dm, pStart, &ct0));
1768: ict = ct0;
1769: PetscCallMPI(MPI_Bcast(&ict, 1, MPIU_INT, 0, comm));
1770: ct0 = (DMPolytopeType)ict;
1771: for (p = pStart; p < pEnd; ++p) {
1772: DMPolytopeType ct;
1774: PetscCall(DMPlexGetCellType(dm, p, &ct));
1775: if (ct == ct0) ++Nc[0];
1776: else ++Nc[1];
1777: }
1778: if (size < maxSize) {
1779: PetscCallMPI(MPI_Gather(&Nc[0], 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm));
1780: PetscCallMPI(MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm));
1781: if (d == depth) PetscCallMPI(MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm));
1782: PetscCall(PetscViewerASCIIPrintf(viewer, " Number of %" PetscInt_FMT "-cells per rank:", (depth == 1) && d ? dim : d));
1783: for (p = 0; p < size; ++p) {
1784: if (rank == 0) {
1785: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, sizes[p] + hybsizes[p]));
1786: if (hybsizes[p] > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ")", hybsizes[p]));
1787: if (ghostsizes[p] > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "]", ghostsizes[p]));
1788: }
1789: }
1790: } else {
1791: sizes[0] = Nc[0] + Nc[1];
1792: sizes[1] = Nc[0] + Nc[1];
1793: PetscCall(PetscGlobalMinMaxInt(comm, sizes, sizes));
1794: hybsizes[0] = Nc[1];
1795: hybsizes[1] = Nc[1];
1796: PetscCall(PetscGlobalMinMaxInt(comm, hybsizes, hybsizes));
1797: if (d == depth) {
1798: ghostsizes[0] = gcNum;
1799: ghostsizes[1] = gcNum;
1800: PetscCall(PetscGlobalMinMaxInt(comm, ghostsizes, ghostsizes));
1801: }
1802: PetscCall(PetscViewerASCIIPrintf(viewer, " Min/Max of %" PetscInt_FMT "-cells per rank:", (depth == 1) && d ? dim : d));
1803: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT "/%" PetscInt_FMT, sizes[0], sizes[1]));
1804: if (hybsizes[0] > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT "/%" PetscInt_FMT ")", hybsizes[0], hybsizes[1]));
1805: if (ghostsizes[0] > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "/%" PetscInt_FMT "]", ghostsizes[0], ghostsizes[1]));
1806: }
1807: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1808: }
1809: PetscCall(PetscFree3(sizes, hybsizes, ghostsizes));
1810: {
1811: DM cdm;
1812: const PetscReal *maxCell;
1813: const PetscReal *L;
1814: PetscBool localized;
1816: PetscCall(DMGetCoordinateDM(dm, &cdm));
1817: PetscCall(DMViewDSFromOptions_Internal(cdm, "-plex_view_ds"));
1818: PetscCall(DMViewSectionFromOptions_Internal(cdm, "-plex_view_section"));
1819: PetscCall(DMGetPeriodicity(dm, &maxCell, NULL, &L));
1820: PetscCall(DMGetCoordinatesLocalized(dm, &localized));
1821: if (L || localized) {
1822: PetscCall(PetscViewerASCIIPrintf(viewer, "Periodic mesh"));
1823: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
1824: if (L) {
1825: PetscCall(PetscViewerASCIIPrintf(viewer, " ("));
1826: for (d = 0; d < dim; ++d) {
1827: if (d > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ", "));
1828: PetscCall(PetscViewerASCIIPrintf(viewer, "%s", L[d] > 0.0 ? "PERIODIC" : "NONE"));
1829: }
1830: PetscCall(PetscViewerASCIIPrintf(viewer, ")"));
1831: }
1832: PetscCall(PetscViewerASCIIPrintf(viewer, " coordinates %s\n", localized ? "localized" : "not localized"));
1833: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
1834: }
1835: }
1836: PetscCall(DMGetNumLabels(dm, &numLabels));
1837: if (numLabels) PetscCall(PetscViewerASCIIPrintf(viewer, "Labels:\n"));
1838: for (l = 0; l < numLabels; ++l) {
1839: DMLabel label;
1840: const char *name;
1841: PetscInt *values;
1842: PetscInt numValues, v;
1844: PetscCall(DMGetLabelName(dm, l, &name));
1845: PetscCall(DMGetLabel(dm, name, &label));
1846: PetscCall(DMLabelGetNumValues(label, &numValues));
1847: PetscCall(PetscViewerASCIIPrintf(viewer, " %s: %" PetscInt_FMT " strata with value/size (", name, numValues));
1849: { // Extract array of DMLabel values so it can be sorted
1850: IS is_values;
1851: const PetscInt *is_values_local = NULL;
1853: PetscCall(DMLabelGetValueIS(label, &is_values));
1854: PetscCall(ISGetIndices(is_values, &is_values_local));
1855: PetscCall(PetscMalloc1(numValues, &values));
1856: PetscCall(PetscArraycpy(values, is_values_local, numValues));
1857: PetscCall(PetscSortInt(numValues, values));
1858: PetscCall(ISRestoreIndices(is_values, &is_values_local));
1859: PetscCall(ISDestroy(&is_values));
1860: }
1861: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
1862: for (v = 0; v < numValues; ++v) {
1863: PetscInt size;
1865: PetscCall(DMLabelGetStratumSize(label, values[v], &size));
1866: if (v > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ", "));
1867: PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " (%" PetscInt_FMT ")", values[v], size));
1868: }
1869: PetscCall(PetscViewerASCIIPrintf(viewer, ")\n"));
1870: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
1871: PetscCall(PetscFree(values));
1872: }
1873: {
1874: char **labelNames;
1875: PetscInt Nl = numLabels;
1876: PetscBool flg;
1878: PetscCall(PetscMalloc1(Nl, &labelNames));
1879: PetscCall(PetscOptionsGetStringArray(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_labels", labelNames, &Nl, &flg));
1880: for (l = 0; l < Nl; ++l) {
1881: DMLabel label;
1883: PetscCall(DMHasLabel(dm, labelNames[l], &flg));
1884: if (flg) {
1885: PetscCall(DMGetLabel(dm, labelNames[l], &label));
1886: PetscCall(DMLabelView(label, viewer));
1887: }
1888: PetscCall(PetscFree(labelNames[l]));
1889: }
1890: PetscCall(PetscFree(labelNames));
1891: }
1892: /* If no fields are specified, people do not want to see adjacency */
1893: if (dm->Nf) {
1894: PetscInt f;
1896: for (f = 0; f < dm->Nf; ++f) {
1897: const char *name;
1899: PetscCall(PetscObjectGetName(dm->fields[f].disc, &name));
1900: if (numLabels) PetscCall(PetscViewerASCIIPrintf(viewer, "Field %s:\n", name));
1901: PetscCall(PetscViewerASCIIPushTab(viewer));
1902: if (dm->fields[f].label) PetscCall(DMLabelView(dm->fields[f].label, viewer));
1903: if (dm->fields[f].adjacency[0]) {
1904: if (dm->fields[f].adjacency[1]) PetscCall(PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n"));
1905: else PetscCall(PetscViewerASCIIPrintf(viewer, "adjacency FVM\n"));
1906: } else {
1907: if (dm->fields[f].adjacency[1]) PetscCall(PetscViewerASCIIPrintf(viewer, "adjacency FEM\n"));
1908: else PetscCall(PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n"));
1909: }
1910: PetscCall(PetscViewerASCIIPopTab(viewer));
1911: }
1912: }
1913: DMPlexTransform tr;
1915: PetscCall(DMPlexGetTransform(dm, &tr));
1916: if (tr) {
1917: PetscCall(PetscViewerASCIIPushTab(viewer));
1918: PetscCall(PetscViewerASCIIPrintf(viewer, "Created using transform:\n"));
1919: PetscCall(DMPlexTransformView(tr, viewer));
1920: PetscCall(PetscViewerASCIIPopTab(viewer));
1921: }
1922: PetscCall(DMGetCoarseDM(dm, &cdm));
1923: if (cdm) {
1924: PetscCall(PetscViewerASCIIPushTab(viewer));
1925: PetscCall(PetscViewerASCIIPrintf(viewer, "Defined by transform from:\n"));
1926: PetscCall(DMPlexView_Ascii(cdm, viewer));
1927: PetscCall(PetscViewerASCIIPopTab(viewer));
1928: }
1929: }
1930: PetscFunctionReturn(PETSC_SUCCESS);
1931: }
1933: /*@
1934: DMPlexDrawCell - Draw the given cell on the `PetscDraw` object.
1936: Not collective
1938: Input Parameters:
1939: + dm - The `DMPLEX` object
1940: . draw - The `PetscDraw` object
1941: . lC - The line color, or `PETSC_DETERMINE` to use the default
1942: . cC - The cell color, or `PETSC_DETERMINE` to use the default
1943: . cell - The cell to draw
1944: - coords - The vertex coordinates for the cell
1946: Level: developer
1948: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`
1949: @*/
1950: PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt lC, PetscInt cC, PetscInt cell, const PetscScalar coords[])
1951: {
1952: DMPolytopeType ct;
1953: PetscMPIInt rank;
1954: PetscInt cdim;
1955: int lineColor, cellColor;
1957: PetscFunctionBegin;
1960: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1961: PetscCall(DMPlexGetCellType(dm, cell, &ct));
1962: PetscCall(DMGetCoordinateDim(dm, &cdim));
1963: lineColor = (int)(lC == PETSC_DETERMINE ? PETSC_DRAW_BLACK : lC);
1964: cellColor = (int)(cC == PETSC_DETERMINE ? PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2 : cC);
1965: switch (ct) {
1966: case DM_POLYTOPE_SEGMENT:
1967: case DM_POLYTOPE_POINT_PRISM_TENSOR:
1968: switch (cdim) {
1969: case 1: {
1970: const PetscReal y = 0.5; /* TODO Put it in the middle of the viewport */
1971: const PetscReal dy = 0.05; /* TODO Make it a fraction of the total length */
1973: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), y, PetscRealPart(coords[1]), y, lineColor));
1974: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), y + dy, PetscRealPart(coords[0]), y - dy, lineColor));
1975: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[1]), y + dy, PetscRealPart(coords[1]), y - dy, lineColor));
1976: } break;
1977: case 2: {
1978: const PetscReal dx = (PetscRealPart(coords[3]) - PetscRealPart(coords[1]));
1979: const PetscReal dy = (PetscRealPart(coords[2]) - PetscRealPart(coords[0]));
1980: const PetscReal l = 0.1 / PetscSqrtReal(dx * dx + dy * dy);
1982: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), lineColor));
1983: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]) + l * dx, PetscRealPart(coords[1]) + l * dy, PetscRealPart(coords[0]) - l * dx, PetscRealPart(coords[1]) - l * dy, lineColor));
1984: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[2]) + l * dx, PetscRealPart(coords[3]) + l * dy, PetscRealPart(coords[2]) - l * dx, PetscRealPart(coords[3]) - l * dy, lineColor));
1985: } break;
1986: default:
1987: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of dimension %" PetscInt_FMT, cdim);
1988: }
1989: break;
1990: case DM_POLYTOPE_TRIANGLE:
1991: if (cellColor >= 0) PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), cellColor, cellColor, cellColor));
1992: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), lineColor));
1993: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), lineColor));
1994: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), lineColor));
1995: break;
1996: case DM_POLYTOPE_QUADRILATERAL:
1997: if (cellColor >= 0) {
1998: PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), cellColor, cellColor, cellColor));
1999: PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), cellColor, cellColor, cellColor));
2000: }
2001: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), lineColor));
2002: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), lineColor));
2003: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), lineColor));
2004: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), lineColor));
2005: break;
2006: case DM_POLYTOPE_SEG_PRISM_TENSOR:
2007: if (cellColor >= 0) {
2008: PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), cellColor, cellColor, cellColor));
2009: PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), cellColor, cellColor, cellColor));
2010: }
2011: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), lineColor));
2012: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), lineColor));
2013: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), lineColor));
2014: PetscCall(PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), lineColor));
2015: break;
2016: case DM_POLYTOPE_FV_GHOST:
2017: break;
2018: default:
2019: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
2020: }
2021: PetscFunctionReturn(PETSC_SUCCESS);
2022: }
2024: static PetscErrorCode DrawPolygon_Private(DM dm, PetscDraw draw, PetscInt cell, PetscInt Nv, const PetscReal refVertices[], const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
2025: {
2026: PetscReal centroid[2] = {0., 0.};
2027: PetscMPIInt rank;
2028: PetscMPIInt fillColor;
2030: PetscFunctionBegin;
2031: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
2032: fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2;
2033: for (PetscInt v = 0; v < Nv; ++v) {
2034: centroid[0] += PetscRealPart(coords[v * 2 + 0]) / Nv;
2035: centroid[1] += PetscRealPart(coords[v * 2 + 1]) / Nv;
2036: }
2037: for (PetscInt e = 0; e < Nv; ++e) {
2038: refCoords[0] = refVertices[e * 2 + 0];
2039: refCoords[1] = refVertices[e * 2 + 1];
2040: for (PetscInt d = 1; d <= edgeDiv; ++d) {
2041: refCoords[d * 2 + 0] = refCoords[0] + (refVertices[(e + 1) % Nv * 2 + 0] - refCoords[0]) * d / edgeDiv;
2042: refCoords[d * 2 + 1] = refCoords[1] + (refVertices[(e + 1) % Nv * 2 + 1] - refCoords[1]) * d / edgeDiv;
2043: }
2044: PetscCall(DMPlexReferenceToCoordinates(dm, cell, edgeDiv + 1, refCoords, edgeCoords));
2045: for (PetscInt d = 0; d < edgeDiv; ++d) {
2046: PetscCall(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));
2047: PetscCall(PetscDrawLine(draw, edgeCoords[d * 2 + 0], edgeCoords[d * 2 + 1], edgeCoords[(d + 1) * 2 + 0], edgeCoords[(d + 1) * 2 + 1], PETSC_DRAW_BLACK));
2048: }
2049: }
2050: PetscFunctionReturn(PETSC_SUCCESS);
2051: }
2053: static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
2054: {
2055: DMPolytopeType ct;
2057: PetscFunctionBegin;
2058: PetscCall(DMPlexGetCellType(dm, cell, &ct));
2059: switch (ct) {
2060: case DM_POLYTOPE_TRIANGLE: {
2061: PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};
2063: PetscCall(DrawPolygon_Private(dm, draw, cell, 3, refVertices, coords, edgeDiv, refCoords, edgeCoords));
2064: } break;
2065: case DM_POLYTOPE_QUADRILATERAL: {
2066: PetscReal refVertices[8] = {-1., -1., 1., -1., 1., 1., -1., 1.};
2068: PetscCall(DrawPolygon_Private(dm, draw, cell, 4, refVertices, coords, edgeDiv, refCoords, edgeCoords));
2069: } break;
2070: default:
2071: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
2072: }
2073: PetscFunctionReturn(PETSC_SUCCESS);
2074: }
2076: static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
2077: {
2078: PetscDraw draw;
2079: DM cdm;
2080: PetscSection coordSection;
2081: Vec coordinates;
2082: PetscReal xyl[3], xyr[3];
2083: PetscReal *refCoords, *edgeCoords;
2084: PetscBool isnull, drawAffine;
2085: PetscInt dim, vStart, vEnd, cStart, cEnd, c, cDegree, edgeDiv, lineColor = PETSC_DETERMINE, cellColor = PETSC_DETERMINE;
2087: PetscFunctionBegin;
2088: PetscCall(DMGetCoordinateDim(dm, &dim));
2089: PetscCheck(dim <= 2, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %" PetscInt_FMT, dim);
2090: PetscCall(DMGetCoordinateDegree_Internal(dm, &cDegree));
2091: drawAffine = cDegree > 1 ? PETSC_FALSE : PETSC_TRUE;
2092: edgeDiv = cDegree + 1;
2093: PetscCall(PetscOptionsGetInt(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_view_draw_line_color", &lineColor, NULL));
2094: PetscCall(PetscOptionsGetInt(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_view_draw_cell_color", &cellColor, NULL));
2095: PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL));
2096: if (!drawAffine) PetscCall(PetscMalloc2((edgeDiv + 1) * dim, &refCoords, (edgeDiv + 1) * dim, &edgeCoords));
2097: PetscCall(DMGetCoordinateDM(dm, &cdm));
2098: PetscCall(DMGetLocalSection(cdm, &coordSection));
2099: PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
2100: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
2101: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2103: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
2104: PetscCall(PetscDrawIsNull(draw, &isnull));
2105: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
2106: PetscCall(PetscDrawSetTitle(draw, "Mesh"));
2108: PetscCall(DMGetBoundingBox(dm, xyl, xyr));
2109: PetscCall(PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]));
2110: PetscCall(PetscDrawClear(draw));
2112: for (c = cStart; c < cEnd; ++c) {
2113: PetscScalar *coords = NULL;
2114: const PetscScalar *coords_arr;
2115: PetscInt numCoords;
2116: PetscBool isDG;
2118: PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &coords_arr, &coords));
2119: if (drawAffine) PetscCall(DMPlexDrawCell(dm, draw, lineColor, cellColor, c, coords));
2120: else PetscCall(DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords));
2121: PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &coords_arr, &coords));
2122: }
2123: if (!drawAffine) PetscCall(PetscFree2(refCoords, edgeCoords));
2124: PetscCall(PetscDrawFlush(draw));
2125: PetscCall(PetscDrawPause(draw));
2126: PetscCall(PetscDrawSave(draw));
2127: PetscFunctionReturn(PETSC_SUCCESS);
2128: }
2130: static PetscErrorCode DMPlexCreateHighOrderSurrogate_Internal(DM dm, DM *hdm)
2131: {
2132: DM odm = dm, rdm = dm, cdm;
2133: PetscFE fe;
2134: PetscSpace sp;
2135: PetscClassId id;
2136: PetscInt degree;
2137: PetscBool hoView = PETSC_TRUE;
2139: PetscFunctionBegin;
2140: PetscObjectOptionsBegin((PetscObject)dm);
2141: PetscCall(PetscOptionsBool("-dm_plex_high_order_view", "Subsample to view meshes with high order coordinates", "DMPlexCreateHighOrderSurrogate_Internal", hoView, &hoView, NULL));
2142: PetscOptionsEnd();
2143: PetscCall(PetscObjectReference((PetscObject)dm));
2144: *hdm = dm;
2145: if (!hoView) PetscFunctionReturn(PETSC_SUCCESS);
2146: PetscCall(DMGetCoordinateDM(dm, &cdm));
2147: PetscCall(DMGetField(cdm, 0, NULL, (PetscObject *)&fe));
2148: PetscCall(PetscObjectGetClassId((PetscObject)fe, &id));
2149: if (id != PETSCFE_CLASSID) PetscFunctionReturn(PETSC_SUCCESS);
2150: PetscCall(PetscFEGetBasisSpace(fe, &sp));
2151: PetscCall(PetscSpaceGetDegree(sp, °ree, NULL));
2152: for (PetscInt r = 0, rd = PetscCeilReal(((PetscReal)degree) / 2.); r < (PetscInt)PetscCeilReal(PetscLog2Real(degree)); ++r, rd = PetscCeilReal(((PetscReal)rd) / 2.)) {
2153: DM cdm, rcdm;
2154: Mat In;
2155: Vec cl, rcl;
2157: PetscCall(DMRefine(odm, PetscObjectComm((PetscObject)odm), &rdm));
2158: PetscCall(DMPlexCreateCoordinateSpace(rdm, rd, PETSC_FALSE, PETSC_FALSE));
2159: PetscCall(PetscObjectSetName((PetscObject)rdm, "Refined Mesh with Linear Coordinates"));
2160: PetscCall(DMGetCoordinateDM(odm, &cdm));
2161: PetscCall(DMGetCoordinateDM(rdm, &rcdm));
2162: PetscCall(DMGetCoordinatesLocal(odm, &cl));
2163: PetscCall(DMGetCoordinatesLocal(rdm, &rcl));
2164: PetscCall(DMSetCoarseDM(rcdm, cdm));
2165: PetscCall(DMCreateInterpolation(cdm, rcdm, &In, NULL));
2166: PetscCall(MatMult(In, cl, rcl));
2167: PetscCall(MatDestroy(&In));
2168: PetscCall(DMSetCoordinatesLocal(rdm, rcl));
2169: PetscCall(DMDestroy(&odm));
2170: odm = rdm;
2171: }
2172: *hdm = rdm;
2173: PetscFunctionReturn(PETSC_SUCCESS);
2174: }
2176: #if defined(PETSC_HAVE_EXODUSII)
2177: #include <exodusII.h>
2178: #endif
2180: PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
2181: {
2182: PetscBool isascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus, iscgns, ispython;
2183: char name[PETSC_MAX_PATH_LEN];
2185: PetscFunctionBegin;
2188: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
2189: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk));
2190: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2191: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
2192: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis));
2193: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodus));
2194: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns));
2195: PetscCall(PetscObjectHasFunction((PetscObject)viewer, "PetscViewerPythonViewObject_C", &ispython));
2196: if (isascii) {
2197: PetscViewerFormat format;
2198: PetscCall(PetscViewerGetFormat(viewer, &format));
2199: if (format == PETSC_VIEWER_ASCII_GLVIS) PetscCall(DMPlexView_GLVis(dm, viewer));
2200: else PetscCall(DMPlexView_Ascii(dm, viewer));
2201: } else if (ishdf5) {
2202: #if defined(PETSC_HAVE_HDF5)
2203: PetscCall(DMPlexView_HDF5_Internal(dm, viewer));
2204: #else
2205: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2206: #endif
2207: } else if (isvtk) {
2208: PetscCall(DMPlexVTKWriteAll((PetscObject)dm, viewer));
2209: } else if (isdraw) {
2210: DM hdm;
2212: PetscCall(DMPlexCreateHighOrderSurrogate_Internal(dm, &hdm));
2213: PetscCall(DMPlexView_Draw(hdm, viewer));
2214: PetscCall(DMDestroy(&hdm));
2215: } else if (isglvis) {
2216: PetscCall(DMPlexView_GLVis(dm, viewer));
2217: #if defined(PETSC_HAVE_EXODUSII)
2218: } else if (isexodus) {
2219: /*
2220: ExodusII requires that all sets be part of exactly one cell set.
2221: If the dm does not have a "Cell Sets" label defined, we create one
2222: with ID 1, containing all cells.
2223: Note that if the Cell Sets label is defined but does not cover all cells,
2224: we may still have a problem. This should probably be checked here or in the viewer;
2225: */
2226: PetscInt numCS;
2227: PetscCall(DMGetLabelSize(dm, "Cell Sets", &numCS));
2228: if (!numCS) {
2229: PetscInt cStart, cEnd, c;
2230: PetscCall(DMCreateLabel(dm, "Cell Sets"));
2231: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2232: for (c = cStart; c < cEnd; ++c) PetscCall(DMSetLabelValue(dm, "Cell Sets", c, 1));
2233: }
2234: PetscCall(DMView_PlexExodusII(dm, viewer));
2235: #endif
2236: #if defined(PETSC_HAVE_CGNS)
2237: } else if (iscgns) {
2238: PetscCall(DMView_PlexCGNS(dm, viewer));
2239: #endif
2240: } else if (ispython) {
2241: PetscCall(PetscViewerPythonViewObject(viewer, (PetscObject)dm));
2242: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
2243: /* Optionally view the partition */
2244: PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_partition_view", &flg));
2245: if (flg) {
2246: Vec ranks;
2247: PetscCall(DMPlexCreateRankField(dm, &ranks));
2248: PetscCall(VecView(ranks, viewer));
2249: PetscCall(VecDestroy(&ranks));
2250: }
2251: /* Optionally view a label */
2252: PetscCall(PetscOptionsGetString(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_label_view", name, sizeof(name), &flg));
2253: if (flg) {
2254: DMLabel label;
2255: Vec val;
2257: PetscCall(DMGetLabel(dm, name, &label));
2258: PetscCheck(label, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Label %s provided to -dm_label_view does not exist in this DM", name);
2259: PetscCall(DMPlexCreateLabelField(dm, label, &val));
2260: PetscCall(VecView(val, viewer));
2261: PetscCall(VecDestroy(&val));
2262: }
2263: PetscFunctionReturn(PETSC_SUCCESS);
2264: }
2266: /*@
2267: DMPlexTopologyView - Saves a `DMPLEX` topology into a file
2269: Collective
2271: Input Parameters:
2272: + dm - The `DM` whose topology is to be saved
2273: - viewer - The `PetscViewer` to save it in
2275: Level: advanced
2277: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsView()`, `DMPlexTopologyLoad()`, `PetscViewer`
2278: @*/
2279: PetscErrorCode DMPlexTopologyView(DM dm, PetscViewer viewer)
2280: {
2281: PetscBool ishdf5;
2283: PetscFunctionBegin;
2286: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2287: PetscCall(PetscLogEventBegin(DMPLEX_TopologyView, viewer, 0, 0, 0));
2288: if (ishdf5) {
2289: #if defined(PETSC_HAVE_HDF5)
2290: IS globalPointNumbering;
2291: PetscViewerFormat format;
2293: PetscCall(PetscViewerGetFormat(viewer, &format));
2294: PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
2295: PetscCall(DMPlexCreatePointNumbering(dm, &globalPointNumbering));
2296: PetscCall(DMPlexTopologyView_HDF5_Internal(dm, globalPointNumbering, viewer));
2297: PetscCall(ISDestroy(&globalPointNumbering));
2298: #else
2299: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2300: #endif
2301: }
2302: PetscCall(PetscLogEventEnd(DMPLEX_TopologyView, viewer, 0, 0, 0));
2303: PetscFunctionReturn(PETSC_SUCCESS);
2304: }
2306: /*@
2307: DMPlexCoordinatesView - Saves `DMPLEX` coordinates into a file
2309: Collective
2311: Input Parameters:
2312: + dm - The `DM` whose coordinates are to be saved
2313: - viewer - The `PetscViewer` for saving
2315: Level: advanced
2317: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexLabelsView()`, `DMPlexCoordinatesLoad()`, `PetscViewer`
2318: @*/
2319: PetscErrorCode DMPlexCoordinatesView(DM dm, PetscViewer viewer)
2320: {
2321: PetscBool ishdf5;
2323: PetscFunctionBegin;
2326: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2327: PetscCall(PetscLogEventBegin(DMPLEX_CoordinatesView, viewer, 0, 0, 0));
2328: if (ishdf5) {
2329: #if defined(PETSC_HAVE_HDF5)
2330: PetscViewerFormat format;
2332: PetscCall(PetscViewerGetFormat(viewer, &format));
2333: PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
2334: PetscCall(DMPlexCoordinatesView_HDF5_Internal(dm, viewer));
2335: #else
2336: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2337: #endif
2338: }
2339: PetscCall(PetscLogEventEnd(DMPLEX_CoordinatesView, viewer, 0, 0, 0));
2340: PetscFunctionReturn(PETSC_SUCCESS);
2341: }
2343: /*@
2344: DMPlexLabelsView - Saves `DMPLEX` labels into a file
2346: Collective
2348: Input Parameters:
2349: + dm - The `DM` whose labels are to be saved
2350: - viewer - The `PetscViewer` for saving
2352: Level: advanced
2354: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsLoad()`, `PetscViewer`
2355: @*/
2356: PetscErrorCode DMPlexLabelsView(DM dm, PetscViewer viewer)
2357: {
2358: PetscBool ishdf5;
2360: PetscFunctionBegin;
2363: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2364: PetscCall(PetscLogEventBegin(DMPLEX_LabelsView, viewer, 0, 0, 0));
2365: if (ishdf5) {
2366: #if defined(PETSC_HAVE_HDF5)
2367: IS globalPointNumbering;
2368: PetscViewerFormat format;
2370: PetscCall(PetscViewerGetFormat(viewer, &format));
2371: PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2372: PetscCall(DMPlexCreatePointNumbering(dm, &globalPointNumbering));
2373: PetscCall(DMPlexLabelsView_HDF5_Internal(dm, globalPointNumbering, viewer));
2374: PetscCall(ISDestroy(&globalPointNumbering));
2375: #else
2376: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2377: #endif
2378: }
2379: PetscCall(PetscLogEventEnd(DMPLEX_LabelsView, viewer, 0, 0, 0));
2380: PetscFunctionReturn(PETSC_SUCCESS);
2381: }
2383: /*@
2384: DMPlexSectionView - Saves a section associated with a `DMPLEX`
2386: Collective
2388: Input Parameters:
2389: + dm - The `DM` that contains the topology on which the section to be saved is defined
2390: . viewer - The `PetscViewer` for saving
2391: - sectiondm - The `DM` that contains the section to be saved, can be `NULL`
2393: Level: advanced
2395: Notes:
2396: 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.
2398: 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 (or in case `sectiondm` is `NULL`) 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.
2400: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsView()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`, `PetscSectionView()`, `DMPlexSectionLoad()`, `PetscViewer`
2401: @*/
2402: PetscErrorCode DMPlexSectionView(DM dm, PetscViewer viewer, DM sectiondm)
2403: {
2404: PetscBool ishdf5;
2406: PetscFunctionBegin;
2409: if (!sectiondm) sectiondm = dm;
2411: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2412: PetscCall(PetscLogEventBegin(DMPLEX_SectionView, viewer, 0, 0, 0));
2413: if (ishdf5) {
2414: #if defined(PETSC_HAVE_HDF5)
2415: PetscCall(DMPlexSectionView_HDF5_Internal(dm, viewer, sectiondm));
2416: #else
2417: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2418: #endif
2419: }
2420: PetscCall(PetscLogEventEnd(DMPLEX_SectionView, viewer, 0, 0, 0));
2421: PetscFunctionReturn(PETSC_SUCCESS);
2422: }
2424: /*@
2425: DMPlexGlobalVectorView - Saves a global vector
2427: Collective
2429: Input Parameters:
2430: + dm - The `DM` that represents the topology
2431: . viewer - The `PetscViewer` to save data with
2432: . sectiondm - The `DM` that contains the global section on which vec is defined, can be `NULL`
2433: - vec - The global vector to be saved
2435: Level: advanced
2437: Notes:
2438: 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 (or in case `sectiondm` is `NULL`) 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.
2440: Calling sequence:
2441: .vb
2442: DMCreate(PETSC_COMM_WORLD, &dm);
2443: DMSetType(dm, DMPLEX);
2444: PetscObjectSetName((PetscObject)dm, "topologydm_name");
2445: DMClone(dm, §iondm);
2446: PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2447: PetscSectionCreate(PETSC_COMM_WORLD, §ion);
2448: DMPlexGetChart(sectiondm, &pStart, &pEnd);
2449: PetscSectionSetChart(section, pStart, pEnd);
2450: PetscSectionSetUp(section);
2451: DMSetLocalSection(sectiondm, section);
2452: PetscSectionDestroy(§ion);
2453: DMGetGlobalVector(sectiondm, &vec);
2454: PetscObjectSetName((PetscObject)vec, "vec_name");
2455: DMPlexTopologyView(dm, viewer);
2456: DMPlexSectionView(dm, viewer, sectiondm);
2457: DMPlexGlobalVectorView(dm, viewer, sectiondm, vec);
2458: DMRestoreGlobalVector(sectiondm, &vec);
2459: DMDestroy(§iondm);
2460: DMDestroy(&dm);
2461: .ve
2463: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyView()`, `DMPlexSectionView()`, `DMPlexLocalVectorView()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`
2464: @*/
2465: PetscErrorCode DMPlexGlobalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
2466: {
2467: PetscBool ishdf5;
2469: PetscFunctionBegin;
2472: if (!sectiondm) sectiondm = dm;
2475: /* Check consistency */
2476: {
2477: PetscSection section;
2478: PetscBool includesConstraints;
2479: PetscInt m, m1;
2481: PetscCall(VecGetLocalSize(vec, &m1));
2482: PetscCall(DMGetGlobalSection(sectiondm, §ion));
2483: PetscCall(PetscSectionGetIncludesConstraints(section, &includesConstraints));
2484: if (includesConstraints) PetscCall(PetscSectionGetStorageSize(section, &m));
2485: else PetscCall(PetscSectionGetConstrainedStorageSize(section, &m));
2486: PetscCheck(m1 == m, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Global vector size (%" PetscInt_FMT ") != global section storage size (%" PetscInt_FMT ")", m1, m);
2487: }
2488: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2489: PetscCall(PetscLogEventBegin(DMPLEX_GlobalVectorView, viewer, 0, 0, 0));
2490: if (ishdf5) {
2491: #if defined(PETSC_HAVE_HDF5)
2492: PetscCall(DMPlexGlobalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec));
2493: #else
2494: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2495: #endif
2496: }
2497: PetscCall(PetscLogEventEnd(DMPLEX_GlobalVectorView, viewer, 0, 0, 0));
2498: PetscFunctionReturn(PETSC_SUCCESS);
2499: }
2501: /*@
2502: DMPlexLocalVectorView - Saves a local vector
2504: Collective
2506: Input Parameters:
2507: + dm - The `DM` that represents the topology
2508: . viewer - The `PetscViewer` to save data with
2509: . sectiondm - The `DM` that contains the local section on which `vec` is defined, can be `NULL`
2510: - vec - The local vector to be saved
2512: Level: advanced
2514: Note:
2515: 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 (or in case `sectiondm` is `NULL`) 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.
2517: Calling sequence:
2518: .vb
2519: DMCreate(PETSC_COMM_WORLD, &dm);
2520: DMSetType(dm, DMPLEX);
2521: PetscObjectSetName((PetscObject)dm, "topologydm_name");
2522: DMClone(dm, §iondm);
2523: PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2524: PetscSectionCreate(PETSC_COMM_WORLD, §ion);
2525: DMPlexGetChart(sectiondm, &pStart, &pEnd);
2526: PetscSectionSetChart(section, pStart, pEnd);
2527: PetscSectionSetUp(section);
2528: DMSetLocalSection(sectiondm, section);
2529: DMGetLocalVector(sectiondm, &vec);
2530: PetscObjectSetName((PetscObject)vec, "vec_name");
2531: DMPlexTopologyView(dm, viewer);
2532: DMPlexSectionView(dm, viewer, sectiondm);
2533: DMPlexLocalVectorView(dm, viewer, sectiondm, vec);
2534: DMRestoreLocalVector(sectiondm, &vec);
2535: DMDestroy(§iondm);
2536: DMDestroy(&dm);
2537: .ve
2539: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyView()`, `DMPlexSectionView()`, `DMPlexGlobalVectorView()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`
2540: @*/
2541: PetscErrorCode DMPlexLocalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
2542: {
2543: PetscBool ishdf5;
2545: PetscFunctionBegin;
2548: if (!sectiondm) sectiondm = dm;
2551: /* Check consistency */
2552: {
2553: PetscSection section;
2554: PetscBool includesConstraints;
2555: PetscInt m, m1;
2557: PetscCall(VecGetLocalSize(vec, &m1));
2558: PetscCall(DMGetLocalSection(sectiondm, §ion));
2559: PetscCall(PetscSectionGetIncludesConstraints(section, &includesConstraints));
2560: if (includesConstraints) PetscCall(PetscSectionGetStorageSize(section, &m));
2561: else PetscCall(PetscSectionGetConstrainedStorageSize(section, &m));
2562: PetscCheck(m1 == m, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local vector size (%" PetscInt_FMT ") != local section storage size (%" PetscInt_FMT ")", m1, m);
2563: }
2564: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2565: PetscCall(PetscLogEventBegin(DMPLEX_LocalVectorView, viewer, 0, 0, 0));
2566: if (ishdf5) {
2567: #if defined(PETSC_HAVE_HDF5)
2568: PetscCall(DMPlexLocalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec));
2569: #else
2570: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2571: #endif
2572: }
2573: PetscCall(PetscLogEventEnd(DMPLEX_LocalVectorView, viewer, 0, 0, 0));
2574: PetscFunctionReturn(PETSC_SUCCESS);
2575: }
2577: PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
2578: {
2579: PetscBool ishdf5;
2581: PetscFunctionBegin;
2584: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2585: if (ishdf5) {
2586: #if defined(PETSC_HAVE_HDF5)
2587: PetscViewerFormat format;
2588: PetscCall(PetscViewerGetFormat(viewer, &format));
2589: if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
2590: PetscCall(DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer));
2591: } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2592: PetscCall(DMPlexLoad_HDF5_Internal(dm, viewer));
2593: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2594: PetscFunctionReturn(PETSC_SUCCESS);
2595: #else
2596: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2597: #endif
2598: } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
2599: }
2601: /*@
2602: DMPlexTopologyLoad - Loads a topology into a `DMPLEX`
2604: Collective
2606: Input Parameters:
2607: + dm - The `DM` into which the topology is loaded
2608: - viewer - The `PetscViewer` for the saved topology
2610: Output Parameter:
2611: . globalToLocalPointSF - The `PetscSF` that pushes points in [0, N) to the associated points in the loaded `DMPLEX`, where N is the global number of points;
2612: `NULL` if unneeded
2614: Level: advanced
2616: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexCoordinatesLoad()`, `DMPlexLabelsLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2617: `PetscViewer`, `PetscSF`
2618: @*/
2619: PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *globalToLocalPointSF)
2620: {
2621: PetscBool ishdf5;
2623: PetscFunctionBegin;
2626: if (globalToLocalPointSF) PetscAssertPointer(globalToLocalPointSF, 3);
2627: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2628: PetscCall(PetscLogEventBegin(DMPLEX_TopologyLoad, viewer, 0, 0, 0));
2629: if (ishdf5) {
2630: #if defined(PETSC_HAVE_HDF5)
2631: PetscViewerFormat format;
2633: PetscCall(PetscViewerGetFormat(viewer, &format));
2634: PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2635: PetscCall(DMPlexTopologyLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF));
2636: #else
2637: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2638: #endif
2639: }
2640: PetscCall(PetscLogEventEnd(DMPLEX_TopologyLoad, viewer, 0, 0, 0));
2641: PetscFunctionReturn(PETSC_SUCCESS);
2642: }
2644: /*@
2645: DMPlexCoordinatesLoad - Loads coordinates into a `DMPLEX`
2647: Collective
2649: Input Parameters:
2650: + dm - The `DM` into which the coordinates are loaded
2651: . viewer - The `PetscViewer` for the saved coordinates
2652: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad()` when loading dm from viewer
2654: Level: advanced
2656: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexLabelsLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2657: `PetscSF`, `PetscViewer`
2658: @*/
2659: PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2660: {
2661: PetscBool ishdf5;
2663: PetscFunctionBegin;
2667: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2668: PetscCall(PetscLogEventBegin(DMPLEX_CoordinatesLoad, viewer, 0, 0, 0));
2669: if (ishdf5) {
2670: #if defined(PETSC_HAVE_HDF5)
2671: PetscViewerFormat format;
2673: PetscCall(PetscViewerGetFormat(viewer, &format));
2674: PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2675: PetscCall(DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF));
2676: #else
2677: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2678: #endif
2679: }
2680: PetscCall(PetscLogEventEnd(DMPLEX_CoordinatesLoad, viewer, 0, 0, 0));
2681: PetscFunctionReturn(PETSC_SUCCESS);
2682: }
2684: /*@
2685: DMPlexLabelsLoad - Loads labels into a `DMPLEX`
2687: Collective
2689: Input Parameters:
2690: + dm - The `DM` into which the labels are loaded
2691: . viewer - The `PetscViewer` for the saved labels
2692: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad()` when loading `dm` from viewer
2694: Level: advanced
2696: Note:
2697: The `PetscSF` argument must not be `NULL` if the `DM` is distributed, otherwise an error occurs.
2699: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexCoordinatesLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2700: `PetscSF`, `PetscViewer`
2701: @*/
2702: PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2703: {
2704: PetscBool ishdf5;
2706: PetscFunctionBegin;
2710: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2711: PetscCall(PetscLogEventBegin(DMPLEX_LabelsLoad, viewer, 0, 0, 0));
2712: if (ishdf5) {
2713: #if defined(PETSC_HAVE_HDF5)
2714: PetscViewerFormat format;
2716: PetscCall(PetscViewerGetFormat(viewer, &format));
2717: PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2718: PetscCall(DMPlexLabelsLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF));
2719: #else
2720: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2721: #endif
2722: }
2723: PetscCall(PetscLogEventEnd(DMPLEX_LabelsLoad, viewer, 0, 0, 0));
2724: PetscFunctionReturn(PETSC_SUCCESS);
2725: }
2727: /*@
2728: DMPlexSectionLoad - Loads section into a `DMPLEX`
2730: Collective
2732: Input Parameters:
2733: + dm - The `DM` that represents the topology
2734: . viewer - The `PetscViewer` that represents the on-disk section (sectionA)
2735: . sectiondm - The `DM` into which the on-disk section (sectionA) is migrated, can be `NULL`
2736: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad(`) when loading dm from viewer
2738: Output Parameters:
2739: + globalDofSF - The `PetscSF` 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)
2740: - localDofSF - The `PetscSF` 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)
2742: Level: advanced
2744: Notes:
2745: 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.
2747: 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 (or in case `sectiondm` is `NULL`) 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.
2749: 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.
2751: Example using 2 processes:
2752: .vb
2753: NX (number of points on dm): 4
2754: sectionA : the on-disk section
2755: vecA : a vector associated with sectionA
2756: sectionB : sectiondm's local section constructed in this function
2757: vecB (local) : a vector associated with sectiondm's local section
2758: vecB (global) : a vector associated with sectiondm's global section
2760: rank 0 rank 1
2761: vecA (global) : [.0 .4 .1 | .2 .3] <- to be loaded in DMPlexGlobalVectorLoad() or DMPlexLocalVectorLoad()
2762: sectionA->atlasOff : 0 2 | 1 <- loaded in PetscSectionLoad()
2763: sectionA->atlasDof : 1 3 | 1 <- loaded in PetscSectionLoad()
2764: sectionA's global point numbers: 0 2 | 3 <- loaded in DMPlexSectionLoad()
2765: [0, NX) : 0 1 | 2 3 <- conceptual partition used in globalToLocalPointSF
2766: sectionB's global point numbers: 0 1 3 | 3 2 <- associated with [0, NX) by globalToLocalPointSF
2767: sectionB->atlasDof : 1 0 1 | 1 3
2768: sectionB->atlasOff (no perm) : 0 1 1 | 0 1
2769: vecB (local) : [.0 .4] | [.4 .1 .2 .3] <- to be constructed by calling DMPlexLocalVectorLoad() with localDofSF
2770: vecB (global) : [.0 .4 | .1 .2 .3] <- to be constructed by calling DMPlexGlobalVectorLoad() with globalDofSF
2771: .ve
2772: where "|" represents a partition of loaded data, and global point 3 is assumed to be owned by rank 0.
2774: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexCoordinatesLoad()`, `DMPlexLabelsLoad()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`, `PetscSectionLoad()`, `DMPlexSectionView()`, `PetscSF`, `PetscViewer`
2775: @*/
2776: PetscErrorCode DMPlexSectionLoad(DM dm, PetscViewer viewer, PeOp DM sectiondm, PetscSF globalToLocalPointSF, PeOp PetscSF *globalDofSF, PeOp PetscSF *localDofSF)
2777: {
2778: PetscBool ishdf5;
2780: PetscFunctionBegin;
2783: if (!sectiondm) sectiondm = dm;
2786: if (globalDofSF) PetscAssertPointer(globalDofSF, 5);
2787: if (localDofSF) PetscAssertPointer(localDofSF, 6);
2788: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2789: PetscCall(PetscLogEventBegin(DMPLEX_SectionLoad, viewer, 0, 0, 0));
2790: if (ishdf5) {
2791: #if defined(PETSC_HAVE_HDF5)
2792: PetscCall(DMPlexSectionLoad_HDF5_Internal(dm, viewer, sectiondm, globalToLocalPointSF, globalDofSF, localDofSF));
2793: #else
2794: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2795: #endif
2796: }
2797: PetscCall(PetscLogEventEnd(DMPLEX_SectionLoad, viewer, 0, 0, 0));
2798: PetscFunctionReturn(PETSC_SUCCESS);
2799: }
2801: /*@
2802: DMPlexGlobalVectorLoad - Loads on-disk vector data into a global vector
2804: Collective
2806: Input Parameters:
2807: + dm - The `DM` that represents the topology
2808: . viewer - The `PetscViewer` that represents the on-disk vector data
2809: . sectiondm - The `DM` that contains the global section on which vec is defined, can be `NULL`
2810: . sf - The `PetscSF` that migrates the on-disk vector data into vec
2811: - vec - The global vector to set values of
2813: Level: advanced
2815: Notes:
2816: 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 (or in case `sectiondm` is `NULL`) 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.
2818: Calling sequence:
2819: .vb
2820: DMCreate(PETSC_COMM_WORLD, &dm);
2821: DMSetType(dm, DMPLEX);
2822: PetscObjectSetName((PetscObject)dm, "topologydm_name");
2823: DMPlexTopologyLoad(dm, viewer, &sfX);
2824: DMClone(dm, §iondm);
2825: PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2826: DMPlexSectionLoad(dm, viewer, sectiondm, sfX, &gsf, NULL);
2827: DMGetGlobalVector(sectiondm, &vec);
2828: PetscObjectSetName((PetscObject)vec, "vec_name");
2829: DMPlexGlobalVectorLoad(dm, viewer, sectiondm, gsf, vec);
2830: DMRestoreGlobalVector(sectiondm, &vec);
2831: PetscSFDestroy(&gsf);
2832: PetscSFDestroy(&sfX);
2833: DMDestroy(§iondm);
2834: DMDestroy(&dm);
2835: .ve
2837: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyLoad()`, `DMPlexSectionLoad()`, `DMPlexLocalVectorLoad()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`,
2838: `PetscSF`, `PetscViewer`
2839: @*/
2840: PetscErrorCode DMPlexGlobalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2841: {
2842: PetscBool ishdf5;
2844: PetscFunctionBegin;
2847: if (!sectiondm) sectiondm = dm;
2851: /* Check consistency */
2852: {
2853: PetscSection section;
2854: PetscBool includesConstraints;
2855: PetscInt m, m1;
2857: PetscCall(VecGetLocalSize(vec, &m1));
2858: PetscCall(DMGetGlobalSection(sectiondm, §ion));
2859: PetscCall(PetscSectionGetIncludesConstraints(section, &includesConstraints));
2860: if (includesConstraints) PetscCall(PetscSectionGetStorageSize(section, &m));
2861: else PetscCall(PetscSectionGetConstrainedStorageSize(section, &m));
2862: PetscCheck(m1 == m, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Global vector size (%" PetscInt_FMT ") != global section storage size (%" PetscInt_FMT ")", m1, m);
2863: }
2864: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2865: PetscCall(PetscLogEventBegin(DMPLEX_GlobalVectorLoad, viewer, 0, 0, 0));
2866: if (ishdf5) {
2867: #if defined(PETSC_HAVE_HDF5)
2868: PetscCall(DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec));
2869: #else
2870: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2871: #endif
2872: }
2873: PetscCall(PetscLogEventEnd(DMPLEX_GlobalVectorLoad, viewer, 0, 0, 0));
2874: PetscFunctionReturn(PETSC_SUCCESS);
2875: }
2877: /*@
2878: DMPlexLocalVectorLoad - Loads on-disk vector data into a local vector
2880: Collective
2882: Input Parameters:
2883: + dm - The `DM` that represents the topology
2884: . viewer - The `PetscViewer` that represents the on-disk vector data
2885: . sectiondm - The `DM` that contains the local section on which vec is defined, can be `NULL`
2886: . sf - The `PetscSF` that migrates the on-disk vector data into vec
2887: - vec - The local vector to set values of
2889: Level: advanced
2891: Notes:
2892: 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 (or in case `sectiondm` is `NULL`) 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.
2894: Calling sequence:
2895: .vb
2896: DMCreate(PETSC_COMM_WORLD, &dm);
2897: DMSetType(dm, DMPLEX);
2898: PetscObjectSetName((PetscObject)dm, "topologydm_name");
2899: DMPlexTopologyLoad(dm, viewer, &sfX);
2900: DMClone(dm, §iondm);
2901: PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2902: DMPlexSectionLoad(dm, viewer, sectiondm, sfX, NULL, &lsf);
2903: DMGetLocalVector(sectiondm, &vec);
2904: PetscObjectSetName((PetscObject)vec, "vec_name");
2905: DMPlexLocalVectorLoad(dm, viewer, sectiondm, lsf, vec);
2906: DMRestoreLocalVector(sectiondm, &vec);
2907: PetscSFDestroy(&lsf);
2908: PetscSFDestroy(&sfX);
2909: DMDestroy(§iondm);
2910: DMDestroy(&dm);
2911: .ve
2913: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyLoad()`, `DMPlexSectionLoad()`, `DMPlexGlobalVectorLoad()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`,
2914: `PetscSF`, `PetscViewer`
2915: @*/
2916: PetscErrorCode DMPlexLocalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2917: {
2918: PetscBool ishdf5;
2920: PetscFunctionBegin;
2923: if (!sectiondm) sectiondm = dm;
2927: /* Check consistency */
2928: {
2929: PetscSection section;
2930: PetscBool includesConstraints;
2931: PetscInt m, m1;
2933: PetscCall(VecGetLocalSize(vec, &m1));
2934: PetscCall(DMGetLocalSection(sectiondm, §ion));
2935: PetscCall(PetscSectionGetIncludesConstraints(section, &includesConstraints));
2936: if (includesConstraints) PetscCall(PetscSectionGetStorageSize(section, &m));
2937: else PetscCall(PetscSectionGetConstrainedStorageSize(section, &m));
2938: PetscCheck(m1 == m, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local vector size (%" PetscInt_FMT ") != local section storage size (%" PetscInt_FMT ")", m1, m);
2939: }
2940: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2941: PetscCall(PetscLogEventBegin(DMPLEX_LocalVectorLoad, viewer, 0, 0, 0));
2942: if (ishdf5) {
2943: #if defined(PETSC_HAVE_HDF5)
2944: PetscCall(DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec));
2945: #else
2946: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2947: #endif
2948: }
2949: PetscCall(PetscLogEventEnd(DMPLEX_LocalVectorLoad, viewer, 0, 0, 0));
2950: PetscFunctionReturn(PETSC_SUCCESS);
2951: }
2953: PetscErrorCode DMDestroy_Plex(DM dm)
2954: {
2955: DM_Plex *mesh = (DM_Plex *)dm->data;
2957: PetscFunctionBegin;
2958: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", NULL));
2959: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", NULL));
2960: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", NULL));
2961: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBounds_C", NULL));
2962: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", NULL));
2963: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", NULL));
2964: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", NULL));
2965: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", NULL));
2966: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", NULL));
2967: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "MatComputeNeumannOverlap_C", NULL));
2968: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", NULL));
2969: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", NULL));
2970: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetDefault_C", NULL));
2971: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetDefault_C", NULL));
2972: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetType_C", NULL));
2973: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetType_C", NULL));
2974: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", NULL));
2975: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", NULL));
2976: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", NULL));
2977: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", NULL));
2978: PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", NULL));
2979: if (--mesh->refct > 0) PetscFunctionReturn(PETSC_SUCCESS);
2980: PetscCall(PetscSectionDestroy(&mesh->coneSection));
2981: PetscCall(PetscFree(mesh->cones));
2982: PetscCall(PetscFree(mesh->coneOrientations));
2983: PetscCall(PetscSectionDestroy(&mesh->supportSection));
2984: PetscCall(PetscSectionDestroy(&mesh->subdomainSection));
2985: PetscCall(PetscFree(mesh->supports));
2986: PetscCall(PetscFree(mesh->cellTypes));
2987: PetscCall(DMPlexTransformDestroy(&mesh->tr));
2988: PetscCall(PetscFree(mesh->tetgenOpts));
2989: PetscCall(PetscFree(mesh->triangleOpts));
2990: PetscCall(PetscFree(mesh->transformType));
2991: PetscCall(PetscFree(mesh->distributionName));
2992: PetscCall(PetscPartitionerDestroy(&mesh->partitioner));
2993: PetscCall(DMLabelDestroy(&mesh->subpointMap));
2994: PetscCall(ISDestroy(&mesh->subpointIS));
2995: PetscCall(ISDestroy(&mesh->globalVertexNumbers));
2996: PetscCall(ISDestroy(&mesh->globalCellNumbers));
2997: if (mesh->periodic.face_sfs) {
2998: for (PetscInt i = 0; i < mesh->periodic.num_face_sfs; i++) PetscCall(PetscSFDestroy(&mesh->periodic.face_sfs[i]));
2999: PetscCall(PetscFree(mesh->periodic.face_sfs));
3000: }
3001: PetscCall(PetscSFDestroy(&mesh->periodic.composed_sf));
3002: if (mesh->periodic.periodic_points) {
3003: for (PetscInt i = 0; i < mesh->periodic.num_face_sfs; i++) PetscCall(ISDestroy(&mesh->periodic.periodic_points[i]));
3004: PetscCall(PetscFree(mesh->periodic.periodic_points));
3005: }
3006: PetscCall(PetscFree(mesh->periodic.transform));
3007: PetscCall(PetscSectionDestroy(&mesh->anchorSection));
3008: PetscCall(ISDestroy(&mesh->anchorIS));
3009: PetscCall(PetscSectionDestroy(&mesh->parentSection));
3010: PetscCall(PetscFree(mesh->parents));
3011: PetscCall(PetscFree(mesh->childIDs));
3012: PetscCall(PetscSectionDestroy(&mesh->childSection));
3013: PetscCall(PetscFree(mesh->children));
3014: PetscCall(DMDestroy(&mesh->referenceTree));
3015: PetscCall(PetscGridHashDestroy(&mesh->lbox));
3016: PetscCall(PetscFree(mesh->neighbors));
3017: PetscCall(PetscFree(mesh->metricCtx));
3018: if (mesh->nonempty_comm != MPI_COMM_NULL && mesh->nonempty_comm != MPI_COMM_SELF) PetscCallMPI(MPI_Comm_free(&mesh->nonempty_comm));
3019: PetscCall(DMPlexTransformDestroy(&mesh->transform));
3020: /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
3021: PetscCall(PetscFree(mesh));
3022: PetscFunctionReturn(PETSC_SUCCESS);
3023: }
3025: PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
3026: {
3027: PetscSection sectionGlobal, sectionLocal;
3028: PetscInt bs = -1, mbs;
3029: PetscInt localSize, localStart = 0;
3030: PetscBool isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
3031: MatType mtype;
3032: ISLocalToGlobalMapping ltog;
3034: PetscFunctionBegin;
3035: PetscCall(MatInitializePackage());
3036: mtype = dm->mattype;
3037: PetscCall(DMGetLocalSection(dm, §ionLocal));
3038: PetscCall(DMGetGlobalSection(dm, §ionGlobal));
3039: /* PetscCall(PetscSectionGetStorageSize(sectionGlobal, &localSize)); */
3040: PetscCall(PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize));
3041: PetscCallMPI(MPI_Exscan(&localSize, &localStart, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)dm)));
3042: PetscCall(MatCreate(PetscObjectComm((PetscObject)dm), J));
3043: PetscCall(MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE));
3044: PetscCall(MatSetType(*J, mtype));
3045: PetscCall(MatSetFromOptions(*J));
3046: PetscCall(MatGetBlockSize(*J, &mbs));
3047: if (mbs > 1) bs = mbs;
3048: PetscCall(PetscStrcmp(mtype, MATSHELL, &isShell));
3049: PetscCall(PetscStrcmp(mtype, MATBAIJ, &isBlock));
3050: PetscCall(PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock));
3051: PetscCall(PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock));
3052: PetscCall(PetscStrcmp(mtype, MATSBAIJ, &isSymBlock));
3053: PetscCall(PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock));
3054: PetscCall(PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock));
3055: PetscCall(PetscStrcmp(mtype, MATIS, &isMatIS));
3056: if (!isShell) {
3057: // There are three states with pblocks, since block starts can have no dofs:
3058: // UNKNOWN) New Block: An open block has been signalled by pblocks[p] == 1
3059: // TRUE) Block Start: The first entry in a block has been added
3060: // FALSE) Block Add: An additional block entry has been added, since pblocks[p] == 0
3061: PetscBT blst;
3062: PetscBool3 bstate = PETSC_BOOL3_UNKNOWN;
3063: PetscBool fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
3064: const PetscInt *perm = NULL;
3065: PetscInt *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *pblocks;
3066: PetscInt pStart, pEnd, dof, cdof, num_fields;
3068: PetscCall(DMGetLocalToGlobalMapping(dm, <og));
3069: PetscCall(PetscSectionGetBlockStarts(sectionLocal, &blst));
3070: if (sectionLocal->perm) PetscCall(ISGetIndices(sectionLocal->perm, &perm));
3072: PetscCall(PetscCalloc1(localSize, &pblocks));
3073: PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
3074: PetscCall(PetscSectionGetNumFields(sectionGlobal, &num_fields));
3075: // We need to process in the permuted order to get block sizes right
3076: for (PetscInt point = pStart; point < pEnd; ++point) {
3077: const PetscInt p = perm ? perm[point] : point;
3079: switch (dm->blocking_type) {
3080: case DM_BLOCKING_TOPOLOGICAL_POINT: { // One block per topological point
3081: PetscInt bdof, offset;
3083: PetscCall(PetscSectionGetDof(sectionGlobal, p, &dof));
3084: PetscCall(PetscSectionGetOffset(sectionGlobal, p, &offset));
3085: PetscCall(PetscSectionGetConstraintDof(sectionGlobal, p, &cdof));
3086: if (blst && PetscBTLookup(blst, p)) bstate = PETSC_BOOL3_UNKNOWN;
3087: if (dof > 0) {
3088: // State change
3089: if (bstate == PETSC_BOOL3_UNKNOWN) bstate = PETSC_BOOL3_TRUE;
3090: else if (bstate == PETSC_BOOL3_TRUE && blst && !PetscBTLookup(blst, p)) bstate = PETSC_BOOL3_FALSE;
3092: for (PetscInt i = 0; i < dof - cdof; ++i) pblocks[offset - localStart + i] = dof - cdof;
3093: // Signal block concatenation
3094: if (bstate == PETSC_BOOL3_FALSE && dof - cdof) pblocks[offset - localStart] = -(dof - cdof);
3095: }
3096: dof = dof < 0 ? -(dof + 1) : dof;
3097: bdof = cdof && (dof - cdof) ? 1 : dof;
3098: if (dof) {
3099: if (bs < 0) {
3100: bs = bdof;
3101: } else if (bs != bdof) {
3102: bs = 1;
3103: }
3104: }
3105: } break;
3106: case DM_BLOCKING_FIELD_NODE: {
3107: for (PetscInt field = 0; field < num_fields; field++) {
3108: PetscInt num_comp, bdof, offset;
3109: PetscCall(PetscSectionGetFieldComponents(sectionGlobal, field, &num_comp));
3110: PetscCall(PetscSectionGetFieldDof(sectionGlobal, p, field, &dof));
3111: if (dof < 0) continue;
3112: PetscCall(PetscSectionGetFieldOffset(sectionGlobal, p, field, &offset));
3113: PetscCall(PetscSectionGetFieldConstraintDof(sectionGlobal, p, field, &cdof));
3114: PetscAssert(dof % num_comp == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Point %" PetscInt_FMT " field %" PetscInt_FMT " has %" PetscInt_FMT " dof, not divisible by %" PetscInt_FMT " component ", p, field, dof, num_comp);
3115: PetscInt num_nodes = dof / num_comp;
3116: for (PetscInt i = 0; i < dof - cdof; i++) pblocks[offset - localStart + i] = (dof - cdof) / num_nodes;
3117: // Handle possibly constant block size (unlikely)
3118: bdof = cdof && (dof - cdof) ? 1 : dof;
3119: if (dof) {
3120: if (bs < 0) {
3121: bs = bdof;
3122: } else if (bs != bdof) {
3123: bs = 1;
3124: }
3125: }
3126: }
3127: } break;
3128: }
3129: }
3130: if (sectionLocal->perm) PetscCall(ISRestoreIndices(sectionLocal->perm, &perm));
3131: /* Must have same blocksize on all procs (some might have no points) */
3132: bsLocal[0] = bs < 0 ? PETSC_INT_MAX : bs;
3133: bsLocal[1] = bs;
3134: PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
3135: if (bsMinMax[0] != bsMinMax[1]) bs = 1;
3136: else bs = bsMinMax[0];
3137: bs = PetscMax(1, bs);
3138: PetscCall(MatSetLocalToGlobalMapping(*J, ltog, ltog));
3139: if (dm->prealloc_skip) { // User will likely use MatSetPreallocationCOO(), but still set structural parameters
3140: PetscCall(MatSetBlockSize(*J, bs));
3141: PetscCall(MatSetUp(*J));
3142: } else {
3143: PetscCall(PetscCalloc4(localSize / bs, &dnz, localSize / bs, &onz, localSize / bs, &dnzu, localSize / bs, &onzu));
3144: PetscCall(DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix));
3145: PetscCall(PetscFree4(dnz, onz, dnzu, onzu));
3146: }
3147: if (pblocks) { // Consolidate blocks
3148: PetscInt nblocks = 0;
3149: pblocks[0] = PetscAbs(pblocks[0]);
3150: for (PetscInt i = 0; i < localSize; i += PetscMax(1, pblocks[i])) {
3151: if (pblocks[i] == 0) continue;
3152: // Negative block size indicates the blocks should be concatenated
3153: if (pblocks[i] < 0) {
3154: pblocks[i] = -pblocks[i];
3155: pblocks[nblocks - 1] += pblocks[i];
3156: } else {
3157: pblocks[nblocks++] = pblocks[i]; // nblocks always <= i
3158: }
3159: for (PetscInt j = 1; j < pblocks[i]; j++)
3160: PetscCheck(pblocks[i + j] == pblocks[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Block of size %" PetscInt_FMT " at %" PetscInt_FMT " mismatches entry %" PetscInt_FMT " at %" PetscInt_FMT, pblocks[i], i, pblocks[i + j], i + j);
3161: }
3162: PetscCall(MatSetVariableBlockSizes(*J, nblocks, pblocks));
3163: }
3164: PetscCall(PetscFree(pblocks));
3165: }
3166: PetscCall(MatSetDM(*J, dm));
3167: PetscFunctionReturn(PETSC_SUCCESS);
3168: }
3170: /*@
3171: DMPlexGetSubdomainSection - Returns the section associated with the subdomain
3173: Not Collective
3175: Input Parameter:
3176: . dm - The `DMPLEX`
3178: Output Parameter:
3179: . subsection - The subdomain section
3181: Level: developer
3183: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscSection`
3184: @*/
3185: PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
3186: {
3187: DM_Plex *mesh = (DM_Plex *)dm->data;
3189: PetscFunctionBegin;
3191: if (!mesh->subdomainSection) {
3192: PetscSection section;
3193: PetscSF sf;
3195: PetscCall(PetscSFCreate(PETSC_COMM_SELF, &sf));
3196: PetscCall(DMGetLocalSection(dm, §ion));
3197: PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, PETSC_TRUE, &mesh->subdomainSection));
3198: PetscCall(PetscSFDestroy(&sf));
3199: }
3200: *subsection = mesh->subdomainSection;
3201: PetscFunctionReturn(PETSC_SUCCESS);
3202: }
3204: /*@
3205: DMPlexGetChart - Return the interval for all mesh points [`pStart`, `pEnd`)
3207: Not Collective
3209: Input Parameter:
3210: . dm - The `DMPLEX`
3212: Output Parameters:
3213: + pStart - The first mesh point
3214: - pEnd - The upper bound for mesh points
3216: Level: beginner
3218: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetChart()`
3219: @*/
3220: PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
3221: {
3222: DM_Plex *mesh = (DM_Plex *)dm->data;
3224: PetscFunctionBegin;
3226: if (mesh->tr) PetscCall(DMPlexTransformGetChart(mesh->tr, pStart, pEnd));
3227: else PetscCall(PetscSectionGetChart(mesh->coneSection, pStart, pEnd));
3228: PetscFunctionReturn(PETSC_SUCCESS);
3229: }
3231: /*@
3232: DMPlexSetChart - Set the interval for all mesh points [`pStart`, `pEnd`)
3234: Not Collective
3236: Input Parameters:
3237: + dm - The `DMPLEX`
3238: . pStart - The first mesh point
3239: - pEnd - The upper bound for mesh points
3241: Level: beginner
3243: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetChart()`
3244: @*/
3245: PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
3246: {
3247: DM_Plex *mesh = (DM_Plex *)dm->data;
3249: PetscFunctionBegin;
3251: PetscCall(PetscSectionSetChart(mesh->coneSection, pStart, pEnd));
3252: PetscCall(PetscSectionSetChart(mesh->supportSection, pStart, pEnd));
3253: PetscCall(PetscFree(mesh->cellTypes));
3254: PetscFunctionReturn(PETSC_SUCCESS);
3255: }
3257: /*@
3258: DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
3260: Not Collective
3262: Input Parameters:
3263: + dm - The `DMPLEX`
3264: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
3266: Output Parameter:
3267: . size - The cone size for point `p`
3269: Level: beginner
3271: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`
3272: @*/
3273: PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
3274: {
3275: DM_Plex *mesh = (DM_Plex *)dm->data;
3277: PetscFunctionBegin;
3279: PetscAssertPointer(size, 3);
3280: if (mesh->tr) PetscCall(DMPlexTransformGetConeSize(mesh->tr, p, size));
3281: else PetscCall(PetscSectionGetDof(mesh->coneSection, p, size));
3282: PetscFunctionReturn(PETSC_SUCCESS);
3283: }
3285: /*@
3286: DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
3288: Not Collective
3290: Input Parameters:
3291: + dm - The `DMPLEX`
3292: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3293: - size - The cone size for point `p`
3295: Level: beginner
3297: Note:
3298: This should be called after `DMPlexSetChart()`.
3300: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetCone()`, `DMPlexCreate()`, `DMPlexGetConeSize()`, `DMPlexSetChart()`
3301: @*/
3302: PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
3303: {
3304: DM_Plex *mesh = (DM_Plex *)dm->data;
3306: PetscFunctionBegin;
3308: PetscCheck(!mesh->tr, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cannot call DMPlexSetConeSize() on a mesh with a transform defined.");
3309: PetscCall(PetscSectionSetDof(mesh->coneSection, p, size));
3310: PetscFunctionReturn(PETSC_SUCCESS);
3311: }
3313: /*@C
3314: DMPlexGetCone - Return the points on the in-edges for this point in the DAG
3316: Not Collective
3318: Input Parameters:
3319: + dm - The `DMPLEX`
3320: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
3322: Output Parameter:
3323: . cone - An array of points which are on the in-edges for point `p`, the length of `cone` is the result of `DMPlexGetConeSize()`
3325: Level: beginner
3327: Fortran Notes:
3328: `cone` must be declared with
3329: .vb
3330: PetscInt, pointer :: cone(:)
3331: .ve
3333: You must call `DMPlexRestoreCone()` after you finish using the array.
3334: `DMPlexRestoreCone()` is not needed/available in C.
3336: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSize()`, `DMPlexSetCone()`, `DMPlexGetConeTuple()`, `DMPlexSetChart()`, `DMPlexRestoreCone()`
3337: @*/
3338: PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
3339: {
3340: DM_Plex *mesh = (DM_Plex *)dm->data;
3341: PetscInt off;
3343: PetscFunctionBegin;
3345: PetscAssertPointer(cone, 3);
3346: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3347: *cone = PetscSafePointerPlusOffset(mesh->cones, off);
3348: PetscFunctionReturn(PETSC_SUCCESS);
3349: }
3351: /*@
3352: DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
3354: Not Collective
3356: Input Parameters:
3357: + dm - The `DMPLEX`
3358: - p - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`
3360: Output Parameters:
3361: + pConesSection - `PetscSection` describing the layout of `pCones`
3362: - pCones - An `IS` containing the points which are on the in-edges for the point set `p`
3364: Level: intermediate
3366: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeRecursive()`, `DMPlexSetChart()`, `PetscSection`, `IS`
3367: @*/
3368: PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PeOp PetscSection *pConesSection, PeOp IS *pCones)
3369: {
3370: PetscSection cs, newcs;
3371: PetscInt *cones;
3372: PetscInt *newarr = NULL;
3373: PetscInt n;
3375: PetscFunctionBegin;
3376: PetscCall(DMPlexGetCones(dm, &cones));
3377: PetscCall(DMPlexGetConeSection(dm, &cs));
3378: PetscCall(PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void **)&newarr) : NULL));
3379: if (pConesSection) *pConesSection = newcs;
3380: if (pCones) {
3381: PetscCall(PetscSectionGetStorageSize(newcs, &n));
3382: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones));
3383: }
3384: PetscFunctionReturn(PETSC_SUCCESS);
3385: }
3387: /*@
3388: DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
3390: Not Collective
3392: Input Parameters:
3393: + dm - The `DMPLEX`
3394: - points - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`
3396: Output Parameter:
3397: . expandedPoints - An `IS` containing the of vertices recursively expanded from input points
3399: Level: advanced
3401: Notes:
3402: Like `DMPlexGetConeRecursive()` but returns only the 0-depth `IS` (i.e. vertices only) and no sections.
3404: There is no corresponding Restore function, just call `ISDestroy()` on the returned `IS` to deallocate.
3406: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexGetConeRecursive()`, `DMPlexRestoreConeRecursive()`,
3407: `DMPlexGetDepth()`, `IS`
3408: @*/
3409: PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
3410: {
3411: IS *expandedPointsAll;
3412: PetscInt depth;
3414: PetscFunctionBegin;
3417: PetscAssertPointer(expandedPoints, 3);
3418: PetscCall(DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL));
3419: *expandedPoints = expandedPointsAll[0];
3420: PetscCall(PetscObjectReference((PetscObject)expandedPointsAll[0]));
3421: PetscCall(DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL));
3422: PetscFunctionReturn(PETSC_SUCCESS);
3423: }
3425: /*@
3426: DMPlexGetConeRecursive - Expand each given point into its cone points and do that recursively until we end up just with vertices
3427: (DAG points of depth 0, i.e., without cones).
3429: Not Collective
3431: Input Parameters:
3432: + dm - The `DMPLEX`
3433: - points - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`
3435: Output Parameters:
3436: + depth - (optional) Size of the output arrays, equal to `DMPLEX` depth, returned by `DMPlexGetDepth()`
3437: . expandedPoints - (optional) An array of index sets with recursively expanded cones
3438: - sections - (optional) An array of sections which describe mappings from points to their cone points
3440: Level: advanced
3442: Notes:
3443: Like `DMPlexGetConeTuple()` but recursive.
3445: 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.
3446: For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
3448: 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\:
3449: (1) DAG points in `expandedPoints`[d+1] with `depth` d+1 to their cone points in `expandedPoints`[d];
3450: (2) DAG points in `expandedPoints`[d+1] with `depth` in [0,d] to the same points in `expandedPoints`[d].
3452: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexRestoreConeRecursive()`, `DMPlexGetConeRecursiveVertices()`,
3453: `DMPlexGetDepth()`, `PetscSection`, `IS`
3454: @*/
3455: PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PeOp PetscInt *depth, PeOp IS *expandedPoints[], PeOp PetscSection *sections[])
3456: {
3457: const PetscInt *arr0 = NULL, *cone = NULL;
3458: PetscInt *arr = NULL, *newarr = NULL;
3459: PetscInt d, depth_, i, n, newn, cn, co, start, end;
3460: IS *expandedPoints_;
3461: PetscSection *sections_;
3463: PetscFunctionBegin;
3466: if (depth) PetscAssertPointer(depth, 3);
3467: if (expandedPoints) PetscAssertPointer(expandedPoints, 4);
3468: if (sections) PetscAssertPointer(sections, 5);
3469: PetscCall(ISGetLocalSize(points, &n));
3470: PetscCall(ISGetIndices(points, &arr0));
3471: PetscCall(DMPlexGetDepth(dm, &depth_));
3472: PetscCall(PetscCalloc1(depth_, &expandedPoints_));
3473: PetscCall(PetscCalloc1(depth_, §ions_));
3474: arr = (PetscInt *)arr0; /* this is ok because first generation of arr is not modified */
3475: for (d = depth_ - 1; d >= 0; d--) {
3476: PetscCall(PetscSectionCreate(PETSC_COMM_SELF, §ions_[d]));
3477: PetscCall(PetscSectionSetChart(sections_[d], 0, n));
3478: for (i = 0; i < n; i++) {
3479: PetscCall(DMPlexGetDepthStratum(dm, d + 1, &start, &end));
3480: if (arr[i] >= start && arr[i] < end) {
3481: PetscCall(DMPlexGetConeSize(dm, arr[i], &cn));
3482: PetscCall(PetscSectionSetDof(sections_[d], i, cn));
3483: } else {
3484: PetscCall(PetscSectionSetDof(sections_[d], i, 1));
3485: }
3486: }
3487: PetscCall(PetscSectionSetUp(sections_[d]));
3488: PetscCall(PetscSectionGetStorageSize(sections_[d], &newn));
3489: PetscCall(PetscMalloc1(newn, &newarr));
3490: for (i = 0; i < n; i++) {
3491: PetscCall(PetscSectionGetDof(sections_[d], i, &cn));
3492: PetscCall(PetscSectionGetOffset(sections_[d], i, &co));
3493: if (cn > 1) {
3494: PetscCall(DMPlexGetCone(dm, arr[i], &cone));
3495: PetscCall(PetscArraycpy(&newarr[co], cone, cn));
3496: } else {
3497: newarr[co] = arr[i];
3498: }
3499: }
3500: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]));
3501: arr = newarr;
3502: n = newn;
3503: }
3504: PetscCall(ISRestoreIndices(points, &arr0));
3505: *depth = depth_;
3506: if (expandedPoints) *expandedPoints = expandedPoints_;
3507: else {
3508: for (d = 0; d < depth_; d++) PetscCall(ISDestroy(&expandedPoints_[d]));
3509: PetscCall(PetscFree(expandedPoints_));
3510: }
3511: if (sections) *sections = sections_;
3512: else {
3513: for (d = 0; d < depth_; d++) PetscCall(PetscSectionDestroy(§ions_[d]));
3514: PetscCall(PetscFree(sections_));
3515: }
3516: PetscFunctionReturn(PETSC_SUCCESS);
3517: }
3519: /*@
3520: DMPlexRestoreConeRecursive - Deallocates arrays created by `DMPlexGetConeRecursive()`
3522: Not Collective
3524: Input Parameters:
3525: + dm - The `DMPLEX`
3526: - points - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`
3528: Output Parameters:
3529: + depth - (optional) Size of the output arrays, equal to `DMPLEX` depth, returned by `DMPlexGetDepth()`
3530: . expandedPoints - (optional) An array of recursively expanded cones
3531: - sections - (optional) An array of sections which describe mappings from points to their cone points
3533: Level: advanced
3535: Note:
3536: See `DMPlexGetConeRecursive()`
3538: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexGetConeRecursive()`, `DMPlexGetConeRecursiveVertices()`,
3539: `DMPlexGetDepth()`, `IS`, `PetscSection`
3540: @*/
3541: PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PeOp PetscInt *depth, PeOp IS *expandedPoints[], PeOp PetscSection *sections[])
3542: {
3543: PetscInt d, depth_;
3545: PetscFunctionBegin;
3546: PetscCall(DMPlexGetDepth(dm, &depth_));
3547: PetscCheck(!depth || *depth == depth_, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
3548: if (depth) *depth = 0;
3549: if (expandedPoints) {
3550: for (d = 0; d < depth_; d++) PetscCall(ISDestroy(&(*expandedPoints)[d]));
3551: PetscCall(PetscFree(*expandedPoints));
3552: }
3553: if (sections) {
3554: for (d = 0; d < depth_; d++) PetscCall(PetscSectionDestroy(&(*sections)[d]));
3555: PetscCall(PetscFree(*sections));
3556: }
3557: PetscFunctionReturn(PETSC_SUCCESS);
3558: }
3560: /*@
3561: 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
3563: Not Collective
3565: Input Parameters:
3566: + dm - The `DMPLEX`
3567: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3568: - cone - An array of points which are on the in-edges for point `p`, its length must have been previously provided with `DMPlexSetConeSize()`
3570: Level: beginner
3572: Note:
3573: This should be called after all calls to `DMPlexSetConeSize()` and `DMSetUp()`.
3575: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`, `DMPlexSetSupport()`, `DMPlexSetSupportSize()`
3576: @*/
3577: PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
3578: {
3579: DM_Plex *mesh = (DM_Plex *)dm->data;
3580: PetscInt dof, off, c;
3582: PetscFunctionBegin;
3584: PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3585: if (dof) PetscAssertPointer(cone, 3);
3586: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3587: if (PetscDefined(USE_DEBUG)) {
3588: PetscInt pStart, pEnd;
3589: PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
3590: PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3591: for (c = 0; c < dof; ++c) {
3592: PetscCheck(!(cone[c] < pStart) && !(cone[c] >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", cone[c], pStart, pEnd);
3593: mesh->cones[off + c] = cone[c];
3594: }
3595: } else {
3596: for (c = 0; c < dof; ++c) mesh->cones[off + c] = cone[c];
3597: }
3598: PetscFunctionReturn(PETSC_SUCCESS);
3599: }
3601: /*@C
3602: DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
3604: Not Collective
3606: Input Parameters:
3607: + dm - The `DMPLEX`
3608: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
3610: Output Parameter:
3611: . coneOrientation - An array of orientations which are on the in-edges for point `p`. An orientation is an
3612: integer giving the prescription for cone traversal. Its length is given by the result of `DMPlexSetConeSize()`
3614: Level: beginner
3616: Note:
3617: The number indexes the symmetry transformations for the cell type (see manual). Orientation 0 is always
3618: the identity transformation. Negative orientation indicates reflection so that -(o+1) is the reflection
3619: of o, however it is not necessarily the inverse. To get the inverse, use `DMPolytopeTypeComposeOrientationInv()`
3620: with the identity.
3622: Fortran Notes:
3623: You must call `DMPlexRestoreConeOrientation()` after you finish using the returned array.
3624: `DMPlexRestoreConeOrientation()` is not needed/available in C.
3626: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetConeSize()`, `DMPolytopeTypeComposeOrientation()`, `DMPolytopeTypeComposeOrientationInv()`,
3627: `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetCone()`, `DMPlexSetChart()`
3628: @*/
3629: PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
3630: {
3631: DM_Plex *mesh = (DM_Plex *)dm->data;
3632: PetscInt off;
3634: PetscFunctionBegin;
3636: if (PetscDefined(USE_DEBUG)) {
3637: PetscInt dof;
3638: PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3639: if (dof) PetscAssertPointer(coneOrientation, 3);
3640: }
3641: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3643: *coneOrientation = &mesh->coneOrientations[off];
3644: PetscFunctionReturn(PETSC_SUCCESS);
3645: }
3647: /*@
3648: DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
3650: Not Collective
3652: Input Parameters:
3653: + dm - The `DMPLEX`
3654: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3655: - coneOrientation - An array of orientations. Its length is given by the result of `DMPlexSetConeSize()`
3657: Level: beginner
3659: Notes:
3660: This should be called after all calls to `DMPlexSetConeSize()` and `DMSetUp()`.
3662: The meaning of coneOrientation is detailed in `DMPlexGetConeOrientation()`.
3664: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetConeOrientation()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3665: @*/
3666: PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
3667: {
3668: DM_Plex *mesh = (DM_Plex *)dm->data;
3669: PetscInt pStart, pEnd;
3670: PetscInt dof, off, c;
3672: PetscFunctionBegin;
3674: PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3675: if (dof) PetscAssertPointer(coneOrientation, 3);
3676: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3677: if (PetscDefined(USE_DEBUG)) {
3678: PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
3679: PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3680: for (c = 0; c < dof; ++c) {
3681: PetscInt cdof, o = coneOrientation[c];
3683: PetscCall(PetscSectionGetDof(mesh->coneSection, mesh->cones[off + c], &cdof));
3684: PetscCheck(!o || (o >= -(cdof + 1) && o < cdof), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone orientation %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ". %" PetscInt_FMT ")", o, -(cdof + 1), cdof);
3685: mesh->coneOrientations[off + c] = o;
3686: }
3687: } else {
3688: for (c = 0; c < dof; ++c) mesh->coneOrientations[off + c] = coneOrientation[c];
3689: }
3690: PetscFunctionReturn(PETSC_SUCCESS);
3691: }
3693: /*@
3694: DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
3696: Not Collective
3698: Input Parameters:
3699: + dm - The `DMPLEX`
3700: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3701: . conePos - The local index in the cone where the point should be put
3702: - conePoint - The mesh point to insert
3704: Level: beginner
3706: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3707: @*/
3708: PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
3709: {
3710: DM_Plex *mesh = (DM_Plex *)dm->data;
3711: PetscInt pStart, pEnd;
3712: PetscInt dof, off;
3714: PetscFunctionBegin;
3716: if (PetscDefined(USE_DEBUG)) {
3717: PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
3718: PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3719: PetscCheck(!(conePoint < pStart) && !(conePoint >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", conePoint, pStart, pEnd);
3720: PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3721: PetscCheck(!(conePos < 0) && !(conePos >= dof), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %" PetscInt_FMT " of point %" PetscInt_FMT " is not in the valid range [0, %" PetscInt_FMT ")", conePos, p, dof);
3722: }
3723: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3724: mesh->cones[off + conePos] = conePoint;
3725: PetscFunctionReturn(PETSC_SUCCESS);
3726: }
3728: /*@
3729: DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
3731: Not Collective
3733: Input Parameters:
3734: + dm - The `DMPLEX`
3735: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3736: . conePos - The local index in the cone where the point should be put
3737: - coneOrientation - The point orientation to insert
3739: Level: beginner
3741: Note:
3742: The meaning of coneOrientation values is detailed in `DMPlexGetConeOrientation()`.
3744: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3745: @*/
3746: PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
3747: {
3748: DM_Plex *mesh = (DM_Plex *)dm->data;
3749: PetscInt pStart, pEnd;
3750: PetscInt dof, off;
3752: PetscFunctionBegin;
3754: if (PetscDefined(USE_DEBUG)) {
3755: PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
3756: PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3757: PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3758: PetscCheck(!(conePos < 0) && !(conePos >= dof), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %" PetscInt_FMT " of point %" PetscInt_FMT " is not in the valid range [0, %" PetscInt_FMT ")", conePos, p, dof);
3759: }
3760: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3761: mesh->coneOrientations[off + conePos] = coneOrientation;
3762: PetscFunctionReturn(PETSC_SUCCESS);
3763: }
3765: /*@C
3766: DMPlexGetOrientedCone - Return the points and orientations on the in-edges for this point in the DAG
3768: Not collective
3770: Input Parameters:
3771: + dm - The DMPlex
3772: - p - The point, which must lie in the chart set with DMPlexSetChart()
3774: Output Parameters:
3775: + cone - An array of points which are on the in-edges for point `p`
3776: - ornt - An array of orientations which are on the in-edges for point `p`. An orientation is an
3777: integer giving the prescription for cone traversal.
3779: Level: beginner
3781: Notes:
3782: The number indexes the symmetry transformations for the cell type (see manual). Orientation 0 is always
3783: the identity transformation. Negative orientation indicates reflection so that -(o+1) is the reflection
3784: of o, however it is not necessarily the inverse. To get the inverse, use `DMPolytopeTypeComposeOrientationInv()`
3785: with the identity.
3787: You must also call `DMPlexRestoreOrientedCone()` after you finish using the returned array.
3789: Fortran Notes:
3790: `cone` and `ornt` must be declared with
3791: .vb
3792: PetscInt, pointer :: cone(:)
3793: PetscInt, pointer :: ornt(:)
3794: .ve
3796: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreOrientedCone()`, `DMPlexGetConeSize()`, `DMPlexGetCone()`, `DMPlexGetChart()`
3797: @*/
3798: PetscErrorCode DMPlexGetOrientedCone(DM dm, PetscInt p, PeOp const PetscInt *cone[], PeOp const PetscInt *ornt[])
3799: {
3800: DM_Plex *mesh = (DM_Plex *)dm->data;
3802: PetscFunctionBegin;
3804: if (mesh->tr) {
3805: PetscCall(DMPlexTransformGetCone(mesh->tr, p, cone, ornt));
3806: } else {
3807: PetscInt off;
3808: if (PetscDefined(USE_DEBUG)) {
3809: PetscInt dof;
3810: PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3811: if (dof) {
3812: if (cone) PetscAssertPointer(cone, 3);
3813: if (ornt) PetscAssertPointer(ornt, 4);
3814: }
3815: }
3816: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3817: if (cone) *cone = PetscSafePointerPlusOffset(mesh->cones, off);
3818: if (ornt) *ornt = PetscSafePointerPlusOffset(mesh->coneOrientations, off);
3819: }
3820: PetscFunctionReturn(PETSC_SUCCESS);
3821: }
3823: /*@C
3824: DMPlexRestoreOrientedCone - Restore the points and orientations on the in-edges for this point in the DAG obtained with `DMPlexGetOrientedCone()`
3826: Not Collective
3828: Input Parameters:
3829: + dm - The DMPlex
3830: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3831: . cone - An array of points which are on the in-edges for point p
3832: - ornt - An array of orientations which are on the in-edges for point `p`. An orientation is an
3833: integer giving the prescription for cone traversal.
3835: Level: beginner
3837: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetOrientedCone()`, `DMPlexGetConeSize()`, `DMPlexGetCone()`, `DMPlexGetChart()`
3838: @*/
3839: PetscErrorCode DMPlexRestoreOrientedCone(DM dm, PetscInt p, const PetscInt *cone[], const PetscInt *ornt[])
3840: {
3841: DM_Plex *mesh = (DM_Plex *)dm->data;
3843: PetscFunctionBegin;
3845: if (mesh->tr) PetscCall(DMPlexTransformRestoreCone(mesh->tr, p, cone, ornt));
3846: PetscFunctionReturn(PETSC_SUCCESS);
3847: }
3849: /*@
3850: DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
3852: Not Collective
3854: Input Parameters:
3855: + dm - The `DMPLEX`
3856: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
3858: Output Parameter:
3859: . size - The support size for point `p`
3861: Level: beginner
3863: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`, `DMPlexGetConeSize()`
3864: @*/
3865: PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
3866: {
3867: DM_Plex *mesh = (DM_Plex *)dm->data;
3869: PetscFunctionBegin;
3871: PetscAssertPointer(size, 3);
3872: PetscCall(PetscSectionGetDof(mesh->supportSection, p, size));
3873: PetscFunctionReturn(PETSC_SUCCESS);
3874: }
3876: /*@
3877: DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
3879: Not Collective
3881: Input Parameters:
3882: + dm - The `DMPLEX`
3883: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3884: - size - The support size for point `p`
3886: Level: beginner
3888: Note:
3889: This should be called after `DMPlexSetChart()`.
3891: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetSupportSize()`, `DMPlexSetChart()`
3892: @*/
3893: PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
3894: {
3895: DM_Plex *mesh = (DM_Plex *)dm->data;
3897: PetscFunctionBegin;
3899: PetscCall(PetscSectionSetDof(mesh->supportSection, p, size));
3900: PetscFunctionReturn(PETSC_SUCCESS);
3901: }
3903: /*@C
3904: DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
3906: Not Collective
3908: Input Parameters:
3909: + dm - The `DMPLEX`
3910: - p - The point, which must lie in the chart set with `DMPlexSetChart()`
3912: Output Parameter:
3913: . support - An array of points which are on the out-edges for point `p`, its length is that obtained from `DMPlexGetSupportSize()`
3915: Level: beginner
3917: Fortran Notes:
3918: `support` must be declared with
3919: .vb
3920: PetscInt, pointer :: support(:)
3921: .ve
3923: You must also call `DMPlexRestoreSupport()` after you finish using the returned array.
3924: `DMPlexRestoreSupport()` is not needed/available in C.
3926: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSupportSize()`, `DMPlexSetSupport()`, `DMPlexGetCone()`, `DMPlexSetChart()`
3927: @*/
3928: PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
3929: {
3930: DM_Plex *mesh = (DM_Plex *)dm->data;
3931: PetscInt off;
3933: PetscFunctionBegin;
3935: PetscAssertPointer(support, 3);
3936: PetscCall(PetscSectionGetOffset(mesh->supportSection, p, &off));
3937: *support = PetscSafePointerPlusOffset(mesh->supports, off);
3938: PetscFunctionReturn(PETSC_SUCCESS);
3939: }
3941: /*@
3942: DMPlexSetSupport - Set the points on the out-edges for this point in the DAG, that is the list of points that this point covers
3944: Not Collective
3946: Input Parameters:
3947: + dm - The `DMPLEX`
3948: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3949: - support - An array of points which are on the out-edges for point `p`, its length is that obtained from `DMPlexGetSupportSize()`
3951: Level: beginner
3953: Note:
3954: This should be called after all calls to `DMPlexSetSupportSize()` and `DMSetUp()`.
3956: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetCone()`, `DMPlexSetConeSize()`, `DMPlexCreate()`, `DMPlexGetSupport()`, `DMPlexSetChart()`, `DMPlexSetSupportSize()`, `DMSetUp()`
3957: @*/
3958: PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
3959: {
3960: DM_Plex *mesh = (DM_Plex *)dm->data;
3961: PetscInt pStart, pEnd;
3962: PetscInt dof, off, c;
3964: PetscFunctionBegin;
3966: PetscCall(PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd));
3967: PetscCall(PetscSectionGetDof(mesh->supportSection, p, &dof));
3968: if (dof) PetscAssertPointer(support, 3);
3969: PetscCall(PetscSectionGetOffset(mesh->supportSection, p, &off));
3970: PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3971: for (c = 0; c < dof; ++c) {
3972: PetscCheck(!(support[c] < pStart) && !(support[c] >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", support[c], pStart, pEnd);
3973: mesh->supports[off + c] = support[c];
3974: }
3975: PetscFunctionReturn(PETSC_SUCCESS);
3976: }
3978: /*@
3979: DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
3981: Not Collective
3983: Input Parameters:
3984: + dm - The `DMPLEX`
3985: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3986: . supportPos - The local index in the cone where the point should be put
3987: - supportPoint - The mesh point to insert
3989: Level: beginner
3991: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3992: @*/
3993: PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
3994: {
3995: DM_Plex *mesh = (DM_Plex *)dm->data;
3996: PetscInt pStart, pEnd;
3997: PetscInt dof, off;
3999: PetscFunctionBegin;
4001: PetscCall(PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd));
4002: PetscCall(PetscSectionGetDof(mesh->supportSection, p, &dof));
4003: PetscCall(PetscSectionGetOffset(mesh->supportSection, p, &off));
4004: PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
4005: PetscCheck(!(supportPoint < pStart) && !(supportPoint >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", supportPoint, pStart, pEnd);
4006: PetscCheck(supportPos < dof, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support position %" PetscInt_FMT " of point %" PetscInt_FMT " is not in the valid range [0, %" PetscInt_FMT ")", supportPos, p, dof);
4007: mesh->supports[off + supportPos] = supportPoint;
4008: PetscFunctionReturn(PETSC_SUCCESS);
4009: }
4011: /* Converts an orientation o in the current numbering to the previous scheme used in Plex */
4012: PetscInt DMPolytopeConvertNewOrientation_Internal(DMPolytopeType ct, PetscInt o)
4013: {
4014: switch (ct) {
4015: case DM_POLYTOPE_SEGMENT:
4016: if (o == -1) return -2;
4017: break;
4018: case DM_POLYTOPE_TRIANGLE:
4019: if (o == -3) return -1;
4020: if (o == -2) return -3;
4021: if (o == -1) return -2;
4022: break;
4023: case DM_POLYTOPE_QUADRILATERAL:
4024: if (o == -4) return -2;
4025: if (o == -3) return -1;
4026: if (o == -2) return -4;
4027: if (o == -1) return -3;
4028: break;
4029: default:
4030: return o;
4031: }
4032: return o;
4033: }
4035: /* Converts an orientation o in the previous scheme used in Plex to the current numbering */
4036: PetscInt DMPolytopeConvertOldOrientation_Internal(DMPolytopeType ct, PetscInt o)
4037: {
4038: switch (ct) {
4039: case DM_POLYTOPE_SEGMENT:
4040: if ((o == -2) || (o == 1)) return -1;
4041: if (o == -1) return 0;
4042: break;
4043: case DM_POLYTOPE_TRIANGLE:
4044: if (o == -3) return -2;
4045: if (o == -2) return -1;
4046: if (o == -1) return -3;
4047: break;
4048: case DM_POLYTOPE_QUADRILATERAL:
4049: if (o == -4) return -2;
4050: if (o == -3) return -1;
4051: if (o == -2) return -4;
4052: if (o == -1) return -3;
4053: break;
4054: default:
4055: return o;
4056: }
4057: return o;
4058: }
4060: /* Takes in a mesh whose orientations are in the previous scheme and converts them all to the current numbering */
4061: PetscErrorCode DMPlexConvertOldOrientations_Internal(DM dm)
4062: {
4063: PetscInt pStart, pEnd, p;
4065: PetscFunctionBegin;
4066: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4067: for (p = pStart; p < pEnd; ++p) {
4068: const PetscInt *cone, *ornt;
4069: PetscInt coneSize, c;
4071: PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
4072: PetscCall(DMPlexGetCone(dm, p, &cone));
4073: PetscCall(DMPlexGetConeOrientation(dm, p, &ornt));
4074: for (c = 0; c < coneSize; ++c) {
4075: DMPolytopeType ct;
4076: const PetscInt o = ornt[c];
4078: PetscCall(DMPlexGetCellType(dm, cone[c], &ct));
4079: switch (ct) {
4080: case DM_POLYTOPE_SEGMENT:
4081: if ((o == -2) || (o == 1)) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -1));
4082: if (o == -1) PetscCall(DMPlexInsertConeOrientation(dm, p, c, 0));
4083: break;
4084: case DM_POLYTOPE_TRIANGLE:
4085: if (o == -3) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -2));
4086: if (o == -2) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -1));
4087: if (o == -1) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -3));
4088: break;
4089: case DM_POLYTOPE_QUADRILATERAL:
4090: if (o == -4) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -2));
4091: if (o == -3) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -1));
4092: if (o == -2) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -4));
4093: if (o == -1) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -3));
4094: break;
4095: default:
4096: break;
4097: }
4098: }
4099: }
4100: PetscFunctionReturn(PETSC_SUCCESS);
4101: }
4103: static inline PetscErrorCode DMPlexGetTransitiveClosure_Hot_Private(DM dm, PetscInt p, PetscBool useCone, PetscInt *size, const PetscInt *arr[], const PetscInt *ornt[])
4104: {
4105: DM_Plex *mesh = (DM_Plex *)dm->data;
4107: PetscFunctionBeginHot;
4108: if (PetscDefined(USE_DEBUG) || mesh->tr) {
4109: if (useCone) {
4110: PetscCall(DMPlexGetConeSize(dm, p, size));
4111: PetscCall(DMPlexGetOrientedCone(dm, p, arr, ornt));
4112: } else {
4113: PetscCall(DMPlexGetSupportSize(dm, p, size));
4114: PetscCall(DMPlexGetSupport(dm, p, arr));
4115: }
4116: } else {
4117: if (useCone) {
4118: const PetscSection s = mesh->coneSection;
4119: const PetscInt ps = p - s->pStart;
4120: const PetscInt off = s->atlasOff[ps];
4122: *size = s->atlasDof[ps];
4123: *arr = mesh->cones + off;
4124: *ornt = mesh->coneOrientations + off;
4125: } else {
4126: const PetscSection s = mesh->supportSection;
4127: const PetscInt ps = p - s->pStart;
4128: const PetscInt off = s->atlasOff[ps];
4130: *size = s->atlasDof[ps];
4131: *arr = mesh->supports + off;
4132: }
4133: }
4134: PetscFunctionReturn(PETSC_SUCCESS);
4135: }
4137: static inline PetscErrorCode DMPlexRestoreTransitiveClosure_Hot_Private(DM dm, PetscInt p, PetscBool useCone, PetscInt *size, const PetscInt *arr[], const PetscInt *ornt[])
4138: {
4139: DM_Plex *mesh = (DM_Plex *)dm->data;
4141: PetscFunctionBeginHot;
4142: if (PetscDefined(USE_DEBUG) || mesh->tr) {
4143: if (useCone) PetscCall(DMPlexRestoreOrientedCone(dm, p, arr, ornt));
4144: }
4145: PetscFunctionReturn(PETSC_SUCCESS);
4146: }
4148: static PetscErrorCode DMPlexGetTransitiveClosure_Depth1_Private(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
4149: {
4150: DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4151: PetscInt *closure;
4152: const PetscInt *tmp = NULL, *tmpO = NULL;
4153: PetscInt off = 0, tmpSize, t;
4155: PetscFunctionBeginHot;
4156: if (ornt) {
4157: PetscCall(DMPlexGetCellType(dm, p, &ct));
4158: if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN || ct == DM_POLYTOPE_UNKNOWN_CELL || ct == DM_POLYTOPE_UNKNOWN_FACE) ct = DM_POLYTOPE_UNKNOWN;
4159: }
4160: if (*points) {
4161: closure = *points;
4162: } else {
4163: PetscInt maxConeSize, maxSupportSize;
4164: PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
4165: PetscCall(DMGetWorkArray(dm, 2 * (PetscMax(maxConeSize, maxSupportSize) + 1), MPIU_INT, &closure));
4166: }
4167: PetscCall(DMPlexGetTransitiveClosure_Hot_Private(dm, p, useCone, &tmpSize, &tmp, &tmpO));
4168: if (ct == DM_POLYTOPE_UNKNOWN) {
4169: closure[off++] = p;
4170: closure[off++] = 0;
4171: for (t = 0; t < tmpSize; ++t) {
4172: closure[off++] = tmp[t];
4173: closure[off++] = tmpO ? tmpO[t] : 0;
4174: }
4175: } else {
4176: const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, ornt);
4178: /* We assume that cells with a valid type have faces with a valid type */
4179: closure[off++] = p;
4180: closure[off++] = ornt;
4181: for (t = 0; t < tmpSize; ++t) {
4182: DMPolytopeType ft;
4184: PetscCall(DMPlexGetCellType(dm, tmp[t], &ft));
4185: closure[off++] = tmp[arr[t]];
4186: closure[off++] = tmpO ? DMPolytopeTypeComposeOrientation(ft, ornt, tmpO[t]) : 0;
4187: }
4188: }
4189: PetscCall(DMPlexRestoreTransitiveClosure_Hot_Private(dm, p, useCone, &tmpSize, &tmp, &tmpO));
4190: if (numPoints) *numPoints = tmpSize + 1;
4191: if (points) *points = closure;
4192: PetscFunctionReturn(PETSC_SUCCESS);
4193: }
4195: /* We need a special tensor version because we want to allow duplicate points in the endcaps for hybrid cells */
4196: static PetscErrorCode DMPlexTransitiveClosure_Tensor_Internal(DM dm, PetscInt point, DMPolytopeType ct, PetscInt o, PetscBool useCone, PetscInt *numPoints, PetscInt **points)
4197: {
4198: const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
4199: const PetscInt *cone, *ornt;
4200: PetscInt *pts, *closure = NULL;
4201: DMPolytopeType ft;
4202: PetscInt maxConeSize, maxSupportSize, coneSeries, supportSeries, maxSize;
4203: PetscInt dim, coneSize, c, d, clSize, cl;
4205: PetscFunctionBeginHot;
4206: PetscCall(DMGetDimension(dm, &dim));
4207: PetscCall(DMPlexGetTransitiveClosure_Hot_Private(dm, point, PETSC_TRUE, &coneSize, &cone, &ornt));
4208: PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
4209: coneSeries = (maxConeSize > 1) ? ((PetscPowInt(maxConeSize, dim + 1) - 1) / (maxConeSize - 1)) : dim + 1;
4210: supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, dim + 1) - 1) / (maxSupportSize - 1)) : dim + 1;
4211: maxSize = PetscMax(coneSeries, supportSeries);
4212: if (*points) {
4213: pts = *points;
4214: } else PetscCall(DMGetWorkArray(dm, 2 * maxSize, MPIU_INT, &pts));
4215: c = 0;
4216: pts[c++] = point;
4217: pts[c++] = o;
4218: PetscCall(DMPlexGetCellType(dm, cone[arr[0 * 2 + 0]], &ft));
4219: PetscCall(DMPlexGetTransitiveClosure_Internal(dm, cone[arr[0 * 2 + 0]], DMPolytopeTypeComposeOrientation(ft, arr[0 * 2 + 1], ornt[0]), useCone, &clSize, &closure));
4220: for (cl = 0; cl < clSize * 2; cl += 2) {
4221: pts[c++] = closure[cl];
4222: pts[c++] = closure[cl + 1];
4223: }
4224: PetscCall(DMPlexGetTransitiveClosure_Internal(dm, cone[arr[1 * 2 + 0]], DMPolytopeTypeComposeOrientation(ft, arr[1 * 2 + 1], ornt[1]), useCone, &clSize, &closure));
4225: for (cl = 0; cl < clSize * 2; cl += 2) {
4226: pts[c++] = closure[cl];
4227: pts[c++] = closure[cl + 1];
4228: }
4229: PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], useCone, &clSize, &closure));
4230: for (d = 2; d < coneSize; ++d) {
4231: PetscCall(DMPlexGetCellType(dm, cone[arr[d * 2 + 0]], &ft));
4232: pts[c++] = cone[arr[d * 2 + 0]];
4233: pts[c++] = DMPolytopeTypeComposeOrientation(ft, arr[d * 2 + 1], ornt[d]);
4234: }
4235: PetscCall(DMPlexRestoreTransitiveClosure_Hot_Private(dm, point, PETSC_TRUE, &coneSize, &cone, &ornt));
4236: if (dim >= 3) {
4237: for (d = 2; d < coneSize; ++d) {
4238: const PetscInt fpoint = cone[arr[d * 2 + 0]];
4239: const PetscInt *fcone, *fornt;
4240: PetscInt fconeSize, fc, i;
4242: PetscCall(DMPlexGetCellType(dm, fpoint, &ft));
4243: const PetscInt *farr = DMPolytopeTypeGetArrangement(ft, DMPolytopeTypeComposeOrientation(ft, arr[d * 2 + 1], ornt[d]));
4244: PetscCall(DMPlexGetTransitiveClosure_Hot_Private(dm, fpoint, PETSC_TRUE, &fconeSize, &fcone, &fornt));
4245: for (fc = 0; fc < fconeSize; ++fc) {
4246: const PetscInt cp = fcone[farr[fc * 2 + 0]];
4247: const PetscInt co = farr[fc * 2 + 1];
4249: for (i = 0; i < c; i += 2)
4250: if (pts[i] == cp) break;
4251: if (i == c) {
4252: PetscCall(DMPlexGetCellType(dm, cp, &ft));
4253: pts[c++] = cp;
4254: pts[c++] = DMPolytopeTypeComposeOrientation(ft, co, fornt[farr[fc * 2 + 0]]);
4255: }
4256: }
4257: PetscCall(DMPlexRestoreTransitiveClosure_Hot_Private(dm, fpoint, PETSC_TRUE, &fconeSize, &fcone, &fornt));
4258: }
4259: }
4260: *numPoints = c / 2;
4261: *points = pts;
4262: PetscFunctionReturn(PETSC_SUCCESS);
4263: }
4265: PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
4266: {
4267: DMPolytopeType ct;
4268: PetscInt *closure, *fifo;
4269: PetscInt closureSize = 0, fifoStart = 0, fifoSize = 0;
4270: PetscInt maxConeSize, maxSupportSize, coneSeries, supportSeries;
4271: PetscInt depth, maxSize;
4273: PetscFunctionBeginHot;
4274: PetscCall(DMPlexGetDepth(dm, &depth));
4275: if (depth == 1) {
4276: PetscCall(DMPlexGetTransitiveClosure_Depth1_Private(dm, p, ornt, useCone, numPoints, points));
4277: PetscFunctionReturn(PETSC_SUCCESS);
4278: }
4279: PetscCall(DMPlexGetCellType(dm, p, &ct));
4280: if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN || ct == DM_POLYTOPE_UNKNOWN_CELL || ct == DM_POLYTOPE_UNKNOWN_FACE) ct = DM_POLYTOPE_UNKNOWN;
4281: if (DMPolytopeTypeIsHybrid(ct) && ct != DM_POLYTOPE_POINT_PRISM_TENSOR) {
4282: PetscCall(DMPlexTransitiveClosure_Tensor_Internal(dm, p, ct, ornt, useCone, numPoints, points));
4283: PetscFunctionReturn(PETSC_SUCCESS);
4284: }
4285: PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
4286: coneSeries = (maxConeSize > 1) ? ((PetscPowInt(maxConeSize, depth + 1) - 1) / (maxConeSize - 1)) : depth + 1;
4287: supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, depth + 1) - 1) / (maxSupportSize - 1)) : depth + 1;
4288: maxSize = PetscMax(coneSeries, supportSeries);
4289: PetscCall(DMGetWorkArray(dm, 3 * maxSize, MPIU_INT, &fifo));
4290: if (*points) {
4291: closure = *points;
4292: } else PetscCall(DMGetWorkArray(dm, 2 * maxSize, MPIU_INT, &closure));
4293: closure[closureSize++] = p;
4294: closure[closureSize++] = ornt;
4295: fifo[fifoSize++] = p;
4296: fifo[fifoSize++] = ornt;
4297: fifo[fifoSize++] = ct;
4298: /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
4299: while (fifoSize - fifoStart) {
4300: const PetscInt q = fifo[fifoStart++];
4301: const PetscInt o = fifo[fifoStart++];
4302: const DMPolytopeType qt = (DMPolytopeType)fifo[fifoStart++];
4303: const PetscInt *qarr = DMPolytopeTypeGetArrangement(qt, o);
4304: const PetscInt *tmp, *tmpO = NULL;
4305: PetscInt tmpSize, t;
4307: if (PetscDefined(USE_DEBUG)) {
4308: PetscInt nO = DMPolytopeTypeGetNumArrangements(qt) / 2;
4309: PetscCheck(!o || !(o >= nO || o < -nO), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid orientation %" PetscInt_FMT " not in [%" PetscInt_FMT ",%" PetscInt_FMT ") for %s %" PetscInt_FMT, o, -nO, nO, DMPolytopeTypes[qt], q);
4310: }
4311: PetscCall(DMPlexGetTransitiveClosure_Hot_Private(dm, q, useCone, &tmpSize, &tmp, &tmpO));
4312: for (t = 0; t < tmpSize; ++t) {
4313: const PetscInt ip = useCone && qarr ? qarr[t * 2] : t;
4314: const PetscInt io = useCone && qarr ? qarr[t * 2 + 1] : 0;
4315: const PetscInt cp = tmp[ip];
4316: PetscCall(DMPlexGetCellType(dm, cp, &ct));
4317: const PetscInt co = tmpO ? DMPolytopeTypeComposeOrientation(ct, io, tmpO[ip]) : 0;
4318: PetscInt c;
4320: /* Check for duplicate */
4321: for (c = 0; c < closureSize; c += 2) {
4322: if (closure[c] == cp) break;
4323: }
4324: if (c == closureSize) {
4325: closure[closureSize++] = cp;
4326: closure[closureSize++] = co;
4327: fifo[fifoSize++] = cp;
4328: fifo[fifoSize++] = co;
4329: fifo[fifoSize++] = ct;
4330: }
4331: }
4332: PetscCall(DMPlexRestoreTransitiveClosure_Hot_Private(dm, q, useCone, &tmpSize, &tmp, &tmpO));
4333: }
4334: PetscCall(DMRestoreWorkArray(dm, 3 * maxSize, MPIU_INT, &fifo));
4335: if (numPoints) *numPoints = closureSize / 2;
4336: if (points) *points = closure;
4337: PetscFunctionReturn(PETSC_SUCCESS);
4338: }
4340: /*@C
4341: DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
4343: Not Collective
4345: Input Parameters:
4346: + dm - The `DMPLEX`
4347: . p - The mesh point
4348: - useCone - `PETSC_TRUE` for the closure, otherwise return the support
4350: Input/Output Parameter:
4351: . points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...];
4352: if *points is `NULL` on input, internal storage will be returned, use `DMPlexRestoreTransitiveClosure()`,
4353: otherwise the provided array is used to hold the values
4355: Output Parameter:
4356: . numPoints - The number of points in the closure, so `points` is of size 2*`numPoints`
4358: Level: beginner
4360: Note:
4361: If using internal storage (points is `NULL` on input), each call overwrites the last output.
4363: Fortran Notes:
4364: `points` must be declared with
4365: .vb
4366: PetscInt, pointer :: points(:)
4367: .ve
4368: and is always allocated by the function.
4370: Pass `PETSC_NULL_INTEGER` for `numPoints` if it is not needed
4372: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreTransitiveClosure()`, `DMPlexCreate()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexGetCone()`
4373: @*/
4374: PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
4375: {
4376: PetscFunctionBeginHot;
4378: if (numPoints) PetscAssertPointer(numPoints, 4);
4379: if (points) PetscAssertPointer(points, 5);
4380: if (PetscDefined(USE_DEBUG)) {
4381: PetscInt pStart, pEnd;
4382: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4383: PetscCheck(p >= pStart && p < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " is not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
4384: }
4385: PetscCall(DMPlexGetTransitiveClosure_Internal(dm, p, 0, useCone, numPoints, points));
4386: PetscFunctionReturn(PETSC_SUCCESS);
4387: }
4389: /*@C
4390: DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
4392: Not Collective
4394: Input Parameters:
4395: + dm - The `DMPLEX`
4396: . p - The mesh point
4397: . useCone - `PETSC_TRUE` for the closure, otherwise return the star
4398: . numPoints - The number of points in the closure, so points[] is of size 2*`numPoints`
4399: - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
4401: Level: beginner
4403: Note:
4404: If not using internal storage (points is not `NULL` on input), this call is unnecessary
4406: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetTransitiveClosure()`, `DMPlexCreate()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexGetCone()`
4407: @*/
4408: PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
4409: {
4410: PetscFunctionBeginHot;
4412: if (numPoints) *numPoints = 0;
4413: PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, points));
4414: PetscFunctionReturn(PETSC_SUCCESS);
4415: }
4417: /*@
4418: DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
4420: Not Collective
4422: Input Parameter:
4423: . dm - The `DMPLEX`
4425: Output Parameters:
4426: + maxConeSize - The maximum number of in-edges
4427: - maxSupportSize - The maximum number of out-edges
4429: Level: beginner
4431: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`
4432: @*/
4433: PetscErrorCode DMPlexGetMaxSizes(DM dm, PeOp PetscInt *maxConeSize, PeOp PetscInt *maxSupportSize)
4434: {
4435: DM_Plex *mesh = (DM_Plex *)dm->data;
4437: PetscFunctionBegin;
4439: if (maxConeSize) PetscCall(PetscSectionGetMaxDof(mesh->coneSection, maxConeSize));
4440: if (maxSupportSize) PetscCall(PetscSectionGetMaxDof(mesh->supportSection, maxSupportSize));
4441: PetscFunctionReturn(PETSC_SUCCESS);
4442: }
4444: PetscErrorCode DMSetUp_Plex(DM dm)
4445: {
4446: DM_Plex *mesh = (DM_Plex *)dm->data;
4447: PetscInt size, maxSupportSize;
4449: PetscFunctionBegin;
4451: PetscCall(PetscSectionSetUp(mesh->coneSection));
4452: PetscCall(PetscSectionGetStorageSize(mesh->coneSection, &size));
4453: PetscCall(PetscMalloc1(size, &mesh->cones));
4454: PetscCall(PetscCalloc1(size, &mesh->coneOrientations));
4455: PetscCall(PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize));
4456: if (maxSupportSize) {
4457: PetscCall(PetscSectionSetUp(mesh->supportSection));
4458: PetscCall(PetscSectionGetStorageSize(mesh->supportSection, &size));
4459: PetscCall(PetscMalloc1(size, &mesh->supports));
4460: }
4461: PetscFunctionReturn(PETSC_SUCCESS);
4462: }
4464: PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
4465: {
4466: PetscFunctionBegin;
4467: if (subdm) PetscCall(DMClone(dm, subdm));
4468: PetscCall(DMCreateSectionSubDM(dm, numFields, fields, NULL, NULL, is, subdm));
4469: if (subdm) (*subdm)->useNatural = dm->useNatural;
4470: if (dm->useNatural && dm->sfMigration) {
4471: PetscSF sfNatural;
4473: (*subdm)->sfMigration = dm->sfMigration;
4474: PetscCall(PetscObjectReference((PetscObject)dm->sfMigration));
4475: PetscCall(DMPlexCreateGlobalToNaturalSF(*subdm, NULL, (*subdm)->sfMigration, &sfNatural));
4476: (*subdm)->sfNatural = sfNatural;
4477: }
4478: PetscFunctionReturn(PETSC_SUCCESS);
4479: }
4481: PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
4482: {
4483: PetscInt i = 0;
4485: PetscFunctionBegin;
4486: PetscCall(DMClone(dms[0], superdm));
4487: PetscCall(DMCreateSectionSuperDM(dms, len, is, superdm));
4488: (*superdm)->useNatural = PETSC_FALSE;
4489: for (i = 0; i < len; i++) {
4490: if (dms[i]->useNatural && dms[i]->sfMigration) {
4491: PetscSF sfNatural;
4493: (*superdm)->sfMigration = dms[i]->sfMigration;
4494: PetscCall(PetscObjectReference((PetscObject)dms[i]->sfMigration));
4495: (*superdm)->useNatural = PETSC_TRUE;
4496: PetscCall(DMPlexCreateGlobalToNaturalSF(*superdm, NULL, (*superdm)->sfMigration, &sfNatural));
4497: (*superdm)->sfNatural = sfNatural;
4498: break;
4499: }
4500: }
4501: PetscFunctionReturn(PETSC_SUCCESS);
4502: }
4504: /*@
4505: DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
4507: Not Collective
4509: Input Parameter:
4510: . dm - The `DMPLEX`
4512: Level: beginner
4514: Note:
4515: This should be called after all calls to `DMPlexSetCone()`
4517: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMPlexSetCone()`
4518: @*/
4519: PetscErrorCode DMPlexSymmetrize(DM dm)
4520: {
4521: DM_Plex *mesh = (DM_Plex *)dm->data;
4522: PetscInt *offsets;
4523: PetscInt supportSize;
4524: PetscInt pStart, pEnd, p;
4526: PetscFunctionBegin;
4528: PetscCheck(!mesh->supports, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
4529: PetscCall(PetscLogEventBegin(DMPLEX_Symmetrize, dm, 0, 0, 0));
4530: /* Calculate support sizes */
4531: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4532: for (p = pStart; p < pEnd; ++p) {
4533: PetscInt dof, off, c;
4535: PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
4536: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
4537: for (c = off; c < off + dof; ++c) PetscCall(PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1));
4538: }
4539: PetscCall(PetscSectionSetUp(mesh->supportSection));
4540: /* Calculate supports */
4541: PetscCall(PetscSectionGetStorageSize(mesh->supportSection, &supportSize));
4542: PetscCall(PetscMalloc1(supportSize, &mesh->supports));
4543: PetscCall(PetscCalloc1(pEnd - pStart, &offsets));
4544: for (p = pStart; p < pEnd; ++p) {
4545: PetscInt dof, off, c;
4547: PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
4548: PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
4549: for (c = off; c < off + dof; ++c) {
4550: const PetscInt q = mesh->cones[c];
4551: PetscInt offS;
4553: PetscCall(PetscSectionGetOffset(mesh->supportSection, q, &offS));
4555: mesh->supports[offS + offsets[q]] = p;
4556: ++offsets[q];
4557: }
4558: }
4559: PetscCall(PetscFree(offsets));
4560: PetscCall(PetscLogEventEnd(DMPLEX_Symmetrize, dm, 0, 0, 0));
4561: PetscFunctionReturn(PETSC_SUCCESS);
4562: }
4564: static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
4565: {
4566: IS stratumIS;
4568: PetscFunctionBegin;
4569: if (pStart >= pEnd) PetscFunctionReturn(PETSC_SUCCESS);
4570: if (PetscDefined(USE_DEBUG)) {
4571: PetscInt qStart, qEnd, numLevels, level;
4572: PetscBool overlap = PETSC_FALSE;
4573: PetscCall(DMLabelGetNumValues(label, &numLevels));
4574: for (level = 0; level < numLevels; level++) {
4575: PetscCall(DMLabelGetStratumBounds(label, level, &qStart, &qEnd));
4576: if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {
4577: overlap = PETSC_TRUE;
4578: break;
4579: }
4580: }
4581: PetscCheck(!overlap, PETSC_COMM_SELF, PETSC_ERR_PLIB, "New depth %" PetscInt_FMT " range [%" PetscInt_FMT ",%" PetscInt_FMT ") overlaps with depth %" PetscInt_FMT " range [%" PetscInt_FMT ",%" PetscInt_FMT ")", depth, pStart, pEnd, level, qStart, qEnd);
4582: }
4583: PetscCall(ISCreateStride(PETSC_COMM_SELF, pEnd - pStart, pStart, 1, &stratumIS));
4584: PetscCall(DMLabelSetStratumIS(label, depth, stratumIS));
4585: PetscCall(ISDestroy(&stratumIS));
4586: PetscFunctionReturn(PETSC_SUCCESS);
4587: }
4589: static PetscErrorCode DMPlexStratify_CellType_Private(DM dm, DMLabel label)
4590: {
4591: PetscInt *pMin, *pMax;
4592: PetscInt pStart, pEnd;
4593: PetscInt dmin = PETSC_INT_MAX, dmax = PETSC_INT_MIN;
4595: PetscFunctionBegin;
4596: {
4597: DMLabel label2;
4599: PetscCall(DMPlexGetCellTypeLabel(dm, &label2));
4600: PetscCall(PetscObjectViewFromOptions((PetscObject)label2, NULL, "-ct_view"));
4601: }
4602: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4603: for (PetscInt p = pStart; p < pEnd; ++p) {
4604: DMPolytopeType ct;
4606: PetscCall(DMPlexGetCellType(dm, p, &ct));
4607: dmin = PetscMin(DMPolytopeTypeGetDim(ct), dmin);
4608: dmax = PetscMax(DMPolytopeTypeGetDim(ct), dmax);
4609: }
4610: PetscCall(PetscMalloc2(dmax + 1, &pMin, dmax + 1, &pMax));
4611: for (PetscInt d = dmin; d <= dmax; ++d) {
4612: pMin[d] = PETSC_INT_MAX;
4613: pMax[d] = PETSC_INT_MIN;
4614: }
4615: for (PetscInt p = pStart; p < pEnd; ++p) {
4616: DMPolytopeType ct;
4617: PetscInt d;
4619: PetscCall(DMPlexGetCellType(dm, p, &ct));
4620: d = DMPolytopeTypeGetDim(ct);
4621: pMin[d] = PetscMin(p, pMin[d]);
4622: pMax[d] = PetscMax(p, pMax[d]);
4623: }
4624: for (PetscInt d = dmin; d <= dmax; ++d) {
4625: if (pMin[d] > pMax[d]) continue;
4626: PetscCall(DMPlexCreateDepthStratum(dm, label, d, pMin[d], pMax[d] + 1));
4627: }
4628: PetscCall(PetscFree2(pMin, pMax));
4629: PetscFunctionReturn(PETSC_SUCCESS);
4630: }
4632: static PetscErrorCode DMPlexStratify_Topological_Private(DM dm, DMLabel label)
4633: {
4634: PetscInt pStart, pEnd;
4635: PetscInt numRoots = 0, numLeaves = 0;
4637: PetscFunctionBegin;
4638: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4639: {
4640: /* Initialize roots and count leaves */
4641: PetscInt sMin = PETSC_INT_MAX;
4642: PetscInt sMax = PETSC_INT_MIN;
4643: PetscInt coneSize, supportSize;
4645: for (PetscInt p = pStart; p < pEnd; ++p) {
4646: PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
4647: PetscCall(DMPlexGetSupportSize(dm, p, &supportSize));
4648: if (!coneSize && supportSize) {
4649: sMin = PetscMin(p, sMin);
4650: sMax = PetscMax(p, sMax);
4651: ++numRoots;
4652: } else if (!supportSize && coneSize) {
4653: ++numLeaves;
4654: } else if (!supportSize && !coneSize) {
4655: /* Isolated points */
4656: sMin = PetscMin(p, sMin);
4657: sMax = PetscMax(p, sMax);
4658: }
4659: }
4660: PetscCall(DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax + 1));
4661: }
4663: if (numRoots + numLeaves == (pEnd - pStart)) {
4664: PetscInt sMin = PETSC_INT_MAX;
4665: PetscInt sMax = PETSC_INT_MIN;
4666: PetscInt coneSize, supportSize;
4668: for (PetscInt p = pStart; p < pEnd; ++p) {
4669: PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
4670: PetscCall(DMPlexGetSupportSize(dm, p, &supportSize));
4671: if (!supportSize && coneSize) {
4672: sMin = PetscMin(p, sMin);
4673: sMax = PetscMax(p, sMax);
4674: }
4675: }
4676: PetscCall(DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax + 1));
4677: } else {
4678: PetscInt level = 0;
4679: PetscInt qStart, qEnd;
4681: PetscCall(DMLabelGetStratumBounds(label, level, &qStart, &qEnd));
4682: while (qEnd > qStart) {
4683: PetscInt sMin = PETSC_INT_MAX;
4684: PetscInt sMax = PETSC_INT_MIN;
4686: for (PetscInt q = qStart; q < qEnd; ++q) {
4687: const PetscInt *support;
4688: PetscInt supportSize;
4690: PetscCall(DMPlexGetSupportSize(dm, q, &supportSize));
4691: PetscCall(DMPlexGetSupport(dm, q, &support));
4692: for (PetscInt s = 0; s < supportSize; ++s) {
4693: sMin = PetscMin(support[s], sMin);
4694: sMax = PetscMax(support[s], sMax);
4695: }
4696: }
4697: PetscCall(DMLabelGetNumValues(label, &level));
4698: PetscCall(DMPlexCreateDepthStratum(dm, label, level, sMin, sMax + 1));
4699: PetscCall(DMLabelGetStratumBounds(label, level, &qStart, &qEnd));
4700: }
4701: }
4702: PetscFunctionReturn(PETSC_SUCCESS);
4703: }
4705: /*@
4706: DMPlexStratify - Computes the strata for all points in the `DMPLEX`
4708: Collective
4710: Input Parameter:
4711: . dm - The `DMPLEX`
4713: Level: beginner
4715: Notes:
4716: The strata group all points of the same grade, and this function calculates the strata. This
4717: grade can be seen as the height (or depth) of the point in the DAG.
4719: The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
4720: can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram).
4721: Concretely, `DMPlexStratify()` creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
4722: meshes, vertices are depth 0 and cells are depth 1. For fully interpolated meshes, depth 0 for vertices, 1 for edges, and so on
4723: until cells have depth equal to the dimension of the mesh. The depth label can be accessed through `DMPlexGetDepthLabel()` or `DMPlexGetDepthStratum()`, or
4724: manually via `DMGetLabel()`. The height is defined implicitly by height = maxDimension - depth, and can be accessed
4725: via `DMPlexGetHeightStratum()`. For example, cells have height 0 and faces have height 1.
4727: The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
4728: if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
4729: 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
4730: to interpolate only that one (e0), so that
4731: .vb
4732: cone(c0) = {e0, v2}
4733: cone(e0) = {v0, v1}
4734: .ve
4735: If `DMPlexStratify()` is run on this mesh, it will give depths
4736: .vb
4737: depth 0 = {v0, v1, v2}
4738: depth 1 = {e0, c0}
4739: .ve
4740: where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
4742: `DMPlexStratify()` should be called after all calls to `DMPlexSymmetrize()`
4744: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSymmetrize()`, `DMPlexComputeCellTypes()`
4745: @*/
4746: PetscErrorCode DMPlexStratify(DM dm)
4747: {
4748: DM_Plex *mesh = (DM_Plex *)dm->data;
4749: DMLabel label;
4750: PetscBool flg = PETSC_FALSE;
4752: PetscFunctionBegin;
4754: PetscCall(PetscLogEventBegin(DMPLEX_Stratify, dm, 0, 0, 0));
4756: // Create depth label
4757: PetscCall(DMRemoveLabel(dm, "depth", NULL));
4758: PetscCall(DMCreateLabel(dm, "depth"));
4759: PetscCall(DMPlexGetDepthLabel(dm, &label));
4761: PetscCall(PetscOptionsGetBool(NULL, dm->hdr.prefix, "-dm_plex_stratify_celltype", &flg, NULL));
4762: if (flg) PetscCall(DMPlexStratify_CellType_Private(dm, label));
4763: else PetscCall(DMPlexStratify_Topological_Private(dm, label));
4765: { /* just in case there is an empty process */
4766: PetscInt numValues, maxValues = 0, v;
4768: PetscCall(DMLabelGetNumValues(label, &numValues));
4769: PetscCallMPI(MPIU_Allreduce(&numValues, &maxValues, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
4770: for (v = numValues; v < maxValues; v++) PetscCall(DMLabelAddStratum(label, v));
4771: }
4772: PetscCall(PetscObjectStateGet((PetscObject)label, &mesh->depthState));
4773: PetscCall(PetscLogEventEnd(DMPLEX_Stratify, dm, 0, 0, 0));
4774: PetscFunctionReturn(PETSC_SUCCESS);
4775: }
4777: PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
4778: {
4779: DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4780: PetscInt dim, depth, pheight, coneSize;
4781: PetscBool preferTensor;
4783: PetscFunctionBeginHot;
4784: PetscCall(DMGetDimension(dm, &dim));
4785: PetscCall(DMPlexGetDepth(dm, &depth));
4786: PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
4787: PetscCall(DMPlexGetInterpolatePreferTensor(dm, &preferTensor));
4788: pheight = depth - pdepth;
4789: if (depth <= 1) {
4790: switch (pdepth) {
4791: case 0:
4792: ct = DM_POLYTOPE_POINT;
4793: break;
4794: case 1:
4795: switch (coneSize) {
4796: case 2:
4797: ct = DM_POLYTOPE_SEGMENT;
4798: break;
4799: case 3:
4800: ct = DM_POLYTOPE_TRIANGLE;
4801: break;
4802: case 4:
4803: switch (dim) {
4804: case 2:
4805: ct = DM_POLYTOPE_QUADRILATERAL;
4806: break;
4807: case 3:
4808: ct = DM_POLYTOPE_TETRAHEDRON;
4809: break;
4810: default:
4811: break;
4812: }
4813: break;
4814: case 5:
4815: ct = DM_POLYTOPE_PYRAMID;
4816: break;
4817: case 6:
4818: ct = preferTensor ? DM_POLYTOPE_TRI_PRISM_TENSOR : DM_POLYTOPE_TRI_PRISM;
4819: break;
4820: case 8:
4821: ct = DM_POLYTOPE_HEXAHEDRON;
4822: break;
4823: default:
4824: break;
4825: }
4826: }
4827: } else {
4828: if (pdepth == 0) {
4829: ct = DM_POLYTOPE_POINT;
4830: } else if (pheight == 0) {
4831: switch (dim) {
4832: case 1:
4833: switch (coneSize) {
4834: case 2:
4835: ct = DM_POLYTOPE_SEGMENT;
4836: break;
4837: default:
4838: break;
4839: }
4840: break;
4841: case 2:
4842: switch (coneSize) {
4843: case 3:
4844: ct = DM_POLYTOPE_TRIANGLE;
4845: break;
4846: case 4:
4847: ct = DM_POLYTOPE_QUADRILATERAL;
4848: break;
4849: default:
4850: break;
4851: }
4852: break;
4853: case 3:
4854: switch (coneSize) {
4855: case 4:
4856: ct = DM_POLYTOPE_TETRAHEDRON;
4857: break;
4858: case 5: {
4859: const PetscInt *cone;
4860: PetscInt faceConeSize;
4862: PetscCall(DMPlexGetCone(dm, p, &cone));
4863: PetscCall(DMPlexGetConeSize(dm, cone[0], &faceConeSize));
4864: switch (faceConeSize) {
4865: case 3:
4866: ct = preferTensor ? DM_POLYTOPE_TRI_PRISM_TENSOR : DM_POLYTOPE_TRI_PRISM;
4867: break;
4868: case 4:
4869: ct = DM_POLYTOPE_PYRAMID;
4870: break;
4871: }
4872: } break;
4873: case 6:
4874: ct = DM_POLYTOPE_HEXAHEDRON;
4875: break;
4876: default:
4877: break;
4878: }
4879: break;
4880: default:
4881: break;
4882: }
4883: } else if (pheight > 0) {
4884: switch (coneSize) {
4885: case 2:
4886: ct = DM_POLYTOPE_SEGMENT;
4887: break;
4888: case 3:
4889: ct = DM_POLYTOPE_TRIANGLE;
4890: break;
4891: case 4:
4892: ct = DM_POLYTOPE_QUADRILATERAL;
4893: break;
4894: default:
4895: break;
4896: }
4897: }
4898: }
4899: *pt = ct;
4900: PetscFunctionReturn(PETSC_SUCCESS);
4901: }
4903: /*@
4904: DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.
4906: Collective
4908: Input Parameter:
4909: . dm - The `DMPLEX`
4911: Level: developer
4913: Note:
4914: This function is normally called automatically when a cell type is requested. It creates an
4915: internal `DMLabel` named "celltype" which can be directly accessed using `DMGetLabel()`. A user may disable
4916: automatic creation by creating the label manually, using `DMCreateLabel`(dm, "celltype").
4918: `DMPlexComputeCellTypes()` should be called after all calls to `DMPlexSymmetrize()` and `DMPlexStratify()`
4920: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSymmetrize()`, `DMPlexStratify()`, `DMGetLabel()`, `DMCreateLabel()`
4921: @*/
4922: PetscErrorCode DMPlexComputeCellTypes(DM dm)
4923: {
4924: DM_Plex *mesh;
4925: DMLabel ctLabel;
4926: PetscInt pStart, pEnd, p;
4928: PetscFunctionBegin;
4930: mesh = (DM_Plex *)dm->data;
4931: PetscCall(DMCreateLabel(dm, "celltype"));
4932: PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
4933: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4934: PetscCall(PetscFree(mesh->cellTypes));
4935: PetscCall(PetscMalloc1(pEnd - pStart, &mesh->cellTypes));
4936: for (p = pStart; p < pEnd; ++p) {
4937: DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4938: PetscInt pdepth;
4940: PetscCall(DMPlexGetPointDepth(dm, p, &pdepth));
4941: PetscCall(DMPlexComputeCellType_Internal(dm, p, pdepth, &ct));
4942: PetscCheck(ct != DM_POLYTOPE_UNKNOWN && ct != DM_POLYTOPE_UNKNOWN_CELL && ct != DM_POLYTOPE_UNKNOWN_FACE, PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %" PetscInt_FMT " has invalid celltype (%s)", p, DMPolytopeTypes[ct]);
4943: PetscCall(DMLabelSetValue(ctLabel, p, ct));
4944: mesh->cellTypes[p - pStart].value_as_uint8 = (uint8_t)ct;
4945: }
4946: PetscCall(PetscObjectStateGet((PetscObject)ctLabel, &mesh->celltypeState));
4947: PetscCall(PetscObjectViewFromOptions((PetscObject)ctLabel, NULL, "-dm_plex_celltypes_view"));
4948: PetscFunctionReturn(PETSC_SUCCESS);
4949: }
4951: /*@C
4952: DMPlexGetJoin - Get an array for the join of the set of points
4954: Not Collective
4956: Input Parameters:
4957: + dm - The `DMPLEX` object
4958: . numPoints - The number of input points for the join
4959: - points - The input points
4961: Output Parameters:
4962: + numCoveredPoints - The number of points in the join
4963: - coveredPoints - The points in the join
4965: Level: intermediate
4967: Note:
4968: Currently, this is restricted to a single level join
4970: Fortran Notes:
4971: `converedPoints` must be declared with
4972: .vb
4973: PetscInt, pointer :: coveredPints(:)
4974: .ve
4976: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreJoin()`, `DMPlexGetMeet()`
4977: @*/
4978: PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
4979: {
4980: DM_Plex *mesh = (DM_Plex *)dm->data;
4981: PetscInt *join[2];
4982: PetscInt joinSize, i = 0;
4983: PetscInt dof, off, p, c, m;
4984: PetscInt maxSupportSize;
4986: PetscFunctionBegin;
4988: PetscAssertPointer(points, 3);
4989: PetscAssertPointer(numCoveredPoints, 4);
4990: PetscAssertPointer(coveredPoints, 5);
4991: PetscCall(PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize));
4992: PetscCall(DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[0]));
4993: PetscCall(DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[1]));
4994: /* Copy in support of first point */
4995: PetscCall(PetscSectionGetDof(mesh->supportSection, points[0], &dof));
4996: PetscCall(PetscSectionGetOffset(mesh->supportSection, points[0], &off));
4997: for (joinSize = 0; joinSize < dof; ++joinSize) join[i][joinSize] = mesh->supports[off + joinSize];
4998: /* Check each successive support */
4999: for (p = 1; p < numPoints; ++p) {
5000: PetscInt newJoinSize = 0;
5002: PetscCall(PetscSectionGetDof(mesh->supportSection, points[p], &dof));
5003: PetscCall(PetscSectionGetOffset(mesh->supportSection, points[p], &off));
5004: for (c = 0; c < dof; ++c) {
5005: const PetscInt point = mesh->supports[off + c];
5007: for (m = 0; m < joinSize; ++m) {
5008: if (point == join[i][m]) {
5009: join[1 - i][newJoinSize++] = point;
5010: break;
5011: }
5012: }
5013: }
5014: joinSize = newJoinSize;
5015: i = 1 - i;
5016: }
5017: *numCoveredPoints = joinSize;
5018: *coveredPoints = join[i];
5019: PetscCall(DMRestoreWorkArray(dm, maxSupportSize, MPIU_INT, &join[1 - i]));
5020: PetscFunctionReturn(PETSC_SUCCESS);
5021: }
5023: /*@C
5024: DMPlexRestoreJoin - Restore an array for the join of the set of points obtained with `DMPlexGetJoin()`
5026: Not Collective
5028: Input Parameters:
5029: + dm - The `DMPLEX` object
5030: . numPoints - The number of input points for the join
5031: - points - The input points
5033: Output Parameters:
5034: + numCoveredPoints - The number of points in the join
5035: - coveredPoints - The points in the join
5037: Level: intermediate
5039: Fortran Notes:
5040: `converedPoints` must be declared with
5041: .vb
5042: PetscInt, pointer :: coveredPoints(:)
5043: .ve
5045: Pass `PETSC_NULL_INTEGER` for `numCoveredPoints` if it is not needed
5047: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetJoin()`, `DMPlexGetFullJoin()`, `DMPlexGetMeet()`
5048: @*/
5049: PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
5050: {
5051: PetscFunctionBegin;
5053: if (points) PetscAssertPointer(points, 3);
5054: if (numCoveredPoints) PetscAssertPointer(numCoveredPoints, 4);
5055: PetscAssertPointer(coveredPoints, 5);
5056: PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, (void *)coveredPoints));
5057: if (numCoveredPoints) *numCoveredPoints = 0;
5058: PetscFunctionReturn(PETSC_SUCCESS);
5059: }
5061: /*@C
5062: DMPlexGetFullJoin - Get an array for the join of the set of points
5064: Not Collective
5066: Input Parameters:
5067: + dm - The `DMPLEX` object
5068: . numPoints - The number of input points for the join
5069: - points - The input points, its length is `numPoints`
5071: Output Parameters:
5072: + numCoveredPoints - The number of points in the join
5073: - coveredPoints - The points in the join, its length is `numCoveredPoints`
5075: Level: intermediate
5077: Fortran Notes:
5078: .vb
5079: PetscInt, pointer :: coveredPints(:)
5080: .ve
5082: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetJoin()`, `DMPlexRestoreJoin()`, `DMPlexGetMeet()`
5083: @*/
5084: PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
5085: {
5086: PetscInt *offsets, **closures;
5087: PetscInt *join[2];
5088: PetscInt depth = 0, maxSize, joinSize = 0, i = 0;
5089: PetscInt p, d, c, m, ms;
5091: PetscFunctionBegin;
5093: PetscAssertPointer(points, 3);
5094: PetscAssertPointer(numCoveredPoints, 4);
5095: PetscAssertPointer(coveredPoints, 5);
5097: PetscCall(DMPlexGetDepth(dm, &depth));
5098: PetscCall(PetscCalloc1(numPoints, &closures));
5099: PetscCall(DMGetWorkArray(dm, numPoints * (depth + 2), MPIU_INT, &offsets));
5100: PetscCall(DMPlexGetMaxSizes(dm, NULL, &ms));
5101: maxSize = (ms > 1) ? ((PetscPowInt(ms, depth + 1) - 1) / (ms - 1)) : depth + 1;
5102: PetscCall(DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]));
5103: PetscCall(DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]));
5105: for (p = 0; p < numPoints; ++p) {
5106: PetscInt closureSize;
5108: PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]));
5110: offsets[p * (depth + 2) + 0] = 0;
5111: for (d = 0; d < depth + 1; ++d) {
5112: PetscInt pStart, pEnd, i;
5114: PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
5115: for (i = offsets[p * (depth + 2) + d]; i < closureSize; ++i) {
5116: if ((pStart > closures[p][i * 2]) || (pEnd <= closures[p][i * 2])) {
5117: offsets[p * (depth + 2) + d + 1] = i;
5118: break;
5119: }
5120: }
5121: if (i == closureSize) offsets[p * (depth + 2) + d + 1] = i;
5122: }
5123: PetscCheck(offsets[p * (depth + 2) + depth + 1] == closureSize, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %" PetscInt_FMT " should be %" PetscInt_FMT, offsets[p * (depth + 2) + depth + 1], closureSize);
5124: }
5125: for (d = 0; d < depth + 1; ++d) {
5126: PetscInt dof;
5128: /* Copy in support of first point */
5129: dof = offsets[d + 1] - offsets[d];
5130: for (joinSize = 0; joinSize < dof; ++joinSize) join[i][joinSize] = closures[0][(offsets[d] + joinSize) * 2];
5131: /* Check each successive cone */
5132: for (p = 1; p < numPoints && joinSize; ++p) {
5133: PetscInt newJoinSize = 0;
5135: dof = offsets[p * (depth + 2) + d + 1] - offsets[p * (depth + 2) + d];
5136: for (c = 0; c < dof; ++c) {
5137: const PetscInt point = closures[p][(offsets[p * (depth + 2) + d] + c) * 2];
5139: for (m = 0; m < joinSize; ++m) {
5140: if (point == join[i][m]) {
5141: join[1 - i][newJoinSize++] = point;
5142: break;
5143: }
5144: }
5145: }
5146: joinSize = newJoinSize;
5147: i = 1 - i;
5148: }
5149: if (joinSize) break;
5150: }
5151: *numCoveredPoints = joinSize;
5152: *coveredPoints = join[i];
5153: for (p = 0; p < numPoints; ++p) PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]));
5154: PetscCall(PetscFree(closures));
5155: PetscCall(DMRestoreWorkArray(dm, numPoints * (depth + 2), MPIU_INT, &offsets));
5156: PetscCall(DMRestoreWorkArray(dm, ms, MPIU_INT, &join[1 - i]));
5157: PetscFunctionReturn(PETSC_SUCCESS);
5158: }
5160: /*@C
5161: DMPlexGetMeet - Get an array for the meet of the set of points
5163: Not Collective
5165: Input Parameters:
5166: + dm - The `DMPLEX` object
5167: . numPoints - The number of input points for the meet
5168: - points - The input points, of length `numPoints`
5170: Output Parameters:
5171: + numCoveringPoints - The number of points in the meet
5172: - coveringPoints - The points in the meet, of length `numCoveringPoints`
5174: Level: intermediate
5176: Note:
5177: Currently, this is restricted to a single level meet
5179: Fortran Note:
5180: `coveringPoints` must be declared with
5181: .vb
5182: PetscInt, pointer :: coveringPoints(:)
5183: .ve
5185: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreMeet()`, `DMPlexGetJoin()`
5186: @*/
5187: PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt *coveringPoints[])
5188: {
5189: DM_Plex *mesh = (DM_Plex *)dm->data;
5190: PetscInt *meet[2];
5191: PetscInt meetSize, i = 0;
5192: PetscInt dof, off, p, c, m;
5193: PetscInt maxConeSize;
5195: PetscFunctionBegin;
5197: PetscAssertPointer(points, 3);
5198: PetscAssertPointer(numCoveringPoints, 4);
5199: PetscAssertPointer(coveringPoints, 5);
5200: PetscCall(PetscSectionGetMaxDof(mesh->coneSection, &maxConeSize));
5201: PetscCall(DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[0]));
5202: PetscCall(DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[1]));
5203: /* Copy in cone of first point */
5204: PetscCall(PetscSectionGetDof(mesh->coneSection, points[0], &dof));
5205: PetscCall(PetscSectionGetOffset(mesh->coneSection, points[0], &off));
5206: for (meetSize = 0; meetSize < dof; ++meetSize) meet[i][meetSize] = mesh->cones[off + meetSize];
5207: /* Check each successive cone */
5208: for (p = 1; p < numPoints; ++p) {
5209: PetscInt newMeetSize = 0;
5211: PetscCall(PetscSectionGetDof(mesh->coneSection, points[p], &dof));
5212: PetscCall(PetscSectionGetOffset(mesh->coneSection, points[p], &off));
5213: for (c = 0; c < dof; ++c) {
5214: const PetscInt point = mesh->cones[off + c];
5216: for (m = 0; m < meetSize; ++m) {
5217: if (point == meet[i][m]) {
5218: meet[1 - i][newMeetSize++] = point;
5219: break;
5220: }
5221: }
5222: }
5223: meetSize = newMeetSize;
5224: i = 1 - i;
5225: }
5226: *numCoveringPoints = meetSize;
5227: *coveringPoints = meet[i];
5228: PetscCall(DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &meet[1 - i]));
5229: PetscFunctionReturn(PETSC_SUCCESS);
5230: }
5232: /*@C
5233: DMPlexRestoreMeet - Restore an array for the meet of the set of points obtained with `DMPlexGetMeet()`
5235: Not Collective
5237: Input Parameters:
5238: + dm - The `DMPLEX` object
5239: . numPoints - The number of input points for the meet
5240: - points - The input points
5242: Output Parameters:
5243: + numCoveredPoints - The number of points in the meet
5244: - coveredPoints - The points in the meet
5246: Level: intermediate
5248: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetMeet()`, `DMPlexGetFullMeet()`, `DMPlexGetJoin()`
5249: @*/
5250: PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
5251: {
5252: PetscFunctionBegin;
5254: if (points) PetscAssertPointer(points, 3);
5255: if (numCoveredPoints) PetscAssertPointer(numCoveredPoints, 4);
5256: PetscAssertPointer(coveredPoints, 5);
5257: PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, (void *)coveredPoints));
5258: if (numCoveredPoints) *numCoveredPoints = 0;
5259: PetscFunctionReturn(PETSC_SUCCESS);
5260: }
5262: /*@C
5263: DMPlexGetFullMeet - Get an array for the meet of the set of points
5265: Not Collective
5267: Input Parameters:
5268: + dm - The `DMPLEX` object
5269: . numPoints - The number of input points for the meet
5270: - points - The input points, of length `numPoints`
5272: Output Parameters:
5273: + numCoveredPoints - The number of points in the meet
5274: - coveredPoints - The points in the meet, of length `numCoveredPoints`
5276: Level: intermediate
5278: Fortran Notes:
5279: `coveredPoints` must be declared with
5280: .vb
5281: PetscInt, pointer :: coveredPoints(:)
5282: .ve
5284: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetMeet()`, `DMPlexRestoreMeet()`, `DMPlexGetJoin()`
5285: @*/
5286: PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
5287: {
5288: PetscInt *offsets, **closures;
5289: PetscInt *meet[2];
5290: PetscInt height = 0, maxSize, meetSize = 0, i = 0;
5291: PetscInt p, h, c, m, mc;
5293: PetscFunctionBegin;
5295: PetscAssertPointer(points, 3);
5296: PetscAssertPointer(numCoveredPoints, 4);
5297: PetscAssertPointer(coveredPoints, 5);
5299: PetscCall(DMPlexGetDepth(dm, &height));
5300: PetscCall(PetscMalloc1(numPoints, &closures));
5301: PetscCall(DMGetWorkArray(dm, numPoints * (height + 2), MPIU_INT, &offsets));
5302: PetscCall(DMPlexGetMaxSizes(dm, &mc, NULL));
5303: maxSize = (mc > 1) ? ((PetscPowInt(mc, height + 1) - 1) / (mc - 1)) : height + 1;
5304: PetscCall(DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]));
5305: PetscCall(DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]));
5307: for (p = 0; p < numPoints; ++p) {
5308: PetscInt closureSize;
5310: PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]));
5312: offsets[p * (height + 2) + 0] = 0;
5313: for (h = 0; h < height + 1; ++h) {
5314: PetscInt pStart, pEnd, i;
5316: PetscCall(DMPlexGetHeightStratum(dm, h, &pStart, &pEnd));
5317: for (i = offsets[p * (height + 2) + h]; i < closureSize; ++i) {
5318: if ((pStart > closures[p][i * 2]) || (pEnd <= closures[p][i * 2])) {
5319: offsets[p * (height + 2) + h + 1] = i;
5320: break;
5321: }
5322: }
5323: if (i == closureSize) offsets[p * (height + 2) + h + 1] = i;
5324: }
5325: PetscCheck(offsets[p * (height + 2) + height + 1] == closureSize, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %" PetscInt_FMT " should be %" PetscInt_FMT, offsets[p * (height + 2) + height + 1], closureSize);
5326: }
5327: for (h = 0; h < height + 1; ++h) {
5328: PetscInt dof;
5330: /* Copy in cone of first point */
5331: dof = offsets[h + 1] - offsets[h];
5332: for (meetSize = 0; meetSize < dof; ++meetSize) meet[i][meetSize] = closures[0][(offsets[h] + meetSize) * 2];
5333: /* Check each successive cone */
5334: for (p = 1; p < numPoints && meetSize; ++p) {
5335: PetscInt newMeetSize = 0;
5337: dof = offsets[p * (height + 2) + h + 1] - offsets[p * (height + 2) + h];
5338: for (c = 0; c < dof; ++c) {
5339: const PetscInt point = closures[p][(offsets[p * (height + 2) + h] + c) * 2];
5341: for (m = 0; m < meetSize; ++m) {
5342: if (point == meet[i][m]) {
5343: meet[1 - i][newMeetSize++] = point;
5344: break;
5345: }
5346: }
5347: }
5348: meetSize = newMeetSize;
5349: i = 1 - i;
5350: }
5351: if (meetSize) break;
5352: }
5353: *numCoveredPoints = meetSize;
5354: *coveredPoints = meet[i];
5355: for (p = 0; p < numPoints; ++p) PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]));
5356: PetscCall(PetscFree(closures));
5357: PetscCall(DMRestoreWorkArray(dm, numPoints * (height + 2), MPIU_INT, &offsets));
5358: PetscCall(DMRestoreWorkArray(dm, mc, MPIU_INT, &meet[1 - i]));
5359: PetscFunctionReturn(PETSC_SUCCESS);
5360: }
5362: /*@
5363: DMPlexEqual - Determine if two `DM` have the same topology
5365: Not Collective
5367: Input Parameters:
5368: + dmA - A `DMPLEX` object
5369: - dmB - A `DMPLEX` object
5371: Output Parameter:
5372: . equal - `PETSC_TRUE` if the topologies are identical
5374: Level: intermediate
5376: Note:
5377: We are not solving graph isomorphism, so we do not permute.
5379: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
5380: @*/
5381: PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
5382: {
5383: PetscInt depth, depthB, pStart, pEnd, pStartB, pEndB, p;
5385: PetscFunctionBegin;
5388: PetscAssertPointer(equal, 3);
5390: *equal = PETSC_FALSE;
5391: PetscCall(DMPlexGetDepth(dmA, &depth));
5392: PetscCall(DMPlexGetDepth(dmB, &depthB));
5393: if (depth != depthB) PetscFunctionReturn(PETSC_SUCCESS);
5394: PetscCall(DMPlexGetChart(dmA, &pStart, &pEnd));
5395: PetscCall(DMPlexGetChart(dmB, &pStartB, &pEndB));
5396: if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(PETSC_SUCCESS);
5397: for (p = pStart; p < pEnd; ++p) {
5398: const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
5399: PetscInt coneSize, coneSizeB, c, supportSize, supportSizeB, s;
5401: PetscCall(DMPlexGetConeSize(dmA, p, &coneSize));
5402: PetscCall(DMPlexGetCone(dmA, p, &cone));
5403: PetscCall(DMPlexGetConeOrientation(dmA, p, &ornt));
5404: PetscCall(DMPlexGetConeSize(dmB, p, &coneSizeB));
5405: PetscCall(DMPlexGetCone(dmB, p, &coneB));
5406: PetscCall(DMPlexGetConeOrientation(dmB, p, &orntB));
5407: if (coneSize != coneSizeB) PetscFunctionReturn(PETSC_SUCCESS);
5408: for (c = 0; c < coneSize; ++c) {
5409: if (cone[c] != coneB[c]) PetscFunctionReturn(PETSC_SUCCESS);
5410: if (ornt[c] != orntB[c]) PetscFunctionReturn(PETSC_SUCCESS);
5411: }
5412: PetscCall(DMPlexGetSupportSize(dmA, p, &supportSize));
5413: PetscCall(DMPlexGetSupport(dmA, p, &support));
5414: PetscCall(DMPlexGetSupportSize(dmB, p, &supportSizeB));
5415: PetscCall(DMPlexGetSupport(dmB, p, &supportB));
5416: if (supportSize != supportSizeB) PetscFunctionReturn(PETSC_SUCCESS);
5417: for (s = 0; s < supportSize; ++s) {
5418: if (support[s] != supportB[s]) PetscFunctionReturn(PETSC_SUCCESS);
5419: }
5420: }
5421: *equal = PETSC_TRUE;
5422: PetscFunctionReturn(PETSC_SUCCESS);
5423: }
5425: /*@
5426: DMPlexGetNumFaceVertices - Returns the number of vertices on a face
5428: Not Collective
5430: Input Parameters:
5431: + dm - The `DMPLEX`
5432: . cellDim - The cell dimension
5433: - numCorners - The number of vertices on a cell
5435: Output Parameter:
5436: . numFaceVertices - The number of vertices on a face
5438: Level: developer
5440: Note:
5441: Of course this can only work for a restricted set of symmetric shapes
5443: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
5444: @*/
5445: PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
5446: {
5447: MPI_Comm comm;
5449: PetscFunctionBegin;
5450: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5451: PetscAssertPointer(numFaceVertices, 4);
5452: switch (cellDim) {
5453: case 0:
5454: *numFaceVertices = 0;
5455: break;
5456: case 1:
5457: *numFaceVertices = 1;
5458: break;
5459: case 2:
5460: switch (numCorners) {
5461: case 3: /* triangle */
5462: *numFaceVertices = 2; /* Edge has 2 vertices */
5463: break;
5464: case 4: /* quadrilateral */
5465: *numFaceVertices = 2; /* Edge has 2 vertices */
5466: break;
5467: case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
5468: *numFaceVertices = 3; /* Edge has 3 vertices */
5469: break;
5470: case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
5471: *numFaceVertices = 3; /* Edge has 3 vertices */
5472: break;
5473: default:
5474: SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %" PetscInt_FMT " for dimension %" PetscInt_FMT, numCorners, cellDim);
5475: }
5476: break;
5477: case 3:
5478: switch (numCorners) {
5479: case 4: /* tetradehdron */
5480: *numFaceVertices = 3; /* Face has 3 vertices */
5481: break;
5482: case 6: /* tet cohesive cells */
5483: *numFaceVertices = 4; /* Face has 4 vertices */
5484: break;
5485: case 8: /* hexahedron */
5486: *numFaceVertices = 4; /* Face has 4 vertices */
5487: break;
5488: case 9: /* tet cohesive Lagrange cells */
5489: *numFaceVertices = 6; /* Face has 6 vertices */
5490: break;
5491: case 10: /* quadratic tetrahedron */
5492: *numFaceVertices = 6; /* Face has 6 vertices */
5493: break;
5494: case 12: /* hex cohesive Lagrange cells */
5495: *numFaceVertices = 6; /* Face has 6 vertices */
5496: break;
5497: case 18: /* quadratic tet cohesive Lagrange cells */
5498: *numFaceVertices = 6; /* Face has 6 vertices */
5499: break;
5500: case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
5501: *numFaceVertices = 9; /* Face has 9 vertices */
5502: break;
5503: default:
5504: SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %" PetscInt_FMT " for dimension %" PetscInt_FMT, numCorners, cellDim);
5505: }
5506: break;
5507: default:
5508: SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %" PetscInt_FMT, cellDim);
5509: }
5510: PetscFunctionReturn(PETSC_SUCCESS);
5511: }
5513: /*@
5514: DMPlexGetDepthLabel - Get the `DMLabel` recording the depth of each point
5516: Not Collective
5518: Input Parameter:
5519: . dm - The `DMPLEX` object
5521: Output Parameter:
5522: . depthLabel - The `DMLabel` recording point depth
5524: Level: developer
5526: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepth()`, `DMPlexGetHeightStratum()`, `DMPlexGetDepthStratum()`, `DMPlexGetPointDepth()`
5527: @*/
5528: PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
5529: {
5530: PetscFunctionBegin;
5532: PetscAssertPointer(depthLabel, 2);
5533: *depthLabel = dm->depthLabel;
5534: PetscFunctionReturn(PETSC_SUCCESS);
5535: }
5537: /*@
5538: DMPlexGetDepth - Get the depth of the DAG representing this mesh
5540: Not Collective
5542: Input Parameter:
5543: . dm - The `DMPLEX` object
5545: Output Parameter:
5546: . depth - The number of strata (breadth first levels) in the DAG
5548: Level: developer
5550: Notes:
5551: This returns maximum of point depths over all points, i.e. maximum value of the label returned by `DMPlexGetDepthLabel()`.
5553: The point depth is described more in detail in `DMPlexGetDepthStratum()`.
5555: An empty mesh gives -1.
5557: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepthLabel()`, `DMPlexGetDepthStratum()`, `DMPlexGetPointDepth()`, `DMPlexSymmetrize()`
5558: @*/
5559: PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
5560: {
5561: DM_Plex *mesh = (DM_Plex *)dm->data;
5562: DMLabel label;
5563: PetscInt d = -1;
5565: PetscFunctionBegin;
5567: PetscAssertPointer(depth, 2);
5568: if (mesh->tr) {
5569: PetscCall(DMPlexTransformGetDepth(mesh->tr, depth));
5570: } else {
5571: PetscCall(DMPlexGetDepthLabel(dm, &label));
5572: // Allow missing depths
5573: if (label) PetscCall(DMLabelGetValueBounds(label, NULL, &d));
5574: *depth = d;
5575: }
5576: PetscFunctionReturn(PETSC_SUCCESS);
5577: }
5579: /*@
5580: DMPlexGetDepthStratum - Get the bounds [`start`, `end`) for all points at a certain depth.
5582: Not Collective
5584: Input Parameters:
5585: + dm - The `DMPLEX` object
5586: - depth - The requested depth
5588: Output Parameters:
5589: + start - The first point at this `depth`
5590: - end - One beyond the last point at this `depth`
5592: Level: developer
5594: Notes:
5595: Depth indexing is related to topological dimension. Depth stratum 0 contains the lowest topological dimension points,
5596: often "vertices". If the mesh is "interpolated" (see `DMPlexInterpolate()`), then depth stratum 1 contains the next
5597: higher dimension, e.g., "edges".
5599: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetHeightStratum()`, `DMPlexGetCellTypeStratum()`, `DMPlexGetDepth()`, `DMPlexGetDepthLabel()`, `DMPlexGetPointDepth()`, `DMPlexSymmetrize()`, `DMPlexInterpolate()`
5600: @*/
5601: PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt depth, PeOp PetscInt *start, PeOp PetscInt *end)
5602: {
5603: DM_Plex *mesh = (DM_Plex *)dm->data;
5604: DMLabel label;
5605: PetscInt pStart, pEnd;
5607: PetscFunctionBegin;
5609: if (start) {
5610: PetscAssertPointer(start, 3);
5611: *start = 0;
5612: }
5613: if (end) {
5614: PetscAssertPointer(end, 4);
5615: *end = 0;
5616: }
5617: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5618: if (pStart == pEnd) PetscFunctionReturn(PETSC_SUCCESS);
5619: if (depth < 0) {
5620: if (start) *start = pStart;
5621: if (end) *end = pEnd;
5622: PetscFunctionReturn(PETSC_SUCCESS);
5623: }
5624: if (mesh->tr) {
5625: PetscCall(DMPlexTransformGetDepthStratum(mesh->tr, depth, start, end));
5626: } else {
5627: PetscCall(DMPlexGetDepthLabel(dm, &label));
5628: PetscCheck(label, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
5629: PetscCall(DMLabelGetStratumBounds(label, depth, start, end));
5630: }
5631: PetscFunctionReturn(PETSC_SUCCESS);
5632: }
5634: /*@
5635: DMPlexGetHeightStratum - Get the bounds [`start`, `end`) for all points at a certain height.
5637: Not Collective
5639: Input Parameters:
5640: + dm - The `DMPLEX` object
5641: - height - The requested height
5643: Output Parameters:
5644: + start - The first point at this `height`
5645: - end - One beyond the last point at this `height`
5647: Level: developer
5649: Notes:
5650: Height indexing is related to topological codimension. Height stratum 0 contains the highest topological dimension
5651: points, often called "cells" or "elements". If the mesh is "interpolated" (see `DMPlexInterpolate()`), then height
5652: stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
5654: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetCellTypeStratum()`, `DMPlexGetDepth()`, `DMPlexGetPointHeight()`
5655: @*/
5656: PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt height, PeOp PetscInt *start, PeOp PetscInt *end)
5657: {
5658: DMLabel label;
5659: PetscInt depth, pStart, pEnd;
5661: PetscFunctionBegin;
5663: if (start) {
5664: PetscAssertPointer(start, 3);
5665: *start = 0;
5666: }
5667: if (end) {
5668: PetscAssertPointer(end, 4);
5669: *end = 0;
5670: }
5671: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5672: if (pStart == pEnd) PetscFunctionReturn(PETSC_SUCCESS);
5673: if (height < 0) {
5674: if (start) *start = pStart;
5675: if (end) *end = pEnd;
5676: PetscFunctionReturn(PETSC_SUCCESS);
5677: }
5678: PetscCall(DMPlexGetDepthLabel(dm, &label));
5679: if (label) PetscCall(DMLabelGetNumValues(label, &depth));
5680: else PetscCall(DMGetDimension(dm, &depth));
5681: PetscCheck(depth >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Depth not yet computed");
5682: PetscCall(DMPlexGetDepthStratum(dm, depth - 1 - height, start, end));
5683: PetscFunctionReturn(PETSC_SUCCESS);
5684: }
5686: /*@
5687: DMPlexGetPointDepth - Get the `depth` of a given point
5689: Not Collective
5691: Input Parameters:
5692: + dm - The `DMPLEX` object
5693: - point - The point
5695: Output Parameter:
5696: . depth - The depth of the `point`
5698: Level: intermediate
5700: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexGetPointHeight()`
5701: @*/
5702: PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
5703: {
5704: PetscFunctionBegin;
5706: PetscAssertPointer(depth, 3);
5707: PetscCall(DMLabelGetValue(dm->depthLabel, point, depth));
5708: PetscFunctionReturn(PETSC_SUCCESS);
5709: }
5711: /*@
5712: DMPlexGetPointHeight - Get the `height` of a given point
5714: Not Collective
5716: Input Parameters:
5717: + dm - The `DMPLEX` object
5718: - point - The point
5720: Output Parameter:
5721: . height - The height of the `point`
5723: Level: intermediate
5725: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexGetPointDepth()`
5726: @*/
5727: PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
5728: {
5729: PetscInt n, pDepth;
5731: PetscFunctionBegin;
5733: PetscAssertPointer(height, 3);
5734: PetscCall(DMLabelGetNumValues(dm->depthLabel, &n));
5735: PetscCall(DMLabelGetValue(dm->depthLabel, point, &pDepth));
5736: *height = n - 1 - pDepth; /* DAG depth is n-1 */
5737: PetscFunctionReturn(PETSC_SUCCESS);
5738: }
5740: /*@
5741: DMPlexGetCellTypeLabel - Get the `DMLabel` recording the polytope type of each cell
5743: Not Collective
5745: Input Parameter:
5746: . dm - The `DMPLEX` object
5748: Output Parameter:
5749: . celltypeLabel - The `DMLabel` recording cell polytope type
5751: Level: developer
5753: Note:
5754: This function will trigger automatica computation of cell types. This can be disabled by calling
5755: `DMCreateLabel`(dm, "celltype") beforehand.
5757: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMCreateLabel()`
5758: @*/
5759: PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
5760: {
5761: PetscFunctionBegin;
5763: PetscAssertPointer(celltypeLabel, 2);
5764: if (!dm->celltypeLabel) PetscCall(DMPlexComputeCellTypes(dm));
5765: *celltypeLabel = dm->celltypeLabel;
5766: PetscFunctionReturn(PETSC_SUCCESS);
5767: }
5769: /*@
5770: DMPlexGetCellType - Get the polytope type of a given cell
5772: Not Collective
5774: Input Parameters:
5775: + dm - The `DMPLEX` object
5776: - cell - The cell
5778: Output Parameter:
5779: . celltype - The polytope type of the cell
5781: Level: intermediate
5783: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPolytopeType`, `DMPlexGetCellTypeLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`
5784: @*/
5785: PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
5786: {
5787: DM_Plex *mesh = (DM_Plex *)dm->data;
5788: DMLabel label;
5789: PetscInt ct;
5791: PetscFunctionBegin;
5793: PetscAssertPointer(celltype, 3);
5794: if (mesh->tr) {
5795: PetscCall(DMPlexTransformGetCellType(mesh->tr, cell, celltype));
5796: } else {
5797: PetscInt pStart, pEnd;
5799: PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, NULL));
5800: if (!mesh->cellTypes) { /* XXX remove? optimize? */
5801: PetscCall(PetscSectionGetChart(mesh->coneSection, NULL, &pEnd));
5802: if (pEnd <= pStart) {
5803: *celltype = DM_POLYTOPE_UNKNOWN;
5804: PetscFunctionReturn(PETSC_SUCCESS);
5805: }
5806: PetscCall(PetscMalloc1(pEnd - pStart, &mesh->cellTypes));
5807: PetscCall(DMPlexGetCellTypeLabel(dm, &label));
5808: for (PetscInt p = pStart; p < pEnd; p++) {
5809: PetscCall(DMLabelGetValue(label, p, &ct));
5810: mesh->cellTypes[p - pStart].value_as_uint8 = (uint8_t)ct;
5811: }
5812: }
5813: *celltype = (DMPolytopeType)mesh->cellTypes[cell - pStart].value_as_uint8;
5814: if (PetscDefined(USE_DEBUG)) {
5815: PetscCall(DMPlexGetCellTypeLabel(dm, &label));
5816: PetscCall(DMLabelGetValue(label, cell, &ct));
5817: PetscCheck(ct >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " has not been assigned a cell type", cell);
5818: PetscCheck(ct == (PetscInt)*celltype, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid cellType for %" PetscInt_FMT ": %d != %" PetscInt_FMT, cell, (int)*celltype, ct);
5819: }
5820: }
5821: PetscFunctionReturn(PETSC_SUCCESS);
5822: }
5824: /*@
5825: DMPlexSetCellType - Set the polytope type of a given cell
5827: Not Collective
5829: Input Parameters:
5830: + dm - The `DMPLEX` object
5831: . cell - The cell
5832: - celltype - The polytope type of the cell
5834: Level: advanced
5836: Note:
5837: By default, cell types will be automatically computed using `DMPlexComputeCellTypes()` before this function
5838: is executed. This function will override the computed type. However, if automatic classification will not succeed
5839: and a user wants to manually specify all types, the classification must be disabled by calling
5840: DMCreateLabel(dm, "celltype") before getting or setting any cell types.
5842: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellTypeLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexComputeCellTypes()`, `DMCreateLabel()`
5843: @*/
5844: PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
5845: {
5846: DM_Plex *mesh = (DM_Plex *)dm->data;
5847: DMLabel label;
5848: PetscInt pStart, pEnd;
5850: PetscFunctionBegin;
5852: PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
5853: PetscCall(DMPlexGetCellTypeLabel(dm, &label));
5854: PetscCall(DMLabelSetValue(label, cell, celltype));
5855: if (!mesh->cellTypes) PetscCall(PetscMalloc1(pEnd - pStart, &mesh->cellTypes));
5856: mesh->cellTypes[cell - pStart].value_as_uint8 = (uint8_t)celltype;
5857: PetscFunctionReturn(PETSC_SUCCESS);
5858: }
5860: PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
5861: {
5862: PetscSection section;
5863: PetscInt maxHeight;
5864: const char *prefix;
5866: PetscFunctionBegin;
5867: PetscCall(DMClone(dm, cdm));
5868: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
5869: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*cdm, prefix));
5870: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)*cdm, "cdm_"));
5871: PetscCall(DMPlexGetMaxProjectionHeight(dm, &maxHeight));
5872: PetscCall(DMPlexSetMaxProjectionHeight(*cdm, maxHeight));
5873: PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ion));
5874: PetscCall(DMSetLocalSection(*cdm, section));
5875: PetscCall(PetscSectionDestroy(§ion));
5877: PetscCall(DMSetNumFields(*cdm, 1));
5878: PetscCall(DMCreateDS(*cdm));
5879: (*cdm)->cloneOpts = PETSC_TRUE;
5880: if (dm->setfromoptionscalled) PetscCall(DMSetFromOptions(*cdm));
5881: PetscFunctionReturn(PETSC_SUCCESS);
5882: }
5884: PetscErrorCode DMCreateCellCoordinateDM_Plex(DM dm, DM *cdm)
5885: {
5886: DM cgcdm;
5887: PetscSection section;
5888: const char *prefix;
5890: PetscFunctionBegin;
5891: PetscCall(DMGetCoordinateDM(dm, &cgcdm));
5892: PetscCall(DMClone(cgcdm, cdm));
5893: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
5894: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*cdm, prefix));
5895: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)*cdm, "cellcdm_"));
5896: PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ion));
5897: PetscCall(DMSetLocalSection(*cdm, section));
5898: PetscCall(PetscSectionDestroy(§ion));
5899: PetscCall(DMSetNumFields(*cdm, 1));
5900: PetscCall(DMCreateDS(*cdm));
5901: (*cdm)->cloneOpts = PETSC_TRUE;
5902: if (dm->setfromoptionscalled) PetscCall(DMSetFromOptions(*cdm));
5903: PetscFunctionReturn(PETSC_SUCCESS);
5904: }
5906: PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
5907: {
5908: Vec coordsLocal, cellCoordsLocal;
5909: DM coordsDM, cellCoordsDM;
5911: PetscFunctionBegin;
5912: *field = NULL;
5913: PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
5914: PetscCall(DMGetCoordinateDM(dm, &coordsDM));
5915: PetscCall(DMGetCellCoordinatesLocal(dm, &cellCoordsLocal));
5916: PetscCall(DMGetCellCoordinateDM(dm, &cellCoordsDM));
5917: if (coordsLocal && coordsDM) {
5918: if (cellCoordsLocal && cellCoordsDM) PetscCall(DMFieldCreateDSWithDG(coordsDM, cellCoordsDM, 0, coordsLocal, cellCoordsLocal, field));
5919: else PetscCall(DMFieldCreateDS(coordsDM, 0, coordsLocal, field));
5920: }
5921: PetscFunctionReturn(PETSC_SUCCESS);
5922: }
5924: /*@
5925: DMPlexGetConeSection - Return a section which describes the layout of cone data
5927: Not Collective
5929: Input Parameter:
5930: . dm - The `DMPLEX` object
5932: Output Parameter:
5933: . section - The `PetscSection` object
5935: Level: developer
5937: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSupportSection()`, `DMPlexGetCones()`, `DMPlexGetConeOrientations()`, `PetscSection`
5938: @*/
5939: PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
5940: {
5941: DM_Plex *mesh = (DM_Plex *)dm->data;
5943: PetscFunctionBegin;
5945: if (section) *section = mesh->coneSection;
5946: PetscFunctionReturn(PETSC_SUCCESS);
5947: }
5949: /*@
5950: DMPlexGetSupportSection - Return a section which describes the layout of support data
5952: Not Collective
5954: Input Parameter:
5955: . dm - The `DMPLEX` object
5957: Output Parameter:
5958: . section - The `PetscSection` object
5960: Level: developer
5962: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`, `PetscSection`
5963: @*/
5964: PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
5965: {
5966: DM_Plex *mesh = (DM_Plex *)dm->data;
5968: PetscFunctionBegin;
5970: if (section) *section = mesh->supportSection;
5971: PetscFunctionReturn(PETSC_SUCCESS);
5972: }
5974: /*@C
5975: DMPlexGetCones - Return cone data
5977: Not Collective
5979: Input Parameter:
5980: . dm - The `DMPLEX` object
5982: Output Parameter:
5983: . cones - The cone for each point
5985: Level: developer
5987: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`
5988: @*/
5989: PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
5990: {
5991: DM_Plex *mesh = (DM_Plex *)dm->data;
5993: PetscFunctionBegin;
5995: if (cones) *cones = mesh->cones;
5996: PetscFunctionReturn(PETSC_SUCCESS);
5997: }
5999: /*@C
6000: DMPlexGetConeOrientations - Return cone orientation data
6002: Not Collective
6004: Input Parameter:
6005: . dm - The `DMPLEX` object
6007: Output Parameter:
6008: . coneOrientations - The array of cone orientations for all points
6010: Level: developer
6012: Notes:
6013: The `PetscSection` returned by `DMPlexGetConeSection()` partitions coneOrientations into cone orientations of particular points
6014: as returned by `DMPlexGetConeOrientation()`.
6016: The meaning of coneOrientations values is detailed in `DMPlexGetConeOrientation()`.
6018: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`, `DMPlexGetConeOrientation()`, `PetscSection`
6019: @*/
6020: PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
6021: {
6022: DM_Plex *mesh = (DM_Plex *)dm->data;
6024: PetscFunctionBegin;
6026: if (coneOrientations) *coneOrientations = mesh->coneOrientations;
6027: PetscFunctionReturn(PETSC_SUCCESS);
6028: }
6030: /* FEM Support */
6032: PetscErrorCode DMPlexGetAllCells_Internal(DM plex, IS *cellIS)
6033: {
6034: PetscInt depth;
6036: PetscFunctionBegin;
6037: PetscCall(DMPlexGetDepth(plex, &depth));
6038: PetscCall(DMGetStratumIS(plex, "dim", depth, cellIS));
6039: if (!*cellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, cellIS));
6040: PetscFunctionReturn(PETSC_SUCCESS);
6041: }
6043: PetscErrorCode DMPlexGetAllFaces_Internal(DM plex, IS *faceIS)
6044: {
6045: PetscInt depth;
6047: PetscFunctionBegin;
6048: PetscCall(DMPlexGetDepth(plex, &depth));
6049: PetscCall(DMGetStratumIS(plex, "dim", depth - 1, faceIS));
6050: if (!*faceIS) PetscCall(DMGetStratumIS(plex, "depth", depth - 1, faceIS));
6051: PetscFunctionReturn(PETSC_SUCCESS);
6052: }
6054: /*
6055: Returns number of components and tensor degree for the field. For interpolated meshes, line should be a point
6056: representing a line in the section.
6057: */
6058: static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(DM dm, PetscSection section, PetscInt field, PetscInt line, PetscInt *Nc, PetscInt *k, PetscBool *continuous, PetscBool *tensor)
6059: {
6060: PetscObject obj;
6061: PetscClassId id;
6062: PetscFE fe = NULL;
6064: PetscFunctionBeginHot;
6065: PetscCall(PetscSectionGetFieldComponents(section, field, Nc));
6066: PetscCall(DMGetField(dm, field, NULL, &obj));
6067: PetscCall(PetscObjectGetClassId(obj, &id));
6068: if (id == PETSCFE_CLASSID) fe = (PetscFE)obj;
6070: if (!fe) {
6071: /* Assume the full interpolated mesh is in the chart; lines in particular */
6072: /* An order k SEM disc has k-1 dofs on an edge */
6073: PetscCall(PetscSectionGetFieldDof(section, line, field, k));
6074: *k = *k / *Nc + 1;
6075: } else {
6076: PetscInt dual_space_size, dim;
6077: PetscDualSpace dsp;
6079: PetscCall(DMGetDimension(dm, &dim));
6080: PetscCall(PetscFEGetDualSpace(fe, &dsp));
6081: PetscCall(PetscDualSpaceGetDimension(dsp, &dual_space_size));
6082: PetscCall(PetscDualSpaceLagrangeGetContinuity(dsp, continuous));
6083: PetscCall(PetscDualSpaceLagrangeGetTensor(dsp, tensor));
6084: if (*tensor) {
6085: *k = (PetscInt)PetscCeilReal(PetscPowReal(dual_space_size / *Nc, 1.0 / dim)) - 1;
6086: } else {
6087: switch (dim) {
6088: case 1:
6089: *k = (dual_space_size / *Nc) - 1;
6090: break;
6091: case 2:
6092: // N = (k + 1) (k + 2) / 2, k^2 + 3 k - 2 (N - 1) = 0, k = (sqrt(8 N + 1) - 3) / 2
6093: *k = (PetscInt)PetscCeilReal((PetscSqrtReal(8 * dual_space_size / *Nc + 1) - 3) / 2);
6094: break;
6095: case 3: {
6096: // N = (k + 1) (k + 2) (k + 3) / 6, k = (sqrt(3) sqrt(243 N^2 - 1) + 27 N)^(1/3)/3^(2/3) + 1/(3^(1/3) (sqrt(3) sqrt(243 N^2 - 1) + 27 N)^(1/3)) - 2
6097: PetscInt N = dual_space_size / *Nc;
6098: *k = (PetscInt)PetscCeilReal(PetscPowReal((PetscSqrtReal(3 * (243 * N * N - 1)) + 27 * N) / 9, 1.0 / 3.0) + 1 / PetscPowReal(3 * (PetscSqrtReal(3 * (243 * N * N - 1)) + 27 * N), 1.0 / 3.0)) - 2;
6099: } break;
6100: default:
6101: *k = -1;
6102: }
6103: }
6104: }
6105: PetscFunctionReturn(PETSC_SUCCESS);
6106: }
6108: static PetscErrorCode GetFieldSize_Private(PetscInt dim, PetscInt k, PetscBool tensor, PetscInt *dof)
6109: {
6110: PetscFunctionBeginHot;
6111: if (tensor) {
6112: *dof = PetscPowInt(k + 1, dim);
6113: } else {
6114: switch (dim) {
6115: case 1:
6116: *dof = k + 1;
6117: break;
6118: case 2:
6119: *dof = ((k + 1) * (k + 2)) / 2;
6120: break;
6121: case 3:
6122: *dof = ((k + 1) * (k + 2) * (k + 3)) / 6;
6123: break;
6124: default:
6125: *dof = 0;
6126: }
6127: }
6128: PetscFunctionReturn(PETSC_SUCCESS);
6129: }
6131: /*@
6132: DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
6133: lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
6134: section provided (or the section of the `DM`).
6136: Not Collective
6138: Input Parameters:
6139: + dm - The `DM`
6140: . point - Either a cell (highest dim point) or an edge (dim 1 point), or `PETSC_DETERMINE`
6141: - section - The `PetscSection` to reorder, or `NULL` for the default section
6143: Example:
6144: A typical interpolated single-quad mesh might order points as
6145: .vb
6146: [c0, v1, v2, v3, v4, e5, e6, e7, e8]
6148: v4 -- e6 -- v3
6149: | |
6150: e7 c0 e8
6151: | |
6152: v1 -- e5 -- v2
6153: .ve
6155: (There is no significance to the ordering described here.) The default section for a Q3 quad might typically assign
6156: dofs in the order of points, e.g.,
6157: .vb
6158: c0 -> [0,1,2,3]
6159: v1 -> [4]
6160: ...
6161: e5 -> [8, 9]
6162: .ve
6164: which corresponds to the dofs
6165: .vb
6166: 6 10 11 7
6167: 13 2 3 15
6168: 12 0 1 14
6169: 4 8 9 5
6170: .ve
6172: The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
6173: .vb
6174: 0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
6175: .ve
6177: After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
6178: .vb
6179: 4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
6180: .ve
6182: Level: developer
6184: Notes:
6185: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
6186: degree of the basis.
6188: This is required to run with libCEED.
6190: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetLocalSection()`, `PetscSectionSetClosurePermutation()`, `DMPlexSetClosurePermutationLexicographic()`, `DMSetGlobalSection()`
6191: @*/
6192: PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
6193: {
6194: DMLabel label;
6195: PetscInt dim, depth = -1, eStart = -1, Nf;
6196: PetscBool continuous = PETSC_TRUE, tensor = PETSC_TRUE;
6198: PetscFunctionBegin;
6201: PetscCall(DMGetDimension(dm, &dim));
6202: if (dim < 1) PetscFunctionReturn(PETSC_SUCCESS);
6203: if (point < 0) {
6204: PetscInt sStart, sEnd;
6206: PetscCall(DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd));
6207: point = sEnd - sStart ? sStart : point;
6208: }
6209: PetscCall(DMPlexGetDepthLabel(dm, &label));
6210: if (point >= 0) PetscCall(DMLabelGetValue(label, point, &depth));
6211: if (!section) PetscCall(DMGetLocalSection(dm, §ion));
6212: if (depth == 1) {
6213: eStart = point;
6214: } else if (depth == dim) {
6215: const PetscInt *cone;
6217: PetscCall(DMPlexGetCone(dm, point, &cone));
6218: if (dim == 2) eStart = cone[0];
6219: else if (dim == 3) {
6220: const PetscInt *cone2;
6221: PetscCall(DMPlexGetCone(dm, cone[0], &cone2));
6222: eStart = cone2[0];
6223: } 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);
6224: } else PetscCheck(depth < 0, 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);
6226: PetscCall(PetscSectionGetNumFields(section, &Nf));
6227: for (PetscInt d = 1; d <= dim; d++) {
6228: PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
6229: PetscInt *perm;
6231: for (f = 0; f < Nf; ++f) {
6232: PetscInt dof;
6234: PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6235: PetscCheck(dim == 1 || tensor || !continuous, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Continuous field %" PetscInt_FMT " must have a tensor product discretization", f);
6236: if (!continuous && d < dim) continue;
6237: PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6238: size += dof * Nc;
6239: }
6240: PetscCall(PetscMalloc1(size, &perm));
6241: for (f = 0; f < Nf; ++f) {
6242: switch (d) {
6243: case 1:
6244: PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6245: if (!continuous && d < dim) continue;
6246: /*
6247: Original ordering is [ edge of length k-1; vtx0; vtx1 ]
6248: We want [ vtx0; edge of length k-1; vtx1 ]
6249: */
6250: if (continuous) {
6251: for (c = 0; c < Nc; c++, offset++) perm[offset] = (k - 1) * Nc + c + foffset;
6252: for (i = 0; i < k - 1; i++)
6253: for (c = 0; c < Nc; c++, offset++) perm[offset] = i * Nc + c + foffset;
6254: for (c = 0; c < Nc; c++, offset++) perm[offset] = k * Nc + c + foffset;
6255: foffset = offset;
6256: } else {
6257: PetscInt dof;
6259: PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6260: for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6261: foffset = offset;
6262: }
6263: break;
6264: case 2:
6265: /* The original quad closure is oriented clockwise, {f, e_b, e_r, e_t, e_l, v_lb, v_rb, v_tr, v_tl} */
6266: PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6267: if (!continuous && d < dim) continue;
6268: /* The SEM order is
6270: v_lb, {e_b}, v_rb,
6271: e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
6272: v_lt, reverse {e_t}, v_rt
6273: */
6274: if (continuous) {
6275: const PetscInt of = 0;
6276: const PetscInt oeb = of + PetscSqr(k - 1);
6277: const PetscInt oer = oeb + (k - 1);
6278: const PetscInt oet = oer + (k - 1);
6279: const PetscInt oel = oet + (k - 1);
6280: const PetscInt ovlb = oel + (k - 1);
6281: const PetscInt ovrb = ovlb + 1;
6282: const PetscInt ovrt = ovrb + 1;
6283: const PetscInt ovlt = ovrt + 1;
6284: PetscInt o;
6286: /* bottom */
6287: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb * Nc + c + foffset;
6288: for (o = oeb; o < oer; ++o)
6289: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6290: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb * Nc + c + foffset;
6291: /* middle */
6292: for (i = 0; i < k - 1; ++i) {
6293: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel + (k - 2) - i) * Nc + c + foffset;
6294: for (o = of + (k - 1) * i; o < of + (k - 1) * (i + 1); ++o)
6295: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6296: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer + i) * Nc + c + foffset;
6297: }
6298: /* top */
6299: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt * Nc + c + foffset;
6300: for (o = oel - 1; o >= oet; --o)
6301: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6302: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt * Nc + c + foffset;
6303: foffset = offset;
6304: } else {
6305: PetscInt dof;
6307: PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6308: for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6309: foffset = offset;
6310: }
6311: break;
6312: case 3:
6313: /* The original hex closure is
6315: {c,
6316: f_b, f_t, f_f, f_b, f_r, f_l,
6317: e_bl, e_bb, e_br, e_bf, e_tf, e_tr, e_tb, e_tl, e_rf, e_lf, e_lb, e_rb,
6318: v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
6319: */
6320: PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6321: if (!continuous && d < dim) continue;
6322: /* The SEM order is
6323: Bottom Slice
6324: v_blf, {e^{(k-1)-n}_bf}, v_brf,
6325: e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
6326: v_blb, {e_bb}, v_brb,
6328: Middle Slice (j)
6329: {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
6330: f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
6331: e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
6333: Top Slice
6334: v_tlf, {e_tf}, v_trf,
6335: e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
6336: v_tlb, {e^{(k-1)-n}_tb}, v_trb,
6337: */
6338: if (continuous) {
6339: const PetscInt oc = 0;
6340: const PetscInt ofb = oc + PetscSqr(k - 1) * (k - 1);
6341: const PetscInt oft = ofb + PetscSqr(k - 1);
6342: const PetscInt off = oft + PetscSqr(k - 1);
6343: const PetscInt ofk = off + PetscSqr(k - 1);
6344: const PetscInt ofr = ofk + PetscSqr(k - 1);
6345: const PetscInt ofl = ofr + PetscSqr(k - 1);
6346: const PetscInt oebl = ofl + PetscSqr(k - 1);
6347: const PetscInt oebb = oebl + (k - 1);
6348: const PetscInt oebr = oebb + (k - 1);
6349: const PetscInt oebf = oebr + (k - 1);
6350: const PetscInt oetf = oebf + (k - 1);
6351: const PetscInt oetr = oetf + (k - 1);
6352: const PetscInt oetb = oetr + (k - 1);
6353: const PetscInt oetl = oetb + (k - 1);
6354: const PetscInt oerf = oetl + (k - 1);
6355: const PetscInt oelf = oerf + (k - 1);
6356: const PetscInt oelb = oelf + (k - 1);
6357: const PetscInt oerb = oelb + (k - 1);
6358: const PetscInt ovblf = oerb + (k - 1);
6359: const PetscInt ovblb = ovblf + 1;
6360: const PetscInt ovbrb = ovblb + 1;
6361: const PetscInt ovbrf = ovbrb + 1;
6362: const PetscInt ovtlf = ovbrf + 1;
6363: const PetscInt ovtrf = ovtlf + 1;
6364: const PetscInt ovtrb = ovtrf + 1;
6365: const PetscInt ovtlb = ovtrb + 1;
6366: PetscInt o, n;
6368: /* Bottom Slice */
6369: /* bottom */
6370: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf * Nc + c + foffset;
6371: for (o = oetf - 1; o >= oebf; --o)
6372: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6373: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf * Nc + c + foffset;
6374: /* middle */
6375: for (i = 0; i < k - 1; ++i) {
6376: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl + i) * Nc + c + foffset;
6377: for (n = 0; n < k - 1; ++n) {
6378: o = ofb + n * (k - 1) + i;
6379: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6380: }
6381: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr + (k - 2) - i) * Nc + c + foffset;
6382: }
6383: /* top */
6384: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb * Nc + c + foffset;
6385: for (o = oebb; o < oebr; ++o)
6386: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6387: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb * Nc + c + foffset;
6389: /* Middle Slice */
6390: for (j = 0; j < k - 1; ++j) {
6391: /* bottom */
6392: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf + (k - 2) - j) * Nc + c + foffset;
6393: for (o = off + j * (k - 1); o < off + (j + 1) * (k - 1); ++o)
6394: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6395: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf + j) * Nc + c + foffset;
6396: /* middle */
6397: for (i = 0; i < k - 1; ++i) {
6398: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl + i * (k - 1) + j) * Nc + c + foffset;
6399: for (n = 0; n < k - 1; ++n)
6400: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oc + (j * (k - 1) + i) * (k - 1) + n) * Nc + c + foffset;
6401: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr + j * (k - 1) + i) * Nc + c + foffset;
6402: }
6403: /* top */
6404: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb + j) * Nc + c + foffset;
6405: for (o = ofk + j * (k - 1) + (k - 2); o >= ofk + j * (k - 1); --o)
6406: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6407: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb + (k - 2) - j) * Nc + c + foffset;
6408: }
6410: /* Top Slice */
6411: /* bottom */
6412: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf * Nc + c + foffset;
6413: for (o = oetf; o < oetr; ++o)
6414: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6415: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf * Nc + c + foffset;
6416: /* middle */
6417: for (i = 0; i < k - 1; ++i) {
6418: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl + (k - 2) - i) * Nc + c + foffset;
6419: for (n = 0; n < k - 1; ++n)
6420: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oft + i * (k - 1) + n) * Nc + c + foffset;
6421: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr + i) * Nc + c + foffset;
6422: }
6423: /* top */
6424: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb * Nc + c + foffset;
6425: for (o = oetl - 1; o >= oetb; --o)
6426: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6427: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb * Nc + c + foffset;
6429: foffset = offset;
6430: } else {
6431: PetscInt dof;
6433: PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6434: for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6435: foffset = offset;
6436: }
6437: break;
6438: default:
6439: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %" PetscInt_FMT, d);
6440: }
6441: }
6442: PetscCheck(offset == size, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Number of permutation entries %" PetscInt_FMT " != %" PetscInt_FMT, offset, size);
6443: /* Check permutation */
6444: {
6445: PetscInt *check;
6447: PetscCall(PetscMalloc1(size, &check));
6448: for (i = 0; i < size; ++i) {
6449: check[i] = -1;
6450: PetscCheck(perm[i] >= 0 && perm[i] < size, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid permutation index p[%" PetscInt_FMT "] = %" PetscInt_FMT, i, perm[i]);
6451: }
6452: for (i = 0; i < size; ++i) check[perm[i]] = i;
6453: for (i = 0; i < size; ++i) PetscCheck(check[i] >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Missing permutation index %" PetscInt_FMT, i);
6454: PetscCall(PetscFree(check));
6455: }
6456: PetscCall(PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size, PETSC_OWN_POINTER, perm));
6457: if (d == dim) { // Add permutation for localized (in case this is a coordinate DM)
6458: PetscInt *loc_perm;
6459: PetscCall(PetscMalloc1(size * 2, &loc_perm));
6460: for (PetscInt i = 0; i < size; i++) {
6461: loc_perm[i] = perm[i];
6462: loc_perm[size + i] = size + perm[i];
6463: }
6464: PetscCall(PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size * 2, PETSC_OWN_POINTER, loc_perm));
6465: }
6466: }
6467: PetscFunctionReturn(PETSC_SUCCESS);
6468: }
6470: /*@
6471: DMPlexSetClosurePermutationLexicographic - Create a permutation from the default (BFS) point ordering in the closure, to a
6472: lexicographic ordering over the simplex (i.e., line, tri, tet, etc.), and set this permutation in the
6473: section provided (or the section of the `DM`).
6475: Not Collective
6477: Input Parameters:
6478: + dm - The `DM`
6479: . point - Either a cell (highest dim point) or an edge (dim 1 point), or `PETSC_DETERMINE`
6480: - section - The `PetscSection` to reorder, or `NULL` for the default section
6482: Example:
6483: A typical interpolated single-tri mesh might order points as
6484: .vb
6485: [c0, v1, v2, v3, e4, e5, e6]
6487: v3
6488: | \
6489: | \
6490: e6 e5
6491: | c0 \
6492: | \
6493: v1 -- e4 -- v2
6494: .ve
6496: (There is no significance to the ordering described here.) The default section for a P3 tri might typically assign
6497: dofs in the order of points, e.g.,
6498: .vb
6499: c0 -> [0]
6500: v1 -> [1]
6501: ...
6502: e4 -> [4, 5]
6503: .ve
6505: which corresponds to the dofs
6506: .vb
6507: 3
6508: 8 7
6509: 9 0 6
6510: 1 4 5 2
6511: .ve
6513: The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
6514: .vb
6515: 0 4 5 6 7 8 9 1 2 3
6516: .ve
6518: After calling DMPlexSetClosurePermutationLexicographic(), the closure will be ordered lexicographically,
6519: .vb
6520: 1 4 5 2 9 0 6 8 7 3
6521: .ve
6523: Level: developer
6525: Notes:
6526: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
6527: degree of the basis.
6529: The lexicographic order starts along the left edge, not the front, to match codes like GMsh with a bottom face oriented into the volume.
6531: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetLocalSection()`, `PetscSectionSetClosurePermutation()`, `DMPlexSetClosurePermutationTensor()`, `DMSetGlobalSection()`
6532: @*/
6533: PetscErrorCode DMPlexSetClosurePermutationLexicographic(DM dm, PetscInt point, PetscSection section)
6534: {
6535: DMLabel label;
6536: PetscInt dim, depth = -1, eStart = -1, Nf;
6537: PetscBool continuous = PETSC_TRUE, tensor = PETSC_TRUE;
6539: PetscFunctionBegin;
6542: // TODO: This needs to be tested for P4-6 using Plex ex3 with a linear field to check the permutations
6543: PetscCall(DMGetDimension(dm, &dim));
6544: if (dim < 1) PetscFunctionReturn(PETSC_SUCCESS);
6545: if (point < 0) {
6546: PetscInt sStart, sEnd;
6548: PetscCall(DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd));
6549: point = sEnd - sStart ? sStart : point;
6550: }
6551: PetscCall(DMPlexGetDepthLabel(dm, &label));
6552: if (point >= 0) PetscCall(DMLabelGetValue(label, point, &depth));
6553: if (!section) PetscCall(DMGetLocalSection(dm, §ion));
6554: if (depth == 1) {
6555: eStart = point;
6556: } else if (depth == dim) {
6557: const PetscInt *cone;
6559: PetscCall(DMPlexGetCone(dm, point, &cone));
6560: if (dim == 2) eStart = cone[0];
6561: else if (dim == 3) {
6562: const PetscInt *cone2;
6563: PetscCall(DMPlexGetCone(dm, cone[0], &cone2));
6564: eStart = cone2[0];
6565: } 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);
6566: } else PetscCheck(depth < 0, 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);
6568: PetscCall(PetscSectionGetNumFields(section, &Nf));
6569: for (PetscInt d = 1; d <= dim; d++) {
6570: PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
6571: PetscInt *perm;
6573: for (f = 0; f < Nf; ++f) {
6574: PetscInt dof;
6576: PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6577: PetscCheck(dim == 1 || !tensor, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Field %" PetscInt_FMT " should not have a tensor product discretization", f);
6578: if (!continuous && d < dim) continue;
6579: PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6580: size += dof * Nc;
6581: }
6582: PetscCall(PetscMalloc1(size, &perm));
6583: for (f = 0; f < Nf; ++f) {
6584: switch (d) {
6585: case 1:
6586: PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6587: if (!continuous && d < dim) continue;
6588: /*
6589: Original ordering is [ edge of length k-1; vtx0; vtx1 ]
6590: We want [ vtx0; edge of length k-1; vtx1 ]
6591: */
6592: if (continuous) {
6593: for (c = 0; c < Nc; c++, offset++) perm[offset] = (k - 1) * Nc + c + foffset;
6594: for (i = 0; i < k - 1; i++)
6595: for (c = 0; c < Nc; c++, offset++) perm[offset] = i * Nc + c + foffset;
6596: for (c = 0; c < Nc; c++, offset++) perm[offset] = k * Nc + c + foffset;
6597: foffset = offset;
6598: } else {
6599: PetscInt dof;
6601: PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6602: for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6603: foffset = offset;
6604: }
6605: break;
6606: case 2:
6607: /* The original tri closure is oriented clockwise, {f, e_b, e_r, e_l, v_lb, v_rb, v_lt} */
6608: PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6609: if (!continuous && d < dim) continue;
6610: /* The SEM order is
6612: v_lb, {e_b}, v_rb,
6613: e^{(k-1)-i}_l, {f^{i*(k-1-i)}}, e^i_r,
6614: v_lt
6615: */
6616: if (continuous) {
6617: const PetscInt of = 0;
6618: const PetscInt oeb = of + (k - 2) * (k - 1) / 2;
6619: const PetscInt oer = oeb + (k - 1);
6620: const PetscInt oel = oer + (k - 1);
6621: const PetscInt ovlb = oel + (k - 1);
6622: const PetscInt ovrb = ovlb + 1;
6623: const PetscInt ovlt = ovrb + 1;
6624: PetscInt o;
6626: /* bottom */
6627: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb * Nc + c + foffset;
6628: for (o = oeb; o < oer; ++o)
6629: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6630: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb * Nc + c + foffset;
6631: /* middle */
6632: for (i = 0; i < k - 1; ++i) {
6633: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel + (k - 2) - i) * Nc + c + foffset;
6634: // (k - 2) (k - 1) / 2 - (k - 2 - i) (k - 1 - i) / 2 = i (2 k - 3 - i) / 2
6635: for (o = of + i * (2 * k - 3 - i) / 2; o < of + (i + 1) * (2 * k - 4 - i) / 2; ++o)
6636: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6637: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer + i) * Nc + c + foffset;
6638: }
6639: /* top */
6640: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt * Nc + c + foffset;
6641: foffset = offset;
6642: } else {
6643: PetscInt dof;
6645: PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6646: for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6647: foffset = offset;
6648: }
6649: break;
6650: case 3:
6651: /* The original tet closure is
6653: {c,
6654: f_b, f_l, f_f, f_r,
6655: e_bl, e_br, e_bf, e_lf, e_rf, e_rb,
6656: v_blf, v_blb, v_brf, v_tlf}
6657: */
6658: PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6659: if (!continuous && d < dim) continue;
6660: /* The SEM order (starting on the left edge since GMsh flips the bottom face) is
6661: Bottom Slice
6662: v_blb, {e^{-n}_bl}, v_blf,
6663: e^{i}_br, f^{i,-n}_b, e^{-i}_bf,
6664: v_brf,
6666: Middle Slice (j)
6667: e^{-j}_rb, {f^{-n,j}_l}, e^{j}_lf,
6668: f^{-n,j}_r, {c^{j,-n,i}}, f^{j,i}_f,
6669: e^{j}_rf,
6671: Top Slice
6672: v_tlf,
6673: */
6674: if (continuous) {
6675: const PetscInt oc = 0;
6676: const PetscInt ofb = oc + (k - 3) * (k - 2) * (k - 1) / 6;
6677: const PetscInt ofl = ofb + (k - 2) * (k - 1) / 2;
6678: const PetscInt off = ofl + (k - 2) * (k - 1) / 2;
6679: const PetscInt ofr = off + (k - 2) * (k - 1) / 2;
6680: const PetscInt oebl = ofr + (k - 2) * (k - 1) / 2;
6681: const PetscInt oebr = oebl + (k - 1);
6682: const PetscInt oebf = oebr + (k - 1);
6683: const PetscInt oelf = oebf + (k - 1);
6684: const PetscInt oerb = oelf + (k - 1);
6685: const PetscInt oerf = oerb + (k - 1);
6686: const PetscInt ovblf = oerf + (k - 1);
6687: const PetscInt ovblb = ovblf + 1;
6688: const PetscInt ovbrf = ovblb + 1;
6689: const PetscInt ovtlf = ovbrf + 1;
6690: PetscInt o, n;
6692: /* Bottom Slice */
6693: /* bottom */
6694: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb * Nc + c + foffset;
6695: for (o = oebr - 1; o >= oebl; --o)
6696: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6697: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf * Nc + c + foffset;
6698: /* middle */
6699: for (i = 0; i < k - 1; ++i) {
6700: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr + i) * Nc + c + foffset;
6701: for (n = 0; n < k - 2 - i; ++n) {
6702: // (k - 2) (k - 1) / 2 - (k - 2 - i) (k - 1 - i) / 2 = i (2 k - 3 - i) / 2
6703: o = ofb + i * (2 * k - 3 - i) / 2 + (k - 2 - i - 1 - n);
6704: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6705: }
6706: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebf + (k - 2) - i) * Nc + c + foffset;
6707: }
6708: /* top */
6709: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf * Nc + c + foffset;
6711: /* Middle Slice */
6712: for (j = 0; j < k - 1; ++j) {
6713: /* bottom */
6714: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb + k - 2 - j) * Nc + c + foffset;
6715: for (n = k - 3 - j; n >= 0; --n) {
6716: // (k - 2) (k - 1) / 2 - (k - 2 - i) (k - 1 - i) / 2 = i (2 k - 3 - i) / 2
6717: o = ofl + n * (2 * k - 3 - n) / 2 + j;
6718: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6719: }
6720: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf + j) * Nc + c + foffset;
6721: /* middle */
6722: for (i = 0; i < k - 2 - j; ++i) {
6723: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr + i * (2 * k - 1 - i) / 2 + j) * Nc + c + foffset;
6724: for (n = k - 4 - j - i; n >= 0; --n) {
6725: // (k - 2) (k - 1) k / 6 - (k - 2 - j) (k - 1 - j) (k - j) / 6 = j (j^2 - 1 - 3 (k - 1) (j + k - 1)) / 6
6726: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oc + j * (j * j - 1 - 3 * (k - 1) * (j + k - 1)) / 6 + n * (2 * k - 3 - n) + i) * Nc + c + foffset;
6727: }
6728: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (off + j * (2 * k - 3 - j) / 2 + i) * Nc + c + foffset;
6729: }
6730: /* top */
6731: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf + j) * Nc + c + foffset;
6732: }
6734: /* Top Slice */
6735: for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf * Nc + c + foffset;
6737: foffset = offset;
6738: } else {
6739: PetscInt dof;
6741: PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6742: for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6743: foffset = offset;
6744: }
6745: break;
6746: default:
6747: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %" PetscInt_FMT, d);
6748: }
6749: }
6750: PetscCheck(offset == size, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Number of permutation entries %" PetscInt_FMT " != %" PetscInt_FMT, offset, size);
6751: /* Check permutation */
6752: {
6753: PetscInt *check;
6755: PetscCall(PetscMalloc1(size, &check));
6756: for (i = 0; i < size; ++i) {
6757: check[i] = -1;
6758: PetscCheck(perm[i] >= 0 && perm[i] < size, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid permutation index p[%" PetscInt_FMT "] = %" PetscInt_FMT, i, perm[i]);
6759: }
6760: for (i = 0; i < size; ++i) check[perm[i]] = i;
6761: for (i = 0; i < size; ++i) PetscCheck(check[i] >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Missing permutation index %" PetscInt_FMT " out of %" PetscInt_FMT, i, size);
6762: PetscCall(PetscFree(check));
6763: }
6764: PetscCall(PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size, PETSC_OWN_POINTER, perm));
6765: if (d == dim) { // Add permutation for localized (in case this is a coordinate DM)
6766: PetscInt *loc_perm;
6767: PetscCall(PetscMalloc1(size * 2, &loc_perm));
6768: for (PetscInt i = 0; i < size; i++) {
6769: loc_perm[i] = perm[i];
6770: loc_perm[size + i] = size + perm[i];
6771: }
6772: PetscCall(PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size * 2, PETSC_OWN_POINTER, loc_perm));
6773: }
6774: }
6775: PetscFunctionReturn(PETSC_SUCCESS);
6776: }
6778: PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
6779: {
6780: PetscDS prob;
6781: PetscInt depth, Nf, h;
6782: DMLabel label;
6784: PetscFunctionBeginHot;
6785: PetscCall(DMGetDS(dm, &prob));
6786: Nf = prob->Nf;
6787: label = dm->depthLabel;
6788: *dspace = NULL;
6789: if (field < Nf) {
6790: PetscObject disc = prob->disc[field];
6792: if (disc->classid == PETSCFE_CLASSID) {
6793: PetscDualSpace dsp;
6795: PetscCall(PetscFEGetDualSpace((PetscFE)disc, &dsp));
6796: PetscCall(DMLabelGetNumValues(label, &depth));
6797: PetscCall(DMLabelGetValue(label, point, &h));
6798: h = depth - 1 - h;
6799: if (h) PetscCall(PetscDualSpaceGetHeightSubspace(dsp, h, dspace));
6800: else *dspace = dsp;
6801: }
6802: }
6803: PetscFunctionReturn(PETSC_SUCCESS);
6804: }
6806: static inline PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
6807: {
6808: PetscScalar *array;
6809: const PetscScalar *vArray;
6810: const PetscInt *cone, *coneO;
6811: PetscInt pStart, pEnd, p, numPoints, size = 0, offset = 0;
6813: PetscFunctionBeginHot;
6814: PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
6815: PetscCall(DMPlexGetConeSize(dm, point, &numPoints));
6816: PetscCall(DMPlexGetCone(dm, point, &cone));
6817: PetscCall(DMPlexGetConeOrientation(dm, point, &coneO));
6818: if (!values || !*values) {
6819: if ((point >= pStart) && (point < pEnd)) {
6820: PetscInt dof;
6822: PetscCall(PetscSectionGetDof(section, point, &dof));
6823: size += dof;
6824: }
6825: for (p = 0; p < numPoints; ++p) {
6826: const PetscInt cp = cone[p];
6827: PetscInt dof;
6829: if ((cp < pStart) || (cp >= pEnd)) continue;
6830: PetscCall(PetscSectionGetDof(section, cp, &dof));
6831: size += dof;
6832: }
6833: if (!values) {
6834: if (csize) *csize = size;
6835: PetscFunctionReturn(PETSC_SUCCESS);
6836: }
6837: PetscCall(DMGetWorkArray(dm, size, MPIU_SCALAR, &array));
6838: } else {
6839: array = *values;
6840: }
6841: size = 0;
6842: PetscCall(VecGetArrayRead(v, &vArray));
6843: if ((point >= pStart) && (point < pEnd)) {
6844: PetscInt dof, off, d;
6845: const PetscScalar *varr;
6847: PetscCall(PetscSectionGetDof(section, point, &dof));
6848: PetscCall(PetscSectionGetOffset(section, point, &off));
6849: varr = PetscSafePointerPlusOffset(vArray, off);
6850: for (d = 0; d < dof; ++d, ++offset) array[offset] = varr[d];
6851: size += dof;
6852: }
6853: for (p = 0; p < numPoints; ++p) {
6854: const PetscInt cp = cone[p];
6855: PetscInt o = coneO[p];
6856: PetscInt dof, off, d;
6857: const PetscScalar *varr;
6859: if ((cp < pStart) || (cp >= pEnd)) continue;
6860: PetscCall(PetscSectionGetDof(section, cp, &dof));
6861: PetscCall(PetscSectionGetOffset(section, cp, &off));
6862: varr = PetscSafePointerPlusOffset(vArray, off);
6863: if (o >= 0) {
6864: for (d = 0; d < dof; ++d, ++offset) array[offset] = varr[d];
6865: } else {
6866: for (d = dof - 1; d >= 0; --d, ++offset) array[offset] = varr[d];
6867: }
6868: size += dof;
6869: }
6870: PetscCall(VecRestoreArrayRead(v, &vArray));
6871: if (!*values) {
6872: if (csize) *csize = size;
6873: *values = array;
6874: } else {
6875: PetscCheck(size <= *csize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %" PetscInt_FMT " < actual size %" PetscInt_FMT, *csize, size);
6876: *csize = size;
6877: }
6878: PetscFunctionReturn(PETSC_SUCCESS);
6879: }
6881: /* Compress out points not in the section */
6882: static inline PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
6883: {
6884: const PetscInt np = *numPoints;
6885: PetscInt pStart, pEnd, p, q;
6887: PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
6888: for (p = 0, q = 0; p < np; ++p) {
6889: const PetscInt r = points[p * 2];
6890: if ((r >= pStart) && (r < pEnd)) {
6891: points[q * 2] = r;
6892: points[q * 2 + 1] = points[p * 2 + 1];
6893: ++q;
6894: }
6895: }
6896: *numPoints = q;
6897: return PETSC_SUCCESS;
6898: }
6900: /* Compressed closure does not apply closure permutation */
6901: PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt ornt, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
6902: {
6903: const PetscInt *cla = NULL;
6904: PetscInt np, *pts = NULL;
6906: PetscFunctionBeginHot;
6907: PetscCall(PetscSectionGetClosureIndex(section, (PetscObject)dm, clSec, clPoints));
6908: if (!ornt && *clPoints) {
6909: PetscInt dof, off;
6911: PetscCall(PetscSectionGetDof(*clSec, point, &dof));
6912: PetscCall(PetscSectionGetOffset(*clSec, point, &off));
6913: PetscCall(ISGetIndices(*clPoints, &cla));
6914: np = dof / 2;
6915: pts = PetscSafePointerPlusOffset((PetscInt *)cla, off);
6916: } else {
6917: PetscCall(DMPlexGetTransitiveClosure_Internal(dm, point, ornt, PETSC_TRUE, &np, &pts));
6918: PetscCall(CompressPoints_Private(section, &np, pts));
6919: }
6920: *numPoints = np;
6921: *points = pts;
6922: *clp = cla;
6923: PetscFunctionReturn(PETSC_SUCCESS);
6924: }
6926: PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
6927: {
6928: PetscFunctionBeginHot;
6929: if (!*clPoints) {
6930: PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points));
6931: } else {
6932: PetscCall(ISRestoreIndices(*clPoints, clp));
6933: }
6934: *numPoints = 0;
6935: *points = NULL;
6936: *clSec = NULL;
6937: *clPoints = NULL;
6938: *clp = NULL;
6939: PetscFunctionReturn(PETSC_SUCCESS);
6940: }
6942: static inline PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
6943: {
6944: PetscInt offset = 0, p;
6945: const PetscInt **perms = NULL;
6946: const PetscScalar **flips = NULL;
6948: PetscFunctionBeginHot;
6949: *size = 0;
6950: PetscCall(PetscSectionGetPointSyms(section, numPoints, points, &perms, &flips));
6951: for (p = 0; p < numPoints; p++) {
6952: const PetscInt point = points[2 * p];
6953: const PetscInt *perm = perms ? perms[p] : NULL;
6954: const PetscScalar *flip = flips ? flips[p] : NULL;
6955: PetscInt dof, off, d;
6956: const PetscScalar *varr;
6958: PetscCall(PetscSectionGetDof(section, point, &dof));
6959: PetscCall(PetscSectionGetOffset(section, point, &off));
6960: varr = PetscSafePointerPlusOffset(vArray, off);
6961: if (clperm) {
6962: if (perm) {
6963: for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]] = varr[d];
6964: } else {
6965: for (d = 0; d < dof; d++) array[clperm[offset + d]] = varr[d];
6966: }
6967: if (flip) {
6968: for (d = 0; d < dof; d++) array[clperm[offset + d]] *= flip[d];
6969: }
6970: } else {
6971: if (perm) {
6972: for (d = 0; d < dof; d++) array[offset + perm[d]] = varr[d];
6973: } else {
6974: for (d = 0; d < dof; d++) array[offset + d] = varr[d];
6975: }
6976: if (flip) {
6977: for (d = 0; d < dof; d++) array[offset + d] *= flip[d];
6978: }
6979: }
6980: offset += dof;
6981: }
6982: PetscCall(PetscSectionRestorePointSyms(section, numPoints, points, &perms, &flips));
6983: *size = offset;
6984: PetscFunctionReturn(PETSC_SUCCESS);
6985: }
6987: 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[])
6988: {
6989: PetscInt offset = 0, f;
6991: PetscFunctionBeginHot;
6992: *size = 0;
6993: for (f = 0; f < numFields; ++f) {
6994: PetscInt p;
6995: const PetscInt **perms = NULL;
6996: const PetscScalar **flips = NULL;
6998: PetscCall(PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips));
6999: for (p = 0; p < numPoints; p++) {
7000: const PetscInt point = points[2 * p];
7001: PetscInt fdof, foff, b;
7002: const PetscScalar *varr;
7003: const PetscInt *perm = perms ? perms[p] : NULL;
7004: const PetscScalar *flip = flips ? flips[p] : NULL;
7006: PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
7007: PetscCall(PetscSectionGetFieldOffset(section, point, f, &foff));
7008: varr = &vArray[foff];
7009: if (clperm) {
7010: if (perm) {
7011: for (b = 0; b < fdof; b++) array[clperm[offset + perm[b]]] = varr[b];
7012: } else {
7013: for (b = 0; b < fdof; b++) array[clperm[offset + b]] = varr[b];
7014: }
7015: if (flip) {
7016: for (b = 0; b < fdof; b++) array[clperm[offset + b]] *= flip[b];
7017: }
7018: } else {
7019: if (perm) {
7020: for (b = 0; b < fdof; b++) array[offset + perm[b]] = varr[b];
7021: } else {
7022: for (b = 0; b < fdof; b++) array[offset + b] = varr[b];
7023: }
7024: if (flip) {
7025: for (b = 0; b < fdof; b++) array[offset + b] *= flip[b];
7026: }
7027: }
7028: offset += fdof;
7029: }
7030: PetscCall(PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips));
7031: }
7032: *size = offset;
7033: PetscFunctionReturn(PETSC_SUCCESS);
7034: }
7036: /*@C
7037: DMPlexVecGetOrientedClosure - Get an array of the values on the closure of 'point' with a given orientation, optionally applying the closure permutation.
7039: Not collective
7041: Input Parameters:
7042: + dm - The `DM`
7043: . section - The section describing the layout in `v`, or `NULL` to use the default section
7044: . useClPerm - Flag for whether the provided closure permutation should be applied to the values
7045: . v - The local vector
7046: . point - The point in the `DM`
7047: - ornt - The orientation of the cell, an integer giving the prescription for cone traversal. Typically, this will be 0.
7049: Input/Output Parameters:
7050: + csize - The size of the input values array, or `NULL`; on output the number of values in the closure
7051: - values - An array to use for the values, or *values = `NULL` to have it allocated automatically;
7052: if the user provided `NULL`, it is a borrowed array and should not be freed, use `DMPlexVecRestoreClosure()` to return it
7054: Level: advanced
7056: Notes:
7057: `DMPlexVecGetOrientedClosure()`/`DMPlexVecRestoreClosure()` only allocates the values array if it set to `NULL` in the
7058: calling function. This is because `DMPlexVecGetOrientedClosure()` is typically called in the inner loop of a `Vec` or `Mat`
7059: assembly function, and a user may already have allocated storage for this operation.
7061: Fortran Notes:
7062: The `csize` argument is present in the Fortran binding. Since the Fortran `values` array contains its length information this argument may not be needed.
7063: In that case one may pass `PETSC_NULL_INTEGER` for `csize`.
7065: `values` must be declared with
7066: .vb
7067: PetscScalar,dimension(:),pointer :: values
7068: .ve
7069: and it will be allocated internally by PETSc to hold the values returned
7071: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexGetCellCoordinates()`, `DMPlexVecRestoreClosure()`, `DMPlexVecSetClosure()`
7072: @*/
7073: PetscErrorCode DMPlexVecGetOrientedClosure(DM dm, PetscSection section, PetscBool useClPerm, Vec v, PetscInt point, PetscInt ornt, PetscInt *csize, PetscScalar *values[])
7074: {
7075: PetscSection clSection;
7076: IS clPoints;
7077: PetscInt *points = NULL;
7078: const PetscInt *clp, *perm = NULL;
7079: PetscInt depth, numFields, numPoints, asize;
7081: PetscFunctionBeginHot;
7083: if (!section) PetscCall(DMGetLocalSection(dm, §ion));
7086: PetscCall(DMPlexGetDepth(dm, &depth));
7087: PetscCall(PetscSectionGetNumFields(section, &numFields));
7088: if (depth == 1 && numFields < 2) {
7089: PetscCall(DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values));
7090: PetscFunctionReturn(PETSC_SUCCESS);
7091: }
7092: /* Get points */
7093: PetscCall(DMPlexGetCompressedClosure(dm, section, point, ornt, &numPoints, &points, &clSection, &clPoints, &clp));
7094: /* Get sizes */
7095: asize = 0;
7096: for (PetscInt p = 0; p < numPoints * 2; p += 2) {
7097: PetscInt dof;
7098: PetscCall(PetscSectionGetDof(section, points[p], &dof));
7099: asize += dof;
7100: }
7101: if (values) {
7102: const PetscScalar *vArray;
7103: PetscInt size;
7105: if (*values) {
7106: PetscCheck(*csize >= asize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Provided array size %" PetscInt_FMT " not sufficient to hold closure size %" PetscInt_FMT, *csize, asize);
7107: } else PetscCall(DMGetWorkArray(dm, asize, MPIU_SCALAR, values));
7108: if (useClPerm) PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, asize, &perm));
7109: PetscCall(VecGetArrayRead(v, &vArray));
7110: /* Get values */
7111: if (numFields > 0) PetscCall(DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values));
7112: else PetscCall(DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values));
7113: PetscCheck(asize == size, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %" PetscInt_FMT " does not match Vec closure size %" PetscInt_FMT, asize, size);
7114: /* Cleanup array */
7115: PetscCall(VecRestoreArrayRead(v, &vArray));
7116: }
7117: if (csize) *csize = asize;
7118: /* Cleanup points */
7119: PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
7120: PetscFunctionReturn(PETSC_SUCCESS);
7121: }
7123: /*@C
7124: DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
7126: Not collective
7128: Input Parameters:
7129: + dm - The `DM`
7130: . section - The section describing the layout in `v`, or `NULL` to use the default section
7131: . v - The local vector
7132: - point - The point in the `DM`
7134: Input/Output Parameters:
7135: + csize - The size of the input values array, or `NULL`; on output the number of values in the closure
7136: - values - An array to use for the values, or *values = `NULL` to have it allocated automatically;
7137: if the user provided `NULL`, it is a borrowed array and should not be freed, use `DMPlexVecRestoreClosure()` to return it
7139: Level: intermediate
7141: Notes:
7142: This is used for getting the all values in a `Vec` in the closure of a mesh point.
7143: To get only the values in the closure of a mesh point at a specific depth (for example, at mesh vertices), use `DMPlexVecGetClosureAtDepth()`.
7145: `DMPlexVecGetClosure()`/`DMPlexVecRestoreClosure()` only allocates the values array if it set to `NULL` in the
7146: calling function. This is because `DMPlexVecGetClosure()` is typically called in the inner loop of a `Vec` or `Mat`
7147: assembly function, and a user may already have allocated storage for this operation.
7149: A typical use could be
7150: .vb
7151: values = NULL;
7152: PetscCall(DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values));
7153: for (cl = 0; cl < clSize; ++cl) {
7154: <Compute on closure>
7155: }
7156: PetscCall(DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values));
7157: .ve
7158: or
7159: .vb
7160: PetscMalloc1(clMaxSize, &values);
7161: for (p = pStart; p < pEnd; ++p) {
7162: clSize = clMaxSize;
7163: PetscCall(DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values));
7164: for (cl = 0; cl < clSize; ++cl) {
7165: <Compute on closure>
7166: }
7167: }
7168: PetscFree(values);
7169: .ve
7171: Fortran Notes:
7172: The `csize` argument is present in the Fortran binding. Since the Fortran `values` array contains its length information this argument may not be needed.
7173: In that case one may pass `PETSC_NULL_INTEGER` for `csize`.
7175: `values` must be declared with
7176: .vb
7177: PetscScalar,dimension(:),pointer :: values
7178: .ve
7179: and it will be allocated internally by PETSc to hold the values returned
7181: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosureAtDepth()`, `DMPlexVecRestoreClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
7182: @*/
7183: PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
7184: {
7185: PetscFunctionBeginHot;
7186: PetscCall(DMPlexVecGetOrientedClosure(dm, section, PETSC_TRUE, v, point, 0, csize, values));
7187: PetscFunctionReturn(PETSC_SUCCESS);
7188: }
7190: /*@C
7191: DMPlexVecGetClosureAtDepth - Get an array of the values on the closure of 'point' that are at a specific depth
7193: Not collective
7195: Input Parameters:
7196: + dm - The `DM`
7197: . section - The section describing the layout in `v`, or `NULL` to use the default section
7198: . v - The local vector
7199: . depth - The depth of mesh points that should be returned
7200: - point - The point in the `DM`
7202: Input/Output Parameters:
7203: + csize - The size of the input values array, or `NULL`; on output the number of values in the closure
7204: - values - An array to use for the values, or *values = `NULL` to have it allocated automatically;
7205: if the user provided `NULL`, it is a borrowed array and should not be freed, use `DMPlexVecRestoreClosure()` to return it
7207: Level: intermediate
7209: Notes:
7210: This is used for getting the values in a `Vec` associated with specific mesh points.
7211: For example, to get only the values at mesh vertices, pass `depth=0`. To get all the values in the closure of a mesh point, use `DMPlexVecGetClosure()`.
7213: `DMPlexVecGetClosureAtDepth()`/`DMPlexVecRestoreClosure()` only allocates the values array if it set to `NULL` in the
7214: calling function. This is because `DMPlexVecGetClosureAtDepth()` is typically called in the inner loop of a `Vec` or `Mat`
7215: assembly function, and a user may already have allocated storage for this operation.
7217: A typical use could be
7218: .vb
7219: values = NULL;
7220: PetscCall(DMPlexVecGetClosureAtDepth(dm, NULL, v, p, depth, &clSize, &values));
7221: for (cl = 0; cl < clSize; ++cl) {
7222: <Compute on closure>
7223: }
7224: PetscCall(DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values));
7225: .ve
7226: or
7227: .vb
7228: PetscMalloc1(clMaxSize, &values);
7229: for (p = pStart; p < pEnd; ++p) {
7230: clSize = clMaxSize;
7231: PetscCall(DMPlexVecGetClosureAtDepth(dm, NULL, v, p, depth, &clSize, &values));
7232: for (cl = 0; cl < clSize; ++cl) {
7233: <Compute on closure>
7234: }
7235: }
7236: PetscFree(values);
7237: .ve
7239: Fortran Notes:
7240: The `csize` argument is present in the Fortran binding. Since the Fortran `values` array contains its length information this argument may not be needed.
7241: In that case one may pass `PETSC_NULL_INTEGER` for `csize`.
7243: `values` must be declared with
7244: .vb
7245: PetscScalar,dimension(:),pointer :: values
7246: .ve
7247: and it will be allocated internally by PETSc to hold the values returned
7249: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexVecRestoreClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
7250: @*/
7251: PetscErrorCode DMPlexVecGetClosureAtDepth(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
7252: {
7253: DMLabel depthLabel;
7254: PetscSection clSection;
7255: IS clPoints;
7256: PetscScalar *array;
7257: const PetscScalar *vArray;
7258: PetscInt *points = NULL;
7259: const PetscInt *clp, *perm = NULL;
7260: PetscInt mdepth, numFields, numPoints, Np = 0, p, clsize, size;
7262: PetscFunctionBeginHot;
7264: if (!section) PetscCall(DMGetLocalSection(dm, §ion));
7267: PetscCall(DMPlexGetDepth(dm, &mdepth));
7268: PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
7269: PetscCall(PetscSectionGetNumFields(section, &numFields));
7270: if (mdepth == 1 && numFields < 2) {
7271: PetscCall(DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values));
7272: PetscFunctionReturn(PETSC_SUCCESS);
7273: }
7274: /* Get points */
7275: PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &numPoints, &points, &clSection, &clPoints, &clp));
7276: for (clsize = 0, p = 0; p < Np; p++) {
7277: PetscInt dof;
7278: PetscCall(PetscSectionGetDof(section, points[2 * p], &dof));
7279: clsize += dof;
7280: }
7281: PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &perm));
7282: /* Filter points */
7283: for (p = 0; p < numPoints * 2; p += 2) {
7284: PetscInt dep;
7286: PetscCall(DMLabelGetValue(depthLabel, points[p], &dep));
7287: if (dep != depth) continue;
7288: points[Np * 2 + 0] = points[p];
7289: points[Np * 2 + 1] = points[p + 1];
7290: ++Np;
7291: }
7292: /* Get array */
7293: if (!values || !*values) {
7294: PetscInt asize = 0, dof;
7296: for (p = 0; p < Np * 2; p += 2) {
7297: PetscCall(PetscSectionGetDof(section, points[p], &dof));
7298: asize += dof;
7299: }
7300: if (!values) {
7301: PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
7302: if (csize) *csize = asize;
7303: PetscFunctionReturn(PETSC_SUCCESS);
7304: }
7305: PetscCall(DMGetWorkArray(dm, asize, MPIU_SCALAR, &array));
7306: } else {
7307: array = *values;
7308: }
7309: PetscCall(VecGetArrayRead(v, &vArray));
7310: /* Get values */
7311: if (numFields > 0) PetscCall(DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array));
7312: else PetscCall(DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array));
7313: /* Cleanup points */
7314: PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
7315: /* Cleanup array */
7316: PetscCall(VecRestoreArrayRead(v, &vArray));
7317: if (!*values) {
7318: if (csize) *csize = size;
7319: *values = array;
7320: } else {
7321: PetscCheck(size <= *csize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %" PetscInt_FMT " < actual size %" PetscInt_FMT, *csize, size);
7322: *csize = size;
7323: }
7324: PetscFunctionReturn(PETSC_SUCCESS);
7325: }
7327: /*@C
7328: DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point' obtained with `DMPlexVecGetClosure()`
7330: Not collective
7332: Input Parameters:
7333: + dm - The `DM`
7334: . section - The section describing the layout in `v`, or `NULL` to use the default section
7335: . v - The local vector
7336: . point - The point in the `DM`
7337: . csize - The number of values in the closure, or `NULL`
7338: - values - The array of values
7340: Level: intermediate
7342: Note:
7343: The array values are discarded and not copied back into `v`. In order to copy values back to `v`, use `DMPlexVecSetClosure()`
7345: Fortran Note:
7346: The `csize` argument is present in the Fortran binding. Since the Fortran `values` array contains its length information this argument may not be needed.
7347: In that case one may pass `PETSC_NULL_INTEGER` for `csize`.
7349: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
7350: @*/
7351: PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
7352: {
7353: PetscInt size = 0;
7355: PetscFunctionBegin;
7356: /* Should work without recalculating size */
7357: PetscCall(DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void *)values));
7358: *values = NULL;
7359: PetscFunctionReturn(PETSC_SUCCESS);
7360: }
7362: static inline void add(PetscScalar *x, PetscScalar y)
7363: {
7364: *x += y;
7365: }
7366: static inline void insert(PetscScalar *x, PetscScalar y)
7367: {
7368: *x = y;
7369: }
7371: 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[])
7372: {
7373: PetscInt cdof; /* The number of constraints on this point */
7374: const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
7375: PetscScalar *a;
7376: PetscInt off, cind = 0, k;
7378: PetscFunctionBegin;
7379: PetscCall(PetscSectionGetConstraintDof(section, point, &cdof));
7380: PetscCall(PetscSectionGetOffset(section, point, &off));
7381: a = &array[off];
7382: if (!cdof || setBC) {
7383: if (clperm) {
7384: if (perm) {
7385: for (k = 0; k < dof; ++k) fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
7386: } else {
7387: for (k = 0; k < dof; ++k) fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
7388: }
7389: } else {
7390: if (perm) {
7391: for (k = 0; k < dof; ++k) fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
7392: } else {
7393: for (k = 0; k < dof; ++k) fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
7394: }
7395: }
7396: } else {
7397: PetscCall(PetscSectionGetConstraintIndices(section, point, &cdofs));
7398: if (clperm) {
7399: if (perm) {
7400: for (k = 0; k < dof; ++k) {
7401: if ((cind < cdof) && (k == cdofs[cind])) {
7402: ++cind;
7403: continue;
7404: }
7405: fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
7406: }
7407: } else {
7408: for (k = 0; k < dof; ++k) {
7409: if ((cind < cdof) && (k == cdofs[cind])) {
7410: ++cind;
7411: continue;
7412: }
7413: fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
7414: }
7415: }
7416: } else {
7417: if (perm) {
7418: for (k = 0; k < dof; ++k) {
7419: if ((cind < cdof) && (k == cdofs[cind])) {
7420: ++cind;
7421: continue;
7422: }
7423: fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
7424: }
7425: } else {
7426: for (k = 0; k < dof; ++k) {
7427: if ((cind < cdof) && (k == cdofs[cind])) {
7428: ++cind;
7429: continue;
7430: }
7431: fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
7432: }
7433: }
7434: }
7435: }
7436: PetscFunctionReturn(PETSC_SUCCESS);
7437: }
7439: 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[])
7440: {
7441: PetscInt cdof; /* The number of constraints on this point */
7442: const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
7443: PetscScalar *a;
7444: PetscInt off, cind = 0, k;
7446: PetscFunctionBegin;
7447: PetscCall(PetscSectionGetConstraintDof(section, point, &cdof));
7448: PetscCall(PetscSectionGetOffset(section, point, &off));
7449: a = &array[off];
7450: if (cdof) {
7451: PetscCall(PetscSectionGetConstraintIndices(section, point, &cdofs));
7452: if (clperm) {
7453: if (perm) {
7454: for (k = 0; k < dof; ++k) {
7455: if ((cind < cdof) && (k == cdofs[cind])) {
7456: fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
7457: cind++;
7458: }
7459: }
7460: } else {
7461: for (k = 0; k < dof; ++k) {
7462: if ((cind < cdof) && (k == cdofs[cind])) {
7463: fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
7464: cind++;
7465: }
7466: }
7467: }
7468: } else {
7469: if (perm) {
7470: for (k = 0; k < dof; ++k) {
7471: if ((cind < cdof) && (k == cdofs[cind])) {
7472: fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
7473: cind++;
7474: }
7475: }
7476: } else {
7477: for (k = 0; k < dof; ++k) {
7478: if ((cind < cdof) && (k == cdofs[cind])) {
7479: fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
7480: cind++;
7481: }
7482: }
7483: }
7484: }
7485: }
7486: PetscFunctionReturn(PETSC_SUCCESS);
7487: }
7489: 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[])
7490: {
7491: PetscScalar *a;
7492: PetscInt fdof, foff, fcdof, foffset = *offset;
7493: const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
7494: PetscInt cind = 0, b;
7496: PetscFunctionBegin;
7497: PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
7498: PetscCall(PetscSectionGetFieldConstraintDof(section, point, f, &fcdof));
7499: PetscCall(PetscSectionGetFieldOffset(section, point, f, &foff));
7500: a = &array[foff];
7501: if (!fcdof || setBC) {
7502: if (clperm) {
7503: if (perm) {
7504: for (b = 0; b < fdof; b++) fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
7505: } else {
7506: for (b = 0; b < fdof; b++) fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
7507: }
7508: } else {
7509: if (perm) {
7510: for (b = 0; b < fdof; b++) fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
7511: } else {
7512: for (b = 0; b < fdof; b++) fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
7513: }
7514: }
7515: } else {
7516: PetscCall(PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs));
7517: if (clperm) {
7518: if (perm) {
7519: for (b = 0; b < fdof; b++) {
7520: if ((cind < fcdof) && (b == fcdofs[cind])) {
7521: ++cind;
7522: continue;
7523: }
7524: fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
7525: }
7526: } else {
7527: for (b = 0; b < fdof; b++) {
7528: if ((cind < fcdof) && (b == fcdofs[cind])) {
7529: ++cind;
7530: continue;
7531: }
7532: fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
7533: }
7534: }
7535: } else {
7536: if (perm) {
7537: for (b = 0; b < fdof; b++) {
7538: if ((cind < fcdof) && (b == fcdofs[cind])) {
7539: ++cind;
7540: continue;
7541: }
7542: fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
7543: }
7544: } else {
7545: for (b = 0; b < fdof; b++) {
7546: if ((cind < fcdof) && (b == fcdofs[cind])) {
7547: ++cind;
7548: continue;
7549: }
7550: fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
7551: }
7552: }
7553: }
7554: }
7555: *offset += fdof;
7556: PetscFunctionReturn(PETSC_SUCCESS);
7557: }
7559: 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[])
7560: {
7561: PetscScalar *a;
7562: PetscInt fdof, foff, fcdof, foffset = *offset;
7563: const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
7564: PetscInt Nc, cind = 0, ncind = 0, b;
7565: PetscBool ncSet, fcSet;
7567: PetscFunctionBegin;
7568: PetscCall(PetscSectionGetFieldComponents(section, f, &Nc));
7569: PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
7570: PetscCall(PetscSectionGetFieldConstraintDof(section, point, f, &fcdof));
7571: PetscCall(PetscSectionGetFieldOffset(section, point, f, &foff));
7572: a = &array[foff];
7573: if (fcdof) {
7574: /* We just override fcdof and fcdofs with Ncc and comps */
7575: PetscCall(PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs));
7576: if (clperm) {
7577: if (perm) {
7578: if (comps) {
7579: for (b = 0; b < fdof; b++) {
7580: ncSet = fcSet = PETSC_FALSE;
7581: if (b % Nc == comps[ncind]) {
7582: ncind = (ncind + 1) % Ncc;
7583: ncSet = PETSC_TRUE;
7584: }
7585: if ((cind < fcdof) && (b == fcdofs[cind])) {
7586: ++cind;
7587: fcSet = PETSC_TRUE;
7588: }
7589: if (ncSet && fcSet) fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
7590: }
7591: } else {
7592: for (b = 0; b < fdof; b++) {
7593: if ((cind < fcdof) && (b == fcdofs[cind])) {
7594: fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
7595: ++cind;
7596: }
7597: }
7598: }
7599: } else {
7600: if (comps) {
7601: for (b = 0; b < fdof; b++) {
7602: ncSet = fcSet = PETSC_FALSE;
7603: if (b % Nc == comps[ncind]) {
7604: ncind = (ncind + 1) % Ncc;
7605: ncSet = PETSC_TRUE;
7606: }
7607: if ((cind < fcdof) && (b == fcdofs[cind])) {
7608: ++cind;
7609: fcSet = PETSC_TRUE;
7610: }
7611: if (ncSet && fcSet) fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
7612: }
7613: } else {
7614: for (b = 0; b < fdof; b++) {
7615: if ((cind < fcdof) && (b == fcdofs[cind])) {
7616: fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
7617: ++cind;
7618: }
7619: }
7620: }
7621: }
7622: } else {
7623: if (perm) {
7624: if (comps) {
7625: for (b = 0; b < fdof; b++) {
7626: ncSet = fcSet = PETSC_FALSE;
7627: if (b % Nc == comps[ncind]) {
7628: ncind = (ncind + 1) % Ncc;
7629: ncSet = PETSC_TRUE;
7630: }
7631: if ((cind < fcdof) && (b == fcdofs[cind])) {
7632: ++cind;
7633: fcSet = PETSC_TRUE;
7634: }
7635: if (ncSet && fcSet) fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
7636: }
7637: } else {
7638: for (b = 0; b < fdof; b++) {
7639: if ((cind < fcdof) && (b == fcdofs[cind])) {
7640: fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
7641: ++cind;
7642: }
7643: }
7644: }
7645: } else {
7646: if (comps) {
7647: for (b = 0; b < fdof; b++) {
7648: ncSet = fcSet = PETSC_FALSE;
7649: if (b % Nc == comps[ncind]) {
7650: ncind = (ncind + 1) % Ncc;
7651: ncSet = PETSC_TRUE;
7652: }
7653: if ((cind < fcdof) && (b == fcdofs[cind])) {
7654: ++cind;
7655: fcSet = PETSC_TRUE;
7656: }
7657: if (ncSet && fcSet) fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
7658: }
7659: } else {
7660: for (b = 0; b < fdof; b++) {
7661: if ((cind < fcdof) && (b == fcdofs[cind])) {
7662: fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
7663: ++cind;
7664: }
7665: }
7666: }
7667: }
7668: }
7669: }
7670: *offset += fdof;
7671: PetscFunctionReturn(PETSC_SUCCESS);
7672: }
7674: static inline PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
7675: {
7676: PetscScalar *array;
7677: const PetscInt *cone, *coneO;
7678: PetscInt pStart, pEnd, p, numPoints, off, dof;
7680: PetscFunctionBeginHot;
7681: PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
7682: PetscCall(DMPlexGetConeSize(dm, point, &numPoints));
7683: PetscCall(DMPlexGetCone(dm, point, &cone));
7684: PetscCall(DMPlexGetConeOrientation(dm, point, &coneO));
7685: PetscCall(VecGetArray(v, &array));
7686: for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
7687: const PetscInt cp = !p ? point : cone[p - 1];
7688: const PetscInt o = !p ? 0 : coneO[p - 1];
7690: if ((cp < pStart) || (cp >= pEnd)) {
7691: dof = 0;
7692: continue;
7693: }
7694: PetscCall(PetscSectionGetDof(section, cp, &dof));
7695: /* ADD_VALUES */
7696: {
7697: const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
7698: PetscScalar *a;
7699: PetscInt cdof, coff, cind = 0, k;
7701: PetscCall(PetscSectionGetConstraintDof(section, cp, &cdof));
7702: PetscCall(PetscSectionGetOffset(section, cp, &coff));
7703: a = &array[coff];
7704: if (!cdof) {
7705: if (o >= 0) {
7706: for (k = 0; k < dof; ++k) a[k] += values[off + k];
7707: } else {
7708: for (k = 0; k < dof; ++k) a[k] += values[off + dof - k - 1];
7709: }
7710: } else {
7711: PetscCall(PetscSectionGetConstraintIndices(section, cp, &cdofs));
7712: if (o >= 0) {
7713: for (k = 0; k < dof; ++k) {
7714: if ((cind < cdof) && (k == cdofs[cind])) {
7715: ++cind;
7716: continue;
7717: }
7718: a[k] += values[off + k];
7719: }
7720: } else {
7721: for (k = 0; k < dof; ++k) {
7722: if ((cind < cdof) && (k == cdofs[cind])) {
7723: ++cind;
7724: continue;
7725: }
7726: a[k] += values[off + dof - k - 1];
7727: }
7728: }
7729: }
7730: }
7731: }
7732: PetscCall(VecRestoreArray(v, &array));
7733: PetscFunctionReturn(PETSC_SUCCESS);
7734: }
7736: /*@C
7737: DMPlexVecSetClosure - Set an array of the values on the closure of `point`
7739: Not collective
7741: Input Parameters:
7742: + dm - The `DM`
7743: . section - The section describing the layout in `v`, or `NULL` to use the default section
7744: . v - The local vector
7745: . point - The point in the `DM`
7746: . values - The array of values
7747: - mode - The insert mode. One of `INSERT_ALL_VALUES`, `ADD_ALL_VALUES`, `INSERT_VALUES`, `ADD_VALUES`, `INSERT_BC_VALUES`, and `ADD_BC_VALUES`,
7748: where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions.
7750: Level: intermediate
7752: Note:
7753: Usually the input arrays were obtained with `DMPlexVecGetClosure()`
7755: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`
7756: @*/
7757: PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
7758: {
7759: PetscSection clSection;
7760: IS clPoints;
7761: PetscScalar *array;
7762: PetscInt *points = NULL;
7763: const PetscInt *clp, *clperm = NULL;
7764: PetscInt depth, numFields, numPoints, p, clsize;
7766: PetscFunctionBeginHot;
7768: if (!section) PetscCall(DMGetLocalSection(dm, §ion));
7771: PetscCall(DMPlexGetDepth(dm, &depth));
7772: PetscCall(PetscSectionGetNumFields(section, &numFields));
7773: if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
7774: PetscCall(DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode));
7775: PetscFunctionReturn(PETSC_SUCCESS);
7776: }
7777: /* Get points */
7778: PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &numPoints, &points, &clSection, &clPoints, &clp));
7779: for (clsize = 0, p = 0; p < numPoints; p++) {
7780: PetscInt dof;
7781: PetscCall(PetscSectionGetDof(section, points[2 * p], &dof));
7782: clsize += dof;
7783: }
7784: PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &clperm));
7785: /* Get array */
7786: PetscCall(VecGetArray(v, &array));
7787: /* Get values */
7788: if (numFields > 0) {
7789: PetscInt offset = 0, f;
7790: for (f = 0; f < numFields; ++f) {
7791: const PetscInt **perms = NULL;
7792: const PetscScalar **flips = NULL;
7794: PetscCall(PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips));
7795: switch (mode) {
7796: case INSERT_VALUES:
7797: for (p = 0; p < numPoints; p++) {
7798: const PetscInt point = points[2 * p];
7799: const PetscInt *perm = perms ? perms[p] : NULL;
7800: const PetscScalar *flip = flips ? flips[p] : NULL;
7801: PetscCall(updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array));
7802: }
7803: break;
7804: case INSERT_ALL_VALUES:
7805: for (p = 0; p < numPoints; p++) {
7806: const PetscInt point = points[2 * p];
7807: const PetscInt *perm = perms ? perms[p] : NULL;
7808: const PetscScalar *flip = flips ? flips[p] : NULL;
7809: PetscCall(updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array));
7810: }
7811: break;
7812: case INSERT_BC_VALUES:
7813: for (p = 0; p < numPoints; p++) {
7814: const PetscInt point = points[2 * p];
7815: const PetscInt *perm = perms ? perms[p] : NULL;
7816: const PetscScalar *flip = flips ? flips[p] : NULL;
7817: PetscCall(updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array));
7818: }
7819: break;
7820: case ADD_VALUES:
7821: for (p = 0; p < numPoints; p++) {
7822: const PetscInt point = points[2 * p];
7823: const PetscInt *perm = perms ? perms[p] : NULL;
7824: const PetscScalar *flip = flips ? flips[p] : NULL;
7825: PetscCall(updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array));
7826: }
7827: break;
7828: case ADD_ALL_VALUES:
7829: for (p = 0; p < numPoints; p++) {
7830: const PetscInt point = points[2 * p];
7831: const PetscInt *perm = perms ? perms[p] : NULL;
7832: const PetscScalar *flip = flips ? flips[p] : NULL;
7833: PetscCall(updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array));
7834: }
7835: break;
7836: case ADD_BC_VALUES:
7837: for (p = 0; p < numPoints; p++) {
7838: const PetscInt point = points[2 * p];
7839: const PetscInt *perm = perms ? perms[p] : NULL;
7840: const PetscScalar *flip = flips ? flips[p] : NULL;
7841: PetscCall(updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array));
7842: }
7843: break;
7844: default:
7845: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
7846: }
7847: PetscCall(PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips));
7848: }
7849: } else {
7850: PetscInt dof, off;
7851: const PetscInt **perms = NULL;
7852: const PetscScalar **flips = NULL;
7854: PetscCall(PetscSectionGetPointSyms(section, numPoints, points, &perms, &flips));
7855: switch (mode) {
7856: case INSERT_VALUES:
7857: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7858: const PetscInt point = points[2 * p];
7859: const PetscInt *perm = perms ? perms[p] : NULL;
7860: const PetscScalar *flip = flips ? flips[p] : NULL;
7861: PetscCall(PetscSectionGetDof(section, point, &dof));
7862: PetscCall(updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array));
7863: }
7864: break;
7865: case INSERT_ALL_VALUES:
7866: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7867: const PetscInt point = points[2 * p];
7868: const PetscInt *perm = perms ? perms[p] : NULL;
7869: const PetscScalar *flip = flips ? flips[p] : NULL;
7870: PetscCall(PetscSectionGetDof(section, point, &dof));
7871: PetscCall(updatePoint_private(section, point, dof, insert, PETSC_TRUE, perm, flip, clperm, values, off, array));
7872: }
7873: break;
7874: case INSERT_BC_VALUES:
7875: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7876: const PetscInt point = points[2 * p];
7877: const PetscInt *perm = perms ? perms[p] : NULL;
7878: const PetscScalar *flip = flips ? flips[p] : NULL;
7879: PetscCall(PetscSectionGetDof(section, point, &dof));
7880: PetscCall(updatePointBC_private(section, point, dof, insert, perm, flip, clperm, values, off, array));
7881: }
7882: break;
7883: case ADD_VALUES:
7884: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7885: const PetscInt point = points[2 * p];
7886: const PetscInt *perm = perms ? perms[p] : NULL;
7887: const PetscScalar *flip = flips ? flips[p] : NULL;
7888: PetscCall(PetscSectionGetDof(section, point, &dof));
7889: PetscCall(updatePoint_private(section, point, dof, add, PETSC_FALSE, perm, flip, clperm, values, off, array));
7890: }
7891: break;
7892: case ADD_ALL_VALUES:
7893: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7894: const PetscInt point = points[2 * p];
7895: const PetscInt *perm = perms ? perms[p] : NULL;
7896: const PetscScalar *flip = flips ? flips[p] : NULL;
7897: PetscCall(PetscSectionGetDof(section, point, &dof));
7898: PetscCall(updatePoint_private(section, point, dof, add, PETSC_TRUE, perm, flip, clperm, values, off, array));
7899: }
7900: break;
7901: case ADD_BC_VALUES:
7902: for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7903: const PetscInt point = points[2 * p];
7904: const PetscInt *perm = perms ? perms[p] : NULL;
7905: const PetscScalar *flip = flips ? flips[p] : NULL;
7906: PetscCall(PetscSectionGetDof(section, point, &dof));
7907: PetscCall(updatePointBC_private(section, point, dof, add, perm, flip, clperm, values, off, array));
7908: }
7909: break;
7910: default:
7911: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
7912: }
7913: PetscCall(PetscSectionRestorePointSyms(section, numPoints, points, &perms, &flips));
7914: }
7915: /* Cleanup points */
7916: PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
7917: /* Cleanup array */
7918: PetscCall(VecRestoreArray(v, &array));
7919: PetscFunctionReturn(PETSC_SUCCESS);
7920: }
7922: /* Check whether the given point is in the label. If not, update the offset to skip this point */
7923: static inline PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset, PetscBool *contains)
7924: {
7925: PetscFunctionBegin;
7926: *contains = PETSC_TRUE;
7927: if (label) {
7928: PetscInt fdof;
7930: PetscCall(DMLabelStratumHasPoint(label, labelId, point, contains));
7931: if (!*contains) {
7932: PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
7933: *offset += fdof;
7934: PetscFunctionReturn(PETSC_SUCCESS);
7935: }
7936: }
7937: PetscFunctionReturn(PETSC_SUCCESS);
7938: }
7940: /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
7941: 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)
7942: {
7943: PetscSection clSection;
7944: IS clPoints;
7945: PetscScalar *array;
7946: PetscInt *points = NULL;
7947: const PetscInt *clp;
7948: PetscInt numFields, numPoints, p;
7949: PetscInt offset = 0, f;
7951: PetscFunctionBeginHot;
7953: if (!section) PetscCall(DMGetLocalSection(dm, §ion));
7956: PetscCall(PetscSectionGetNumFields(section, &numFields));
7957: /* Get points */
7958: PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &numPoints, &points, &clSection, &clPoints, &clp));
7959: /* Get array */
7960: PetscCall(VecGetArray(v, &array));
7961: /* Get values */
7962: for (f = 0; f < numFields; ++f) {
7963: const PetscInt **perms = NULL;
7964: const PetscScalar **flips = NULL;
7965: PetscBool contains;
7967: if (!fieldActive[f]) {
7968: for (p = 0; p < numPoints * 2; p += 2) {
7969: PetscInt fdof;
7970: PetscCall(PetscSectionGetFieldDof(section, points[p], f, &fdof));
7971: offset += fdof;
7972: }
7973: continue;
7974: }
7975: PetscCall(PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips));
7976: switch (mode) {
7977: case INSERT_VALUES:
7978: for (p = 0; p < numPoints; p++) {
7979: const PetscInt point = points[2 * p];
7980: const PetscInt *perm = perms ? perms[p] : NULL;
7981: const PetscScalar *flip = flips ? flips[p] : NULL;
7982: PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
7983: if (!contains) continue;
7984: PetscCall(updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array));
7985: }
7986: break;
7987: case INSERT_ALL_VALUES:
7988: for (p = 0; p < numPoints; p++) {
7989: const PetscInt point = points[2 * p];
7990: const PetscInt *perm = perms ? perms[p] : NULL;
7991: const PetscScalar *flip = flips ? flips[p] : NULL;
7992: PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
7993: if (!contains) continue;
7994: PetscCall(updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array));
7995: }
7996: break;
7997: case INSERT_BC_VALUES:
7998: for (p = 0; p < numPoints; p++) {
7999: const PetscInt point = points[2 * p];
8000: const PetscInt *perm = perms ? perms[p] : NULL;
8001: const PetscScalar *flip = flips ? flips[p] : NULL;
8002: PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
8003: if (!contains) continue;
8004: PetscCall(updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array));
8005: }
8006: break;
8007: case ADD_VALUES:
8008: for (p = 0; p < numPoints; p++) {
8009: const PetscInt point = points[2 * p];
8010: const PetscInt *perm = perms ? perms[p] : NULL;
8011: const PetscScalar *flip = flips ? flips[p] : NULL;
8012: PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
8013: if (!contains) continue;
8014: PetscCall(updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array));
8015: }
8016: break;
8017: case ADD_ALL_VALUES:
8018: for (p = 0; p < numPoints; p++) {
8019: const PetscInt point = points[2 * p];
8020: const PetscInt *perm = perms ? perms[p] : NULL;
8021: const PetscScalar *flip = flips ? flips[p] : NULL;
8022: PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
8023: if (!contains) continue;
8024: PetscCall(updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array));
8025: }
8026: break;
8027: default:
8028: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
8029: }
8030: PetscCall(PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips));
8031: }
8032: /* Cleanup points */
8033: PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
8034: /* Cleanup array */
8035: PetscCall(VecRestoreArray(v, &array));
8036: PetscFunctionReturn(PETSC_SUCCESS);
8037: }
8039: static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
8040: {
8041: PetscMPIInt rank;
8042: PetscInt i, j;
8044: PetscFunctionBegin;
8045: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
8046: PetscCall(PetscViewerASCIIPrintf(viewer, "[%d]mat for point %" PetscInt_FMT "\n", rank, point));
8047: for (i = 0; i < numRIndices; i++) PetscCall(PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%" PetscInt_FMT "] = %" PetscInt_FMT "\n", rank, i, rindices[i]));
8048: for (i = 0; i < numCIndices; i++) PetscCall(PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%" PetscInt_FMT "] = %" PetscInt_FMT "\n", rank, i, cindices[i]));
8049: numCIndices = numCIndices ? numCIndices : numRIndices;
8050: if (!values) PetscFunctionReturn(PETSC_SUCCESS);
8051: for (i = 0; i < numRIndices; i++) {
8052: PetscCall(PetscViewerASCIIPrintf(viewer, "[%d]", rank));
8053: for (j = 0; j < numCIndices; j++) {
8054: #if defined(PETSC_USE_COMPLEX)
8055: PetscCall(PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i * numCIndices + j]), (double)PetscImaginaryPart(values[i * numCIndices + j])));
8056: #else
8057: PetscCall(PetscViewerASCIIPrintf(viewer, " %g", (double)values[i * numCIndices + j]));
8058: #endif
8059: }
8060: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
8061: }
8062: PetscFunctionReturn(PETSC_SUCCESS);
8063: }
8065: /*
8066: DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
8068: Input Parameters:
8069: + section - The section for this data layout
8070: . islocal - Is the section (and thus indices being requested) local or global?
8071: . point - The point contributing dofs with these indices
8072: . off - The global offset of this point
8073: . loff - The local offset of each field
8074: . setBC - The flag determining whether to include indices of boundary values
8075: . perm - A permutation of the dofs on this point, or NULL
8076: - indperm - A permutation of the entire indices array, or NULL
8078: Output Parameter:
8079: . indices - Indices for dofs on this point
8081: Level: developer
8083: Note: The indices could be local or global, depending on the value of 'off'.
8084: */
8085: PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
8086: {
8087: PetscInt dof; /* The number of unknowns on this point */
8088: PetscInt cdof; /* The number of constraints on this point */
8089: const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
8090: PetscInt cind = 0, k;
8092: PetscFunctionBegin;
8093: PetscCheck(islocal || !setBC, PetscObjectComm((PetscObject)section), PETSC_ERR_ARG_INCOMP, "setBC incompatible with global indices; use a local section or disable setBC");
8094: PetscCall(PetscSectionGetDof(section, point, &dof));
8095: PetscCall(PetscSectionGetConstraintDof(section, point, &cdof));
8096: if (!cdof || setBC) {
8097: for (k = 0; k < dof; ++k) {
8098: const PetscInt preind = perm ? *loff + perm[k] : *loff + k;
8099: const PetscInt ind = indperm ? indperm[preind] : preind;
8101: indices[ind] = off + k;
8102: }
8103: } else {
8104: PetscCall(PetscSectionGetConstraintIndices(section, point, &cdofs));
8105: for (k = 0; k < dof; ++k) {
8106: const PetscInt preind = perm ? *loff + perm[k] : *loff + k;
8107: const PetscInt ind = indperm ? indperm[preind] : preind;
8109: if ((cind < cdof) && (k == cdofs[cind])) {
8110: /* Insert check for returning constrained indices */
8111: indices[ind] = -(off + k + 1);
8112: ++cind;
8113: } else {
8114: indices[ind] = off + k - (islocal ? 0 : cind);
8115: }
8116: }
8117: }
8118: *loff += dof;
8119: PetscFunctionReturn(PETSC_SUCCESS);
8120: }
8122: /*
8123: DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.
8125: Input Parameters:
8126: + section - a section (global or local)
8127: - islocal - `PETSC_TRUE` if requesting local indices (i.e., section is local); `PETSC_FALSE` for global
8128: . point - point within section
8129: . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
8130: . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
8131: . setBC - identify constrained (boundary condition) points via involution.
8132: . perms - perms[f][permsoff][:] is a permutation of dofs within each field
8133: . permsoff - offset
8134: - indperm - index permutation
8136: Output Parameter:
8137: . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
8138: . indices - array to hold indices (as defined by section) of each dof associated with point
8140: Notes:
8141: If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
8142: If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
8143: in the local vector.
8145: If section is global and setBC=false, the indices for constrained points are negative (and their value is not
8146: significant). It is invalid to call with a global section and setBC=true.
8148: Developer Note:
8149: The section is only used for field layout, so islocal is technically a statement about the offset (off). At some point
8150: in the future, global sections may have fields set, in which case we could pass the global section and obtain the
8151: offset could be obtained from the section instead of passing it explicitly as we do now.
8153: Example:
8154: Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
8155: When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
8156: Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
8157: 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.
8159: Level: developer
8160: */
8161: 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[])
8162: {
8163: PetscInt numFields, foff, f;
8165: PetscFunctionBegin;
8166: PetscCheck(islocal || !setBC, PetscObjectComm((PetscObject)section), PETSC_ERR_ARG_INCOMP, "setBC incompatible with global indices; use a local section or disable setBC");
8167: PetscCall(PetscSectionGetNumFields(section, &numFields));
8168: for (f = 0, foff = 0; f < numFields; ++f) {
8169: PetscInt fdof, cfdof;
8170: const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
8171: PetscInt cind = 0, b;
8172: const PetscInt *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
8174: PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
8175: PetscCall(PetscSectionGetFieldConstraintDof(section, point, f, &cfdof));
8176: if (!cfdof || setBC) {
8177: for (b = 0; b < fdof; ++b) {
8178: const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
8179: const PetscInt ind = indperm ? indperm[preind] : preind;
8181: indices[ind] = off + foff + b;
8182: }
8183: } else {
8184: PetscCall(PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs));
8185: for (b = 0; b < fdof; ++b) {
8186: const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
8187: const PetscInt ind = indperm ? indperm[preind] : preind;
8189: if ((cind < cfdof) && (b == fcdofs[cind])) {
8190: indices[ind] = -(off + foff + b + 1);
8191: ++cind;
8192: } else {
8193: indices[ind] = off + foff + b - (islocal ? 0 : cind);
8194: }
8195: }
8196: }
8197: foff += (setBC || islocal ? fdof : (fdof - cfdof));
8198: foffs[f] += fdof;
8199: }
8200: PetscFunctionReturn(PETSC_SUCCESS);
8201: }
8203: /*
8204: This version believes the globalSection offsets for each field, rather than just the point offset
8206: . foffs - The offset into 'indices' for each field, since it is segregated by field
8208: Notes:
8209: The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
8210: Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
8211: */
8212: static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
8213: {
8214: PetscInt numFields, foff, f;
8216: PetscFunctionBegin;
8217: PetscCall(PetscSectionGetNumFields(section, &numFields));
8218: for (f = 0; f < numFields; ++f) {
8219: PetscInt fdof, cfdof;
8220: const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
8221: PetscInt cind = 0, b;
8222: const PetscInt *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
8224: PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
8225: PetscCall(PetscSectionGetFieldConstraintDof(section, point, f, &cfdof));
8226: PetscCall(PetscSectionGetFieldOffset(globalSection, point, f, &foff));
8227: if (!cfdof) {
8228: for (b = 0; b < fdof; ++b) {
8229: const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
8230: const PetscInt ind = indperm ? indperm[preind] : preind;
8232: indices[ind] = foff + b;
8233: }
8234: } else {
8235: PetscCall(PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs));
8236: for (b = 0; b < fdof; ++b) {
8237: const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
8238: const PetscInt ind = indperm ? indperm[preind] : preind;
8240: if ((cind < cfdof) && (b == fcdofs[cind])) {
8241: indices[ind] = -(foff + b + 1);
8242: ++cind;
8243: } else {
8244: indices[ind] = foff + b - cind;
8245: }
8246: }
8247: }
8248: foffs[f] += fdof;
8249: }
8250: PetscFunctionReturn(PETSC_SUCCESS);
8251: }
8253: static PetscErrorCode DMPlexAnchorsGetSubMatIndices(PetscInt nPoints, const PetscInt pnts[], PetscSection section, PetscSection cSec, PetscInt tmpIndices[], PetscInt fieldOffsets[], PetscInt indices[], const PetscInt ***perms)
8254: {
8255: PetscInt numFields, sStart, sEnd, cStart, cEnd;
8257: PetscFunctionBegin;
8258: PetscCall(PetscSectionGetNumFields(section, &numFields));
8259: PetscCall(PetscSectionGetChart(section, &sStart, &sEnd));
8260: PetscCall(PetscSectionGetChart(cSec, &cStart, &cEnd));
8261: for (PetscInt p = 0; p < nPoints; p++) {
8262: PetscInt b = pnts[2 * p];
8263: PetscInt bSecDof = 0, bOff;
8264: PetscInt cSecDof = 0;
8265: PetscSection indices_section;
8267: if (b >= sStart && b < sEnd) PetscCall(PetscSectionGetDof(section, b, &bSecDof));
8268: if (!bSecDof) continue;
8269: if (b >= cStart && b < cEnd) PetscCall(PetscSectionGetDof(cSec, b, &cSecDof));
8270: indices_section = cSecDof > 0 ? cSec : section;
8271: if (numFields) {
8272: PetscInt fStart[32], fEnd[32];
8274: fStart[0] = 0;
8275: fEnd[0] = 0;
8276: for (PetscInt f = 0; f < numFields; f++) {
8277: PetscInt fDof = 0;
8279: PetscCall(PetscSectionGetFieldDof(indices_section, b, f, &fDof));
8280: fStart[f + 1] = fStart[f] + fDof;
8281: fEnd[f + 1] = fStart[f + 1];
8282: }
8283: PetscCall(PetscSectionGetOffset(indices_section, b, &bOff));
8284: // only apply permutations on one side
8285: PetscCall(DMPlexGetIndicesPointFields_Internal(indices_section, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, perms ? p : -1, NULL, tmpIndices));
8286: for (PetscInt f = 0; f < numFields; f++) {
8287: for (PetscInt i = fStart[f]; i < fEnd[f]; i++) indices[fieldOffsets[f]++] = (cSecDof > 0) ? tmpIndices[i] : -(tmpIndices[i] + 1);
8288: }
8289: } else {
8290: PetscInt bEnd = 0;
8292: PetscCall(PetscSectionGetOffset(indices_section, b, &bOff));
8293: PetscCall(DMPlexGetIndicesPoint_Internal(indices_section, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, tmpIndices));
8295: for (PetscInt i = 0; i < bEnd; i++) indices[fieldOffsets[0]++] = (cSecDof > 0) ? tmpIndices[i] : -(tmpIndices[i] + 1);
8296: }
8297: }
8298: PetscFunctionReturn(PETSC_SUCCESS);
8299: }
8301: PETSC_INTERN PetscErrorCode DMPlexAnchorsGetSubMatModification(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscInt ***perms, PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscInt offsets[], PetscScalar *outMat[])
8302: {
8303: Mat cMat;
8304: PetscSection aSec, cSec;
8305: IS aIS;
8306: PetscInt aStart = -1, aEnd = -1;
8307: PetscInt sStart = -1, sEnd = -1;
8308: PetscInt cStart = -1, cEnd = -1;
8309: const PetscInt *anchors;
8310: PetscInt numFields, p;
8311: PetscInt newNumPoints = 0, newNumIndices = 0;
8312: PetscInt *newPoints, *indices, *newIndices, *tmpIndices, *tmpNewIndices;
8313: PetscInt oldOffsets[32];
8314: PetscInt newOffsets[32];
8315: PetscInt oldOffsetsCopy[32];
8316: PetscInt newOffsetsCopy[32];
8317: PetscScalar *modMat = NULL;
8318: PetscBool anyConstrained = PETSC_FALSE;
8320: PetscFunctionBegin;
8323: PetscCall(PetscSectionGetNumFields(section, &numFields));
8325: PetscCall(DMPlexGetAnchors(dm, &aSec, &aIS));
8326: /* if there are point-to-point constraints */
8327: if (aSec) {
8328: PetscCall(PetscArrayzero(newOffsets, 32));
8329: PetscCall(PetscArrayzero(oldOffsets, 32));
8330: PetscCall(ISGetIndices(aIS, &anchors));
8331: PetscCall(PetscSectionGetChart(aSec, &aStart, &aEnd));
8332: PetscCall(PetscSectionGetChart(section, &sStart, &sEnd));
8333: /* figure out how many points are going to be in the new element matrix
8334: * (we allow double counting, because it's all just going to be summed
8335: * into the global matrix anyway) */
8336: for (p = 0; p < 2 * numPoints; p += 2) {
8337: PetscInt b = points[p];
8338: PetscInt bDof = 0, bSecDof = 0;
8340: if (b >= sStart && b < sEnd) PetscCall(PetscSectionGetDof(section, b, &bSecDof));
8341: if (!bSecDof) continue;
8343: for (PetscInt f = 0; f < numFields; f++) {
8344: PetscInt fDof = 0;
8346: PetscCall(PetscSectionGetFieldDof(section, b, f, &fDof));
8347: oldOffsets[f + 1] += fDof;
8348: }
8349: if (b >= aStart && b < aEnd) PetscCall(PetscSectionGetDof(aSec, b, &bDof));
8350: if (bDof) {
8351: /* this point is constrained */
8352: /* it is going to be replaced by its anchors */
8353: PetscInt bOff, q;
8355: PetscCall(PetscSectionGetOffset(aSec, b, &bOff));
8356: for (q = 0; q < bDof; q++) {
8357: PetscInt a = anchors[bOff + q];
8358: PetscInt aDof = 0;
8360: if (a >= sStart && a < sEnd) PetscCall(PetscSectionGetDof(section, a, &aDof));
8361: if (aDof) {
8362: anyConstrained = PETSC_TRUE;
8363: newNumPoints += 1;
8364: }
8365: newNumIndices += aDof;
8366: for (PetscInt f = 0; f < numFields; ++f) {
8367: PetscInt fDof = 0;
8369: if (a >= sStart && a < sEnd) PetscCall(PetscSectionGetFieldDof(section, a, f, &fDof));
8370: newOffsets[f + 1] += fDof;
8371: }
8372: }
8373: } else {
8374: /* this point is not constrained */
8375: newNumPoints++;
8376: newNumIndices += bSecDof;
8377: for (PetscInt f = 0; f < numFields; ++f) {
8378: PetscInt fDof;
8380: PetscCall(PetscSectionGetFieldDof(section, b, f, &fDof));
8381: newOffsets[f + 1] += fDof;
8382: }
8383: }
8384: }
8385: }
8386: if (!anyConstrained) {
8387: if (outNumPoints) *outNumPoints = 0;
8388: if (outNumIndices) *outNumIndices = 0;
8389: if (outPoints) *outPoints = NULL;
8390: if (outMat) *outMat = NULL;
8391: if (aSec) PetscCall(ISRestoreIndices(aIS, &anchors));
8392: PetscFunctionReturn(PETSC_SUCCESS);
8393: }
8395: if (outNumPoints) *outNumPoints = newNumPoints;
8396: if (outNumIndices) *outNumIndices = newNumIndices;
8398: for (PetscInt f = 0; f < numFields; ++f) newOffsets[f + 1] += newOffsets[f];
8399: for (PetscInt f = 0; f < numFields; ++f) oldOffsets[f + 1] += oldOffsets[f];
8401: if (!outPoints && !outMat) {
8402: if (offsets) {
8403: for (PetscInt f = 0; f <= numFields; f++) offsets[f] = newOffsets[f];
8404: }
8405: if (aSec) PetscCall(ISRestoreIndices(aIS, &anchors));
8406: PetscFunctionReturn(PETSC_SUCCESS);
8407: }
8409: PetscCheck(!numFields || newOffsets[numFields] == newNumIndices, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, newOffsets[numFields], newNumIndices);
8410: PetscCheck(!numFields || oldOffsets[numFields] == numIndices, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, oldOffsets[numFields], numIndices);
8412: PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
8413: PetscCall(PetscSectionGetChart(cSec, &cStart, &cEnd));
8415: /* output arrays */
8416: PetscCall(DMGetWorkArray(dm, 2 * newNumPoints, MPIU_INT, &newPoints));
8417: PetscCall(PetscArrayzero(newPoints, 2 * newNumPoints));
8419: // get the new Points
8420: for (PetscInt p = 0, newP = 0; p < numPoints; p++) {
8421: PetscInt b = points[2 * p];
8422: PetscInt bDof = 0, bSecDof = 0, bOff;
8424: if (b >= sStart && b < sEnd) PetscCall(PetscSectionGetDof(section, b, &bSecDof));
8425: if (!bSecDof) continue;
8426: if (b >= aStart && b < aEnd) PetscCall(PetscSectionGetDof(aSec, b, &bDof));
8427: if (bDof) {
8428: PetscCall(PetscSectionGetOffset(aSec, b, &bOff));
8429: for (PetscInt q = 0; q < bDof; q++) {
8430: PetscInt a = anchors[bOff + q], aDof = 0;
8432: if (a >= sStart && a < sEnd) PetscCall(PetscSectionGetDof(section, a, &aDof));
8433: if (aDof) {
8434: newPoints[2 * newP] = a;
8435: newPoints[2 * newP + 1] = 0; // orientations are accounted for in constructing the matrix, newly added points are in default orientation
8436: newP++;
8437: }
8438: }
8439: } else {
8440: newPoints[2 * newP] = b;
8441: newPoints[2 * newP + 1] = points[2 * p + 1];
8442: newP++;
8443: }
8444: }
8446: if (outMat) {
8447: PetscScalar *tmpMat;
8448: PetscCall(PetscArraycpy(oldOffsetsCopy, oldOffsets, 32));
8449: PetscCall(PetscArraycpy(newOffsetsCopy, newOffsets, 32));
8451: PetscCall(DMGetWorkArray(dm, numIndices, MPIU_INT, &indices));
8452: PetscCall(DMGetWorkArray(dm, numIndices, MPIU_INT, &tmpIndices));
8453: PetscCall(DMGetWorkArray(dm, newNumIndices, MPIU_INT, &newIndices));
8454: PetscCall(DMGetWorkArray(dm, newNumIndices, MPIU_INT, &tmpNewIndices));
8456: for (PetscInt i = 0; i < numIndices; i++) indices[i] = -1;
8457: for (PetscInt i = 0; i < newNumIndices; i++) newIndices[i] = -1;
8459: PetscCall(DMPlexAnchorsGetSubMatIndices(numPoints, points, section, cSec, tmpIndices, oldOffsetsCopy, indices, perms));
8460: PetscCall(DMPlexAnchorsGetSubMatIndices(newNumPoints, newPoints, section, section, tmpNewIndices, newOffsetsCopy, newIndices, NULL));
8462: PetscCall(DMGetWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &modMat));
8463: PetscCall(DMGetWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &tmpMat));
8464: PetscCall(PetscArrayzero(modMat, newNumIndices * numIndices));
8465: // for each field, insert the anchor modification into modMat
8466: for (PetscInt f = 0; f < PetscMax(1, numFields); f++) {
8467: PetscInt fStart = oldOffsets[f];
8468: PetscInt fNewStart = newOffsets[f];
8469: for (PetscInt p = 0, o = fStart, oNew = fNewStart; p < numPoints; p++) {
8470: PetscInt b = points[2 * p];
8471: PetscInt bDof = 0, bSecDof = 0, bOff;
8473: if (b >= sStart && b < sEnd) {
8474: if (numFields) PetscCall(PetscSectionGetFieldDof(section, b, f, &bSecDof));
8475: else PetscCall(PetscSectionGetDof(section, b, &bSecDof));
8476: }
8477: if (!bSecDof) continue;
8478: if (b >= aStart && b < aEnd) PetscCall(PetscSectionGetDof(aSec, b, &bDof));
8479: if (bDof) {
8480: PetscCall(PetscSectionGetOffset(aSec, b, &bOff));
8481: for (PetscInt q = 0; q < bDof; q++) {
8482: PetscInt a = anchors[bOff + q], aDof = 0;
8484: if (a >= sStart && a < sEnd) {
8485: if (numFields) PetscCall(PetscSectionGetFieldDof(section, a, f, &aDof));
8486: else PetscCall(PetscSectionGetDof(section, a, &aDof));
8487: }
8488: if (aDof) {
8489: PetscCall(MatGetValues(cMat, bSecDof, &indices[o], aDof, &newIndices[oNew], tmpMat));
8490: for (PetscInt d = 0; d < bSecDof; d++) {
8491: for (PetscInt e = 0; e < aDof; e++) modMat[(o + d) * newNumIndices + oNew + e] = tmpMat[d * aDof + e];
8492: }
8493: }
8494: oNew += aDof;
8495: }
8496: } else {
8497: // Insert the identity matrix in this block
8498: for (PetscInt d = 0; d < bSecDof; d++) modMat[(o + d) * newNumIndices + oNew + d] = 1;
8499: oNew += bSecDof;
8500: }
8501: o += bSecDof;
8502: }
8503: }
8505: *outMat = modMat;
8507: PetscCall(DMRestoreWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &tmpMat));
8508: PetscCall(DMRestoreWorkArray(dm, newNumIndices, MPIU_INT, &tmpNewIndices));
8509: PetscCall(DMRestoreWorkArray(dm, newNumIndices, MPIU_INT, &newIndices));
8510: PetscCall(DMRestoreWorkArray(dm, numIndices, MPIU_INT, &tmpIndices));
8511: PetscCall(DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices));
8512: }
8513: PetscCall(ISRestoreIndices(aIS, &anchors));
8515: /* output */
8516: if (outPoints) {
8517: *outPoints = newPoints;
8518: } else {
8519: PetscCall(DMRestoreWorkArray(dm, 2 * newNumPoints, MPIU_INT, &newPoints));
8520: }
8521: for (PetscInt f = 0; f <= numFields; f++) offsets[f] = newOffsets[f];
8522: PetscFunctionReturn(PETSC_SUCCESS);
8523: }
8525: PETSC_INTERN PetscErrorCode DMPlexAnchorsModifyMat_Internal(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscInt ***perms, PetscInt numRows, PetscInt numCols, const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[], PetscBool multiplyRight, PetscBool multiplyLeft)
8526: {
8527: PetscScalar *modMat = NULL;
8528: PetscInt newNumIndices = -1;
8530: PetscFunctionBegin;
8531: /* If M is the matrix represented by values, get the matrix C such that we will add M * C (or, if multiplyLeft, C^T * M * C) into the global matrix.
8532: modMat is that matrix C */
8533: PetscCall(DMPlexAnchorsGetSubMatModification(dm, section, numPoints, numIndices, points, perms, outNumPoints, &newNumIndices, outPoints, offsets, outValues ? &modMat : NULL));
8534: if (outNumIndices) *outNumIndices = newNumIndices;
8535: if (modMat) {
8536: const PetscScalar *newValues = values;
8538: if (multiplyRight) {
8539: PetscScalar *newNewValues = NULL;
8540: PetscBLASInt M, N, K;
8541: PetscScalar a = 1.0, b = 0.0;
8543: PetscCheck(numCols == numIndices, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "values matrix has the wrong number of columns: %" PetscInt_FMT ", expected %" PetscInt_FMT, numCols, numIndices);
8545: PetscCall(PetscBLASIntCast(newNumIndices, &M));
8546: PetscCall(PetscBLASIntCast(numRows, &N));
8547: PetscCall(PetscBLASIntCast(numIndices, &K));
8548: PetscCall(DMGetWorkArray(dm, numRows * newNumIndices, MPIU_SCALAR, &newNewValues));
8549: // row-major to column-major conversion, right multiplication becomes left multiplication
8550: PetscCallBLAS("BLASgemm", BLASgemm_("N", "N", &M, &N, &K, &a, modMat, &M, newValues, &K, &b, newNewValues, &M));
8551: numCols = newNumIndices;
8552: newValues = newNewValues;
8553: }
8555: if (multiplyLeft) {
8556: PetscScalar *newNewValues = NULL;
8557: PetscBLASInt M, N, K;
8558: PetscScalar a = 1.0, b = 0.0;
8560: PetscCheck(numRows == numIndices, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "values matrix has the wrong number of rows: %" PetscInt_FMT ", expected %" PetscInt_FMT, numRows, numIndices);
8562: PetscCall(PetscBLASIntCast(numCols, &M));
8563: PetscCall(PetscBLASIntCast(newNumIndices, &N));
8564: PetscCall(PetscBLASIntCast(numIndices, &K));
8565: PetscCall(DMGetWorkArray(dm, newNumIndices * numCols, MPIU_SCALAR, &newNewValues));
8566: // row-major to column-major conversion, left multiplication becomes right multiplication
8567: PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &M, &N, &K, &a, newValues, &M, modMat, &N, &b, newNewValues, &M));
8568: if (newValues != values) PetscCall(DMRestoreWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &newValues));
8569: newValues = newNewValues;
8570: }
8571: *outValues = (PetscScalar *)newValues;
8572: PetscCall(DMRestoreWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &modMat));
8573: }
8574: PetscFunctionReturn(PETSC_SUCCESS);
8575: }
8577: PETSC_INTERN 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)
8578: {
8579: PetscFunctionBegin;
8580: PetscCall(DMPlexAnchorsModifyMat_Internal(dm, section, numPoints, numIndices, points, perms, numIndices, numIndices, values, outNumPoints, outNumIndices, outPoints, outValues, offsets, PETSC_TRUE, multiplyLeft));
8581: PetscFunctionReturn(PETSC_SUCCESS);
8582: }
8584: static PetscErrorCode DMPlexGetClosureIndicesSize_Internal(DM dm, PetscSection section, PetscInt point, PetscInt *closureSize)
8585: {
8586: /* Closure ordering */
8587: PetscSection clSection;
8588: IS clPoints;
8589: const PetscInt *clp;
8590: PetscInt *points;
8591: PetscInt Ncl, Ni = 0;
8593: PetscFunctionBeginHot;
8594: PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &Ncl, &points, &clSection, &clPoints, &clp));
8595: for (PetscInt p = 0; p < Ncl * 2; p += 2) {
8596: PetscInt dof;
8598: PetscCall(PetscSectionGetDof(section, points[p], &dof));
8599: Ni += dof;
8600: }
8601: PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp));
8602: *closureSize = Ni;
8603: PetscFunctionReturn(PETSC_SUCCESS);
8604: }
8606: static PetscErrorCode DMPlexGetClosureIndices_Internal(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numRows, PetscInt *numCols, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[], PetscBool multiplyRight, PetscBool multiplyLeft)
8607: {
8608: /* Closure ordering */
8609: PetscSection clSection;
8610: IS clPoints;
8611: const PetscInt *clp;
8612: PetscInt *points;
8613: const PetscInt *clperm = NULL;
8614: /* Dof permutation and sign flips */
8615: const PetscInt **perms[32] = {NULL};
8616: const PetscScalar **flips[32] = {NULL};
8617: PetscScalar *valCopy = NULL;
8618: /* Hanging node constraints */
8619: PetscInt *pointsC = NULL;
8620: PetscScalar *valuesC = NULL;
8621: PetscInt NclC, NiC;
8623: PetscInt *idx;
8624: PetscInt Nf, Ncl, Ni = 0, offsets[32], p, f;
8625: PetscBool isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
8626: PetscInt idxStart, idxEnd;
8627: PetscInt nRows, nCols;
8629: PetscFunctionBeginHot;
8633: PetscAssertPointer(numRows, 6);
8634: PetscAssertPointer(numCols, 7);
8635: if (indices) PetscAssertPointer(indices, 8);
8636: if (outOffsets) PetscAssertPointer(outOffsets, 9);
8637: if (values) PetscAssertPointer(values, 10);
8638: PetscCall(PetscSectionGetNumFields(section, &Nf));
8639: PetscCheck(Nf <= 31, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %" PetscInt_FMT " limited to 31", Nf);
8640: PetscCall(PetscArrayzero(offsets, 32));
8641: /* 1) Get points in closure */
8642: PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &Ncl, &points, &clSection, &clPoints, &clp));
8643: if (useClPerm) {
8644: PetscInt depth, clsize;
8645: PetscCall(DMPlexGetPointDepth(dm, point, &depth));
8646: for (clsize = 0, p = 0; p < Ncl; p++) {
8647: PetscInt dof;
8648: PetscCall(PetscSectionGetDof(section, points[2 * p], &dof));
8649: clsize += dof;
8650: }
8651: PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &clperm));
8652: }
8653: /* 2) Get number of indices on these points and field offsets from section */
8654: for (p = 0; p < Ncl * 2; p += 2) {
8655: PetscInt dof, fdof;
8657: PetscCall(PetscSectionGetDof(section, points[p], &dof));
8658: for (f = 0; f < Nf; ++f) {
8659: PetscCall(PetscSectionGetFieldDof(section, points[p], f, &fdof));
8660: offsets[f + 1] += fdof;
8661: }
8662: Ni += dof;
8663: }
8664: if (*numRows == -1) *numRows = Ni;
8665: if (*numCols == -1) *numCols = Ni;
8666: nRows = *numRows;
8667: nCols = *numCols;
8668: for (f = 1; f < Nf; ++f) offsets[f + 1] += offsets[f];
8669: PetscCheck(!Nf || offsets[Nf] == Ni, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, offsets[Nf], Ni);
8670: /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
8671: if (multiplyRight) PetscCheck(nCols == Ni, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Expected %" PetscInt_FMT " columns, got %" PetscInt_FMT, Ni, nCols);
8672: if (multiplyLeft) PetscCheck(nRows == Ni, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Expected %" PetscInt_FMT " rows, got %" PetscInt_FMT, Ni, nRows);
8673: for (f = 0; f < PetscMax(1, Nf); ++f) {
8674: if (Nf) PetscCall(PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]));
8675: else PetscCall(PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]));
8676: /* may need to apply sign changes to the element matrix */
8677: if (values && flips[f]) {
8678: PetscInt foffset = offsets[f];
8680: for (p = 0; p < Ncl; ++p) {
8681: PetscInt pnt = points[2 * p], fdof;
8682: const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
8684: if (!Nf) PetscCall(PetscSectionGetDof(section, pnt, &fdof));
8685: else PetscCall(PetscSectionGetFieldDof(section, pnt, f, &fdof));
8686: if (flip) {
8687: PetscInt i, j, k;
8689: if (!valCopy) {
8690: PetscCall(DMGetWorkArray(dm, Ni * Ni, MPIU_SCALAR, &valCopy));
8691: for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
8692: *values = valCopy;
8693: }
8694: for (i = 0; i < fdof; ++i) {
8695: PetscScalar fval = flip[i];
8697: if (multiplyRight) {
8698: for (k = 0; k < nRows; ++k) valCopy[Ni * k + (foffset + i)] *= fval;
8699: }
8700: if (multiplyLeft) {
8701: for (k = 0; k < nCols; ++k) valCopy[nCols * (foffset + i) + k] *= fval;
8702: }
8703: }
8704: }
8705: foffset += fdof;
8706: }
8707: }
8708: }
8709: /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
8710: PetscCall(DMPlexAnchorsModifyMat_Internal(dm, section, Ncl, Ni, points, perms, nRows, nCols, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, multiplyRight, multiplyLeft));
8711: if (NclC) {
8712: if (multiplyRight) *numCols = NiC;
8713: if (multiplyLeft) *numRows = NiC;
8714: if (valCopy) PetscCall(DMRestoreWorkArray(dm, Ni * Ni, MPIU_SCALAR, &valCopy));
8715: for (f = 0; f < PetscMax(1, Nf); ++f) {
8716: if (Nf) PetscCall(PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]));
8717: else PetscCall(PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]));
8718: }
8719: for (f = 0; f < PetscMax(1, Nf); ++f) {
8720: if (Nf) PetscCall(PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]));
8721: else PetscCall(PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]));
8722: }
8723: PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp));
8724: Ncl = NclC;
8725: Ni = NiC;
8726: points = pointsC;
8727: if (values) *values = valuesC;
8728: }
8729: /* 5) Calculate indices */
8730: PetscCall(DMGetWorkArray(dm, Ni, MPIU_INT, &idx));
8731: PetscCall(PetscSectionGetChart(idxSection, &idxStart, &idxEnd));
8732: if (Nf) {
8733: PetscInt idxOff;
8734: PetscBool useFieldOffsets;
8736: if (outOffsets) {
8737: for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];
8738: }
8739: PetscCall(PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets));
8740: if (useFieldOffsets) {
8741: for (p = 0; p < Ncl; ++p) {
8742: const PetscInt pnt = points[p * 2];
8744: PetscCall(DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx));
8745: }
8746: } else {
8747: for (p = 0; p < Ncl; ++p) {
8748: const PetscInt pnt = points[p * 2];
8750: if (pnt < idxStart || pnt >= idxEnd) continue;
8751: PetscCall(PetscSectionGetOffset(idxSection, pnt, &idxOff));
8752: /* Note that we pass a local section even though we're using global offsets. This is because global sections do
8753: * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
8754: * global section. */
8755: PetscCall(DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff + 1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx));
8756: }
8757: }
8758: } else {
8759: PetscInt off = 0, idxOff;
8761: for (p = 0; p < Ncl; ++p) {
8762: const PetscInt pnt = points[p * 2];
8763: const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
8765: if (pnt < idxStart || pnt >= idxEnd) continue;
8766: PetscCall(PetscSectionGetOffset(idxSection, pnt, &idxOff));
8767: /* Note that we pass a local section even though we're using global offsets. This is because global sections do
8768: * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
8769: PetscCall(DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff + 1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx));
8770: }
8771: }
8772: /* 6) Cleanup */
8773: for (f = 0; f < PetscMax(1, Nf); ++f) {
8774: if (Nf) PetscCall(PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]));
8775: else PetscCall(PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]));
8776: }
8777: if (NclC) {
8778: PetscCall(DMRestoreWorkArray(dm, NclC * 2, MPIU_INT, &pointsC));
8779: } else {
8780: PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp));
8781: }
8783: if (indices) *indices = idx;
8784: PetscFunctionReturn(PETSC_SUCCESS);
8785: }
8787: /*@C
8788: DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.
8790: Not collective
8792: Input Parameters:
8793: + dm - The `DM`
8794: . section - The `PetscSection` describing the points (a local section)
8795: . idxSection - The `PetscSection` from which to obtain indices (may be local or global)
8796: . point - The point defining the closure
8797: - useClPerm - Use the closure point permutation if available
8799: Output Parameters:
8800: + numIndices - The number of dof indices in the closure of point with the input sections
8801: . indices - The dof indices
8802: . outOffsets - Array, of length the number of fields plus 1, to write the field offsets into, or `NULL`
8803: - values - The input values, which may be modified if sign flips are induced by the point symmetries, or `NULL`
8805: Level: advanced
8807: Notes:
8808: Call `DMPlexRestoreClosureIndices()` to free allocated memory
8810: If `idxSection` is global, any constrained dofs (see `DMAddBoundary()`, for example) will get negative indices. The value
8811: of those indices is not significant. If `idxSection` is local, the constrained dofs will yield the involution -(idx+1)
8812: of their index in a local vector. A caller who does not wish to distinguish those points may recover the nonnegative
8813: indices via involution, -(-(idx+1)+1)==idx. Local indices are provided when `idxSection` == section, otherwise global
8814: indices (with the above semantics) are implied.
8816: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreClosureIndices()`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`, `DMGetLocalSection()`,
8817: `PetscSection`, `DMGetGlobalSection()`
8818: @*/
8819: PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numIndices, PetscInt *indices[], PeOp PetscInt outOffsets[], PeOp PetscScalar *values[])
8820: {
8821: PetscInt numRows = -1, numCols = -1;
8823: PetscFunctionBeginHot;
8824: PetscCall(DMPlexGetClosureIndices_Internal(dm, section, idxSection, point, useClPerm, &numRows, &numCols, indices, outOffsets, values, PETSC_TRUE, PETSC_TRUE));
8825: PetscCheck(numRows == numCols, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Symmetric matrix transformation produces rectangular dimensions (%" PetscInt_FMT ", %" PetscInt_FMT ")", numRows, numCols);
8826: *numIndices = numRows;
8827: PetscFunctionReturn(PETSC_SUCCESS);
8828: }
8830: /*@C
8831: DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.
8833: Not collective
8835: Input Parameters:
8836: + dm - The `DM`
8837: . section - The `PetscSection` describing the points (a local section)
8838: . idxSection - The `PetscSection` from which to obtain indices (may be local or global)
8839: . point - The point defining the closure
8840: - useClPerm - Use the closure point permutation if available
8842: Output Parameters:
8843: + numIndices - The number of dof indices in the closure of point with the input sections
8844: . indices - The dof indices
8845: . outOffsets - Array to write the field offsets into, or `NULL`
8846: - values - The input values, which may be modified if sign flips are induced by the point symmetries, or `NULL`
8848: Level: advanced
8850: Notes:
8851: If values were modified, the user is responsible for calling `DMRestoreWorkArray`(dm, 0, `MPIU_SCALAR`, &values).
8853: If idxSection is global, any constrained dofs (see `DMAddBoundary()`, for example) will get negative indices. The value
8854: of those indices is not significant. If idxSection is local, the constrained dofs will yield the involution -(idx+1)
8855: of their index in a local vector. A caller who does not wish to distinguish those points may recover the nonnegative
8856: indices via involution, -(-(idx+1)+1)==idx. Local indices are provided when idxSection == section, otherwise global
8857: indices (with the above semantics) are implied.
8859: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetClosureIndices()`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
8860: @*/
8861: PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numIndices, PetscInt *indices[], PeOp PetscInt outOffsets[], PeOp PetscScalar *values[])
8862: {
8863: PetscFunctionBegin;
8865: PetscAssertPointer(indices, 7);
8866: PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, indices));
8867: PetscFunctionReturn(PETSC_SUCCESS);
8868: }
8870: PetscErrorCode DMPlexMatSetClosure_Internal(DM dm, PetscSection section, PetscSection globalSection, PetscBool useClPerm, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
8871: {
8872: DM_Plex *mesh = (DM_Plex *)dm->data;
8873: PetscInt *indices;
8874: PetscInt numIndices;
8875: const PetscScalar *valuesOrig = values;
8876: PetscErrorCode ierr;
8878: PetscFunctionBegin;
8880: if (!section) PetscCall(DMGetLocalSection(dm, §ion));
8882: if (!globalSection) PetscCall(DMGetGlobalSection(dm, &globalSection));
8886: PetscCall(DMPlexGetClosureIndices(dm, section, globalSection, point, useClPerm, &numIndices, &indices, NULL, (PetscScalar **)&values));
8888: if (mesh->printSetValues) PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values));
8889: /* TODO: fix this code to not use error codes as handle-able exceptions! */
8890: ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
8891: if (ierr) {
8892: PetscMPIInt rank;
8894: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
8895: PetscCall((*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank));
8896: PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values));
8897: PetscCall(DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values));
8898: if (values != valuesOrig) PetscCall(DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values));
8899: SETERRQ(PetscObjectComm((PetscObject)dm), ierr, "Not possible to set matrix values");
8900: }
8901: if (mesh->printFEM > 1) {
8902: PetscInt i;
8903: PetscCall(PetscPrintf(PETSC_COMM_SELF, " Indices:"));
8904: for (i = 0; i < numIndices; ++i) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, indices[i]));
8905: PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
8906: }
8908: PetscCall(DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values));
8909: if (values != valuesOrig) PetscCall(DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values));
8910: PetscFunctionReturn(PETSC_SUCCESS);
8911: }
8913: /*@C
8914: DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
8916: Not collective
8918: Input Parameters:
8919: + dm - The `DM`
8920: . section - The section describing the layout in `v`, or `NULL` to use the default section
8921: . globalSection - The section describing the layout in `v`, or `NULL` to use the default global section
8922: . A - The matrix
8923: . point - The point in the `DM`
8924: . values - The array of values
8925: - mode - The insert mode, where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions
8927: Level: intermediate
8929: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosureGeneral()`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`
8930: @*/
8931: PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
8932: {
8933: PetscFunctionBegin;
8934: PetscCall(DMPlexMatSetClosure_Internal(dm, section, globalSection, PETSC_TRUE, A, point, values, mode));
8935: PetscFunctionReturn(PETSC_SUCCESS);
8936: }
8938: /*@C
8939: DMPlexMatSetClosureGeneral - Set an array of the values on the closure of 'point' using a different row and column section
8941: Not collective
8943: Input Parameters:
8944: + dmRow - The `DM` for the row fields
8945: . sectionRow - The section describing the layout, or `NULL` to use the default section in `dmRow`
8946: . useRowPerm - The flag to use the closure permutation of the `dmRow` if available
8947: . globalSectionRow - The section describing the layout, or `NULL` to use the default global section in `dmRow`
8948: . dmCol - The `DM` for the column fields
8949: . sectionCol - The section describing the layout, or `NULL` to use the default section in `dmCol`
8950: . useColPerm - The flag to use the closure permutation of the `dmCol` if available
8951: . globalSectionCol - The section describing the layout, or `NULL` to use the default global section in `dmCol`
8952: . A - The matrix
8953: . point - The point in the `DM`
8954: . values - The array of values
8955: - mode - The insert mode, where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions
8957: Level: intermediate
8959: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosure()`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`
8960: @*/
8961: PetscErrorCode DMPlexMatSetClosureGeneral(DM dmRow, PetscSection sectionRow, PetscSection globalSectionRow, PetscBool useRowPerm, DM dmCol, PetscSection sectionCol, PetscSection globalSectionCol, PetscBool useColPerm, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
8962: {
8963: DM_Plex *mesh = (DM_Plex *)dmRow->data;
8964: PetscInt *indicesRow, *indicesCol;
8965: PetscInt numIndicesRow = -1, numIndicesCol = -1;
8966: const PetscScalar *valuesV0 = values, *valuesV1, *valuesV2;
8968: PetscErrorCode ierr;
8970: PetscFunctionBegin;
8972: if (!sectionRow) PetscCall(DMGetLocalSection(dmRow, §ionRow));
8974: if (!globalSectionRow) PetscCall(DMGetGlobalSection(dmRow, &globalSectionRow));
8977: if (!sectionCol) PetscCall(DMGetLocalSection(dmCol, §ionCol));
8979: if (!globalSectionCol) PetscCall(DMGetGlobalSection(dmCol, &globalSectionCol));
8983: PetscCall(DMPlexGetClosureIndicesSize_Internal(dmRow, sectionRow, point, &numIndicesRow));
8984: PetscCall(DMPlexGetClosureIndicesSize_Internal(dmCol, sectionCol, point, &numIndicesCol));
8985: valuesV1 = valuesV0;
8986: PetscCall(DMPlexGetClosureIndices_Internal(dmRow, sectionRow, globalSectionRow, point, useRowPerm, &numIndicesRow, &numIndicesCol, &indicesRow, NULL, (PetscScalar **)&valuesV1, PETSC_FALSE, PETSC_TRUE));
8987: valuesV2 = valuesV1;
8988: PetscCall(DMPlexGetClosureIndices_Internal(dmCol, sectionCol, globalSectionCol, point, useColPerm, &numIndicesRow, &numIndicesCol, &indicesCol, NULL, (PetscScalar **)&valuesV2, PETSC_TRUE, PETSC_FALSE));
8990: if (mesh->printSetValues) PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, valuesV2));
8991: /* TODO: fix this code to not use error codes as handle-able exceptions! */
8992: ierr = MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, valuesV2, mode);
8993: if (ierr) {
8994: PetscMPIInt rank;
8996: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
8997: PetscCall((*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank));
8998: PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values));
8999: PetscCall(DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **)&valuesV2));
9000: PetscCall(DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&valuesV1));
9001: if (valuesV2 != valuesV1) PetscCall(DMRestoreWorkArray(dmCol, 0, MPIU_SCALAR, &valuesV2));
9002: if (valuesV1 != valuesV0) PetscCall(DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &valuesV1));
9003: }
9005: PetscCall(DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, useColPerm, &numIndicesCol, &indicesCol, NULL, (PetscScalar **)&valuesV2));
9006: PetscCall(DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, useRowPerm, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&valuesV1));
9007: if (valuesV2 != valuesV1) PetscCall(DMRestoreWorkArray(dmCol, 0, MPIU_SCALAR, &valuesV2));
9008: if (valuesV1 != valuesV0) PetscCall(DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &valuesV1));
9009: PetscFunctionReturn(PETSC_SUCCESS);
9010: }
9012: PetscErrorCode DMPlexMatSetClosureRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
9013: {
9014: DM_Plex *mesh = (DM_Plex *)dmf->data;
9015: PetscInt *fpoints = NULL, *ftotpoints = NULL;
9016: PetscInt *cpoints = NULL;
9017: PetscInt *findices, *cindices;
9018: const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
9019: PetscInt foffsets[32], coffsets[32];
9020: DMPolytopeType ct;
9021: PetscInt numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
9022: PetscErrorCode ierr;
9024: PetscFunctionBegin;
9027: if (!fsection) PetscCall(DMGetLocalSection(dmf, &fsection));
9029: if (!csection) PetscCall(DMGetLocalSection(dmc, &csection));
9031: if (!globalFSection) PetscCall(DMGetGlobalSection(dmf, &globalFSection));
9033: if (!globalCSection) PetscCall(DMGetGlobalSection(dmc, &globalCSection));
9036: PetscCall(PetscSectionGetNumFields(fsection, &numFields));
9037: PetscCheck(numFields <= 31, PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %" PetscInt_FMT " limited to 31", numFields);
9038: PetscCall(PetscArrayzero(foffsets, 32));
9039: PetscCall(PetscArrayzero(coffsets, 32));
9040: /* Column indices */
9041: PetscCall(DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints));
9042: maxFPoints = numCPoints;
9043: /* Compress out points not in the section */
9044: /* TODO: Squeeze out points with 0 dof as well */
9045: PetscCall(PetscSectionGetChart(csection, &pStart, &pEnd));
9046: for (p = 0, q = 0; p < numCPoints * 2; p += 2) {
9047: if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
9048: cpoints[q * 2] = cpoints[p];
9049: cpoints[q * 2 + 1] = cpoints[p + 1];
9050: ++q;
9051: }
9052: }
9053: numCPoints = q;
9054: for (p = 0, numCIndices = 0; p < numCPoints * 2; p += 2) {
9055: PetscInt fdof;
9057: PetscCall(PetscSectionGetDof(csection, cpoints[p], &dof));
9058: if (!dof) continue;
9059: for (f = 0; f < numFields; ++f) {
9060: PetscCall(PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof));
9061: coffsets[f + 1] += fdof;
9062: }
9063: numCIndices += dof;
9064: }
9065: for (f = 1; f < numFields; ++f) coffsets[f + 1] += coffsets[f];
9066: /* Row indices */
9067: PetscCall(DMPlexGetCellType(dmc, point, &ct));
9068: {
9069: DMPlexTransform tr;
9070: DMPolytopeType *rct;
9071: PetscInt *rsize, *rcone, *rornt, Nt;
9073: PetscCall(DMPlexTransformCreate(PETSC_COMM_SELF, &tr));
9074: PetscCall(DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR));
9075: PetscCall(DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt));
9076: numSubcells = rsize[Nt - 1];
9077: PetscCall(DMPlexTransformDestroy(&tr));
9078: }
9079: PetscCall(DMGetWorkArray(dmf, maxFPoints * 2 * numSubcells, MPIU_INT, &ftotpoints));
9080: for (r = 0, q = 0; r < numSubcells; ++r) {
9081: /* TODO Map from coarse to fine cells */
9082: PetscCall(DMPlexGetTransitiveClosure(dmf, point * numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints));
9083: /* Compress out points not in the section */
9084: PetscCall(PetscSectionGetChart(fsection, &pStart, &pEnd));
9085: for (p = 0; p < numFPoints * 2; p += 2) {
9086: if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
9087: PetscCall(PetscSectionGetDof(fsection, fpoints[p], &dof));
9088: if (!dof) continue;
9089: for (s = 0; s < q; ++s)
9090: if (fpoints[p] == ftotpoints[s * 2]) break;
9091: if (s < q) continue;
9092: ftotpoints[q * 2] = fpoints[p];
9093: ftotpoints[q * 2 + 1] = fpoints[p + 1];
9094: ++q;
9095: }
9096: }
9097: PetscCall(DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints));
9098: }
9099: numFPoints = q;
9100: for (p = 0, numFIndices = 0; p < numFPoints * 2; p += 2) {
9101: PetscInt fdof;
9103: PetscCall(PetscSectionGetDof(fsection, ftotpoints[p], &dof));
9104: if (!dof) continue;
9105: for (f = 0; f < numFields; ++f) {
9106: PetscCall(PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof));
9107: foffsets[f + 1] += fdof;
9108: }
9109: numFIndices += dof;
9110: }
9111: for (f = 1; f < numFields; ++f) foffsets[f + 1] += foffsets[f];
9113: PetscCheck(!numFields || foffsets[numFields] == numFIndices, PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, foffsets[numFields], numFIndices);
9114: PetscCheck(!numFields || coffsets[numFields] == numCIndices, PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, coffsets[numFields], numCIndices);
9115: PetscCall(DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices));
9116: PetscCall(DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices));
9117: if (numFields) {
9118: const PetscInt **permsF[32] = {NULL};
9119: const PetscInt **permsC[32] = {NULL};
9121: for (f = 0; f < numFields; f++) {
9122: PetscCall(PetscSectionGetFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL));
9123: PetscCall(PetscSectionGetFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL));
9124: }
9125: for (p = 0; p < numFPoints; p++) {
9126: PetscCall(PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff));
9127: PetscCall(DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices));
9128: }
9129: for (p = 0; p < numCPoints; p++) {
9130: PetscCall(PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff));
9131: PetscCall(DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices));
9132: }
9133: for (f = 0; f < numFields; f++) {
9134: PetscCall(PetscSectionRestoreFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL));
9135: PetscCall(PetscSectionRestoreFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL));
9136: }
9137: } else {
9138: const PetscInt **permsF = NULL;
9139: const PetscInt **permsC = NULL;
9141: PetscCall(PetscSectionGetPointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL));
9142: PetscCall(PetscSectionGetPointSyms(csection, numCPoints, cpoints, &permsC, NULL));
9143: for (p = 0, off = 0; p < numFPoints; p++) {
9144: const PetscInt *perm = permsF ? permsF[p] : NULL;
9146: PetscCall(PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff));
9147: PetscCall(DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices));
9148: }
9149: for (p = 0, off = 0; p < numCPoints; p++) {
9150: const PetscInt *perm = permsC ? permsC[p] : NULL;
9152: PetscCall(PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff));
9153: PetscCall(DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices));
9154: }
9155: PetscCall(PetscSectionRestorePointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL));
9156: PetscCall(PetscSectionRestorePointSyms(csection, numCPoints, cpoints, &permsC, NULL));
9157: }
9158: if (mesh->printSetValues) PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values));
9159: /* TODO: flips */
9160: /* TODO: fix this code to not use error codes as handle-able exceptions! */
9161: ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
9162: if (ierr) {
9163: PetscMPIInt rank;
9165: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
9166: PetscCall((*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank));
9167: PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values));
9168: PetscCall(DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices));
9169: PetscCall(DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices));
9170: }
9171: PetscCall(DMRestoreWorkArray(dmf, numCPoints * 2 * 4, MPIU_INT, &ftotpoints));
9172: PetscCall(DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints));
9173: PetscCall(DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices));
9174: PetscCall(DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices));
9175: PetscFunctionReturn(PETSC_SUCCESS);
9176: }
9178: PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
9179: {
9180: PetscInt *fpoints = NULL, *ftotpoints = NULL;
9181: PetscInt *cpoints = NULL;
9182: PetscInt foffsets[32] = {0}, coffsets[32] = {0};
9183: const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
9184: DMPolytopeType ct;
9185: PetscInt numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
9187: PetscFunctionBegin;
9190: if (!fsection) PetscCall(DMGetLocalSection(dmf, &fsection));
9192: if (!csection) PetscCall(DMGetLocalSection(dmc, &csection));
9194: if (!globalFSection) PetscCall(DMGetGlobalSection(dmf, &globalFSection));
9196: if (!globalCSection) PetscCall(DMGetGlobalSection(dmc, &globalCSection));
9198: PetscCall(PetscSectionGetNumFields(fsection, &numFields));
9199: PetscCheck(numFields <= 31, PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %" PetscInt_FMT " limited to 31", numFields);
9200: /* Column indices */
9201: PetscCall(DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints));
9202: maxFPoints = numCPoints;
9203: /* Compress out points not in the section */
9204: /* TODO: Squeeze out points with 0 dof as well */
9205: PetscCall(PetscSectionGetChart(csection, &pStart, &pEnd));
9206: for (p = 0, q = 0; p < numCPoints * 2; p += 2) {
9207: if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
9208: cpoints[q * 2] = cpoints[p];
9209: cpoints[q * 2 + 1] = cpoints[p + 1];
9210: ++q;
9211: }
9212: }
9213: numCPoints = q;
9214: for (p = 0, numCIndices = 0; p < numCPoints * 2; p += 2) {
9215: PetscInt fdof;
9217: PetscCall(PetscSectionGetDof(csection, cpoints[p], &dof));
9218: if (!dof) continue;
9219: for (f = 0; f < numFields; ++f) {
9220: PetscCall(PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof));
9221: coffsets[f + 1] += fdof;
9222: }
9223: numCIndices += dof;
9224: }
9225: for (f = 1; f < numFields; ++f) coffsets[f + 1] += coffsets[f];
9226: /* Row indices */
9227: PetscCall(DMPlexGetCellType(dmc, point, &ct));
9228: {
9229: DMPlexTransform tr;
9230: DMPolytopeType *rct;
9231: PetscInt *rsize, *rcone, *rornt, Nt;
9233: PetscCall(DMPlexTransformCreate(PETSC_COMM_SELF, &tr));
9234: PetscCall(DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR));
9235: PetscCall(DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt));
9236: numSubcells = rsize[Nt - 1];
9237: PetscCall(DMPlexTransformDestroy(&tr));
9238: }
9239: PetscCall(DMGetWorkArray(dmf, maxFPoints * 2 * numSubcells, MPIU_INT, &ftotpoints));
9240: for (r = 0, q = 0; r < numSubcells; ++r) {
9241: /* TODO Map from coarse to fine cells */
9242: PetscCall(DMPlexGetTransitiveClosure(dmf, point * numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints));
9243: /* Compress out points not in the section */
9244: PetscCall(PetscSectionGetChart(fsection, &pStart, &pEnd));
9245: for (p = 0; p < numFPoints * 2; p += 2) {
9246: if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
9247: PetscCall(PetscSectionGetDof(fsection, fpoints[p], &dof));
9248: if (!dof) continue;
9249: for (s = 0; s < q; ++s)
9250: if (fpoints[p] == ftotpoints[s * 2]) break;
9251: if (s < q) continue;
9252: ftotpoints[q * 2] = fpoints[p];
9253: ftotpoints[q * 2 + 1] = fpoints[p + 1];
9254: ++q;
9255: }
9256: }
9257: PetscCall(DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints));
9258: }
9259: numFPoints = q;
9260: for (p = 0, numFIndices = 0; p < numFPoints * 2; p += 2) {
9261: PetscInt fdof;
9263: PetscCall(PetscSectionGetDof(fsection, ftotpoints[p], &dof));
9264: if (!dof) continue;
9265: for (f = 0; f < numFields; ++f) {
9266: PetscCall(PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof));
9267: foffsets[f + 1] += fdof;
9268: }
9269: numFIndices += dof;
9270: }
9271: for (f = 1; f < numFields; ++f) foffsets[f + 1] += foffsets[f];
9273: PetscCheck(!numFields || foffsets[numFields] == numFIndices, PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, foffsets[numFields], numFIndices);
9274: PetscCheck(!numFields || coffsets[numFields] == numCIndices, PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, coffsets[numFields], numCIndices);
9275: if (numFields) {
9276: const PetscInt **permsF[32] = {NULL};
9277: const PetscInt **permsC[32] = {NULL};
9279: for (f = 0; f < numFields; f++) {
9280: PetscCall(PetscSectionGetFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL));
9281: PetscCall(PetscSectionGetFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL));
9282: }
9283: for (p = 0; p < numFPoints; p++) {
9284: PetscCall(PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff));
9285: PetscCall(DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices));
9286: }
9287: for (p = 0; p < numCPoints; p++) {
9288: PetscCall(PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff));
9289: PetscCall(DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices));
9290: }
9291: for (f = 0; f < numFields; f++) {
9292: PetscCall(PetscSectionRestoreFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL));
9293: PetscCall(PetscSectionRestoreFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL));
9294: }
9295: } else {
9296: const PetscInt **permsF = NULL;
9297: const PetscInt **permsC = NULL;
9299: PetscCall(PetscSectionGetPointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL));
9300: PetscCall(PetscSectionGetPointSyms(csection, numCPoints, cpoints, &permsC, NULL));
9301: for (p = 0, off = 0; p < numFPoints; p++) {
9302: const PetscInt *perm = permsF ? permsF[p] : NULL;
9304: PetscCall(PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff));
9305: PetscCall(DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices));
9306: }
9307: for (p = 0, off = 0; p < numCPoints; p++) {
9308: const PetscInt *perm = permsC ? permsC[p] : NULL;
9310: PetscCall(PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff));
9311: PetscCall(DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices));
9312: }
9313: PetscCall(PetscSectionRestorePointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL));
9314: PetscCall(PetscSectionRestorePointSyms(csection, numCPoints, cpoints, &permsC, NULL));
9315: }
9316: PetscCall(DMRestoreWorkArray(dmf, numCPoints * 2 * 4, MPIU_INT, &ftotpoints));
9317: PetscCall(DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints));
9318: PetscFunctionReturn(PETSC_SUCCESS);
9319: }
9321: /*@
9322: DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
9324: Input Parameter:
9325: . dm - The `DMPLEX` object
9327: Output Parameter:
9328: . cellHeight - The height of a cell
9330: Level: developer
9332: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetVTKCellHeight()`
9333: @*/
9334: PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
9335: {
9336: DM_Plex *mesh = (DM_Plex *)dm->data;
9338: PetscFunctionBegin;
9340: PetscAssertPointer(cellHeight, 2);
9341: *cellHeight = mesh->vtkCellHeight;
9342: PetscFunctionReturn(PETSC_SUCCESS);
9343: }
9345: /*@
9346: DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
9348: Input Parameters:
9349: + dm - The `DMPLEX` object
9350: - cellHeight - The height of a cell
9352: Level: developer
9354: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetVTKCellHeight()`
9355: @*/
9356: PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
9357: {
9358: DM_Plex *mesh = (DM_Plex *)dm->data;
9360: PetscFunctionBegin;
9362: mesh->vtkCellHeight = cellHeight;
9363: PetscFunctionReturn(PETSC_SUCCESS);
9364: }
9366: /*@
9367: DMPlexGetCellTypeStratum - Get the range of cells of a given celltype
9369: Input Parameters:
9370: + dm - The `DMPLEX` object
9371: - ct - The `DMPolytopeType` of the cell
9373: Output Parameters:
9374: + start - The first cell of this type, or `NULL`
9375: - end - The upper bound on this celltype, or `NULL`
9377: Level: advanced
9379: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructGhostCells()`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
9380: @*/
9381: PetscErrorCode DMPlexGetCellTypeStratum(DM dm, DMPolytopeType ct, PeOp PetscInt *start, PeOp PetscInt *end)
9382: {
9383: DM_Plex *mesh = (DM_Plex *)dm->data;
9384: DMLabel label;
9385: PetscInt pStart, pEnd;
9387: PetscFunctionBegin;
9389: if (start) {
9390: PetscAssertPointer(start, 3);
9391: *start = 0;
9392: }
9393: if (end) {
9394: PetscAssertPointer(end, 4);
9395: *end = 0;
9396: }
9397: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
9398: if (pStart == pEnd) PetscFunctionReturn(PETSC_SUCCESS);
9399: if (mesh->tr) {
9400: PetscCall(DMPlexTransformGetCellTypeStratum(mesh->tr, ct, start, end));
9401: } else {
9402: PetscCall(DMPlexGetCellTypeLabel(dm, &label));
9403: PetscCheck(label, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "No label named celltype was found");
9404: PetscCall(DMLabelGetStratumBounds(label, ct, start, end));
9405: }
9406: PetscFunctionReturn(PETSC_SUCCESS);
9407: }
9409: /*@
9410: DMPlexGetDepthStratumGlobalSize - Get the global size for a given depth stratum
9412: Input Parameters:
9413: + dm - The `DMPLEX` object
9414: - depth - The depth for the given point stratum
9416: Output Parameter:
9417: . gsize - The global number of points in the stratum
9419: Level: advanced
9421: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`, `DMPlexGetVertexNumbering()`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
9422: @*/
9423: PetscErrorCode DMPlexGetDepthStratumGlobalSize(DM dm, PetscInt depth, PetscInt *gsize)
9424: {
9425: PetscSF sf;
9426: const PetscInt *leaves;
9427: PetscInt Nl, loc, start, end, lsize = 0;
9429: PetscFunctionBegin;
9430: PetscCall(DMGetPointSF(dm, &sf));
9431: PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
9432: PetscCall(DMPlexGetDepthStratum(dm, depth, &start, &end));
9433: for (PetscInt p = start; p < end; ++p) {
9434: PetscCall(PetscFindInt(p, Nl, leaves, &loc));
9435: if (loc < 0) ++lsize;
9436: }
9437: PetscCallMPI(MPIU_Allreduce(&lsize, gsize, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)dm)));
9438: PetscFunctionReturn(PETSC_SUCCESS);
9439: }
9441: PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
9442: {
9443: PetscSection section, globalSection;
9444: PetscInt *numbers, p;
9446: PetscFunctionBegin;
9447: if (PetscDefined(USE_DEBUG)) PetscCall(DMPlexCheckPointSF(dm, sf, PETSC_TRUE));
9448: PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), §ion));
9449: PetscCall(PetscSectionSetChart(section, pStart, pEnd));
9450: for (p = pStart; p < pEnd; ++p) PetscCall(PetscSectionSetDof(section, p, 1));
9451: PetscCall(PetscSectionSetUp(section));
9452: PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &globalSection));
9453: PetscCall(PetscMalloc1(pEnd - pStart, &numbers));
9454: for (p = pStart; p < pEnd; ++p) {
9455: PetscCall(PetscSectionGetOffset(globalSection, p, &numbers[p - pStart]));
9456: if (numbers[p - pStart] < 0) numbers[p - pStart] -= shift;
9457: else numbers[p - pStart] += shift;
9458: }
9459: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering));
9460: if (globalSize) {
9461: PetscLayout layout;
9462: PetscCall(PetscSectionGetPointLayout(PetscObjectComm((PetscObject)dm), globalSection, &layout));
9463: PetscCall(PetscLayoutGetSize(layout, globalSize));
9464: PetscCall(PetscLayoutDestroy(&layout));
9465: }
9466: PetscCall(PetscSectionDestroy(§ion));
9467: PetscCall(PetscSectionDestroy(&globalSection));
9468: PetscFunctionReturn(PETSC_SUCCESS);
9469: }
9471: /*@
9472: DMPlexCreateCellNumbering - Get a global cell numbering for all cells on this process
9474: Input Parameters:
9475: + dm - The `DMPLEX` object
9476: - includeAll - Whether to include all cells, or just the simplex and box cells
9478: Output Parameter:
9479: . globalCellNumbers - Global cell numbers for all cells on this process
9481: Level: developer
9483: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`, `DMPlexGetVertexNumbering()`
9484: @*/
9485: PetscErrorCode DMPlexCreateCellNumbering(DM dm, PetscBool includeAll, IS *globalCellNumbers)
9486: {
9487: PetscInt cellHeight, cStart, cEnd;
9489: PetscFunctionBegin;
9490: PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
9491: if (includeAll) PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
9492: else PetscCall(DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd));
9493: PetscCall(DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers));
9494: PetscFunctionReturn(PETSC_SUCCESS);
9495: }
9497: /*@
9498: DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
9500: Input Parameter:
9501: . dm - The `DMPLEX` object
9503: Output Parameter:
9504: . globalCellNumbers - Global cell numbers for all cells on this process
9506: Level: developer
9508: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateCellNumbering()`, `DMPlexGetVertexNumbering()`
9509: @*/
9510: PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
9511: {
9512: DM_Plex *mesh = (DM_Plex *)dm->data;
9514: PetscFunctionBegin;
9516: if (!mesh->globalCellNumbers) PetscCall(DMPlexCreateCellNumbering(dm, PETSC_FALSE, &mesh->globalCellNumbers));
9517: *globalCellNumbers = mesh->globalCellNumbers;
9518: PetscFunctionReturn(PETSC_SUCCESS);
9519: }
9521: PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
9522: {
9523: PetscInt vStart, vEnd;
9525: PetscFunctionBegin;
9527: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
9528: PetscCall(DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers));
9529: PetscFunctionReturn(PETSC_SUCCESS);
9530: }
9532: /*@
9533: DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
9535: Input Parameter:
9536: . dm - The `DMPLEX` object
9538: Output Parameter:
9539: . globalVertexNumbers - Global vertex numbers for all vertices on this process
9541: Level: developer
9543: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`
9544: @*/
9545: PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
9546: {
9547: DM_Plex *mesh = (DM_Plex *)dm->data;
9549: PetscFunctionBegin;
9551: if (!mesh->globalVertexNumbers) PetscCall(DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers));
9552: *globalVertexNumbers = mesh->globalVertexNumbers;
9553: PetscFunctionReturn(PETSC_SUCCESS);
9554: }
9556: /*@
9557: DMPlexCreatePointNumbering - Create a global numbering for all points.
9559: Collective
9561: Input Parameter:
9562: . dm - The `DMPLEX` object
9564: Output Parameter:
9565: . globalPointNumbers - Global numbers for all points on this process
9567: Level: developer
9569: Notes:
9570: The point numbering `IS` is parallel, with local portion indexed by local points (see `DMGetLocalSection()`). The global
9571: points are taken as stratified, with each MPI rank owning a contiguous subset of each stratum. In the IS, owned points
9572: will have their non-negative value while points owned by different ranks will be involuted -(idx+1). As an example,
9573: consider a parallel mesh in which the first two elements and first two vertices are owned by rank 0.
9575: The partitioned mesh is
9576: ```
9577: (2)--0--(3)--1--(4) (1)--0--(2)
9578: ```
9579: and its global numbering is
9580: ```
9581: (3)--0--(4)--1--(5)--2--(6)
9582: ```
9583: Then the global numbering is provided as
9584: ```
9585: [0] Number of indices in set 5
9586: [0] 0 0
9587: [0] 1 1
9588: [0] 2 3
9589: [0] 3 4
9590: [0] 4 -6
9591: [1] Number of indices in set 3
9592: [1] 0 2
9593: [1] 1 5
9594: [1] 2 6
9595: ```
9597: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`
9598: @*/
9599: PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
9600: {
9601: IS nums[4];
9602: PetscInt depths[4], gdepths[4], starts[4];
9603: PetscInt depth, d, shift = 0;
9604: PetscBool empty = PETSC_FALSE;
9606: PetscFunctionBegin;
9608: PetscCall(DMPlexGetDepth(dm, &depth));
9609: // For unstratified meshes use dim instead of depth
9610: if (depth < 0) PetscCall(DMGetDimension(dm, &depth));
9611: // If any stratum is empty, we must mark all empty
9612: for (d = 0; d <= depth; ++d) {
9613: PetscInt end;
9615: depths[d] = depth - d;
9616: PetscCall(DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end));
9617: if (!(starts[d] - end)) empty = PETSC_TRUE;
9618: }
9619: if (empty)
9620: for (d = 0; d <= depth; ++d) {
9621: depths[d] = -1;
9622: starts[d] = -1;
9623: }
9624: else PetscCall(PetscSortIntWithArray(depth + 1, starts, depths));
9625: PetscCallMPI(MPIU_Allreduce(depths, gdepths, depth + 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
9626: for (d = 0; d <= depth; ++d) PetscCheck(starts[d] < 0 || depths[d] == gdepths[d], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected depth %" PetscInt_FMT ", found %" PetscInt_FMT, depths[d], gdepths[d]);
9627: // Note here that 'shift' is collective, so that the numbering is stratified by depth
9628: for (d = 0; d <= depth; ++d) {
9629: PetscInt pStart, pEnd, gsize;
9631: PetscCall(DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd));
9632: PetscCall(DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]));
9633: shift += gsize;
9634: }
9635: PetscCall(ISConcatenate(PETSC_COMM_SELF, depth + 1, nums, globalPointNumbers));
9636: for (d = 0; d <= depth; ++d) PetscCall(ISDestroy(&nums[d]));
9637: PetscFunctionReturn(PETSC_SUCCESS);
9638: }
9640: /*@
9641: DMPlexCreateEdgeNumbering - Create a global numbering for edges.
9643: Collective
9645: Input Parameter:
9646: . dm - The `DMPLEX` object
9648: Output Parameter:
9649: . globalEdgeNumbers - Global numbers for all edges on this process
9651: Level: developer
9653: Notes:
9654: The point numbering `IS` is parallel, with local portion indexed by local points (see `DMGetLocalSection()`). In the IS, owned edges will have their non-negative value while edges owned by different ranks will be involuted -(idx+1).
9656: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`, `DMPlexGetVertexNumbering()`, `DMPlexCreatePointNumbering()`
9657: @*/
9658: PetscErrorCode DMPlexCreateEdgeNumbering(DM dm, IS *globalEdgeNumbers)
9659: {
9660: PetscSF sf;
9661: PetscInt eStart, eEnd;
9663: PetscFunctionBegin;
9665: PetscCall(DMGetPointSF(dm, &sf));
9666: PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
9667: PetscCall(DMPlexCreateNumbering_Plex(dm, eStart, eEnd, 0, NULL, sf, globalEdgeNumbers));
9668: PetscFunctionReturn(PETSC_SUCCESS);
9669: }
9671: /*@
9672: DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
9674: Input Parameter:
9675: . dm - The `DMPLEX` object
9677: Output Parameter:
9678: . ranks - The rank field
9680: Options Database Key:
9681: . -dm_partition_view - Adds the rank field into the `DM` output from `-dm_view` using the same viewer
9683: Level: intermediate
9685: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`
9686: @*/
9687: PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
9688: {
9689: DM rdm;
9690: PetscFE fe;
9691: PetscScalar *r;
9692: PetscMPIInt rank;
9693: DMPolytopeType ct;
9694: PetscInt dim, cStart, cEnd, c;
9695: PetscBool simplex;
9697: PetscFunctionBeginUser;
9699: PetscAssertPointer(ranks, 2);
9700: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
9701: PetscCall(DMClone(dm, &rdm));
9702: PetscCall(DMGetDimension(rdm, &dim));
9703: PetscCall(DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd));
9704: PetscCall(DMPlexGetCellType(dm, cStart, &ct));
9705: simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
9706: PetscCall(PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe));
9707: PetscCall(PetscObjectSetName((PetscObject)fe, "rank"));
9708: PetscCall(DMSetField(rdm, 0, NULL, (PetscObject)fe));
9709: PetscCall(PetscFEDestroy(&fe));
9710: PetscCall(DMCreateDS(rdm));
9711: PetscCall(DMCreateGlobalVector(rdm, ranks));
9712: PetscCall(PetscObjectSetName((PetscObject)*ranks, "partition"));
9713: PetscCall(VecGetArray(*ranks, &r));
9714: for (c = cStart; c < cEnd; ++c) {
9715: PetscScalar *lr;
9717: PetscCall(DMPlexPointGlobalRef(rdm, c, r, &lr));
9718: if (lr) *lr = rank;
9719: }
9720: PetscCall(VecRestoreArray(*ranks, &r));
9721: PetscCall(DMDestroy(&rdm));
9722: PetscFunctionReturn(PETSC_SUCCESS);
9723: }
9725: /*@
9726: DMPlexCreateLabelField - Create a field whose value is the label value for that point
9728: Input Parameters:
9729: + dm - The `DMPLEX`
9730: - label - The `DMLabel`
9732: Output Parameter:
9733: . val - The label value field
9735: Options Database Key:
9736: . -dm_label_view - Adds the label value field into the `DM` output from `-dm_view` using the same viewer
9738: Level: intermediate
9740: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`
9741: @*/
9742: PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
9743: {
9744: DM rdm, plex;
9745: Vec lval;
9746: PetscSection section;
9747: PetscFE fe;
9748: PetscScalar *v;
9749: PetscInt dim, pStart, pEnd, p, cStart;
9750: DMPolytopeType ct;
9751: char name[PETSC_MAX_PATH_LEN];
9752: const char *lname, *prefix;
9754: PetscFunctionBeginUser;
9756: PetscAssertPointer(label, 2);
9757: PetscAssertPointer(val, 3);
9758: PetscCall(DMClone(dm, &rdm));
9759: PetscCall(DMConvert(rdm, DMPLEX, &plex));
9760: PetscCall(DMPlexGetHeightStratum(plex, 0, &cStart, NULL));
9761: PetscCall(DMPlexGetCellType(plex, cStart, &ct));
9762: PetscCall(DMDestroy(&plex));
9763: PetscCall(DMGetDimension(rdm, &dim));
9764: PetscCall(DMGetOptionsPrefix(dm, &prefix));
9765: PetscCall(PetscObjectGetName((PetscObject)label, &lname));
9766: PetscCall(PetscSNPrintf(name, sizeof(name), "%s%s_", prefix ? prefix : "", lname));
9767: PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, 1, ct, name, -1, &fe));
9768: PetscCall(PetscObjectSetName((PetscObject)fe, ""));
9769: PetscCall(DMSetField(rdm, 0, NULL, (PetscObject)fe));
9770: PetscCall(PetscFEDestroy(&fe));
9771: PetscCall(DMCreateDS(rdm));
9772: PetscCall(DMCreateGlobalVector(rdm, val));
9773: PetscCall(DMCreateLocalVector(rdm, &lval));
9774: PetscCall(PetscObjectSetName((PetscObject)*val, lname));
9775: PetscCall(DMGetLocalSection(rdm, §ion));
9776: PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
9777: PetscCall(VecGetArray(lval, &v));
9778: for (p = pStart; p < pEnd; ++p) {
9779: PetscInt cval, dof, off;
9781: PetscCall(PetscSectionGetDof(section, p, &dof));
9782: if (!dof) continue;
9783: PetscCall(DMLabelGetValue(label, p, &cval));
9784: PetscCall(PetscSectionGetOffset(section, p, &off));
9785: for (PetscInt d = 0; d < dof; d++) v[off + d] = cval;
9786: }
9787: PetscCall(VecRestoreArray(lval, &v));
9788: PetscCall(DMLocalToGlobal(rdm, lval, INSERT_VALUES, *val));
9789: PetscCall(VecDestroy(&lval));
9790: PetscCall(DMDestroy(&rdm));
9791: PetscFunctionReturn(PETSC_SUCCESS);
9792: }
9794: /*@
9795: DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
9797: Input Parameter:
9798: . dm - The `DMPLEX` object
9800: Level: developer
9802: Notes:
9803: This is a useful diagnostic when creating meshes programmatically.
9805: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
9807: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
9808: @*/
9809: PetscErrorCode DMPlexCheckSymmetry(DM dm)
9810: {
9811: PetscSection coneSection, supportSection;
9812: const PetscInt *cone, *support;
9813: PetscInt coneSize, c, supportSize, s;
9814: PetscInt pStart, pEnd, p, pp, csize, ssize;
9815: PetscBool storagecheck = PETSC_TRUE;
9817: PetscFunctionBegin;
9819: PetscCall(DMViewFromOptions(dm, NULL, "-sym_dm_view"));
9820: PetscCall(DMPlexGetConeSection(dm, &coneSection));
9821: PetscCall(DMPlexGetSupportSection(dm, &supportSection));
9822: /* Check that point p is found in the support of its cone points, and vice versa */
9823: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
9824: for (p = pStart; p < pEnd; ++p) {
9825: PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
9826: PetscCall(DMPlexGetCone(dm, p, &cone));
9827: for (c = 0; c < coneSize; ++c) {
9828: PetscBool dup = PETSC_FALSE;
9829: PetscInt d;
9830: for (d = c - 1; d >= 0; --d) {
9831: if (cone[c] == cone[d]) {
9832: dup = PETSC_TRUE;
9833: break;
9834: }
9835: }
9836: PetscCall(DMPlexGetSupportSize(dm, cone[c], &supportSize));
9837: PetscCall(DMPlexGetSupport(dm, cone[c], &support));
9838: for (s = 0; s < supportSize; ++s) {
9839: if (support[s] == p) break;
9840: }
9841: if ((s >= supportSize) || (dup && (support[s + 1] != p))) {
9842: PetscCall(PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " cone: ", p));
9843: for (s = 0; s < coneSize; ++s) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", cone[s]));
9844: PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
9845: PetscCall(PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " support: ", cone[c]));
9846: for (s = 0; s < supportSize; ++s) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", support[s]));
9847: PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
9848: PetscCheck(!dup, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not repeatedly found in support of repeated cone point %" PetscInt_FMT, p, cone[c]);
9849: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not found in support of cone point %" PetscInt_FMT, p, cone[c]);
9850: }
9851: }
9852: PetscCall(DMPlexGetTreeParent(dm, p, &pp, NULL));
9853: if (p != pp) {
9854: storagecheck = PETSC_FALSE;
9855: continue;
9856: }
9857: PetscCall(DMPlexGetSupportSize(dm, p, &supportSize));
9858: PetscCall(DMPlexGetSupport(dm, p, &support));
9859: for (s = 0; s < supportSize; ++s) {
9860: PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
9861: PetscCall(DMPlexGetCone(dm, support[s], &cone));
9862: for (c = 0; c < coneSize; ++c) {
9863: PetscCall(DMPlexGetTreeParent(dm, cone[c], &pp, NULL));
9864: if (cone[c] != pp) {
9865: c = 0;
9866: break;
9867: }
9868: if (cone[c] == p) break;
9869: }
9870: if (c >= coneSize) {
9871: PetscCall(PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " support: ", p));
9872: for (c = 0; c < supportSize; ++c) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", support[c]));
9873: PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
9874: PetscCall(PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " cone: ", support[s]));
9875: for (c = 0; c < coneSize; ++c) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", cone[c]));
9876: PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
9877: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not found in cone of support point %" PetscInt_FMT, p, support[s]);
9878: }
9879: }
9880: }
9881: if (storagecheck) {
9882: PetscCall(PetscSectionGetStorageSize(coneSection, &csize));
9883: PetscCall(PetscSectionGetStorageSize(supportSection, &ssize));
9884: PetscCheck(csize == ssize, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %" PetscInt_FMT " != Total support size %" PetscInt_FMT, csize, ssize);
9885: }
9886: PetscFunctionReturn(PETSC_SUCCESS);
9887: }
9889: /*
9890: 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.
9891: */
9892: static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
9893: {
9894: DMPolytopeType cct;
9895: PetscInt ptpoints[4];
9896: const PetscInt *cone, *ccone, *ptcone;
9897: PetscInt coneSize, cp, cconeSize, ccp, npt = 0, pt;
9899: PetscFunctionBegin;
9900: *unsplit = 0;
9901: switch (ct) {
9902: case DM_POLYTOPE_POINT_PRISM_TENSOR:
9903: ptpoints[npt++] = c;
9904: break;
9905: case DM_POLYTOPE_SEG_PRISM_TENSOR:
9906: PetscCall(DMPlexGetCone(dm, c, &cone));
9907: PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
9908: for (cp = 0; cp < coneSize; ++cp) {
9909: PetscCall(DMPlexGetCellType(dm, cone[cp], &cct));
9910: if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
9911: }
9912: break;
9913: case DM_POLYTOPE_TRI_PRISM_TENSOR:
9914: case DM_POLYTOPE_QUAD_PRISM_TENSOR:
9915: PetscCall(DMPlexGetCone(dm, c, &cone));
9916: PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
9917: for (cp = 0; cp < coneSize; ++cp) {
9918: PetscCall(DMPlexGetCone(dm, cone[cp], &ccone));
9919: PetscCall(DMPlexGetConeSize(dm, cone[cp], &cconeSize));
9920: for (ccp = 0; ccp < cconeSize; ++ccp) {
9921: PetscCall(DMPlexGetCellType(dm, ccone[ccp], &cct));
9922: if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
9923: PetscInt p;
9924: for (p = 0; p < npt; ++p)
9925: if (ptpoints[p] == ccone[ccp]) break;
9926: if (p == npt) ptpoints[npt++] = ccone[ccp];
9927: }
9928: }
9929: }
9930: break;
9931: default:
9932: break;
9933: }
9934: for (pt = 0; pt < npt; ++pt) {
9935: PetscCall(DMPlexGetCone(dm, ptpoints[pt], &ptcone));
9936: if (ptcone[0] == ptcone[1]) ++(*unsplit);
9937: }
9938: PetscFunctionReturn(PETSC_SUCCESS);
9939: }
9941: /*@
9942: DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
9944: Input Parameters:
9945: + dm - The `DMPLEX` object
9946: - cellHeight - Normally 0
9948: Level: developer
9950: Notes:
9951: This is a useful diagnostic when creating meshes programmatically.
9952: Currently applicable only to homogeneous simplex or tensor meshes.
9954: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
9956: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
9957: @*/
9958: PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
9959: {
9960: DMPlexInterpolatedFlag interp;
9961: DMPolytopeType ct;
9962: PetscInt vStart, vEnd, cStart, cEnd, c;
9964: PetscFunctionBegin;
9966: PetscCall(DMPlexIsInterpolated(dm, &interp));
9967: PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
9968: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
9969: for (c = cStart; c < cEnd; ++c) {
9970: PetscInt *closure = NULL;
9971: PetscInt coneSize, closureSize, cl, Nv = 0;
9973: PetscCall(DMPlexGetCellType(dm, c, &ct));
9974: if (ct == DM_POLYTOPE_UNKNOWN) continue;
9975: if (interp == DMPLEX_INTERPOLATED_FULL) {
9976: PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
9977: PetscCheck(coneSize == DMPolytopeTypeGetConeSize(ct), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " of type %s has cone size %" PetscInt_FMT " != %" PetscInt_FMT, c, DMPolytopeTypes[ct], coneSize, DMPolytopeTypeGetConeSize(ct));
9978: }
9979: PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
9980: for (cl = 0; cl < closureSize * 2; cl += 2) {
9981: const PetscInt p = closure[cl];
9982: if ((p >= vStart) && (p < vEnd)) ++Nv;
9983: }
9984: PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
9985: /* Special Case: Tensor faces with identified vertices */
9986: if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
9987: PetscInt unsplit;
9989: PetscCall(DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit));
9990: if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
9991: }
9992: PetscCheck(Nv == DMPolytopeTypeGetNumVertices(ct), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " of type %s has %" PetscInt_FMT " vertices != %" PetscInt_FMT, c, DMPolytopeTypes[ct], Nv, DMPolytopeTypeGetNumVertices(ct));
9993: }
9994: PetscFunctionReturn(PETSC_SUCCESS);
9995: }
9997: /*@
9998: DMPlexCheckFaces - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type
10000: Collective
10002: Input Parameters:
10003: + dm - The `DMPLEX` object
10004: - cellHeight - Normally 0
10006: Level: developer
10008: Notes:
10009: This is a useful diagnostic when creating meshes programmatically.
10010: This routine is only relevant for meshes that are fully interpolated across all ranks.
10011: It will error out if a partially interpolated mesh is given on some rank.
10012: It will do nothing for locally uninterpolated mesh (as there is nothing to check).
10014: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
10016: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexGetVTKCellHeight()`, `DMSetFromOptions()`
10017: @*/
10018: PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
10019: {
10020: PetscInt dim, depth, vStart, vEnd, cStart, cEnd, c, h;
10021: DMPlexInterpolatedFlag interpEnum;
10023: PetscFunctionBegin;
10025: PetscCall(DMPlexIsInterpolatedCollective(dm, &interpEnum));
10026: if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(PETSC_SUCCESS);
10027: if (interpEnum != DMPLEX_INTERPOLATED_FULL) {
10028: PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "DMPlexCheckFaces() warning: Mesh is only partially interpolated, this is currently not supported"));
10029: PetscFunctionReturn(PETSC_SUCCESS);
10030: }
10032: PetscCall(DMGetDimension(dm, &dim));
10033: PetscCall(DMPlexGetDepth(dm, &depth));
10034: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
10035: for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
10036: PetscCall(DMPlexGetHeightStratum(dm, h, &cStart, &cEnd));
10037: for (c = cStart; c < cEnd; ++c) {
10038: const PetscInt *cone, *ornt, *faceSizes, *faces;
10039: const DMPolytopeType *faceTypes;
10040: DMPolytopeType ct;
10041: PetscInt numFaces, coneSize, f;
10042: PetscInt *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;
10044: PetscCall(DMPlexGetCellType(dm, c, &ct));
10045: PetscCall(DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit));
10046: if (unsplit) continue;
10047: PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
10048: PetscCall(DMPlexGetCone(dm, c, &cone));
10049: PetscCall(DMPlexGetConeOrientation(dm, c, &ornt));
10050: PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
10051: for (cl = 0; cl < closureSize * 2; cl += 2) {
10052: const PetscInt p = closure[cl];
10053: if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
10054: }
10055: PetscCall(DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces));
10056: PetscCheck(coneSize == numFaces, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " of type %s has %" PetscInt_FMT " faces but should have %" PetscInt_FMT, c, DMPolytopeTypes[ct], coneSize, numFaces);
10057: for (f = 0; f < numFaces; ++f) {
10058: DMPolytopeType fct;
10059: PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
10061: PetscCall(DMPlexGetCellType(dm, cone[f], &fct));
10062: PetscCall(DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure));
10063: for (cl = 0; cl < fclosureSize * 2; cl += 2) {
10064: const PetscInt p = fclosure[cl];
10065: if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
10066: }
10067: PetscCheck(fnumCorners == faceSizes[f], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " of type %s (cone idx %" PetscInt_FMT ") of cell %" PetscInt_FMT " of type %s has %" PetscInt_FMT " vertices but should have %" PetscInt_FMT, cone[f], DMPolytopeTypes[fct], f, c, DMPolytopeTypes[ct], fnumCorners, faceSizes[f]);
10068: for (v = 0; v < fnumCorners; ++v) {
10069: if (fclosure[v] != faces[fOff + v]) {
10070: PetscInt v1;
10072: PetscCall(PetscPrintf(PETSC_COMM_SELF, "face closure:"));
10073: for (v1 = 0; v1 < fnumCorners; ++v1) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, fclosure[v1]));
10074: PetscCall(PetscPrintf(PETSC_COMM_SELF, "\ncell face:"));
10075: for (v1 = 0; v1 < fnumCorners; ++v1) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, faces[fOff + v1]));
10076: PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
10077: 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]);
10078: }
10079: }
10080: PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure));
10081: fOff += faceSizes[f];
10082: }
10083: PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces));
10084: PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
10085: }
10086: }
10087: PetscFunctionReturn(PETSC_SUCCESS);
10088: }
10090: /*@
10091: DMPlexCheckGeometry - Check the geometry of mesh cells
10093: Input Parameter:
10094: . dm - The `DMPLEX` object
10096: Level: developer
10098: Notes:
10099: This is a useful diagnostic when creating meshes programmatically.
10101: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
10103: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
10104: @*/
10105: PetscErrorCode DMPlexCheckGeometry(DM dm)
10106: {
10107: Vec coordinates;
10108: PetscReal detJ, J[9], refVol = 1.0;
10109: PetscReal vol;
10110: PetscInt dim, depth, dE, d, cStart, cEnd, c;
10112: PetscFunctionBegin;
10113: PetscCall(DMGetDimension(dm, &dim));
10114: PetscCall(DMGetCoordinateDim(dm, &dE));
10115: if (dim != dE) PetscFunctionReturn(PETSC_SUCCESS);
10116: PetscCall(DMPlexGetDepth(dm, &depth));
10117: for (d = 0; d < dim; ++d) refVol *= 2.0;
10118: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
10119: /* Make sure local coordinates are created, because that step is collective */
10120: PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
10121: if (!coordinates) PetscFunctionReturn(PETSC_SUCCESS);
10122: for (c = cStart; c < cEnd; ++c) {
10123: DMPolytopeType ct;
10124: PetscInt unsplit;
10125: PetscBool ignoreZeroVol = PETSC_FALSE;
10127: PetscCall(DMPlexGetCellType(dm, c, &ct));
10128: switch (ct) {
10129: case DM_POLYTOPE_SEG_PRISM_TENSOR:
10130: case DM_POLYTOPE_TRI_PRISM_TENSOR:
10131: case DM_POLYTOPE_QUAD_PRISM_TENSOR:
10132: ignoreZeroVol = PETSC_TRUE;
10133: break;
10134: default:
10135: break;
10136: }
10137: switch (ct) {
10138: case DM_POLYTOPE_TRI_PRISM:
10139: case DM_POLYTOPE_TRI_PRISM_TENSOR:
10140: case DM_POLYTOPE_QUAD_PRISM_TENSOR:
10141: case DM_POLYTOPE_PYRAMID:
10142: continue;
10143: default:
10144: break;
10145: }
10146: PetscCall(DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit));
10147: if (unsplit) continue;
10148: PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ));
10149: PetscCheck(detJ >= -PETSC_SMALL && (detJ > 0.0 || ignoreZeroVol), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %" PetscInt_FMT " of type %s is inverted, |J| = %g", c, DMPolytopeTypes[ct], (double)detJ);
10150: PetscCall(PetscInfo(dm, "Cell %" PetscInt_FMT " FEM Volume %g\n", c, (double)(detJ * refVol)));
10151: /* This should work with periodicity since DG coordinates should be used */
10152: if (depth > 1) {
10153: PetscCall(DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL));
10154: PetscCheck(vol >= -PETSC_SMALL && (vol > 0.0 || ignoreZeroVol), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %" PetscInt_FMT " of type %s is inverted, vol = %g", c, DMPolytopeTypes[ct], (double)vol);
10155: PetscCall(PetscInfo(dm, "Cell %" PetscInt_FMT " FVM Volume %g\n", c, (double)vol));
10156: }
10157: }
10158: PetscFunctionReturn(PETSC_SUCCESS);
10159: }
10161: /*@
10162: DMPlexCheckPointSF - Check that several necessary conditions are met for the point `PetscSF` of this plex.
10164: Collective
10166: Input Parameters:
10167: + dm - The `DMPLEX` object
10168: . pointSF - The `PetscSF`, or `NULL` for `PointSF` attached to `DM`
10169: - allowExtraRoots - Flag to allow extra points not present in the `DM`
10171: Level: developer
10173: Notes:
10174: This is mainly intended for debugging/testing purposes.
10176: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
10178: Extra roots can come from periodic cuts, where additional points appear on the boundary
10180: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetPointSF()`, `DMSetFromOptions()`
10181: @*/
10182: PetscErrorCode DMPlexCheckPointSF(DM dm, PetscSF pointSF, PetscBool allowExtraRoots)
10183: {
10184: PetscInt l, nleaves, nroots, overlap;
10185: const PetscInt *locals;
10186: const PetscSFNode *remotes;
10187: PetscBool distributed;
10188: MPI_Comm comm;
10189: PetscMPIInt rank;
10191: PetscFunctionBegin;
10194: else pointSF = dm->sf;
10195: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
10196: PetscCheck(pointSF, comm, PETSC_ERR_ARG_WRONGSTATE, "DMPlex must have Point SF attached");
10197: PetscCallMPI(MPI_Comm_rank(comm, &rank));
10198: {
10199: PetscMPIInt mpiFlag;
10201: PetscCallMPI(MPI_Comm_compare(comm, PetscObjectComm((PetscObject)pointSF), &mpiFlag));
10202: PetscCheck(mpiFlag == MPI_CONGRUENT || mpiFlag == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "DM and Point SF have different communicators (flag %d)", mpiFlag);
10203: }
10204: PetscCall(PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, &remotes));
10205: PetscCall(DMPlexIsDistributed(dm, &distributed));
10206: if (!distributed) {
10207: PetscCheck(nroots < 0 || nleaves == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Undistributed DMPlex cannot have non-empty PointSF (has %" PetscInt_FMT " roots, %" PetscInt_FMT " leaves)", nroots, nleaves);
10208: PetscFunctionReturn(PETSC_SUCCESS);
10209: }
10210: PetscCheck(nroots >= 0, comm, PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set (has %" PetscInt_FMT " roots, %" PetscInt_FMT " leaves)", nroots, nleaves);
10211: PetscCall(DMPlexGetOverlap(dm, &overlap));
10213: /* Check SF graph is compatible with DMPlex chart */
10214: {
10215: PetscInt pStart, pEnd, maxLeaf;
10217: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
10218: PetscCall(PetscSFGetLeafRange(pointSF, NULL, &maxLeaf));
10219: PetscCheck(allowExtraRoots || pEnd - pStart == nroots, PETSC_COMM_SELF, PETSC_ERR_PLIB, "pEnd - pStart = %" PetscInt_FMT " != nroots = %" PetscInt_FMT, pEnd - pStart, nroots);
10220: PetscCheck(maxLeaf < pEnd, PETSC_COMM_SELF, PETSC_ERR_PLIB, "maxLeaf = %" PetscInt_FMT " >= pEnd = %" PetscInt_FMT, maxLeaf, pEnd);
10221: }
10223: /* Check there are no cells in interface */
10224: if (!overlap) {
10225: PetscInt cellHeight, cStart, cEnd;
10227: PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
10228: PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
10229: for (l = 0; l < nleaves; ++l) {
10230: const PetscInt point = locals ? locals[l] : l;
10232: PetscCheck(point < cStart || point >= cEnd, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %" PetscInt_FMT " which is a cell", point);
10233: }
10234: }
10236: /* If some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
10237: {
10238: const PetscInt *rootdegree;
10240: PetscCall(PetscSFComputeDegreeBegin(pointSF, &rootdegree));
10241: PetscCall(PetscSFComputeDegreeEnd(pointSF, &rootdegree));
10242: for (l = 0; l < nleaves; ++l) {
10243: const PetscInt point = locals ? locals[l] : l;
10244: const PetscInt *cone;
10245: PetscInt coneSize, c, idx;
10247: PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
10248: PetscCall(DMPlexGetCone(dm, point, &cone));
10249: for (c = 0; c < coneSize; ++c) {
10250: if (!rootdegree[cone[c]]) {
10251: if (locals) PetscCall(PetscFindInt(cone[c], nleaves, locals, &idx));
10252: else idx = (cone[c] < nleaves) ? cone[c] : -1;
10253: PetscCheck(idx >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %" PetscInt_FMT " but not %" PetscInt_FMT " from its cone", point, cone[c]);
10254: }
10255: }
10256: }
10257: }
10258: PetscFunctionReturn(PETSC_SUCCESS);
10259: }
10261: /*@
10262: DMPlexCheckOrphanVertices - Check that no vertices are disconnected from the mesh, unless the mesh only consists of disconnected vertices.
10264: Collective
10266: Input Parameter:
10267: . dm - The `DMPLEX` object
10269: Level: developer
10271: Notes:
10272: This is mainly intended for debugging/testing purposes.
10274: Other cell types which are disconnected would be caught by the symmetry and face checks.
10276: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
10278: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCheck()`, `DMSetFromOptions()`
10279: @*/
10280: PetscErrorCode DMPlexCheckOrphanVertices(DM dm)
10281: {
10282: PetscInt pStart, pEnd, vStart, vEnd;
10284: PetscFunctionBegin;
10285: PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
10286: PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
10287: if (pStart == vStart && pEnd == vEnd) PetscFunctionReturn(PETSC_SUCCESS);
10288: for (PetscInt v = vStart; v < vEnd; ++v) {
10289: PetscInt suppSize;
10291: PetscCall(DMPlexGetSupportSize(dm, v, &suppSize));
10292: PetscCheck(suppSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is disconnected from the mesh", v);
10293: }
10294: PetscFunctionReturn(PETSC_SUCCESS);
10295: }
10297: /*@
10298: DMPlexCheck - Perform various checks of `DMPLEX` sanity
10300: Input Parameter:
10301: . dm - The `DMPLEX` object
10303: Level: developer
10305: Notes:
10306: This is a useful diagnostic when creating meshes programmatically.
10308: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
10310: Currently does not include `DMPlexCheckCellShape()`.
10312: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
10313: @*/
10314: PetscErrorCode DMPlexCheck(DM dm)
10315: {
10316: PetscInt cellHeight;
10318: PetscFunctionBegin;
10319: PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
10320: PetscCall(DMPlexCheckSymmetry(dm));
10321: PetscCall(DMPlexCheckSkeleton(dm, cellHeight));
10322: PetscCall(DMPlexCheckFaces(dm, cellHeight));
10323: PetscCall(DMPlexCheckGeometry(dm));
10324: PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
10325: PetscCall(DMPlexCheckInterfaceCones(dm));
10326: PetscCall(DMPlexCheckOrphanVertices(dm));
10327: PetscFunctionReturn(PETSC_SUCCESS);
10328: }
10330: typedef struct cell_stats {
10331: PetscReal min, max, sum, squaresum;
10332: PetscInt count;
10333: } cell_stats_t;
10335: static void MPIAPI cell_stats_reduce(void *a, void *b, int *len, MPI_Datatype *datatype)
10336: {
10337: PetscInt i, N = *len;
10339: for (i = 0; i < N; i++) {
10340: cell_stats_t *A = (cell_stats_t *)a;
10341: cell_stats_t *B = (cell_stats_t *)b;
10343: B->min = PetscMin(A->min, B->min);
10344: B->max = PetscMax(A->max, B->max);
10345: B->sum += A->sum;
10346: B->squaresum += A->squaresum;
10347: B->count += A->count;
10348: }
10349: }
10351: /*@
10352: DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
10354: Collective
10356: Input Parameters:
10357: + dm - The `DMPLEX` object
10358: . output - If true, statistics will be displayed on `stdout`
10359: - condLimit - Display all cells above this condition number, or `PETSC_DETERMINE` for no cell output
10361: Level: developer
10363: Notes:
10364: This is mainly intended for debugging/testing purposes.
10366: For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.
10368: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexComputeOrthogonalQuality()`
10369: @*/
10370: PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
10371: {
10372: DM dmCoarse;
10373: cell_stats_t stats, globalStats;
10374: MPI_Comm comm = PetscObjectComm((PetscObject)dm);
10375: PetscReal *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
10376: PetscReal limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
10377: PetscInt cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
10378: PetscMPIInt rank, size;
10380: PetscFunctionBegin;
10382: stats.min = PETSC_MAX_REAL;
10383: stats.max = PETSC_MIN_REAL;
10384: stats.sum = stats.squaresum = 0.;
10385: stats.count = 0;
10387: PetscCallMPI(MPI_Comm_size(comm, &size));
10388: PetscCallMPI(MPI_Comm_rank(comm, &rank));
10389: PetscCall(DMGetCoordinateDim(dm, &cdim));
10390: PetscCall(PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ));
10391: PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
10392: PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
10393: for (c = cStart; c < cEnd; c++) {
10394: PetscInt i;
10395: PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
10397: PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, c, NULL, J, invJ, &detJ));
10398: PetscCheck(detJ >= 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %" PetscInt_FMT " is inverted", c);
10399: for (i = 0; i < PetscSqr(cdim); ++i) {
10400: frobJ += J[i] * J[i];
10401: frobInvJ += invJ[i] * invJ[i];
10402: }
10403: cond2 = frobJ * frobInvJ;
10404: cond = PetscSqrtReal(cond2);
10406: stats.min = PetscMin(stats.min, cond);
10407: stats.max = PetscMax(stats.max, cond);
10408: stats.sum += cond;
10409: stats.squaresum += cond2;
10410: stats.count++;
10411: if (output && cond > limit) {
10412: PetscSection coordSection;
10413: Vec coordsLocal;
10414: PetscScalar *coords = NULL;
10415: PetscInt Nv, d, clSize, cl, *closure = NULL;
10417: PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
10418: PetscCall(DMGetCoordinateSection(dm, &coordSection));
10419: PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords));
10420: PetscCall(PetscSynchronizedPrintf(comm, "[%d] Cell %" PetscInt_FMT " cond %g\n", rank, c, (double)cond));
10421: for (i = 0; i < Nv / cdim; ++i) {
10422: PetscCall(PetscSynchronizedPrintf(comm, " Vertex %" PetscInt_FMT ": (", i));
10423: for (d = 0; d < cdim; ++d) {
10424: if (d > 0) PetscCall(PetscSynchronizedPrintf(comm, ", "));
10425: PetscCall(PetscSynchronizedPrintf(comm, "%g", (double)PetscRealPart(coords[i * cdim + d])));
10426: }
10427: PetscCall(PetscSynchronizedPrintf(comm, ")\n"));
10428: }
10429: PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
10430: for (cl = 0; cl < clSize * 2; cl += 2) {
10431: const PetscInt edge = closure[cl];
10433: if ((edge >= eStart) && (edge < eEnd)) {
10434: PetscReal len;
10436: PetscCall(DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL));
10437: PetscCall(PetscSynchronizedPrintf(comm, " Edge %" PetscInt_FMT ": length %g\n", edge, (double)len));
10438: }
10439: }
10440: PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
10441: PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords));
10442: }
10443: }
10444: if (output) PetscCall(PetscSynchronizedFlush(comm, NULL));
10446: if (size > 1) {
10447: PetscMPIInt blockLengths[2] = {4, 1};
10448: MPI_Aint blockOffsets[2] = {offsetof(cell_stats_t, min), offsetof(cell_stats_t, count)};
10449: MPI_Datatype blockTypes[2] = {MPIU_REAL, MPIU_INT}, statType;
10450: MPI_Op statReduce;
10452: PetscCallMPI(MPI_Type_create_struct(2, blockLengths, blockOffsets, blockTypes, &statType));
10453: PetscCallMPI(MPI_Type_commit(&statType));
10454: PetscCallMPI(MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce));
10455: PetscCallMPI(MPI_Reduce(&stats, &globalStats, 1, statType, statReduce, 0, comm));
10456: PetscCallMPI(MPI_Op_free(&statReduce));
10457: PetscCallMPI(MPI_Type_free(&statType));
10458: } else {
10459: PetscCall(PetscArraycpy(&globalStats, &stats, 1));
10460: }
10461: if (rank == 0) {
10462: count = globalStats.count;
10463: min = globalStats.min;
10464: max = globalStats.max;
10465: mean = globalStats.sum / globalStats.count;
10466: stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1), 0)) : 0.0;
10467: }
10469: if (output) PetscCall(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));
10470: PetscCall(PetscFree2(J, invJ));
10472: PetscCall(DMGetCoarseDM(dm, &dmCoarse));
10473: if (dmCoarse) {
10474: PetscBool isplex;
10476: PetscCall(PetscObjectTypeCompare((PetscObject)dmCoarse, DMPLEX, &isplex));
10477: if (isplex) PetscCall(DMPlexCheckCellShape(dmCoarse, output, condLimit));
10478: }
10479: PetscFunctionReturn(PETSC_SUCCESS);
10480: }
10482: /*@
10483: DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
10484: orthogonal quality below given tolerance.
10486: Collective
10488: Input Parameters:
10489: + dm - The `DMPLEX` object
10490: . fv - Optional `PetscFV` object for pre-computed cell/face centroid information
10491: - atol - [0, 1] Absolute tolerance for tagging cells.
10493: Output Parameters:
10494: + OrthQual - `Vec` containing orthogonal quality per cell
10495: - OrthQualLabel - `DMLabel` tagging cells below atol with `DM_ADAPT_REFINE`
10497: Options Database Keys:
10498: + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only `PETSCVIEWERASCII` is supported.
10499: - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.
10501: Level: intermediate
10503: Notes:
10504: Orthogonal quality is given by the following formula\:
10506: $ \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]$
10508: 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
10509: is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
10510: current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
10511: calculating the cosine of the angle between these vectors.
10513: Orthogonal quality ranges from 1 (best) to 0 (worst).
10515: This routine is mainly useful for FVM, however is not restricted to only FVM. The `PetscFV` object is optionally used to check for
10516: pre-computed FVM cell data, but if it is not passed in then this data will be computed.
10518: Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.
10520: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCheckCellShape()`, `DMCreateLabel()`, `PetscFV`, `DMLabel`, `Vec`
10521: @*/
10522: PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PeOp PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
10523: {
10524: PetscInt nc, cellHeight, cStart, cEnd, cell, cellIter = 0;
10525: PetscInt *idx;
10526: PetscScalar *oqVals;
10527: const PetscScalar *cellGeomArr, *faceGeomArr;
10528: PetscReal *ci, *fi, *Ai;
10529: MPI_Comm comm;
10530: Vec cellgeom, facegeom;
10531: DM dmFace, dmCell;
10532: IS glob;
10533: ISLocalToGlobalMapping ltog;
10534: PetscViewer vwr;
10536: PetscFunctionBegin;
10539: PetscAssertPointer(OrthQual, 4);
10540: PetscCheck(atol >= 0.0 && atol <= 1.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Absolute tolerance %g not in [0,1]", (double)atol);
10541: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
10542: PetscCall(DMGetDimension(dm, &nc));
10543: PetscCheck(nc >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %" PetscInt_FMT ")", nc);
10544: {
10545: DMPlexInterpolatedFlag interpFlag;
10547: PetscCall(DMPlexIsInterpolated(dm, &interpFlag));
10548: if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
10549: PetscMPIInt rank;
10551: PetscCallMPI(MPI_Comm_rank(comm, &rank));
10552: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
10553: }
10554: }
10555: if (OrthQualLabel) {
10556: PetscAssertPointer(OrthQualLabel, 5);
10557: PetscCall(DMCreateLabel(dm, "Orthogonal_Quality"));
10558: PetscCall(DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel));
10559: } else {
10560: *OrthQualLabel = NULL;
10561: }
10562: PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
10563: PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
10564: PetscCall(DMPlexCreateCellNumbering(dm, PETSC_TRUE, &glob));
10565: PetscCall(ISLocalToGlobalMappingCreateIS(glob, <og));
10566: PetscCall(ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH));
10567: PetscCall(VecCreate(comm, OrthQual));
10568: PetscCall(VecSetType(*OrthQual, VECSTANDARD));
10569: PetscCall(VecSetSizes(*OrthQual, cEnd - cStart, PETSC_DETERMINE));
10570: PetscCall(VecSetLocalToGlobalMapping(*OrthQual, ltog));
10571: PetscCall(VecSetUp(*OrthQual));
10572: PetscCall(ISDestroy(&glob));
10573: PetscCall(ISLocalToGlobalMappingDestroy(<og));
10574: PetscCall(DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL));
10575: PetscCall(VecGetArrayRead(cellgeom, &cellGeomArr));
10576: PetscCall(VecGetArrayRead(facegeom, &faceGeomArr));
10577: PetscCall(VecGetDM(cellgeom, &dmCell));
10578: PetscCall(VecGetDM(facegeom, &dmFace));
10579: PetscCall(PetscMalloc5(cEnd - cStart, &idx, cEnd - cStart, &oqVals, nc, &ci, nc, &fi, nc, &Ai));
10580: for (cell = cStart; cell < cEnd; cellIter++, cell++) {
10581: PetscInt cellneigh, cellneighiter = 0, adjSize = PETSC_DETERMINE;
10582: PetscInt cellarr[2], *adj = NULL;
10583: PetscScalar *cArr, *fArr;
10584: PetscReal minvalc = 1.0, minvalf = 1.0;
10585: PetscFVCellGeom *cg;
10587: idx[cellIter] = cell - cStart;
10588: cellarr[0] = cell;
10589: /* Make indexing into cellGeom easier */
10590: PetscCall(DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg));
10591: PetscCall(DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj));
10592: /* Technically 1 too big, but easier than fiddling with empty adjacency array */
10593: PetscCall(PetscCalloc2(adjSize, &cArr, adjSize, &fArr));
10594: for (cellneigh = 0; cellneigh < adjSize; cellneighiter++, cellneigh++) {
10595: PetscInt i;
10596: const PetscInt neigh = adj[cellneigh];
10597: PetscReal normci = 0, normfi = 0, normai = 0;
10598: PetscFVCellGeom *cgneigh;
10599: PetscFVFaceGeom *fg;
10601: /* Don't count ourselves in the neighbor list */
10602: if (neigh == cell) continue;
10603: PetscCall(DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh));
10604: cellarr[1] = neigh;
10605: {
10606: PetscInt numcovpts;
10607: const PetscInt *covpts;
10609: PetscCall(DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts));
10610: PetscCall(DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg));
10611: PetscCall(DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts));
10612: }
10614: /* Compute c_i, f_i and their norms */
10615: for (i = 0; i < nc; i++) {
10616: ci[i] = cgneigh->centroid[i] - cg->centroid[i];
10617: fi[i] = fg->centroid[i] - cg->centroid[i];
10618: Ai[i] = fg->normal[i];
10619: normci += PetscPowReal(ci[i], 2);
10620: normfi += PetscPowReal(fi[i], 2);
10621: normai += PetscPowReal(Ai[i], 2);
10622: }
10623: normci = PetscSqrtReal(normci);
10624: normfi = PetscSqrtReal(normfi);
10625: normai = PetscSqrtReal(normai);
10627: /* Normalize and compute for each face-cell-normal pair */
10628: for (i = 0; i < nc; i++) {
10629: ci[i] = ci[i] / normci;
10630: fi[i] = fi[i] / normfi;
10631: Ai[i] = Ai[i] / normai;
10632: /* PetscAbs because I don't know if normals are guaranteed to point out */
10633: cArr[cellneighiter] += PetscAbs(Ai[i] * ci[i]);
10634: fArr[cellneighiter] += PetscAbs(Ai[i] * fi[i]);
10635: }
10636: if (PetscRealPart(cArr[cellneighiter]) < minvalc) minvalc = PetscRealPart(cArr[cellneighiter]);
10637: if (PetscRealPart(fArr[cellneighiter]) < minvalf) minvalf = PetscRealPart(fArr[cellneighiter]);
10638: }
10639: PetscCall(PetscFree(adj));
10640: PetscCall(PetscFree2(cArr, fArr));
10641: /* Defer to cell if they're equal */
10642: oqVals[cellIter] = PetscMin(minvalf, minvalc);
10643: if (OrthQualLabel) {
10644: if (PetscRealPart(oqVals[cellIter]) <= atol) PetscCall(DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE));
10645: }
10646: }
10647: PetscCall(VecSetValuesLocal(*OrthQual, cEnd - cStart, idx, oqVals, INSERT_VALUES));
10648: PetscCall(VecAssemblyBegin(*OrthQual));
10649: PetscCall(VecAssemblyEnd(*OrthQual));
10650: PetscCall(VecRestoreArrayRead(cellgeom, &cellGeomArr));
10651: PetscCall(VecRestoreArrayRead(facegeom, &faceGeomArr));
10652: PetscCall(PetscOptionsCreateViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL));
10653: if (OrthQualLabel) {
10654: if (vwr) PetscCall(DMLabelView(*OrthQualLabel, vwr));
10655: }
10656: PetscCall(PetscFree5(idx, oqVals, ci, fi, Ai));
10657: PetscCall(PetscViewerDestroy(&vwr));
10658: PetscCall(VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view"));
10659: PetscFunctionReturn(PETSC_SUCCESS);
10660: }
10662: /* this is here instead of DMGetOutputDM because output DM still has constraints in the local indices that affect
10663: * interpolator construction */
10664: static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
10665: {
10666: PetscSection section, newSection, gsection;
10667: PetscSF sf;
10668: PetscBool hasConstraints, ghasConstraints;
10670: PetscFunctionBegin;
10672: PetscAssertPointer(odm, 2);
10673: PetscCall(DMGetLocalSection(dm, §ion));
10674: PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
10675: PetscCallMPI(MPIU_Allreduce(&hasConstraints, &ghasConstraints, 1, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
10676: if (!ghasConstraints) {
10677: PetscCall(PetscObjectReference((PetscObject)dm));
10678: *odm = dm;
10679: PetscFunctionReturn(PETSC_SUCCESS);
10680: }
10681: PetscCall(DMClone(dm, odm));
10682: PetscCall(DMCopyFields(dm, PETSC_DETERMINE, PETSC_DETERMINE, *odm));
10683: PetscCall(DMGetLocalSection(*odm, &newSection));
10684: PetscCall(DMGetPointSF(*odm, &sf));
10685: PetscCall(PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_TRUE, PETSC_FALSE, &gsection));
10686: PetscCall(DMSetGlobalSection(*odm, gsection));
10687: PetscCall(PetscSectionDestroy(&gsection));
10688: PetscFunctionReturn(PETSC_SUCCESS);
10689: }
10691: static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
10692: {
10693: DM dmco, dmfo;
10694: Mat interpo;
10695: Vec rscale;
10696: Vec cglobalo, clocal;
10697: Vec fglobal, fglobalo, flocal;
10698: PetscBool regular;
10700: PetscFunctionBegin;
10701: PetscCall(DMGetFullDM(dmc, &dmco));
10702: PetscCall(DMGetFullDM(dmf, &dmfo));
10703: PetscCall(DMSetCoarseDM(dmfo, dmco));
10704: PetscCall(DMPlexGetRegularRefinement(dmf, ®ular));
10705: PetscCall(DMPlexSetRegularRefinement(dmfo, regular));
10706: PetscCall(DMCreateInterpolation(dmco, dmfo, &interpo, &rscale));
10707: PetscCall(DMCreateGlobalVector(dmco, &cglobalo));
10708: PetscCall(DMCreateLocalVector(dmc, &clocal));
10709: PetscCall(VecSet(cglobalo, 0.));
10710: PetscCall(VecSet(clocal, 0.));
10711: PetscCall(DMCreateGlobalVector(dmf, &fglobal));
10712: PetscCall(DMCreateGlobalVector(dmfo, &fglobalo));
10713: PetscCall(DMCreateLocalVector(dmf, &flocal));
10714: PetscCall(VecSet(fglobal, 0.));
10715: PetscCall(VecSet(fglobalo, 0.));
10716: PetscCall(VecSet(flocal, 0.));
10717: PetscCall(DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL));
10718: PetscCall(DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo));
10719: PetscCall(DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo));
10720: PetscCall(MatMult(interpo, cglobalo, fglobalo));
10721: PetscCall(DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal));
10722: PetscCall(DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal));
10723: PetscCall(DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal));
10724: PetscCall(DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal));
10725: *shift = fglobal;
10726: PetscCall(VecDestroy(&flocal));
10727: PetscCall(VecDestroy(&fglobalo));
10728: PetscCall(VecDestroy(&clocal));
10729: PetscCall(VecDestroy(&cglobalo));
10730: PetscCall(VecDestroy(&rscale));
10731: PetscCall(MatDestroy(&interpo));
10732: PetscCall(DMDestroy(&dmfo));
10733: PetscCall(DMDestroy(&dmco));
10734: PetscFunctionReturn(PETSC_SUCCESS);
10735: }
10737: PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
10738: {
10739: PetscObject shifto;
10740: Vec shift;
10742: PetscFunctionBegin;
10743: if (!interp) {
10744: Vec rscale;
10746: PetscCall(DMCreateInterpolation(coarse, fine, &interp, &rscale));
10747: PetscCall(VecDestroy(&rscale));
10748: } else {
10749: PetscCall(PetscObjectReference((PetscObject)interp));
10750: }
10751: PetscCall(PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto));
10752: if (!shifto) {
10753: PetscCall(DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift));
10754: PetscCall(PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject)shift));
10755: shifto = (PetscObject)shift;
10756: PetscCall(VecDestroy(&shift));
10757: }
10758: shift = (Vec)shifto;
10759: PetscCall(MatInterpolate(interp, coarseSol, fineSol));
10760: PetscCall(VecAXPY(fineSol, 1.0, shift));
10761: PetscCall(MatDestroy(&interp));
10762: PetscFunctionReturn(PETSC_SUCCESS);
10763: }
10765: /* Pointwise interpolation
10766: Just code FEM for now
10767: u^f = I u^c
10768: sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
10769: u^f_i = sum_j psi^f_i I phi^c_j u^c_j
10770: I_{ij} = psi^f_i phi^c_j
10771: */
10772: PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
10773: {
10774: PetscSection gsc, gsf;
10775: PetscInt m, n;
10776: void *ctx;
10777: DM cdm;
10778: PetscBool regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;
10780: PetscFunctionBegin;
10781: PetscCall(DMGetGlobalSection(dmFine, &gsf));
10782: PetscCall(PetscSectionGetConstrainedStorageSize(gsf, &m));
10783: PetscCall(DMGetGlobalSection(dmCoarse, &gsc));
10784: PetscCall(PetscSectionGetConstrainedStorageSize(gsc, &n));
10786: PetscCall(PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis));
10787: PetscCall(MatCreate(PetscObjectComm((PetscObject)dmCoarse), interpolation));
10788: PetscCall(MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
10789: PetscCall(MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype));
10790: PetscCall(DMGetApplicationContext(dmFine, &ctx));
10792: PetscCall(DMGetCoarseDM(dmFine, &cdm));
10793: PetscCall(DMPlexGetRegularRefinement(dmFine, ®ular));
10794: if (!isRefined || (regular && cdm == dmCoarse)) PetscCall(DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx));
10795: else PetscCall(DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx));
10796: PetscCall(MatViewFromOptions(*interpolation, NULL, "-interp_mat_view"));
10797: if (scaling) {
10798: /* Use naive scaling */
10799: PetscCall(DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling));
10800: }
10801: PetscFunctionReturn(PETSC_SUCCESS);
10802: }
10804: PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
10805: {
10806: VecScatter ctx;
10808: PetscFunctionBegin;
10809: PetscCall(DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL));
10810: PetscCall(MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat));
10811: PetscCall(VecScatterDestroy(&ctx));
10812: PetscFunctionReturn(PETSC_SUCCESS);
10813: }
10815: 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[])
10816: {
10817: const PetscInt f = (PetscInt)PetscRealPart(constants[numConstants]);
10818: const PetscInt Nc = uOff[f + 1] - uOff[f];
10819: for (PetscInt c = 0; c < Nc; ++c) g0[c * Nc + c] = 1.0;
10820: }
10822: // The assumption here is that the test field is a vector and the basis field is a scalar (so we need the gradient)
10823: static void g1_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 g1[])
10824: {
10825: for (PetscInt c = 0; c < dim; ++c) g1[c * dim + c] = 1.0;
10826: }
10828: PetscErrorCode DMCreateMassMatrixLumped_Plex(DM dm, Vec *lmass, Vec *mass)
10829: {
10830: DM dmc;
10831: PetscDS ds;
10832: Vec ones, locmass;
10833: IS cellIS;
10834: PetscFormKey key;
10835: PetscInt depth;
10837: PetscFunctionBegin;
10838: PetscCall(DMClone(dm, &dmc));
10839: PetscCall(DMCopyDisc(dm, dmc));
10840: PetscCall(DMGetDS(dmc, &ds));
10841: for (PetscInt f = 0; f < dmc->Nf; ++f) PetscCall(PetscDSSetJacobian(ds, f, f, g0_identity_private, NULL, NULL, NULL));
10842: if (mass) PetscCall(DMCreateGlobalVector(dm, mass));
10843: if (lmass) PetscCall(DMCreateLocalVector(dm, &locmass));
10844: else PetscCall(DMGetLocalVector(dm, &locmass));
10845: PetscCall(DMGetLocalVector(dm, &ones));
10846: PetscCall(DMPlexGetDepth(dm, &depth));
10847: PetscCall(DMGetStratumIS(dm, "depth", depth, &cellIS));
10848: PetscCall(VecSet(locmass, 0.0));
10849: PetscCall(VecSet(ones, 1.0));
10850: key.label = NULL;
10851: key.value = 0;
10852: key.field = 0;
10853: key.part = 0;
10854: PetscCall(DMPlexComputeJacobianActionByKey(dmc, key, cellIS, 0.0, 0.0, ones, NULL, ones, locmass, NULL));
10855: PetscCall(ISDestroy(&cellIS));
10856: if (mass) {
10857: PetscCall(DMLocalToGlobalBegin(dm, locmass, ADD_VALUES, *mass));
10858: PetscCall(DMLocalToGlobalEnd(dm, locmass, ADD_VALUES, *mass));
10859: }
10860: PetscCall(DMRestoreLocalVector(dm, &ones));
10861: if (lmass) *lmass = locmass;
10862: else PetscCall(DMRestoreLocalVector(dm, &locmass));
10863: PetscCall(DMDestroy(&dmc));
10864: PetscFunctionReturn(PETSC_SUCCESS);
10865: }
10867: PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
10868: {
10869: PetscSection gsc, gsf;
10870: PetscInt m, n;
10871: void *ctx;
10872: DM cdm;
10873: PetscBool regular;
10875: PetscFunctionBegin;
10876: if (dmFine == dmCoarse) {
10877: DM dmc;
10878: PetscDS ds;
10879: PetscWeakForm wf;
10880: Vec u;
10881: IS cellIS;
10882: PetscFormKey key;
10883: PetscInt depth;
10885: PetscCall(DMClone(dmFine, &dmc));
10886: PetscCall(DMCopyDisc(dmFine, dmc));
10887: PetscCall(DMGetDS(dmc, &ds));
10888: PetscCall(PetscDSGetWeakForm(ds, &wf));
10889: PetscCall(PetscWeakFormClear(wf));
10890: for (PetscInt f = 0; f < dmc->Nf; ++f) PetscCall(PetscDSSetJacobian(ds, f, f, g0_identity_private, NULL, NULL, NULL));
10891: PetscCall(DMCreateMatrix(dmc, mass));
10892: PetscCall(DMGetLocalVector(dmc, &u));
10893: PetscCall(DMPlexGetDepth(dmc, &depth));
10894: PetscCall(DMGetStratumIS(dmc, "depth", depth, &cellIS));
10895: PetscCall(MatZeroEntries(*mass));
10896: key.label = NULL;
10897: key.value = 0;
10898: key.field = 0;
10899: key.part = 0;
10900: PetscCall(DMPlexComputeJacobianByKey(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL));
10901: PetscCall(ISDestroy(&cellIS));
10902: PetscCall(DMRestoreLocalVector(dmc, &u));
10903: PetscCall(DMDestroy(&dmc));
10904: } else {
10905: PetscCall(DMGetGlobalSection(dmFine, &gsf));
10906: PetscCall(PetscSectionGetConstrainedStorageSize(gsf, &m));
10907: PetscCall(DMGetGlobalSection(dmCoarse, &gsc));
10908: PetscCall(PetscSectionGetConstrainedStorageSize(gsc, &n));
10910: PetscCall(MatCreate(PetscObjectComm((PetscObject)dmCoarse), mass));
10911: PetscCall(MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
10912: PetscCall(MatSetType(*mass, dmCoarse->mattype));
10913: PetscCall(DMGetApplicationContext(dmFine, &ctx));
10915: PetscCall(DMGetCoarseDM(dmFine, &cdm));
10916: PetscCall(DMPlexGetRegularRefinement(dmFine, ®ular));
10917: if (regular && cdm == dmCoarse) PetscCall(DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx));
10918: else PetscCall(DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx));
10919: }
10920: PetscCall(MatViewFromOptions(*mass, NULL, "-mass_mat_view"));
10921: PetscFunctionReturn(PETSC_SUCCESS);
10922: }
10924: PetscErrorCode DMCreateGradientMatrix_Plex(DM dmc, DM dmr, Mat *derv)
10925: {
10926: PetscSection gsc, gsf;
10927: PetscInt m, n;
10928: void *ctx;
10930: PetscFunctionBegin;
10931: PetscCall(DMGetGlobalSection(dmr, &gsf));
10932: PetscCall(PetscSectionGetConstrainedStorageSize(gsf, &m));
10933: PetscCall(DMGetGlobalSection(dmc, &gsc));
10934: PetscCall(PetscSectionGetConstrainedStorageSize(gsc, &n));
10936: PetscCall(MatCreate(PetscObjectComm((PetscObject)dmc), derv));
10937: PetscCall(PetscObjectSetName((PetscObject)*derv, "Plex Derivative Matrix"));
10938: PetscCall(MatSetSizes(*derv, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
10939: PetscCall(MatSetType(*derv, dmc->mattype));
10941: PetscCall(DMGetApplicationContext(dmr, &ctx));
10942: {
10943: DM ndmr;
10944: PetscDS ds;
10945: PetscWeakForm wf;
10946: Vec u;
10947: IS cellIS;
10948: PetscFormKey key;
10949: PetscInt depth, Nf;
10951: PetscCall(DMClone(dmr, &ndmr));
10952: PetscCall(DMCopyDisc(dmr, ndmr));
10953: PetscCall(DMGetDS(ndmr, &ds));
10954: PetscCall(PetscDSGetWeakForm(ds, &wf));
10955: PetscCall(PetscWeakFormClear(wf));
10956: PetscCall(PetscDSGetNumFields(ds, &Nf));
10957: for (PetscInt f = 0; f < Nf; ++f) PetscCall(PetscDSSetJacobian(ds, f, f, NULL, g1_identity_private, NULL, NULL));
10958: PetscCall(DMGetLocalVector(ndmr, &u));
10959: PetscCall(DMPlexGetDepth(ndmr, &depth));
10960: PetscCall(DMGetStratumIS(ndmr, "depth", depth, &cellIS));
10961: PetscCall(MatZeroEntries(*derv));
10962: key.label = NULL;
10963: key.value = 0;
10964: key.field = 0;
10965: key.part = 0;
10966: PetscCall(DMPlexComputeJacobianByKeyGeneral(ndmr, dmc, key, cellIS, 0.0, 0.0, u, NULL, *derv, *derv, NULL));
10967: PetscCall(ISDestroy(&cellIS));
10968: PetscCall(DMRestoreLocalVector(ndmr, &u));
10969: PetscCall(DMDestroy(&ndmr));
10970: }
10971: PetscCall(MatViewFromOptions(*derv, NULL, "-gradient_mat_view"));
10972: PetscFunctionReturn(PETSC_SUCCESS);
10973: }
10975: /*@
10976: DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
10978: Input Parameter:
10979: . dm - The `DMPLEX` object
10981: Output Parameter:
10982: . regular - The flag
10984: Level: intermediate
10986: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetRegularRefinement()`
10987: @*/
10988: PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
10989: {
10990: PetscFunctionBegin;
10992: PetscAssertPointer(regular, 2);
10993: *regular = ((DM_Plex *)dm->data)->regularRefinement;
10994: PetscFunctionReturn(PETSC_SUCCESS);
10995: }
10997: /*@
10998: DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
11000: Input Parameters:
11001: + dm - The `DMPLEX` object
11002: - regular - The flag
11004: Level: intermediate
11006: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetRegularRefinement()`
11007: @*/
11008: PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
11009: {
11010: PetscFunctionBegin;
11012: ((DM_Plex *)dm->data)->regularRefinement = regular;
11013: PetscFunctionReturn(PETSC_SUCCESS);
11014: }
11016: /*@
11017: DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints. Typically, the user will not have to
11018: call DMPlexGetAnchors() directly: if there are anchors, then `DMPlexGetAnchors()` is called during `DMGetDefaultConstraints()`.
11020: Not Collective
11022: Input Parameter:
11023: . dm - The `DMPLEX` object
11025: Output Parameters:
11026: + anchorSection - If not `NULL`, set to the section describing which points anchor the constrained points.
11027: - anchorIS - If not `NULL`, set to the list of anchors indexed by `anchorSection`
11029: Level: intermediate
11031: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetAnchors()`, `DMGetDefaultConstraints()`, `DMSetDefaultConstraints()`, `IS`, `PetscSection`
11032: @*/
11033: PetscErrorCode DMPlexGetAnchors(DM dm, PeOp PetscSection *anchorSection, PeOp IS *anchorIS)
11034: {
11035: DM_Plex *plex = (DM_Plex *)dm->data;
11037: PetscFunctionBegin;
11039: if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) PetscCall((*plex->createanchors)(dm));
11040: if (anchorSection) *anchorSection = plex->anchorSection;
11041: if (anchorIS) *anchorIS = plex->anchorIS;
11042: PetscFunctionReturn(PETSC_SUCCESS);
11043: }
11045: /*@
11046: DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.
11048: Collective
11050: Input Parameters:
11051: + dm - The `DMPLEX` object
11052: . anchorSection - The section that describes the mapping from constrained points to the anchor points listed in anchorIS.
11053: Must have a local communicator (`PETSC_COMM_SELF` or derivative).
11054: - anchorIS - The list of all anchor points. Must have a local communicator (`PETSC_COMM_SELF` or derivative).
11056: Level: intermediate
11058: Notes:
11059: Unlike boundary conditions, when a point's degrees of freedom in a section are constrained to
11060: an outside value, the anchor constraints set a point's degrees of freedom to be a linear
11061: combination of other points' degrees of freedom.
11063: After specifying the layout of constraints with `DMPlexSetAnchors()`, one specifies the constraints by calling
11064: `DMGetDefaultConstraints()` and filling in the entries in the constraint matrix.
11066: The reference counts of `anchorSection` and `anchorIS` are incremented.
11068: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetAnchors()`, `DMGetDefaultConstraints()`, `DMSetDefaultConstraints()`
11069: @*/
11070: PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
11071: {
11072: DM_Plex *plex = (DM_Plex *)dm->data;
11073: PetscMPIInt result;
11075: PetscFunctionBegin;
11077: if (anchorSection) {
11079: PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)anchorSection), &result));
11080: PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "anchor section must have local communicator");
11081: }
11082: if (anchorIS) {
11084: PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)anchorIS), &result));
11085: PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "anchor IS must have local communicator");
11086: }
11088: PetscCall(PetscObjectReference((PetscObject)anchorSection));
11089: PetscCall(PetscSectionDestroy(&plex->anchorSection));
11090: plex->anchorSection = anchorSection;
11092: PetscCall(PetscObjectReference((PetscObject)anchorIS));
11093: PetscCall(ISDestroy(&plex->anchorIS));
11094: plex->anchorIS = anchorIS;
11096: if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
11097: PetscInt size, a, pStart, pEnd;
11098: const PetscInt *anchors;
11100: PetscCall(PetscSectionGetChart(anchorSection, &pStart, &pEnd));
11101: PetscCall(ISGetLocalSize(anchorIS, &size));
11102: PetscCall(ISGetIndices(anchorIS, &anchors));
11103: for (a = 0; a < size; a++) {
11104: PetscInt p;
11106: p = anchors[a];
11107: if (p >= pStart && p < pEnd) {
11108: PetscInt dof;
11110: PetscCall(PetscSectionGetDof(anchorSection, p, &dof));
11111: if (dof) {
11112: PetscCall(ISRestoreIndices(anchorIS, &anchors));
11113: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Point %" PetscInt_FMT " cannot be constrained and an anchor", p);
11114: }
11115: }
11116: }
11117: PetscCall(ISRestoreIndices(anchorIS, &anchors));
11118: }
11119: /* reset the generic constraints */
11120: PetscCall(DMSetDefaultConstraints(dm, NULL, NULL, NULL));
11121: PetscFunctionReturn(PETSC_SUCCESS);
11122: }
11124: static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
11125: {
11126: PetscSection anchorSection;
11127: PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
11129: PetscFunctionBegin;
11131: PetscCall(DMPlexGetAnchors(dm, &anchorSection, NULL));
11132: PetscCall(PetscSectionCreate(PETSC_COMM_SELF, cSec));
11133: PetscCall(PetscSectionGetNumFields(section, &numFields));
11134: if (numFields) {
11135: PetscInt f;
11136: PetscCall(PetscSectionSetNumFields(*cSec, numFields));
11138: for (f = 0; f < numFields; f++) {
11139: PetscInt numComp;
11141: PetscCall(PetscSectionGetFieldComponents(section, f, &numComp));
11142: PetscCall(PetscSectionSetFieldComponents(*cSec, f, numComp));
11143: }
11144: }
11145: PetscCall(PetscSectionGetChart(anchorSection, &pStart, &pEnd));
11146: PetscCall(PetscSectionGetChart(section, &sStart, &sEnd));
11147: pStart = PetscMax(pStart, sStart);
11148: pEnd = PetscMin(pEnd, sEnd);
11149: pEnd = PetscMax(pStart, pEnd);
11150: PetscCall(PetscSectionSetChart(*cSec, pStart, pEnd));
11151: for (p = pStart; p < pEnd; p++) {
11152: PetscCall(PetscSectionGetDof(anchorSection, p, &dof));
11153: if (dof) {
11154: PetscCall(PetscSectionGetDof(section, p, &dof));
11155: PetscCall(PetscSectionSetDof(*cSec, p, dof));
11156: for (f = 0; f < numFields; f++) {
11157: PetscCall(PetscSectionGetFieldDof(section, p, f, &dof));
11158: PetscCall(PetscSectionSetFieldDof(*cSec, p, f, dof));
11159: }
11160: }
11161: }
11162: PetscCall(PetscSectionSetUp(*cSec));
11163: PetscCall(PetscObjectSetName((PetscObject)*cSec, "Constraint Section"));
11164: PetscFunctionReturn(PETSC_SUCCESS);
11165: }
11167: static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
11168: {
11169: PetscSection aSec;
11170: PetscInt pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
11171: const PetscInt *anchors;
11172: PetscInt numFields, f;
11173: IS aIS;
11174: MatType mtype;
11175: PetscBool iscuda, iskokkos;
11177: PetscFunctionBegin;
11179: PetscCall(PetscSectionGetStorageSize(cSec, &m));
11180: PetscCall(PetscSectionGetStorageSize(section, &n));
11181: PetscCall(MatCreate(PETSC_COMM_SELF, cMat));
11182: PetscCall(MatSetSizes(*cMat, m, n, m, n));
11183: PetscCall(PetscStrcmp(dm->mattype, MATSEQAIJCUSPARSE, &iscuda));
11184: if (!iscuda) PetscCall(PetscStrcmp(dm->mattype, MATMPIAIJCUSPARSE, &iscuda));
11185: PetscCall(PetscStrcmp(dm->mattype, MATSEQAIJKOKKOS, &iskokkos));
11186: if (!iskokkos) PetscCall(PetscStrcmp(dm->mattype, MATMPIAIJKOKKOS, &iskokkos));
11187: if (iscuda) mtype = MATSEQAIJCUSPARSE;
11188: else if (iskokkos) mtype = MATSEQAIJKOKKOS;
11189: else mtype = MATSEQAIJ;
11190: PetscCall(MatSetType(*cMat, mtype));
11191: PetscCall(DMPlexGetAnchors(dm, &aSec, &aIS));
11192: PetscCall(ISGetIndices(aIS, &anchors));
11193: /* cSec will be a subset of aSec and section */
11194: PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
11195: PetscCall(PetscSectionGetChart(section, &sStart, &sEnd));
11196: PetscCall(PetscMalloc1(m + 1, &i));
11197: i[0] = 0;
11198: PetscCall(PetscSectionGetNumFields(section, &numFields));
11199: for (p = pStart; p < pEnd; p++) {
11200: PetscInt rDof, rOff, r;
11202: PetscCall(PetscSectionGetDof(aSec, p, &rDof));
11203: if (!rDof) continue;
11204: PetscCall(PetscSectionGetOffset(aSec, p, &rOff));
11205: if (numFields) {
11206: for (f = 0; f < numFields; f++) {
11207: annz = 0;
11208: for (r = 0; r < rDof; r++) {
11209: a = anchors[rOff + r];
11210: if (a < sStart || a >= sEnd) continue;
11211: PetscCall(PetscSectionGetFieldDof(section, a, f, &aDof));
11212: annz += aDof;
11213: }
11214: PetscCall(PetscSectionGetFieldDof(cSec, p, f, &dof));
11215: PetscCall(PetscSectionGetFieldOffset(cSec, p, f, &off));
11216: for (q = 0; q < dof; q++) i[off + q + 1] = i[off + q] + annz;
11217: }
11218: } else {
11219: annz = 0;
11220: PetscCall(PetscSectionGetDof(cSec, p, &dof));
11221: for (q = 0; q < dof; q++) {
11222: a = anchors[rOff + q];
11223: if (a < sStart || a >= sEnd) continue;
11224: PetscCall(PetscSectionGetDof(section, a, &aDof));
11225: annz += aDof;
11226: }
11227: PetscCall(PetscSectionGetDof(cSec, p, &dof));
11228: PetscCall(PetscSectionGetOffset(cSec, p, &off));
11229: for (q = 0; q < dof; q++) i[off + q + 1] = i[off + q] + annz;
11230: }
11231: }
11232: nnz = i[m];
11233: PetscCall(PetscMalloc1(nnz, &j));
11234: offset = 0;
11235: for (p = pStart; p < pEnd; p++) {
11236: if (numFields) {
11237: for (f = 0; f < numFields; f++) {
11238: PetscCall(PetscSectionGetFieldDof(cSec, p, f, &dof));
11239: for (q = 0; q < dof; q++) {
11240: PetscInt rDof, rOff, r;
11241: PetscCall(PetscSectionGetDof(aSec, p, &rDof));
11242: PetscCall(PetscSectionGetOffset(aSec, p, &rOff));
11243: for (r = 0; r < rDof; r++) {
11244: PetscInt s;
11246: a = anchors[rOff + r];
11247: if (a < sStart || a >= sEnd) continue;
11248: PetscCall(PetscSectionGetFieldDof(section, a, f, &aDof));
11249: PetscCall(PetscSectionGetFieldOffset(section, a, f, &aOff));
11250: for (s = 0; s < aDof; s++) j[offset++] = aOff + s;
11251: }
11252: }
11253: }
11254: } else {
11255: PetscCall(PetscSectionGetDof(cSec, p, &dof));
11256: for (q = 0; q < dof; q++) {
11257: PetscInt rDof, rOff, r;
11258: PetscCall(PetscSectionGetDof(aSec, p, &rDof));
11259: PetscCall(PetscSectionGetOffset(aSec, p, &rOff));
11260: for (r = 0; r < rDof; r++) {
11261: PetscInt s;
11263: a = anchors[rOff + r];
11264: if (a < sStart || a >= sEnd) continue;
11265: PetscCall(PetscSectionGetDof(section, a, &aDof));
11266: PetscCall(PetscSectionGetOffset(section, a, &aOff));
11267: for (s = 0; s < aDof; s++) j[offset++] = aOff + s;
11268: }
11269: }
11270: }
11271: }
11272: PetscCall(MatSeqAIJSetPreallocationCSR(*cMat, i, j, NULL));
11273: PetscCall(PetscFree(i));
11274: PetscCall(PetscFree(j));
11275: PetscCall(ISRestoreIndices(aIS, &anchors));
11276: PetscFunctionReturn(PETSC_SUCCESS);
11277: }
11279: PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
11280: {
11281: DM_Plex *plex = (DM_Plex *)dm->data;
11282: PetscSection anchorSection, section, cSec;
11283: Mat cMat;
11285: PetscFunctionBegin;
11287: PetscCall(DMPlexGetAnchors(dm, &anchorSection, NULL));
11288: if (anchorSection) {
11289: PetscInt Nf;
11291: PetscCall(DMGetLocalSection(dm, §ion));
11292: PetscCall(DMPlexCreateConstraintSection_Anchors(dm, section, &cSec));
11293: PetscCall(DMPlexCreateConstraintMatrix_Anchors(dm, section, cSec, &cMat));
11294: PetscCall(DMGetNumFields(dm, &Nf));
11295: if (Nf && plex->computeanchormatrix) PetscCall((*plex->computeanchormatrix)(dm, section, cSec, cMat));
11296: PetscCall(DMSetDefaultConstraints(dm, cSec, cMat, NULL));
11297: PetscCall(PetscSectionDestroy(&cSec));
11298: PetscCall(MatDestroy(&cMat));
11299: }
11300: PetscFunctionReturn(PETSC_SUCCESS);
11301: }
11303: PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
11304: {
11305: IS subis;
11306: PetscSection section, subsection;
11308: PetscFunctionBegin;
11309: PetscCall(DMGetLocalSection(dm, §ion));
11310: PetscCheck(section, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
11311: PetscCheck(subdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
11312: /* Create subdomain */
11313: PetscCall(DMPlexFilter(dm, label, value, PETSC_FALSE, PETSC_FALSE, PetscObjectComm((PetscObject)dm), NULL, subdm));
11314: /* Create submodel */
11315: PetscCall(DMPlexGetSubpointIS(*subdm, &subis));
11316: PetscCall(PetscSectionCreateSubmeshSection(section, subis, &subsection));
11317: PetscCall(DMSetLocalSection(*subdm, subsection));
11318: PetscCall(PetscSectionDestroy(&subsection));
11319: PetscCall(DMCopyDisc(dm, *subdm));
11320: /* Create map from submodel to global model */
11321: if (is) {
11322: PetscSection sectionGlobal, subsectionGlobal;
11323: IS spIS;
11324: const PetscInt *spmap;
11325: PetscInt *subIndices;
11326: PetscInt subSize = 0, subOff = 0, pStart, pEnd, p;
11327: PetscInt Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
11329: PetscCall(DMPlexGetSubpointIS(*subdm, &spIS));
11330: PetscCall(ISGetIndices(spIS, &spmap));
11331: PetscCall(PetscSectionGetNumFields(section, &Nf));
11332: PetscCall(DMGetGlobalSection(dm, §ionGlobal));
11333: PetscCall(DMGetGlobalSection(*subdm, &subsectionGlobal));
11334: PetscCall(PetscSectionGetChart(subsection, &pStart, &pEnd));
11335: for (p = pStart; p < pEnd; ++p) {
11336: PetscInt gdof, pSubSize = 0;
11338: PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
11339: if (gdof > 0) {
11340: for (f = 0; f < Nf; ++f) {
11341: PetscInt fdof, fcdof;
11343: PetscCall(PetscSectionGetFieldDof(subsection, p, f, &fdof));
11344: PetscCall(PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof));
11345: pSubSize += fdof - fcdof;
11346: }
11347: subSize += pSubSize;
11348: if (pSubSize) {
11349: if (bs < 0) {
11350: bs = pSubSize;
11351: } else if (bs != pSubSize) {
11352: /* Layout does not admit a pointwise block size */
11353: bs = 1;
11354: }
11355: }
11356: }
11357: }
11358: /* Must have same blocksize on all procs (some might have no points) */
11359: bsLocal[0] = bs < 0 ? PETSC_INT_MAX : bs;
11360: bsLocal[1] = bs;
11361: PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11362: if (bsMinMax[0] != bsMinMax[1]) bs = 1;
11363: else bs = bsMinMax[0];
11364: PetscCall(PetscMalloc1(subSize, &subIndices));
11365: for (p = pStart; p < pEnd; ++p) {
11366: PetscInt gdof, goff;
11368: PetscCall(PetscSectionGetDof(subsectionGlobal, p, &gdof));
11369: if (gdof > 0) {
11370: const PetscInt point = spmap[p];
11372: PetscCall(PetscSectionGetOffset(sectionGlobal, point, &goff));
11373: for (f = 0; f < Nf; ++f) {
11374: PetscInt fdof, fcdof, fc, f2, poff = 0;
11376: /* Can get rid of this loop by storing field information in the global section */
11377: for (f2 = 0; f2 < f; ++f2) {
11378: PetscCall(PetscSectionGetFieldDof(section, p, f2, &fdof));
11379: PetscCall(PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof));
11380: poff += fdof - fcdof;
11381: }
11382: PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
11383: PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
11384: for (fc = 0; fc < fdof - fcdof; ++fc, ++subOff) subIndices[subOff] = goff + poff + fc;
11385: }
11386: }
11387: }
11388: PetscCall(ISRestoreIndices(spIS, &spmap));
11389: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is));
11390: if (bs > 1) {
11391: /* We need to check that the block size does not come from non-contiguous fields */
11392: PetscInt i, j, set = 1;
11393: for (i = 0; i < subSize; i += bs) {
11394: for (j = 0; j < bs; ++j) {
11395: if (subIndices[i + j] != subIndices[i] + j) {
11396: set = 0;
11397: break;
11398: }
11399: }
11400: }
11401: if (set) PetscCall(ISSetBlockSize(*is, bs));
11402: }
11403: // Attach nullspace
11404: if (dm->nullspaceConstructors) {
11405: for (f = 0; f < Nf; ++f) {
11406: (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
11407: if ((*subdm)->nullspaceConstructors[f]) break;
11408: }
11409: if (f < Nf) {
11410: MatNullSpace nullSpace;
11411: PetscCall((*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace));
11413: PetscCall(PetscObjectCompose((PetscObject)*is, "nullspace", (PetscObject)nullSpace));
11414: PetscCall(MatNullSpaceDestroy(&nullSpace));
11415: }
11416: }
11417: }
11418: PetscFunctionReturn(PETSC_SUCCESS);
11419: }
11421: /*@
11422: DMPlexMonitorThroughput - Report the cell throughput of FE integration
11424: Input Parameters:
11425: + dm - The `DM`
11426: - unused - unused argument
11428: Options Database Key:
11429: . -dm_plex_monitor_throughput - Activate the monitor
11431: Level: developer
11433: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreate()`
11434: @*/
11435: PetscErrorCode DMPlexMonitorThroughput(DM dm, void *unused)
11436: {
11437: PetscLogHandler default_handler;
11439: PetscFunctionBegin;
11441: PetscCall(PetscLogGetDefaultHandler(&default_handler));
11442: if (default_handler) {
11443: PetscLogEvent event;
11444: PetscEventPerfInfo eventInfo;
11445: PetscLogDouble cellRate, flopRate;
11446: PetscInt cStart, cEnd, Nf, N;
11447: const char *name;
11449: PetscCall(PetscObjectGetName((PetscObject)dm, &name));
11450: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
11451: PetscCall(DMGetNumFields(dm, &Nf));
11452: PetscCall(PetscLogEventGetId("DMPlexResidualFE", &event));
11453: PetscCall(PetscLogEventGetPerfInfo(PETSC_DEFAULT, event, &eventInfo));
11454: N = (cEnd - cStart) * Nf * eventInfo.count;
11455: flopRate = eventInfo.flops / eventInfo.time;
11456: cellRate = N / eventInfo.time;
11457: PetscCall(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, cellRate, flopRate / 1.e6));
11458: } else {
11459: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off or the default log handler is not running. Reconfigure using --with-log and run with -log_view.");
11460: }
11461: PetscFunctionReturn(PETSC_SUCCESS);
11462: }
11464: static inline PetscInt DMPlex_GlobalID(PetscInt point)
11465: {
11466: return point >= 0 ? point : -(point + 1);
11467: }
11469: /*
11470: Computes the graph laplacian L at the given depth.
11471: L = D - A, with D = degree matrix and A = adjacency matrix
11472: */
11473: static PetscErrorCode DMPlexCreateGraphLaplacian_Private(DM dm, PetscInt depth, Mat *oL)
11474: {
11475: Mat L, preall;
11476: Vec x, y;
11477: IS pointNumbering;
11478: const PetscInt *pointNum;
11479: PetscInt *i, *j, numVertices, numEdges, shift, maxnnzrow, dim, *numDof, numFields;
11480: PetscInt pStart, pEnd;
11481: PetscScalar *vals;
11482: PetscSection s;
11484: PetscFunctionBeginUser;
11485: PetscCall(DMGetDimension(dm, &dim));
11486: {
11487: /* XXX this generalizes DMPlexCreatePartitionerGraph to any height and adjacency */
11488: PetscCall(DMPlexGetDepthStratum(dm, depth, &pStart, &pEnd));
11489: PetscCall(DMPlexCreatePointNumbering(dm, &pointNumbering));
11490: PetscCall(ISGetIndices(pointNumbering, &pointNum));
11491: shift = DMPlex_GlobalID(pointNum[pStart]);
11492: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &shift, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm)));
11493: /* Determine sizes */
11494: numVertices = 0;
11495: for (PetscInt p = pStart; p < pEnd; p++) {
11496: /* Skip non-owned cells in parallel */
11497: if (pointNum[p] < 0) continue;
11498: numVertices++;
11499: }
11500: numEdges = 0;
11501: for (PetscInt p = pStart; p < pEnd; p++) {
11502: PetscInt nadj = PETSC_DETERMINE;
11503: PetscInt *adj = NULL;
11504: /* Skip non-owned cells in parallel */
11505: if (pointNum[p] < 0) continue;
11506: PetscCall(DMPlexGetAdjacency(dm, p, &nadj, &adj));
11507: for (PetscInt a = 0; a < nadj; a++)
11508: if (adj[a] != p && pStart <= adj[a] && adj[a] < pEnd) numEdges++;
11509: PetscCall(PetscFree(adj));
11510: }
11511: /* Determine adjacency */
11512: PetscCall(PetscMalloc1(numVertices + 1, &i));
11513: PetscCall(PetscMalloc1(numEdges, &j));
11514: PetscInt iptr = 0;
11515: i[0] = iptr;
11516: for (PetscInt p = pStart; p < pEnd; p++) {
11517: PetscInt nadj = PETSC_DETERMINE;
11518: PetscInt *adj = NULL;
11519: /* Skip non-owned cells in parallel */
11520: if (pointNum[p] < 0) continue;
11521: PetscCall(DMPlexGetAdjacency(dm, p, &nadj, &adj));
11522: for (PetscInt a = 0; a < nadj; a++)
11523: if (adj[a] != p && pStart <= adj[a] && adj[a] < pEnd) j[iptr++] = DMPlex_GlobalID(pointNum[adj[a]]) - shift;
11524: PetscCall(PetscFree(adj));
11525: i[p - pStart + 1] = iptr;
11526: /* Sort adjacencies (not strictly necessary) */
11527: PetscCall(PetscSortInt(iptr - i[p - pStart], &j[i[p - pStart]]));
11528: }
11529: PetscCall(ISRestoreIndices(pointNumbering, &pointNum));
11530: PetscCall(ISDestroy(&pointNumbering));
11531: }
11532: /* First create a matrix object */
11533: PetscCall(MatCreate(PetscObjectComm((PetscObject)dm), &L));
11534: PetscCall(MatSetSizes(L, numVertices, numVertices, PETSC_DECIDE, PETSC_DECIDE));
11535: PetscCall(MatSetOptionsPrefix(L, "dm_plex_laplacian_"));
11536: PetscCall(MatSetFromOptions(L));
11537: /* Preallocation */
11538: PetscCall(MatCreate(PetscObjectComm((PetscObject)dm), &preall));
11539: PetscCall(MatSetSizes(preall, numVertices, numVertices, PETSC_DECIDE, PETSC_DECIDE));
11540: PetscCall(MatSetType(preall, MATPREALLOCATOR));
11541: PetscCall(MatSetUp(preall));
11542: PetscCall(MatGetOwnershipRange(preall, &shift, NULL));
11543: maxnnzrow = 0;
11544: for (PetscInt k = 0; k < numVertices; k++) {
11545: PetscInt nnzrow = i[k + 1] - i[k];
11546: PetscInt row = shift + k;
11547: PetscInt *col = j + i[k];
11548: maxnnzrow = PetscMax(maxnnzrow, nnzrow);
11549: /* Add adjacency connection */
11550: PetscCall(MatSetValues(preall, 1, &row, nnzrow, col, NULL, INSERT_VALUES));
11551: /* The graph CSR does not represent self-to-self connections, we need them
11552: for the graph laplacian */
11553: PetscCall(MatSetValues(preall, 1, &row, 1, &row, NULL, INSERT_VALUES));
11554: }
11555: PetscCall(MatAssemblyBegin(preall, MAT_FINAL_ASSEMBLY));
11556: PetscCall(MatAssemblyEnd(preall, MAT_FINAL_ASSEMBLY));
11557: /* Preallocate the graph laplacian matrix */
11558: PetscCall(MatPreallocatorPreallocate(preall, PETSC_TRUE, L));
11559: PetscCall(MatDestroy(&preall));
11560: /* Set values. We first set all values to -1.0 to obtain -A,
11561: and then use matrix API to modify for our needs and add the diagonal D matrix */
11562: PetscCall(PetscMalloc1(maxnnzrow, &vals));
11563: for (PetscInt k = 0; k < maxnnzrow; k++) vals[k] = -1.0;
11564: for (PetscInt k = 0; k < numVertices; k++) {
11565: PetscInt nnzrow = i[k + 1] - i[k];
11566: PetscInt row = shift + k;
11567: PetscInt *col = j + i[k];
11568: PetscCall(MatSetValues(L, 1, &row, nnzrow, col, vals, INSERT_VALUES));
11569: }
11570: PetscCall(MatAssemblyBegin(L, MAT_FINAL_ASSEMBLY));
11571: PetscCall(MatAssemblyEnd(L, MAT_FINAL_ASSEMBLY));
11572: /* Add D. Here we use the fact that D = rowsum(A) */
11573: PetscCall(MatCreateVecs(L, &x, &y));
11574: PetscCall(VecSet(x, -1.0));
11575: PetscCall(MatMult(L, x, y));
11576: PetscCall(MatDiagonalSet(L, y, INSERT_VALUES));
11577: PetscCall(MatAssemblyBegin(L, MAT_FINAL_ASSEMBLY));
11578: PetscCall(MatAssemblyEnd(L, MAT_FINAL_ASSEMBLY));
11579: PetscCall(VecDestroy(&x));
11580: PetscCall(VecDestroy(&y));
11581: /* Clean up */
11582: PetscCall(PetscFree(vals));
11583: PetscCall(PetscFree(i));
11584: PetscCall(PetscFree(j));
11585: /* Allow command line view via -laplacian_view */
11586: PetscCall(MatViewFromOptions(L, NULL, "-view"));
11587: /*
11588: For visualization purposes, we attach a DM to the matrix.
11589: Cloning makes a shallow (pointer) copy of the mesh topology and geometry,
11590: and allows us to consider different discretization spaces.
11591: In this case, we specify a one-field discretization with a PetscSection object.
11592: */
11593: PetscCall(DMClone(dm, &dm));
11594: numFields = 1;
11595: PetscCall(DMSetNumFields(dm, numFields));
11596: PetscCall(PetscCalloc1(dim + 1, &numDof));
11597: numDof[depth] = 1;
11598: PetscCall(DMPlexCreateSection(dm, NULL, &numFields, numDof, 0, NULL, NULL, NULL, NULL, &s));
11599: PetscCall(DMSetLocalSection(dm, s));
11600: PetscCall(PetscSectionDestroy(&s));
11601: PetscCall(PetscFree(numDof));
11602: /* Attach the DM to the matrix */
11603: PetscCall(MatSetDM(L, dm));
11604: /* the matrix holds a reference to the DM, we can decrease reference counting */
11605: PetscCall(DMDestroy(&dm));
11606: /* Return matrix to caller */
11607: *oL = L;
11608: PetscFunctionReturn(PETSC_SUCCESS);
11609: }
11611: /*@
11612: DMPlexCreateColoring - Gets coloring of the connectivity graph of the `DMPlex` points at a given depth.
11614: Collective
11616: Input Parameters:
11617: + dm - the `DMPlex` object
11618: . depth - the dimension of the entities in the connectivity graph.
11619: - distance - the distance of the coloring (either 1 or 2).
11621: Output Parameter:
11622: . coloring - the coloring
11624: Level: developer
11626: Notes:
11627: Unlike `DMCreateColoring`, the graph used for the coloring does not represent the operator matrix associated with the discretization of a PDE on the `DM`.
11628: Here the coloring is computed from the connectivity graph of the mesh entities.
11630: Coloring of matrices can also be computed directly from the sparse matrix nonzero structure via the `MatColoring` object or from the mesh from which the
11631: matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
11633: Mesh colorings are useful for additive and multiplicative Schwarz methods.
11634: In particular, they mitigate overhead costs associated with setting up individual KSPs and PCs on many subdomains per process.
11635: A coloring of the vertices (`depth=0`) with `distance=1` can be use can be used to group non-overlapping vertex-star patches into multi-patch subdomains.
11636: Similarly, a vertex coloring with `distance=2` can be used to group non-overlapping Vanka patches into multi-patch subdomains.
11638: .seealso: [](ch_unstructured), `DMPlex`, `ISColoring`, `MatColoring`, `DMCreateColoring()`
11639: @*/
11640: PetscErrorCode DMPlexCreateColoring(DM dm, PetscInt depth, PetscInt distance, ISColoring *coloring)
11641: {
11642: Mat L = NULL;
11643: MatColoring mc = NULL;
11644: IS *iscolors = NULL;
11645: PetscInt pStart = 0, offset = 0, ncolors = 0;
11647: PetscFunctionBegin;
11648: /* Create a graph Laplacian */
11649: PetscCall(DMPlexCreateGraphLaplacian_Private(dm, depth, &L));
11650: /* Compute offset */
11651: PetscCall(MatGetOwnershipRange(L, &offset, NULL));
11652: PetscCall(DMPlexGetDepthStratum(dm, depth, &pStart, NULL));
11653: offset = pStart - offset;
11654: /* Obtain ISColoring via MatColoring */
11655: PetscCall(MatColoringCreate(L, &mc));
11656: PetscCall(MatColoringSetType(mc, MATCOLORINGGREEDY));
11657: PetscCall(MatColoringSetDistance(mc, distance));
11658: PetscCall(MatColoringSetFromOptions(mc));
11659: PetscCall(MatColoringApply(mc, coloring));
11660: PetscCall(MatColoringDestroy(&mc));
11661: /* Destroy the graph Laplacian */
11662: PetscCall(MatDestroy(&L));
11663: /* Shift ISColoring to align with the DMPlex numbering */
11664: PetscCall(ISColoringGetIS(*coloring, PETSC_USE_POINTER, &ncolors, &iscolors));
11665: for (PetscInt c = 0; c < ncolors; c++) {
11666: PetscCall(ISShift(iscolors[c], offset, iscolors[c]));
11667: }
11668: PetscCall(ISColoringRestoreIS(*coloring, PETSC_USE_POINTER, &iscolors));
11669: PetscFunctionReturn(PETSC_SUCCESS);
11670: }