flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
flow_ibm_hydro.hpp
Go to the documentation of this file.
1
8#ifndef PECLET_FLOW_FLOW_IBM_HYDRO_HPP
9#define PECLET_FLOW_FLOW_IBM_HYDRO_HPP
10
11namespace peclet::flow {
12
13template <class Grid>
14void Solver<Grid>::scaleForceTorque(std::vector<double>& out, std::size_t m) const {
15 const double kF = u_.forceTotalToPhys(), kT = u_.torqueToPhys();
16 if (kF == 1.0 && kT == 1.0)
17 return;
18 for (std::size_t b = 0; m > 0 && b * m < out.size(); ++b) {
19 const double k = (b == 1) ? kT : kF;
20 for (std::size_t i = b * m; i < (b + 1) * m && i < out.size(); ++i)
21 out[i] *= k;
22 }
23}
24
25template <class Grid>
26std::vector<double> Solver<Grid>::hydroForceTorque() {
27 std::vector<double> out((std::size_t)(nInst_ > 0 ? nInst_ : 0) * 12, 0.0);
28 if (!hasScene_ || nInst_ <= 0 || cutOwner_.extent(0) != (std::size_t)nx_ * ny_ * nz_)
29 return out;
30 const std::size_t m = (std::size_t)nInst_ * 3;
31 Kokkos::View<double*, CCMem> Fd("hydroF", m), Td("hydroT", m), Pd("hydroFp", m), Vd("hydroFv", m);
32 Kokkos::deep_copy(Fd, 0.0);
33 Kokkos::deep_copy(Td, 0.0);
34 Kokkos::deep_copy(Pd, 0.0);
35 Kokkos::deep_copy(Vd, 0.0);
36 CCExec space;
37 const C3 e = e_, og = og_;
38 const int nx = nx_, ny = ny_;
39 const double mu = mu_;
40 // §4.4: kA[a] = V'/h_a' turns the index fragment normal W_a into the physical area vector; the
41 // hp ratios below turn the index-velocity gradient pair into the physical strain rate.
42 const double kA0 = u_.vol / u_.hp[0], kA1 = u_.vol / u_.hp[1], kA2 = u_.vol / u_.hp[2];
43 const double hp0 = u_.hp[0], hp1 = u_.hp[1], hp2 = u_.hp[2];
44 CCConst oxv = CCConst(ox_), oyv = CCConst(oy_), ozv = CCConst(oz_);
45 CCConst U = CCConst(C[0].u), Vv = CCConst(C[1].u), W = CCConst(C[2].u);
46 CCConst Pf = CCConst(P_);
47 CCConst sd = CCConst(sdf_);
48 const bool haveWallVel = (uwCell_[0].extent(0) == n_);
49 CCConst wcx = haveWallVel ? CCConst(uwCell_[0]) : CCConst();
50 CCConst wcy = haveWallVel ? CCConst(uwCell_[1]) : CCConst();
51 CCConst wcz = haveWallVel ? CCConst(uwCell_[2]) : CCConst();
52
53 auto own = cutOwner_;
54 auto cen = instCenD_;
55 const auto box = sceneQ_->view().box;
56 const SceneMap sm = sceneMap();
57 using MD = MDRange3<CCExec>;
58 Kokkos::parallel_for(
59 "peclet::flow::hydro_force", MD(space, {G, G, G}, {e.x - G, e.y - G, e.z - G}),
60 KOKKOS_LAMBDA(int x, int y, int z) {
61 const long st[3] = {1, e.x, (long)e.x * e.y};
62 const long i = (long)x + (long)y * st[1] + (long)z * st[2];
63 const double A[3] = {-(oxv(i + st[0]) - oxv(i)), -(oyv(i + st[1]) - oyv(i)),
64 -(ozv(i + st[2]) - ozv(i))};
65 if (A[0] == 0.0 && A[1] == 0.0 && A[2] == 0.0)
66 return; // no wall passes through this cell
67 const int oi = own((std::size_t)(x - G) + (std::size_t)(y - G) * nx +
68 (std::size_t)(z - G) * (std::size_t)nx * ny);
69 if (oi < 0)
70 return;
71 // Cell-centred velocity from the staggered faces.
72 //
73 // A SOLID CELL STORES A MASKED ZERO, WHICH IS THE MATERIAL VELOCITY ONLY WHEN THE WALL
74 // IS AT REST. Reading it as physical is harmless for static geometry and a sign error
75 // for moving geometry: the stencil then sees a spurious shear of the wall speed over one
76 // cell across the entire surface. Measured on the Galilean pair -- with the velocity
77 // field itself frame-invariant to 7e-7, the integrated force came out +7.08e+01 in the
78 // lab frame and -1.71e+02 in a frame boosted by 0.7, a ratio of -2.42. A resolved
79 // CFD-DEM loop driven by that does not settle, it runs away. Substituting the wall's own
80 // velocity restores frame invariance; with static geometry uwCell_ is empty and this is
81 // bit-identical to the plain expression.
82 auto uc = [&](int a, long c) {
83 if (haveWallVel && sd(c) < 0.0)
84 return a == 0 ? wcx(c) : (a == 1 ? wcy(c) : wcz(c));
85 const CCConst& F = a == 0 ? U : (a == 1 ? Vv : W);
86 return 0.5 * (F(c) + F(c + st[a]));
87 };
88 // PLAIN CENTRAL DIFFERENCE, as the Layer-4 spec prescribes. It spans 2h while the wall
89 // sits a fraction of a cell away, so it under-reads the wall shear -- measured as a
90 // RESOLUTION-INDEPENDENT ~29% drag deficit that lives almost entirely in the viscous
91 // part. The obvious one-sided repair, differencing to the wall over the crossing
92 // distance theta, was TRIED AND IS WORSE: cut cells with theta -> 0 make 1/theta
93 // unbounded and the drag came out 17x too large. That is precisely why the momentum
94 // operator uses a Robust-Scaled reconstruction rather than a raw one-sided difference,
95 // and it is why a correct wall-aware traction has to come from that machinery (or from
96 // the discrete reaction the operator already applies) rather than from a patch here.
97 // See the design note's OPEN FOR REVIEW.
98 double gu[3][3];
99 for (int a = 0; a < 3; ++a)
100 for (int b = 0; b < 3; ++b)
101 gu[a][b] = 0.5 * (uc(a, i + st[b]) - uc(a, i - st[b]));
102 const double p = Pf(i);
103 const double kA[3] = {kA0, kA1, kA2}; // V'/h_a' (exactly 1.0 isotropic)
104 const double hpv[3] = {hp0, hp1, hp2};
105 double dF[3], dFp[3], dFv[3];
106 for (int a = 0; a < 3; ++a) {
107 // A_wall is fluid-outward; the traction on the BODY takes -A_wall. Pressure and
108 // viscous parts are kept apart because they fail differently: the pressure term reads
109 // one cell-centred value, while the viscous term differences a velocity whose stencil
110 // reaches into solid cells -- so a deficit that lives entirely in one of them says
111 // immediately which.
112 dFp[a] = (p * A[a]) * kA[a];
113 double t = 0.0;
114 for (int b = 0; b < 3; ++b) {
115 const double rab = hpv[a] / hpv[b], rba = hpv[b] / hpv[a];
116 t += mu * (rab * gu[a][b] + rba * gu[b][a]) * (A[b] * kA[b]);
117 }
118 dFv[a] = -t;
119 dF[a] = dFp[a] + dFv[a];
120 }
121 // The instance centre and the min-image box live in the SCENE's coordinates; dF is an
122 // index-unit force, so the lever arm has to come back to index units (a uniform scale, so
123 // min-imaging in either system is the same shortening).
124 const peclet::core::Vec3<double> rp = peclet::core::geom::minImage(
125 peclet::core::Vec3<double>{
126 sm.a[0] + sm.b[0] * (double)(x - G + og.x) - cen[3 * oi + 0],
127 sm.a[1] + sm.b[1] * (double)(y - G + og.y) - cen[3 * oi + 1],
128 sm.a[2] + sm.b[2] * (double)(z - G + og.z) - cen[3 * oi + 2]},
129 box);
130 const peclet::core::Vec3<double> r{rp.x * sm.dToInt, rp.y * sm.dToInt, rp.z * sm.dToInt};
131 for (int a = 0; a < 3; ++a) {
132 Kokkos::atomic_add(&Fd(3 * oi + a), dF[a]);
133 Kokkos::atomic_add(&Pd(3 * oi + a), dFp[a]);
134 Kokkos::atomic_add(&Vd(3 * oi + a), dFv[a]);
135 }
136 Kokkos::atomic_add(&Td(3 * oi + 0), r.y * dF[2] - r.z * dF[1]);
137 Kokkos::atomic_add(&Td(3 * oi + 1), r.z * dF[0] - r.x * dF[2]);
138 Kokkos::atomic_add(&Td(3 * oi + 2), r.x * dF[1] - r.y * dF[0]);
139 });
140 space.fence();
141 using HostV = Kokkos::View<double*, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
142 Kokkos::deep_copy(HostV(out.data(), m), Fd);
143 Kokkos::deep_copy(HostV(out.data() + m, m), Td);
144 Kokkos::deep_copy(HostV(out.data() + 2 * m, m), Pd);
145 Kokkos::deep_copy(HostV(out.data() + 3 * m, m), Vd);
146 // ONCE: the conversion covers all four blocks (force, torque, pressure part, viscous part). A
147 // duplicated call here squared the factor -- the identity in cell units, so no cell-unit gate saw
148 // it, and 0.019x the reaction at L = 1 under a physical domain (tests/python/
149 // test_hydro_force_units.py).
150 scaleForceTorque(out, m);
151#ifdef PECLET_FLOW_MPI
152 if (distributed_) {
153 // Instances are REPLICATED, so each rank integrates only the wall cells inside its own
154 // block; the body's total is the sum over ranks.
155 std::vector<double> g(out.size(), 0.0);
156 MPI_Allreduce(out.data(), g.data(), (int)out.size(), MPI_DOUBLE, MPI_SUM, comm_);
157 out.swap(g);
158 }
159#endif
160 return out;
161}
162
163template <class Grid>
164void Solver<Grid>::hydroReactionChecks() {
165 if constexpr (Grid::collocated)
166 throw std::runtime_error("hydro_force_torque_reaction: staggered only (v1)");
167 if (implicitAdv() || porous_ || varRho_ || varProps_ || hasBc_ || ghostProjection_ || hasDrag_ ||
168 fluidOnlyMode_ != 0)
169 throw std::runtime_error(
170 "hydro_force_torque_reaction: implicit advection / porous / variable-properties / "
171 "domain-BC / ghost-projection / drag / star modes put momentum terms in the step that "
172 "this budget does not carry (v2) -- a missing term is a silently mis-attributed force");
173 if (!haveUStar_)
174 throw std::runtime_error(
175 "hydro_force_torque_reaction: call step() first (u* is stashed "
176 "during the step)");
177 if (advectsMomentum() && (!haveAdvRhs_ || advRhs_[0].extent(0) != n_))
178 throw std::runtime_error(
179 "hydro_force_torque_reaction: the advective RHS term was not "
180 "stashed -- set_advection was enabled after the last step()");
181}
182
183template <class Grid>
185 std::vector<double> out((std::size_t)(nInst_ > 0 ? nInst_ : 0) * 6, 0.0);
186 if (!hasScene_ || nInst_ <= 0)
187 return out;
188 hydroReactionChecks();
189 // u* ghosts: refresh with the standard fill (periodic wrap single-rank, halo exchange under
190 // MPI; hasBc_ is refused above so no BC is imposed). The audit's viscous term reads +-1.
191 for (int c = 0; c < 3; ++c)
192 fillVelGhostsTo(uStar_[c], c, 0);
193 const std::size_t m = (std::size_t)nInst_ * 3;
194 Kokkos::View<double*, CCMem> Fd("reactF", m), Td("reactT", m);
195 Kokkos::deep_copy(Fd, 0.0);
196 Kokkos::deep_copy(Td, 0.0);
197 CCExec space;
198 const C3 e = e_, og = og_;
199 const double idt = rho_ / dt_, mu = mu_;
200 const auto q = sceneQ_->view();
201 auto cen = instCenD_;
202 const auto box = q.box;
203 const SceneMap sm = sceneMap();
204 const bool hasFb = hasCellForce_;
205 // §4.4: the momentum row of component c is a force density in the component-c normalisation, so
206 // the total force carries h_c'*V' (exactly 1.0 isotropic -- an identity multiplication).
207 const double kR[3] = {u_.hp[0] * u_.vol, u_.hp[1] * u_.vol, u_.hp[2] * u_.vol};
208 // ... while the v3 wall-torque traction below carries V'/h_b' on its AREA vector (E3).
209 const double kA0 = u_.vol / u_.hp[0], kA1 = u_.vol / u_.hp[1], kA2 = u_.vol / u_.hp[2];
210 for (int c = 0; c < 3; ++c) {
211 CCConst un = CCConst(old_[c]), uc = CCConst(C[c].u), us = CCConst(uStar_[c]),
212 mk = CCConst(C[c].mask);
213 CCConst fb = hasFb ? CCConst(cellForce_[c]) : CCConst();
214 CCConst av = advectsMomentum() ? CCConst(advRhs_[c]) : CCConst();
215 const double fc = f_[c];
216 const auto po = Grid::offset(c);
217 const double offx = po.x, offy = po.y, offz = po.z;
218 const int cc = c;
219 const double kRc = kR[c];
220 Kokkos::parallel_for(
221 "peclet::flow::hydro_reaction",
222 MDRange3<CCExec>(space, {G, G, G}, {e.x - G, e.y - G, e.z - G}),
223 KOKKOS_LAMBDA(int x, int y, int z) {
224 const long st[3] = {1, e.x, (long)e.x * e.y};
225 const long i = (long)x + (long)y * st[1] + (long)z * st[2];
226 if (mk(i) > 0.5)
227 return; // solid staggered point: no fluid momentum here
228 // the body force exactly as buildRhsForced placed it (Grid::atVelocity)
229 double R = idt * (uc(i) - un(i)) - fc -
230 (fb.data() ? Grid::atVelocity(fb, i, st[cc]) : 0.0) -
231 (av.data() ? av(i) : 0.0);
232 for (int a = 0; a < 3; ++a) {
233 const long jp = i + st[a], jm = i - st[a];
234 if (mk(jp) <= 0.5)
235 R -= mu * (us(jp) - us(i));
236 if (mk(jm) <= 0.5)
237 R -= mu * (us(jm) - us(i));
238 }
239 // The scene (owner query, instance centres, min-image box) is in the CALLER's
240 // coordinates; F is an index-unit force, so the lever arm comes back to index units.
241 const peclet::core::Vec3<double> p{sm.a[0] + sm.b[0] * ((double)(x - G + og.x) + offx),
242 sm.a[1] + sm.b[1] * ((double)(y - G + og.y) + offy),
243 sm.a[2] + sm.b[2] * ((double)(z - G + og.z) + offz)};
244 const int oi = q.owner(p);
245 if (oi < 0)
246 return;
247 // force ON the body = minus the wall force on the fluid, times h_c'V' (§4.4)
248 const double F = -R * kRc;
249 Kokkos::atomic_add(&Fd(3 * oi + cc), F);
250 const peclet::core::Vec3<double> rq = peclet::core::geom::minImage(
251 peclet::core::Vec3<double>{p.x - cen(3 * oi + 0), p.y - cen(3 * oi + 1),
252 p.z - cen(3 * oi + 2)},
253 box);
254 const peclet::core::Vec3<double> r{rq.x * sm.dToInt, rq.y * sm.dToInt, rq.z * sm.dToInt};
255 // torque of the scalar force F e_c at lever r: r x (F e_c)
256 if (cc == 0) {
257 Kokkos::atomic_add(&Td(3 * oi + 1), r.z * F);
258 Kokkos::atomic_add(&Td(3 * oi + 2), -r.y * F);
259 } else if (cc == 1) {
260 Kokkos::atomic_add(&Td(3 * oi + 0), -r.z * F);
261 Kokkos::atomic_add(&Td(3 * oi + 2), r.x * F);
262 } else {
263 Kokkos::atomic_add(&Td(3 * oi + 0), r.y * F);
264 Kokkos::atomic_add(&Td(3 * oi + 1), -r.x * F);
265 }
266 });
267 }
268 space.fence();
269 // v3: the transposed-stress wall torque, mu * r x (n dA x Omega) per cut cell. n dA is the
270 // exact aperture wall-area vector with the BODY-outward orientation, (oE-oW, oN-oS, oT-oB)
271 // per the wallAreaProbe convention (sum x*(oE-oW) = +V_solid). Skipped entirely when no
272 // instance moves, so a static run stays bit-identical; a purely TRANSLATING instance has
273 // Omega = 0 and contributes exact zeros. The FORCE is deliberately left alone: the term's
274 // force integral is identically zero over a closed surface, and adding its discrete
275 // counterpart would only inject aperture-level rounding into an exactly-gated identity.
276 if (hasMotion_ && cutcellPressure_) {
277 CCConst oxv = CCConst(ox_), oyv = CCConst(oy_), ozv = CCConst(oz_);
278 auto ang = instAngD_;
279 Kokkos::parallel_for(
280 "peclet::flow::hydro_reaction_torque_transpose",
281 MDRange3<CCExec>(space, {G, G, G}, {e.x - G, e.y - G, e.z - G}),
282 KOKKOS_LAMBDA(int x, int y, int z) {
283 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
284 const long i = (long)x + (long)y * sy + (long)z * sz;
285 const double ax = oxv(i + sx) - oxv(i);
286 const double ay = oyv(i + sy) - oyv(i);
287 const double az = ozv(i + sz) - ozv(i);
288 if (ax == 0.0 && ay == 0.0 && az == 0.0)
289 return; // not a cut cell (a sign test: the positive per-axis factors below cannot
290 // change it)
291 // PHASE 2 (doc/anisotropic_metric.md §4.4, E3 reading 2): (ax, ay, az) is an INDEX
292 // aperture area vector; the physical one in hRef^2 is A_b = a_b V'/h_b' -- the same
293 // A the traction paragraph of §4.4 forms -- so the metric multiplies the AREA
294 // component b, which is the index the cross product with Omega consumes, applied
295 // OUTSIDE the existing expression so V'/h_b' = 1.0 reduces exactly.
296 const double Ax = ax * kA0, Ay = ay * kA1, Az = az * kA2;
297 const peclet::core::Vec3<double> p{sm.a[0] + sm.b[0] * (double)(x - G + og.x),
298 sm.a[1] + sm.b[1] * (double)(y - G + og.y),
299 sm.a[2] + sm.b[2] * (double)(z - G + og.z)};
300 const int oi = q.owner(p);
301 if (oi < 0)
302 return;
303 // The scene's angular velocity is 1/time in the caller's units; the index one is
304 // omega*tRef (v = omega x r holds on both sides of the map).
305 const double wx = ang(3 * oi + 0) * sm.angToInt, wy = ang(3 * oi + 1) * sm.angToInt,
306 wz = ang(3 * oi + 2) * sm.angToInt;
307 if (wx == 0.0 && wy == 0.0 && wz == 0.0)
308 return;
309 // v = (n dA) x Omega -- the missing traction integrated over this cell's wall patch
310 const double vx = Ay * wz - Az * wy;
311 const double vy = Az * wx - Ax * wz;
312 const double vz = Ax * wy - Ay * wx;
313 const peclet::core::Vec3<double> rq = peclet::core::geom::minImage(
314 peclet::core::Vec3<double>{p.x - cen(3 * oi + 0), p.y - cen(3 * oi + 1),
315 p.z - cen(3 * oi + 2)},
316 box);
317 const peclet::core::Vec3<double> r{rq.x * sm.dToInt, rq.y * sm.dToInt, rq.z * sm.dToInt};
318 Kokkos::atomic_add(&Td(3 * oi + 0), mu * (r.y * vz - r.z * vy));
319 Kokkos::atomic_add(&Td(3 * oi + 1), mu * (r.z * vx - r.x * vz));
320 Kokkos::atomic_add(&Td(3 * oi + 2), mu * (r.x * vy - r.y * vx));
321 });
322 space.fence();
323 }
324 // v4: owner-boundary attribution correction (see the doc block). Only meaningful with at
325 // least two instances and the incremental pressure (P_ holds the physical pressure).
326 if (nInst_ > 1 && incremental_ && cutcellPressure_) {
327 fillGhosts(P_);
328 CCConst pf = CCConst(P_);
329 for (int c = 0; c < 3; ++c) {
330 CCConst mk = CCConst(C[c].mask);
331 const long strd = (c == 0) ? 1 : (c == 1) ? e_.x : (long)e_.x * e_.y;
332 const auto po = Grid::offset(c);
333 const double offx = po.x, offy = po.y, offz = po.z;
334 const int cc = c;
335 // PHASE 2 (§4.4, a site the note's list does not name): this correction REMOVES a term that
336 // is already inside `F_a = -sum R_a h_a' V'`, so it must carry exactly the factor that term
337 // carries there. The momentum row's pressure gradient is `w_c (P(i) - P(i-s))` since C1/C2,
338 // and F multiplies by `h_c' V'`, so the combined factor on `pi(i)` is
339 // `w_c h_c' V' = V'/h_c'` -- the physical area of the face, as it must be for a pressure
340 // force. Exactly 1.0 isotropic (1.0/1.0), so this is an identity multiplication.
341 const double kP = u_.vol / u_.hp[c];
342 Kokkos::parallel_for(
343 "peclet::flow::hydro_reaction_owner_flux",
344 MDRange3<CCExec>(space, {G, G, G}, {e.x - G, e.y - G, e.z - G}),
345 KOKKOS_LAMBDA(int x, int y, int z) {
346 const long i = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
347 const long j = i + strd; // the +s neighbour: visit once
348 if (mk(i) > 0.5 || mk(j) > 0.5)
349 return; // wall faces stay in the wall force
350 const peclet::core::Vec3<double> pa{
351 sm.a[0] + sm.b[0] * ((double)(x - G + og.x) + offx),
352 sm.a[1] + sm.b[1] * ((double)(y - G + og.y) + offy),
353 sm.a[2] + sm.b[2] * ((double)(z - G + og.z) + offz)};
354 peclet::core::Vec3<double> pb = pa; // one cell along cc, in scene coordinates
355 (cc == 0 ? pb.x : cc == 1 ? pb.y : pb.z) += sm.b[cc];
356 const int oa = q.owner(pa), ob = q.owner(pb);
357 if (oa == ob || oa < 0 || ob < 0)
358 return;
359 // One-sided staggered gradients: point i reads pi(i) - pi(i-s); point j = i+s
360 // reads pi(j) - pi(i). The cell shared by this owner-boundary face is cell i,
361 // entering a's telescoped sum(grad pi) with +pi(i) (via point i) and b's with
362 // -pi(i) (via point j). F_attr = -sum R carries +sum(grad pi), so a's attribution
363 // holds +pi(i) and b's -pi(i) from this face: a pure transfer across the owner
364 // partition that belongs to NEITHER wall. Remove it from both, symmetrically --
365 // the pairwise cancellation is what keeps the total exact.
366 const double flux = pf(i) * kP;
367 Kokkos::atomic_add(&Fd(3 * oa + cc), -flux);
368 Kokkos::atomic_add(&Fd(3 * ob + cc), +flux);
369 const peclet::core::Vec3<double> raq = peclet::core::geom::minImage(
370 peclet::core::Vec3<double>{pa.x - cen(3 * oa + 0), pa.y - cen(3 * oa + 1),
371 pa.z - cen(3 * oa + 2)},
372 box);
373 const peclet::core::Vec3<double> rbq = peclet::core::geom::minImage(
374 peclet::core::Vec3<double>{pa.x - cen(3 * ob + 0), pa.y - cen(3 * ob + 1),
375 pa.z - cen(3 * ob + 2)},
376 box);
377 const peclet::core::Vec3<double> ra{raq.x * sm.dToInt, raq.y * sm.dToInt,
378 raq.z * sm.dToInt};
379 const peclet::core::Vec3<double> rb{rbq.x * sm.dToInt, rbq.y * sm.dToInt,
380 rbq.z * sm.dToInt};
381 if (cc == 0) {
382 Kokkos::atomic_add(&Td(3 * oa + 1), ra.z * -flux);
383 Kokkos::atomic_add(&Td(3 * oa + 2), -ra.y * -flux);
384 Kokkos::atomic_add(&Td(3 * ob + 1), rb.z * +flux);
385 Kokkos::atomic_add(&Td(3 * ob + 2), -rb.y * +flux);
386 } else if (cc == 1) {
387 Kokkos::atomic_add(&Td(3 * oa + 0), -ra.z * -flux);
388 Kokkos::atomic_add(&Td(3 * oa + 2), ra.x * -flux);
389 Kokkos::atomic_add(&Td(3 * ob + 0), -rb.z * +flux);
390 Kokkos::atomic_add(&Td(3 * ob + 2), rb.x * +flux);
391 } else {
392 Kokkos::atomic_add(&Td(3 * oa + 0), ra.y * -flux);
393 Kokkos::atomic_add(&Td(3 * oa + 1), -ra.x * -flux);
394 Kokkos::atomic_add(&Td(3 * ob + 0), rb.y * +flux);
395 Kokkos::atomic_add(&Td(3 * ob + 1), -rb.x * +flux);
396 }
397 });
398 }
399 space.fence();
400 }
401 using HostV = Kokkos::View<double*, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
402 Kokkos::deep_copy(HostV(out.data(), m), Fd);
403 Kokkos::deep_copy(HostV(out.data() + m, m), Td);
404 scaleForceTorque(out, m);
405#ifdef PECLET_FLOW_MPI
406 if (distributed_) {
407 std::vector<double> g(out.size(), 0.0);
408 MPI_Allreduce(out.data(), g.data(), (int)out.size(), MPI_DOUBLE, MPI_SUM, comm_);
409 out.swap(g);
410 }
411#endif
412 return out;
413}
414
415template <class Grid>
416std::array<long, 3> Solver<Grid>::fluidMomentumCells() {
417 std::array<long, 3> out{0, 0, 0};
418 CCExec space;
419 const C3 e = e_;
420 for (int c = 0; c < 3; ++c) {
421 CCConst mk = CCConst(C[c].mask);
422 long n = 0;
423 Kokkos::parallel_reduce(
424 "peclet::flow::fluid_cells",
425 MDRange3<CCExec>(space, {G, G, G}, {e.x - G, e.y - G, e.z - G}),
426 KOKKOS_LAMBDA(int x, int y, int z, long& acc) {
427 const long i = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
428 if (mk(i) <= 0.5)
429 ++acc;
430 },
431 n);
432 out[(std::size_t)c] = n;
433 }
434 space.fence();
435#ifdef PECLET_FLOW_MPI
436 if (distributed_) {
437 std::array<long, 3> g{0, 0, 0};
438 MPI_Allreduce(out.data(), g.data(), 3, MPI_LONG, MPI_SUM, comm_);
439 out = g;
440 }
441#endif
442 return out;
443}
444
445template <class Grid>
447 std::vector<double> out(6, 0.0);
448 CCExec space;
449 const C3 e = e_;
450 const double idt = rho_ / dt_;
451 const bool haveA = advectsMomentum() && haveAdvRhs_ && advRhs_[0].extent(0) == n_;
452 for (int c = 0; c < 3; ++c) {
453 CCConst un = CCConst(old_[c]), uc = CCConst(C[c].u), mk = CCConst(C[c].mask);
454 CCConst av = haveA ? CCConst(advRhs_[c]) : CCConst();
455 double su = 0.0, sa = 0.0;
456 Kokkos::parallel_reduce(
457 "peclet::flow::budget_terms",
458 MDRange3<CCExec>(space, {G, G, G}, {e.x - G, e.y - G, e.z - G}),
459 KOKKOS_LAMBDA(int x, int y, int z, double& au, double& aa) {
460 const long i = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
461 if (mk(i) > 0.5)
462 return;
463 au += idt * (uc(i) - un(i));
464 aa += av.data() ? av(i) : 0.0;
465 },
466 su, sa);
467 out[(std::size_t)c] = su;
468 out[(std::size_t)c + 3] = sa;
469 }
470 space.fence();
471#ifdef PECLET_FLOW_MPI
472 if (distributed_) {
473 std::vector<double> g(6, 0.0);
474 MPI_Allreduce(out.data(), g.data(), 6, MPI_DOUBLE, MPI_SUM, comm_);
475 out.swap(g);
476 }
477#endif
478 return out;
479}
480
481template <class Grid>
482std::array<double, 3> Solver<Grid>::wallAreaProbe() {
483 std::array<double, 3> out{0, 0, 0};
484 if (!hasScene_)
485 return out;
486 CCExec space;
487 const C3 e = e_, og = og_;
488 CCConst oxv = CCConst(ox_), oyv = CCConst(oy_), ozv = CCConst(oz_);
489 double sx = 0, sy = 0, sz = 0;
490 Kokkos::parallel_reduce(
491 "peclet::flow::wall_area_probe",
492 MDRange3<CCExec>(space, {G, G, G}, {e.x - G, e.y - G, e.z - G}),
493 KOKKOS_LAMBDA(int x, int y, int z, double& ax, double& ay, double& az) {
494 const long st[3] = {1, e.x, (long)e.x * e.y};
495 const long i = (long)x + (long)y * st[1] + (long)z * st[2];
496 ax += (double)(x - G + og.x) * -(oxv(i + st[0]) - oxv(i));
497 ay += (double)(y - G + og.y) * -(oyv(i + st[1]) - oyv(i));
498 az += (double)(z - G + og.z) * -(ozv(i + st[2]) - ozv(i));
499 },
500 sx, sy, sz);
501 space.fence();
502 out = {sx, sy, sz};
503#ifdef PECLET_FLOW_MPI
504 if (distributed_) {
505 std::array<double, 3> g{0, 0, 0};
506 MPI_Allreduce(out.data(), g.data(), 3, MPI_DOUBLE, MPI_SUM, comm_);
507 out = g;
508 }
509#endif
510 return out;
511}
512
513template <class Grid>
515 if (!hasScene_ || !hasMotion_ || uwCell_[0].extent(0) != n_)
516 return 0.0;
517 CCExec space;
518 const C3 e = e_;
519 CCConst oxv = CCConst(ox_), oyv = CCConst(oy_), ozv = CCConst(oz_);
520 CCConst wx = CCConst(uwCell_[0]), wy = CCConst(uwCell_[1]), wz = CCConst(uwCell_[2]);
521 double sum = 0.0;
522 Kokkos::parallel_reduce(
523 "peclet::flow::wall_flux_sum",
524 MDRange3<CCExec>(space, {G, G, G}, {e.x - G, e.y - G, e.z - G}),
525 KOKKOS_LAMBDA(int x, int y, int z, double& acc) {
526 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
527 const long i = (long)x + (long)y * sy + (long)z * sz;
528 acc += wx(i) * -(oxv(i + sx) - oxv(i)) + wy(i) * -(oyv(i + sy) - oyv(i)) +
529 wz(i) * -(ozv(i + sz) - ozv(i));
530 },
531 sum);
532#ifdef PECLET_FLOW_MPI
533 if (distributed_) {
534 double g = 0;
535 MPI_Allreduce(&sum, &g, 1, MPI_DOUBLE, MPI_SUM, comm_);
536 return g;
537 }
538#endif
539 return sum;
540}
541
542template <class Grid>
544 return uBc_[c].extent(0) == n_ ? CCConst(uBc_[c]) : CCConst();
545}
546
547} // namespace peclet::flow
548
549#endif // PECLET_FLOW_FLOW_IBM_HYDRO_HPP
double wallFluxImbalance()
Net wall flux this rank injects, sum over inner cells of u_w .
bool advectsMomentum() const
void fillGhosts(CCField f)
std::array< long, 3 > fluidMomentumCells()
The number of unmasked (fluid) staggered momentum cells per component – the exact discrete datum the ...
void scaleForceTorque(std::vector< double > &out, std::size_t m) const
Hydrodynamic force and torque on each scene instance (Layer 4 rung 2) – the resolved CFD-DEM feedback...
std::array< double, 3 > wallAreaProbe()
A_wall EXACTNESS PROBE (diagnostic for the Layer-4 force integral).
std::vector< double > hydroForceTorque()
PHASE 2 (anisotropic cells), doc/anisotropic_metric.md §4.4 — ADMITTED since commit C4.
static constexpr int G
Definition flow_ibm.hpp:82
SceneMap sceneMap() const
std::vector< double > reactionBudgetTerms()
R0 DECOMPOSITION PROBE.
void fillVelGhostsTo(CCField f, int comp, int fold, bool doOutflow=true, bool faceEdges=false)
CCConst wallVelView(int c) const
std::vector< double > hydroForceTorqueReaction()
Hydrodynamic force and torque per instance from the DISCRETE REACTION (route (b) of the design note's...
Kokkos::DefaultExecutionSpace CCExec
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 3, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange3
3-D range, x (the first index) fastest (both the tile order and the order within a tile).
Definition policy.hpp:36
Kokkos::View< const double *, CCMem > CCConst
The map between the solver's INDEX coordinates and the coordinate system the analytic scene is writte...
Definition flow_ibm.hpp:243
static constexpr double F
static constexpr double U
static constexpr double R