6#include <Kokkos_Core.hpp>
16 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
21std::vector<double> allFluid(
int nx,
int ny,
int nz) {
22 return std::vector<double>((std::size_t)nx * ny * nz, 10.0);
26double measureEigen(
int N) {
27 const double D = 1.0,
dt = 0.5;
30 s.setPressureGeometry(allFluid(
N, 4, 4));
31 s.addScalar(
"c", D, 0, 600);
32 const double k = 2.0 * M_PI /
N;
33 std::vector<double> c0((std::size_t)
N * 4 * 4);
34 for (
int z = 0; z < 4; ++z)
35 for (
int y = 0; y < 4; ++y)
36 for (
int x = 0; x <
N; ++x)
37 c0[(std::size_t)x + (std::size_t)y *
N + (std::size_t)z *
N * 4] = std::cos(k * x);
40 auto c1 = s.getField(
"c");
41 const double peak0 = c0[0], peak1 = c1[0];
42 return (peak0 / peak1 - 1.0) / (
dt * D);
48 std::fabs(measureEigen(16) - std::pow(2 * M_PI / 16, 2)) / std::pow(2 * M_PI / 16, 2);
50 std::fabs(measureEigen(32) - std::pow(2 * M_PI / 32, 2)) / std::pow(2 * M_PI / 32, 2);
51 const double order = std::log(e16 / e32) / std::log(2.0);
52 std::printf(
"diffusion rel-errs %.3e %.3e order %.2f\n", e16, e32, order);
57 const double U = 0.4,
dt = 0.5;
60 s.setPressureGeometry(allFluid(
N, 4, 4));
61 s.addScalar(
"c", 0.0, 1, 1);
62 std::vector<double> u((std::size_t)
N * 4 * 4, U), zero((std::size_t)
N * 4 * 4, 0.0);
63 s.uploadVelocity(u, zero, zero);
64 std::vector<double> c0((std::size_t)
N * 4 * 4);
66 for (
int z = 0; z < 4; ++z)
67 for (
int y = 0; y < 4; ++y)
68 for (
int x = 0; x <
N; ++x) {
69 const double b = std::exp(-std::pow(x -
N / 2.0, 2) / (2 * std::pow(
N / 12.0, 2)));
70 c0[(std::size_t)x + (std::size_t)y *
N + (std::size_t)z *
N * 4] = b;
74 for (
int it = 0; it < 20; ++it)
76 auto c = s.getField(
"c");
77 double tot = 0.0,
mx = -1e30;
80 mx = std::fmax(mx, v);
82 const double cons = std::fabs(tot - tot0) / tot0;
83 std::printf(
"advection conservation rel %.2e max %.4f (init 1.0)\n", cons, mx);
85 CHECK(mx < 1.0 + 1e-7);
89int main(
int argc,
char** argv) {
90 Kokkos::initialize(argc, argv);
97 std::fprintf(stderr,
"%d failure(s)\n", failures);
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
int main(int argc, char **argv)