peclet-amr 0.2.0
Block-local-Morton AMR octree + collocated cut-cell Navier–Stokes solver (header-only C++20, Kokkos + MPI)
Loading...
Searching...
No Matches
seam_recon.hpp
Go to the documentation of this file.
1// amr — the prebuilt tables the convective flux needs at a 2:1 coarse/fine seam (ROADMAP B5).
2//
3// docs/amr_cf_convective.md: the SOU/Koren reconstruction of the ADVECTED value is O(h) wrong at
4// every face whose upwind-side stencil crosses a level — the coarse upwind cell's column is
5// TANGENTIALLY offset from the sub-face centroid (case 1), and the second upwind probe of the two
6// same-level faces beside the seam sits at the wrong DISTANCE and at a tangential offset as well
7// (cases 2 and 3). The fix is a level-aware, upwind-side reconstruction; everything it needs that
8// depends only on the mesh is prebuilt here, once, in setSolid.
9//
10// THREE KINDS OF ENTRY, one CSR of records (§5.2):
11//
12// * a SAMPLE record, one per 2:1 sub-face pair (C, F): `φ_C` sampled at F's tangential column,
13// i.e. the entry (C, 1) followed VERBATIM by `cf_scheme.hpp::cfAppendStencil`'s
14// (coarse* − coarse) delta — so the record's weights sum to 1 and the gathered value is the
15// Martin–Cartwright tangential quadratic the C/F operator overlays already use. It is read
16// twice: as the sub-face's own `φ_U*` (case 1) and as F's upstream probe on the face behind it
17// (case 2), where its `recDist` = 1.5·width(F) is the probe's TRUE distance.
18// * a LAYER record, one per (coarse cell C, face) whose far side is refined: the mean of the
19// four fine cells in that face layer, `recDist` = 0.75·width(C) — the upstream probe of C's
20// opposite face (case 3), again at its true distance.
21// * a DESCRIPTOR, one per face slot that references at least one record: which record plays
22// `φ_U*` / `φ_D*` and which plays the upstream probe, for each of the two flow directions,
23// plus the half widths `d1I/d1J`. A slot with no live field gets `seam = −1` and the kernels
24// take TODAY'S line, bit for bit.
25//
26// GATES (§5.4). No sample record unless both incident cells are REGULAR (fluid and not cut — the
27// per-face C/F flux gate of docs/amr_cf_flux_gate.md, ghost-visible under MPI); no layer record
28// unless the coarse cell is regular and all four fine cells are fluid; never a partial record. A
29// withheld record leaves the slot on today's arithmetic, so cut seams are bit-identical.
30//
31// DETERMINISM. The walk is SEQUENTIAL over the local rows in `forEachFaceFull` order and record
32// ids are assigned on first reference, so the tables are a pure function of the mesh — which is
33// what makes the np = 1 build bitwise identical to the single-rank one (records are gathered in
34// record order by the kernel, never reordered).
35#ifndef PECLET_AMR_SEAM_RECON_HPP
36#define PECLET_AMR_SEAM_RECON_HPP
37
38#include <array>
39#include <map>
40#include <type_traits>
41#include <utility>
42#include <vector>
43
45#include "peclet/amr/common.hpp"
47#include "peclet/core/common/types.hpp"
48
49namespace peclet::amr {
50
60 std::vector<Index> seam;
61 std::vector<Index> sampI;
62 std::vector<Index> sampJ;
63 std::vector<Index> uuRecI;
64 std::vector<Index> uuRecJ;
65 std::vector<double> d1I;
66 std::vector<double> d1J;
67 std::vector<Index> recStart;
68 std::vector<double> recDist;
69 std::vector<Index> recCell;
70 std::vector<double> recW;
71 Index numSampleRecords = 0;
72 Index numLayerRecords = 0;
74 Index numDescriptors() const { return static_cast<Index>(sampI.size()); }
75};
76
77namespace detail {
78
82template <unsigned Bits>
83inline bool seamFaceLayer(const AmrPoisson<3, Bits>& ap, Index C, int axis, int dir,
84 std::array<Index, 4>& cells) {
85 const unsigned Lc = ap.levelOf(C);
86 if (Lc == 0)
87 return false; // C is at the finest level: nothing below it
88 const std::array<long, 3> lo = ap.loOf(C);
89 const long sc = 1L << Lc, sh = sc >> 1;
90 std::array<long, 3> p = lo;
91 p[axis] = (dir > 0) ? lo[axis] + sc : lo[axis] - 1;
92 for (int k = 0; k < 4; ++k) {
93 std::array<long, 3> q = p;
94 int bit = 0;
95 for (int t = 0; t < 3; ++t) {
96 if (t == axis)
97 continue;
98 q[t] = lo[t] + (((k >> bit) & 1) ? sh : 0L);
99 ++bit;
100 }
101 const auto [c, Lq] = ap.probeSlot(q);
102 if (c < 0 || Lq + 1 != Lc)
103 return false;
104 cells[static_cast<std::size_t>(k)] = c;
105 }
106 return true;
107}
108
109} // namespace detail
110
111namespace detail {
112
115template <unsigned Bits, class RegularFn, class FluidFn>
118 RegularFn& regularOk;
119 FluidFn& fluidOk;
122 std::vector<std::vector<detail::ScalarEnt>> rec; // entries, one vector per record
123 std::vector<double> dist; // recDist, one per record
124 // (coarse, fine, axis) -> sample record id; (coarse, axis, dir) -> layer record id. A withheld
125 // record is cached as −1 so a gate is evaluated once per key, not once per referencing slot.
126 std::map<std::array<Index, 3>, Index> sampleOf, layerOf;
127
129 Index sample(Index C, Index F, int axis) {
130 if (C < 0 || F < 0)
131 return -1;
132 const std::array<Index, 3> key{C, F, static_cast<Index>(axis)};
133 const auto it = sampleOf.find(key);
134 if (it != sampleOf.end())
135 return it->second;
136 Index id = -1;
137 if (regularOk(C) && regularOk(F)) {
138 std::vector<detail::ScalarEnt> e;
139 e.push_back(detail::ScalarEnt{C, 1.0}); // the raw coarse value; the stencil is its DELTA
140 detail::cfAppendStencil(ap, e, C, F, axis, 1.0, fluidOk, scheme);
141 id = static_cast<Index>(rec.size());
142 rec.push_back(std::move(e));
143 dist.push_back(1.5 * static_cast<double>(ap.cellWidth(F, axis)));
144 ++out.numSampleRecords;
145 }
146 sampleOf.emplace(key, id);
147 return id;
148 }
149
151 Index layer(Index C, int axis, int dir) {
152 if (C < 0)
153 return -1;
154 const std::array<Index, 3> key{C, static_cast<Index>(axis), static_cast<Index>(dir)};
155 const auto it = layerOf.find(key);
156 if (it != layerOf.end())
157 return it->second;
158 Index id = -1;
159 std::array<Index, 4> cells{};
160 if (regularOk(C) && detail::seamFaceLayer(ap, C, axis, dir, cells)) {
161 bool ok = true;
162 for (const Index c : cells)
163 ok = ok && fluidOk(c);
164 if (ok) {
165 std::vector<detail::ScalarEnt> e;
166 for (const Index c : cells)
167 e.push_back(detail::ScalarEnt{c, 0.25});
168 id = static_cast<Index>(rec.size());
169 rec.push_back(std::move(e));
170 dist.push_back(0.75 * static_cast<double>(ap.cellWidth(C, axis)));
171 ++out.numLayerRecords;
172 }
173 }
174 layerOf.emplace(key, id);
175 return id;
176 }
177
179 void faceSlot(Index i, Index j, int a, int d) {
180 const unsigned Li = ap.levelOf(i), Lj = ap.levelOf(j);
181 Index rsI = -1, rsJ = -1, ruI = -1, ruJ = -1;
182 // Case 1 — this slot IS a 2:1 sub-face: the coarse side's value is sampled at the fine
183 // side's tangential column. (A level in this octree counts DOWN: 0 is the finest.)
184 if (Lj != Li) {
185 if (Li > Lj)
186 rsI = sample(i, j, a);
187 else
188 rsJ = sample(j, i, a);
189 }
190 // Cases 2/3 — the upstream probe of each side crosses a level: coarser ⇒ the same
191 // tangential sample at its true distance; finer ⇒ the mean of that face's fine layer.
192 const Index uI = ap.periodicNeighbor(i, a, -d);
193 if (uI >= 0) {
194 const unsigned Lu = ap.levelOf(uI);
195 if (Lu > Li)
196 ruI = sample(uI, i, a);
197 else if (Lu < Li)
198 ruI = layer(i, a, -d);
199 }
200 const Index uJ = ap.periodicNeighbor(j, a, d);
201 if (uJ >= 0) {
202 const unsigned Lu = ap.levelOf(uJ);
203 if (Lu > Lj)
204 ruJ = sample(uJ, j, a);
205 else if (Lu < Lj)
206 ruJ = layer(j, a, d);
207 }
208 if (rsI < 0 && rsJ < 0 && ruI < 0 && ruJ < 0) {
209 out.seam.push_back(-1); // plain slot: today's reconstruction, bit for bit
210 return;
211 }
212 out.seam.push_back(out.numDescriptors());
213 out.sampI.push_back(rsI);
214 out.sampJ.push_back(rsJ);
215 out.uuRecI.push_back(ruI);
216 out.uuRecJ.push_back(ruJ);
217 out.d1I.push_back(0.5 * static_cast<double>(ap.cellWidth(i, a)));
218 out.d1J.push_back(0.5 * static_cast<double>(ap.cellWidth(j, a)));
219 }
220
223 const Index nRec = static_cast<Index>(rec.size());
224 out.recStart.assign(static_cast<std::size_t>(nRec) + 1, 0);
225 for (Index r = 0; r < nRec; ++r)
226 out.recStart[static_cast<std::size_t>(r) + 1] =
227 out.recStart[static_cast<std::size_t>(r)] +
228 static_cast<Index>(rec[static_cast<std::size_t>(r)].size());
229 out.recDist = std::move(dist);
230 out.recCell.reserve(static_cast<std::size_t>(out.recStart[static_cast<std::size_t>(nRec)]));
231 out.recW.reserve(static_cast<std::size_t>(out.recStart[static_cast<std::size_t>(nRec)]));
232 for (Index r = 0; r < nRec; ++r)
233 for (const auto& e : rec[static_cast<std::size_t>(r)]) {
234 out.recCell.push_back(e.cell);
235 out.recW.push_back(e.w);
236 }
237 return std::move(out);
238 }
239};
240
241} // namespace detail
242
261template <unsigned Bits, class RegularFn, class FluidFn>
262SeamReconHost buildSeamRecon(const AmrPoisson<3, Bits>& ap, RegularFn&& regularOk,
263 FluidFn&& fluidOk, CfScheme scheme) {
264 const Index n = ap.octree().numLeaves();
266 std::remove_reference_t<FluidFn>>
267 b{ap, regularOk, fluidOk, scheme, {}, {}, {}, {}, {}};
268 for (Index i = 0; i < n; ++i)
270 i, [&](Index j, int a, int d, double, double, double) { b.faceSlot(i, j, a, d); });
271 return b.pack();
272}
273
274} // namespace peclet::amr
275
276#endif // PECLET_AMR_SEAM_RECON_HPP
Cell-centered FV Poisson operator on one (periodic) block octree.
Definition poisson.hpp:43
unsigned levelOf(Index slot) const
Octree level of an extended slot (local leaf or declared ghost).
Definition poisson.hpp:98
void forEachFaceFull(Index i, Fn &&fn) const
Like forEachFaceNeighbor but exposes geometry for a consistent FV divergence/gradient: fn(neighbour,...
Definition poisson.hpp:329
std::pair< Index, unsigned > probeSlot(const std::array< long, Dim > &p) const
Covering slot + level of a block-local probe (longs, possibly outside the block).
Definition poisson.hpp:120
const Octree & octree() const
Definition poisson.hpp:586
std::array< long, Dim > loOf(Index slot) const
Block-local lo corner of an extended slot as longs (ghosts may lie outside the block).
Definition poisson.hpp:103
void cfAppendStencil(const AmrPoisson< 3, Bits > &ap, std::vector< Ent > &out, Index coarse, Index fine, int axis, double scale, FluidFn &&fluidOk, CfScheme scheme, const Ent &proto=Ent{})
Append the scheme's (coarse* − coarse_raw) substitution stencil for ONE directed C/F sub-face to out,...
bool seamFaceLayer(const AmrPoisson< 3, Bits > &ap, Index C, int axis, int dir, std::array< Index, 4 > &cells)
The four sub-face neighbour leaves across C's face (axis, dir), in forEachFaceFull's own sub-face ord...
SeamReconHost buildSeamRecon(const AmrPoisson< 3, Bits > &ap, RegularFn &&regularOk, FluidFn &&fluidOk, CfScheme scheme)
Build the seam-reconstruction tables over the LOCAL rows of ap (docs/amr_cf_convective....
CfScheme
Coarse/fine interface scheme for the collocated flow operators.
Definition cf_scheme.hpp:73
Host-side seam-reconstruction tables (docs/amr_cf_convective.md §5.2), built once per AmrFlow::setSol...
Index numDescriptors() const
Number of descriptors, i.e. of face slots that take the seam path (seam[slot] >= 0).
std::vector< Index > recStart
record CSR offsets, size nRec+1
std::vector< double > recW
record entries: weight
Index numSampleRecords
diagnostics: how many of the records are sample records
Index numLayerRecords
diagnostics: how many are layer records
std::vector< double > d1J
per descriptor: half width of j along the face axis (physical)
std::vector< Index > seam
per face slot: descriptor id, −1 = plain slot
std::vector< double > recDist
per record: dUU for UPSTREAM use (unused as a φ_U* sample)
std::vector< Index > sampI
per descriptor: sample record when i is the COARSE cell, else −1
std::vector< double > d1I
per descriptor: half width of i along the face axis (physical)
std::vector< Index > uuRecJ
per descriptor: upstream-probe record for up == j, else −1
std::vector< Index > sampJ
per descriptor: sample record when j is the COARSE cell, else −1
std::vector< Index > recCell
record entries: cell slot
std::vector< Index > uuRecI
per descriptor: upstream-probe record for up == i, else −1
The record tables of buildSeamRecon under construction: the descriptors (out), the record entries and...
const AmrPoisson< 3, Bits > & ap
Index layer(Index C, int axis, int dir)
The fine-layer record of C's (axis, dir) face (cached, -1 when withheld).
SeamReconHost pack()
Pack the records into the CSR of the result.
std::map< std::array< Index, 3 >, Index > sampleOf
Index sample(Index C, Index F, int axis)
The tangential-sample record of coarse C at fine F's column (cached, -1 when withheld).
std::vector< std::vector< detail::ScalarEnt > > rec
void faceSlot(Index i, Index j, int a, int d)
The descriptor of face slot (i, j, a, d) in forEachFaceFull order.
std::map< std::array< Index, 3 >, Index > layerOf