From 0c0087ef9482634273ec1a23d3a915754f94926d Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Wed, 11 Nov 2026 01:16:44 +0100 Subject: Snapshot. Migration to Lbm Unit --- examples/cavity_2d/cavity_2d.cpp | 54 +++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 14 deletions(-) (limited to 'examples/cavity_2d/cavity_2d.cpp') diff --git a/examples/cavity_2d/cavity_2d.cpp b/examples/cavity_2d/cavity_2d.cpp index 177bf05..ea63745 100644 --- a/examples/cavity_2d/cavity_2d.cpp +++ b/examples/cavity_2d/cavity_2d.cpp @@ -39,10 +39,18 @@ using CellStruct = Struct< Member, "info"> >; -template +/** + * Macroscopic fields, one chunk per quantity. + * + * This used to be an Array of per cell structs. The VTK writer takes a Struct + * whose members are all Chunks of the same extent, and compute_rho_u wants + * Scalar/Vector rather than a bare primitive and a FixedArray, so the layout + * moved from array-of-struct to struct-of-arrays. + */ +template using MacroStruct = Struct< - Member, "momentum">, - Member + Member, 0u, DimX, DimY>, "velocity">, + Member, 0u, DimX, DimY>, "density"> >; using CavityFieldD2Q9 = CellField>; @@ -267,7 +275,9 @@ int main(){ {0.1} }; - print_lbm_meta(conv, {1e-3}); + // Lid driven cavity: the lid velocity and the cavity height are the + // characteristic scales. + print_lbm_meta(conv, {1e-3}, {0.1}, {dim_y}); //auto& df_field = lattices.at(0).template get<"dfs">(); //for(uint64_t i = 0; i < df_field.get_dim_size<0u>(); ++i){ @@ -308,7 +318,8 @@ int main(){ uint64_t print_every = 256u; uint64_t file_no = 0u; - kel::lbm::data,sch::D2Q9::D>> macros{dim}; + // Chunks carry their extent in the type, so no runtime dim argument. + kel::lbm::data> macros; for(uint64_t step = 0; step < lattice_steps; ++step){ @@ -319,9 +330,21 @@ int main(){ apply_for_cells([&](auto& cell, std::size_t i, std::size_t j){ auto& dfs = cell.template get<"dfs">(); - typename kel::lbm::native_data_type::type rho; - std::array::type, sch::D2Q9::D> vel; - compute_rho_u(dfs,rho,vel); + // compute_rho_u takes a FixedArray of populations now, while this + // example still stores them in a Cell. Same Q values, different + // schema, so copy across rather than reinterpret. + kel::lbm::data> dfs_arr; + for(uint64_t q{0u}; q < sch::D2Q9::Q; ++q){ + dfs_arr.at({q}) = dfs({q}); + } + + kel::lbm::data> rho_d; + kel::lbm::data> vel_d; + compute_rho_u(dfs_arr,rho_d,vel_d); + + const auto rho = rho_d.at({}).get(); + const typename kel::lbm::native_data_type::type vel[sch::D2Q9::D] = { + vel_d.at({{0u}}).get(), vel_d.at({{1u}}).get()}; if(i > 1 && (i+2) < dim_x && j > 1 && (j+2) < dim_y){ sum += rho; @@ -404,14 +427,17 @@ int main(){ apply_for_cells([&](auto& cell, std::size_t i, std::size_t j){ auto dfs = even_step ? cell.template get<"dfs_old">() : cell.template get<"dfs">(); - auto& rho = macros.at({{i,j}}).template get<"pressure">(); - auto& vel = macros.at({{i,j}}).template get<"momentum">(); - compute_rho_u(dfs,rho,vel); + kel::lbm::data> dfs_arr; + for(uint64_t q{0u}; q < sch::D2Q9::Q; ++q){ + dfs_arr.at({q}) = dfs({q}); + } + + auto& rho = macros.template get<"density">().at({{i,j}}); + auto& vel = macros.template get<"velocity">().at({{i,j}}); + compute_rho_u(dfs_arr,rho,vel); }, lattice); - std::string vtk_f_name{"tmp/cavity_2d_"}; - vtk_f_name += std::to_string(file_no) + ".vtk"; - write_vtk_file(vtk_f_name, macros); + write_vtk_file("tmp", "cavity_2d", file_no, macros); ++file_no; } -- cgit v1.3.1