26#include <Kokkos_Core.hpp>
38 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
43namespace cs = peclet::core::scheme;
54 const int t = i < 0 ? bcLo : bcHi;
56 return ((i % n) + n) % n;
58 return i < 0 ? -1 - i : 2 * n - 1 - i;
59 return i < 0 ? 0 : n - 1;
64void checkGhosts(
const char* label,
const int bc[6]) {
65 const int nx = 12,
ny = 10, nz = 8;
66 auto f = [&](
int x,
int y,
int z) {
return 1000.0 *
x + 37.0 *
y + 1.25 *
z - 300.0; };
67 std::vector<double>
sdf((std::size_t)nx * ny * nz);
68 for (
int z = 0;
z < nz; ++
z)
69 for (
int y = 0;
y <
ny; ++
y)
70 for (
int x = 0;
x < nx; ++
x)
71 sdf[(std::size_t)
x + (std::size_t)y * nx + (std::size_t)
z * nx *
ny] =
f(x, y, z);
73 s.setAdvection(
false);
74 for (
int face = 0; face < 6; ++face)
76 s.setDomainBc(face, bc[face], 0.0, 0.0, 0.0);
77 s.setSolid(sdf,
false);
78 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
s.fieldView(
"sdf"));
79 const int ex = nx + 2 *
G, ey =
ny + 2 *
G;
80 const int n[3] = {nx,
ny, nz};
81 long bad = 0, checked = 0;
82 for (
int a = 0;
a < 3; ++
a)
84 for (
int layer = 1; layer <=
G; ++layer) {
85 const int ia =
side ?
n[
a] - 1 + layer : -layer;
86 const int b = (
a + 1) % 3, c = (a + 2) % 3;
87 for (
int jb = 0; jb <
n[
b]; ++jb)
94 h((
long)(g3[0] +
G) + (
long)(g3[1] +
G) * ex + (
long)(g3[2] +
G) * (
long)ex * ey);
95 const int ex3[3] = {
expectIndex(g3[0], nx, bc[0], bc[1]),
99 if (got !=
f(ex3[0], ex3[1], ex3[2]))
103 std::printf(
" ghosts %-28s: %ld / %ld ghost samples (layers 1-2) stale\n", label, bad, checked);
111 double solidVol = 0.0, fluidVol = 0.0, area = 0.0;
112 long cutCells = 0, notSingle = 0, lowRho = 0, sealed = 0, facets = 0, r0 = 0, r2 = 0;
113 double minRho = 1.0, maxSnapRes = 0.0;
114 double sealedMaxKappa = 0.0, sealedVolFrac = 0.0;
115 long kappaMismatch = 0, countMismatch = 0;
116 double maxKappaDiff = 0.0;
120constexpr bool kDeviceBackend =
121 !std::is_same_v<Kokkos::DefaultExecutionSpace::memory_space, Kokkos::HostSpace>;
123Measure measure(
IbmSolver& s,
const int n[3],
const double hp[3]) {
125 const auto& rec =
s.scalarCutGeometry();
126 const auto c =
s.scalarCutCensus();
127 const int ex =
n[0] + 2 *
G, ey =
n[1] + 2 *
G;
128 const long sy = ex,
sz = (long)ex * ey;
129 auto sdf = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
s.fieldView(
"sdf"));
130 auto kap = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), rec.kappa);
131 auto sax = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), rec.sax);
132 auto say = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), rec.say);
133 auto saz = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), rec.saz);
134 auto alpha = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), rec.fac.alpha);
135 auto rung = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), rec.fac.rungF);
136 const double vol = hp[0] * hp[1] * hp[2];
137 const double maxA = std::fmax(hp[1] * hp[2], std::fmax(hp[0] * hp[2], hp[0] * hp[1]));
139 for (
int z =
G;
z <
n[2] +
G; ++
z)
140 for (
int y =
G;
y <
n[1] +
G; ++
y)
141 for (
int x =
G;
x <
n[0] +
G; ++
x) {
142 const long i =
x +
y * sy +
z *
sz;
143 double corner[8], face[6], centre;
145 const cs::CutCellGeometry
g = cs::cutCellGeometryFanTet(corner, face, centre, hp);
146 m.maxKappaDiff = std::fmax(
m.maxKappaDiff, std::fabs(
g.kappa - kap(i)));
147 if (kDeviceBackend ? !(std::fabs(
g.kappa - kap(i)) <= 1e-12)
148 : std::memcmp(&
g.kappa, &kap(i),
sizeof(
double)) != 0)
150 m.fluidVol +=
g.kappa *
vol;
151 m.solidVol += (1.0 -
g.kappa) * vol;
154 double area[2][3], cen[2][3];
158 const double rho = std::sqrt(
g.areaPL[0] *
g.areaPL[0] +
g.areaPL[1] *
g.areaPL[1] +
159 g.areaPL[2] *
g.areaPL[2]) /
161 m.minRho = std::fmin(
m.minRho, rho);
162 if (
g.kind != cs::CutCellKind::single)
167 cs::facetAreaVector(ap, hp, aSnap);
168 for (
int d = 0;
d < 3; ++
d)
169 m.maxSnapRes = std::fmax(
m.maxSnapRes, std::fabs(aSnap[d] -
g.areaPL[d]) / maxA);
172 for (
long f = 0;
f < rec.fac.n; ++
f) {
174 if (rung(f) == cs::kProbeR0)
176 if (rung(f) == cs::kProbeR2)
179 m.facets = rec.fac.n;
180 m.sealed =
c.numSealed;
181 m.sealedVolFrac =
c.sealedVolume /
m.fluidVol;
183 auto unk = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), rec.unknown);
184 for (
int z =
G;
z <
n[2] +
G; ++
z)
185 for (
int y =
G;
y <
n[1] +
G; ++
y)
186 for (
int x =
G;
x <
n[0] +
G; ++
x) {
187 const long i =
x +
y * sy +
z *
sz;
188 if (kap(i) > 0.0 && unk(i) < 0.5)
189 m.sealedMaxKappa = std::fmax(
m.sealedMaxKappa, kap(i));
192 if (nfacHost != rec.fac.n)
194 CHECK(
c.numFacets == rec.fac.n);
195 CHECK(
c.rungs[0] +
c.rungs[1] +
c.rungs[2] +
c.rungs[3] == rec.fac.n);
201Measure sphereCase(
const int n[3],
const double hp[3],
double R,
const double off[3]) {
202 const std::array<double, 3> extent{
n[0] * hp[0],
n[1] * hp[1],
n[2] * hp[2]};
203 IbmSolver s(n[0], n[1], n[2], extent, {0.0, 0.0, 0.0}, {(long)n[0], (
long)
n[1], (long)n[2]});
204 s.setAdvection(
false);
205 std::vector<double>
sdf((std::size_t)n[0] * n[1] * n[2]);
206 const double c[3] = {0.5 * extent[0] +
off[0], 0.5 * extent[1] +
off[1],
207 0.5 * extent[2] +
off[2]};
208 for (
int z = 0;
z <
n[2]; ++
z)
209 for (
int y = 0;
y <
n[1]; ++
y)
210 for (
int x = 0;
x <
n[0]; ++
x) {
211 const double dx = (
x + 0.5) * hp[0] - c[0], dy = (y + 0.5) * hp[1] -
c[1],
212 dz = (
z + 0.5) * hp[2] - c[2];
213 sdf[(std::size_t)x + (std::size_t)
y *
n[0] + (std::size_t)z * n[0] * n[1]] =
214 std::sqrt(dx * dx + dy * dy + dz * dz) -
R;
216 s.setSolid(sdf,
false);
217 return measure(s, n, hp);
224void sphereSet(
const char* label,
const double hp[3],
bool strictR0) {
225 const double h = std::fmax(hp[0], std::fmax(hp[1], hp[2]));
226 const double ratios[3] = {8.0, 16.0, 32.0};
227 std::mt19937_64
rng(4242);
228 std::uniform_real_distribution<double>
u(-0.5, 0.5);
229 double rmsV[3], rmsA[3];
230 for (
int r = 0;
r < 3; ++
r) {
233 for (
int a = 0;
a < 3; ++
a)
234 n[a] = (
int)std::lround((2.0 *
R + 8.0 * h) / hp[a]);
235 const double Vex = 4.0 / 3.0 * M_PI *
R *
R *
R, Aex = 4.0 * M_PI *
R *
R;
236 const double bound2 = (
h /
R) * (h /
R);
237 double sV = 0.0,
sA = 0.0;
238 for (
int o = 0;
o < 3; ++
o) {
239 const double off[3] = {
u(rng) *
h,
u(rng) *
h,
u(rng) *
h};
240 const Measure
m = sphereCase(n, hp,
R, off);
241 const double eV =
m.solidVol / Vex - 1.0,
eA =
m.area / Aex - 1.0;
245 " %s R/h=%2.0f off %d: eV %+.3e (bound %.2e) eA %+.3e (bound %.2e) cut %ld "
246 "facets %ld R0 %ld R2 %ld min rho %.4f not-single %ld sealed %ld (max kappa %.1e, "
247 "vol/fluid %.1e) (f) %.2e\n",
248 label, ratios[r], o, eV, 2.0 * bound2, eA, 3.0 * bound2,
m.cutCells,
m.facets,
m.r0,
m.r2,
249 m.minRho,
m.notSingle,
m.sealed,
m.sealedMaxKappa,
m.sealedVolFrac,
m.maxSnapRes);
250 CHECK(std::fabs(eV) <= 2.0 * bound2);
251 CHECK(std::fabs(eA) <= 3.0 * bound2);
252 CHECK(
m.notSingle == 0 &&
m.lowRho == 0);
255 CHECK(
m.sealedVolFrac <= 1e-6);
260 CHECK((
double)
m.r0 >= 0.999 * (
double)
m.facets &&
m.r2 == 0);
261 CHECK(
m.maxSnapRes <= 2e-3);
262 if (
m.kappaMismatch != 0 ||
m.countMismatch != 0)
263 std::printf(
" record vs host: %ld kappa mismatches (max diff %.2e), facet count %s\n",
264 m.kappaMismatch,
m.maxKappaDiff,
m.countMismatch ?
"DIFFERS" :
"equal");
265 CHECK(
m.kappaMismatch == 0 &&
m.countMismatch == 0);
267 rmsV[
r] = std::sqrt(sV / 3.0);
268 rmsA[
r] = std::sqrt(sA / 3.0);
270 for (
int r = 0;
r + 1 < 3; ++
r) {
271 const double oV = std::log2(rmsV[r] / rmsV[r + 1]),
oA = std::log2(rmsA[r] / rmsA[r + 1]);
272 std::printf(
" %s order %2.0f->%2.0f: volume %.3f area %.3f\n", label, ratios[r],
273 ratios[r + 1], oV, oA);
281 std::mt19937_64
rng(77);
282 std::uniform_real_distribution<double>
u(-0.5, 0.5);
283 for (
double R : {16.0, 32.0, 64.0}) {
284 const int nxy = (
int)std::lround(2.0 *
R + 8.0);
285 const int n[3] = {nxy, nxy, 4};
286 const double hp[3] = {1.0, 1.0, 1.0};
287 for (
int o = 0;
o < 3; ++
o) {
288 const double cx = 0.5 * nxy +
u(rng), cy = 0.5 * nxy +
u(rng);
289 IbmSolver s(n[0], n[1], n[2], {(double)n[0], (
double)
n[1], (double)n[2]}, {0.0, 0.0, 0.0},
290 {(long)n[0], (
long)
n[1], (long)n[2]});
291 s.setAdvection(
false);
292 std::vector<double>
sdf((std::size_t)n[0] * n[1] * n[2]);
293 for (
int z = 0;
z <
n[2]; ++
z)
294 for (
int y = 0;
y <
n[1]; ++
y)
295 for (
int x = 0;
x <
n[0]; ++
x) {
296 const double dx =
x + 0.5 - cx,
dy =
y + 0.5 - cy;
297 sdf[(std::size_t)x + (std::size_t)
y *
n[0] + (std::size_t)z * n[0] * n[1]] =
298 R - std::sqrt(dx * dx + dy * dy);
300 s.setSolid(sdf,
false);
301 const Measure
m = measure(s, n, hp);
302 const double eV =
m.fluidVol / (M_PI *
R *
R *
n[2]) - 1.0;
304 " pipe R/h=%2.0f off %d: eV %+.3e facets %ld R0 %ld min rho %.4f sealed %ld "
306 R, o, eV,
m.facets,
m.r0,
m.minRho,
m.sealed,
m.maxSnapRes);
307 CHECK(
m.r0 ==
m.facets &&
m.facets > 0);
308 CHECK(
m.sealed == 0 &&
m.notSingle == 0);
309 CHECK(
m.maxSnapRes <= 2e-3);
310 if (
m.kappaMismatch != 0 ||
m.countMismatch != 0)
311 std::printf(
" record vs host: %ld kappa mismatches (max diff %.2e), facet count %s\n",
312 m.kappaMismatch,
m.maxKappaDiff,
m.countMismatch ?
"DIFFERS" :
"equal");
313 CHECK(
m.kappaMismatch == 0 &&
m.countMismatch == 0);
320int main(
int argc,
char** argv) {
321 Kokkos::ScopeGuard guard(argc, argv);
323 const int periodic[6] = {0, 0, 0, 0, 0, 0};
324 const int walls[6] = {1, 1, 4, 4, 0, 0};
325 const int mixed[6] = {0, 0, 1, 4, 4, 1};
326 checkGhosts(
"periodic wrap", periodic);
327 checkGhosts(
"x wall + y slip", walls);
328 checkGhosts(
"y wall/slip + z slip/wall", mixed);
330 const double iso[3] = {1.0, 1.0, 1.0}, aniso[3] = {1.0, 1.0, 2.0};
331 sphereSet(
"iso ", iso,
true);
332 sphereSet(
"aniso", aniso,
false);
336 std::fprintf(stderr,
"%d failure(s)\n",
failures);
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
void cellSamples(const V &s, long i, long sy, long sz, double corner[8], double face[6], double ¢re)
The 15 PL samples of the cell at extended linear index i (corner index bx + 2 by + 4 bz,...
void cellSnapped(const V &sax, const V &say, const V &saz, long i, long sy, long sz, double ap[6])
The snapped apertures of cell i in face order -x,+x,-y,+y,-z,+z.
int cellFacets(const cs::CutCellGeometry &g, const double ap[6], const double h[3], double area[2][3], double cen[2][3])
The operator facets of one cell (§2.3) from its PL record and snapped apertures: returns the count (0...
Solver< Staggered > IbmSolver
peclet::flow::IbmSolver IbmSolver
static constexpr double R
static int expectIndex(int i, int n, int bcLo, int bcHi)