8#include <Kokkos_Core.hpp>
18 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
23double maxAbs(
const std::vector<double>& v) {
26 m = std::fmax(m, std::fabs(x));
31 const int N = 8, NZ = 24;
37 s.setDomainBc(4, 1, 0, 0, 0);
38 s.setDomainBc(5, 1, 0, 0, 0);
39 s.setPressureGeometry(std::vector<double>((std::size_t)
N *
N * NZ, 10.0));
40 std::vector<double> rho((std::size_t)
N *
N * NZ);
41 for (
int z = 0; z < NZ; ++z)
42 for (
int y = 0; y <
N; ++y)
43 for (
int x = 0; x <
N; ++x)
44 rho[(std::size_t)x + (std::size_t)y *
N + (std::size_t)z *
N *
N] =
45 (z < NZ / 2) ? ratio : 1.0;
47 s.setField(
"rho", rho);
48 s.setDensityMode(
true);
51 for (
int it = 0; it < 100; ++it) {
53 last = std::fmax(maxAbs(s.getVelocity(0)),
54 std::fmax(maxAbs(s.getVelocity(1)), maxAbs(s.getVelocity(2))));
55 CHECK(!std::isnan(last));
60 auto p = s.getPressure();
62 const int xc =
N / 2, yc =
N / 2;
63 for (
int z = 1; z < NZ; ++z) {
64 const double dp = p[(std::size_t)xc + (std::size_t)yc *
N + (std::size_t)z *
N *
N] -
65 p[(std::size_t)xc + (std::size_t)yc *
N + (std::size_t)(z - 1) *
N *
N];
66 const double rf = 0.5 * (((z < NZ / 2) ? ratio : 1.0) + ((z - 1 < NZ / 2) ? ratio : 1.0));
67 perr = std::fmax(perr, std::fabs(dp + g * rf) / (g * ratio));
69 std::printf(
"hydrostatic ratio %g: final max|u| %.2e P-grad rel-err %.2e\n", ratio, last, perr);
74void uniformReduction() {
76 const int N = 12, NZ = 6;
77 std::vector<std::vector<double>> uu, pp;
78 for (
int var = 0; var < 2; ++var) {
83 s.setBodyForce(1e-3, 0, 0);
84 std::vector<double>
sdf((std::size_t)
N *
N * NZ);
85 for (
int z = 0; z < NZ; ++z)
86 for (
int y = 0; y <
N; ++y)
87 for (
int x = 0; x <
N; ++x)
88 sdf[(std::size_t)x + (std::size_t)y *
N + (std::size_t)z *
N *
N] =
89 std::sqrt(std::pow(x -
N / 2.0, 2) + std::pow(y -
N / 2.0, 2)) -
N / 5.0;
90 s.setSolid(sdf,
true);
92 s.setDensityMode(
true);
93 for (
int it = 0; it < 40; ++it)
95 uu.push_back(s.getVelocity(0));
96 pp.push_back(s.getPressure());
98 double du = 0, dp = 0, us = 0;
99 for (std::size_t i = 0; i < uu[0].size(); ++i) {
100 du = std::fmax(du, std::fabs(uu[0][i] - uu[1][i]));
101 dp = std::fmax(dp, std::fabs(pp[0][i] - pp[1][i]));
102 us = std::fmax(us, std::fabs(uu[0][i]));
104 std::printf(
"uniform-rho reduction: rel du %.2e dp %.2e\n", du / us, dp);
105 CHECK(du / us < 1e-6);
109int main(
int argc,
char** argv) {
110 Kokkos::initialize(argc, argv);
119 std::fprintf(stderr,
"%d failure(s)\n", failures);
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
hydrostatic(ratio, mu=0.0, steps=100, g=0.1, dt=1.0, N=8, NZ=24)
int main(int argc, char **argv)