flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_field_registry.cpp
Go to the documentation of this file.
1// Field registry on the flow Solver — the named directory of cell fields (multiphysics container).
2// Verifies the built-in fields are adopted, a user field allocates + round-trips through the inner
3// region (scatterInner/gatherInner), and fieldView aliases the live buffer.
4#include <cstdio>
5#include <Kokkos_Core.hpp>
6#include <vector>
7
8#include "flow_ibm.hpp"
9
10namespace {
11int failures = 0;
12#define CHECK(cond) \
13 do { \
14 if (!(cond)) { \
15 std::fprintf(stderr, "CHECK failed: %s\n at %s:%d\n", #cond, __FILE__, __LINE__); \
16 ++failures; \
17 } \
18 } while (0)
19
20void run() {
22 const int nx = 6, ny = 5, nz = 4;
23 IbmSolver s(nx, ny, nz);
24 s.setAdvection(false);
25
26 // Built-in members are registered.
27 CHECK(s.hasField("u") && s.hasField("v") && s.hasField("w"));
28 CHECK(s.hasField("p") && s.hasField("sdf"));
29 CHECK(!s.hasField("temperature"));
30 auto names = s.fieldNames();
31 CHECK(names.size() == 5); // u,v,w,p,sdf; sorted
32 CHECK(names.front() == "p");
33
34 // Add a user field: zero-initialised, then x-fastest inner round-trip.
35 s.addField("temperature");
36 CHECK(s.hasField("temperature"));
37 std::vector<double> in((std::size_t)nx * ny * nz);
38 for (std::size_t i = 0; i < in.size(); ++i)
39 in[i] = 1.0 + (double)i;
40 s.setField("temperature", in);
41 auto out = s.getField("temperature");
42 CHECK(out.size() == in.size());
43 bool exact = true;
44 for (std::size_t i = 0; i < in.size(); ++i)
45 exact = exact && (out[i] == in[i]);
46 CHECK(exact);
47
48 // fieldView aliases the live buffer: mutate a ghost-included element, gatherInner unaffected at
49 // ghosts but inner cell (g,g,g) maps to inner index 0.
50 auto fv = s.fieldView("temperature");
51 const auto bs = s.blockShape();
52 const int g = s.ghostWidth();
53 const long ex = bs[0], ey = bs[1];
54 // inner (0,0,0) lives at flat (g + g*ex + g*ex*ey)
55 const long inner0 = g + (long)g * ex + (long)g * ex * ey;
56 auto h = Kokkos::create_mirror_view(fv);
57 Kokkos::deep_copy(h, fv);
58 CHECK(h(inner0) == in[0]);
59 h(inner0) = 123.0;
60 Kokkos::deep_copy(fv, h);
61 CHECK(s.getField("temperature")[0] == 123.0);
62
63 // Idempotent add: existing data preserved.
64 s.addField("temperature");
65 CHECK(s.getField("temperature")[0] == 123.0);
66}
67} // namespace
68
69int main(int argc, char** argv) {
70 Kokkos::initialize(argc, argv);
71 run();
72 Kokkos::finalize();
73 if (failures == 0) {
74 std::printf("OK\n");
75 return 0;
76 }
77 std::fprintf(stderr, "%d failure(s)\n", failures);
78 return 1;
79}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Solver< Staggered > IbmSolver
#define CHECK(cond)
static int run(int bc)
peclet::flow::IbmSolver IbmSolver