flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_scalar_transport.cpp
Go to the documentation of this file.
1// Scalar transport on the flow Solver: openness-weighted implicit diffusion (2nd-order operator via
2// the discrete-eigenvalue convergence) + explicit conservative advection (machine-precision
3// conservation on a periodic all-fluid box).
4#include <cmath>
5#include <cstdio>
6#include <Kokkos_Core.hpp>
7#include <vector>
8
9#include "flow_ibm.hpp"
10
11namespace {
12int failures = 0;
13#define CHECK(cond) \
14 do { \
15 if (!(cond)) { \
16 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
17 ++failures; \
18 } \
19 } while (0)
20
21std::vector<double> allFluid(int nx, int ny, int nz) {
22 return std::vector<double>((std::size_t)nx * ny * nz, 10.0);
23}
24
25// Discrete Laplacian eigenvalue from one backward-Euler diffusion step of a cosine mode.
26double measureEigen(int N) {
27 const double D = 1.0, dt = 0.5;
29 s.setAdvection(false);
30 s.setDt(dt);
31 s.setPressureGeometry(allFluid(N, 4, 4));
32 s.addScalar("c", D, 0, 600);
33 const double k = 2.0 * M_PI / N;
34 std::vector<double> c0((std::size_t)N * 4 * 4);
35 for (int z = 0; z < 4; ++z)
36 for (int y = 0; y < 4; ++y)
37 for (int x = 0; x < N; ++x)
38 c0[(std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * 4] = std::cos(k * x);
39 s.setField("c", c0);
40 s.advanceScalars(); // velocity zero -> pure diffusion
41 auto c1 = s.getField("c");
42 const double peak0 = c0[0], peak1 = c1[0]; // x=y=z=0
43 return (peak0 / peak1 - 1.0) / (dt * D); // mu_h (eigenmode: c1 = c0/(1+dt*D*mu_h))
44}
45
46void run() {
47 // 2nd-order diffusion operator: |mu_h - k^2| ~ O(k^4) => relative error halves^2 per refinement.
48 const double e16 =
49 std::fabs(measureEigen(16) - std::pow(2 * M_PI / 16, 2)) / std::pow(2 * M_PI / 16, 2);
50 const double e32 =
51 std::fabs(measureEigen(32) - std::pow(2 * M_PI / 32, 2)) / std::pow(2 * M_PI / 32, 2);
52 const double order = std::log(e16 / e32) / std::log(2.0);
53 std::printf("diffusion rel-errs %.3e %.3e order %.2f\n", e16, e32, order);
54 CHECK(order > 1.9);
55
56 // Uniform-velocity advection conserves the scalar to machine precision (periodic, openness 1).
57 const int N = 32;
58 const double U = 0.4, dt = 0.5;
60 s.setAdvection(false);
61 s.setDt(dt);
62 s.setPressureGeometry(allFluid(N, 4, 4));
63 s.addScalar("c", 0.0, 1, 1); // pure advection, Koren
64 std::vector<double> u((std::size_t)N * 4 * 4, U), zero((std::size_t)N * 4 * 4, 0.0);
65 s.uploadVelocity(u, zero, zero);
66 std::vector<double> c0((std::size_t)N * 4 * 4);
67 double tot0 = 0.0;
68 for (int z = 0; z < 4; ++z)
69 for (int y = 0; y < 4; ++y)
70 for (int x = 0; x < N; ++x) {
71 const double b = std::exp(-std::pow(x - N / 2.0, 2) / (2 * std::pow(N / 12.0, 2)));
72 c0[(std::size_t)x + (std::size_t)y * N + (std::size_t)z * N * 4] = b;
73 tot0 += b;
74 }
75 s.setField("c", c0);
76 for (int it = 0; it < 20; ++it)
77 s.advanceScalars();
78 auto c = s.getField("c");
79 double tot = 0.0, mx = -1e30;
80 for (double v : c) {
81 tot += v;
82 mx = std::fmax(mx, v);
83 }
84 const double cons = std::fabs(tot - tot0) / tot0;
85 std::printf("advection conservation rel %.2e max %.4f (init 1.0)\n", cons, mx);
86 CHECK(cons < 1e-10);
87 CHECK(mx < 1.0 + 1e-7); // no overshoot
88}
89} // namespace
90
91int main(int argc, char** argv) {
92 Kokkos::initialize(argc, argv);
93 run();
94 Kokkos::finalize();
95 if (failures == 0) {
96 std::printf("OK\n");
97 return 0;
98 }
99 std::fprintf(stderr, "%d failure(s)\n", failures);
100 return 1;
101}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
y
Definition cmp.py:24
void zero(const ScalarVec &y)
Solver< Staggered > IbmSolver
static constexpr int N
static int run(int bc)
static constexpr double U
#define CHECK(cond)