summaryrefslogtreecommitdiff
path: root/examples/cavity_2d
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2026-11-11 01:16:44 +0100
committerClaudius "keldu" Holeksa <mail@keldu.de>2026-11-11 01:16:44 +0100
commit0c0087ef9482634273ec1a23d3a915754f94926d (patch)
tree972a8e48c1fae67af23b982d7a9e1a506fc8705f /examples/cavity_2d
parent2e63c4a31f946a8bafa92c49c1a0a116c0eb6dae (diff)
downloadlibs-lbm-0c0087ef9482634273ec1a23d3a915754f94926d.tar.gz
Snapshot. Migration to Lbm Unit
Diffstat (limited to 'examples/cavity_2d')
-rw-r--r--examples/cavity_2d/.nix/derivation.nix4
-rw-r--r--examples/cavity_2d/cavity_2d.cpp54
2 files changed, 42 insertions, 16 deletions
diff --git a/examples/cavity_2d/.nix/derivation.nix b/examples/cavity_2d/.nix/derivation.nix
index 9ae46b5..d0660e9 100644
--- a/examples/cavity_2d/.nix/derivation.nix
+++ b/examples/cavity_2d/.nix/derivation.nix
@@ -5,7 +5,7 @@
, forstio
, pname
, version
-, kel-lbm
+, kel
}:
stdenv.mkDerivation {
@@ -24,7 +24,7 @@ stdenv.mkDerivation {
forstio.codec
forstio.codec-unit
forstio.codec-json
- kel-lbm.core
+ kel.lbm.core
];
preferLocalBuild = true;
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<CellInfo<Desc>, "info">
>;
-template<typename T, uint64_t D>
+/**
+ * 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<typename T, uint64_t D, uint64_t DimX, uint64_t DimY>
using MacroStruct = Struct<
- Member<FixedArray<T,D>, "momentum">,
- Member<T, "pressure">
+ Member<Chunk<Vector<T,D>, 0u, DimX, DimY>, "velocity">,
+ Member<Chunk<Scalar<T>, 0u, DimX, DimY>, "density">
>;
using CavityFieldD2Q9 = CellField<D2Q9, CellStruct<D2Q9>>;
@@ -267,7 +275,9 @@ int main(){
{0.1}
};
- print_lbm_meta<sch::T, sch::D2Q9>(conv, {1e-3});
+ // Lid driven cavity: the lid velocity and the cavity height are the
+ // characteristic scales.
+ print_lbm_meta<sch::T, sch::D2Q9>(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::Array<sch::MacroStruct<sch::T,sch::D2Q9::D>,sch::D2Q9::D>> macros{dim};
+ // Chunks carry their extent in the type, so no runtime dim argument.
+ kel::lbm::data<sch::MacroStruct<sch::T,sch::D2Q9::D,dim_x,dim_y>> 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<sch::T>::type rho;
- std::array<typename kel::lbm::native_data_type<sch::T>::type, sch::D2Q9::D> vel;
- compute_rho_u<sch::T,sch::D2Q9>(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<sch::FixedArray<sch::T,sch::D2Q9::Q>> dfs_arr;
+ for(uint64_t q{0u}; q < sch::D2Q9::Q; ++q){
+ dfs_arr.at({q}) = dfs({q});
+ }
+
+ kel::lbm::data<sch::Scalar<sch::T>> rho_d;
+ kel::lbm::data<sch::Vector<sch::T,sch::D2Q9::D>> vel_d;
+ compute_rho_u<sch::T,sch::D2Q9>(dfs_arr,rho_d,vel_d);
+
+ const auto rho = rho_d.at({}).get();
+ const typename kel::lbm::native_data_type<sch::T>::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<sch::T,sch::D2Q9>(dfs,rho,vel);
+ kel::lbm::data<sch::FixedArray<sch::T,sch::D2Q9::Q>> 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<sch::T,sch::D2Q9>(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;
}