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 --- default.nix | 42 +- examples/cavity_2d/.nix/derivation.nix | 4 +- examples/cavity_2d/cavity_2d.cpp | 54 +- examples/cavity_2d_gpu/cavity_2d_gpu.cpp | 8 +- examples/cavity_3d.cpp | 4 +- examples/dkt/common.hpp | 2 +- examples/dkt/init.hpp | 10 +- examples/dkt/sim.cpp | 4 +- examples/dkt/step.hpp | 6 +- examples/feature_development/common.hpp | 2 +- examples/feature_development/init.hpp | 4 +- examples/feature_development/sim.cpp | 4 +- .../heterogeneous_computing/.nix/derivation.nix | 6 +- examples/meta/meta.cpp | 12 +- .../sim.cpp | 31 +- .../moving_poiseulle_particles_2d_hlbm_gpu/sim.cpp | 60 +- .../moving_poiseulle_particles_2d_psm_gpu/sim.cpp | 62 +- examples/one_particle_sedimentation_2d/common.hpp | 2 +- examples/one_particle_sedimentation_2d/init.hpp | 10 +- examples/one_particle_sedimentation_2d/sim.cpp | 11 +- examples/one_particle_sedimentation_2d/step.hpp | 11 +- examples/particles_gpu/particles_gpu.cpp | 8 +- examples/poiseulle_2d/poiseulle_2d.cpp | 4 +- examples/poiseulle_2d_gpu/poiseulle_2d_gpu.cpp | 12 +- examples/poiseulle_3d/.nix/derivation.nix | 4 +- examples/poiseulle_3d_gpu/sim.cpp | 10 +- .../poiseulle_3d_gpu_minimal/.nix/derivation.nix | 41 ++ examples/poiseulle_3d_gpu_minimal/SConscript | 34 ++ examples/poiseulle_3d_gpu_minimal/SConstruct | 81 +++ examples/poiseulle_3d_gpu_minimal/sim.cpp | 433 ++++++++++++++ .../poiseulle_moving_particle_2d_psm_gpu/sim.cpp | 8 +- .../.nix/derivation.nix | 41 ++ examples/poiseulle_particle_3d_hlbm_gpu/SConscript | 34 ++ examples/poiseulle_particle_3d_hlbm_gpu/SConstruct | 81 +++ examples/poiseulle_particle_3d_hlbm_gpu/sim.cpp | 604 +++++++++++++++++++ examples/poiseulle_particles_2d_bgk_gpu/sim.cpp | 8 +- examples/poiseulle_particles_2d_fplbm_gpu/sim.cpp | 8 +- examples/poiseulle_particles_2d_gpu/common.hpp | 4 +- examples/poiseulle_particles_2d_gpu/init.hpp | 4 +- examples/poiseulle_particles_2d_gpu/sim.cpp | 9 +- examples/poiseulle_particles_2d_hlbm_gpu/sim.cpp | 8 +- examples/poiseulle_particles_2d_ibm_gpu/sim.cpp | 33 +- examples/poiseulle_particles_2d_psm_gpu/sim.cpp | 8 +- .../poiseulle_particles_channel_2d.cpp | 6 +- .../common.hpp | 2 +- .../init.hpp | 10 +- .../schaefer_turek_durst_krause_rannbacher/sim.cpp | 9 +- .../step.hpp | 12 +- examples/settling_cubes_2d_ibm_gpu/sim.cpp | 49 +- examples/settling_spheres_2d_hlbm_gpu/sim.cpp | 47 +- examples/stdkr_2d_bgk/common.hpp | 2 +- examples/stdkr_2d_bgk/init.hpp | 8 +- examples/stdkr_2d_bgk/sim.cpp | 4 +- examples/stdkr_2d_bgk/step.hpp | 2 +- examples/stokes_drag_particle_2d_bgk_gpu/sim.cpp | 8 +- examples/stokes_drag_particle_2d_fplbm_gpu/sim.cpp | 8 +- examples/stokes_drag_particle_2d_hlbm_gpu/sim.cpp | 8 +- examples/stokes_drag_particle_2d_psm_gpu/sim.cpp | 8 +- examples/throat_clog_2d/common.hpp | 2 +- examples/throat_clog_2d/init.hpp | 10 +- examples/throat_clog_2d/sim.cpp | 4 +- examples/throat_clog_2d/step.hpp | 12 +- examples/vawt_3d_hlbm_gpu/.nix/derivation.nix | 41 ++ examples/vawt_3d_hlbm_gpu/SConscript | 34 ++ examples/vawt_3d_hlbm_gpu/SConstruct | 81 +++ examples/vawt_3d_hlbm_gpu/sim.cpp | 656 +++++++++++++++++++++ modules/core/c++/abstract/math.hpp | 63 ++ modules/core/c++/abstract/unit.hpp | 247 ++++++-- modules/core/c++/abstract/unit_print.hpp | 75 ++- modules/core/c++/boundary.hpp | 46 +- modules/core/c++/collision.hpp | 4 +- modules/core/c++/component/psm.hpp | 2 +- modules/core/c++/converter.hpp | 2 +- modules/core/c++/descriptor/d3q27.hpp | 70 ++- modules/core/c++/fplbm.hpp | 41 +- modules/core/c++/hlbm.hpp | 55 +- modules/core/c++/particle/aabb.hpp | 2 +- modules/core/c++/particle/contact.hpp | 20 +- modules/core/c++/particle/couple.hpp | 2 +- modules/core/c++/particle/integrate.hpp | 166 +++++- modules/core/c++/particle/particle.hpp | 26 +- modules/core/c++/particle/porosity.hpp | 2 +- modules/core/c++/particle/schema.hpp | 22 +- modules/core/c++/psm.hpp | 18 +- modules/core/tests/cell_list.cpp | 3 +- modules/core/tests/component_instantiation.cpp | 6 +- modules/core/tests/contact_dynamics.cpp | 5 +- modules/core/tests/descriptor.cpp | 84 +++ modules/core/tests/macroscopic.cpp | 113 ++++ modules/core/tests/mask.cpp | 6 +- modules/core/tests/particle_3d.cpp | 3 +- modules/core/tests/particle_consistency.cpp | 3 +- modules/core/tests/particle_edges.cpp | 3 +- modules/core/tests/particle_gaps.cpp | 3 +- modules/core/tests/particles.cpp | 24 +- modules/core/tests/rotation.cpp | 3 +- modules/core/tests/rotation_3d.cpp | 254 ++++++++ modules/core/tests/unit.cpp | 343 +++++++++++ modules/core/tests/voxel_narrow.cpp | 3 +- modules/core/tests/vtk_write.cpp | 6 +- probe_c.patch | 55 ++ 101 files changed, 4177 insertions(+), 452 deletions(-) create mode 100644 examples/poiseulle_3d_gpu_minimal/.nix/derivation.nix create mode 100644 examples/poiseulle_3d_gpu_minimal/SConscript create mode 100644 examples/poiseulle_3d_gpu_minimal/SConstruct create mode 100644 examples/poiseulle_3d_gpu_minimal/sim.cpp create mode 100644 examples/poiseulle_particle_3d_hlbm_gpu/.nix/derivation.nix create mode 100644 examples/poiseulle_particle_3d_hlbm_gpu/SConscript create mode 100644 examples/poiseulle_particle_3d_hlbm_gpu/SConstruct create mode 100644 examples/poiseulle_particle_3d_hlbm_gpu/sim.cpp create mode 100644 examples/vawt_3d_hlbm_gpu/.nix/derivation.nix create mode 100644 examples/vawt_3d_hlbm_gpu/SConscript create mode 100644 examples/vawt_3d_hlbm_gpu/SConstruct create mode 100644 examples/vawt_3d_hlbm_gpu/sim.cpp create mode 100644 modules/core/tests/macroscopic.cpp create mode 100644 modules/core/tests/rotation_3d.cpp create mode 100644 modules/core/tests/unit.cpp create mode 100644 probe_c.patch diff --git a/default.nix b/default.nix index e577796..916c3f5 100644 --- a/default.nix +++ b/default.nix @@ -52,13 +52,24 @@ let # What the examples and the default build use. Flip with --arg rocm true. adaptive-cpp = if rocm then adaptive-cpp-rocm else adaptive-cpp-base; + # forstio master as of 2026-11-09. Bump together with the sha256 below. + forstioRev = "97ae1a8f23074fea3784a008921c5d9fc3c1e8fb"; + forstio = let forstioSrc = stdenv.mkDerivation { name = "forstio-src"; + # Pinned to a commit, not to master. A branch snapshot is a moving + # target: every push to forstio regenerates the tarball and the sha256 + # below stops matching, so the build breaks with a hash mismatch for + # reasons that have nothing to do with this repo. + # + # To bump: set forstioRev, then run + # nix-prefetch-url --type sha256 \ + # https://git.keldu.de/forstio-forstio/snapshot/.tar.gz src = builtins.fetchurl { - url = "https://git.keldu.de/forstio-forstio/snapshot/master.tar.gz"; - sha256 = "sha256:1kb2f2g549hklpld5bsv6wzcmrfwfs0b6qp6fcbws3g5gz621cjj"; + url = "https://git.keldu.de/forstio-forstio/snapshot/${forstioRev}.tar.gz"; + sha256 = "sha256:0rmss3d83ryb24s8nirjrnxz32irws3hzwqkkxhk4xcf3rn8p8ls"; }; phases = [ "unpackPhase" "installPhase" ]; @@ -121,6 +132,26 @@ in rec { inherit pname version stdenv forstio adaptive-cpp; inherit kel; }; + + # Same case on a 32x16x16 domain, so it runs end to end in seconds. + poiseulle_3d_gpu_minimal = pkgs.callPackage ./examples/poiseulle_3d_gpu_minimal/.nix/derivation.nix { + inherit pname version stdenv forstio adaptive-cpp; + inherit kel; + }; + + # 32^3 channel carrying one HLBM-coupled sphere. The first 3D case with + # a particle at all, and the stepping stone to the fixed-axis rotor. + poiseulle_particle_3d_hlbm_gpu = pkgs.callPackage ./examples/poiseulle_particle_3d_hlbm_gpu/.nix/derivation.nix { + inherit pname version stdenv forstio adaptive-cpp; + inherit kel; + }; + + # Fixed-position rotor, free to spin about one arbitrary fixed axis and + # driven by the fluid. Angular velocity is an output. + vawt_3d_hlbm_gpu = pkgs.callPackage ./examples/vawt_3d_hlbm_gpu/.nix/derivation.nix { + inherit pname version stdenv forstio adaptive-cpp; + inherit kel; + }; cavity_2d = pkgs.callPackage ./examples/cavity_2d/.nix/derivation.nix { inherit pname version stdenv forstio; @@ -132,13 +163,8 @@ in rec { inherit kel; }; - planetary_3d = pkgs.callPackage ./examples/planetary_3d/.nix/derivation.nix { - inherit pname version stdenv forstio adaptive-cpp; - inherit kel; - }; - poiseulle_2d = pkgs.callPackage ./examples/poiseulle_3d/.nix/derivation.nix { - inherit pname version stdenv forstio; + inherit pname version stdenv forstio adaptive-cpp; inherit kel; }; 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, "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; } diff --git a/examples/cavity_2d_gpu/cavity_2d_gpu.cpp b/examples/cavity_2d_gpu/cavity_2d_gpu.cpp index ab1ab40..55225d1 100644 --- a/examples/cavity_2d_gpu/cavity_2d_gpu.cpp +++ b/examples/cavity_2d_gpu/cavity_2d_gpu.cpp @@ -46,7 +46,7 @@ using CellStruct = CellFieldStruct< template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member >; @@ -309,7 +309,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 eo_lbm_dir = output_directory(); if(eo_lbm_dir.is_error()){ @@ -361,7 +363,7 @@ int main(){ auto dfs_field = even_step ? dfs_old : dfs; auto& rho = macros.at(index).template get<"pressure">(); - auto& vel = macros.at(index).template get<"momentum">(); + auto& vel = macros.at(index).template get<"velocity">(); compute_rho_u(dfs_field[j],rho,vel); }, {{0u,0u}}, meta); std::string vtk_f_name{"tmp/cavity_2d_gpu_"}; diff --git a/examples/cavity_3d.cpp b/examples/cavity_3d.cpp index e62c017..f38ca00 100644 --- a/examples/cavity_3d.cpp +++ b/examples/cavity_3d.cpp @@ -45,7 +45,7 @@ using CellStruct = Struct< template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member >; @@ -410,7 +410,7 @@ int main(){ 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">(); + auto& vel = macros.at({{i,j}}).template get<"velocity">(); compute_rho_u(dfs,rho,vel); }, lattice); diff --git a/examples/dkt/common.hpp b/examples/dkt/common.hpp index 6c1dc44..abef548 100644 --- a/examples/dkt/common.hpp +++ b/examples/dkt/common.hpp @@ -56,7 +56,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "porosity">, Member, "force"> diff --git a/examples/dkt/init.hpp b/examples/dkt/init.hpp index c52078c..4a55dbc 100644 --- a/examples/dkt/init.hpp +++ b/examples/dkt/init.hpp @@ -57,7 +57,7 @@ kel::lbm::error_or init( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -94,8 +94,8 @@ kel::lbm::error_or init( ); { - kel::lbm::data> radius_p; - radius_p.at({}).set(conv.meter_si_to_lbm({{0.05}}).handle().get()); + kel::lbm::data>> radius_p; + radius_p.at({}).set(conv.meter_si_to_lbm({0.05}).handle().get()); std::cout<<"RADIUS: "<> dense_p; dense_p.at({}).set(1); @@ -107,8 +107,8 @@ kel::lbm::error_or init( auto& prb = p.template get<"rigid_body">(); auto& p_pos = prb.template get<"position">(); auto& p_posold = prb.template get<"position_old">(); - p_pos.at({{0u}}) = conv.meter_si_to_lbm({{0.16}}).handle(); - p_pos.at({{1u}}) = conv.meter_si_to_lbm({{0.2}}).handle(); + p_pos.at({{0u}}) = conv.meter_si_to_lbm({0.16}).handle(); + p_pos.at({{1u}}) = conv.meter_si_to_lbm({0.2}).handle(); p_posold = p_pos; } diff --git a/examples/dkt/sim.cpp b/examples/dkt/sim.cpp index 44fd145..c230aae 100644 --- a/examples/dkt/sim.cpp +++ b/examples/dkt/sim.cpp @@ -45,9 +45,9 @@ kel::lbm::error_or lbm_main(const kel::lbm::data& args){ converter conv { // delta_x - {{0.41 / dim_y}}, + {0.41 / dim_y}, // delta_t - {{1.067e-4}} + {1.067e-4} }; print_lbm_meta(conv,{1e-3},{1e-4},{0.5 * dim_y}); diff --git a/examples/dkt/step.hpp b/examples/dkt/step.hpp index 879def8..fa15609 100644 --- a/examples/dkt/step.hpp +++ b/examples/dkt/step.hpp @@ -76,7 +76,7 @@ kel::lbm::error_or step( kel::lbm::data> vel; { auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}}).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; @@ -152,7 +152,7 @@ kel::lbm::error_or step( kel::lbm::data> vel; { auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}}).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; @@ -228,7 +228,7 @@ kel::lbm::error_or step( kel::lbm::data> vel; { auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}}).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; diff --git a/examples/feature_development/common.hpp b/examples/feature_development/common.hpp index 6806b2c..e5274c2 100644 --- a/examples/feature_development/common.hpp +++ b/examples/feature_development/common.hpp @@ -48,7 +48,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "porosity"> >; diff --git a/examples/feature_development/init.hpp b/examples/feature_development/init.hpp index a61ef37..333e9a3 100644 --- a/examples/feature_development/init.hpp +++ b/examples/feature_development/init.hpp @@ -57,7 +57,7 @@ kel::lbm::error_or init( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -113,7 +113,7 @@ kel::lbm::error_or init( ); { - kel::lbm::data> radius_p; + kel::lbm::data>> radius_p; radius_p.at({}).set(2); kel::lbm::data> dense_p; dense_p.at({}).set(1); diff --git a/examples/feature_development/sim.cpp b/examples/feature_development/sim.cpp index e852313..34ad5e3 100644 --- a/examples/feature_development/sim.cpp +++ b/examples/feature_development/sim.cpp @@ -46,9 +46,9 @@ kel::lbm::error_or lbm_main(const kel::lbm::data& args){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/heterogeneous_computing/.nix/derivation.nix b/examples/heterogeneous_computing/.nix/derivation.nix index 48dfc7a..bf824bf 100644 --- a/examples/heterogeneous_computing/.nix/derivation.nix +++ b/examples/heterogeneous_computing/.nix/derivation.nix @@ -5,7 +5,8 @@ , forstio , pname , version -, kel-lbm +, adaptive-cpp +, kel }: stdenv.mkDerivation { @@ -30,7 +31,8 @@ stdenv.mkDerivation { forstio.remote-filesystem forstio.remote-io forstio.remote-sycl - kel-lbm.core + kel.lbm.core + adaptive-cpp ]; preferLocalBuild = true; diff --git a/examples/meta/meta.cpp b/examples/meta/meta.cpp index 6e0649f..3e573e5 100644 --- a/examples/meta/meta.cpp +++ b/examples/meta/meta.cpp @@ -28,10 +28,16 @@ template kel::lbm::error_or meta_lbm_info(const kel::lbm::data& args_str){ using namespace kel::lbm; converter conv{ - {args_str.template get<"dx">()}, - {args_str.template get<"dt">()} + as_unit, sch::LbmMeter>::Schema>( + args_str.template get<"dx">()), + as_unit, sch::LbmSecond>::Schema>( + args_str.template get<"dt">()) }; - print_lbm_meta(conv, {args_str.template get<"kin_vis">()},{args_str.template get<"char_vel">()},{args_str.template get<"char_len">()}); + print_lbm_meta( + conv, + as_unit>(args_str.template get<"kin_vis">()), + as_unit>(args_str.template get<"char_vel">()), + as_unit>(args_str.template get<"char_len">())); return kel::lbm::make_void(); } diff --git a/examples/moving_poiseulle_particles_2d_fplbm_gpu/sim.cpp b/examples/moving_poiseulle_particles_2d_fplbm_gpu/sim.cpp index d8c65fb..6d0dfb3 100644 --- a/examples/moving_poiseulle_particles_2d_fplbm_gpu/sim.cpp +++ b/examples/moving_poiseulle_particles_2d_fplbm_gpu/sim.cpp @@ -6,7 +6,6 @@ #include #include #include -#include namespace kel { namespace lbm { @@ -54,7 +53,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "porosity">, Member, "force"> @@ -118,7 +117,7 @@ kel::lbm::error_or init( auto& dfs_old_f = fields.template get<"dfs_old">(); auto& dfs_f = fields.template get<"dfs">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); auto& force_f = macros.template get<"force">(); @@ -145,7 +144,7 @@ kel::lbm::error_or init( dfs_f.get_dims() ); - kel::lbm::data> rad; + kel::lbm::data>> rad; rad.at({}) = dim_y * 0.1f; kel::lbm::data> dense; dense.at({}) = 1.0f; @@ -159,7 +158,12 @@ kel::lbm::error_or init( kel::lbm::data> zero; zero.at({{0u}}) = 0.0f; zero.at({{1u}}) = 0.0f; - rb.template get<"acceleration">() = zero; + // Acceleration is unit typed, so its zero has to be too. + kel::lbm::data>> zero_acc; + for(uint64_t i{0u}; i < Desc::D; ++i){ + zero_acc.at({{i}}).set(0); + } + rb.template get<"acceleration">() = zero_acc; rb.template get<"rotation">() = {}; rb.template get<"rotation_old">() = {}; rb.template get<"angular_acceleration">() = {}; @@ -174,11 +178,11 @@ kel::lbm::error_or step( kel::lbm::data>,encode::Sycl>& macros, kel::lbm::data>,encode::Sycl>& particles, kel::lbm::data t_i, + kel::lbm::data sub_steps, device& dev ){ auto& q = dev.get_handle(); auto& info_f = fields.template get<"info">(); - auto& porous_f = macros.template get<"porosity">(); q.submit([&](acpp::sycl::handler& h){ component> fplbm_reset; @@ -201,7 +205,7 @@ kel::lbm::error_or step( index.at({{i}}).set(idx[i]); } - fplbm_one_part.apply(fields,macros,particles,index,t_i,{64u}); + fplbm_one_part.apply(fields,macros,particles,index,t_i,sub_steps); }); }).wait(); @@ -262,20 +266,21 @@ template kel::lbm::error_or lbm_main(int argc, char** argv){ using namespace kel::lbm; - using dfi = df_info; - kel::lbm::data args; { auto& args_n = args.template get<"args">(); auto& name = args_n.template get<"name">(); name.set("moving_poiseulle_particles_2d_fplbm_gpu"); + auto& sub_steps_arg = args_n.template get<"sub_steps">(); + sub_steps_arg.set(64u); } { - auto eov = saw::parse_args(args,argc,argv); + auto eov = kel::lbm::parse_args(args,argc,argv); if(eov.is_error()){ return eov; } } + kel::lbm::data sub_steps = args.template get<"args">().template get<"sub_steps">(); auto eo_lbm_env = init_lbm_env(args); if(eo_lbm_env.is_error()){ return std::move(eo_lbm_env.get_error()); @@ -294,9 +299,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); @@ -378,7 +383,7 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ for(kel::lbm::data i{0u}; i < time_steps and krun; ++i){ // BC + Collision { - auto eov = step(lsd_view,lsdm_view,lsdp_view,i,dev); + auto eov = step(lsd_view,lsdm_view,lsdp_view,i,sub_steps,dev); if(eov.is_error()){ return eov; } diff --git a/examples/moving_poiseulle_particles_2d_hlbm_gpu/sim.cpp b/examples/moving_poiseulle_particles_2d_hlbm_gpu/sim.cpp index 86f0ded..d96fa1b 100644 --- a/examples/moving_poiseulle_particles_2d_hlbm_gpu/sim.cpp +++ b/examples/moving_poiseulle_particles_2d_hlbm_gpu/sim.cpp @@ -17,6 +17,14 @@ constexpr uint64_t particle_amount = 1ul; namespace sch { +using LbmArgs = Args< + Struct< + Member, + Member + >, + Tuple<> +>; + using InfoChunk = Chunk; template @@ -45,8 +53,9 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, + Member, "force">, Member, "porosity"> >; @@ -66,7 +75,6 @@ kel::lbm::error_or setup_initial_conditions( kel::lbm::data>& particles ){ auto& info_f = fields.template get<"info">(); - auto& porous_f = macros.template get<"porosity">(); // Set everything as walls iterator::apply( [&](auto& index){ @@ -108,7 +116,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -144,7 +152,7 @@ kel::lbm::error_or setup_initial_conditions( {{1u,1u}} ); - kel::lbm::data> rad; + kel::lbm::data>> rad; rad.at({}) = dim_y * 0.1f; kel::lbm::data> dense; dense.at({}) = 1.0f; @@ -158,7 +166,12 @@ kel::lbm::error_or setup_initial_conditions( kel::lbm::data> zero; zero.at({{0u}}) = 0.0f; zero.at({{1u}}) = 0.0f; - rb.template get<"acceleration">() = zero; + // Acceleration is unit typed, so its zero has to be too. + kel::lbm::data>> zero_acc; + for(uint64_t i{0u}; i < Desc::D; ++i){ + zero_acc.at({{i}}).set(0); + } + rb.template get<"acceleration">() = zero_acc; rb.template get<"rotation">() = {}; rb.template get<"rotation_old">() = {}; rb.template get<"angular_acceleration">() = {}; @@ -173,11 +186,11 @@ kel::lbm::error_or step( kel::lbm::data>,encode::Sycl>& macros, kel::lbm::data>,encode::Sycl>& particles, kel::lbm::data t_i, + kel::lbm::data sub_steps, device& dev ){ auto& q = dev.get_handle(); auto& info_f = fields.template get<"info">(); - auto& porous_f = macros.template get<"porosity">(); q.submit([&](acpp::sycl::handler& h){ component> hlbm_reset; @@ -200,7 +213,7 @@ kel::lbm::error_or step( index.at({{i}}).set(idx[i]); } - hlbm_one_part.apply(fields,macros,particles,index,t_i,{16u}); + hlbm_one_part.apply(fields,macros,particles,index,t_i,sub_steps); }); }).wait(); @@ -269,15 +282,28 @@ template kel::lbm::error_or lbm_main(int argc, char** argv){ using namespace kel::lbm; - using dfi = df_info; - - auto eo_lbm_dir = output_directory(); - if(eo_lbm_dir.is_error()){ - return std::move(eo_lbm_dir.get_error()); + kel::lbm::data args; + { + auto& args_n = args.template get<"args">(); + auto& name = args_n.template get<"name">(); + name.set("moving_poiseulle_particles_2d_hlbm_gpu"); + auto& sub_steps_arg = args_n.template get<"sub_steps">(); + sub_steps_arg.set(16u); + } + { + auto eov = kel::lbm::parse_args(args,argc,argv); + if(eov.is_error()){ + return eov; + } + } + kel::lbm::data sub_steps = args.template get<"args">().template get<"sub_steps">(); + auto eo_lbm_env = init_lbm_env(args); + if(eo_lbm_env.is_error()){ + return std::move(eo_lbm_env.get_error()); } - auto& lbm_dir = eo_lbm_dir.get_value(); + const auto& lbm_env = eo_lbm_env.get_value(); - auto out_dir = lbm_dir / "moving_poiseulle_particles_2d_hlbm_gpu"; + auto& out_dir = lbm_env.name_dir; { std::error_code ec; @@ -289,9 +315,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); @@ -373,7 +399,7 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ for(kel::lbm::data i{0u}; i < time_steps and krun; ++i){ // BC + Collision { - auto eov = step(lsd_view,lsdm_view,lsdp_view,i,dev); + auto eov = step(lsd_view,lsdm_view,lsdp_view,i,sub_steps,dev); if(eov.is_error()){ return eov; } diff --git a/examples/moving_poiseulle_particles_2d_psm_gpu/sim.cpp b/examples/moving_poiseulle_particles_2d_psm_gpu/sim.cpp index a27c402..0bfefba 100644 --- a/examples/moving_poiseulle_particles_2d_psm_gpu/sim.cpp +++ b/examples/moving_poiseulle_particles_2d_psm_gpu/sim.cpp @@ -17,6 +17,14 @@ constexpr uint64_t particle_amount = 1ul; namespace sch { +using LbmArgs = Args< + Struct< + Member, + Member + >, + Tuple<> +>; + using InfoChunk = Chunk; template @@ -45,7 +53,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "porosity">, Member, "force"> @@ -109,7 +117,7 @@ kel::lbm::error_or init( auto& dfs_old_f = fields.template get<"dfs_old">(); auto& dfs_f = fields.template get<"dfs">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); auto& force_f = macros.template get<"force">(); @@ -136,7 +144,7 @@ kel::lbm::error_or init( dfs_f.get_dims() ); - kel::lbm::data> rad; + kel::lbm::data>> rad; rad.at({}) = dim_y * 0.1f; kel::lbm::data> dense; dense.at({}) = 1.0f; @@ -150,7 +158,12 @@ kel::lbm::error_or init( kel::lbm::data> zero; zero.at({{0u}}) = 0.0f; zero.at({{1u}}) = 0.0f; - rb.template get<"acceleration">() = zero; + // Acceleration is unit typed, so its zero has to be too. + kel::lbm::data>> zero_acc; + for(uint64_t i{0u}; i < Desc::D; ++i){ + zero_acc.at({{i}}).set(0); + } + rb.template get<"acceleration">() = zero_acc; rb.template get<"rotation">() = {}; rb.template get<"rotation_old">() = {}; rb.template get<"angular_acceleration">() = {}; @@ -165,11 +178,11 @@ kel::lbm::error_or step( kel::lbm::data>,encode::Sycl>& macros, kel::lbm::data>,encode::Sycl>& particles, kel::lbm::data t_i, + kel::lbm::data sub_steps, device& dev ){ auto& q = dev.get_handle(); auto& info_f = fields.template get<"info">(); - auto& porous_f = macros.template get<"porosity">(); q.submit([&](acpp::sycl::handler& h){ component> psm_reset; @@ -204,9 +217,9 @@ kel::lbm::error_or step( } p_reset.apply(particles,index,t_i); - p_gather.apply(fields,macros,particles,index,t_i,{16u}); - p_contact.apply(particles,index,t_i,{16u}); - p_integrate.apply(particles,index,t_i,{16u}); + p_gather.apply(fields,macros,particles,index,t_i,sub_steps); + p_contact.apply(particles,index,t_i,sub_steps); + p_integrate.apply(particles,index,t_i,sub_steps); }); }).wait(); @@ -267,15 +280,28 @@ template kel::lbm::error_or lbm_main(int argc, char** argv){ using namespace kel::lbm; - using dfi = df_info; - - auto eo_lbm_dir = output_directory(); - if(eo_lbm_dir.is_error()){ - return std::move(eo_lbm_dir.get_error()); + kel::lbm::data args; + { + auto& args_n = args.template get<"args">(); + auto& name = args_n.template get<"name">(); + name.set("moving_poiseulle_particles_2d_psm_gpu"); + auto& sub_steps_arg = args_n.template get<"sub_steps">(); + sub_steps_arg.set(16u); + } + { + auto eov = kel::lbm::parse_args(args,argc,argv); + if(eov.is_error()){ + return eov; + } + } + kel::lbm::data sub_steps = args.template get<"args">().template get<"sub_steps">(); + auto eo_lbm_env = init_lbm_env(args); + if(eo_lbm_env.is_error()){ + return std::move(eo_lbm_env.get_error()); } - auto& lbm_dir = eo_lbm_dir.get_value(); + const auto& lbm_env = eo_lbm_env.get_value(); - auto out_dir = lbm_dir / "moving_poiseulle_particles_2d_psm_gpu"; + auto& out_dir = lbm_env.name_dir; { std::error_code ec; @@ -287,9 +313,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); @@ -371,7 +397,7 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ for(kel::lbm::data i{0u}; i < time_steps and krun; ++i){ // BC + Collision { - auto eov = step(lsd_view,lsdm_view,lsdp_view,i,dev); + auto eov = step(lsd_view,lsdm_view,lsdp_view,i,sub_steps,dev); if(eov.is_error()){ return eov; } diff --git a/examples/one_particle_sedimentation_2d/common.hpp b/examples/one_particle_sedimentation_2d/common.hpp index e28622f..f5382e0 100644 --- a/examples/one_particle_sedimentation_2d/common.hpp +++ b/examples/one_particle_sedimentation_2d/common.hpp @@ -58,7 +58,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "porosity">, Member, "force">, diff --git a/examples/one_particle_sedimentation_2d/init.hpp b/examples/one_particle_sedimentation_2d/init.hpp index 989ec4f..94ad3db 100644 --- a/examples/one_particle_sedimentation_2d/init.hpp +++ b/examples/one_particle_sedimentation_2d/init.hpp @@ -37,7 +37,7 @@ kel::lbm::error_or init( auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -74,8 +74,8 @@ kel::lbm::error_or init( ); { - kel::lbm::data> radius_p; - radius_p.at({}).set(conv.meter_si_to_lbm({{0.00125}}).handle().get()); + kel::lbm::data>> radius_p; + radius_p.at({}).set(conv.meter_si_to_lbm({0.00125}).handle().get()); std::cout<<"RADIUS: "<> dense_p; dense_p.at({}).set(1); @@ -87,8 +87,8 @@ kel::lbm::error_or init( auto& prb = p.template get<"rigid_body">(); auto& p_pos = prb.template get<"position">(); auto& p_posold = prb.template get<"position_old">(); - p_pos.at({{0u}}) = conv.meter_si_to_lbm({{0.01}}).handle(); - p_pos.at({{1u}}) = conv.meter_si_to_lbm({{0.04}}).handle(); + p_pos.at({{0u}}) = conv.meter_si_to_lbm({0.01}).handle(); + p_pos.at({{1u}}) = conv.meter_si_to_lbm({0.04}).handle(); p_posold = p_pos; } diff --git a/examples/one_particle_sedimentation_2d/sim.cpp b/examples/one_particle_sedimentation_2d/sim.cpp index 7c8456f..c9fae4d 100644 --- a/examples/one_particle_sedimentation_2d/sim.cpp +++ b/examples/one_particle_sedimentation_2d/sim.cpp @@ -2,12 +2,13 @@ #include "init.hpp" #include "step.hpp" -#include /** * For deciding what parameters to use maybe? */ namespace args { +using namespace kel::lbm::sch; + using LbmArgsStruct = Struct< Member @@ -45,16 +46,16 @@ kel::lbm::error_or lbm_main(const kel::lbm::data& args){ converter conv { // delta_x - {{0.02 / dim_x}}, + {0.02 / dim_x}, // delta_t - {{3e-7}} + {3e-7} }; kel::lbm::data> kin_vis_si{1e-3}; auto kin_vis_lbm = conv.kinematic_viscosity_si_to_lbm(kin_vis_si); auto tau = conv.template kinematic_viscosity_lbm_to_tau(kin_vis_lbm); print_lbm_meta(conv,kin_vis_si,{1e-4},{2}); - std::cout<<"Position: "<> meta{{dim_x,dim_y}}; auto lbm_data_ptr = kel::lbm::heap>>(); @@ -220,7 +221,7 @@ kel::lbm::error_or k_main(int argc, char** argv){ kel::lbm::data args; { - auto eov = saw::parse_args(args,argc,argv); + auto eov = kel::lbm::parse_args(args,argc,argv); if(eov.is_error()){ return eov; } diff --git a/examples/one_particle_sedimentation_2d/step.hpp b/examples/one_particle_sedimentation_2d/step.hpp index de42dec..f28586a 100644 --- a/examples/one_particle_sedimentation_2d/step.hpp +++ b/examples/one_particle_sedimentation_2d/step.hpp @@ -90,7 +90,8 @@ kel::lbm::error_or step( }).wait(); q.submit([&](acpp::sycl::handler& h){ - kel::lbm::data> gf; + // Gravity is an acceleration, which is what the integrator expects. + kel::lbm::data>> gf; gf.at({{1u}}) = conv.acceleration_si_to_lbm({-9.81}).handle(); component> fplbm_one_part{gf}; @@ -134,8 +135,8 @@ kel::lbm::error_or step( [&]() -> kel::lbm::data> { kel::lbm::data> vel; { - auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}}).handle(); + auto y_si = conv.meter_lbm_to_si(as_unit>(index.at({{1u}}).template cast_to())).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; @@ -210,8 +211,8 @@ kel::lbm::error_or step( [&]() -> kel::lbm::data> { kel::lbm::data> vel; { - auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}}).handle(); + auto y_si = conv.meter_lbm_to_si(as_unit>(index.at({{1u}}).template cast_to())).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; diff --git a/examples/particles_gpu/particles_gpu.cpp b/examples/particles_gpu/particles_gpu.cpp index f3edbf2..94ec5f1 100644 --- a/examples/particles_gpu/particles_gpu.cpp +++ b/examples/particles_gpu/particles_gpu.cpp @@ -30,7 +30,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ auto& old_pos = body.template get<"position_old">(); auto& acceleration = body.template get<"acceleration">(); auto& p_rad = part.template get<"collision">().template get<"radius">(); - p_rad = {0.4f}; + // Scalar base, so the value goes through the element rather than a + // braced whole-unit assignment. + p_rad.at({}).set(0.4f); if(j.get() % 2u == 0) acceleration.at({{1u}}) = {-9.81}; @@ -41,7 +43,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ } } - kel::lbm::data> time_step; + // A timestep is a lattice time, which makes (pos - pos_old)/time_step a + // velocity rather than an unlabelled vector. + kel::lbm::data>> time_step; time_step.at({}).set(0.05f); for(kel::lbm::data dt{0u}; dt < kel::lbm::data{32ul}; ++dt){ diff --git a/examples/poiseulle_2d/poiseulle_2d.cpp b/examples/poiseulle_2d/poiseulle_2d.cpp index 7ade1ca..39fc65f 100644 --- a/examples/poiseulle_2d/poiseulle_2d.cpp +++ b/examples/poiseulle_2d/poiseulle_2d.cpp @@ -40,7 +40,7 @@ using CellStruct = Struct< template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member >; @@ -374,7 +374,7 @@ int main(int argc, char** argv){ auto& dfs = even_step ? cell.template get<"dfs">() : cell.template get<"dfs_old">(); auto& rho = macros.at(index).template get<"pressure">(); - auto& vel = macros.at(index).template get<"momentum">(); + auto& vel = macros.at(index).template get<"velocity">(); compute_rho_u(dfs,rho,vel); rho = rho * kel::lbm::data{dfi::cs2}; diff --git a/examples/poiseulle_2d_gpu/poiseulle_2d_gpu.cpp b/examples/poiseulle_2d_gpu/poiseulle_2d_gpu.cpp index bece18f..5959506 100644 --- a/examples/poiseulle_2d_gpu/poiseulle_2d_gpu.cpp +++ b/examples/poiseulle_2d_gpu/poiseulle_2d_gpu.cpp @@ -35,7 +35,7 @@ using CellStruct = Struct< >; using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member >; @@ -244,7 +244,7 @@ void step( auto& macro_c = macro_cells[acc_id]; kel::lbm::data& rho = macro_c.template get<"pressure">(); - kel::lbm::data>& vel = macro_c.template get<"momentum">(); + kel::lbm::data>& vel = macro_c.template get<"velocity">(); compute_rho_u(dfs_old,rho,vel); auto eq = equilibrium(rho,vel); @@ -261,7 +261,7 @@ void step( auto& macro_c = macro_cells[acc_id]; kel::lbm::data& rho = macro_c.template get<"pressure">(); - kel::lbm::data>& vel = macro_c.template get<"momentum">(); + kel::lbm::data>& vel = macro_c.template get<"velocity">(); compute_rho_u(dfs_old,rho,vel); auto eq = equilibrium(rho,vel); @@ -278,7 +278,7 @@ void step( auto& macro_c = macro_cells[acc_id]; kel::lbm::data& rho = macro_c.template get<"pressure">(); - kel::lbm::data>& vel = macro_c.template get<"momentum">(); + kel::lbm::data>& vel = macro_c.template get<"velocity">(); compute_rho_u(dfs_old,rho,vel); auto eq = equilibrium(rho,vel); @@ -341,9 +341,9 @@ kel::lbm::error_or kel_main(int argc, char** argv){ // lbm::converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; uint64_t x_d = 1024u; diff --git a/examples/poiseulle_3d/.nix/derivation.nix b/examples/poiseulle_3d/.nix/derivation.nix index d53b5fe..b090add 100644 --- a/examples/poiseulle_3d/.nix/derivation.nix +++ b/examples/poiseulle_3d/.nix/derivation.nix @@ -5,7 +5,7 @@ , forstio , pname , version -, kel-lbm +, kel , adaptive-cpp }: @@ -28,7 +28,7 @@ stdenv.mkDerivation { forstio.remote forstio.remote-sycl adaptive-cpp - kel-lbm.core + kel.lbm.core ]; preferLocalBuild = true; diff --git a/examples/poiseulle_3d_gpu/sim.cpp b/examples/poiseulle_3d_gpu/sim.cpp index 3fe7358..83ba309 100644 --- a/examples/poiseulle_3d_gpu/sim.cpp +++ b/examples/poiseulle_3d_gpu/sim.cpp @@ -45,7 +45,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y, dim_z>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "porosity"> >; @@ -100,7 +100,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& mom_f = macros.template get<"momentum">(); + auto& mom_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -155,7 +155,7 @@ kel::lbm::error_or step( // auto coll_ev = q.submit([&](acpp::sycl::handler& h){ // Need nicer things to handle the flow. I see improvement here - component> collision{conv.template kinematic_viscosity_si_to_tau({0.1}).handle()}; + component> collision{conv.template kinematic_viscosity_si_to_tau({0.1})}; // component> collision{0.6}; component> bb; @@ -254,9 +254,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{0.01},{1024.0}); diff --git a/examples/poiseulle_3d_gpu_minimal/.nix/derivation.nix b/examples/poiseulle_3d_gpu_minimal/.nix/derivation.nix new file mode 100644 index 0000000..98ad46a --- /dev/null +++ b/examples/poiseulle_3d_gpu_minimal/.nix/derivation.nix @@ -0,0 +1,41 @@ +{ lib +, stdenv +, scons +, clang-tools +, forstio +, python3 +, pname +, version +, adaptive-cpp +, kel +}: + +stdenv.mkDerivation { + pname = pname + "-examples-" + "poiseulle_3d_gpu_minimal"; + inherit version; + src = ./..; + + nativeBuildInputs = [ + scons + clang-tools + python3 + ]; + + buildInputs = [ + forstio.core + forstio.async + forstio.codec + forstio.codec-unit + forstio.io + forstio.remote + forstio.remote-filesystem + forstio.codec-json + adaptive-cpp + kel.lbm.core + kel.lbm.sycl + ]; + + preferLocalBuild = true; + + outputs = [ "out" "dev" ]; +} diff --git a/examples/poiseulle_3d_gpu_minimal/SConscript b/examples/poiseulle_3d_gpu_minimal/SConscript new file mode 100644 index 0000000..29f73db --- /dev/null +++ b/examples/poiseulle_3d_gpu_minimal/SConscript @@ -0,0 +1,34 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +examples_env = env.Clone(); +examples_env['CXX'] = 'syclcc-clang'; +examples_env['CXXFLAGS'] += ['-O3']; + +examples_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +examples_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += examples_env.sources; +env.headers += examples_env.headers; + +# Cavity2D +examples_objects = []; +examples_env.add_source_files(examples_objects, ['sim.cpp'], shared=False); +examples_env.poiseulle_2d_gpu = examples_env.Program('#bin/poiseulle_3d_gpu_minimal', [examples_objects]); + +# Set Alias +env.examples = [ + examples_env.poiseulle_2d_gpu +]; +env.Alias('examples', env.examples); +env.targets += ['examples']; +env.Install('$prefix/bin/', env.examples); diff --git a/examples/poiseulle_3d_gpu_minimal/SConstruct b/examples/poiseulle_3d_gpu_minimal/SConstruct new file mode 100644 index 0000000..0611b67 --- /dev/null +++ b/examples/poiseulle_3d_gpu_minimal/SConstruct @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import glob +import re + + +if sys.version_info < (3,): + def isbasestring(s): + return isinstance(s,basestring) +else: + def isbasestring(s): + return isinstance(s, (str,bytes)) + +def add_kel_source_files(self, sources, filetype, lib_env=None, shared=False, target_post=""): + + if isbasestring(filetype): + dir_path = self.Dir('.').abspath + filetype = sorted(glob.glob(dir_path+"/"+filetype)) + + for path in filetype: + target_name = re.sub( r'(.*?)(\.cpp|\.c\+\+)', r'\1' + target_post, path ) + if shared: + target_name+='.os' + sources.append( self.SharedObject( target=target_name, source=path ) ) + else: + target_name+='.o' + sources.append( self.StaticObject( target=target_name, source=path ) ) + pass + +def isAbsolutePath(key, dirname, env): + assert os.path.isabs(dirname), "%r must have absolute path syntax" % (key,) + +env_vars = Variables( + args=ARGUMENTS +) + +env_vars.Add('prefix', + help='Installation target location of build results and headers', + default='/usr/local/', + validator=isAbsolutePath +) + +env_vars.Add('build_examples', + help='If examples should be built', + default="true" +) + +env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], + CPPDEFINES=['SAW_UNIX'], + CXXFLAGS=[ + '-std=c++20', + '-g', + '-Wall', + '-Wextra' + ], + LIBS=[ + 'forstio-core', + 'forstio-async', + 'forstio-io' + ] +); +env.__class__.add_source_files = add_kel_source_files +env.Tool('compilation_db'); +env.cdb = env.CompilationDatabase('compile_commands.json'); + +env.objects = []; +env.sources = []; +env.headers = []; +env.targets = []; + +Export('env') +SConscript('SConscript') + +env.Alias('cdb', env.cdb); +env.Alias('all', [env.targets]); +env.Default('all'); + +env.Alias('install', '$prefix') diff --git a/examples/poiseulle_3d_gpu_minimal/sim.cpp b/examples/poiseulle_3d_gpu_minimal/sim.cpp new file mode 100644 index 0000000..0bc4642 --- /dev/null +++ b/examples/poiseulle_3d_gpu_minimal/sim.cpp @@ -0,0 +1,433 @@ +#include +#include +#include + +#include +#include +#include +#include + +namespace kel { +namespace lbm { + +// Deliberately tiny: 32 x 16 x 16 = 8192 cells, so a full run finishes in +// seconds and the inlet signal reaches the outlet in ~32 steps. The full size +// case lives in poiseulle_3d_gpu. +constexpr uint64_t dim_x = 32ul; +constexpr uint64_t dim_y = 16ul; +constexpr uint64_t dim_z = 16ul; +constexpr uint64_t particle_size = 4ul; + +namespace sch { + +using LbmArgs = Args< + Struct< + Member, + Member + >, + Tuple<> +>; + +using InfoChunk = Chunk; + +template +using DfChunk = Chunk, 1u, dim_x, dim_y, dim_z>; + +template +using ScalarChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using VectorChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using ChunkStruct = Struct< + Member, + Member, "dfs">, + Member, "dfs_old">, + Member, "particle_N">, + Member, "particle_D"> +>; + +template +using MomChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using RhoChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using MacroStruct = Struct< + Member, "velocity">, + Member, "density">, + Member, "porosity"> +>; +} + +template +kel::lbm::error_or setup_initial_conditions( + converter& conv, + kel::lbm::data>& fields, + kel::lbm::data>& macros, + kel::lbm::data, particle_size>>& particles +){ + // Unit conversion is not exercised in this reduced case. + (void) conv; + + auto& info_f = fields.template get<"info">(); + // Set everything as walls + iterator::apply( + [&](auto& index){ + info_f.at(index).set(1u); + }, + {}, + info_f.get_dims(), + {} + ); + // Fluid + iterator::apply( + [&](auto& index){ + info_f.at(index).set(2u); + }, + {}, + info_f.get_dims(), + {{1u,1u,1u}} + ); + + // Inflow + iterator::apply( + [&](auto& index){ + info_f.at(index).set(3u); + }, + {{0u,0u,0u}}, + {{1u,dim_y,dim_z}}, + {{0u,1u,1u}} + ); + + // Outflow + iterator::apply( + [&](auto& index){ + info_f.at(index).set(4u); + }, + {{dim_x-1u,0u,0u}}, + {{dim_x,dim_y,dim_z}}, + {{0u,1u,1u}} + ); + // + auto& df_f = fields.template get<"dfs_old">(); + auto& rho_f = macros.template get<"density">(); + auto& mom_f = macros.template get<"velocity">(); + auto& por_f = macros.template get<"porosity">(); + + iterator::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + por_f.at(index).at({}) = {1}; + rho.at({}) = {1}; + auto& mom = mom_f.at(index); + auto eq = equilibrium(rho,mom); + + df = eq; + }, + {},// 0-index + df_f.get_dims() + ); + + iterator::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + rho.at({}) = {1}; + auto& mom = mom_f.at(index); + mom.at({{0u}}) = 0.01; + auto eq = equilibrium(rho,mom); + + df = eq; + }, + {},// 0-index + df_f.get_dims(), + {{1u,1u,1u}} + ); + + // No particles in this case -- the array is carried only so the schema + // matches poiseulle_3d_gpu. + (void) particles; + + return kel::lbm::make_void(); +} + +template +kel::lbm::error_or step( + converter& conv, + kel::lbm::data>,encode::Sycl>& fields, + kel::lbm::data>,encode::Sycl>& macros, + kel::lbm::data t_i, + device& dev +){ + auto& q = dev.get_handle(); + auto& info_f = fields.template get<"info">(); + + // auto coll_ev = + q.submit([&](acpp::sycl::handler& h){ + // Need nicer things to handle the flow. I see improvement here + component> collision{conv.template kinematic_viscosity_si_to_tau({0.1})}; + // component> collision{0.6}; + component> bb; + + component,encode::Sycl> flow_in{ + [&](){ + uint64_t target_t_i = 256u; + if(t_i.get() < target_t_i){ + return 1.0 + (0.001 / target_t_i) * t_i.get(); + } + return 1.001; + }() + }; + component,encode::Sycl> flow_out{1.0}; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y,dim_z}, [=](acpp::sycl::id idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + auto info = info_f.at(index); + + switch(info.get()){ + case 0u: + break; + case 1u: + bb.apply(fields,index,t_i); + break; + case 2u: + collision.apply(fields,macros,index,t_i); + break; + case 3u: + flow_in.apply(fields,index,t_i); + collision.apply(fields,macros,index,t_i); + break; + case 4u: + flow_out.apply(fields,index,t_i); + collision.apply(fields,macros,index,t_i); + break; + default: + break; + } + }); + }).wait(); + + q.submit([&](acpp::sycl::handler& h){ + component> stream; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y,dim_z}, [=](acpp::sycl::id idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + auto info = info_f.at(index); + + if(info.get() > 0u){ + stream.apply(fields,index,t_i); + } + }); + }).wait(); + + // Step + /* + q.submit([&](acpp::sycl::handler& h){ + // h.depends_on(collision_ev); + }).wait(); + */ + + return kel::lbm::make_void(); +} +} +} + +template +kel::lbm::error_or lbm_main(int argc, char** argv){ + using namespace kel::lbm; + + kel::lbm::data args; + { + auto& args_n = args.template get<"args">(); + auto& name = args_n.template get<"name">(); + name.set("poiseulle_3d_gpu_minimal"); + auto& steps_arg = args_n.template get<"time_steps">(); + steps_arg.set(64u); + } + { + auto eov = kel::lbm::parse_args(args,argc,argv); + if(eov.is_error()){ + return eov; + } + } + auto eo_lbm_env = init_lbm_env(args); + if(eo_lbm_env.is_error()){ + return std::move(eo_lbm_env.get_error()); + } + const auto& lbm_env = eo_lbm_env.get_value(); + + auto& out_dir = lbm_env.name_dir; + + { + std::error_code ec; + std::filesystem::create_directories(out_dir,ec); + if(ec != std::errc{}){ + return kel::lbm::make_error("Could not create output directory"); + } + } + + converter conv { + // delta_x + {1.0}, + // delta_t + {1.0} + }; + + print_lbm_meta(conv,{0.1},{0.01},{1024.0}); + + auto lbm_data_ptr = kel::lbm::heap>>(); + auto lbm_macro_data_ptr = kel::lbm::heap>>(); + auto lbm_particle_data_ptr = kel::lbm::heap, particle_size>>>(); + + std::cout<<"Estimated Bytes: "<,sch::MacroStruct>().get()<( + std::string{eo_aio.get_error().get_message()}); + } + auto& aio = eo_aio.get_value(); + saw::wait_scope wait{aio.event_loop}; + + bool krun = true; + bool print_status = false; + aio.event_port.on_signal(saw::Signal::Terminate).then([&](){ + krun = false; + }).detach(); + aio.event_port.on_signal(saw::Signal::User1).then([&](){ + print_status = true; + }).detach(); + + device dev; + + auto& sycl_q = dev.get_handle(); + + sycl_q.wait(); + { + auto eov = setup_initial_conditions(conv,*lbm_data_ptr,*lbm_macro_data_ptr,*lbm_particle_data_ptr); + if(eov.is_error()){ + return eov; + } + } + + kel::lbm::data, encode::Sycl> lbm_sycl_data{sycl_q}; + kel::lbm::data, encode::Sycl> lbm_sycl_macro_data{sycl_q}; + kel::lbm::data, particle_size>, encode::Sycl> lbm_sycl_particle_data{sycl_q}; + sycl_q.wait(); + + auto lsd_view = make_view(lbm_sycl_data); + auto lsdm_view = make_view(lbm_sycl_macro_data); + { + auto eov = dev.copy_to_device(*lbm_data_ptr,lbm_sycl_data); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_device(*lbm_macro_data_ptr,lbm_sycl_macro_data); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_device(*lbm_particle_data_ptr,lbm_sycl_particle_data); + if(eov.is_error()){ + return eov; + } + } + sycl_q.wait(); + kel::lbm::data time_steps = args.template get<"args">().template get<"time_steps">(); + + for(kel::lbm::data i{0u}; i < time_steps and krun; ++i){ + { + { + auto eov = write_vtk_file(out_dir,"t",i.get(),*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + /*{ + std::string file_name = "p_"; + file_name += std::to_string(i.get()); + file_name += ".json"; + auto eov = saw::easy::encode_and_write_file,particle_size>,saw::encode::Json>(out_dir, *lbm_particle_data_ptr); + if(eov.is_error()){ + return eov; + } + }*/ + } + { + auto eov = step(conv,lsd_view,lsdm_view,i,dev); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_host(lbm_sycl_data,*lbm_data_ptr); + if(eov.is_error()){ + return eov; + } + } + + wait.poll(); + if(print_status){ + std::cout<<"Status: "<().get() * 100 / time_steps.get())<<"%"<::apply( + [&](auto& index){ + std::cout<().at(index).template cast_to().get()<<"\n"; + }, + {{0u,0u}}, + {{dim_x, dim_y}} + ); + */ + + sycl_q.wait(); + return kel::lbm::make_void(); +} + +using FloatT = kel::lbm::sch::Float32; + +int main(int argc, char** argv){ + auto eov = lbm_main(argc, argv); + if(eov.is_error()){ + auto& err = eov.get_error(); + std::cerr<<"[Error] "< 0u){ + std::cerr<<" - "<, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "porosity"> >; @@ -122,7 +122,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -261,9 +261,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/poiseulle_particle_3d_hlbm_gpu/.nix/derivation.nix b/examples/poiseulle_particle_3d_hlbm_gpu/.nix/derivation.nix new file mode 100644 index 0000000..3f1dd7b --- /dev/null +++ b/examples/poiseulle_particle_3d_hlbm_gpu/.nix/derivation.nix @@ -0,0 +1,41 @@ +{ lib +, stdenv +, scons +, clang-tools +, forstio +, python3 +, pname +, version +, adaptive-cpp +, kel +}: + +stdenv.mkDerivation { + pname = pname + "-examples-" + "poiseulle_particle_3d_hlbm_gpu"; + inherit version; + src = ./..; + + nativeBuildInputs = [ + scons + clang-tools + python3 + ]; + + buildInputs = [ + forstio.core + forstio.async + forstio.codec + forstio.codec-unit + forstio.io + forstio.remote + forstio.remote-filesystem + forstio.codec-json + adaptive-cpp + kel.lbm.core + kel.lbm.sycl + ]; + + preferLocalBuild = true; + + outputs = [ "out" "dev" ]; +} diff --git a/examples/poiseulle_particle_3d_hlbm_gpu/SConscript b/examples/poiseulle_particle_3d_hlbm_gpu/SConscript new file mode 100644 index 0000000..478ee44 --- /dev/null +++ b/examples/poiseulle_particle_3d_hlbm_gpu/SConscript @@ -0,0 +1,34 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +examples_env = env.Clone(); +examples_env['CXX'] = 'syclcc-clang'; +examples_env['CXXFLAGS'] += ['-O3']; + +examples_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +examples_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += examples_env.sources; +env.headers += examples_env.headers; + +# Cavity2D +examples_objects = []; +examples_env.add_source_files(examples_objects, ['sim.cpp'], shared=False); +examples_env.poiseulle_2d_gpu = examples_env.Program('#bin/poiseulle_particle_3d_hlbm_gpu', [examples_objects]); + +# Set Alias +env.examples = [ + examples_env.poiseulle_2d_gpu +]; +env.Alias('examples', env.examples); +env.targets += ['examples']; +env.Install('$prefix/bin/', env.examples); diff --git a/examples/poiseulle_particle_3d_hlbm_gpu/SConstruct b/examples/poiseulle_particle_3d_hlbm_gpu/SConstruct new file mode 100644 index 0000000..0611b67 --- /dev/null +++ b/examples/poiseulle_particle_3d_hlbm_gpu/SConstruct @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import glob +import re + + +if sys.version_info < (3,): + def isbasestring(s): + return isinstance(s,basestring) +else: + def isbasestring(s): + return isinstance(s, (str,bytes)) + +def add_kel_source_files(self, sources, filetype, lib_env=None, shared=False, target_post=""): + + if isbasestring(filetype): + dir_path = self.Dir('.').abspath + filetype = sorted(glob.glob(dir_path+"/"+filetype)) + + for path in filetype: + target_name = re.sub( r'(.*?)(\.cpp|\.c\+\+)', r'\1' + target_post, path ) + if shared: + target_name+='.os' + sources.append( self.SharedObject( target=target_name, source=path ) ) + else: + target_name+='.o' + sources.append( self.StaticObject( target=target_name, source=path ) ) + pass + +def isAbsolutePath(key, dirname, env): + assert os.path.isabs(dirname), "%r must have absolute path syntax" % (key,) + +env_vars = Variables( + args=ARGUMENTS +) + +env_vars.Add('prefix', + help='Installation target location of build results and headers', + default='/usr/local/', + validator=isAbsolutePath +) + +env_vars.Add('build_examples', + help='If examples should be built', + default="true" +) + +env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], + CPPDEFINES=['SAW_UNIX'], + CXXFLAGS=[ + '-std=c++20', + '-g', + '-Wall', + '-Wextra' + ], + LIBS=[ + 'forstio-core', + 'forstio-async', + 'forstio-io' + ] +); +env.__class__.add_source_files = add_kel_source_files +env.Tool('compilation_db'); +env.cdb = env.CompilationDatabase('compile_commands.json'); + +env.objects = []; +env.sources = []; +env.headers = []; +env.targets = []; + +Export('env') +SConscript('SConscript') + +env.Alias('cdb', env.cdb); +env.Alias('all', [env.targets]); +env.Default('all'); + +env.Alias('install', '$prefix') diff --git a/examples/poiseulle_particle_3d_hlbm_gpu/sim.cpp b/examples/poiseulle_particle_3d_hlbm_gpu/sim.cpp new file mode 100644 index 0000000..59ccd46 --- /dev/null +++ b/examples/poiseulle_particle_3d_hlbm_gpu/sim.cpp @@ -0,0 +1,604 @@ +#include +#include +#include + +#include + +#include +#include +#include +#include + +namespace kel { +namespace lbm { + +// Deliberately tiny: 32 x 32 x 32 = 32768 cells, so a full run finishes in +// seconds. Square in y/z so the channel cross section is symmetric about the +// particle, which makes a stray transverse force obvious. +constexpr uint64_t dim_x = 32ul; +constexpr uint64_t dim_y = 32ul; +constexpr uint64_t dim_z = 32ul; + +constexpr uint64_t particle_amount = 1ul; + +/** + * The particle lives in lattice space, exactly like the fluid: the radius is a + * number of cells, the density is relative to the fluid's rho = 1, and the + * mass, inertia, force and torque that follow are all lattice quantities. The + * converter handles the fluid's SI <-> LBM bookkeeping and is deliberately not + * applied to any of this -- so every column of particle.csv is in cells, + * steps, and lattice mass. + */ +constexpr double particle_radius = 0.15 * static_cast(dim_y); +constexpr double particle_density = 1.0; + +namespace sch { + +using LbmArgs = Args< + Struct< + Member, + Member, + Member + >, + Tuple<> +>; + +using InfoChunk = Chunk; + +template +using DfChunk = Chunk, 1u, dim_x, dim_y, dim_z>; + +template +using ScalarChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using VectorChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using ChunkStruct = Struct< + Member, + Member, "dfs">, + Member, "dfs_old">, + Member, "particle_N">, + Member, "particle_D"> +>; + +template +using MomChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using RhoChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +/** + * "force" is not optional here: HlbmOneParticleGather reads it, so the 3D + * macro struct needs it the moment a particle is carried. + */ +template +using MacroStruct = Struct< + Member, "velocity">, + Member, "density">, + Member, "force">, + Member, "porosity"> +>; + +/** + * particle_aabb is specialised only on ParticleGroup, so a bare + * FixedArray> hits its always_false primary template. + */ +template +using ParticleSpheroidGroup = ParticleGroup>; +} + +template +kel::lbm::error_or setup_initial_conditions( + converter& conv, + kel::lbm::data>& fields, + kel::lbm::data>& macros, + kel::lbm::data>& particles +){ + // Unit conversion is not exercised in this reduced case. + (void) conv; + + auto& info_f = fields.template get<"info">(); + // Set everything as walls + iterator::apply( + [&](auto& index){ + info_f.at(index).set(1u); + }, + {}, + info_f.get_dims(), + {} + ); + // Fluid + iterator::apply( + [&](auto& index){ + info_f.at(index).set(2u); + }, + {}, + info_f.get_dims(), + {{1u,1u,1u}} + ); + + // Inflow + iterator::apply( + [&](auto& index){ + info_f.at(index).set(3u); + }, + {{0u,0u,0u}}, + {{1u,dim_y,dim_z}}, + {{0u,1u,1u}} + ); + + // Outflow + iterator::apply( + [&](auto& index){ + info_f.at(index).set(4u); + }, + {{dim_x-1u,0u,0u}}, + {{dim_x,dim_y,dim_z}}, + {{0u,1u,1u}} + ); + // + auto& df_f = fields.template get<"dfs_old">(); + auto& rho_f = macros.template get<"density">(); + auto& mom_f = macros.template get<"velocity">(); + auto& por_f = macros.template get<"porosity">(); + auto& force_f = macros.template get<"force">(); + + iterator::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + por_f.at(index).at({}) = {1}; + rho.at({}) = {1}; + auto& mom = mom_f.at(index); + auto eq = equilibrium(rho,mom); + + df = eq; + + auto& force = force_f.at(index); + for(uint64_t i{0u}; i < Desc::D; ++i){ + force.at({{i}}).set(0); + } + }, + {},// 0-index + df_f.get_dims() + ); + + iterator::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + rho.at({}) = {1}; + auto& mom = mom_f.at(index); + mom.at({{0u}}) = 0.01; + auto eq = equilibrium(rho,mom); + + df = eq; + }, + {},// 0-index + df_f.get_dims(), + {{1u,1u,1u}} + ); + + kel::lbm::data>> rad; + rad.at({}).set(particle_radius); + kel::lbm::data> dense; + dense.at({}).set(particle_density); + + // Mask resolution 16 in 3D is 16^3 = 4096 voxels. The analytic spheroid + // porosity is what HLBM actually calls, but the mask is still filled. + particles = create_spheroid_particle_group(rad,dense,{16u}); + { + auto& rb = particles.template get<"particles">().at({0u}).template get<"rigid_body">(); + auto& rbp = rb.template get<"position">(); + rbp.at({{0u}}) = 0.3 * dim_x; + rbp.at({{1u}}) = 0.5 * dim_y; + rbp.at({{2u}}) = 0.5 * dim_z; + + // position_old == position means the body starts at rest. It also + // makes a pinned body exact later: 2x - x_old + 0 == x. + rb.template get<"position_old">() = rbp; + + kel::lbm::data> zero; + for(uint64_t i{0u}; i < Desc::D; ++i){ + zero.at({{i}}).set(0); + } + // Acceleration is unit typed now, so its zero has to be too. + kel::lbm::data>> zero_acc; + for(uint64_t i{0u}; i < Desc::D; ++i){ + zero_acc.at({{i}}).set(0); + } + rb.template get<"acceleration">() = zero_acc; + + // In 3D these are Vector (a rotation vector), not a scalar angle. + rb.template get<"rotation">() = zero; + rb.template get<"rotation_old">() = zero; + rb.template get<"angular_acceleration">() = zero; + } + + return kel::lbm::make_void(); +} + +template +kel::lbm::error_or step( + converter& conv, + kel::lbm::data>,encode::Sycl>& fields, + kel::lbm::data>,encode::Sycl>& macros, + kel::lbm::data>,encode::Sycl>& particles, + kel::lbm::data t_i, + kel::lbm::data sub_steps, + device& dev +){ + auto& q = dev.get_handle(); + auto& info_f = fields.template get<"info">(); + + // Porosity back to 1 (all fluid) before the particle restamps itself. + q.submit([&](acpp::sycl::handler& h){ + component> hlbm_reset; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y,dim_z}, [=](acpp::sycl::id idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + hlbm_reset.apply(fields,macros,index,t_i); + }); + }).wait(); + + /** + * One work item per particle. The stamp has to come after the integrate: + * it writes the post-integration surface velocity back onto the lattice. + */ + q.submit([&](acpp::sycl::handler& h){ + component> p_reset; + component> p_gather; + component> p_integrate; + component> p_stamp; + + // Soft shell against the domain walls so the sphere cannot tunnel out. + dem_params p_dem{1.0e-2, 0.3, 0.0, 0.0}; + kel::lbm::data> p_extent{{dim_x,dim_y,dim_z}}; + component> p_contact{p_dem,p_extent}; + + h.parallel_for(acpp::sycl::range<1u>{particle_amount}, [=](acpp::sycl::id<1u> idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < 1u; ++i){ + index.at({{i}}).set(idx[i]); + } + + p_reset.apply(particles,index,t_i); + p_gather.apply(fields,macros,particles,index,t_i,sub_steps); + p_contact.apply(particles,index,t_i,sub_steps); + p_integrate.apply(particles,index,t_i,sub_steps); + p_stamp.apply(fields,macros,particles,index,t_i,sub_steps); + }); + }).wait(); + + // auto coll_ev = + q.submit([&](acpp::sycl::handler& h){ + /** + * Hlbm rather than BGK: it is the collision that consumes the + * porosity and particle_N/particle_D the stamp pass writes. + */ + component> collision{conv.template kinematic_viscosity_si_to_tau({0.1})}; + component> bb; + + component,encode::Sycl> flow_in{ + [&](){ + uint64_t target_t_i = 256u; + if(t_i.get() < target_t_i){ + return 1.0 + (0.001 / target_t_i) * t_i.get(); + } + return 1.001; + }() + }; + component,encode::Sycl> flow_out{1.0}; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y,dim_z}, [=](acpp::sycl::id idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + auto info = info_f.at(index); + + switch(info.get()){ + case 0u: + break; + case 1u: + bb.apply(fields,index,t_i); + break; + case 2u: + collision.apply(fields,macros,index,t_i); + break; + case 3u: + flow_in.apply(fields,index,t_i); + collision.apply(fields,macros,index,t_i); + break; + case 4u: + flow_out.apply(fields,index,t_i); + collision.apply(fields,macros,index,t_i); + break; + default: + break; + } + }); + }).wait(); + + q.submit([&](acpp::sycl::handler& h){ + component> stream; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y,dim_z}, [=](acpp::sycl::id idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + auto info = info_f.at(index); + + if(info.get() > 0u){ + stream.apply(fields,index,t_i); + } + }); + }).wait(); + + // Step + /* + q.submit([&](acpp::sycl::handler& h){ + // h.depends_on(collision_ev); + }).wait(); + */ + + return kel::lbm::make_void(); +} +} +} + +template +kel::lbm::error_or lbm_main(int argc, char** argv){ + using namespace kel::lbm; + + kel::lbm::data args; + { + auto& args_n = args.template get<"args">(); + auto& name = args_n.template get<"name">(); + name.set("poiseulle_particle_3d_hlbm_gpu"); + auto& steps_arg = args_n.template get<"time_steps">(); + steps_arg.set(256u); + auto& sub_arg = args_n.template get<"sub_steps">(); + sub_arg.set(16u); + } + { + auto eov = kel::lbm::parse_args(args,argc,argv); + if(eov.is_error()){ + return eov; + } + } + auto eo_lbm_env = init_lbm_env(args); + if(eo_lbm_env.is_error()){ + return std::move(eo_lbm_env.get_error()); + } + const auto& lbm_env = eo_lbm_env.get_value(); + + auto& out_dir = lbm_env.name_dir; + + { + std::error_code ec; + std::filesystem::create_directories(out_dir,ec); + if(ec != std::errc{}){ + return kel::lbm::make_error("Could not create output directory"); + } + } + + converter conv { + // delta_x + {1.0}, + // delta_t + {1.0} + }; + + // Characteristic length is the body diameter -- the scale that sets Re here. + print_lbm_meta(conv,{0.1},{0.01},{2.0 * particle_radius}); + + auto lbm_data_ptr = kel::lbm::heap>>(); + auto lbm_macro_data_ptr = kel::lbm::heap>>(); + auto lbm_particle_data_ptr = kel::lbm::heap>>(); + + std::cout<<"Estimated Bytes: "<,sch::MacroStruct>().get()<( + std::string{eo_aio.get_error().get_message()}); + } + auto& aio = eo_aio.get_value(); + saw::wait_scope wait{aio.event_loop}; + + bool krun = true; + bool print_status = false; + aio.event_port.on_signal(saw::Signal::Terminate).then([&](){ + krun = false; + }).detach(); + aio.event_port.on_signal(saw::Signal::User1).then([&](){ + print_status = true; + }).detach(); + + device dev; + + auto& sycl_q = dev.get_handle(); + + sycl_q.wait(); + { + auto eov = setup_initial_conditions(conv,*lbm_data_ptr,*lbm_macro_data_ptr,*lbm_particle_data_ptr); + if(eov.is_error()){ + return eov; + } + } + + kel::lbm::data, encode::Sycl> lbm_sycl_data{sycl_q}; + kel::lbm::data, encode::Sycl> lbm_sycl_macro_data{sycl_q}; + kel::lbm::data, encode::Sycl> lbm_sycl_particle_data{sycl_q}; + sycl_q.wait(); + + auto lsd_view = make_view(lbm_sycl_data); + auto lsdm_view = make_view(lbm_sycl_macro_data); + auto lsdp_view = make_view(lbm_sycl_particle_data); + { + auto eov = dev.copy_to_device(*lbm_data_ptr,lbm_sycl_data); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_device(*lbm_macro_data_ptr,lbm_sycl_macro_data); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_device(*lbm_particle_data_ptr,lbm_sycl_particle_data); + if(eov.is_error()){ + return eov; + } + } + sycl_q.wait(); + kel::lbm::data time_steps = args.template get<"args">().template get<"time_steps">(); + kel::lbm::data sub_steps = args.template get<"args">().template get<"sub_steps">(); + + /** + * Particle time series. write_vtk_file only accepts a struct of chunks, so + * a particle group cannot go through it -- a CSV beside the VTK output is + * the simpler answer, and it is what you actually plot. + * + * Velocity and angular velocity are not stored on the body: the scheme is + * position-Verlet, so both are recovered as (now - previous) * steps. + */ + std::ofstream particle_csv{out_dir / "particle.csv"}; + if(not particle_csv.is_open()){ + return kel::lbm::make_error("Could not open particle.csv"); + } + particle_csv + <<"step,x,y,z,vx,vy,vz,fx,fy,fz," + <<"rot_x,rot_y,rot_z,omega_x,omega_y,omega_z," + <<"alpha_x,alpha_y,alpha_z,tau_x,tau_y,tau_z\n"; + + auto write_particle_row = [&](uint64_t step_i){ + auto& rb = lbm_particle_data_ptr->template get<"particles">() + .at({0u}).template get<"rigid_body">(); + + auto& pos = rb.template get<"position">(); + auto& pos_old = rb.template get<"position_old">(); + auto& force = rb.template get<"force">(); + auto& rot = rb.template get<"rotation">(); + auto& rot_old = rb.template get<"rotation_old">(); + auto& ang_acc = rb.template get<"angular_acceleration">(); + auto& torque = rb.template get<"torque">(); + + const auto steps = static_cast(particle_sub_steps(sub_steps)); + + particle_csv< i{0u}; i < time_steps and krun; ++i){ + { + { + auto eov = write_vtk_file(out_dir,"t",i.get(),*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + } + { + auto eov = step(conv,lsd_view,lsdm_view,lsdp_view,i,sub_steps,dev); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_host(lbm_sycl_particle_data,*lbm_particle_data_ptr); + if(eov.is_error()){ + return eov; + } + } + write_particle_row(i.get()); + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_host(lbm_sycl_data,*lbm_data_ptr); + if(eov.is_error()){ + return eov; + } + } + + wait.poll(); + if(print_status){ + std::cout<<"Status: "<().get() * 100 / time_steps.get())<<"%"<::apply( + [&](auto& index){ + std::cout<().at(index).template cast_to().get()<<"\n"; + }, + {{0u,0u}}, + {{dim_x, dim_y}} + ); + */ + + sycl_q.wait(); + return kel::lbm::make_void(); +} + +using FloatT = kel::lbm::sch::Float32; + +int main(int argc, char** argv){ + auto eov = lbm_main(argc, argv); + if(eov.is_error()){ + auto& err = eov.get_error(); + std::cerr<<"[Error] "< 0u){ + std::cerr<<" - "<, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density"> >; @@ -101,7 +101,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); iterator::apply( [&](auto& index){ @@ -270,9 +270,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/poiseulle_particles_2d_fplbm_gpu/sim.cpp b/examples/poiseulle_particles_2d_fplbm_gpu/sim.cpp index 9d9e32e..06c1d21 100644 --- a/examples/poiseulle_particles_2d_fplbm_gpu/sim.cpp +++ b/examples/poiseulle_particles_2d_fplbm_gpu/sim.cpp @@ -48,7 +48,7 @@ using ForceChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "force">, Member, "porosity"> @@ -108,7 +108,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -258,9 +258,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/poiseulle_particles_2d_gpu/common.hpp b/examples/poiseulle_particles_2d_gpu/common.hpp index 6806b2c..f4c9fed 100644 --- a/examples/poiseulle_particles_2d_gpu/common.hpp +++ b/examples/poiseulle_particles_2d_gpu/common.hpp @@ -48,8 +48,10 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, + // HlbmOneParticleGather reads this one. + Member, "force">, Member, "porosity"> >; diff --git a/examples/poiseulle_particles_2d_gpu/init.hpp b/examples/poiseulle_particles_2d_gpu/init.hpp index a61ef37..333e9a3 100644 --- a/examples/poiseulle_particles_2d_gpu/init.hpp +++ b/examples/poiseulle_particles_2d_gpu/init.hpp @@ -57,7 +57,7 @@ kel::lbm::error_or init( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -113,7 +113,7 @@ kel::lbm::error_or init( ); { - kel::lbm::data> radius_p; + kel::lbm::data>> radius_p; radius_p.at({}).set(2); kel::lbm::data> dense_p; dense_p.at({}).set(1); diff --git a/examples/poiseulle_particles_2d_gpu/sim.cpp b/examples/poiseulle_particles_2d_gpu/sim.cpp index e852313..f3c001b 100644 --- a/examples/poiseulle_particles_2d_gpu/sim.cpp +++ b/examples/poiseulle_particles_2d_gpu/sim.cpp @@ -2,12 +2,13 @@ #include "init.hpp" #include "step.hpp" -#include /** * For deciding what parameters to use maybe? */ namespace args { +using namespace kel::lbm::sch; + using LbmArgsStruct = Struct< Member, @@ -46,9 +47,9 @@ kel::lbm::error_or lbm_main(const kel::lbm::data& args){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); @@ -197,7 +198,7 @@ kel::lbm::error_or k_main(int argc, char** argv){ kel::lbm::data args; { - auto eov = saw::parse_args(args,argc,argv); + auto eov = kel::lbm::parse_args(args,argc,argv); return eov; } auto& an = args.template get<"args">(); diff --git a/examples/poiseulle_particles_2d_hlbm_gpu/sim.cpp b/examples/poiseulle_particles_2d_hlbm_gpu/sim.cpp index 926086c..f96a022 100644 --- a/examples/poiseulle_particles_2d_hlbm_gpu/sim.cpp +++ b/examples/poiseulle_particles_2d_hlbm_gpu/sim.cpp @@ -45,7 +45,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "force">, Member, "porosity"> @@ -105,7 +105,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -280,9 +280,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/poiseulle_particles_2d_ibm_gpu/sim.cpp b/examples/poiseulle_particles_2d_ibm_gpu/sim.cpp index b051c4e..dd284dd 100644 --- a/examples/poiseulle_particles_2d_ibm_gpu/sim.cpp +++ b/examples/poiseulle_particles_2d_ibm_gpu/sim.cpp @@ -15,7 +15,7 @@ namespace lbm { constexpr uint64_t dim_y = 256ul; constexpr uint64_t dim_x = dim_y * 20ul; -// constexpr uint64_t particle_amount = 1ul; +constexpr uint64_t particle_amount = 1ul; namespace sch { @@ -45,7 +45,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "force"> >; @@ -56,11 +56,8 @@ using MacroStruct = Struct< //>; template -using ParticleSpheroidGroup = ParticleGroup< - T, - Desc::D, - sch::ParticleCollisionSpheroid ->; +using ParticleSpheroidGroup = + ParticleGroup>; } @@ -112,7 +109,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& force_f = macros.template get<"force">(); iterator::apply( @@ -152,20 +149,22 @@ kel::lbm::error_or setup_initial_conditions( // Particles { + // The radius is a per particle property now, and a lattice length. + kel::lbm::data>> rad_p; + rad_p.at({}).set(2.0); kel::lbm::data> dense_p; dense_p.at({}).set(1); auto& spheroid_grp = particles; - spheroid_grp = create_spheroid_particle_group( + spheroid_grp = create_spheroid_particle_group( + rad_p, dense_p, {32u} ); { auto& p = spheroid_grp.template get<"particles">(); - p = {{{1u}}}; - iterator<1u>::apply( [&](auto& index){ auto& p_ind = p.at(index); @@ -180,7 +179,7 @@ kel::lbm::error_or setup_initial_conditions( p_pos_old = p_pos; }, {}, - p.meta() + p.get_dims() ); } } @@ -217,13 +216,12 @@ kel::lbm::error_or step( q.submit([&](acpp::sycl::handler& h){ h.parallel_for(acpp::sycl::range<1u>{1u}, [=](acpp::sycl::id<1u> idx){ - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& dense_f = macros.template get<"density">(); auto& ps = particles; auto& mask = ps.template get<"mask">(); auto& mask_step = ps.template get<"mask_step">().at({}); - auto& p_dense = ps.template get<"density">().at({}); auto& com = ps.template get<"center_of_mass">(); auto& parts = ps.template get<"particles">(); @@ -235,7 +233,8 @@ kel::lbm::error_or step( /// In this case it's simple since I'm too lazy to do scaling and rotation /// Technically scale => rotate => translate /// Here it's only translate - auto& p_i_rb_pos = p_i_rb.template get<"position">(); + // Unwrapped: the IBM shift works in raw lattice offsets. + auto& p_i_rb_pos = p_i_rb.template get<"position">().handle(); iterator::apply([&](const auto& index){ /// Calculate the shift from the mask @@ -371,9 +370,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/poiseulle_particles_2d_psm_gpu/sim.cpp b/examples/poiseulle_particles_2d_psm_gpu/sim.cpp index 3fd5579..122fd9f 100644 --- a/examples/poiseulle_particles_2d_psm_gpu/sim.cpp +++ b/examples/poiseulle_particles_2d_psm_gpu/sim.cpp @@ -46,7 +46,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "force">, Member, "porosity"> @@ -123,7 +123,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -320,9 +320,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/poiseulle_particles_channel_2d/poiseulle_particles_channel_2d.cpp b/examples/poiseulle_particles_channel_2d/poiseulle_particles_channel_2d.cpp index aadeb12..44d874d 100644 --- a/examples/poiseulle_particles_channel_2d/poiseulle_particles_channel_2d.cpp +++ b/examples/poiseulle_particles_channel_2d/poiseulle_particles_channel_2d.cpp @@ -67,7 +67,7 @@ using CellStruct = Struct< template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, Member, Member, "force"> @@ -368,7 +368,7 @@ void couple_particles_to_lattice( // Interpolate this from close U cells. // For now pick the closest U - auto& closest_u = macros.at(p_cell_pos).template get<"momentum">(); + auto& closest_u = macros.at(p_cell_pos).template get<"velocity">(); // auto p_shift = closest_u; // p_pos - p_pos_old @@ -720,7 +720,7 @@ kel::lbm::error_or real_main(int argc, char** argv){ auto& macro_cell = macros.at(index); auto& rho = macro_cell.template get<"pressure">(); - auto& vel = macro_cell.template get<"momentum">(); + auto& vel = macro_cell.template get<"velocity">(); auto& force = macro_cell.template get<"force">(); auto& part_mask = macro_cell.template get<"particle">(); diff --git a/examples/schaefer_turek_durst_krause_rannbacher/common.hpp b/examples/schaefer_turek_durst_krause_rannbacher/common.hpp index cd5d944..74ef1b1 100644 --- a/examples/schaefer_turek_durst_krause_rannbacher/common.hpp +++ b/examples/schaefer_turek_durst_krause_rannbacher/common.hpp @@ -71,7 +71,7 @@ using RhoChunk = Chunk, 0u, dim_x*N, dim_y*N>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "porosity">, Member, "force"> diff --git a/examples/schaefer_turek_durst_krause_rannbacher/init.hpp b/examples/schaefer_turek_durst_krause_rannbacher/init.hpp index c8e2cc9..b6db4ea 100644 --- a/examples/schaefer_turek_durst_krause_rannbacher/init.hpp +++ b/examples/schaefer_turek_durst_krause_rannbacher/init.hpp @@ -57,7 +57,7 @@ kel::lbm::error_or init( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -94,8 +94,8 @@ kel::lbm::error_or init( ); { - kel::lbm::data> radius_p; - radius_p.at({}).set(conv.meter_si_to_lbm({{0.05}}).handle().get()); + kel::lbm::data>> radius_p; + radius_p.at({}).set(conv.meter_si_to_lbm({0.05}).handle().get()); std::cout<<"RADIUS: "<> dense_p; dense_p.at({}).set(1); @@ -107,8 +107,8 @@ kel::lbm::error_or init( auto& prb = p.template get<"rigid_body">(); auto& p_pos = prb.template get<"position">(); auto& p_posold = prb.template get<"position_old">(); - p_pos.at({{0u}}) = conv.meter_si_to_lbm({{0.16}}).handle(); - p_pos.at({{1u}}) = conv.meter_si_to_lbm({{0.2}}).handle(); + p_pos.at({{0u}}) = conv.meter_si_to_lbm({0.16}).handle(); + p_pos.at({{1u}}) = conv.meter_si_to_lbm({0.2}).handle(); p_posold = p_pos; } diff --git a/examples/schaefer_turek_durst_krause_rannbacher/sim.cpp b/examples/schaefer_turek_durst_krause_rannbacher/sim.cpp index 2d389d1..5446639 100644 --- a/examples/schaefer_turek_durst_krause_rannbacher/sim.cpp +++ b/examples/schaefer_turek_durst_krause_rannbacher/sim.cpp @@ -2,12 +2,13 @@ #include "init.hpp" #include "step.hpp" -#include /** * For deciding what parameters to use maybe? */ namespace args { +using namespace kel::lbm::sch; + using LbmArgsStruct = Struct< Member @@ -45,9 +46,9 @@ kel::lbm::error_or lbm_main(const kel::lbm::data& args){ converter conv { // delta_x - {{0.41 / (dim_y*N)}}, + {0.41 / (dim_y*N)}, // delta_t - {{3e-3/(N*N)}} + {3e-3/(N*N)} }; print_lbm_meta(conv,{1e-3},{0.3},{0.1}); @@ -203,7 +204,7 @@ kel::lbm::error_or k_main(int argc, char** argv){ kel::lbm::data args; { - auto eov = saw::parse_args(args,argc,argv); + auto eov = kel::lbm::parse_args(args,argc,argv); if(eov.is_error()){ return eov; } diff --git a/examples/schaefer_turek_durst_krause_rannbacher/step.hpp b/examples/schaefer_turek_durst_krause_rannbacher/step.hpp index 080d27d..adb0740 100644 --- a/examples/schaefer_turek_durst_krause_rannbacher/step.hpp +++ b/examples/schaefer_turek_durst_krause_rannbacher/step.hpp @@ -73,8 +73,8 @@ kel::lbm::error_or step( [&]() -> kel::lbm::data> { kel::lbm::data> vel; { - auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}}).handle(); + auto y_si = conv.meter_lbm_to_si(as_unit>(index.at({{1u}}).template cast_to())).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; @@ -149,8 +149,8 @@ kel::lbm::error_or step( [&]() -> kel::lbm::data> { kel::lbm::data> vel; { - auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}}).handle(); + auto y_si = conv.meter_lbm_to_si(as_unit>(index.at({{1u}}).template cast_to())).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; @@ -225,8 +225,8 @@ kel::lbm::error_or step( [&]() -> kel::lbm::data> { kel::lbm::data> vel; { - auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}}).handle(); + auto y_si = conv.meter_lbm_to_si(as_unit>(index.at({{1u}}).template cast_to())).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; diff --git a/examples/settling_cubes_2d_ibm_gpu/sim.cpp b/examples/settling_cubes_2d_ibm_gpu/sim.cpp index 50e6ce5..9c41fa9 100644 --- a/examples/settling_cubes_2d_ibm_gpu/sim.cpp +++ b/examples/settling_cubes_2d_ibm_gpu/sim.cpp @@ -44,17 +44,14 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "force"> >; template -using ParticleSpheroidGroup = ParticleGroup< - T, - Desc::D, - sch::ParticleCollisionSpheroid ->; +using ParticleSpheroidGroup = + ParticleGroup>; template using ParticleGroups = Tuple< @@ -90,9 +87,9 @@ kel::lbm::error_or setup_initial_conditions( {{1u,1u}} ); // - auto& df_f = fields.template gStarted hearing about similar cases not long after, many of which were successful on the part of the crooks. Scary stuff. et<"dfs_old">(); + auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); iterator::apply( [&](auto& index){ @@ -110,12 +107,16 @@ kel::lbm::error_or setup_initial_conditions( // Particles { + // Radius is per particle now, and a lattice length. + kel::lbm::data>> rad_p; + rad_p.at({}).set(2.0); kel::lbm::data> dense_p; dense_p.at({}).set(1); // auto& spheroid_group = particles.template get<0u>(); auto& spheroid_group = particles; - spheroid_group = create_spheroid_particle_group( + spheroid_group = create_spheroid_particle_group( + rad_p, dense_p, {64u} ); @@ -123,8 +124,6 @@ kel::lbm::error_or setup_initial_conditions( { auto& p = spheroid_group.template get<"particles">(); - p = {{{16u}}}; - iterator<1u>::apply( [&](auto& index){ // Set Pos here? @@ -139,7 +138,7 @@ kel::lbm::error_or setup_initial_conditions( p_pos_old = p_pos; }, {}, - p.meta() + p.get_dims() ); } } @@ -162,7 +161,7 @@ kel::lbm::error_or step( auto& parts = p_group.template get<"particles">(); auto& p_mask = p_group.template get<"mask">(); - auto& vels = macros.template get<"momentum">(); + auto& vels = macros.template get<"velocity">(); auto& forces = macros.template get<"force">(); auto p_meta = parts.meta(); @@ -189,13 +188,13 @@ kel::lbm::error_or step( auto& p = parts.at(index); auto& p_rb = p.template get<"rigid_body">(); - kel::lbm::data> delta_t; + kel::lbm::data>> delta_t; // verlet takes a lattice time delta_t.at({}).set(1.0f); - auto& p_pos = p_rb.template get<"position">(); + auto& p_pos = p_rb.template get<"position">().handle(); // raw: IBM works in lattice offsets auto& p_rot = p_rb.template get<"rotation">(); - auto& p_acc = p_rb.template get<"acceleration">(); + auto& p_acc = p_rb.template get<"acceleration">().handle(); // raw: the force below is not divided by mass yet (see TODO) iterator::apply( [&](auto& m_ind){ @@ -227,12 +226,12 @@ kel::lbm::error_or step( // this is our relative position to the particle auto rel_cell_to_part_pos = p_cell_pos_vec.template cast_to() - p_pos; - auto p_vel = (p_pos - p_rb.template get<"position_old">()) * delta_t; + auto p_vel = (p_pos - p_rb.template get<"position_old">().handle()) * delta_t.handle(); auto u_solid = p_vel + kel::lbm::math::cross(p_rot,rel_cell_to_part_pos); // Force - auto force = (u_solid - u_fluid) / delta_t; + auto force = (u_solid - u_fluid) / delta_t.handle(); // TODO HERE ATOMIC! !!!! forces.at(p_cell_pos) = forces.at(p_cell_pos) + force; @@ -295,13 +294,13 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ using dfi = df_info; - auto eo_lbm_env = lbm_directory(); - if(eo_lbm_env.is_error()){ - return std::move(eo_lbm_env.get_error()); + auto eo_lbm_dir = output_directory(); + if(eo_lbm_dir.is_error()){ + return std::move(eo_lbm_dir.get_error()); } - auto& lbm_env = eo_lbm_env.get_value(); + auto& lbm_dir = eo_lbm_dir.get_value(); - auto out_dir = lbm_env.data_dir / "settling_cubes_2d_ibm_gpu"; + auto out_dir = lbm_dir / "settling_cubes_2d_ibm_gpu"; { std::error_code ec; @@ -313,9 +312,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.05},{0.01},{0.4 * dim_y}); diff --git a/examples/settling_spheres_2d_hlbm_gpu/sim.cpp b/examples/settling_spheres_2d_hlbm_gpu/sim.cpp index 26c2ed8..201a47c 100644 --- a/examples/settling_spheres_2d_hlbm_gpu/sim.cpp +++ b/examples/settling_spheres_2d_hlbm_gpu/sim.cpp @@ -33,8 +33,8 @@ using ChunkStruct = Struct< Member, Member, "dfs">, Member, "dfs_old">, - Member, "p_N">, - Member, "p_D"> + Member, "particle_N">, + Member, "particle_D"> >; template @@ -48,8 +48,10 @@ using PorChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, + // HlbmOneParticleGather reads this one. + Member, "force">, Member, "porosity"> >; @@ -88,9 +90,9 @@ kel::lbm::error_or setup_initial_conditions( {{1u,1u}} ); // - auto& df_f = fields.template gStarted hearing about similar cases not long after, many of which were successful on the part of the crooks. Scary stuff. et<"dfs_old">(); + auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); iterator::apply( [&](auto& index){ @@ -108,12 +110,16 @@ kel::lbm::error_or setup_initial_conditions( // Particles { + // Radius is per particle now, and a lattice length. + kel::lbm::data>> rad_p; + rad_p.at({}).set(2.0); kel::lbm::data> dense_p; dense_p.at({}).set(1); // auto& spheroid_group = particles.template get<0u>(); - auto& spheroid_group = particles; + auto& spheroid_group = p_group; - spheroid_group = create_spheroid_particle_group( + spheroid_group = create_spheroid_particle_group( + rad_p, dense_p, {64u} ); @@ -121,9 +127,6 @@ kel::lbm::error_or setup_initial_conditions( { auto& p = spheroid_group.template get<"particles">(); - /// 16 if I remember correctly? - p = {{{particle_amount}}}; - iterator<1u>::apply( [&](auto& index){ // Set Pos here? @@ -138,13 +141,13 @@ kel::lbm::error_or setup_initial_conditions( p.at({{0u}}).set( 0.2 + 0.6 / (x-1u) ); p.at({{1u}}).set( 0.8 + y * 0.1 ); - p_pos = p; + p_pos.handle() = p; auto& p_pos_old = p_rb.template get<"position_old">(); p_pos_old = p_pos; }, {}, - p.meta() + p.get_dims() ); } } @@ -167,7 +170,7 @@ kel::lbm::error_or step( auto& parts = p_group.template get<"particles">(); auto& p_mask = p_group.template get<"mask">(); - auto& vels = macros.template get<"momentum">(); + auto& vels = macros.template get<"velocity">(); auto& forces = macros.template get<"force">(); auto p_meta = parts.meta(); @@ -196,9 +199,9 @@ kel::lbm::error_or step( auto& p_rot = pirb.template get<"rotation">(); auto& p_acc = pirb.template get<"acceleration">(); - kel::lbm::data> delta_t; + kel::lbm::data>> delta_t; delta_t.at({}).set(1.0f); - verlet_step_lambda(p,delta_t); + verlet_step_lambda(pi,delta_t); }); }).wait(); @@ -249,13 +252,13 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ using dfi = df_info; - auto eo_lbm_env = lbm_directory(); - if(eo_lbm_env.is_error()){ - return std::move(eo_lbm_env.get_error()); + auto eo_lbm_dir = output_directory(); + if(eo_lbm_dir.is_error()){ + return std::move(eo_lbm_dir.get_error()); } - auto& lbm_env = eo_lbm_env.get_value(); + auto& lbm_dir = eo_lbm_dir.get_value(); - auto out_dir = lbm_env.data_dir / "settling_spheres_2d_hlbm_gpu"; + auto out_dir = lbm_dir / "settling_spheres_2d_hlbm_gpu"; { std::error_code ec; @@ -267,9 +270,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.05},{0.01},{0.4 * dim_y}); diff --git a/examples/stdkr_2d_bgk/common.hpp b/examples/stdkr_2d_bgk/common.hpp index cac01fd..64fe124 100644 --- a/examples/stdkr_2d_bgk/common.hpp +++ b/examples/stdkr_2d_bgk/common.hpp @@ -57,7 +57,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density"> >; diff --git a/examples/stdkr_2d_bgk/init.hpp b/examples/stdkr_2d_bgk/init.hpp index 10edd9c..d37c8de 100644 --- a/examples/stdkr_2d_bgk/init.hpp +++ b/examples/stdkr_2d_bgk/init.hpp @@ -55,7 +55,7 @@ kel::lbm::error_or init( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); iterator::apply( [&](auto& index){ @@ -93,13 +93,13 @@ kel::lbm::error_or init( [&](const auto& index){ auto& info = info_f.at(index); kel::lbm::data> radius_p; - radius_p.at({}).set(conv.meter_si_to_lbm({{0.05}}).handle().get()); + radius_p.at({}).set(conv.meter_si_to_lbm({0.05}).handle().get()); kel::lbm::data> dense_p; dense_p.at({}).set(1); kel::lbm::data> middle,ind_vec; - middle.at({{0u}}) = conv.meter_si_to_lbm({{0.16}}).handle(); - middle.at({{1u}}) = conv.meter_si_to_lbm({{0.2}}).handle(); + middle.at({{0u}}) = conv.meter_si_to_lbm({0.16}).handle(); + middle.at({{1u}}) = conv.meter_si_to_lbm({0.2}).handle(); for(uint64_t i{0u}; i < Desc::D; ++i){ ind_vec.at({{i}}) = index.at({i}).template cast_to(); diff --git a/examples/stdkr_2d_bgk/sim.cpp b/examples/stdkr_2d_bgk/sim.cpp index 9b9b5a7..b388e3c 100644 --- a/examples/stdkr_2d_bgk/sim.cpp +++ b/examples/stdkr_2d_bgk/sim.cpp @@ -45,9 +45,9 @@ kel::lbm::error_or lbm_main(const kel::lbm::data& args){ converter conv { // delta_x - {{0.41 / dim_y}}, + {0.41 / dim_y}, // delta_t - {{1.067e-4}} + {1.067e-4} }; print_lbm_meta(conv,{1e-3},{1e-4},{0.5 * dim_y}); diff --git a/examples/stdkr_2d_bgk/step.hpp b/examples/stdkr_2d_bgk/step.hpp index 67f8486..d7d4fd4 100644 --- a/examples/stdkr_2d_bgk/step.hpp +++ b/examples/stdkr_2d_bgk/step.hpp @@ -48,7 +48,7 @@ kel::lbm::error_or step( kel::lbm::data> vel; { auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}}).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.41) * y_si.get() - (1.2 / (0.41*0.41)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; diff --git a/examples/stokes_drag_particle_2d_bgk_gpu/sim.cpp b/examples/stokes_drag_particle_2d_bgk_gpu/sim.cpp index ffa921f..38d059a 100644 --- a/examples/stokes_drag_particle_2d_bgk_gpu/sim.cpp +++ b/examples/stokes_drag_particle_2d_bgk_gpu/sim.cpp @@ -48,7 +48,7 @@ using ForceChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density"> >; @@ -121,7 +121,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); iterator::apply( [&](auto& index){ @@ -292,9 +292,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/stokes_drag_particle_2d_fplbm_gpu/sim.cpp b/examples/stokes_drag_particle_2d_fplbm_gpu/sim.cpp index 90a73cd..37551bc 100644 --- a/examples/stokes_drag_particle_2d_fplbm_gpu/sim.cpp +++ b/examples/stokes_drag_particle_2d_fplbm_gpu/sim.cpp @@ -48,7 +48,7 @@ using ForceChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "force">, Member, "porosity"> @@ -124,7 +124,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -299,9 +299,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/stokes_drag_particle_2d_hlbm_gpu/sim.cpp b/examples/stokes_drag_particle_2d_hlbm_gpu/sim.cpp index 8117b19..1d20b80 100644 --- a/examples/stokes_drag_particle_2d_hlbm_gpu/sim.cpp +++ b/examples/stokes_drag_particle_2d_hlbm_gpu/sim.cpp @@ -45,7 +45,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "force">, Member, "porosity"> @@ -121,7 +121,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -298,9 +298,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/stokes_drag_particle_2d_psm_gpu/sim.cpp b/examples/stokes_drag_particle_2d_psm_gpu/sim.cpp index 49a6128..fc9fdc1 100644 --- a/examples/stokes_drag_particle_2d_psm_gpu/sim.cpp +++ b/examples/stokes_drag_particle_2d_psm_gpu/sim.cpp @@ -46,7 +46,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "force">, Member, "porosity"> @@ -123,7 +123,7 @@ kel::lbm::error_or setup_initial_conditions( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -281,9 +281,9 @@ kel::lbm::error_or lbm_main(int argc, char** argv){ converter conv { // delta_x - {{1.0}}, + {1.0}, // delta_t - {{1.0}} + {1.0} }; print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); diff --git a/examples/throat_clog_2d/common.hpp b/examples/throat_clog_2d/common.hpp index 84baeb3..8fb9840 100644 --- a/examples/throat_clog_2d/common.hpp +++ b/examples/throat_clog_2d/common.hpp @@ -62,7 +62,7 @@ using RhoChunk = Chunk, 0u, dim_x, dim_y>; template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member, "density">, Member, "porosity">, Member, "force"> diff --git a/examples/throat_clog_2d/init.hpp b/examples/throat_clog_2d/init.hpp index e03bd5e..fab51b3 100644 --- a/examples/throat_clog_2d/init.hpp +++ b/examples/throat_clog_2d/init.hpp @@ -120,7 +120,7 @@ kel::lbm::error_or init( // auto& df_f = fields.template get<"dfs_old">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); iterator::apply( @@ -157,8 +157,8 @@ kel::lbm::error_or init( ); { - kel::lbm::data> radius_p; - radius_p.at({}).set(conv.meter_si_to_lbm({{0.05}}).handle().get()); + kel::lbm::data>> radius_p; + radius_p.at({}).set(conv.meter_si_to_lbm({0.05}).handle().get()); std::cout<<"RADIUS: "<> dense_p; dense_p.at({}).set(1); @@ -175,8 +175,8 @@ kel::lbm::error_or init( const auto radius = radius_p.at({}).get(); const auto pitch = radius * 2.4; - const auto x0 = conv.meter_si_to_lbm({{0.10}}).handle().get(); - const auto y_mid = conv.meter_si_to_lbm({{0.25}}).handle().get(); + const auto x0 = conv.meter_si_to_lbm({0.10}).handle().get(); + const auto y_mid = conv.meter_si_to_lbm({0.25}).handle().get(); constexpr uint64_t per_column = 4ul; diff --git a/examples/throat_clog_2d/sim.cpp b/examples/throat_clog_2d/sim.cpp index 89741ca..a25c1a3 100644 --- a/examples/throat_clog_2d/sim.cpp +++ b/examples/throat_clog_2d/sim.cpp @@ -33,9 +33,9 @@ kel::lbm::error_or lbm_main(std::string_view coupling){ converter conv { // delta_x - {{0.64 / dim_y}}, + {0.64 / dim_y}, // delta_t - {{1.067e-2}} + {1.067e-2} }; print_lbm_meta(conv,{1e-3},{1e-4},{0.2 * dim_y}); diff --git a/examples/throat_clog_2d/step.hpp b/examples/throat_clog_2d/step.hpp index 5bb889e..1605cf0 100644 --- a/examples/throat_clog_2d/step.hpp +++ b/examples/throat_clog_2d/step.hpp @@ -83,8 +83,8 @@ kel::lbm::error_or step( }()); } { - auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((0.1 / 0.64) * y_si.get() - (0.1 / (0.64*0.64)) * y_si.get() * y_si.get())}}).handle() * scale; + auto y_si = conv.meter_lbm_to_si(as_unit>(index.at({{1u}}).template cast_to())).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((0.1 / 0.64) * y_si.get() - (0.1 / (0.64*0.64)) * y_si.get() * y_si.get())}).handle() * scale; vel.at({{1u}}) = 0.0f; } return vel; @@ -178,8 +178,8 @@ kel::lbm::error_or step( [&]() -> kel::lbm::data> { kel::lbm::data> vel; { - auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((0.01 / 0.64) * y_si.get() - (0.01 / (0.64*0.64)) * y_si.get() * y_si.get())}}).handle(); + auto y_si = conv.meter_lbm_to_si(as_unit>(index.at({{1u}}).template cast_to())).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((0.01 / 0.64) * y_si.get() - (0.01 / (0.64*0.64)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; @@ -254,8 +254,8 @@ kel::lbm::error_or step( [&]() -> kel::lbm::data> { kel::lbm::data> vel; { - auto y_si = conv.meter_lbm_to_si(index.at({{1u}}).template cast_to()).handle(); - vel.at({{0u}}) = conv.velocity_si_to_lbm({{static_cast::type>((1.2 / 0.64) * y_si.get() - (1.2 / (0.64*0.64)) * y_si.get() * y_si.get())}}).handle(); + auto y_si = conv.meter_lbm_to_si(as_unit>(index.at({{1u}}).template cast_to())).handle(); + vel.at({{0u}}) = conv.velocity_si_to_lbm({static_cast::type>((1.2 / 0.64) * y_si.get() - (1.2 / (0.64*0.64)) * y_si.get() * y_si.get())}).handle(); vel.at({{1u}}) = 0.0f; } return vel; diff --git a/examples/vawt_3d_hlbm_gpu/.nix/derivation.nix b/examples/vawt_3d_hlbm_gpu/.nix/derivation.nix new file mode 100644 index 0000000..8d64547 --- /dev/null +++ b/examples/vawt_3d_hlbm_gpu/.nix/derivation.nix @@ -0,0 +1,41 @@ +{ lib +, stdenv +, scons +, clang-tools +, forstio +, python3 +, pname +, version +, adaptive-cpp +, kel +}: + +stdenv.mkDerivation { + pname = pname + "-examples-" + "vawt_3d_hlbm_gpu"; + inherit version; + src = ./..; + + nativeBuildInputs = [ + scons + clang-tools + python3 + ]; + + buildInputs = [ + forstio.core + forstio.async + forstio.codec + forstio.codec-unit + forstio.io + forstio.remote + forstio.remote-filesystem + forstio.codec-json + adaptive-cpp + kel.lbm.core + kel.lbm.sycl + ]; + + preferLocalBuild = true; + + outputs = [ "out" "dev" ]; +} diff --git a/examples/vawt_3d_hlbm_gpu/SConscript b/examples/vawt_3d_hlbm_gpu/SConscript new file mode 100644 index 0000000..aecf80a --- /dev/null +++ b/examples/vawt_3d_hlbm_gpu/SConscript @@ -0,0 +1,34 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +examples_env = env.Clone(); +examples_env['CXX'] = 'syclcc-clang'; +examples_env['CXXFLAGS'] += ['-O3']; + +examples_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +examples_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += examples_env.sources; +env.headers += examples_env.headers; + +# Cavity2D +examples_objects = []; +examples_env.add_source_files(examples_objects, ['sim.cpp'], shared=False); +examples_env.poiseulle_2d_gpu = examples_env.Program('#bin/vawt_3d_hlbm_gpu', [examples_objects]); + +# Set Alias +env.examples = [ + examples_env.poiseulle_2d_gpu +]; +env.Alias('examples', env.examples); +env.targets += ['examples']; +env.Install('$prefix/bin/', env.examples); diff --git a/examples/vawt_3d_hlbm_gpu/SConstruct b/examples/vawt_3d_hlbm_gpu/SConstruct new file mode 100644 index 0000000..0611b67 --- /dev/null +++ b/examples/vawt_3d_hlbm_gpu/SConstruct @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import glob +import re + + +if sys.version_info < (3,): + def isbasestring(s): + return isinstance(s,basestring) +else: + def isbasestring(s): + return isinstance(s, (str,bytes)) + +def add_kel_source_files(self, sources, filetype, lib_env=None, shared=False, target_post=""): + + if isbasestring(filetype): + dir_path = self.Dir('.').abspath + filetype = sorted(glob.glob(dir_path+"/"+filetype)) + + for path in filetype: + target_name = re.sub( r'(.*?)(\.cpp|\.c\+\+)', r'\1' + target_post, path ) + if shared: + target_name+='.os' + sources.append( self.SharedObject( target=target_name, source=path ) ) + else: + target_name+='.o' + sources.append( self.StaticObject( target=target_name, source=path ) ) + pass + +def isAbsolutePath(key, dirname, env): + assert os.path.isabs(dirname), "%r must have absolute path syntax" % (key,) + +env_vars = Variables( + args=ARGUMENTS +) + +env_vars.Add('prefix', + help='Installation target location of build results and headers', + default='/usr/local/', + validator=isAbsolutePath +) + +env_vars.Add('build_examples', + help='If examples should be built', + default="true" +) + +env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], + CPPDEFINES=['SAW_UNIX'], + CXXFLAGS=[ + '-std=c++20', + '-g', + '-Wall', + '-Wextra' + ], + LIBS=[ + 'forstio-core', + 'forstio-async', + 'forstio-io' + ] +); +env.__class__.add_source_files = add_kel_source_files +env.Tool('compilation_db'); +env.cdb = env.CompilationDatabase('compile_commands.json'); + +env.objects = []; +env.sources = []; +env.headers = []; +env.targets = []; + +Export('env') +SConscript('SConscript') + +env.Alias('cdb', env.cdb); +env.Alias('all', [env.targets]); +env.Default('all'); + +env.Alias('install', '$prefix') diff --git a/examples/vawt_3d_hlbm_gpu/sim.cpp b/examples/vawt_3d_hlbm_gpu/sim.cpp new file mode 100644 index 0000000..28d404b --- /dev/null +++ b/examples/vawt_3d_hlbm_gpu/sim.cpp @@ -0,0 +1,656 @@ +#include +#include +#include + +#include + +#include +#include +#include +#include + +namespace kel { +namespace lbm { + +// Deliberately tiny: 32 x 32 x 32 = 32768 cells, so a full run finishes in +// seconds. Square in y/z so the channel cross section is symmetric about the +// particle, which makes a stray transverse force obvious. +constexpr uint64_t dim_x = 32ul; +constexpr uint64_t dim_y = 32ul; +constexpr uint64_t dim_z = 32ul; + +constexpr uint64_t particle_amount = 1ul; + +/** + * The particle lives in lattice space, exactly like the fluid: the radius is a + * number of cells, the density is relative to the fluid's rho = 1, and the + * mass, inertia, force and torque that follow are all lattice quantities. The + * converter handles the fluid's SI <-> LBM bookkeeping and is deliberately not + * applied to any of this -- so every column of particle.csv is in cells, + * steps, and lattice mass. + */ +constexpr double particle_radius = 0.15 * static_cast(dim_y); +constexpr double particle_density = 1.0; + +namespace sch { + +/** + * The spin axis is an arbitrary fixed vector, not a lattice direction. It is + * normalised by the integrator, so these three need not be a unit vector. + * + * Defaults to +y: flow along x, axis vertical and perpendicular to it, blades + * sweeping the x-z plane -- the vertical-axis turbine arrangement. + */ +using LbmArgs = Args< + Struct< + Member, + Member, + Member, + Member, + Member, + Member + >, + Tuple<> +>; + +using InfoChunk = Chunk; + +template +using DfChunk = Chunk, 1u, dim_x, dim_y, dim_z>; + +template +using ScalarChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using VectorChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using ChunkStruct = Struct< + Member, + Member, "dfs">, + Member, "dfs_old">, + Member, "particle_N">, + Member, "particle_D"> +>; + +template +using MomChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +template +using RhoChunk = Chunk, 0u, dim_x, dim_y, dim_z>; + +/** + * "force" is not optional here: HlbmOneParticleGather reads it, so the 3D + * macro struct needs it the moment a particle is carried. + */ +template +using MacroStruct = Struct< + Member, "velocity">, + Member, "density">, + Member, "force">, + Member, "porosity"> +>; + +/** + * particle_aabb is specialised only on ParticleGroup, so a bare + * FixedArray> hits its always_false primary template. + */ +template +using ParticleSpheroidGroup = ParticleGroup>; +} + +template +kel::lbm::error_or setup_initial_conditions( + converter& conv, + kel::lbm::data>& fields, + kel::lbm::data>& macros, + kel::lbm::data>& particles +){ + // Unit conversion is not exercised in this reduced case. + (void) conv; + + auto& info_f = fields.template get<"info">(); + // Set everything as walls + iterator::apply( + [&](auto& index){ + info_f.at(index).set(1u); + }, + {}, + info_f.get_dims(), + {} + ); + // Fluid + iterator::apply( + [&](auto& index){ + info_f.at(index).set(2u); + }, + {}, + info_f.get_dims(), + {{1u,1u,1u}} + ); + + // Inflow + iterator::apply( + [&](auto& index){ + info_f.at(index).set(3u); + }, + {{0u,0u,0u}}, + {{1u,dim_y,dim_z}}, + {{0u,1u,1u}} + ); + + // Outflow + iterator::apply( + [&](auto& index){ + info_f.at(index).set(4u); + }, + {{dim_x-1u,0u,0u}}, + {{dim_x,dim_y,dim_z}}, + {{0u,1u,1u}} + ); + // + auto& df_f = fields.template get<"dfs_old">(); + auto& rho_f = macros.template get<"density">(); + auto& mom_f = macros.template get<"velocity">(); + auto& por_f = macros.template get<"porosity">(); + auto& force_f = macros.template get<"force">(); + + iterator::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + por_f.at(index).at({}) = {1}; + rho.at({}) = {1}; + auto& mom = mom_f.at(index); + auto eq = equilibrium(rho,mom); + + df = eq; + + auto& force = force_f.at(index); + for(uint64_t i{0u}; i < Desc::D; ++i){ + force.at({{i}}).set(0); + } + }, + {},// 0-index + df_f.get_dims() + ); + + iterator::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + rho.at({}) = {1}; + auto& mom = mom_f.at(index); + mom.at({{0u}}) = 0.01; + auto eq = equilibrium(rho,mom); + + df = eq; + }, + {},// 0-index + df_f.get_dims(), + {{1u,1u,1u}} + ); + + kel::lbm::data>> rad; + rad.at({}).set(particle_radius); + kel::lbm::data> dense; + dense.at({}).set(particle_density); + + // Mask resolution 16 in 3D is 16^3 = 4096 voxels. The analytic spheroid + // porosity is what HLBM actually calls, but the mask is still filled. + particles = create_spheroid_particle_group(rad,dense,{16u}); + { + auto& rb = particles.template get<"particles">().at({0u}).template get<"rigid_body">(); + auto& rbp = rb.template get<"position">(); + rbp.at({{0u}}) = 0.3 * dim_x; + rbp.at({{1u}}) = 0.5 * dim_y; + rbp.at({{2u}}) = 0.5 * dim_z; + + // position_old == position means the body starts at rest. It also + // makes a pinned body exact later: 2x - x_old + 0 == x. + rb.template get<"position_old">() = rbp; + + kel::lbm::data> zero; + for(uint64_t i{0u}; i < Desc::D; ++i){ + zero.at({{i}}).set(0); + } + // Acceleration is unit typed now, so its zero has to be too. + kel::lbm::data>> zero_acc; + for(uint64_t i{0u}; i < Desc::D; ++i){ + zero_acc.at({{i}}).set(0); + } + rb.template get<"acceleration">() = zero_acc; + + // In 3D these are Vector (a rotation vector), not a scalar angle. + rb.template get<"rotation">() = zero; + rb.template get<"rotation_old">() = zero; + rb.template get<"angular_acceleration">() = zero; + } + + return kel::lbm::make_void(); +} + +template +kel::lbm::error_or step( + converter& conv, + kel::lbm::data>,encode::Sycl>& fields, + kel::lbm::data>,encode::Sycl>& macros, + kel::lbm::data>,encode::Sycl>& particles, + kel::lbm::data t_i, + kel::lbm::data sub_steps, + const kel::lbm::data>& spin_axis, + device& dev +){ + auto& q = dev.get_handle(); + auto& info_f = fields.template get<"info">(); + + // Porosity back to 1 (all fluid) before the particle restamps itself. + q.submit([&](acpp::sycl::handler& h){ + component> hlbm_reset; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y,dim_z}, [=](acpp::sycl::id idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + hlbm_reset.apply(fields,macros,index,t_i); + }); + }).wait(); + + /** + * One work item per particle. The stamp has to come after the integrate: + * it writes the post-integration surface velocity back onto the lattice. + */ + q.submit([&](acpp::sycl::handler& h){ + component> p_reset; + component> p_gather; + component> p_stamp; + + /** + * The rotor replaces the free-body integrator. No contact pass: the + * body is pinned, so it cannot reach a wall or another particle. + */ + component> p_integrate{spin_axis}; + + h.parallel_for(acpp::sycl::range<1u>{particle_amount}, [=](acpp::sycl::id<1u> idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < 1u; ++i){ + index.at({{i}}).set(idx[i]); + } + + p_reset.apply(particles,index,t_i); + p_gather.apply(fields,macros,particles,index,t_i,sub_steps); + p_integrate.apply(particles,index,t_i,sub_steps); + p_stamp.apply(fields,macros,particles,index,t_i,sub_steps); + }); + }).wait(); + + // auto coll_ev = + q.submit([&](acpp::sycl::handler& h){ + /** + * Hlbm rather than BGK: it is the collision that consumes the + * porosity and particle_N/particle_D the stamp pass writes. + */ + component> collision{conv.template kinematic_viscosity_si_to_tau({0.1})}; + component> bb; + + component,encode::Sycl> flow_in{ + [&](){ + uint64_t target_t_i = 256u; + if(t_i.get() < target_t_i){ + return 1.0 + (0.001 / target_t_i) * t_i.get(); + } + return 1.001; + }() + }; + component,encode::Sycl> flow_out{1.0}; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y,dim_z}, [=](acpp::sycl::id idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + auto info = info_f.at(index); + + switch(info.get()){ + case 0u: + break; + case 1u: + bb.apply(fields,index,t_i); + break; + case 2u: + collision.apply(fields,macros,index,t_i); + break; + case 3u: + flow_in.apply(fields,index,t_i); + collision.apply(fields,macros,index,t_i); + break; + case 4u: + flow_out.apply(fields,index,t_i); + collision.apply(fields,macros,index,t_i); + break; + default: + break; + } + }); + }).wait(); + + q.submit([&](acpp::sycl::handler& h){ + component> stream; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y,dim_z}, [=](acpp::sycl::id idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + auto info = info_f.at(index); + + if(info.get() > 0u){ + stream.apply(fields,index,t_i); + } + }); + }).wait(); + + // Step + /* + q.submit([&](acpp::sycl::handler& h){ + // h.depends_on(collision_ev); + }).wait(); + */ + + return kel::lbm::make_void(); +} +} +} + +template +kel::lbm::error_or lbm_main(int argc, char** argv){ + using namespace kel::lbm; + + kel::lbm::data args; + { + auto& args_n = args.template get<"args">(); + auto& name = args_n.template get<"name">(); + name.set("vawt_3d_hlbm_gpu"); + auto& steps_arg = args_n.template get<"time_steps">(); + steps_arg.set(256u); + auto& sub_arg = args_n.template get<"sub_steps">(); + sub_arg.set(16u); + + // +y by default: flow along x, axis perpendicular to it. + args_n.template get<"axis_x">().set(0.0); + args_n.template get<"axis_y">().set(1.0); + args_n.template get<"axis_z">().set(0.0); + } + { + auto eov = kel::lbm::parse_args(args,argc,argv); + if(eov.is_error()){ + return eov; + } + } + auto eo_lbm_env = init_lbm_env(args); + if(eo_lbm_env.is_error()){ + return std::move(eo_lbm_env.get_error()); + } + const auto& lbm_env = eo_lbm_env.get_value(); + + auto& out_dir = lbm_env.name_dir; + + { + std::error_code ec; + std::filesystem::create_directories(out_dir,ec); + if(ec != std::errc{}){ + return kel::lbm::make_error("Could not create output directory"); + } + } + + converter conv { + // delta_x + {1.0}, + // delta_t + {1.0} + }; + + // Characteristic length is the body diameter -- the scale that sets Re here. + print_lbm_meta(conv,{0.1},{0.01},{2.0 * particle_radius}); + + auto lbm_data_ptr = kel::lbm::heap>>(); + auto lbm_macro_data_ptr = kel::lbm::heap>>(); + auto lbm_particle_data_ptr = kel::lbm::heap>>(); + + std::cout<<"Estimated Bytes: "<,sch::MacroStruct>().get()<( + std::string{eo_aio.get_error().get_message()}); + } + auto& aio = eo_aio.get_value(); + saw::wait_scope wait{aio.event_loop}; + + bool krun = true; + bool print_status = false; + aio.event_port.on_signal(saw::Signal::Terminate).then([&](){ + krun = false; + }).detach(); + aio.event_port.on_signal(saw::Signal::User1).then([&](){ + print_status = true; + }).detach(); + + device dev; + + auto& sycl_q = dev.get_handle(); + + sycl_q.wait(); + { + auto eov = setup_initial_conditions(conv,*lbm_data_ptr,*lbm_macro_data_ptr,*lbm_particle_data_ptr); + if(eov.is_error()){ + return eov; + } + } + + kel::lbm::data, encode::Sycl> lbm_sycl_data{sycl_q}; + kel::lbm::data, encode::Sycl> lbm_sycl_macro_data{sycl_q}; + kel::lbm::data, encode::Sycl> lbm_sycl_particle_data{sycl_q}; + sycl_q.wait(); + + auto lsd_view = make_view(lbm_sycl_data); + auto lsdm_view = make_view(lbm_sycl_macro_data); + auto lsdp_view = make_view(lbm_sycl_particle_data); + { + auto eov = dev.copy_to_device(*lbm_data_ptr,lbm_sycl_data); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_device(*lbm_macro_data_ptr,lbm_sycl_macro_data); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_device(*lbm_particle_data_ptr,lbm_sycl_particle_data); + if(eov.is_error()){ + return eov; + } + } + sycl_q.wait(); + kel::lbm::data time_steps = args.template get<"args">().template get<"time_steps">(); + kel::lbm::data sub_steps = args.template get<"args">().template get<"sub_steps">(); + + kel::lbm::data> spin_axis; + { + auto& args_n = args.template get<"args">(); + spin_axis.at({{0u}}).set(args_n.template get<"axis_x">().get()); + spin_axis.at({{1u}}).set(args_n.template get<"axis_y">().get()); + spin_axis.at({{2u}}).set(args_n.template get<"axis_z">().get()); + } + + // Unit axis, for projecting the rotation and torque onto it below. + auto axis_hat = kel::lbm::math::normalize(spin_axis); + + /** + * Particle time series. write_vtk_file only accepts a struct of chunks, so + * a particle group cannot go through it -- a CSV beside the VTK output is + * the simpler answer, and it is what you actually plot. + * + * Velocity and angular velocity are not stored on the body: the scheme is + * position-Verlet, so both are recovered as (now - previous) * steps. + */ + std::ofstream particle_csv{out_dir / "particle.csv"}; + if(not particle_csv.is_open()){ + return kel::lbm::make_error("Could not open particle.csv"); + } + /** + * theta/omega/alpha/tau are the rotation, angular velocity, angular + * acceleration and torque projected onto the spin axis -- the rotor + * numbers. drag is the streamwise force. The raw vectors follow so a + * stray off-axis component is still visible rather than hidden by the + * projection. + */ + particle_csv + <<"step,theta,omega,alpha,tau,drag," + <<"x,y,z,vx,vy,vz,fx,fy,fz," + <<"rot_x,rot_y,rot_z,omega_x,omega_y,omega_z," + <<"alpha_x,alpha_y,alpha_z,tau_x,tau_y,tau_z\n"; + + auto write_particle_row = [&](uint64_t step_i){ + auto& rb = lbm_particle_data_ptr->template get<"particles">() + .at({0u}).template get<"rigid_body">(); + + auto& pos = rb.template get<"position">(); + auto& pos_old = rb.template get<"position_old">(); + auto& force = rb.template get<"force">(); + auto& rot = rb.template get<"rotation">(); + auto& rot_old = rb.template get<"rotation_old">(); + auto& ang_acc = rb.template get<"angular_acceleration">(); + auto& torque = rb.template get<"torque">(); + + const auto steps = static_cast(particle_sub_steps(sub_steps)); + + // Project onto the spin axis. + double theta = 0.0, omega = 0.0, alpha = 0.0, tau = 0.0; + for(uint64_t k{0u}; k < 3u; ++k){ + const double a = axis_hat.at({{k}}).get(); + theta += rot.at({{k}}).get() * a; + omega += (rot.at({{k}}).get() - rot_old.at({{k}}).get()) * steps * a; + alpha += ang_acc.at({{k}}).get() * a; + tau += torque.at({{k}}).get() * a; + } + + particle_csv + < i{0u}; i < time_steps and krun; ++i){ + { + { + auto eov = write_vtk_file(out_dir,"t",i.get(),*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + } + { + auto eov = step(conv,lsd_view,lsdm_view,lsdp_view,i,sub_steps,spin_axis,dev); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_host(lbm_sycl_particle_data,*lbm_particle_data_ptr); + if(eov.is_error()){ + return eov; + } + } + write_particle_row(i.get()); + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = dev.copy_to_host(lbm_sycl_data,*lbm_data_ptr); + if(eov.is_error()){ + return eov; + } + } + + wait.poll(); + if(print_status){ + std::cout<<"Status: "<().get() * 100 / time_steps.get())<<"%"<::apply( + [&](auto& index){ + std::cout<().at(index).template cast_to().get()<<"\n"; + }, + {{0u,0u}}, + {{dim_x, dim_y}} + ); + */ + + sycl_q.wait(); + return kel::lbm::make_void(); +} + +using FloatT = kel::lbm::sch::Float32; + +int main(int argc, char** argv){ + auto eov = lbm_main(argc, argv); + if(eov.is_error()){ + auto& err = eov.get_error(); + std::cerr<<"[Error] "< 0u){ + std::cerr<<" - "<, Encoding> &input) { return out; } +/** + * Square root of a unit carrying scalar: the exponents halve, so the root of + * an area is a length. + * + * Only defined when every exponent is even -- a half power of a base unit is + * not expressible here, and silently truncating would be worse than refusing. + */ +template +data, sch::UnitElement...>, Encoding> +sqrt(const data, sch::UnitElement...>, + Encoding> &inp) { + static_assert(((E % 2 == 0) && ...), + "sqrt of a unit needs even exponents"); + return data, sch::UnitElement...>, + Encoding>{sqrt(inp.handle())}; +} + +/** + * Unit aware vector algebra. + * + * A unit belongs to the vector as a whole (see unit.hpp), so each of these + * recovers the raw vector, reuses the plain implementation above, and then + * re-attaches the unit the result actually carries. The result unit is + * computed rather than copied: the dot product of two lengths is an area, and + * a normalised vector is a direction and therefore dimensionless. + */ +template +auto dot(const data, EleL...>, Encoding> &left, + const data, EleR...>, Encoding> &right) { + using ResultSchema = + typename kel::lbm::unit_multiplication, EleL...>, + sch::Unit, EleR...>>::Schema; + return data{ + dot(left.handle(), right.handle())}; +} + +/** + * Length of a unit carrying vector. sqrt of an area is a length, so the unit + * is unchanged. + */ +template +data, Ele...>, Encoding> +norm(const data, Ele...>, Encoding> &input) { + return data, Ele...>, Encoding>{ + sqrt(dot(input.handle(), input.handle()))}; +} + +/** + * A direction is dimensionless, and dimensionless is spelled Pure -- a bare + * Vector would only mean "carries no annotation", which is a different claim. + */ +template +data>, Encoding> +normalize(const data, Ele...>, Encoding> &input) { + return data>, Encoding>{ + normalize(input.handle())}; +} + /** * Reinterprets a plain fixed array as a vector, so the algebra below applies * to it. diff --git a/modules/core/c++/abstract/unit.hpp b/modules/core/c++/abstract/unit.hpp index 406cb2f..b3d7391 100644 --- a/modules/core/c++/abstract/unit.hpp +++ b/modules/core/c++/abstract/unit.hpp @@ -10,11 +10,21 @@ namespace lbm { namespace sch { /** - * One factor of a unit: a base quantity raised to `Expo`, at scale `Scal`. + * One factor of a unit: a base quantity raised to `Expo`, at decimal scale + * `Scal`. * - * Scaling distinguishes units of the same quantity that are not - * interchangeable, e.g. lattice metres from SI metres, so that mixing them is - * a compile time error rather than a silent numeric mistake. + * `Scal` is an SI prefix exponent -- nano -9, micro -6, milli -3, none 0, kilo + * 3, mega 6, giga 9. It is a pure tag: the stored number is the value as + * written, so 2 km holds 2.0 and nothing is ever multiplied by a power of ten. + * + * It is NOT what separates lattice from SI. That comes from the base tag type + * itself -- `lbm_type::meter` and `si_type::meter` are distinct types, so + * mixing them cannot compile regardless of scale (see lbm_unit.hpp). + * + * Two factors fold their exponents only when base AND scale match + * (unit_matching_reduce below). Differently scaled quantities therefore do not + * add: km + m is a compile error by type identity. Implicit rescaling is + * deliberately absent -- add it here if it is ever wanted. */ template struct UnitElement { using Schema = Un; @@ -43,21 +53,19 @@ namespace impl { template class unit_matching; +/** + * A plain type list of UnitElements, used only to carry a pack around while + * the factors are being reduced. It holds no members: the specialisation below + * exists purely so that a non empty pack does not hit this assert. + */ template class unit_redux_list { static_assert(sizeof...(T) == 0u, "Template type not supported"); - - using Type = unit_redux_list<>; }; template struct unit_redux_list, - sch::UnitElement...> { - using Type = typename unit_matching< - unit_redux_list, - sch::UnitElement...>, - unit_redux_list<>>::TypeList; -}; + sch::UnitElement...> {}; template class unit_matching_reduce { public: @@ -108,7 +116,6 @@ public: using Type = sch::UnitElement; using TypeList = unit_redux_list...>; static constexpr int64_t Num = E; - static constexpr int64_t Scale = S; }; template class unit_matching { @@ -141,6 +148,85 @@ public: using TypeList = typename unit_matching::TypeList; }; +/** + * Whether a schema is a Unit at all. Lets the arithmetic below tell a united + * operand from a bare one. + */ +template struct is_unit_schema : std::false_type {}; + +template +struct is_unit_schema> : std::true_type {}; + +/** + * Splits a Unit into its base schema and its component list, so the list can + * be re-hosted on a different base. + * + * Needed because multiplying a unit vector by a unit scalar changes the base + * (Vector * Scalar -> Vector) while the component lists still have to be + * combined against each other. unit_multiplication requires both operands to + * share a base, so both are rebased onto the result first. + */ +template struct unit_parts; + +template struct unit_parts> { + using Base = B; + template using Rebase = sch::Unit; +}; + +/** + * A total order on unit factors, so that the same physical unit is always the + * same C++ type. + * + * Without this the reduction keeps encounter order, and m * s^-1 produces a + * different type from s^-1 * m -- the same quantity reached two ways then + * fails to match, and the mismatch shows up as a baffling template error far + * from its cause. Sorting by the base tag name gives a canonical form; the + * scale breaks ties so that factors differing only in prefix still order + * deterministically. + */ +template struct unit_element_less; + +template +struct unit_element_less, sch::UnitElement> { + static constexpr bool value = + (TA::name != TB::name) ? (TA::name < TB::name) : (SA < SB); +}; + +template struct unit_prepend; + +template +struct unit_prepend> { + using Type = unit_redux_list; +}; + +template struct unit_insert; + +template struct unit_insert> { + using Type = unit_redux_list; +}; + +template +struct unit_insert> { + using Type = typename std::conditional< + unit_element_less::value, + unit_redux_list, + typename unit_prepend< + Head, + typename unit_insert>::Type>::Type>::type; +}; + +template struct unit_sort; + +template <> struct unit_sort> { + using Type = unit_redux_list<>; +}; + +template +struct unit_sort> { + using Type = typename unit_insert< + Head, typename unit_sort>::Type>::Type; +}; + template class unit_add_base { static_assert(always_false, "Template type not supported"); }; @@ -154,24 +240,47 @@ public: } // namespace impl template struct unit_element_reduction { - using TypeList = + // Sorted after reducing, so the result is canonical regardless of the + // order the factors arrived in. + using TypeList = typename impl::unit_sort< typename impl::unit_matching, - impl::unit_redux_list<>>::TypeList; + impl::unit_redux_list<>>::TypeList>::Type; using Schema = typename impl::unit_add_base::Schema; }; +/** + * Labels a bare value with a unit. + * + * The deliberate way to cross from untyped to typed now that the wrapping + * constructor is explicit. Being a named function, every such crossing is + * greppable instead of happening silently at a call site. + */ +template +constexpr data as_unit(const data &value) { + return data{value}; +} + template struct unit_multiplication { static_assert(always_false, "Template type not supported"); }; +/** + * The scale packs have to be deduced explicitly. Writing + * `sch::UnitElement` here would mean + * `sch::UnitElement` after the default argument is + * substituted, so the specialisation would only ever match zero scaled factors + * and anything prefixed would fall through to the always_false primary. + */ template + int64_t... UnitS, typename... UnitRhsT, int64_t... UnitRhsE, + int64_t... UnitRhsS> struct unit_multiplication< - sch::Unit...>, - sch::Unit...>> { + sch::Unit...>, + sch::Unit...>> { using Schema = typename unit_element_reduction< - BaseSchema, sch::UnitElement..., - sch::UnitElement...>::Schema; + BaseSchema, sch::UnitElement..., + sch::UnitElement...>::Schema; }; template struct unit_division { @@ -180,15 +289,21 @@ template struct unit_division { /** * Division is multiplication by the negated exponents. + * + * The scale is deliberately NOT negated. Scale says which base unit is meant + * -- km rather than m -- while the exponent says what power it is raised to. + * So (km)^-1 is scale 3, exponent -1; negating the scale as well would quietly + * turn it into (mm)^-1. The symmetry with the exponent is only apparent. */ template + int64_t... UnitS, typename... UnitRhsT, int64_t... UnitRhsE, + int64_t... UnitRhsS> struct unit_division< - sch::Unit...>, - sch::Unit...>> { + sch::Unit...>, + sch::Unit...>> { using Schema = typename unit_element_reduction< - BaseSchema, sch::UnitElement..., - sch::UnitElement...>::Schema; + BaseSchema, sch::UnitElement..., + sch::UnitElement...>::Schema; }; /** @@ -212,16 +327,32 @@ public: KEL_DEFAULT_COPY(data); KEL_DEFAULT_MOVE(data); - data(const BaseData &dat__) : dat_{dat__} {} +/** + * Explicit: an already built value of the base type does not get to pick + * up a unit just because a unit was expected there. That is how an + * untyped sub step scalar silently became a time. + */ + explicit data(const BaseData &dat__) : dat_{dat__} {} + + explicit data(BaseData &&dat__) : dat_{std::move(dat__)} {} - data(BaseData &&dat__) : dat_{std::move(dat__)} {} + /** + * A bare literal is still allowed to become a united quantity at a + * construction site, because that is where the author is stating the + * unit. Only primitives, so this never competes with the base copy above + * for tensor valued units. + */ + template + requires(sch::is_primitive::value && std::is_arithmetic_v) + data(V value__) + : dat_{static_cast::type>(value__)} {} data operator+(const data &rhs) const { - return {dat_ + rhs.handle()}; + return data{dat_ + rhs.handle()}; } data operator-(const data &rhs) const { - return {dat_ - rhs.handle()}; + return data{dat_ - rhs.handle()}; } data &operator+=(const data &rhs) { @@ -250,22 +381,68 @@ public: return dat_ > rhs.handle(); } +/** + * The operands need not share a base schema: a length vector times a + * dimensionless scalar is a length vector. The base of the result is + * whatever the underlying arithmetic produces, and both component lists + * are rebased onto it before being combined. + */ template - data::Schema, enc::Native> - operator*(const data &rhs) const { - return {dat_ * rhs.handle()}; + requires impl::is_unit_schema::value + auto operator*(const data &rhs) const { + auto raw = dat_ * rhs.handle(); + using ResultBase = typename std::remove_cvref_t::Schema; + using ResultSchema = typename unit_multiplication< + typename impl::unit_parts::template Rebase, + typename impl::unit_parts::template Rebase>::Schema; + return data{raw}; } template - data::Schema, enc::Native> - operator/(const data &rhs) const { - return {dat_ / rhs.handle()}; + requires impl::is_unit_schema::value + auto operator/(const data &rhs) const { + auto raw = dat_ / rhs.handle(); + using ResultBase = typename std::remove_cvref_t::Schema; + using ResultSchema = typename unit_division< + typename impl::unit_parts::template Rebase, + typename impl::unit_parts::template Rebase>::Schema; + return data{raw}; } /** * The underlying value, with the unit stripped. Mostly for the * multiplication and division operators above. */ +/** + * Element access forwards to the underlying tensor and returns the raw + * element. + * + * The unit belongs to the quantity as a whole, not to one component -- a + * position is metres in every component -- so there is nothing distinct to + * attach per element. The wrapper is also not stored per element, so a + * reference to a unit typed component could not be formed even if it were + * wanted. Mixing is still caught, just at the vector level. + */ + /** + * The rank is taken through a defaulted template parameter so that it is + * a substitution failure, not a hard error, when the base is not a tensor. + * The parameter type itself stays concrete, because a braced index like + * at({{0u}}) cannot be deduced against a template parameter. + */ + template + auto &at(const index_data &index) { + return dat_.at(index); + } + + template + const auto &at(const index_data &index) const { + return dat_.at(index); + } + + auto &flat_at(uint64_t i) { return dat_.flat_at(i); } + + const auto &flat_at(uint64_t i) const { return dat_.flat_at(i); } + const BaseData &handle() const { return dat_; } BaseData &handle() { return dat_; } diff --git a/modules/core/c++/abstract/unit_print.hpp b/modules/core/c++/abstract/unit_print.hpp index 049e54f..2cb2ba6 100644 --- a/modules/core/c++/abstract/unit_print.hpp +++ b/modules/core/c++/abstract/unit_print.hpp @@ -1,13 +1,66 @@ #pragma once #include +#include #include "unit.hpp" +namespace kel { +namespace lbm { +namespace impl { +/** + * SI prefix symbol for a decimal scale exponent. + * + * Returns an empty view for the unprefixed case and for anything outside the + * table, which the caller renders as an explicit 10^n instead. Printing a + * scaled unit identically to an unscaled one would be worse than not printing + * it at all, so there is no silent fallback. + */ +inline constexpr std::string_view unit_prefix_symbol(int64_t scale) { + switch (scale) { + case -9: + return "n"; + case -6: + return "u"; + case -3: + return "m"; + case 0: + return ""; + case 3: + return "k"; + case 6: + return "M"; + case 9: + return "G"; + default: + return ""; + } +} + +inline constexpr bool unit_prefix_known(int64_t scale) { + return scale == -9 || scale == -6 || scale == -3 || scale == 0 || + scale == 3 || scale == 6 || scale == 9; +} +} // namespace impl +} // namespace lbm +} // namespace kel + namespace std { -template +/** + * The scale has to be deduced, not defaulted: writing UnitElement here + * would only ever match zero scaled factors, and a prefixed one would fail to + * bind at all. + */ +template inline ostream &operator<<(ostream &o, - const ::kel::lbm::sch::UnitElement &) { + const ::kel::lbm::sch::UnitElement &) { + if constexpr (US != 0) { + if constexpr (::kel::lbm::impl::unit_prefix_known(US)) { + o << ::kel::lbm::impl::unit_prefix_symbol(US); + } else { + o << "10^(" << US << ')' << '*'; + } + } o << UT::short_name; if constexpr (UE != 1) { o << '^' << '(' << UE << ')'; @@ -23,16 +76,19 @@ template struct unit_print { static_assert(always_false, "Template type not supported"); }; -template -struct unit_print, sch::UnitElement...> { +template +struct unit_print, + sch::UnitElement...> { static std::ostream &print(std::ostream &o) { - sch::UnitElement element; + sch::UnitElement element; std::ostream &o_ret = o << element; if constexpr (sizeof...(UTL) > 0) { std::ostream &o_ret_next = o_ret << ' ' << '*' << ' '; - return unit_print...>::print(o_ret_next); + return unit_print...>::print( + o_ret_next); } return o_ret << ']'; @@ -44,18 +100,19 @@ struct unit_print, sch::UnitElement...> { namespace std { template + int64_t... UnitE, int64_t... UnitS> inline ostream & operator<<(ostream &o, const ::kel::lbm::data< ::kel::lbm::sch::Unit< - StorageT, ::kel::lbm::sch::UnitElement...>, + StorageT, + ::kel::lbm::sch::UnitElement...>, Encode> &unit) { o << unit.handle().get(); if constexpr (sizeof...(UnitT) > 0) { auto &o_ret = o << ' ' << '['; return ::kel::lbm::impl::unit_print< - ::kel::lbm::sch::UnitElement...>::print(o_ret); + ::kel::lbm::sch::UnitElement...>::print(o_ret); } return o; } diff --git a/modules/core/c++/boundary.hpp b/modules/core/c++/boundary.hpp index d9ac941..7e5e65b 100644 --- a/modules/core/c++/boundary.hpp +++ b/modules/core/c++/boundary.hpp @@ -230,6 +230,22 @@ template class component, cmpt::ZouHePressureX, Encode> final { public: using Descriptor = sch::Descriptor<3u,27u>; +private: + /** + * The flag has to mean the same thing here as it does in the D2Q9 + * specialisation above, or the same call site picks a different wall + * depending on the descriptor. + * + * There, Dir == true reconstructs the populations with cx = +1, which are + * the unknowns at the x = 0 inlet. The body below is written for the + * opposite wall (unknowns with cx = -1, i.e. x = max), so the sense is + * flipped once here instead of at each branch. + * + * This used to be inverted, and since every example passes true for the + * inlet -- correct for D2Q9 -- the 3D cases drove the wrong face and + * diverged. + */ + static constexpr bool AtXMax = not East; private: kel::lbm::data rho_setting_; public: @@ -290,7 +306,7 @@ public: kel::lbm::data S; - if constexpr (East) { + if constexpr (AtXMax) { S = // cx = 0 @@ -347,10 +363,24 @@ public: kel::lbm::data rho_ux; - if constexpr (East) { - rho_ux = rho_setting_ - S; - } else { + /* + * With S = S0 + 2*S_known: + * + * x = max (unknown cx = -1): + * S_unknown = rho - S0 - S_known + * rho*ux = S_known - S_unknown = S - rho + * + * x = 0 (unknown cx = +1): + * rho*ux = S_unknown - S_known = rho - S + * + * Both of these were inverted, which drove the inlet and outlet the wrong + * way round and pumped mass into the domain every step. The D2Q9 + * specialisation above already has the correct signs. + */ + if constexpr (AtXMax) { rho_ux = S - rho_setting_; + } else { + rho_ux = rho_setting_ - S; } @@ -378,7 +408,7 @@ public: kel::lbm::data rho_uy; kel::lbm::data rho_uz; - if constexpr (East) { + if constexpr (AtXMax) { rho_uy = // cx = 0 @@ -506,7 +536,7 @@ public: * ============================================================ */ - if constexpr (East) { + if constexpr (AtXMax) { /* * -------------------------------------------------------- @@ -516,7 +546,7 @@ public: f.at({1u}) = f.at({2u}) - - kel::lbm::data{1.0 / 6.0} * rho_ux; + - kel::lbm::data{4.0 / 9.0} * rho_ux; /* @@ -624,7 +654,7 @@ public: f.at({2u}) = f.at({1u}) - + kel::lbm::data{1.0 / 6.0} * rho_ux; + + kel::lbm::data{4.0 / 9.0} * rho_ux; /* diff --git a/modules/core/c++/collision.hpp b/modules/core/c++/collision.hpp index d3e70cb..bf83994 100644 --- a/modules/core/c++/collision.hpp +++ b/modules/core/c++/collision.hpp @@ -81,7 +81,7 @@ public: auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); kel::lbm::data>& rho = rho_f.at(index); kel::lbm::data>& vel = vel_f.at(index); @@ -141,7 +141,7 @@ public: auto& dfs = dfs_old_f.at(index); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); kel::lbm::data>& rho = rho_f.at(index); diff --git a/modules/core/c++/component/psm.hpp b/modules/core/c++/component/psm.hpp index e3ffa67..de71228 100644 --- a/modules/core/c++/component/psm.hpp +++ b/modules/core/c++/component/psm.hpp @@ -39,7 +39,7 @@ public: auto& porous_f = macros.template get<"porosity">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); kel::lbm::data>& rho = rho_f.at(index); kel::lbm::data>& vel = vel_f.at(index); diff --git a/modules/core/c++/converter.hpp b/modules/core/c++/converter.hpp index 93c7617..a3c348d 100644 --- a/modules/core/c++/converter.hpp +++ b/modules/core/c++/converter.hpp @@ -19,7 +19,7 @@ private: kel::lbm::data, sch::LbmSecond >::Schema > second_conv_; public: converter() = delete; - converter( + explicit converter( kel::lbm::data, sch::LbmMeter >::Schema > meter_conv__, kel::lbm::data, sch::LbmSecond >::Schema > second_conv__ ): diff --git a/modules/core/c++/descriptor/d3q27.hpp b/modules/core/c++/descriptor/d3q27.hpp index 9358852..4190f15 100644 --- a/modules/core/c++/descriptor/d3q27.hpp +++ b/modules/core/c++/descriptor/d3q27.hpp @@ -47,37 +47,49 @@ public: { 1, 1, 1} // 26 }}; + /** + * D3Q27 weights, keyed on |e_i|^2: + * + * rest (|e|^2 = 0, 1 of them) 8/27 + * face (|e|^2 = 1, 6 of them) 2/27 + * edge (|e|^2 = 2, 12 of them) 1/54 + * corner (|e|^2 = 3, 8 of them) 1/216 + * + * These sum to 1. The six face weights used to read 1/36, which summed to + * 13/18 -- equilibrium then returned less mass than it was given and any + * 3D run diverged within a handful of steps. + */ static constexpr std::array::type,Q> weights = { - 8./27., + 8./27., // 0 rest // 1D - 1./36., - 1./36., - // 2D - 1./36., - 1./54., - 1./54., - 1./36., - 1./54., - 1./54., - // 3D - 1./36., - 1./54., - 1./54., - 1./54., - 1./216., - 1./216., - 1./54., - 1./216., - 1./216., - 1./36., - 1./54., - 1./54., - 1./54., - 1./216., - 1./216., - 1./54., - 1./216., - 1./216. + 2./27., // 1 face + 2./27., // 2 face + // Expand into 2D + 2./27., // 3 face + 1./54., // 4 edge + 1./54., // 5 edge + 2./27., // 6 face + 1./54., // 7 edge + 1./54., // 8 edge + // Expand into 3D + 2./27., // 9 face + 1./54., // 10 edge + 1./54., // 11 edge + 1./54., // 12 edge + 1./216., // 13 corner + 1./216., // 14 corner + 1./54., // 15 edge + 1./216., // 16 corner + 1./216., // 17 corner + 2./27., // 18 face + 1./54., // 19 edge + 1./54., // 20 edge + 1./54., // 21 edge + 1./216., // 22 corner + 1./216., // 23 corner + 1./54., // 24 edge + 1./216., // 25 corner + 1./216. // 26 corner }; static constexpr std::array opposite_index = { diff --git a/modules/core/c++/fplbm.hpp b/modules/core/c++/fplbm.hpp index f961bc9..bc99da5 100644 --- a/modules/core/c++/fplbm.hpp +++ b/modules/core/c++/fplbm.hpp @@ -48,7 +48,7 @@ public: auto& rho_f = macros.template get<"density">(); auto& rho = rho_f.at(index); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& vel = vel_f.at(index); compute_rho_momentum(dfs,rho,vel); @@ -96,7 +96,7 @@ public: auto& force_f = macros.template get<"force">(); auto& force = force_f.at(index); - auto& mom_f = macros.template get<"momentum">(); + auto& mom_f = macros.template get<"velocity">(); auto& mom = mom_f.at(index); // We already computed this in reset @@ -144,13 +144,14 @@ class component final { public: using Component = cmpt::FpLbmOneParticleGather; private: - kel::lbm::data> global_force_; + // An acceleration, as the comment above says -- now typed as one. + kel::lbm::data>> body_acceleration_; public: component(): - global_force_{} + body_acceleration_{} {} - component(kel::lbm::data> global_force__): - global_force_{global_force__} + component(kel::lbm::data>> body_acceleration__): + body_acceleration_{body_acceleration__} {} template @@ -162,7 +163,7 @@ public: // auto& dfs = dfs_old_f.at(index); auto& rho_f = macros.template get<"density">(); - auto& mom_f = macros.template get<"momentum">(); + auto& mom_f = macros.template get<"velocity">(); auto& por_f = macros.template get<"porosity">(); auto& force_f = macros.template get<"force">(); @@ -177,9 +178,9 @@ public: auto& pi = parts.at(index); auto& pirb = pi.template get<"rigid_body">(); - auto& pirb_pos = pirb.template get<"position">(); - auto& pirb_pos_old = pirb.template get<"position_old">(); - auto& p_rad = pi.template get<"collision">().template get<"radius">(); + auto& pirb_pos = pirb.template get<"position">().handle(); + auto& pirb_pos_old = pirb.template get<"position_old">().handle(); + auto& p_rad = pi.template get<"collision">().template get<"radius">().handle(); @@ -205,7 +206,7 @@ public: auto vel_s = (pirb_pos-pirb_pos_old) * sss; kel::lbm::data> force_p{{}}; - //= global_force_; + //= body_acceleration_; iterator::apply([&](const auto& index_f) -> void { auto& force = force_f.at(index_f); @@ -220,7 +221,7 @@ public: // vel_s is technically time the density of the particle? - // force = global_force_ * por + ( vel_s - mom) * two * flip_por; + // force = body_acceleration_ * por + ( vel_s - mom) * two * flip_por; force = ( vel_s - mom) * two * flip_por; // force = (vel_s - mom) * flip_por / (flip_por / two + one); @@ -238,28 +239,30 @@ public: /** * The original fused one-particle step, kept so existing call sites keep - * working. global_force_ is an acceleration, so it rides along as the - * integrator's body acceleration. + * working. The body acceleration rides straight through to the integrator; + * it used to be called global_force_, which it never was -- the integrator + * takes an acceleration, not a force. */ template class component final { public: using Component = cmpt::FpLbmOneParticle; private: - kel::lbm::data> global_force_; + // An acceleration, as the comment above says -- now typed as one. + kel::lbm::data>> body_acceleration_; public: component(): - global_force_{} + body_acceleration_{} {} - component(kel::lbm::data> global_force__): - global_force_{global_force__} + component(kel::lbm::data>> body_acceleration__): + body_acceleration_{body_acceleration__} {} template void apply(const kel::lbm::data& field, const kel::lbm::data& macros, const kel::lbm::data& pg, kel::lbm::data> index, kel::lbm::data time_step, kel::lbm::data sub_steps) const { component reset; component gather; - component integrate{global_force_}; + component integrate{body_acceleration_}; reset.apply(pg,index,time_step); gather.apply(field,macros,pg,index,time_step,sub_steps); diff --git a/modules/core/c++/hlbm.hpp b/modules/core/c++/hlbm.hpp index acb2121..3bfdd6c 100644 --- a/modules/core/c++/hlbm.hpp +++ b/modules/core/c++/hlbm.hpp @@ -78,7 +78,7 @@ public: auto& porosity_f = macros.template get<"porosity">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); kel::lbm::data>& rho = rho_f.at(index); kel::lbm::data>& vel = vel_f.at(index); @@ -128,7 +128,7 @@ public: auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); auto& part_spheroid_group = part_group; - auto& mvel_f = macros.template get<"momentum">(); + auto& mvel_f = macros.template get<"velocity">(); auto& mrho_f = macros.template get<"density">(); auto& mpor_f = macros.template get<"porosity">(); { @@ -136,9 +136,15 @@ public: auto& pi = parts.at(index); auto& pirb = pi.template get<"rigid_body">(); - auto& p_rad = pi.template get<"collision">().template get<"radius">(); - kel::lbm::data>& pirb_pos = pirb.template get<"position">(); - auto& pirb_pos_old = pirb.template get<"position_old">(); + auto& p_rad = pi.template get<"collision">().template get<"radius">().handle(); + /** + * Unwrapped once here. The unit is checked where the position is + * stored and assigned; inside the sweep it meets lattice + * coordinates and lattice directions, which carry no unit yet, so + * the kernel works on the raw vector. + */ + auto& pirb_pos = pirb.template get<"position">().handle(); + auto& pirb_pos_old = pirb.template get<"position_old">().handle(); // Matches cmpt::ParticleIntegrate: zero sub steps means one step. uint64_t steps = sub_steps.get(); @@ -163,6 +169,16 @@ public: for(uint64_t i{0u}; i < Desc::D; ++i){ force_p.at({{i}}) = 0.0; } + + /** + * Hydrodynamic torque about the body centre. + * + * Accumulated from the same per-cell momentum as force_p and with + * the same sign applied at the end, so the two cannot disagree + * about which way the reaction points. + */ + kel::lbm::data::Schema> torque_p{}; + auto vel_p_old = (pirb_pos-pirb_pos_old) / ts; iterator::apply([&](const auto& index_f) -> void{ @@ -203,18 +219,37 @@ public: momentum = momentum + ei_dfs; } - // TODO technically needs to adjust for rotation as well - auto& rho = mrho_f.at(index_f); (void) rho; macros.template get<"force">().at(index_f) = momentum; force_p = force_p + momentum; + + /** + * r x f, with r the cell offset from the body centre already + * computed above. cross() returns a Scalar in 2D and a + * Vector3 in 3D, which is exactly the rotation type in each + * case, so one spelling covers both. + */ + torque_p = torque_p + kel::lbm::math::cross(rel_dist,momentum); },start,stop); // Accumulate, do not assign: contact adds to this before integration. auto& pirb_force = pirb.template get<"force">(); pirb_force = pirb_force - force_p; + + /** + * Same sign as the force above -- the per-cell momentum is what + * the fluid gained, so the body gets the negative of it. + * + * Gated: accumulating a torque no integrator will consume is a + * silent no-op, and in 3D only the fixed-axis integrator consumes + * it (see particle_fixed_axis_supported). + */ + if constexpr ( particle_angular_supported or particle_fixed_axis_supported ){ + auto& pirb_torque = pirb.template get<"torque">(); + pirb_torque = pirb_torque - torque_p; + } } } }; @@ -236,7 +271,7 @@ public: kel::lbm::data> one; one.at({}) = 1.0; - auto& mvel_f = macros.template get<"momentum">(); + auto& mvel_f = macros.template get<"velocity">(); auto& mpor_f = macros.template get<"porosity">(); auto& particle_N_f = field.template get<"particle_N">(); auto& particle_D_f = field.template get<"particle_D">(); @@ -244,8 +279,8 @@ public: auto parts = part_group.template get<"particles">(); auto& pi = parts.at(index); auto& pirb = pi.template get<"rigid_body">(); - auto& pirb_pos = pirb.template get<"position">(); - auto& pirb_pos_old = pirb.template get<"position_old">(); + auto& pirb_pos = pirb.template get<"position">().handle(); + auto& pirb_pos_old = pirb.template get<"position_old">().handle(); uint64_t steps = sub_steps.get(); if(steps == 0u){ diff --git a/modules/core/c++/particle/aabb.hpp b/modules/core/c++/particle/aabb.hpp index e207ad8..125232d 100644 --- a/modules/core/c++/particle/aabb.hpp +++ b/modules/core/c++/particle/aabb.hpp @@ -56,7 +56,7 @@ public: auto& b = aabb.template get<"b">(); // Radius is per particle now, so a group can hold mixed sizes. - const kel::lbm::data>& rad_d = pi.template get<"collision">().template get<"radius">(); + const kel::lbm::data>& rad_d = pi.template get<"collision">().template get<"radius">().handle(); kel::lbm::data> lower; kel::lbm::data> upper; diff --git a/modules/core/c++/particle/contact.hpp b/modules/core/c++/particle/contact.hpp index 857e116..cda4a01 100644 --- a/modules/core/c++/particle/contact.hpp +++ b/modules/core/c++/particle/contact.hpp @@ -269,7 +269,7 @@ contact_response soft_shell_response( * Velocity is implicit in position-Verlet, recovered from the position pair. */ template -kel::lbm::data> surface_velocity( +kel::lbm::data>> surface_velocity( const PRef& p, const kel::lbm::data>& r, typename kel::lbm::native_data_type::type steps @@ -304,7 +304,10 @@ kel::lbm::data> surface_velocity( } } - return v; + // (position difference) * steps is a lattice velocity, and saying so is + // the whole point of the label. The force law downstream is still untyped, + // because there is no mass base unit yet. + return kel::lbm::as_unit>>(v); } } @@ -348,16 +351,16 @@ contact_response particle_contact_response( auto v_s = impl::surface_velocity(self,r_s,steps); auto v_o = impl::surface_velocity(other,r_o,steps); - kel::lbm::data> v_rel; - for(uint64_t i{0u}; i < D; ++i){ - v_rel.at({{i}}).set(v_s.at({{i}}).get() - v_o.at({{i}}).get()); - } + // Velocity minus velocity is a velocity, checked rather than assumed. + auto v_rel = v_s - v_o; auto m_s = self.template get<"rigid_body">().template get<"mass">().at({}).get(); auto m_o = other.template get<"rigid_body">().template get<"mass">().at({}).get(); auto m_eff = (m_s * m_o) / (m_s + m_o); - return impl::soft_shell_response(m,r_s,v_rel,m_eff,dem); + // Unwrapped here: the spring-dashpot produces a force, and force has no + // unit until there is a mass base unit. + return impl::soft_shell_response(m,r_s,v_rel.handle(),m_eff,dem); } /** @@ -390,7 +393,8 @@ contact_response wall_contact_response( auto m_s = self.template get<"rigid_body">().template get<"mass">().at({}).get(); - return impl::soft_shell_response(m,r_s,v_rel,m_s,dem); + // Same boundary as the pair case: the force law is untyped. + return impl::soft_shell_response(m,r_s,v_rel.handle(),m_s,dem); } /** diff --git a/modules/core/c++/particle/couple.hpp b/modules/core/c++/particle/couple.hpp index 19a276c..d882081 100644 --- a/modules/core/c++/particle/couple.hpp +++ b/modules/core/c++/particle/couple.hpp @@ -43,7 +43,7 @@ kel::lbm::data> cell_porosity( for_each_bin_candidate(cl,cell,[&](uint64_t p){ auto& pi = parts.at({p}); auto& pos = pi.template get<"rigid_body">().template get<"position">(); - auto rad = pi.template get<"collision">().template get<"radius">(); + auto rad = pi.template get<"collision">().template get<"radius">().handle(); kel::lbm::data> rel; for(uint64_t i{0u}; i < D; ++i){ diff --git a/modules/core/c++/particle/integrate.hpp b/modules/core/c++/particle/integrate.hpp index a84cd37..1e85338 100644 --- a/modules/core/c++/particle/integrate.hpp +++ b/modules/core/c++/particle/integrate.hpp @@ -45,21 +45,29 @@ constexpr bool particle_angular_supported = (D == 2u); template void integrate_particle( kel::lbm::data>& pi, - const kel::lbm::data>& body_acceleration, + const kel::lbm::data>>& body_acceleration, kel::lbm::data sub_steps ){ auto& pirb = pi.template get<"rigid_body">(); const uint64_t steps = particle_sub_steps(sub_steps); - kel::lbm::data> ts; + // A sub step is a lattice time, not a bare number. + kel::lbm::data>> ts; ts.at({}).set( typename kel::lbm::native_data_type::type(1) / static_cast::type>(steps) ); + /** + * The seam between the typed and untyped halves. Force and mass carry no + * units yet -- there is no mass base unit -- so their quotient is a bare + * vector and the acceleration unit is attached here, deliberately and in + * one place, rather than being acquired silently at an assignment. + */ pirb.template get<"acceleration">() = - pirb.template get<"force">() / pirb.template get<"mass">() + kel::lbm::data>>{ + pirb.template get<"force">() / pirb.template get<"mass">()} + body_acceleration; // See particle_angular_supported for why 3D is excluded rather than @@ -74,6 +82,113 @@ void integrate_particle( } } +/** + * Whether rotation about a single FIXED axis can be integrated exactly. + * + * True in 3D as well as 2D, unlike particle_angular_supported. Constraining + * the body to one axis does not paper over the three problems listed there -- + * it removes all of them: + * + * - Rotation composition only fails to commute when the axis moves. If the + * angular acceleration is always parallel to a fixed axis and the rotation + * starts parallel to it, the rotation vector stays parallel to it for all + * time, so verlet_step_lambda's component-wise update is exact rather than + * a small-angle approximation. + * - omega x (I omega) vanishes when omega lies along a principal axis. Every + * axis of a sphere is principal, and a rotor symmetric about its own axis + * has that axis principal. The term is genuinely zero, not neglected. + * - A scalar inertia is exactly the moment about that one axis. + * + * This is the wind turbine case: a body pinned in place, free to spin about + * one axis and nothing else. + */ +template +constexpr bool particle_fixed_axis_supported = (D == 2u or D == 3u); + +namespace impl { +/** + * The spin axis has the same schema as the rotation it drives: a signed scalar + * in 2D (there is only the out-of-plane direction), a unit vector in 3D. + */ +template +using spin_axis_schema = typename sch::impl::rotation_type_helper::Schema; + +template +kel::lbm::data> normalise_spin_axis( + const kel::lbm::data>& axis +){ + if constexpr ( D == 2u ){ + kel::lbm::data> out; + const auto a = axis.at({}).get(); + out.at({}).set((a < 0) ? -1 : 1); + return out; + }else{ + // math::normalize returns a zero vector unchanged rather than NaN, so + // a caller that forgets the axis gets a body that never spins instead + // of one that poisons the whole simulation. + return kel::lbm::math::normalize(axis); + } +} +} + +/** + * Projects the accumulated torque onto a fixed axis and advances the body + * about it, holding the position fixed. + * + * The position is pinned by zeroing the acceleration, not by writing the + * position back each step. Position-Verlet is x_new = 2x - x_old + a dt^2, so + * with x_old == x at construction and a == 0 the body stays exactly where it + * started, every step, bitwise, with no drift to correct for. + */ +template +void integrate_particle_fixed_axis( + kel::lbm::data>& pi, + const kel::lbm::data>& axis, + kel::lbm::data sub_steps +){ + static_assert(particle_fixed_axis_supported, "Fixed axis spin needs D == 2 or D == 3"); + + auto& pirb = pi.template get<"rigid_body">(); + + const uint64_t steps = particle_sub_steps(sub_steps); + + // A sub step is a lattice time, not a bare number. + kel::lbm::data>> ts; + ts.at({}).set( + typename kel::lbm::native_data_type::type(1) + / static_cast::type>(steps) + ); + + // Pinned: the accumulated force is deliberately ignored. + auto& acc = pirb.template get<"acceleration">(); + for(uint64_t i{0u}; i < D; ++i){ + acc.at({{i}}).set(0); + } + + auto& torque = pirb.template get<"torque">(); + auto& inertia = pirb.template get<"inertia">(); + auto& ang_acc = pirb.template get<"angular_acceleration">(); + + if constexpr ( D == 2u ){ + ang_acc.at({}).set( + axis.at({}).get() * torque.at({}).get() / inertia.at({}).get() + ); + }else{ + // tau_axis = axis . torque, then alpha = axis * (tau_axis / I). Any + // torque component off the axis is reacted by the mounting and does + // not accelerate the rotor. + const auto tau_axis = kel::lbm::math::dot(axis,torque).at({}).get(); + const auto alpha = tau_axis / inertia.at({}).get(); + for(uint64_t i{0u}; i < 3u; ++i){ + ang_acc.at({{i}}).set(axis.at({{i}}).get() * alpha); + } + } + + for(uint64_t i{0u}; i < steps; ++i){ + verlet_step_lambda(pi,ts); + } +} + /** * Clears the force/torque accumulators. Must run before any contributor. */ @@ -113,6 +228,16 @@ struct ParticleForceReset {}; * acceleration directly, so a second contributor silently overwrote the first. */ struct ParticleIntegrate {}; + +/** + * Integrates a body pinned in place and free to spin about one fixed axis. + * + * The rotor case: the fluid torque projected onto the axis spins it, the + * position never moves, and angular velocity is an output. Unlike + * ParticleIntegrate this works in 3D -- see particle_fixed_axis_supported for + * why the constraint makes that exact rather than approximate. + */ +struct ParticleAxisIntegrate {}; } template @@ -145,14 +270,14 @@ private: * Body acceleration applied to every particle, e.g. gravity. Kept as an * acceleration rather than a force so it does not need the mass. */ - kel::lbm::data> body_acceleration_; + kel::lbm::data>> body_acceleration_; public: component(): body_acceleration_{} {} - component(const kel::lbm::data>& body_acceleration__): + component(const kel::lbm::data>>& body_acceleration__): body_acceleration_{body_acceleration__} {} @@ -170,5 +295,36 @@ public: } }; +template +class component final { +public: + using Component = cmpt::ParticleAxisIntegrate; + +private: + /** + * Normalised once here rather than per step, so the kernel never divides + * by a length and a caller cannot change the axis mid-run. + */ + kel::lbm::data> axis_; + +public: + component(const kel::lbm::data>& axis__): + axis_{impl::normalise_spin_axis(axis__)} + {} + + template + void apply( + const kel::lbm::data& pg, + kel::lbm::data> index, + kel::lbm::data time_step, + kel::lbm::data sub_steps + ) const { + (void) time_step; + + auto& parts = pg.template get<"particles">(); + integrate_particle_fixed_axis(parts.at(index), axis_, sub_steps); + } +}; + } } diff --git a/modules/core/c++/particle/particle.hpp b/modules/core/c++/particle/particle.hpp index 00e401a..4d96440 100644 --- a/modules/core/c++/particle/particle.hpp +++ b/modules/core/c++/particle/particle.hpp @@ -18,7 +18,7 @@ namespace lbm { */ template kel::lbm::data>> create_spheroid_particle_group( - kel::lbm::data> radius_p, + kel::lbm::data>> radius_p, kel::lbm::data> density_p, const kel::lbm::data& mask_resolution ){ @@ -169,7 +169,7 @@ kel::lbm::data>> create_sph */ template -constexpr auto verlet_step_lambda = [](kel::lbm::data>& particle, kel::lbm::data> time_step_delta){ +constexpr auto verlet_step_lambda = [](kel::lbm::data>& particle, kel::lbm::data>> time_step_delta){ auto& body = particle.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -184,10 +184,13 @@ constexpr auto verlet_step_lambda = [](kel::lbm::data>& parti auto tsd_squared = time_step_delta * time_step_delta; - kel::lbm::data> pos_new; + kel::lbm::data>> pos_new; // Actual step - kel::lbm::data> two; + // Dimensionless, and spelled as such: Pure is the claim, Scalar is only + // the shape. + kel::lbm::data>> two; two.at({}).set(2.0); + // m + (m/s^2)*s^2 = m. pos_new = pos * two - pos_old + pos_acc * tsd_squared; // Swap - Could be std::swap? No, this is device code @@ -196,7 +199,12 @@ constexpr auto verlet_step_lambda = [](kel::lbm::data>& parti // Angular kel::lbm::data::Schema> rot_new; - rot_new = rot * two - rot_old + rot_acc * tsd_squared; + /** + * The angular half runs on raw values. Torque and inertia have no units + * yet (there is no mass base unit), so angular acceleration cannot be + * typed either, and the timestep has to be unwrapped to meet it. + */ + rot_new = rot * two.handle() - rot_old + rot_acc * tsd_squared.handle(); rot_old = rot; rot = rot_new; @@ -240,7 +248,8 @@ template> constexpr auto broadphase_collision_distance_squared = []( const PRef& left, const PRef& right -) -> std::pair>>{ +) -> std::pair>, sch::LbmMeter>>::Schema>>{ auto rad_l = left.template get<"collision">().template get<"radius">(); auto rad_r = right.template get<"collision">().template get<"radius">(); @@ -252,9 +261,10 @@ constexpr auto broadphase_collision_distance_squared = []( auto norm_2 = kel::lbm::math::dot(pos_dist,pos_dist); - kel::lbm::data> two; + // Dimensionless factor, so it has to say Pure. + kel::lbm::data>> two; two.at({}) = 2.0; - // (r_l + r_r)^2 + // (r_l + r_r)^2 -- an area, matching the squared centre distance above. auto rad_ab_2 = rad_l * rad_l + rad_r * rad_r + rad_r * rad_l * two; return std::make_pair((norm_2.at({}).get() < rad_ab_2.at({}).get()), norm_2); diff --git a/modules/core/c++/particle/porosity.hpp b/modules/core/c++/particle/porosity.hpp index cd4e804..f393eb1 100644 --- a/modules/core/c++/particle/porosity.hpp +++ b/modules/core/c++/particle/porosity.hpp @@ -106,7 +106,7 @@ public: kel::lbm::data> dist_len = kel::lbm::math::dot(dist,dist); dist_len.at({}).set(std::sqrt(dist_len.at({}).get())); auto& coll = pi.template get<"collision">(); - const kel::lbm::data>& rad_d = coll.template get<"radius">(); + const kel::lbm::data>& rad_d = coll.template get<"radius">().handle(); const auto& eps = part_group.template get<"epsilon">().at({}); // Move this somewhere diff --git a/modules/core/c++/particle/schema.hpp b/modules/core/c++/particle/schema.hpp index 39e1a36..b0a6f5d 100644 --- a/modules/core/c++/particle/schema.hpp +++ b/modules/core/c++/particle/schema.hpp @@ -2,6 +2,8 @@ #include "common.hpp" +#include "../lbm_unit.hpp" + namespace kel { namespace lbm { @@ -10,7 +12,8 @@ template struct Spheroid { using ValueSchema = T; using Schema = sch::Struct< - sch::Member,"radius"> + // A radius is a lattice length, like the position it is compared against. + sch::Member>,"radius"> >; }; } @@ -47,12 +50,23 @@ struct inertia_type_helper { template using ParticleRigidBody = Struct< - Member, "position">, - Member, "position_old">, + /** + * Lattice length. The unit sits on the vector rather than on its + * components: a position is metres in every direction, and the component + * type is what the kernels index. + */ + Member>, "position">, + Member>, "position_old">, Member::Schema, "rotation">, Member::Schema, "rotation_old">, - Member, "acceleration">, + /** + * Lattice acceleration. Typed because the Verlet update adds + * acceleration * dt^2 to a position and so must come out in metres. + * It is also where the typed and untyped halves meet: force and mass have + * no units yet, so integrate_particle attaches this one explicitly. + */ + Member>, "acceleration">, Member::Schema, "angular_acceleration">, /** diff --git a/modules/core/c++/psm.hpp b/modules/core/c++/psm.hpp index b540f38..7f43e2d 100644 --- a/modules/core/c++/psm.hpp +++ b/modules/core/c++/psm.hpp @@ -65,7 +65,7 @@ public: auto& porous_f = macros.template get<"porosity">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); kel::lbm::data>& rho = rho_f.at(index); kel::lbm::data>& vel = vel_f.at(index); @@ -125,7 +125,7 @@ void apply( auto& por_f = macros.template get<"porosity">(); auto& rho_f = macros.template get<"density">(); - auto& vel_f = macros.template get<"momentum">(); + auto& vel_f = macros.template get<"velocity">(); auto& force_f = macros.template get<"force">(); kel::lbm::data> one; @@ -145,16 +145,20 @@ void apply( auto& pi = parts.at(index); auto& p_rad = - pi.template get<"collision">().template get<"radius">(); + pi.template get<"collision">().template get<"radius">().handle(); auto& pirb = pi.template get<"rigid_body">(); - kel::lbm::data>& pirb_pos = - pirb.template get<"position">(); + /** + * Unwrapped once here. The unit is checked where the position is + * stored and assigned; inside the sweep it meets lattice coordinates + * and lattice directions, which carry no unit yet, so the kernel + * works on the raw vector. + */ + auto& pirb_pos = pirb.template get<"position">().handle(); - auto& pirb_pos_old = - pirb.template get<"position_old">(); + auto& pirb_pos_old = pirb.template get<"position_old">().handle(); /* diff --git a/modules/core/tests/cell_list.cpp b/modules/core/tests/cell_list.cpp index 00b260c..f04e065 100644 --- a/modules/core/tests/cell_list.cpp +++ b/modules/core/tests/cell_list.cpp @@ -17,7 +17,8 @@ constexpr uint64_t Cap = 8u; template auto make_group(double radius){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{4u}); diff --git a/modules/core/tests/component_instantiation.cpp b/modules/core/tests/component_instantiation.cpp index 94d0536..e4a890b 100644 --- a/modules/core/tests/component_instantiation.cpp +++ b/modules/core/tests/component_instantiation.cpp @@ -33,7 +33,8 @@ using GroupSch = kel::lbm::sch::ParticleGroup, kel::lbm::enc::Native>; auto make_group(double radius){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{8u}); @@ -102,7 +103,8 @@ SAW_TEST("Component / ParticleIntegrate instantiates and moves the body"){ auto mass = rb.template get<"mass">().at({}).get(); rb.template get<"force">().at({{0u}}).set(mass); // unit acceleration - kel::lbm::data> gravity; + // Gravity is an acceleration, and now has to say so. + kel::lbm::data>> gravity; gravity.at({{1u}}).set(-0.5); lbm::component integrate{gravity}; diff --git a/modules/core/tests/contact_dynamics.cpp b/modules/core/tests/contact_dynamics.cpp index b6545b9..b918bc1 100644 --- a/modules/core/tests/contact_dynamics.cpp +++ b/modules/core/tests/contact_dynamics.cpp @@ -17,7 +17,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius, double density){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(density); return kel::lbm::create_spheroid_particle_group(r,d,{8u}); @@ -138,7 +139,7 @@ SAW_TEST("Dynamics / resting contact settles at the force balance"){ kel::lbm::data> extent{{64u,32u}}; // Gravity pointing at the wall, as a body acceleration. - kel::lbm::data> body_acc; + kel::lbm::data>> body_acc; body_acc.at({{0u}}).set(-gravity); body_acc.at({{1u}}).set(0.0); diff --git a/modules/core/tests/descriptor.cpp b/modules/core/tests/descriptor.cpp index 7f743ce..96d6531 100644 --- a/modules/core/tests/descriptor.cpp +++ b/modules/core/tests/descriptor.cpp @@ -2,8 +2,63 @@ #include "../c++/descriptor.hpp" +#include + namespace { +/** + * The lattice moment conditions: + * + * sum_i w_i = 1 + * sum_i w_i e_ia = 0 + * sum_i w_i e_ia e_ib = cs^2 * delta_ab + * + * A weight table that violates these still compiles and still passes an + * opposite-index check, but it makes the equilibrium return the wrong mass and + * momentum, and the only symptom is a simulation that diverges within a few + * steps. D3Q27 carried exactly that bug: its six face weights read 1/36 rather + * than 2/27, so the weights summed to 13/18. + * + * The second moment is the useful one -- it pins the weights per shell, where + * the plain sum only pins them in aggregate. + */ +template +void check_weight_moments(){ + using namespace kel; + + using dfi = lbm::df_info; + + constexpr double tol = 1e-12; + + double sum_w = 0.0; + for(uint64_t k = 0u; k < Descriptor::Q; ++k){ + sum_w += dfi::weights[k]; + } + SAW_EXPECT(std::abs(sum_w - 1.0) < tol, "Weights have to sum to 1"); + + for(uint64_t a = 0u; a < Descriptor::D; ++a){ + double first = 0.0; + for(uint64_t k = 0u; k < Descriptor::Q; ++k){ + first += dfi::weights[k] * static_cast(dfi::directions[k][a]); + } + SAW_EXPECT(std::abs(first) < tol, "First moment has to vanish"); + } + + for(uint64_t a = 0u; a < Descriptor::D; ++a){ + for(uint64_t b = 0u; b < Descriptor::D; ++b){ + double second = 0.0; + for(uint64_t k = 0u; k < Descriptor::Q; ++k){ + second += + dfi::weights[k] + * static_cast(dfi::directions[k][a]) + * static_cast(dfi::directions[k][b]); + } + const double expected = (a == b) ? static_cast(dfi::cs2) : 0.0; + SAW_EXPECT(std::abs(second - expected) < tol, "Second moment has to be cs^2 delta_ab"); + } + } +} + template void check_opposite_dirs(){ using namespace kel; @@ -32,8 +87,37 @@ SAW_TEST("Opposites and Dirs D2Q9"){ using namespace kel; check_opposite_dirs>(); } +SAW_TEST("Opposites and Dirs D3Q19"){ + using namespace kel; + check_opposite_dirs>(); +} SAW_TEST("Opposites and Dirs D3Q27"){ using namespace kel; check_opposite_dirs>(); } + +/** + * D2Q4 is deliberately absent: it is experimental and its weights sum to 5/6, + * so it would fail these outright. + */ +SAW_TEST("Weight moments D1Q3"){ + using namespace kel; + check_weight_moments>(); +} +SAW_TEST("Weight moments D2Q5"){ + using namespace kel; + check_weight_moments>(); +} +SAW_TEST("Weight moments D2Q9"){ + using namespace kel; + check_weight_moments>(); +} +SAW_TEST("Weight moments D3Q19"){ + using namespace kel; + check_weight_moments>(); +} +SAW_TEST("Weight moments D3Q27"){ + using namespace kel; + check_weight_moments>(); +} } diff --git a/modules/core/tests/macroscopic.cpp b/modules/core/tests/macroscopic.cpp new file mode 100644 index 0000000..9184819 --- /dev/null +++ b/modules/core/tests/macroscopic.cpp @@ -0,0 +1,113 @@ +#include + +#include + +#include "../c++/macroscopic.hpp" +#include "../c++/equilibrium.hpp" + +namespace { +namespace sch { +using namespace kel::lbm::sch; +using T = Float64; +} + +/** + * The macro field used to be called "momentum" while holding velocity: + * compute_rho_u divides the first moment by the density (macroscopic.hpp). + * These pin that the name now matches the content, so the rename cannot be + * quietly undone by removing the division. + */ + +template +void roundtrip(double rho_in, double ux_in){ + using namespace kel; + + lbm::data> rho; + rho.at({}).set(rho_in); + + lbm::data> vel; + for(uint64_t i = 0u; i < Desc::D; ++i){ + vel.at({{i}}).set(0.0); + } + vel.at({{0u}}).set(ux_in); + + auto dfs = lbm::equilibrium(rho,vel); + + lbm::data> rho_out; + lbm::data> vel_out; + lbm::compute_rho_u(dfs,rho_out,vel_out); + + SAW_EXPECT(std::abs(rho_out.at({}).get() - rho_in) < 1e-12, + "density must round trip through equilibrium"); + SAW_EXPECT(std::abs(vel_out.at({{0u}}).get() - ux_in) < 1e-12, + "velocity must round trip through equilibrium"); +} + +SAW_TEST("Macroscopic / the field holds velocity, not momentum"){ + using namespace kel; + using Desc = sch::D2Q9; + + // Density deliberately not 1, which is the only case where velocity and + // momentum differ numerically. + const double rho_in = 2.0; + const double ux_in = 0.1; + + lbm::data> rho; + rho.at({}).set(rho_in); + lbm::data> vel; + vel.at({{0u}}).set(ux_in); + vel.at({{1u}}).set(0.0); + + auto dfs = lbm::equilibrium(rho,vel); + + lbm::data> rho_out; + lbm::data> vel_out; + lbm::compute_rho_u(dfs,rho_out,vel_out); + + const double got = vel_out.at({{0u}}).get(); + + SAW_EXPECT(std::abs(got - ux_in) < 1e-12, "must be the velocity u"); + // rho*u would be 0.2 here. If the division by rho were dropped, this is + // the assertion that would catch it. + SAW_EXPECT(std::abs(got - rho_in * ux_in) > 1e-3, + "must NOT be the momentum rho*u"); +} + +SAW_TEST("Macroscopic / round trip D2Q9"){ + roundtrip(1.0, 0.05); + roundtrip(2.0, 0.05); +} + +SAW_TEST("Macroscopic / round trip D2Q5"){ + roundtrip>(1.0, 0.0); +} + +SAW_TEST("Macroscopic / round trip D3Q27"){ + // The 3D path divides by rho too -- it is the same loop. + roundtrip(1.0, 0.05); + roundtrip(1.7, 0.03); +} + +SAW_TEST("Macroscopic / a quiescent field has zero velocity at any density"){ + using namespace kel; + using Desc = sch::D2Q9; + + for(double r : {0.5, 1.0, 3.0}){ + lbm::data> rho; + rho.at({}).set(r); + lbm::data> vel; + vel.at({{0u}}).set(0.0); + vel.at({{1u}}).set(0.0); + + auto dfs = lbm::equilibrium(rho,vel); + + lbm::data> rho_out; + lbm::data> vel_out; + lbm::compute_rho_u(dfs,rho_out,vel_out); + + SAW_EXPECT(std::abs(rho_out.at({}).get() - r) < 1e-12, "density preserved"); + SAW_EXPECT(std::abs(vel_out.at({{0u}}).get()) < 1e-12, "no x velocity at rest"); + SAW_EXPECT(std::abs(vel_out.at({{1u}}).get()) < 1e-12, "no y velocity at rest"); + } +} +} diff --git a/modules/core/tests/mask.cpp b/modules/core/tests/mask.cpp index dfb5ef8..f67cc78 100644 --- a/modules/core/tests/mask.cpp +++ b/modules/core/tests/mask.cpp @@ -15,7 +15,8 @@ using T = Float64; constexpr uint64_t D = 2u; auto make_one(double radius){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{32u}); @@ -209,7 +210,8 @@ SAW_TEST("Mask / porosity agrees with the analytic sphere"){ kel::lbm::data> eps; eps.at({}).set(1.5); - auto rad = p.template get<"collision">().template get<"radius">(); + // The analytic porosity works on raw lengths, as the solvers do. + auto rad = p.template get<"collision">().template get<"radius">().handle(); double worst = 0.0; for(int i = 0; i <= 40; ++i){ diff --git a/modules/core/tests/particle_3d.cpp b/modules/core/tests/particle_3d.cpp index 8c3e579..a169f10 100644 --- a/modules/core/tests/particle_3d.cpp +++ b/modules/core/tests/particle_3d.cpp @@ -26,7 +26,8 @@ constexpr uint64_t D = 3u; template auto make_group(double radius, double density, uint64_t res = 12u){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(density); return kel::lbm::create_spheroid_particle_group(r,d,{res}); diff --git a/modules/core/tests/particle_consistency.cpp b/modules/core/tests/particle_consistency.cpp index ae40aa8..7e84ab5 100644 --- a/modules/core/tests/particle_consistency.cpp +++ b/modules/core/tests/particle_consistency.cpp @@ -19,7 +19,8 @@ constexpr uint64_t Cap = 16u; template auto make_group(double radius, uint64_t res = 24u){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{res}); diff --git a/modules/core/tests/particle_edges.cpp b/modules/core/tests/particle_edges.cpp index cbc1bab..e39817b 100644 --- a/modules/core/tests/particle_edges.cpp +++ b/modules/core/tests/particle_edges.cpp @@ -17,7 +17,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{8u}); diff --git a/modules/core/tests/particle_gaps.cpp b/modules/core/tests/particle_gaps.cpp index 3db831e..f7213a6 100644 --- a/modules/core/tests/particle_gaps.cpp +++ b/modules/core/tests/particle_gaps.cpp @@ -18,7 +18,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius, uint64_t res = 32u){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{res}); diff --git a/modules/core/tests/particles.cpp b/modules/core/tests/particles.cpp index 2105abc..1bc5d46 100644 --- a/modules/core/tests/particles.cpp +++ b/modules/core/tests/particles.cpp @@ -26,7 +26,8 @@ SAW_TEST("Verlet step 2D - Planar"){ acc.at({{0}}).set({1.0}); - kel::lbm::data> dt; + // A timestep is a lattice time. + kel::lbm::data>> dt; dt.at({}).set(0.5); lbm::verlet_step_lambda(particle,dt); @@ -131,7 +132,8 @@ SAW_TEST("Moving particles 2D"){ acc.at({{0u}}) = -0.1; } - kel::lbm::data> dt; + // A timestep is a lattice time. + kel::lbm::data>> dt; dt.at({}).set(0.5); bool has_collided = false; for(uint64_t i = 0u; i < 32u; ++i){ @@ -284,7 +286,8 @@ SAW_TEST("Spheroid Particle / AABB"){ constexpr uint64_t D = 2u; constexpr uint64_t PC = 1u; - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 2.2f; dense.at({}) = 1.0f; auto spheroid_pgrp = lbm::create_spheroid_particle_group(radi,dense,{8u}); @@ -331,7 +334,8 @@ SAW_TEST("Spheroid Particle / AABB per particle"){ constexpr uint64_t D = 2u; constexpr uint64_t PC = 3u; - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 2.2f; dense.at({}) = 1.0f; auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{8u}); @@ -390,7 +394,8 @@ SAW_TEST("Spheroid Particle / per-particle inertial fan-out"){ constexpr uint64_t D = 2u; constexpr uint64_t PC = 4u; - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 2.0; dense.at({}) = 3.0; auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{8u}); @@ -466,7 +471,8 @@ SAW_TEST("Particle / force accumulates then integrates"){ - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 1.0; dense.at({}) = 1.0 / 3.14159265358979323846; // gives mass == 1 auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{4u}); @@ -502,7 +508,8 @@ SAW_TEST("Particle / zero sub steps still advances"){ - kel::lbm::data> radi,dense; + kel::lbm::data>> radi; + kel::lbm::data> dense; radi.at({}) = 1.0; dense.at({}) = 1.0 / 3.14159265358979323846; auto pgrp = lbm::create_spheroid_particle_group(radi,dense,{4u}); @@ -530,7 +537,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius, double density){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(density); return kel::lbm::create_spheroid_particle_group(r,d,{4u}); diff --git a/modules/core/tests/rotation.cpp b/modules/core/tests/rotation.cpp index e1e8c46..a4bab33 100644 --- a/modules/core/tests/rotation.cpp +++ b/modules/core/tests/rotation.cpp @@ -18,7 +18,8 @@ constexpr uint64_t D = 2u; template auto make_group(double radius, double density){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(density); return kel::lbm::create_spheroid_particle_group(r,d,{16u}); diff --git a/modules/core/tests/rotation_3d.cpp b/modules/core/tests/rotation_3d.cpp new file mode 100644 index 0000000..4bfb067 --- /dev/null +++ b/modules/core/tests/rotation_3d.cpp @@ -0,0 +1,254 @@ +#include + +#include + +#include "../c++/particle/particle.hpp" +#include "../c++/particle/integrate.hpp" + +namespace { +namespace sch { +using namespace kel::lbm::sch; +using T = Float64; +} + +constexpr uint64_t D = 3u; + +/** + * The fixed-axis rotor: a body pinned in place, free to spin about one axis. + * + * particle_angular_supported<3> is false and stays false -- general 3D spin + * needs a quaternion and an inertia tensor. These cover the constrained case + * instead, which is exact rather than approximate because the axis never + * moves. See particle_fixed_axis_supported for the argument. + */ + +auto make_rotor(double radius = 2.0, double density = 1.0){ + kel::lbm::data>> r; + kel::lbm::data> d; + r.at({}).set(radius); + d.at({}).set(density); + return kel::lbm::create_spheroid_particle_group(r,d,{8u}); +} + +template +auto& body(G& g){ + return g.template get<"particles">().at({0u}).template get<"rigid_body">(); +} + +template +void place_at_rest(G& g, double x, double y, double z){ + auto& rb = body(g); + auto& pos = rb.template get<"position">(); + auto& old = rb.template get<"position_old">(); + const double v[3] = {x,y,z}; + for(uint64_t i = 0u; i < 3u; ++i){ + pos.at({{i}}).set(v[i]); + // position_old == position is what makes the pin exact. + old.at({{i}}).set(v[i]); + } + auto& rot = rb.template get<"rotation">(); + auto& rot_old = rb.template get<"rotation_old">(); + auto& ang = rb.template get<"angular_acceleration">(); + for(uint64_t i = 0u; i < 3u; ++i){ + rot.at({{i}}).set(0); + rot_old.at({{i}}).set(0); + ang.at({{i}}).set(0); + } +} + +kel::lbm::data> vec3(double x, double y, double z){ + kel::lbm::data> v; + v.at({{0u}}).set(x); + v.at({{1u}}).set(y); + v.at({{2u}}).set(z); + return v; +} + +template +void set_torque(G& g, double x, double y, double z){ + auto& t = body(g).template get<"torque">(); + t.at({{0u}}).set(x); + t.at({{1u}}).set(y); + t.at({{2u}}).set(z); +} + +SAW_TEST("Rotor 3D / fixed axis is supported where general spin is not"){ + using namespace kel; + static_assert(not lbm::particle_angular_supported<3u>, + "General 3D spin must stay gated off"); + static_assert(lbm::particle_fixed_axis_supported<3u>, + "Fixed axis spin must be available in 3D"); + static_assert(lbm::particle_fixed_axis_supported<2u>, + "Fixed axis spin must also cover 2D"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Rotor 3D / position is pinned bitwise under load"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 7.0, 11.0, 13.0); + + auto& rb = body(g); + // A force far larger than anything the fluid would produce. + auto& f = rb.template get<"force">(); + f.at({{0u}}).set(1.0e6); + f.at({{1u}}).set(-5.0e5); + f.at({{2u}}).set(2.5e5); + + set_torque(g, 0.0, 3.0, 0.0); + + auto axis = vec3(0.0,1.0,0.0); + auto& pi = g.template get<"particles">().at({0u}); + + for(uint64_t s = 0u; s < 64u; ++s){ + lbm::integrate_particle_fixed_axis(pi, axis, {4u}); + } + + auto& pos = rb.template get<"position">(); + // Exact equality, not a tolerance: 2x - x_old + 0 reproduces x bitwise. + SAW_EXPECT(pos.at({{0u}}).get() == 7.0, "x must not drift"); + SAW_EXPECT(pos.at({{1u}}).get() == 11.0, "y must not drift"); + SAW_EXPECT(pos.at({{2u}}).get() == 13.0, "z must not drift"); +} + +SAW_TEST("Rotor 3D / only the on-axis torque component spins it"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + + // Mostly off-axis: only the y component should drive a y-axis rotor. + set_torque(g, 9.0, 2.0, -4.0); + + auto axis = vec3(0.0,1.0,0.0); + auto& pi = g.template get<"particles">().at({0u}); + lbm::integrate_particle_fixed_axis(pi, axis, {1u}); + + auto& ang = body(g).template get<"angular_acceleration">(); + const double inertia = body(g).template get<"inertia">().at({}).get(); + + SAW_EXPECT(std::abs(ang.at({{0u}}).get()) < 1e-12, "No spin off the axis in x"); + SAW_EXPECT(std::abs(ang.at({{2u}}).get()) < 1e-12, "No spin off the axis in z"); + SAW_EXPECT(std::abs(ang.at({{1u}}).get() - (2.0 / inertia)) < 1e-12, + "On-axis component must give tau/I"); +} + +SAW_TEST("Rotor 3D / rotation stays parallel to the axis"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + + // A deliberately tilted, non-unit axis: the component must normalise it. + auto axis = vec3(1.0, 2.0, -2.0); + auto& pi = g.template get<"particles">().at({0u}); + + for(uint64_t s = 0u; s < 128u; ++s){ + set_torque(g, 0.5, 1.0, 0.25); + lbm::integrate_particle_fixed_axis(pi, axis, {2u}); + } + + auto& rot = body(g).template get<"rotation">(); + + // |rot| must equal |rot . axis_hat|: no component ever grows off-axis. + const double n = std::sqrt(1.0 + 4.0 + 4.0); + const double ax[3] = {1.0/n, 2.0/n, -2.0/n}; + + double along = 0.0; + double len2 = 0.0; + for(uint64_t i = 0u; i < 3u; ++i){ + along += rot.at({{i}}).get() * ax[i]; + len2 += rot.at({{i}}).get() * rot.at({{i}}).get(); + } + const double len = std::sqrt(len2); + + SAW_EXPECT(len > 1e-9, "The rotor should actually have turned"); + SAW_EXPECT(std::abs(len - std::abs(along)) < 1e-9, + "Rotation must stay parallel to the spin axis"); +} + +SAW_TEST("Rotor 3D / constant torque spins up linearly"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + + auto axis = vec3(0.0,0.0,1.0); + auto& pi = g.template get<"particles">().at({0u}); + auto& rb = body(g); + + const double inertia = rb.template get<"inertia">().at({}).get(); + const double tau = 3.0; + + // One sub step, so a step is a unit of time and omega is rot - rot_old. + auto omega_z = [&](){ + return rb.template get<"rotation">().at({{2u}}).get() + - rb.template get<"rotation_old">().at({{2u}}).get(); + }; + + for(uint64_t s = 0u; s < 8u; ++s){ + set_torque(g, 0.0, 0.0, tau); + lbm::integrate_particle_fixed_axis(pi, axis, {1u}); + } + const double w8 = omega_z(); + + for(uint64_t s = 0u; s < 8u; ++s){ + set_torque(g, 0.0, 0.0, tau); + lbm::integrate_particle_fixed_axis(pi, axis, {1u}); + } + const double w16 = omega_z(); + + SAW_EXPECT(w8 > 0.0, "A positive torque must spin it positively"); + // Constant angular acceleration: omega is linear in step count, so + // doubling the steps doubles omega. + SAW_EXPECT(std::abs(w16 - 2.0 * w8) < 1e-9 * std::abs(w16), + "Angular velocity must grow linearly under constant torque"); + SAW_EXPECT(std::abs(w8 - 8.0 * (tau / inertia)) < 1e-9, + "Angular velocity must match tau/I per step"); +} + +SAW_TEST("Rotor 3D / no torque means no spin"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + set_torque(g, 0.0, 0.0, 0.0); + + auto axis = vec3(0.0,1.0,0.0); + auto& pi = g.template get<"particles">().at({0u}); + + for(uint64_t s = 0u; s < 32u; ++s){ + lbm::integrate_particle_fixed_axis(pi, axis, {4u}); + } + + auto& rot = body(g).template get<"rotation">(); + for(uint64_t i = 0u; i < 3u; ++i){ + SAW_EXPECT(rot.at({{i}}).get() == 0.0, "A torque free rotor must not turn"); + } +} + +SAW_TEST("Rotor 3D / a zero axis is inert rather than NaN"){ + using namespace kel; + + auto g = make_rotor(); + place_at_rest(g, 0.0, 0.0, 0.0); + set_torque(g, 1.0, 1.0, 1.0); + + // math::normalize returns a zero vector unchanged, so this must produce a + // body that never spins -- not one that poisons the run with NaN. + auto axis = vec3(0.0,0.0,0.0); + auto& pi = g.template get<"particles">().at({0u}); + + for(uint64_t s = 0u; s < 8u; ++s){ + lbm::integrate_particle_fixed_axis(pi, axis, {2u}); + } + + auto& rot = body(g).template get<"rotation">(); + for(uint64_t i = 0u; i < 3u; ++i){ + const double v = rot.at({{i}}).get(); + SAW_EXPECT(v == v, "Rotation must not be NaN"); + SAW_EXPECT(std::abs(v) < 1e-12, "A zero axis must not spin the body"); + } +} +} diff --git a/modules/core/tests/unit.cpp b/modules/core/tests/unit.cpp new file mode 100644 index 0000000..1c187c0 --- /dev/null +++ b/modules/core/tests/unit.cpp @@ -0,0 +1,343 @@ +#include + +#include +#include + +#include "../c++/abstract/unit.hpp" +#include "../c++/abstract/unit_print.hpp" +#include "../c++/lbm_unit.hpp" +#include "../c++/particle/schema.hpp" + +namespace { +namespace sch { +using namespace kel::lbm::sch; +using T = Float64; +} + +using kel::lbm::si_type::meter; +using kel::lbm::si_type::second; +using lbm_meter = kel::lbm::lbm_type::meter; + +template +using Met = sch::UnitElement; + +template +using Sec = sch::UnitElement; + +template using U = sch::Unit; + +template +using Mul = typename kel::lbm::unit_multiplication::Schema; + +template +using Div = typename kel::lbm::unit_division::Schema; + +/** + * The unit algebra had no direct coverage: converter.cpp asserts one + * conversion and args.cpp exercises unit_division once. Nothing pinned + * multiplication, exponent cancellation, or the scaling comparison -- and the + * scaling path could not even be instantiated, because unit_multiplication, + * unit_division and the three print patterns all matched the two argument + * form, which means scale 0 after the default argument is substituted. + */ + +SAW_TEST("Unit / multiplication folds same base and same scale"){ + static_assert(std::is_same_v>, U>>, U>>, + "m * m must fold to m^2"); + static_assert(std::is_same_v>, U>>, U>>, + "km * km must fold to km^2, keeping the scale"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / different scales do not fold"){ + // Same base quantity, different prefix: the factors must both survive + // rather than silently combining, because nothing rescales the value. + using Mixed = Mul>, U>>; + static_assert(!std::is_same_v>>, "must not fold to m^2"); + static_assert(!std::is_same_v>>, "must not fold to km^2"); + static_assert(std::is_same_v, Met<1,3>>>, + "both factors must survive, in order"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / division negates the exponent but keeps the scale"){ + // (km)^-1 is scale 3 exponent -1. Negating the scale too would quietly + // mean (mm)^-1. + static_assert(std::is_same_v>, U>>, U>>, + "km^2 / km must be km"); + static_assert(std::is_same_v, U>>, U>>, + "1 / km must be km^-1, not mm^-1"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / cancelling exponents drop out entirely"){ + // m/m must be genuinely dimensionless, not m^0. + static_assert(std::is_same_v>, U>>, sch::Pure>, + "m / m must reduce to Pure"); + static_assert(std::is_same_v>, U>>, sch::Pure>, + "km / km must reduce to Pure"); + // But only when the scales agree. + static_assert(!std::is_same_v>, U>>, sch::Pure>, + "km / m must not cancel"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / lattice and SI metres never fold"){ + // This is what actually separates lattice from SI -- distinct base tag + // types, not the scale. + using LbmMet = sch::UnitElement; + using Mixed = Mul>, U>; + static_assert(!std::is_same_v>>, "must not fold"); + // Canonical order, not encounter order: "meter_lbm" sorts before "meter_si". + static_assert(std::is_same_v>>, "both survive, sorted"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / existing aliases are unchanged"){ + // Regression pin: widening the patterns must not have altered any + // composition that lbm_unit.hpp already produced. + static_assert(std::is_same_v, U>>, "SiMeter"); + static_assert(std::is_same_v, U, Sec<-1>>>, + "SiVelocity"); + static_assert( + std::is_same_v, U, Sec<-1>>>, + "SiKinematicViscosity"); + static_assert( + std::is_same_v, sch::SiSecond>, + sch::SiVelocity>, + "m / s must be the velocity alias exactly"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / a scaled unit prints with its prefix"){ + std::stringstream plain, kilo, milli; + + kel::lbm::data>> p{2.0}; + kel::lbm::data>> k{2.0}; + kel::lbm::data>> m{2.0}; + + plain << p; + kilo << k; + milli << m; + + SAW_EXPECT(plain.str() == "2 [m_si]", "unscaled prints bare"); + SAW_EXPECT(kilo.str() == "2 [km_si]", "kilo prints a k prefix"); + SAW_EXPECT(milli.str() == "2 [mm_si]", "milli prints an m prefix"); + // The whole point: a scaled unit must not render identically to an + // unscaled one, or the output lies about the value. + SAW_EXPECT(plain.str() != kilo.str(), "scaled must differ from unscaled"); +} + +SAW_TEST("Unit / an off table scale prints explicitly"){ + std::stringstream o; + kel::lbm::data>> odd{2.0}; + o << odd; + // No SI letter exists for 10^4, so it must say so rather than drop it. + SAW_EXPECT(o.str() == "2 [10^(4)*m_si]", "unknown scale prints as a power"); +} +} + +namespace { +/** + * The particle position is unit typed. These pin the two things that are easy + * to break without noticing. + */ +SAW_TEST("Unit / a united wrapper is layout compatible with its base"){ + // The whole device path depends on this and nothing else checks it: the + // particle group crosses to the GPU through sycl_copy_helper, + // which is a bulk h.copy of the element block and never recurses into the + // element schema. A unit wrapper that changed the size would corrupt every + // particle silently rather than failing to compile. + using Raw = kel::lbm::data>; + using United = kel::lbm::data>>; + + static_assert(sizeof(Raw) == sizeof(United), + "A unit wrapper must not change the layout"); + static_assert(std::is_trivially_copyable_v, + "A unit wrapper must stay memcpy-able"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / particle position carries lattice metres"){ + kel::lbm::data> rb; + + auto &pos = rb.template get<"position">(); + auto &pos_old = rb.template get<"position_old">(); + auto &acc = rb.template get<"acceleration">(); + + static_assert( + std::is_same_v, + kel::lbm::data>>>, + "position must be a lattice length"); + static_assert( + std::is_same_v, + std::remove_cvref_t>, + "position_old must match position"); + static_assert( + std::is_same_v, + kel::lbm::data>>>, + "acceleration must be a lattice acceleration"); + + // Element access still reaches the raw component. + pos.at({{0u}}).set(3.0); + SAW_EXPECT(pos.at({{0u}}).get() == 3.0, "element access must still work"); + + // A difference of two lengths is a length; its dot product is an area. + auto delta = pos - pos_old; + static_assert(std::is_same_v>, + "length minus length is a length"); + + auto area = kel::lbm::math::dot(delta, delta); + static_assert( + std::is_same_v, + sch::UnitElement>>>, + "dot of two lengths must be an area"); + SAW_EXPECT(area.at({}).get() == 9.0, "and the value must be right"); +} +} + +namespace { +/** + * Commutativity at the type level. + * + * m * s and s * m are the same physical unit. If the reduction preserves + * encounter order rather than imposing a canonical one, they become distinct + * C++ types and the same quantity reached two different ways stops matching. + */ +SAW_TEST("Unit / multiplication is commutative in the type"){ + using A = Mul>, U>>; + using B = Mul>, U>>; + static_assert(std::is_same_v, "m*s and s*m must be the same type"); + SAW_EXPECT(true, "static assert carries this one"); +} +} + +namespace { +/** + * An alias written by hand must already be in canonical order, or it will not + * match the same unit arrived at by arithmetic. + * + * Multiplying by a dimensionless unit runs the alias back through the + * reduction and the sort, so if its factors were declared out of order the + * round trip changes the type and this fails. + */ +template +inline constexpr bool is_canonical = + std::is_same_v, Alias>, Alias>; + +SAW_TEST("Unit / division is commutative in the type too"){ + // a/b reached two ways must agree. + using A = Div>, U>>, U>>; + using B = Div>, U>>, U>>; + static_assert(std::is_same_v, "operand order must not change the type"); + SAW_EXPECT(true, "static assert carries this one"); +} + +SAW_TEST("Unit / three factors sort canonically whatever the order"){ + using Kv = sch::UnitElement; + using Sl = sch::UnitElement; + + using One = Mul>, U>, U>; + using Two = Mul, U>>, U>; + using Three = Mul, U>, U>>; + + static_assert(std::is_same_v, "any permutation must agree"); + static_assert(std::is_same_v, "any permutation must agree"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / every lbm_unit alias is already canonical"){ + static_assert(is_canonical>, "SiMeter"); + static_assert(is_canonical>, "LbmMeter"); + static_assert(is_canonical>, "SiSecond"); + static_assert(is_canonical>, "LbmSecond"); + static_assert(is_canonical>, "SiVelocity"); + static_assert(is_canonical>, "LbmVelocity"); + static_assert(is_canonical>, "SiAcceleration"); + static_assert(is_canonical>, "LbmAcceleration"); + static_assert(is_canonical>, "SiKinematicViscosity"); + static_assert(is_canonical>, "LbmKinematicViscosity"); + SAW_EXPECT(true, "static asserts carry this one"); +} +} + +namespace { +/** + * Velocity is typed through the kinematics and unwrapped at the force law, + * because force needs a mass base unit that does not exist yet. + */ +SAW_TEST("Unit / a length over a time is a velocity"){ + using Vel = Div, sch::LbmSecond>; + static_assert(std::is_same_v>, + "m / s must be exactly the LbmVelocity alias"); + + // And a velocity times a time is back to a length. + using Len = Mul, sch::LbmSecond>; + static_assert(std::is_same_v>, + "velocity * time must be a length again"); + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Unit / a normalised vector is Pure, not merely unannotated"){ + kel::lbm::data>> v; + v.at({{0u}}).set(3.0); + v.at({{1u}}).set(4.0); + v.at({{2u}}).set(0.0); + + auto dir = kel::lbm::math::normalize(v); + static_assert( + std::is_same_v>>>, + "a direction must be Pure"); + + SAW_EXPECT(std::abs(dir.at({{0u}}).get() - 0.6) < 1e-12, "x component"); + SAW_EXPECT(std::abs(dir.at({{1u}}).get() - 0.8) < 1e-12, "y component"); + + // The length keeps the unit it measured. + auto len = kel::lbm::math::norm(v); + static_assert(std::is_same_v>>>, + "a length must stay a length"); + SAW_EXPECT(std::abs(len.at({}).get() - 5.0) < 1e-12, "norm value"); +} +} + +namespace { +/** + * sqrt halves the exponents, so the root of an area is a length. This is what + * lets `pos_rel / sqrt(distance_squared)` come out as a dimensionless + * direction rather than an unlabelled vector. + */ +SAW_TEST("Unit / sqrt of an area is a length"){ + using Area = Mul>, sch::LbmMeter>>; + + kel::lbm::data a; + a.at({}).set(9.0); + + auto len = kel::lbm::math::sqrt(a); + static_assert( + std::is_same_v>>>, + "sqrt(m^2) must be m"); + SAW_EXPECT(std::abs(len.at({}).get() - 3.0) < 1e-12, "and the value must be right"); + + // A length divided by a length is a direction scale: dimensionless. + using Ratio = Div>, sch::LbmMeter>>; + static_assert(std::is_same_v>>, + "m / m must be Pure"); + SAW_EXPECT(true, "static asserts carry the rest"); +} + +SAW_TEST("Unit / sqrt refuses an odd exponent"){ + // m^3 has no expressible square root in this system. The static_assert in + // math::sqrt is what stops it, so this test records the intent rather + // than invoking it -- instantiating it would fail the build by design. + using Cube = Mul>, + sch::LbmMeter>>, + sch::LbmMeter>>; + static_assert(std::is_same_v, + sch::UnitElement>>, + "m*m*m must be m^3"); + SAW_EXPECT(true, "sqrt of this would not compile, by design"); +} +} diff --git a/modules/core/tests/voxel_narrow.cpp b/modules/core/tests/voxel_narrow.cpp index 5cffc76..08c1c1f 100644 --- a/modules/core/tests/voxel_narrow.cpp +++ b/modules/core/tests/voxel_narrow.cpp @@ -15,7 +15,8 @@ using T = Float64; constexpr uint64_t D = 2u; auto make_pair(double radius, uint64_t res = 32u){ - kel::lbm::data> r,d; + kel::lbm::data>> r; + kel::lbm::data> d; r.at({}).set(radius); d.at({}).set(1.0); return kel::lbm::create_spheroid_particle_group(r,d,{res}); diff --git a/modules/core/tests/vtk_write.cpp b/modules/core/tests/vtk_write.cpp index b2a5682..61fb548 100644 --- a/modules/core/tests/vtk_write.cpp +++ b/modules/core/tests/vtk_write.cpp @@ -31,7 +31,7 @@ using CellStruct = Struct< template using MacroStruct = Struct< - Member, "momentum">, + Member, "velocity">, Member >; @@ -47,7 +47,7 @@ SAW_TEST("VTK Write test example"){ kel::lbm::data, 2>> cells{{{2u,2u}}}; auto& cell_0 = cells.at({{{0,0}}}); - cell_0.template get<"momentum">()= {{0.5,-0.1}}; + cell_0.template get<"velocity">()= {{0.5,-0.1}}; cell_0.template get<"pressure">().set(1.1); auto eov = lbm::impl::lbm_vtk_writer, 2>>::apply(sstream, cells); @@ -74,7 +74,7 @@ SAW_TEST("VTK Write raw test example"){ auto meta = cells.dims(); auto& cell_0 = cells.at({{{0,0}}}); - cell_0.template get<"momentum">()= {{0.5,-0.1}}; + cell_0.template get<"velocity">()= {{0.5,-0.1}}; cell_0.template get<"pressure">().set(1.1); auto eov = lbm::impl::lbm_vtk_writer_raw,2u>::apply(sstream, &cell_0, meta); diff --git a/probe_c.patch b/probe_c.patch new file mode 100644 index 0000000..5950d34 --- /dev/null +++ b/probe_c.patch @@ -0,0 +1,55 @@ +Probe C: split the remaining D3Q27 JIT failure in half. + +WHAT IS BROKEN + At D3Q27 the HlbmOneParticleGather kernel fails AdaptiveCpp's SSCP JIT at + run time, every launch: + + LLVMToHost: clang invocation failed with exit code 1 + + The kernel then never executes, so the particle never moves and its force + reads exactly 0. It builds fine -- this only shows up when running. + The same chain at D2Q9 is fine, so it is specific to 3D. + +WHAT IS ALREADY KNOWN + Inserting an early return at successive points in the gather: + + after the AABB setup ................................. JIT passes + + iterator<3>::apply with an empty body .............. JIT passes + + porosity, before the 27-direction loop ............. THIS PROBE + full gather .......................................... JIT fails + + So the failure is inside the per-cell lambda, and this probe is the one + that separates its two halves. + +HOW TO READ THE RESULT + JIT passes -> the 27-direction momentum-exchange loop is the culprit. + JIT fails -> the particle_porosity call is the culprit. + +APPLY / REVERT (from the repo root) + patch -p1 < probe_c.patch + patch -p1 -R < probe_c.patch + +RUN + nix-build -A ex.poiseulle_particle_3d_hlbm_gpu + HOME=$(mktemp -d) ./result/bin/* --name probe_c --time_steps 2 2>&1 \ + | grep -c 'clang invocation failed' + + 0 = JIT passed, nonzero = still failing. Expect roughly one failure per + launch per step, so 2 steps gives 0 or ~2-4. + +NOTE + This makes the gather compute no force at all -- it is a diagnostic, not a + fix. Revert before running anything real. + +--- a/modules/core/c++/hlbm.hpp ++++ b/modules/core/c++/hlbm.hpp +@@ -188,6 +188,9 @@ + return; + } + ++ // --- PROBE C: stop here, before the 27-direction loop. --- ++ return; ++ + kel::lbm::data> momentum; + for(uint64_t i{0u}; i < Desc::D; ++i){ + momentum.at({{i}}) = 0.0; -- cgit v1.3.1