Actual source code: dmperiodicity.c
1: #include <petsc/private/dmimpl.h>
3: #include <petscdmplex.h>
5: /*@C
6: DMGetPeriodicity - Get the description of mesh periodicity
8: Not collective
10: Input Parameter:
11: . dm - The `DM` object
13: Output Parameters:
14: + maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates
15: . Lstart - If we assume the mesh is a torus, this is the start of each coordinate, or `NULL` for 0.0
16: - L - If we assume the mesh is a torus, this is the length of each coordinate, otherwise it is < 0.0
18: Level: developer
20: .seealso: `DM`
21: @*/
22: PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal *maxCell[], const PetscReal *Lstart[], const PetscReal *L[])
23: {
24: PetscFunctionBegin;
26: if (maxCell) *maxCell = dm->maxCell;
27: if (Lstart) *Lstart = dm->Lstart;
28: if (L) *L = dm->L;
29: PetscFunctionReturn(PETSC_SUCCESS);
30: }
32: /*@
33: DMSetPeriodicity - Set the description of mesh periodicity
35: Logically Collective
37: Input Parameters:
38: + dm - The `DM` object
39: . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates. Pass `NULL` to remove such information.
40: . Lstart - If we assume the mesh is a torus, this is the start of each coordinate, or `NULL` for 0.0
41: - L - If we assume the mesh is a torus, this is the length of each coordinate, otherwise it is < 0.0
43: Level: developer
45: .seealso: `DM`, `DMGetPeriodicity()`
46: @*/
47: PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal Lstart[], const PetscReal L[])
48: {
49: PetscInt dim, d;
51: PetscFunctionBegin;
53: if (maxCell) PetscAssertPointer(maxCell, 2);
54: if (Lstart) PetscAssertPointer(Lstart, 3);
55: if (L) PetscAssertPointer(L, 4);
56: PetscCall(DMGetDimension(dm, &dim));
57: if (maxCell) {
58: if (!dm->maxCell) PetscCall(PetscMalloc1(dim, &dm->maxCell));
59: for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
60: } else { /* remove maxCell information to disable automatic computation of localized vertices */
61: PetscCall(PetscFree(dm->maxCell));
62: dm->maxCell = NULL;
63: }
64: if (Lstart) {
65: if (!dm->Lstart) PetscCall(PetscMalloc1(dim, &dm->Lstart));
66: for (d = 0; d < dim; ++d) dm->Lstart[d] = Lstart[d];
67: } else { /* remove L information to disable automatic computation of localized vertices */
68: PetscCall(PetscFree(dm->Lstart));
69: dm->Lstart = NULL;
70: }
71: if (L) {
72: if (!dm->L) PetscCall(PetscMalloc1(dim, &dm->L));
73: for (d = 0; d < dim; ++d) dm->L[d] = L[d];
74: } else { /* remove L information to disable automatic computation of localized vertices */
75: PetscCall(PetscFree(dm->L));
76: dm->L = NULL;
77: }
78: PetscCheck((dm->maxCell && dm->L) || (!dm->maxCell && !dm->L), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cannot set only one of maxCell/L");
79: PetscFunctionReturn(PETSC_SUCCESS);
80: }
82: /*@
83: DMLocalizeCoordinate - If a mesh is periodic (a torus with lengths L_i, some of which can be infinite), project the coordinate onto [0, L_i) in each dimension.
85: Input Parameters:
86: + dm - The `DM`
87: . in - The input coordinate point (dim numbers)
88: - endpoint - Include the endpoint L_i
90: Output Parameter:
91: . out - The localized coordinate point (dim numbers)
93: Level: developer
95: .seealso: `DM`, `DMLocalizeCoordinates()`, `DMLocalizeAddCoordinate()`
96: @*/
97: PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
98: {
99: PetscInt dim, d;
101: PetscFunctionBegin;
102: PetscCall(DMGetCoordinateDim(dm, &dim));
103: if (!dm->maxCell) {
104: for (d = 0; d < dim; ++d) out[d] = in[d];
105: } else {
106: if (endpoint) {
107: for (d = 0; d < dim; ++d) {
108: if ((PetscAbsReal(PetscRealPart(in[d]) / dm->L[d] - PetscFloorReal(PetscRealPart(in[d]) / dm->L[d])) < PETSC_SMALL) && (PetscRealPart(in[d]) / dm->L[d] > PETSC_SMALL)) {
109: out[d] = in[d] - dm->L[d] * (PetscFloorReal(PetscRealPart(in[d]) / dm->L[d]) - 1);
110: } else {
111: out[d] = in[d] - dm->L[d] * PetscFloorReal(PetscRealPart(in[d]) / dm->L[d]);
112: }
113: }
114: } else {
115: for (d = 0; d < dim; ++d) out[d] = in[d] - dm->L[d] * PetscFloorReal(PetscRealPart(in[d]) / dm->L[d]);
116: }
117: }
118: PetscFunctionReturn(PETSC_SUCCESS);
119: }
121: static PetscErrorCode DMLocalizeCoordinatePerDimension_Internal(DM dm, PetscInt d, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
122: {
123: PetscFunctionBegin;
124: if ((dm->L[d] > 0.0) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
125: out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
126: } else {
127: out[d] = in[d];
128: }
129: PetscFunctionReturn(PETSC_SUCCESS);
130: }
132: /*
133: DMLocalizeCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
135: Input Parameters:
136: + dm - The `DM`
137: . dim - The spatial dimension
138: . anchor - The anchor point, the input point can be no more than maxCell away from it
139: - in - The input coordinate point (dim numbers)
141: Output Parameter:
142: . out - The localized coordinate point (dim numbers)
144: Level: developer
146: Note:
147: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
149: .seealso: `DM`, `DMLocalizeCoordinates()`, `DMLocalizeAddCoordinate()`
150: */
151: PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
152: {
153: PetscInt d;
155: PetscFunctionBegin;
156: if (!dm->maxCell) {
157: for (d = 0; d < dim; ++d) out[d] = in[d];
158: } else {
159: for (d = 0; d < dim; ++d) PetscCall(DMLocalizeCoordinatePerDimension_Internal(dm, d, anchor, in, out));
160: }
161: PetscFunctionReturn(PETSC_SUCCESS);
162: }
164: PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
165: {
166: PetscInt d;
168: PetscFunctionBegin;
169: if (!dm->maxCell) {
170: for (d = 0; d < dim; ++d) out[d] = in[d];
171: } else {
172: for (d = 0; d < dim; ++d) {
173: if ((dm->L[d] > 0.0) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
174: out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
175: } else {
176: out[d] = in[d];
177: }
178: }
179: }
180: PetscFunctionReturn(PETSC_SUCCESS);
181: }
183: /*
184: DMLocalizeAddCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
186: Input Parameters:
187: + dm - The `DM`
188: . dim - The spatial dimension
189: . anchor - The anchor point, the input point can be no more than maxCell away from it
190: . in - The input coordinate delta (dim numbers)
191: - out - The input coordinate point (dim numbers)
193: Output Parameter:
194: . out - The localized coordinate in + out
196: Level: developer
198: Note:
199: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually one of the vertices on a containing cell
201: .seealso: `DM`, `DMLocalizeCoordinates()`, `DMLocalizeCoordinate()`
202: */
203: PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
204: {
205: PetscInt d;
207: PetscFunctionBegin;
208: if (!dm->maxCell) {
209: for (d = 0; d < dim; ++d) out[d] += in[d];
210: } else {
211: for (d = 0; d < dim; ++d) {
212: const PetscReal maxC = dm->maxCell[d];
214: if ((dm->L[d] > 0.0) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
215: const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
217: if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
218: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT "-Coordinate %g more than %g away from anchor %g", d, (double)PetscRealPart(in[d]), (double)maxC, (double)PetscRealPart(anchor[d]));
219: out[d] += newCoord;
220: } else {
221: out[d] += in[d];
222: }
223: }
224: }
225: PetscFunctionReturn(PETSC_SUCCESS);
226: }
228: /*@
229: DMGetCoordinatesLocalizedLocal - Check if the `DM` coordinates have been localized for cells on this process
231: Not Collective
233: Input Parameter:
234: . dm - The `DM`
236: Output Parameter:
237: . areLocalized - `PETSC_TRUE` if localized
239: Level: developer
241: .seealso: `DM`, `DMLocalizeCoordinates()`, `DMGetCoordinatesLocalized()`, `DMSetPeriodicity()`
242: @*/
243: PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm, PetscBool *areLocalized)
244: {
245: PetscFunctionBegin;
247: PetscAssertPointer(areLocalized, 2);
248: *areLocalized = dm->coordinates[1].dim < 0 ? PETSC_FALSE : PETSC_TRUE;
249: PetscFunctionReturn(PETSC_SUCCESS);
250: }
252: /*@
253: DMGetCoordinatesLocalized - Check if the `DM` coordinates have been localized for cells
255: Collective
257: Input Parameter:
258: . dm - The `DM`
260: Output Parameter:
261: . areLocalized - `PETSC_TRUE` if localized
263: Level: developer
265: .seealso: `DM`, `DMLocalizeCoordinates()`, `DMSetPeriodicity()`, `DMGetCoordinatesLocalizedLocal()`
266: @*/
267: PetscErrorCode DMGetCoordinatesLocalized(DM dm, PetscBool *areLocalized)
268: {
269: PetscBool localized;
271: PetscFunctionBegin;
273: PetscAssertPointer(areLocalized, 2);
274: PetscCall(DMGetCoordinatesLocalizedLocal(dm, &localized));
275: PetscCallMPI(MPIU_Allreduce(&localized, areLocalized, 1, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
276: PetscFunctionReturn(PETSC_SUCCESS);
277: }
279: /*@
280: DMGetSparseLocalize - Check if the `DM` coordinates should be localized only for cells near the periodic boundary.
282: Not collective
284: Input Parameter:
285: . dm - The `DM`
287: Output Parameter:
288: . sparse - `PETSC_TRUE` if only cells near the periodic boundary are localized
290: Level: intermediate
292: .seealso: `DMSetSparseLocalize()`, `DMLocalizeCoordinates()`, `DMSetPeriodicity()`
293: @*/
294: PetscErrorCode DMGetSparseLocalize(DM dm, PetscBool *sparse)
295: {
296: PetscFunctionBegin;
298: PetscAssertPointer(sparse, 2);
299: *sparse = dm->sparseLocalize;
300: PetscFunctionReturn(PETSC_SUCCESS);
301: }
303: /*@
304: DMSetSparseLocalize - Set the flag indicating that `DM` coordinates should be localized only for cells near the periodic boundary.
306: Collective
308: Input Parameters:
309: + dm - The `DM`
310: - sparse - `PETSC_TRUE` if only cells near the periodic boundary are localized
312: Level: intermediate
314: Note:
315: If previous cell coordinates existed with a different sparse localization then these will be destroyed.
317: .seealso: `DMGetSparseLocalize()`, `DMLocalizeCoordinates()`, `DMSetPeriodicity()`
318: @*/
319: PetscErrorCode DMSetSparseLocalize(DM dm, PetscBool sparse)
320: {
321: PetscBool prevSparse, areLocalized;
323: PetscFunctionBegin;
326: // Blow away previously localized coordinates if they were created with the other sparse localize option
327: PetscCall(DMGetSparseLocalize(dm, &prevSparse));
328: PetscCall(DMGetCoordinatesLocalized(dm, &areLocalized));
329: if (prevSparse != sparse && areLocalized) PetscCall(DMDestroyCoordinates_Internal(&dm->coordinates[1]));
330: dm->sparseLocalize = sparse;
331: PetscFunctionReturn(PETSC_SUCCESS);
332: }
334: /*@
335: DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
337: Collective
339: Input Parameter:
340: . dm - The `DM`
342: Level: developer
344: .seealso: `DM`, `DMSetPeriodicity()`, `DMLocalizeCoordinate()`, `DMLocalizeAddCoordinate()`
345: @*/
346: PetscErrorCode DMLocalizeCoordinates(DM dm)
347: {
348: DM cdm, cdgdm, cplex, plex;
349: PetscSection cs, csDG;
350: Vec coordinates, cVec;
351: PetscScalar *coordsDG, *anchor, *localized;
352: const PetscReal *Lstart, *L;
353: PetscInt Nc, vStart, vEnd, sStart, sEnd, newStart = PETSC_INT_MAX, newEnd = PETSC_INT_MIN, bs, coordSize;
354: PetscBool isLocalized, sparseLocalize, useDG = PETSC_FALSE, useDGGlobal;
355: PetscInt maxHeight = 0, h;
356: PetscInt *pStart = NULL, *pEnd = NULL;
357: MPI_Comm comm;
359: PetscFunctionBegin;
361: PetscCall(DMGetPeriodicity(dm, NULL, &Lstart, &L));
362: PetscCall(DMGetSparseLocalize(dm, &sparseLocalize));
363: /* Cannot automatically localize without L and maxCell right now */
364: if (!L) PetscFunctionReturn(PETSC_SUCCESS);
365: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
366: PetscCall(DMGetCoordinatesLocalized(dm, &isLocalized));
367: if (isLocalized) PetscFunctionReturn(PETSC_SUCCESS);
369: PetscCall(DMGetCoordinateDM(dm, &cdm));
370: PetscCall(DMConvert(dm, DMPLEX, &plex));
371: PetscCall(DMConvert(cdm, DMPLEX, &cplex));
372: PetscCheck(cplex, comm, PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
373: PetscCall(DMPlexGetDepthStratum(cplex, 0, &vStart, &vEnd));
374: PetscCall(DMPlexGetMaxProjectionHeight(cplex, &maxHeight));
375: PetscCall(DMGetWorkArray(dm, 2 * (maxHeight + 1), MPIU_INT, &pStart));
376: pEnd = &pStart[maxHeight + 1];
377: newStart = vStart;
378: newEnd = vEnd;
379: for (h = 0; h <= maxHeight; h++) {
380: PetscCall(DMPlexGetHeightStratum(cplex, h, &pStart[h], &pEnd[h]));
381: newStart = PetscMin(newStart, pStart[h]);
382: newEnd = PetscMax(newEnd, pEnd[h]);
383: }
384: PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
385: PetscCheck(coordinates, comm, PETSC_ERR_SUP, "Missing local coordinate vector");
386: PetscCall(DMGetCoordinateSection(dm, &cs));
387: PetscCall(VecGetBlockSize(coordinates, &bs));
388: PetscCall(PetscSectionGetChart(cs, &sStart, &sEnd));
390: PetscCall(PetscSectionCreate(comm, &csDG));
391: PetscCall(PetscSectionSetNumFields(csDG, 1));
392: PetscCall(PetscSectionGetFieldComponents(cs, 0, &Nc));
393: PetscCall(PetscSectionSetFieldComponents(csDG, 0, Nc));
394: PetscCall(PetscSectionSetChart(csDG, newStart, newEnd));
395: PetscCheck(bs == Nc, comm, PETSC_ERR_ARG_INCOMP, "Coordinate block size %" PetscInt_FMT " != %" PetscInt_FMT " number of components", bs, Nc);
397: PetscCall(DMGetWorkArray(dm, 2 * Nc, MPIU_SCALAR, &anchor));
398: localized = &anchor[Nc];
399: for (h = 0; h <= maxHeight; h++) {
400: PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
402: for (c = cStart; c < cEnd; ++c) {
403: PetscScalar *cellCoords = NULL;
404: DMPolytopeType ct;
405: PetscInt dof, d, p;
407: PetscCall(DMPlexGetCellType(plex, c, &ct));
408: if (ct == DM_POLYTOPE_FV_GHOST) continue;
409: PetscCall(DMPlexVecGetClosure(cplex, cs, coordinates, c, &dof, &cellCoords));
410: PetscCheck(!(dof % Nc), comm, PETSC_ERR_ARG_INCOMP, "Coordinate size on cell %" PetscInt_FMT " closure %" PetscInt_FMT " not divisible by %" PetscInt_FMT " number of components", c, dof, Nc);
411: for (d = 0; d < Nc; ++d) anchor[d] = cellCoords[d];
412: for (p = 0; p < dof / Nc; ++p) {
413: PetscCall(DMLocalizeCoordinate_Internal(dm, Nc, anchor, &cellCoords[p * Nc], localized));
414: for (d = 0; d < Nc; ++d)
415: if (cellCoords[p * Nc + d] != localized[d]) break;
416: if (d < Nc) break;
417: }
418: if (p < dof / Nc) useDG = PETSC_TRUE;
419: if (p < dof / Nc || !sparseLocalize) {
420: PetscCall(PetscSectionSetDof(csDG, c, dof));
421: PetscCall(PetscSectionSetFieldDof(csDG, c, 0, dof));
422: }
423: PetscCall(DMPlexVecRestoreClosure(cplex, cs, coordinates, c, &dof, &cellCoords));
424: }
425: }
426: PetscCallMPI(MPIU_Allreduce(&useDG, &useDGGlobal, 1, MPI_C_BOOL, MPI_LOR, comm));
427: if (!useDGGlobal) goto end;
429: PetscCall(PetscSectionSetUp(csDG));
430: PetscCall(PetscSectionGetStorageSize(csDG, &coordSize));
431: PetscCall(VecCreate(PETSC_COMM_SELF, &cVec));
432: PetscCall(PetscObjectSetName((PetscObject)cVec, "coordinates"));
433: PetscCall(VecSetBlockSize(cVec, bs));
434: PetscCall(VecSetSizes(cVec, coordSize, PETSC_DETERMINE));
435: PetscCall(VecSetType(cVec, VECSTANDARD));
436: PetscCall(VecGetArray(cVec, &coordsDG));
437: for (h = 0; h <= maxHeight; h++) {
438: PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
440: for (c = cStart; c < cEnd; ++c) {
441: PetscScalar *cellCoords = NULL;
442: PetscInt p = 0, q, dof, cdof, d, offDG;
444: PetscCall(PetscSectionGetDof(csDG, c, &cdof));
445: if (!cdof) continue;
446: PetscCall(DMPlexVecGetClosure(cplex, cs, coordinates, c, &dof, &cellCoords));
447: PetscCall(PetscSectionGetOffset(csDG, c, &offDG));
448: // TODO The coordinates are set in closure order, which might not be the tensor order
449: for (d = 0; d < Nc; ++d) {
450: for (q = 0; q < dof / Nc; ++q) {
451: anchor[d] = cellCoords[q * Nc + d];
452: // Map anchor into [Lstart, Lstart+L)
453: if (L[d] > 0.0) {
454: if (PetscRealPart(anchor[d]) < (Lstart ? Lstart[d] : 0.)) anchor[d] += L[d];
455: else if (PetscRealPart(anchor[d]) > (Lstart ? Lstart[d] : 0.) + L[d]) anchor[d] -= L[d];
456: }
457: for (p = 0; p < dof / Nc; ++p) {
458: PetscCall(DMLocalizeCoordinatePerDimension_Internal(dm, d, anchor, &cellCoords[p * Nc], &coordsDG[offDG + p * Nc]));
459: // We need the cell to fit into the torus [lower, lower+L)
460: if (L[d] > 0. && ((PetscRealPart(coordsDG[offDG + p * Nc + d]) < (Lstart ? Lstart[d] : 0.)) || (PetscRealPart(coordsDG[offDG + p * Nc + d]) > (Lstart ? Lstart[d] : 0.) + L[d]))) break;
461: }
462: // Suitable anchor found, continue
463: if (p == dof / Nc) break;
464: }
465: // No suitable anchor found, abort
466: if (q == dof / Nc) break;
467: }
468: PetscCheck(d == Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " does not fit into the torus %s[0, L]", c, Lstart ? "Lstart + " : "");
469: PetscCall(DMPlexVecRestoreClosure(cplex, cs, coordinates, c, &dof, &cellCoords));
470: }
471: }
472: PetscCall(VecRestoreArray(cVec, &coordsDG));
473: PetscUseTypeMethod(dm, createcellcoordinatedm, &cdgdm);
474: PetscCall(DMSetCellCoordinateDM(dm, cdgdm));
475: PetscCall(DMDestroy(&cdgdm));
476: // Convert the discretization
477: {
478: PetscFE fe;
479: PetscClassId id;
481: PetscCall(DMGetField(cdm, 0, NULL, (PetscObject *)&fe));
482: PetscCall(PetscObjectGetClassId((PetscObject)fe, &id));
483: if (id == PETSCFE_CLASSID) {
484: PetscSpace P;
485: PetscInt degree;
487: PetscCall(PetscFEGetBasisSpace(fe, &P));
488: PetscCall(PetscSpaceGetDegree(P, °ree, NULL));
489: PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, PETSC_FALSE));
490: }
491: }
492: PetscCall(DMSetCellCoordinateSection(dm, PETSC_DETERMINE, csDG));
493: PetscCall(DMSetCellCoordinatesLocal(dm, cVec));
494: PetscCall(VecDestroy(&cVec));
496: end:
497: PetscCall(DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
498: PetscCall(DMRestoreWorkArray(dm, 2 * (maxHeight + 1), MPIU_INT, &pStart));
499: PetscCall(PetscSectionDestroy(&csDG));
500: PetscCall(DMDestroy(&plex));
501: PetscCall(DMDestroy(&cplex));
502: PetscFunctionReturn(PETSC_SUCCESS);
503: }