From e821d617802ebbfff756844e4d9720342fc78a4e Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 13 Nov 2026 14:12:49 +0100 Subject: Swarm POC --- examples/cavity_2d/.nix/derivation.nix | 33 - examples/cavity_2d/SConscript | 32 - examples/cavity_2d/SConstruct | 79 --- examples/cavity_2d/cavity_2d.cpp | 456 ------------- examples/cavity_2d_gpu/.nix/derivation.nix | 3 + examples/cavity_2d_gpu/SConstruct | 2 + examples/cavity_2d_gpu/cavity_2d_gpu.cpp | 726 ++++++++++++--------- .../heterogeneous_computing/.nix/derivation.nix | 41 -- examples/heterogeneous_computing/README.md | 7 - examples/heterogeneous_computing/SConscript | 34 - examples/heterogeneous_computing/SConstruct | 82 --- examples/heterogeneous_computing/sim.cpp | 95 --- examples/meta/.nix/derivation.nix | 33 - examples/meta/SConscript | 32 - examples/meta/SConstruct | 79 --- examples/meta/meta.cpp | 133 ---- examples/particles_gpu/.nix/derivation.nix | 37 -- examples/particles_gpu/SConscript | 34 - examples/particles_gpu/SConstruct | 82 --- examples/particles_gpu/particles_gpu.cpp | 136 ---- examples/poiseulle_2d_gpu/.nix/derivation.nix | 2 + examples/poiseulle_2d_gpu/SConstruct | 4 +- examples/poiseulle_2d_gpu/poiseulle_2d_gpu.cpp | 680 ++++++++++--------- examples/poiseulle_3d/.nix/derivation.nix | 4 +- examples/poiseulle_3d/SConstruct | 2 + examples/poiseulle_3d/poiseulle_3d.cpp | 713 ++++++++++---------- examples/stdkr_2d_bgk/sim.cpp | 5 +- examples/stdkr_2d_bgk/step.hpp | 2 +- examples/throat_clog_2d/init.hpp | 36 +- examples/throat_clog_2d/sim.cpp | 80 ++- examples/throat_clog_2d/step.hpp | 32 +- 31 files changed, 1203 insertions(+), 2513 deletions(-) delete mode 100644 examples/cavity_2d/.nix/derivation.nix delete mode 100644 examples/cavity_2d/SConscript delete mode 100644 examples/cavity_2d/SConstruct delete mode 100644 examples/cavity_2d/cavity_2d.cpp delete mode 100644 examples/heterogeneous_computing/.nix/derivation.nix delete mode 100644 examples/heterogeneous_computing/README.md delete mode 100644 examples/heterogeneous_computing/SConscript delete mode 100644 examples/heterogeneous_computing/SConstruct delete mode 100644 examples/heterogeneous_computing/sim.cpp delete mode 100644 examples/meta/.nix/derivation.nix delete mode 100644 examples/meta/SConscript delete mode 100644 examples/meta/SConstruct delete mode 100644 examples/meta/meta.cpp delete mode 100644 examples/particles_gpu/.nix/derivation.nix delete mode 100644 examples/particles_gpu/SConscript delete mode 100644 examples/particles_gpu/SConstruct delete mode 100644 examples/particles_gpu/particles_gpu.cpp (limited to 'examples') diff --git a/examples/cavity_2d/.nix/derivation.nix b/examples/cavity_2d/.nix/derivation.nix deleted file mode 100644 index d0660e9..0000000 --- a/examples/cavity_2d/.nix/derivation.nix +++ /dev/null @@ -1,33 +0,0 @@ -{ lib -, stdenv -, scons -, clang-tools -, forstio -, pname -, version -, kel -}: - -stdenv.mkDerivation { - pname = pname + "-examples-" + "cavity_2d"; - inherit version; - src = ./..; - - nativeBuildInputs = [ - scons - clang-tools - ]; - - buildInputs = [ - forstio.core - forstio.async - forstio.codec - forstio.codec-unit - forstio.codec-json - kel.lbm.core - ]; - - preferLocalBuild = true; - - outputs = [ "out" "dev" ]; -} diff --git a/examples/cavity_2d/SConscript b/examples/cavity_2d/SConscript deleted file mode 100644 index 3fabe4b..0000000 --- a/examples/cavity_2d/SConscript +++ /dev/null @@ -1,32 +0,0 @@ -#!/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.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, ['cavity_2d.cpp'], shared=False); -examples_env.cavity_2d = examples_env.Program('#bin/cavity_2d', [examples_objects]); - -# Set Alias -env.examples = [ - examples_env.cavity_2d -]; -env.Alias('examples', env.examples); -env.targets += ['examples']; -env.Install('$prefix/bin/', env.examples); diff --git a/examples/cavity_2d/SConstruct b/examples/cavity_2d/SConstruct deleted file mode 100644 index a7201cb..0000000 --- a/examples/cavity_2d/SConstruct +++ /dev/null @@ -1,79 +0,0 @@ -#!/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' - ] -); -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/cavity_2d/cavity_2d.cpp b/examples/cavity_2d/cavity_2d.cpp deleted file mode 100644 index ea63745..0000000 --- a/examples/cavity_2d/cavity_2d.cpp +++ /dev/null @@ -1,456 +0,0 @@ -#include - -#include - -#include -#include -#include - -namespace kel { -namespace lbm { -namespace sch { - -/** - * Basic distribution function - * Base type - * D - * Q - * Scalar factor - * D factor - * Q factor - */ -using T = Float32; -using D2Q5 = Descriptor<2u,5u>; -using D2Q9 = Descriptor<2u,9u>; - -template -using DfCell = Cell; - -template -using CellInfo = Cell; - -/** - * Basic type for simulation - */ -template -using CellStruct = Struct< - Member, "dfs">, - Member, "dfs_old">, - Member, "info"> ->; - -/** - * 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, 0u, DimX, DimY>, "velocity">, - Member, 0u, DimX, DimY>, "density"> ->; - -using CavityFieldD2Q9 = CellField>; -} - - -/* -template -class df_cell_view; -*/ -/** - * Minor helper for the AA-Pull Pattern, so I can use only one lattice - * - * Am I sure I want to use AA this way? - * Esoteric Twist technically reduces the needed memory access footprint - */ -/* -template -class df_cell_view, Encode> { -public: - using Schema = sch::Cell; -private: - std::array::type>*, QN> view_; -public: - df_cell_view(const std::array::type>*, QN>& view): - view_{view} - {} -}; -*/ -namespace cmpt { -struct MovingWall {}; -} - -/** - * Full-Way moving wall Bounce back, something is not right here. - * Technically it should reflect properly. - */ -template -class component { -public: - std::array::type, Desc::D> lid_vel; - -public: - void apply( - kel::lbm::data>& dfs - ){ - using dfi = df_info; - - // Technically use .copy() - /* - auto dfs_cpy = dfs; - - for(uint64_t i = 0u; i < Desc::Q; ++i){ - dfs({dfi::opposite_index.at(i)}) = dfs_cpy({i}) - 2.0 * dfi::weights[i] * 1.0 * ( lid_vel[0] * dfi::directions[i][0] + lid_vel[1] * dfi::directions[i][1]) * dfi::inv_cs2; - } - */ - } -}; -} -} - -constexpr size_t dim_size = 2; -constexpr size_t dim_x = 128; -constexpr size_t dim_y = 128; - - -template -void apply_for_cells(Func&& func, kel::lbm::data& dat){ - for(std::size_t i = 0; i < dat.meta().at({1u}).get(); ++i){ - for(std::size_t j = 0; j < dat.meta().at({0u}).get(); ++j){ - kel::lbm::data di{i}; - kel::lbm::data dj{j}; - auto& cell_v = dat({{dj,di}}); - func(cell_v, j, i); - } - } -} - -void set_geometry(kel::lbm::data& latt){ - using namespace kel::lbm; - apply_for_cells([](auto& cell, std::size_t i, std::size_t j){ - uint8_t val = 0; - if(j == 1){ - val = 2u; - } - if(i == 1 || (i+2) == dim_x || (j+2) == dim_y){ - val = 3u; - } - if(i > 1 && (i+2) < dim_x && j > 1 && (j+2) < dim_y){ - val = 1u; - } - if(i == 0 || j == 0 || (i+1) == dim_x || (j+1) == dim_y){ - val = 0u; - } - cell.template get<"info">()(0u).set(val); - }, latt); -} - -void set_initial_conditions(kel::lbm::data& latt){ - using namespace kel::lbm; - - kel::lbm::data rho{1.0}; - { - kel::lbm::data> vel{{0.0,0.0}}; - auto eq = equilibrium(rho, vel); - - apply_for_cells([&eq](auto& cell, std::size_t i, std::size_t j){ - (void) i; - (void) j; - auto& dfs = cell.template get<"dfs">(); - auto& dfs_old = cell.template get<"dfs_old">(); - auto info = cell.template get<"info">()(0u).get(); - for(uint64_t k = 0; k < sch::D2Q9::Q; ++k){ - dfs(k) = eq.at({k}); - dfs_old(k) = eq.at({k}); - } - }, latt); - } - - { - kel::lbm::data> vel{{0.1,0.0}}; - auto eq = equilibrium(rho, vel); - - apply_for_cells([&eq](auto& cell, std::size_t i, std::size_t j){ - (void) i; - (void) j; - auto& dfs = cell.template get<"dfs">(); - auto& dfs_old = cell.template get<"dfs_old">(); - auto info = cell.template get<"info">()(0u).get(); - if(info == 2u){ - for(uint64_t k = 0; k < sch::D2Q9::Q; ++k){ - dfs(k) = eq.at({k}); - dfs_old(k) = eq.at({k}); - } - } - }, latt); - } -} - -void lbm_step( - kel::lbm::data& latt, - bool even_step, - uint64_t time_step -){ - using namespace kel::lbm; - using dfi = df_info; - - /** - * 1. Relaxation parameter \tau - */ - component coll{0.5384}; - component bb; - - component bb_lid; - bb_lid.lid_vel = {0.1,0.0}; - - auto meta = latt.meta(); - - // Collide - for(kel::lbm::data i{0u}; i < meta.at(0u); ++i){ - for(kel::lbm::data j{0u}; j < meta.at(1u); ++j ){ - auto& cell = latt({{i,j}}); - auto& info = cell.template get<"info">(); - - switch(info({0u}).get()){ - case 1u: { - coll.apply(latt, {{i,j}}, time_step); - break; - } - case 2u: { - auto& df = even_step ? cell.template get<"dfs_old">() : cell.template get<"dfs">(); - bb_lid.apply(df); - break; - } - case 3u: { - bb.apply(latt, {{i,j}}, time_step); - break; - } - default: - break; - } - } - } - - // Stream - for(uint64_t i = 1u; (i+1u) < latt.template get_dim_size<0>().get(); ++i){ - for(uint64_t j = 1u; (j+1u) < latt.template get_dim_size<1>().get(); ++j){ - auto& cell = latt({{i,j}}); - auto& df_new = even_step ? cell.template get<"dfs">() : cell.template get<"dfs_old">(); - auto& info_new = cell.template get<"info">(); - - if(info_new({0u}).get() > 0u && info_new({0u}).get() != 2u){ - for(uint64_t k = 0u; k < sch::D2Q9::Q; ++k){ - auto dir = dfi::directions[dfi::opposite_index[k]]; - auto& cell_dir_old = latt({{i+dir[0],j+dir[1]}}); - - auto& df_old = even_step ? cell_dir_old.template get<"dfs_old">() : cell_dir_old.template get<"dfs">(); - auto& info_old = cell_dir_old.template get<"info">(); - - if( info_old({0}).get() == 2u ){ - auto& df_old_loc = even_step ? latt({{i,j}}).template get<"dfs_old">() : latt({{i,j}}).template get<"dfs">(); - df_new({k}) = df_old_loc({dfi::opposite_index.at(k)}) - 2.0 * dfi::inv_cs2 * dfi::weights.at(k) * 1.0 * ( bb_lid.lid_vel[0] * dir[0] + bb_lid.lid_vel[1] * dir[1]); - // dfs({dfi::opposite_index.at(i)}) = dfs_cpy({i}) - 2.0 * dfi::weights[i] * 1.0 * ( lid_vel[0] * dfi::directions[i][0] + lid_vel[1] * dfi::directions[i][1]) * dfi::inv_cs2; - } else { - df_new({k}) = df_old({k}); - } - } - } - } - } -} - -int main(){ - using namespace kel::lbm; - - kel::lbm::data> dim{{dim_x, dim_y}}; - - kel::lbm::data lattice{dim}; - - converter conv{ - {0.1}, - {0.1} - }; - - // 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){ - // lattices.at(i) = {dim_x, dim_y}; - //} - - /** - * Set meta information describing what this cell is - */ - set_geometry(lattice); - - /** - * - */ - set_initial_conditions(lattice); - - /** - * Timeloop - */ - - /** - * Print basic setup info - */ - apply_for_cells([](auto& cell, std::size_t i, std::size_t j){ - // Not needed - (void) j; - std::cout<<(static_cast(cell.template get<"info">()({0}).get())); - if( (i+1) < dim_x){ - std::cout<<" "; - }else{ - std::cout<<"\n"; - } - }, lattice); - - uint64_t lattice_steps = 512000u; - bool even_step = true; - - uint64_t print_every = 256u; - uint64_t file_no = 0u; - - // 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){ - - if(step % print_every == 0u){ - std::cout<<"\n"; - typename kel::lbm::native_data_type::type sum = 0.0; - - - apply_for_cells([&](auto& cell, std::size_t i, std::size_t j){ - auto& dfs = cell.template get<"dfs">(); - // 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; - } - auto angle = std::atan2(vel[1],vel[0]); - double vel_mag = vel[1] * vel[1] + vel[0] * vel[0]; - - auto dir_char = [&]() -> std::string_view { - /** - * Flipped y due to print order - */ - constexpr auto pi = M_PI; - return "■"; - if(vel_mag < 1e-4){ - return "•"; - } - if(angle > 7.0 / 8.0 * pi){ - return "←"; - } - if(angle > 5.0 / 8.0 * pi){ - return "↙"; - } - if(angle > 3.0 / 8.0 * pi){ - return "↓"; - } - if(angle > 1.0 / 8.0 * pi){ - return "↘"; - } - if(angle > -1.0 / 8.0 * pi){ - return "→"; - } - if(angle > -3.0 / 8.0 * pi){ - return "↗"; - } - if(angle > -5.0 / 8.0 * pi){ - return "↑"; - } - if(angle > -7.0 / 8.0 * pi){ - return "↖"; - } - return "←"; - }(); - - std::array rgb_start{64,64,255}; - std::array rgb_stop{255,64,64}; - std::array rgb_middle{255,255,255}; - - - auto col_interpol = [&](){ - std::array rgb_interpol = rgb_start; - double vel_mag_cut = std::min(1.0,std::max(vel_mag/(0.07*0.07),0.0)); - - if(vel_mag_cut < 0.5){ - uint32_t vel_mag_i = static_cast(2.0 * vel_mag_cut * 256); - for(uint8_t i = 0u; i < 3u; ++i){ - rgb_interpol[i] = (rgb_middle[i] * vel_mag_i + (256-vel_mag_i) * rgb_start[i]) / 256; - } - }else{ - uint32_t vel_mag_i = static_cast((2.0*(vel_mag_cut-0.5)) * 256); - for(uint8_t i = 0u; i < 3u; ++i){ - rgb_interpol[i] = (rgb_stop[i] * vel_mag_i + (256-vel_mag_i) * rgb_middle[i]) / 256; - } - } - return rgb_interpol; - }; - auto rgb_interpol = col_interpol(); - - std::cout<<"\x1b[38;2;"<() : cell.template get<"dfs">(); - - 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); - - write_vtk_file("tmp", "cavity_2d", file_no, macros); - - ++file_no; - } - - lbm_step(lattice, even_step, step); - - even_step = not even_step; - } - - /** - * Flush cout - */ - std::cout<<"\n\n"; - std::cout.flush(); - return 0; -} diff --git a/examples/cavity_2d_gpu/.nix/derivation.nix b/examples/cavity_2d_gpu/.nix/derivation.nix index 8724d96..3b210a3 100644 --- a/examples/cavity_2d_gpu/.nix/derivation.nix +++ b/examples/cavity_2d_gpu/.nix/derivation.nix @@ -23,13 +23,16 @@ stdenv.mkDerivation { buildInputs = [ forstio.core + forstio.io forstio.async forstio.codec forstio.codec-unit forstio.remote + forstio.remote-filesystem forstio.codec-json adaptive-cpp kel.lbm.core + kel.lbm.sycl # kel-lbm.sycl ]; diff --git a/examples/cavity_2d_gpu/SConstruct b/examples/cavity_2d_gpu/SConstruct index fe206e1..7882265 100644 --- a/examples/cavity_2d_gpu/SConstruct +++ b/examples/cavity_2d_gpu/SConstruct @@ -59,6 +59,8 @@ env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], ], LIBS=[ 'forstio-core', + 'forstio-async', + 'forstio-io', 'acpp-rt', 'omp' ] diff --git a/examples/cavity_2d_gpu/cavity_2d_gpu.cpp b/examples/cavity_2d_gpu/cavity_2d_gpu.cpp index 55225d1..6b71db3 100644 --- a/examples/cavity_2d_gpu/cavity_2d_gpu.cpp +++ b/examples/cavity_2d_gpu/cavity_2d_gpu.cpp @@ -1,384 +1,464 @@ +#include #include -#include -#include - -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include namespace kel { namespace lbm { + +// Square cavity, as this case has always used. +constexpr uint64_t dim_x = 128ul; +constexpr uint64_t dim_y = 128ul; + +// Lid velocity, tangential along +x on the top row. +constexpr double lid_velocity = 0.1; + namespace sch { -/** - * Basic distribution function - * Base type - * D - * Q - * Scalar factor - * D factor - * Q factor - */ -using T = Float32; -using D2Q9 = Descriptor<2u,9u>; - -template -using DfCell = Cell; - -template -using CellInfo = Cell; - -/** - * Basic type for simulation - */ -template -using CellStruct = CellFieldStruct< - Desc, - Struct< - Member>, "dfs">, - Member>, "dfs_old">, - Member>, "info"> - > ->; +using InfoChunk = Chunk; -template -using MacroStruct = Struct< - Member, "velocity">, - Member ->; +template +using DfChunk = Chunk, 1u, dim_x, dim_y>; -using CavityFieldD2Q9 = CellStruct; -} +template +using ScalarChunk = Chunk, 0u, dim_x, dim_y>; +template +using VectorChunk = Chunk, 0u, dim_x, dim_y>; -/* -template -class df_cell_view; -*/ -/** - * Minor helper for the AA-Pull Pattern, so I can use only one lattice - * - * Am I sure I want to use AA this way? - * Esoteric Twist technically reduces the needed memory access footprint - */ -/* -template -class df_cell_view, Encode> { -public: - using Schema = sch::Cell; -private: - std::array::type>*, QN> view_; -public: - df_cell_view(const std::array::type>*, QN>& view): - view_{view} - {} -}; -*/ -namespace cmpt { -struct MovingWall {}; -} +template +using ChunkStruct = Struct< + Member, + Member, "dfs">, + Member, "dfs_old"> +>; -/** - * Full-Way moving wall Bounce back, something is not right here. - * Technically it should reflect properly. - */ -template -class component { -public: - std::array::type, Desc::D> lid_vel; - -public: - void apply( - kel::lbm::data>& dfs - ){ - using dfi = df_info; - - // Technically use .copy() - /* - auto dfs_cpy = dfs; +template +using VelChunk = Chunk, 0u, dim_x, dim_y>; - for(uint64_t i = 0u; i < Desc::Q; ++i){ - dfs({dfi::opposite_index.at(i)}) = dfs_cpy({i}) - 2.0 * dfi::weights[i] * 1.0 * ( lid_vel[0] * dfi::directions[i][0] + lid_vel[1] * dfi::directions[i][1]) * dfi::inv_cs2; - } - */ - } -}; +template +using RhoChunk = Chunk, 0u, dim_x, dim_y>; -constexpr size_t dim_x = 128; -constexpr size_t dim_y = 128; +template +using MacroStruct = Struct< + Member, "velocity">, + Member, "density"> +>; -template -void set_geometry( - kel::lbm::data>* info_field -){ - using namespace kel::lbm; - using namespace acpp; - kel::lbm::data> meta{{dim_x,dim_y}}; - /** - * Set ghost - */ - iterate_over([&](const kel::lbm::data>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& info = info_field[i]; - - info({0u}).set(0u); - - }, {{0u,0u}}, meta); - - /** - * Set wall - */ - iterate_over([&](const kel::lbm::data>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& info = info_field[i]; - - info({0u}).set(1u); - - }, {{0u,0u}}, meta, {{1u,1u}}); - - /** - * Set fluid - */ - iterate_over([&](const kel::lbm::data>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& info = info_field[i]; - - info({0u}).set(2u); - - }, {{0u,0u}}, meta, {{2u,2u}}); - - /** - * Set top lid - */ - iterate_over([&](const kel::lbm::data>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& info = info_field[i]; - info({0u}).set(3u); - - }, {{0u,1u}}, {{meta.at({0u}), 2u}}, {{2u,0u}}); +//template +//using ParticleArray = Array< +// Particle +//>; } -template -void set_initial_conditions( - kel::lbm::data>* info_field, - kel::lbm::data>* dfs_field, - kel::lbm::data>* dfs_old_field +template +kel::lbm::error_or setup_initial_conditions( + kel::lbm::data>& fields, + kel::lbm::data>& macros ){ - using namespace kel::lbm; - using namespace acpp; - - kel::lbm::data rho{1.0}; - kel::lbm::data> vel{{0.0,0.0}}; - auto eq = equilibrium(rho, vel); - kel::lbm::data> meta{{dim_x,dim_y}}; - - /** - * Set distribution - */ - iterate_over([&](const kel::lbm::data>& index){ - size_t i = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto& dfs = dfs_field[i]; - auto& dfs_old = dfs_old_field[i]; - - for(kel::lbm::data k = 0; k < kel::lbm::data{Desc::Q}; ++k){ - dfs(k) = eq.at(k); - dfs_old(k) = eq.at(k); - } + 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}} + ); + + // The lid: the top row, moving tangentially. Corners stay plain walls. + iterator::apply( + [&](auto& index){ + info_f.at(index).set(5u); + }, + {{0u,dim_y-1u}}, + {{dim_x,dim_y}}, + {{1u,0u}} + ); + // + auto& df_f = fields.template get<"dfs_old">(); + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + + iterator::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + rho.at({}) = {1}; + auto& vel = vel_f.at(index); + auto eq = equilibrium(rho,vel); + + 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& vel = vel_f.at(index); + if(info_f.at(index).get() == 2u){ + vel.at({{0u}}) = 0.0; + } + auto eq = equilibrium(rho,vel); + + df = eq; + }, + {},// 0-index + df_f.get_dims(), + {{1u,1u}} + ); + + iterator::apply( + [&](auto& index){ + kel::lbm::data> middle, ind_vec; + middle.at({{0u}}) = dim_x * 0.25; + middle.at({{1u}}) = dim_y * 0.5; + + ind_vec.at({{0u}}) = index.at({{0u}}).template cast_to(); + ind_vec.at({{1u}}) = index.at({{1u}}).template cast_to(); + + auto dist = middle - ind_vec; + auto dist_2 = kel::lbm::math::dot(dist,dist); + if(dist_2.at({}).get() < dim_y*dim_y*0.01){ + info_f.at(index) = 1u; + } + }, + {},// 0-index + df_f.get_dims() + ); - }, {{0u,0u}}, meta); + return kel::lbm::make_void(); } template -void lbm_step( - kel::lbm::data>* info_r, - kel::lbm::data>* dfs_r, - kel::lbm::data>* dfs_r_old, - bool is_even, - uint64_t time_step, - acpp::sycl::queue& sycl_q +kel::lbm::error_or step( + kel::lbm::data>,encode::Sycl>& fields, + kel::lbm::data>,encode::Sycl>& macros, + kel::lbm::data t_i, + device& dev ){ - using namespace kel::lbm; - using namespace acpp; - - using dfi = df_info; - - //component coll{0.59}; - constexpr kel::lbm::data frequency{1.0 / 0.59}; - //component bb; - component bb_lid; - bb_lid.lid_vel = {0.1, 0.0}; - - // Submit collision kernel - sycl_q.submit([&](sycl::handler& cgh) { - // Accessor for latt with read/write - - cgh.parallel_for( - sycl::range<2>{dim_x, dim_y}, [=](sycl::id<2> idx) { - size_t i = idx[0]; - size_t j = idx[1]; - - size_t acc_id = i * dim_x + j; - - // Read cell info - auto& info = info_r[acc_id]; - - switch (info({0u}).get()) { - case 1u: { - // bb.apply(latt_acc, {i, j}, time_step); + 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{0.8}; + component> bb; + component,encode::Sycl> abb; + + kel::lbm::data> rho_b; + rho_b.at({}) = 1.0; + kel::lbm::data> vel_b; + vel_b.at({{0u}}) = 0.015; + + component> equi{rho_b,vel_b}; + + component,encode::Sycl> flow_in{ + [&](){ + uint64_t target_t_i = 32u; + if(t_i.get() < target_t_i){ + return 1.0 + (0.0002 / target_t_i) * t_i.get(); + } + return 1.0002; + }() + }; + component,encode::Sycl> flow_out{1.0}; - auto& dfs_old = is_even ? dfs_r_old[acc_id] : dfs_r[acc_id]; - auto df_cpy = dfs_old.copy(); - for(uint64_t i = 1u; i < Desc::Q; ++i){ - dfs_old({i}) = df_cpy({dfi::opposite_index.at(i)}); - } + h.parallel_for(acpp::sycl::range{dim_x,dim_y}, [=](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 2u: { - // coll.apply(latt_acc, {i, j}, time_step); - auto& dfs_old = is_even ? dfs_r_old[acc_id] : dfs_r[acc_id]; - - kel::lbm::data rho; - kel::lbm::data> vel; - compute_rho_u(dfs_old,rho,vel); - auto eq = equilibrium(rho,vel); - - for(uint64_t i = 0u; i < Desc::Q; ++i){ - dfs_old({i}) = dfs_old({i}) + frequency * (eq.at(i) - dfs_old({i})); - } + case 1u: + bb.apply(fields,index,t_i); break; - } - default: - // Do nothing + case 2u: + collision.apply(fields,macros,index,t_i); + break; + case 5u: { + /** + * Moving wall: bounce back plus the tangential momentum the + * lid injects. + * + * f_i = f_opp + 2 w_i rho_w (e_i . u_w) / cs^2 + * + * written from f_opp because that is the direction the + * library's BounceBack writes in. There is no component for a + * y normal moving wall, and the original example had this + * commented out, so it is implemented here. + */ + using dfi = df_info; + const bool is_even = ((t_i.get() % 2u) == 0u); + auto& dfs_f = (is_even) + ? fields.template get<"dfs_old">() + : fields.template get<"dfs">(); + + auto& dfs = dfs_f.at(index); + auto df_cpy = dfs; + + for(uint64_t i = 0u; i < Desc::Q; ++i){ + const auto e_dot_u = + static_cast::type>( + dfi::directions[i][0]) * lid_velocity; + + dfs.at({i}) = df_cpy.at({dfi::opposite_index.at(i)}) + + kel::lbm::data{ + static_cast::type>( + 2.0 * dfi::weights[i] * dfi::inv_cs2 * e_dot_u)}; + } + } break; + default: break; } }); - }); - - // Submit streaming kernel - sycl_q.submit([&](sycl::handler& cgh) { - - cgh.parallel_for( - sycl::range<2>{dim_x - 2, dim_y - 2}, [=](sycl::id<2> idx) { - size_t i = idx[0] + 1; // avoid boundary - size_t j = idx[1] + 1; + }).wait(); - size_t acc_id = i * dim_x + j; + + // Step + /* + q.submit([&](acpp::sycl::handler& h){ + // h.depends_on(collision_ev); + }).wait(); + */ - auto& dfs_new = is_even ? dfs_r[acc_id] : dfs_r_old[acc_id]; - auto& info = info_r[acc_id]; - - - if (info({0u}).get() > 0u && info({0u}).get() != 3u) { - for (uint64_t k = 0u; k < Desc::Q; ++k) { - auto dir = dfi::directions[dfi::opposite_index[k]]; - // auto& cell_dir_old = latt_acc({i + dir[0], j + dir[1]}); - // - size_t acc_old_id = (i+dir[0]) * dim_x + (j+dir[1]); - - auto& dfs_old = is_even ? dfs_r_old[acc_old_id] : dfs_r[acc_old_id]; - auto& info_old = info_r[acc_old_id]; - - if (info_old({0}).get() == 3u) { - auto& dfs_old_loc = is_even ? dfs_r_old[acc_id] : dfs_r[acc_id]; - dfs_new({k}) = dfs_old_loc({dfi::opposite_index.at(k)}) - 2.0 * dfi::inv_cs2 * dfi::weights.at(k) * 1.0 * (bb_lid.lid_vel[0] * dir[0] + bb_lid.lid_vel[1] * dir[1]); - } else { - dfs_new({k}) = dfs_old({k}); - } - } - } - }); - }); + return kel::lbm::make_void(); } } } -int main(){ +template +kel::lbm::error_or lbm_main(int argc, char** argv){ using namespace kel::lbm; - using namespace acpp; - - kel::lbm::data> meta{{dim_x, dim_y}}; - constexpr size_t dim_size = dim_x * dim_y; - converter conv{ - {0.1}, - {0.1} - }; + /* + auto eo_lbm_args = setup_lbm_env,sch::Tuple<>>>(argc,argv); + if(eo_lbm_args.is_error()){ + return std::move(eo_lbm_args.get_error()); + } + auto& lbm_args = eo_lbm_args.get_value(); + */ - // Lid driven cavity: the lid velocity and the cavity height are the - // characteristic scales. - print_lbm_meta(conv, {1e-3}, {0.1}, {dim_y}); + using dfi = df_info; auto eo_lbm_dir = output_directory(); if(eo_lbm_dir.is_error()){ - return -1; + return std::move(eo_lbm_dir.get_error()); } auto& lbm_dir = eo_lbm_dir.get_value(); - auto out_dir = lbm_dir / "cavity_gpu_2d"; - sycl::queue sycl_q{sycl::default_selector_v, sycl::property::queue::in_order{}}; + auto out_dir = lbm_dir / "cavity_2d_gpu"; - constexpr size_t num_cells = dim_x * dim_y; - - // Allocate USM shared memory for your main data - auto* info = sycl::malloc_shared>>(num_cells, sycl_q); - auto* dfs = sycl::malloc_shared>>(num_cells, sycl_q); - auto* dfs_old = sycl::malloc_shared>>(num_cells, sycl_q); + { + 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"); + } + } - /** - * Set meta information describing what this cell is - */ - set_geometry(info); + converter conv { + // delta_x + {1.0}, + // delta_t + {1.0} + }; + + print_lbm_meta(conv,{0.1},{1e-4},{0.4 * dim_y}); + + // kel::lbm::data> meta{{dim_x,dim_y}}; + auto lbm_data_ptr = kel::lbm::heap>>(); + auto lbm_macro_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}; - /** - * - */ - set_initial_conditions(info,dfs,dfs_old); + 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(); - /** - * Timeloop - */ - uint64_t lattice_steps = 512000u; - bool even_step = true; + device dev; - uint64_t print_every = 256u; - uint64_t file_no = 0u; + auto& sycl_q = dev.get_handle(); - kel::lbm::data,sch::D2Q9::D>> macros{meta}; + sycl_q.wait(); + { + auto eov = setup_initial_conditions(*lbm_data_ptr,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_vtk_file(out_dir,"initial_state",0u,*lbm_data_ptr); + if(eov.is_error()){ + return eov; + } + } - std::cout<<"Start"<, encode::Sycl> lbm_sycl_data{sycl_q}; + kel::lbm::data, encode::Sycl> lbm_sycl_macro_data{sycl_q}; + sycl_q.wait(); - auto start = std::chrono::steady_clock::now(); + { + 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; + } + } sycl_q.wait(); - for(uint64_t i = 0u; i < lattice_steps; ++i){ - lbm_step(info, dfs, dfs_old, even_step, i, sycl_q); - if(i % 1024u == 0u){ - sycl_q.wait(); - iterate_over([&](const kel::lbm::data>& index){ - size_t j = index.at({0u}).get() * dim_x + index.at({1u}).get(); - auto dfs_field = even_step ? dfs_old : dfs; - - auto& rho = macros.at(index).template get<"pressure">(); - 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_"}; - vtk_f_name += std::to_string(i) + ".vtk"; - write_vtk_file(vtk_f_name, macros); + auto lsd_view = make_view(lbm_sycl_data); + auto lsdm_view = make_view(lbm_sycl_macro_data); + kel::lbm::data time_steps{16u*4096ul}; + + auto& info_f = lsd_view.template get<"info">(); + + for(kel::lbm::data i{0u}; i < time_steps and krun; ++i){ + // BC + Collision + { + auto eov = step(lsd_view,lsdm_view,i,dev); + if(eov.is_error()){ + return eov; + } } + sycl_q.wait(); + /* + if(i.get() % 128u == 0u){ + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_vtk_file(out_dir,"m",i.get(), *lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + } + */ + /* + if(i.get() % 32u == 0u){ + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_csv_file(out_dir,"m",i.get(), *lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + } + */ + // Stream + sycl_q.submit([&](acpp::sycl::handler& h){ + component> stream; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y}, [=](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(lsd_view,index,i); + } + }); + }).wait(); + wait.poll(); + if(print_status){ + std::cout<<"Status: "<().get() * 100 / time_steps.get())<<"%"<(argc, argv); + if(eov.is_error()){ + auto& err = eov.get_error(); + std::cerr<<"[Error] "< 0u){ + std::cerr<<" - "< -#include - -#include - -namespace kel { -namespace lbm { -namespace sch { - -/** - * struct lbm_data { - * std::array, 64u*64u> dfs; - * std::array info; - * }; - * - * which leads to the form - * - * template - * struct lbm_data { - * std::array, Size*Size> dfs; - * std::array info; - * }; - * - * which transferred into sycl requires us to go to - * - * template - * struct lbm_sycl_data { - * std::array* dfs; - * uint8_t* info; - * }; - * - * in data form on host - * - * template - * using LbmData = Struct< - * Member, Size*Size>, "dfs">, - * Member, "info"> - * >; - * - * If we specialize the encode::Sycl data type, then we get - * With a helper class we can copy single values back and forth and the whole block is guaranteed - * to be allocated. And this one can be dynamic while the host definition might be compile time. - * - * template<...> - * class data,encode::Sycl> final { - * kel::lbm::data> meta; - * kel::lbm::data>* dfs; - * kel::lbm::data* info; - * }; - */ - -template -using CellStruct = Struct< - Member,Desc::D, "dfs">, - Member, "dfs_old">, - Member ->; - -} -template -kel::lbm::error_or simulate(int argc, char** argv, - const kel::lbm::data>& meta -{ - constexpr auto cell_size = sizeof(kel::lbm::data>); - - auto lbm_data = kel::lbm::heap,Desc::D, 64u>>>{meta}; - - return kel::lbm::make_void(); -} - -} - -kel::lbm::error_or lbm_main(int argc, char** argv){ - using namespace lbm; - return simulate(argc, argv); -} -} - -int main(int argc, char** argv){ - auto eov = kel::lbm_main(argc, argv); - if(eov.is_error()){ - auto& err = eov.get_error(); - auto err_msg = err.get_message(); - std::cerr<<"[Error]: "< - -#include - -namespace kel { -namespace lbm { -namespace sch { - -using MetaArgsStruct = Struct< - Member, - Member, - Member, - Member, - Member ->; - -using MetaArgs = Args< - MetaArgsStruct, - Tuple ->; - -using Prec = Float64; -} -} -} - -template -kel::lbm::error_or meta_lbm_info(const kel::lbm::data& args_str){ - using namespace kel::lbm; - converter conv{ - as_unit, sch::LbmMeter>::Schema>( - args_str.template get<"dx">()), - as_unit, sch::LbmSecond>::Schema>( - args_str.template get<"dt">()) - }; - 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(); -} - - -kel::lbm::error_or meta_main(int argc, char** argv){ - using namespace kel::lbm; - - kel::lbm::data args_dat; - auto& args_str = args_dat.template get<"args">(); - - // Defaults for anything the caller leaves out. - args_str.template get<"dx">().set(1); - args_str.template get<"dt">().set(1); - - { - auto eov = parse_args(args_dat, argc, argv); - if(eov.is_error()){ - return eov; - } - } - - auto& args_pos = args_dat.template get<"positionals">(); - - switch(args_pos.template get<0u>().get()){ - case 1u: - { - switch(args_pos.template get<1u>().get()){ - case 3u: - meta_lbm_info>(args_str); - break; - default: - return kel::lbm::make_error("Second positional needs to be a valid momentum set"); - } - } - break; - case 2u: - { - switch(args_pos.template get<1u>().get()){ - case 5u: - meta_lbm_info>(args_str); - break; - case 9u: - meta_lbm_info>(args_str); - break; - default: - return kel::lbm::make_error("Second positional needs to be a valid momentum set"); - } - } - break; - case 3u: - { - switch(args_pos.template get<1u>().get()){ - case 7u: - return kel::lbm::make_error("D3Q7 Velocity Set is not implemented"); - // meta_lbm_info>(args_str); - break; - case 19u: - return kel::lbm::make_error("D3Q17 Velocity Set is not implemented"); - // meta_lbm_info>(args_str); - break; - case 27u: - meta_lbm_info>(args_str); - break; - default: - return kel::lbm::make_error("Second positional needs to be a valid momentum set"); - } - } - break; - default: - return kel::lbm::make_error("First positional needs to be Dimension 1,2 or 3"); - } - - return kel::lbm::make_void(); -} - -int main(int argc, char** argv){ - auto eov = meta_main(argc,argv); - if(eov.is_error()){ - auto& err = eov.get_error(); - - std::cerr<<"[Error]: "< -#include - - -#include -#include - -namespace kel{ -namespace lbm { -namespace sch { -} -} - -kel::lbm::error_or lbm_main(int argc, char** argv){ - (void) argc; - (void) argv; - - using namespace lbm; - using namespace acpp; - - kel::lbm::data>> particles{1024u}; - - for(kel::lbm::data i{0u}; i < kel::lbm::data{32u}; ++i){ - for(kel::lbm::data j{0u}; j < kel::lbm::data{32u}; ++j){ - - auto& part = particles.at(i*32ul+j); - auto& body = part.template get<"rigid_body">(); - - auto& pos = body.template get<"position">(); - auto& old_pos = body.template get<"position_old">(); - auto& acceleration = body.template get<"acceleration">(); - auto& p_rad = part.template get<"collision">().template get<"radius">(); - // 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}; - - pos.at({{0u}}) = {i.template cast_to()+0.5f}; - pos.at({{1u}}) = {j.template cast_to()+64.0f}; - - old_pos = pos; - } - } - - // 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){ - // Do Verlet Step - for(kel::lbm::data i{0u}; i < particles.size(); ++i){ - auto& part_i = particles.at(i); - - verlet_step_lambda(part_i,time_step); - auto& body_i = part_i.template get<"rigid_body">(); - auto& acc_i = body_i.template get<"acceleration">(); - - acc_i.at({{0u}}) = 0.0; - acc_i.at({{1u}}) = -9.81; - } - - // - for(kel::lbm::data i{0u}; i < particles.size(); ++i){ - auto& part_i = particles.at(i); - auto& body_i = part_i.template get<"rigid_body">(); - auto& pos_i = body_i.template get<"position">(); - auto& pos_old_i = body_i.template get<"position_old">(); - /** - * Test against other particles - */ - for(kel::lbm::data j{i+1ul}; j < particles.size(); ++j){ - auto& part_j = particles.at(j); - auto& body_j = part_j.template get<"rigid_body">(); - auto& pos_j = body_j.template get<"position">(); - auto& pos_old_j = body_j.template get<"position_old">(); - - auto res = broadphase_collision_distance_squared(part_i, part_j); - if(res.first){ - //std::cout<<"Collision"<= 40 ){ - auto pos_i_0 = pos_i.at({{0u}}); - pos_i.at({{0u}}) = pos_old_i.at({{0u}}); - pos_old_i.at({{0u}}) = pos_i_0; - } - if(pos_i.at({{1u}}).get() <= 0 ){ - auto pos_i_1 = pos_i.at({{1u}}); - pos_i.at({{1u}}) = pos_old_i.at({{1u}}); - pos_old_i.at({{1u}}) = pos_i_1; - } - } - auto& pos = particles.at({0u}).template get<"rigid_body">().template get<"position">(); - - std::cout< 0u){ - std::cerr<<" - "< #include -#include -#include - -template -using SyclHostAlloc = acpp::sycl::usm_allocator, acpp::sycl::usm::alloc::host>; +#include +#include +#include +#include +#include namespace kel { namespace lbm { + +// The channel this case has always used. +constexpr uint64_t dim_x = 1024ul; +constexpr uint64_t dim_y = 512ul; + namespace sch { -/** - * Basic distribution function - * Base type - * D - * Q - * Scalar factor - * D factor - * Q factor - */ -using T = Float64; -using D2Q9 = Descriptor<2u,9u>; - -using DfCell = Cell; -using CellInfo = Cell; - -/** - * Basic type for simulation - */ -using CellStruct = Struct< - Member, - Member, - Member ->; +using InfoChunk = Chunk; -using MacroStruct = Struct< - Member, "velocity">, - Member +template +using DfChunk = Chunk, 1u, dim_x, dim_y>; + +template +using ScalarChunk = Chunk, 0u, dim_x, dim_y>; + +template +using VectorChunk = Chunk, 0u, dim_x, dim_y>; + +template +using ChunkStruct = Struct< + Member, + Member, "dfs">, + Member, "dfs_old"> >; -} +template +using VelChunk = Chunk, 0u, dim_x, dim_y>; -namespace cmpt { -template -struct PressureBoundaryRestrictedVelocityTo {}; -} +template +using RhoChunk = Chunk, 0u, dim_x, dim_y>; -template -struct component> { -private: - kel::lbm::data pressure_setting_; - kel::lbm::data rho_setting_; -public: - component(const kel::lbm::data& pressure_setting__): - pressure_setting_{pressure_setting__}, - rho_setting_{pressure_setting__ * df_info::inv_cs2} - {} - - template - void apply( - kel::lbm::data* field, - kel::lbm::data> index, - const kel::lbm::data>& meta, - uint64_t time_step - ) const { - - using dfi = df_info; - - auto flat_ind = flatten_index::apply(index,meta); - - bool is_even = ((time_step % 2u) == 0u); - auto& cell = field[flat_ind.get()]; - - auto& info = cell.template get<"info">(); - if(info({0u}).get() == 0u){ - return; - } - auto& dfs_old = (is_even) ? cell.template get<"dfs_old">() : cell.template get<"dfs">(); - - /** - * Sum all known DFs - */ - kel::lbm::data sum_df{0u}; - for(kel::lbm::data k{0u}; k < kel::lbm::data{Desc::Q}; ++k){ - auto c_k = dfi::directions[k.get()]; - auto flat_ind_n = flatten_index::apply({{index.at({0u})+c_k[0u], index.at({1u})+c_k[1u]}},meta); - auto& cell_n = field[flat_ind_n.get()]; - auto& info_n = cell_n.template get<"info">(); - auto info_n_val = info_n({0u}); - auto k_opp = dfi::opposite_index[k.get()]; - - if(info_n_val.get() > 0u){ - sum_df += dfs_old({k_opp}); - } - } - /** - * Get the sum of the unknown dfs and precalculate the direction - */ - constexpr int known_dir = East ? 1 : -1; - auto sum_unknown_dfs = (rho_setting_ - sum_df) * kel::lbm::data{known_dir}; - - for(kel::lbm::data k{0u}; k < kel::lbm::data{Desc::Q}; ++k){ - auto c_k = dfi::directions[k.get()]; - auto flat_ind_n = flatten_index::apply({{index.at({0u})+c_k[0u], index.at({1u})+c_k[1u]}},meta); - auto& cell_n = field[flat_ind_n.get()]; - auto& info_n = cell_n.template get<"info">(); - auto info_n_val = info_n({0u}); - - if(info_n_val.get() > 0u){ - sum_unknown_dfs += dfs_old({k}) * c_k[0u]; - } - } +template +using MacroStruct = Struct< + Member, "velocity">, + Member, "density"> +>; - auto vel_x = sum_unknown_dfs / rho_setting_; +//template +//using ParticleArray = Array< +// Particle +//>; +} - if constexpr (East) { - dfs_old({2u}) = dfs_old({1u}) + kel::lbm::data{2.0 / 3.0} * rho_setting_ * vel_x; - dfs_old({6u}) = dfs_old({5u}) + kel::lbm::data{1.0 / 6.0} * rho_setting_ * vel_x + kel::lbm::data{0.5} * (dfs_old({4u}) - dfs_old({3u})); - dfs_old({8u}) = dfs_old({7u}) + kel::lbm::data{1.0 / 6.0} * rho_setting_ * vel_x + kel::lbm::data{0.5} * (dfs_old({3u}) - dfs_old({4u})); - }else if constexpr (not East){ - dfs_old({1u}) = dfs_old({2u}) - kel::lbm::data{2.0 / 3.0} * rho_setting_ * vel_x; - dfs_old({5u}) = dfs_old({6u}) - kel::lbm::data{1.0 / 6.0} * rho_setting_ * vel_x + kel::lbm::data{0.5} * (dfs_old({3u}) - dfs_old({4u})); - dfs_old({7u}) = dfs_old({8u}) - kel::lbm::data{1.0 / 6.0} * rho_setting_ * vel_x + kel::lbm::data{0.5} * (dfs_old({4u}) - dfs_old({3u})); - } - } -}; - -/** - * This is massively hacky and expects a lot of conditions - * Either this or mirrored along the horizontal line works - * - * 0 - 2 - 2 - * 0 - 3 - 1 - * 0 - 3 - 1 - * ......... - * 0 - 3 - 1 - * 0 - 2 - 2 - * - */ - -template -kel::lbm::error_or set_geometry( - kel::lbm::data* cells, - const kel::lbm::data>& meta, - acpp::sycl::queue& sycl_q +template +kel::lbm::error_or setup_initial_conditions( + kel::lbm::data>& fields, + kel::lbm::data>& macros ){ - using namespace kel::lbm; + 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}} + ); + + // Inflow + iterator::apply( + [&](auto& index){ + info_f.at(index).set(3u); + }, + {{0u,0u}}, + {{1u,dim_y}}, + {{0u,1u}} + ); - kel::lbm::data rho{1.0}; - kel::lbm::data> vel{{0.0,0.0}}; - auto eq = equilibrium(rho, vel); - - sycl_q.submit([&](acpp::sycl::handler& h){ - h.parallel_for(acpp::sycl::range<2>{meta.at({0}).get(), meta.at({1}).get()},[=](acpp::sycl::id<2> idx){ - size_t i = idx[0]; - size_t j = idx[1]; - size_t acc_id = j * meta.at({0u}).get() + i; - - auto& c = cells[acc_id]; - auto& info = c.template get<"info">()({0}); - auto& dfs = c.template get<"dfs">(); - auto& dfs_old = c.template get<"dfs_old">(); - - if(i >= 2u and j >= 2u and (i+2u) < meta.at({0u}).get() and (j+2u) < meta.at({1u}).get()){ - // Fluid - info.set({2u}); - }else if(((j+2u) == meta.at({1u}).get() or j == 1u) and (i>=1u and (i+1u)= 1 and (j+1 < meta.at({1u}).get()) ) ){ - // Left input - info.set({3u}); - }else if((i+2u) == meta.at({0u}).get() and (j >= 1 and (j+1) < meta.at({1u}).get() )){ - // Right output - info.set({4u}); - }else { - info.set({0u}); + // Outflow + iterator::apply( + [&](auto& index){ + info_f.at(index).set(4u); + }, + {{dim_x-1u,0u}}, + {{dim_x, dim_y}}, + {{0u,1u}} + ); + // + auto& df_f = fields.template get<"dfs_old">(); + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + + iterator::apply( + [&](auto& index){ + auto& df = df_f.at(index); + auto& rho = rho_f.at(index); + rho.at({}) = {1}; + auto& vel = vel_f.at(index); + auto eq = equilibrium(rho,vel); + + 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& vel = vel_f.at(index); + if(info_f.at(index).get() == 2u){ + vel.at({{0u}}) = 0.0; } - for(kel::lbm::data k{0u}; k < kel::lbm::data{Desc::Q}; ++k){ - dfs(k) = eq.at(k); - dfs_old(k) = eq.at(k); + auto eq = equilibrium(rho,vel); + + df = eq; + }, + {},// 0-index + df_f.get_dims(), + {{1u,1u}} + ); + + iterator::apply( + [&](auto& index){ + kel::lbm::data> middle, ind_vec; + middle.at({{0u}}) = dim_x * 0.25; + middle.at({{1u}}) = dim_y * 0.5; + + ind_vec.at({{0u}}) = index.at({{0u}}).template cast_to(); + ind_vec.at({{1u}}) = index.at({{1u}}).template cast_to(); + + auto dist = middle - ind_vec; + auto dist_2 = kel::lbm::math::dot(dist,dist); + if(dist_2.at({}).get() < dim_y*dim_y*0.01){ + info_f.at(index) = 1u; } - }); - }).wait(); + }, + {},// 0-index + df_f.get_dims() + ); return kel::lbm::make_void(); } -template -void step( - kel::lbm::data* cells, - kel::lbm::data* macro_cells, - const kel::lbm::data>& meta, - uint64_t time_step, - acpp::sycl::queue& sycl_q +template +kel::lbm::error_or step( + kel::lbm::data>,encode::Sycl>& fields, + kel::lbm::data>,encode::Sycl>& macros, + kel::lbm::data t_i, + device& dev ){ - using namespace kel::lbm; - using dfi = df_info; - - constexpr kel::lbm::data frequency{1.0 / 0.6}; - - bool is_even = ((time_step % 2u) == 0u); - /** - * 1. Relaxation parameter \tau - */ - /* - component coll{0.5384}; - component bb; - */ - component> inlet{1.01 * dfi::cs2 * 2.0/3.0}; - component> outlet{1.0 * dfi::cs2 * 2.0/3.0}; - - - // auto collision_ev = - sycl_q.submit([&](acpp::sycl::handler& h){ - - /// Collision - h.parallel_for(acpp::sycl::range<2>{meta.at({0}).get(), meta.at({1}).get()},[=](acpp::sycl::id idx){ - size_t i = idx[0]; - size_t j = idx[1]; - size_t acc_id = j * meta.at({0u}).get() + i; - - auto& c = cells[acc_id]; - auto& info = cells[acc_id].template get<"info">(); - - switch (info({0u}).get()) { - // Bounce Back - case 1u: { - auto& dfs_old = is_even ? c.template get<"dfs_old">() : c.template get<"dfs">(); - auto df_cpy = dfs_old.copy(); - - for(uint64_t k = 1u; k < Desc::Q; ++k){ - dfs_old({k}) = df_cpy({dfi::opposite_index.at(k)}); + 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{0.8}; + component> bb; + component,encode::Sycl> abb; + + kel::lbm::data> rho_b; + rho_b.at({}) = 1.0; + kel::lbm::data> vel_b; + vel_b.at({{0u}}) = 0.015; + + component> equi{rho_b,vel_b}; + + component,encode::Sycl> flow_in{ + [&](){ + uint64_t target_t_i = 32u; + if(t_i.get() < target_t_i){ + return 1.0 + (0.0002 / target_t_i) * t_i.get(); } + return 1.0002; + }() + }; + component,encode::Sycl> flow_out{1.0}; - break; - } - // Collision - case 2u: { - // coll.apply(latt_acc, {i, j}, time_step); - auto& dfs_old = is_even ? c.template get<"dfs_old">() : c.template get<"dfs">(); - - auto& macro_c = macro_cells[acc_id]; - kel::lbm::data& rho = macro_c.template get<"pressure">(); - kel::lbm::data>& vel = macro_c.template get<"velocity">(); - - compute_rho_u(dfs_old,rho,vel); - auto eq = equilibrium(rho,vel); - - for(uint64_t k = 0u; k < Desc::Q; ++k){ - dfs_old({k}) = dfs_old({k}) + frequency * (eq.at({k}) - dfs_old({k})); - } - break; + h.parallel_for(acpp::sycl::range{dim_x,dim_y}, [=](acpp::sycl::id idx){ + kel::lbm::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); } - case 3u: { - inlet.apply(cells, {{i,j}}, meta, time_step); - auto& dfs_old = is_even ? c.template get<"dfs_old">() : c.template get<"dfs">(); - auto& macro_c = macro_cells[acc_id]; - - kel::lbm::data& rho = macro_c.template get<"pressure">(); - kel::lbm::data>& vel = macro_c.template get<"velocity">(); - - compute_rho_u(dfs_old,rho,vel); - auto eq = equilibrium(rho,vel); - - for(uint64_t k = 0u; k < Desc::Q; ++k){ - dfs_old({k}) = dfs_old({k}) + frequency * (eq.at({k}) - dfs_old({k})); - } + auto info = info_f.at(index); + + switch(info.get()){ + case 0u: break; - } - case 4u: { - outlet.apply(cells, {{i,j}}, meta, time_step); - auto& dfs_old = is_even ? c.template get<"dfs_old">() : c.template get<"dfs">(); - - auto& macro_c = macro_cells[acc_id]; - - kel::lbm::data& rho = macro_c.template get<"pressure">(); - kel::lbm::data>& vel = macro_c.template get<"velocity">(); - - compute_rho_u(dfs_old,rho,vel); - auto eq = equilibrium(rho,vel); - - for(uint64_t k = 0u; k < Desc::Q; ++k){ - dfs_old({k}) = dfs_old({k}) + frequency * (eq.at({k}) - dfs_old({k})); - } + case 1u: + bb.apply(fields,index,t_i); break; - } - default: - // Do nothing + case 2u: + collision.apply(fields,macros,index,t_i); + break; + case 3u: + flow_in.apply(fields,index,t_i); + //equi.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(); - - //auto stream_ev = - sycl_q.submit([&](acpp::sycl::handler& h){ - /// Stream - h.parallel_for(acpp::sycl::range<2>{meta.at({0}).get(), meta.at({1}).get()},[=](acpp::sycl::id idx){ - size_t i = idx[0]; - size_t j = idx[1]; - size_t acc_id = j * meta.at({0u}).get() + i; - - auto& c = cells[acc_id]; - auto& info = c.template get<"info">(); - auto& dfs_new = is_even ? c.template get<"dfs">() : c.template get<"dfs_old">(); - - if (info({0u}).get() > 0u) { - for (uint64_t k = 0u; k < Desc::Q; ++k) { - auto dir = dfi::directions[dfi::opposite_index[k]]; - size_t acc_old_id = (j+dir[1]) * meta.at({0u}).get() + (i+dir[0]); - - auto& dfs_old = is_even ? cells[acc_old_id].template get<"dfs_old">() : cells[acc_old_id].template get<"dfs">(); - auto& info_old = cells[acc_old_id].template get<"info">(); - - dfs_new({k}) = dfs_old({k}); - } - } - }); + + // Step + /* + q.submit([&](acpp::sycl::handler& h){ + // h.depends_on(collision_ev); }).wait(); + */ + + return kel::lbm::make_void(); } } } template -kel::lbm::error_or kel_main(int argc, char** argv){ - using namespace kel; +kel::lbm::error_or lbm_main(int argc, char** argv){ + using namespace kel::lbm; - using dfi = lbm::df_info; + /* + auto eo_lbm_args = setup_lbm_env,sch::Tuple<>>>(argc,argv); + if(eo_lbm_args.is_error()){ + return std::move(eo_lbm_args.get_error()); + } + auto& lbm_args = eo_lbm_args.get_value(); + */ - auto eo_lbm_dir = lbm::output_directory(); + using dfi = df_info; + + auto eo_lbm_dir = output_directory(); if(eo_lbm_dir.is_error()){ return std::move(eo_lbm_dir.get_error()); } auto& lbm_dir = eo_lbm_dir.get_value(); - auto out_dir = lbm_dir / "poiseulle_channel_2d_gpu"; - std::filesystem::create_directories(out_dir); - // - lbm::converter conv { + + auto out_dir = lbm_dir / "poiseulle_2d_gpu"; + + { + 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},{1e-4},{0.4 * dim_y}); + + // kel::lbm::data> meta{{dim_x,dim_y}}; + auto lbm_data_ptr = kel::lbm::heap>>(); + auto lbm_macro_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}; - kel::lbm::data> meta{{x_d,y_d}}; + 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(); - acpp::sycl::queue sycl_q; - SyclHostAlloc sycl_host_alloc{sycl_q}; - // SyclDeviceAlloc sycl_dev_alloc{sycl_q}; + device dev; - std::vector, SyclHostAlloc> host_cells{x_d * y_d,sycl_host_alloc}; + auto& sycl_q = dev.get_handle(); - kel::lbm::data* cells = acpp::sycl::malloc_device>(x_d * y_d,sycl_q); - kel::lbm::data* macro_cells = acpp::sycl::malloc_device>(x_d * y_d,sycl_q); + sycl_q.wait(); { - auto eov = lbm::set_geometry(cells,meta,sycl_q); + auto eov = setup_initial_conditions(*lbm_data_ptr,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_vtk_file(out_dir,"initial_state",0u,*lbm_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}; + sycl_q.wait(); - uint64_t time_max = 1024u*128u; - for(uint64_t i = 0u; i < time_max; ++i){ - lbm::step(cells,macro_cells,meta,i,sycl_q); + { + 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; + } + } + sycl_q.wait(); + auto lsd_view = make_view(lbm_sycl_data); + auto lsdm_view = make_view(lbm_sycl_macro_data); + kel::lbm::data time_steps{16u*4096ul}; + + auto& info_f = lsd_view.template get<"info">(); + + for(kel::lbm::data i{0u}; i < time_steps and krun; ++i){ + // BC + Collision + { + auto eov = step(lsd_view,lsdm_view,i,dev); + if(eov.is_error()){ + return eov; + } + } sycl_q.wait(); - if(i%4u == 0u){ - std::string vtk_f_name{"tmp/poiseulle_2d_gpu_"}; - vtk_f_name += std::to_string(i) + ".vtk"; - // write_vtk_file(vtk_f_name,host_cells); - sycl_q.memcpy(&host_cells[0u], macro_cells, x_d * y_d * sizeof(kel::lbm::data) ).wait(); - lbm::write_vtk_file(vtk_f_name, &host_cells[0], meta); + /* + if(i.get() % 128u == 0u){ + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_vtk_file(out_dir,"m",i.get(), *lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } } + */ + /* + if(i.get() % 32u == 0u){ + { + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_csv_file(out_dir,"m",i.get(), *lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + } + */ + // Stream + sycl_q.submit([&](acpp::sycl::handler& h){ + component> stream; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y}, [=](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(lsd_view,index,i); + } + }); + }).wait(); + wait.poll(); + if(print_status){ + std::cout<<"Status: "<().get() * 100 / time_steps.get())<<"%"<(argc, argv); + auto eov = lbm_main(argc, argv); if(eov.is_error()){ auto& err = eov.get_error(); std::cerr<<"[Error] "< #include +#include -#include - -namespace saw { -/* -template -class data, encode::Sycl> final { -public: - using Schema = kel::lbm::sch::CellField; - using MetaSchema = typename meta_schema::MetaSchema; -private: - data, encode::Sycl> inner_; -public: - data() = delete; - data(const data& inner_meta__, acpp::sycl::queue& q): - inner_{inner_meta__,q} - {} - - const data meta() const { - return inner_.dims(); - } - - template - data get_dim_size() const { - static_assert(i < Desc::D, "Not enough dimensions"); - return inner_.template get_dim_size(); - } - - const data& operator()(const data, Encode>& index)const{ - return inner_.at(index); - } - - data& operator()(const data, Encode>& index){ - return inner_.at(index); - } - - const data& at(const data, Encode>& index)const{ - return inner_.at(index); - } - - data& at(const data, Encode>& index){ - return inner_.at(index); - } - - ref, encode::Sycl >> inner_data() { - return {inner_}; - } - - data internal_size() const { - return inner_.internal_size(); - } - - data* internal_data() { - return inner_.internal_data(); - } -}; -*/ -/* -template -class data>, encode::Sycl> final { -public: - using Schema = schema::Ref>; -private: - data>, encode::Sycl>> inner_ref_; -public: - data() = delete; - data(ref,encode::Sycl>> inner_ref__): - inner_ref_{inner_ref__().inner_data()} - {} - - const data meta() const { - return inner_ref_().dims(); - } - - const data& operator()(const data, Encode>& index)const{ - return inner_ref_().at(index); - } - - data& operator()(const data, Encode>& index){ - return inner_ref_().at(index); - } - - const data& at(const data, Encode>& index)const{ - return inner_ref_().at(index); - } - - data& at(const data, Encode>& index){ - return inner_ref_().at(index); - } - - data internal_size() const { - return inner_ref_().internal_size(); - } - - data* internal_data() { - return inner_ref_().internal_data(); - } -}; - - -template -class data>, encode::Sycl> final { -public: - using Schema = schema::Ref>; -private: - data>, encode::Sycl>> inner_ref_; -public: - data() = delete; - data(ref,encode::Sycl>> inner_ref__): - inner_ref_{inner_ref__().inner_data()} - {} - - const data meta() const { - return inner_ref_().dims(); - } - - const data& operator()(const data, Encode>& index)const{ - return inner_ref_().at(index); - } - - data& operator()(const data, Encode>& index){ - return inner_ref_().at(index); - } - - const data& at(const data, Encode>& index)const{ - return inner_ref_().at(index); - } - - data& at(const data, Encode>& index){ - return inner_ref_().at(index); - } - - data internal_size() const { - return inner_ref_().internal_size(); - } - - data* internal_data() { - return inner_ref_().internal_data(); - } -}; -*/ - -} +#include +#include +#include +#include namespace kel { namespace lbm { -namespace sch { -using T = Float32; -using Desc = Descriptor<3u,27u>; -using DfCell = Cell; +// The channel this case has always used. +constexpr uint64_t dim_x = 2048ul; +constexpr uint64_t dim_y = 128ul; +constexpr uint64_t dim_z = 128ul; +constexpr uint64_t particle_size = 4ul; -using CellInfo = Cell; +namespace sch { -using CellStruct = Struct< - Member, - Member, - //Member, "force">, - Member +using LbmArgs = Args< + Struct< + Member, + Member + >, + Tuple<> >; -} -using SyclKelAllocator = acpp::sycl::usm_allocator, acpp::sycl::usm::alloc::shared>; -uint64_t to_flat_index(const kel::lbm::data>& index, const kel::lbm::data>& meta){ - auto flat = index.at({2u}) + meta.at({2u})*(index.at({1u}) + index.at({0u})*meta.at({1u})); - return flat.get(); -} - -template -kel::lbm::error_or set_geometry(std::vector,SyclKelAllocator>& latt, const kel::lbm::data>& meta){ - using namespace kel::lbm; - - /** - * Set ghost - */ - iterator::apply([&](const kel::lbm::data>& index){ - auto findex = to_flat_index(index,meta); - auto& cell = latt.at(findex); - auto& info = cell.template get<"info">(); - - info({0u}).set(0u); - }, {{0u,0u}}, meta); - - kel::lbm::data> distance; - - /** - * Set wall - */ - iterator<1u>::apply([&](const kel::lbm::data>& index){ - distance.at(index.at({0u})) = {1u}; - },{{0u}},{{sch::Desc::D}}); - /* - */ - iterator::apply([&](const kel::lbm::data>& index){ - auto findex = to_flat_index(index,meta); - auto& cell = latt.at(findex); - auto& info = cell.template get<"info">(); - - info({0u}).set(2u); - }, {}, meta, distance); - - /** - * Set fluid - */ - iterator<1u>::apply([&](const kel::lbm::data>& index){ - distance.at(index.at({0u})) = {2u}; - },{{0u}},{{sch::Desc::D}}); - /* - */ - iterator::apply([&](const kel::lbm::data>& index){ - auto findex = to_flat_index(index,meta); - auto& cell = latt.at(findex); - auto& info = cell.template get<"info">(); +using InfoChunk = Chunk; - info({0u}).set(1u); +template +using DfChunk = Chunk, 1u, dim_x, dim_y, dim_z>; - }, {}, meta, distance); +template +using ScalarChunk = Chunk, 0u, dim_x, dim_y, dim_z>; - /** - * Set inflow - */ +template +using VectorChunk = Chunk, 0u, dim_x, dim_y, dim_z>; - kel::lbm::data> shifted_point; - iterator<1u>::apply([&](const kel::lbm::data>& index){ - distance.at(index.at({0u})) = (index.at({0u}).get() == 0u)?1u:2u; - shifted_point.at(index.at({0u})) = (index.at({0u}).get() == 0u)?3u:meta.at({index.at({0u})}); - },{{0u}},{{sch::Desc::D}}); - /* - */ - iterator::apply([&](const kel::lbm::data>& index){ - auto findex = to_flat_index(index,meta); - auto& cell = latt.at(findex); - auto& info = cell.template get<"info">(); - - info({0u}).set(3u); - - }, {}, shifted_point, distance); - - /** - * Set outflow - */ - iterator<1u>::apply([&](const kel::lbm::data>& index){ - distance.at(index.at({0u})) = (index.at({0u}).get() == 0u)?1u:2u; - shifted_point.at(index.at({0u})) = (index.at({0u}).get() == 0u)?(meta.at({0u})-3u):0u; - },{{0u}},{{sch::Desc::D}}); - /* - */ - iterator::apply([&](const kel::lbm::data>& index){ - auto findex = to_flat_index(index,meta); - auto& cell = latt.at(findex); - auto& info = cell.template get<"info">(); +template +using ChunkStruct = Struct< + Member, + Member, "dfs">, + Member, "dfs_old">, + Member, "particle_N">, + Member, "particle_D"> +>; - info({0u}).set(4u); +template +using MomChunk = Chunk, 0u, dim_x, dim_y, dim_z>; - }, shifted_point, meta, distance); +template +using RhoChunk = Chunk, 0u, dim_x, dim_y, dim_z>; - return kel::lbm::make_void(); +template +using MacroStruct = Struct< + Member, "velocity">, + Member, "density">, + Member, "porosity"> +>; } -template -kel::lbm::error_or set_initial_conditions(std::vector,SyclKelAllocator>& latt, const kel::lbm::data>& meta){ - - kel::lbm::data rho{1.0}; - kel::lbm::data> vel{}; - vel.at({0u}) = {0.1f}; - auto eq = equilibrium(rho, vel); - - /** - * Set distribution - */ - iterator::apply([&](const kel::lbm::data>& index){ - auto findex = to_flat_index(index,meta); - auto& cell = latt.at(findex); - auto& dfs = cell.template get<"dfs">(); - auto& dfs_old = cell.template get<"dfs_old">(); - - for(kel::lbm::data k = 0; k < kel::lbm::data{sch::Desc::Q}; ++k){ - dfs(k) = eq.at(k); - dfs_old(k) = eq.at(k); - } - }, {}, meta); +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 lbm_step( - std::vector,SyclKelAllocator>& latt, - const kel::lbm::data>& meta, - kel::lbm::data time_step, - acpp::sycl::queue& sycl_q +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 ){ - using dfi = df_info; - bool even_step = ((time_step.get() % 2u) == 0u); - - component coll{0.5384}; - component bb; - - /** - * Collision - */ - iterator::apply([&](const kel::lbm::data>& index){ - auto findex = to_flat_index(index,meta); - auto& cell = latt.at(findex); - auto& info = cell.template get<"info">(); - - switch(info({0u}).get()){ - case 1u: { - bb.apply(&latt[0u], meta, index, time_step); - break; + 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]); } - case 2u: { - coll.apply(&latt[0u], meta, index, time_step); + + auto info = info_f.at(index); + + switch(info.get()){ + case 0u: break; - } - case 3u: { - //inlet.apply(latt, index, time_step); + case 1u: + bb.apply(fields,index,t_i); break; - } - case 4u: { - //outlet.apply(latt, index, time_step); + case 2u: + collision.apply(fields,macros,index,t_i); break; - } - default: + 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(); - }, {}, meta); - - // Stream - iterator::apply([&](const kel::lbm::data>& index){ - auto findex = to_flat_index(index,meta); - auto& cell = latt.at(findex); - auto& df_new = even_step ? cell.template get<"dfs">() : cell.template get<"dfs_old">(); - auto& info_new = cell.template get<"info">(); - - if(info_new({0u}).get() > 0u){ - for(uint64_t k = 0u; k < sch::Desc::Q; ++k){ - auto dir = dfi::directions[dfi::opposite_index[k]]; - auto index_dir = index; - for(uint64_t i = 0; i < sch::Desc::D; ++i){ - index_dir.at({i}) = index_dir.at({i}) + dir[i]; - } + q.submit([&](acpp::sycl::handler& h){ + component> stream; - auto findex_dir = to_flat_index(index_dir,meta); - auto& cell_dir_old = latt.at(findex_dir); + 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(); - auto& df_old = even_step ? cell_dir_old.template get<"dfs_old">() : cell_dir_old.template get<"dfs">(); + // Step + /* + q.submit([&](acpp::sycl::handler& h){ + // h.depends_on(collision_ev); + }).wait(); + */ - df_new({k}) = df_old({k}); - } - } - }, {}, meta); return kel::lbm::make_void(); } +} +} -kel::lbm::error_or real_main(int argc, char** argv){ - using dfi = df_info; +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"); + 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 { - {0.1f}, - {0.1f} + converter conv { + // delta_x + {1.0}, + // delta_t + {1.0} }; - print_lbm_meta(conv, {1e-3f}); - acpp::sycl::queue sycl_q; + print_lbm_meta(conv,{0.1},{0.01},{1024.0}); - /** - * Set the meta and initialize the lattice - */ - kel::lbm::data> meta{{2048u,128u,128u}}; + 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()<,SyclKelAllocator> lattice{(meta.at({0u})*meta.at({1u})*meta.at({2u})).get(), sycl_shared_alloc}; + auto eo_aio = saw::setup_async_io(); + if(eo_aio.is_error()){ + return kel::lbm::make_error( + std::string{eo_aio.get_error().get_message()}); + } + auto& aio = eo_aio.get_value(); + saw::wait_scope wait{aio.event_loop}; - /** - * Set the geometry - */ + 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 = set_geometry(lattice, meta); + auto eov = setup_initial_conditions(conv,*lbm_data_ptr,*lbm_macro_data_ptr,*lbm_particle_data_ptr); if(eov.is_error()){ return eov; } } - /** - * Set initial conditions in the simulations - */ + 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 = set_initial_conditions(lattice, meta); + 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; + } + } - for(kel::lbm::data ts{0u}; ts < kel::lbm::data{1024u*32u}; ++ts){ - auto eov = lbm_step(lattice,meta,ts,sycl_q); + 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(); } -} // lbm -} // kel +using FloatT = kel::lbm::sch::Float32; -/** - * main, but I don't like the error handling - */ int main(int argc, char** argv){ - auto eov = kel::lbm::real_main(argc, argv); + auto eov = lbm_main(argc, argv); if(eov.is_error()){ auto& err = eov.get_error(); + std::cerr<<"[Error] "< 0u){ std::cerr<<" - "< /** * For deciding what parameters to use maybe? */ namespace args { +using namespace kel::lbm::sch; + using LbmArgsStruct = Struct< >; @@ -193,7 +194,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/stdkr_2d_bgk/step.hpp b/examples/stdkr_2d_bgk/step.hpp index d7d4fd4..a127bf6 100644 --- a/examples/stdkr_2d_bgk/step.hpp +++ b/examples/stdkr_2d_bgk/step.hpp @@ -47,7 +47,7 @@ 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(); + 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; } diff --git a/examples/throat_clog_2d/init.hpp b/examples/throat_clog_2d/init.hpp index fab51b3..7683942 100644 --- a/examples/throat_clog_2d/init.hpp +++ b/examples/throat_clog_2d/init.hpp @@ -157,47 +157,23 @@ kel::lbm::error_or init( ); { + // A radius is a lattice length now. 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); - particles = create_spheroid_particle_group(radius_p, dense_p, {{64u}}); + particles = create_spheroid_particle_group(radius_p, dense_p, {64u}); auto& parts = particles.template get<"particles">(); - /** - * Seed the particles upstream of the throat on a staggered grid. - * - * Spacing is 2.4 radii, so they start clear of one another -- any - * initial overlap would be resolved as a contact on the first step and - * fire them apart. - */ - 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(); - - constexpr uint64_t per_column = 4ul; - - for(uint64_t i = 0u; i < particle_amount; ++i){ - auto col = i / per_column; - auto row = i % per_column; - - // Offset alternate columns by half a pitch so they interleave. - auto y_off = (static_cast(row) - (per_column - 1u) * 0.5) * pitch - + ((col % 2u == 1u) ? pitch * 0.5 : 0.0); - - auto& p = parts.at({i}); + { + auto& p = parts.at({0u}); auto& prb = p.template get<"rigid_body">(); auto& p_pos = prb.template get<"position">(); auto& p_pos_old = prb.template get<"position_old">(); + p_pos.at({{0u}}) = conv.meter_si_to_lbm({0.1}).handle(); + p_pos.at({{1u}}) = conv.meter_si_to_lbm({0.32}).handle(); - p_pos.at({{0u}}).set(x0 + static_cast(col) * pitch); - p_pos.at({{1u}}).set(y_mid + y_off); - - // Start at rest: position_old defaults to zero, which would read - // as an enormous velocity. p_pos_old = p_pos; } } diff --git a/examples/throat_clog_2d/sim.cpp b/examples/throat_clog_2d/sim.cpp index a25c1a3..04d0092 100644 --- a/examples/throat_clog_2d/sim.cpp +++ b/examples/throat_clog_2d/sim.cpp @@ -2,17 +2,31 @@ #include "init.hpp" #include "step.hpp" -#include +#include /** - * The coupling method is the only thing this example takes from the command - * line, so it is read straight from argv rather than through a codec -- the - * args codec is forstio's and would need a schema round trip for one string. + * For deciding what parameters to use maybe? */ +namespace args { +using namespace kel::lbm::sch; + +using LbmArgsStruct = Struct< + Member + //,Member +>; + +using LbmArgs = Args< + LbmArgsStruct, + Tuple<> +>; +} + template -kel::lbm::error_or lbm_main(std::string_view coupling){ +kel::lbm::error_or lbm_main(const kel::lbm::data& args){ using namespace kel::lbm; + auto& an = args.template get<"args">(); + using dfi = df_info; auto eo_lbm_dir = output_directory(); @@ -21,7 +35,7 @@ kel::lbm::error_or lbm_main(std::string_view coupling){ } auto& lbm_dir = eo_lbm_dir.get_value(); - auto out_dir = lbm_dir / "throat_clog_2d" / coupling; + auto out_dir = lbm_dir / "throat_clog_2d" / an.template get<"coupling">().stl_view(); { std::error_code ec; @@ -49,6 +63,7 @@ kel::lbm::error_or lbm_main(std::string_view coupling){ auto eo_aio = saw::setup_async_io(); if(eo_aio.is_error()){ + // forstio io reports its own error type; translate at the boundary. return kel::lbm::make_error( std::string{eo_aio.get_error().get_message()}); } @@ -116,7 +131,7 @@ kel::lbm::error_or lbm_main(std::string_view coupling){ auto lsdm_view = make_view(lbm_sycl_macro_data); auto lsdp_view = make_view(lbm_sycl_particle_data); - kel::lbm::data time_steps{256u*512ul}; + kel::lbm::data time_steps{1024ul}; auto& info_f = lsd_view.template get<"info">(); @@ -130,7 +145,7 @@ kel::lbm::error_or lbm_main(std::string_view coupling){ } sycl_q.wait(); - if(i.get() % 256u == 0u){ + if(i.get() % 1u == 0u){ { auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); if(eov.is_error()){ @@ -138,7 +153,7 @@ kel::lbm::error_or lbm_main(std::string_view coupling){ } } { - auto eov = write_vtk_file(out_dir,std::string{"throat_clog_2d_"}+std::string{coupling},i.get(), *lbm_macro_data_ptr); + auto eov = write_vtk_file(out_dir,std::string{"throat_clog_2d_"}+an.template get<"coupling">().stl_string(),i.get(), *lbm_macro_data_ptr); if(eov.is_error()){ return eov; } @@ -178,7 +193,7 @@ kel::lbm::error_or lbm_main(std::string_view coupling){ } } { - auto eov = write_vtk_file(out_dir,std::string{"throat_clog_2d_"}+std::string{coupling},time_steps.get(), *lbm_macro_data_ptr); + auto eov = write_vtk_file(out_dir,std::string{"throat_clog_2d_"}+an.template get<"coupling">().stl_string(),time_steps.get(), *lbm_macro_data_ptr); if(eov.is_error()){ return eov; } @@ -198,36 +213,37 @@ using DescT = kel::lbm::sch::D2Q9; kel::lbm::error_or k_main(int argc, char** argv){ using namespace kel::lbm; - - // Default to fplbm so the example runs with no arguments at all. - std::string_view coupling{"fplbm"}; - if(argc > 1){ - coupling = argv[1]; + + kel::lbm::data args; + { + auto eov = kel::lbm::parse_args(args,argc,argv); + if(eov.is_error()){ + return eov; + } } + auto& an = args.template get<"args">(); + auto& coupling = an.template get<"coupling">(); auto resolution_resolve = [&]() -> kel::lbm::error_or{ - return lbm_main(coupling); + return lbm_main(args); }; - if(coupling == "hlbm"){ - return resolution_resolve.template operator()(); - } - if(coupling == "fplbm"){ - return resolution_resolve.template operator()(); - } - if(coupling == "psm"){ - return resolution_resolve.template operator()(); - } + auto coupling_resolve = [&]() -> kel::lbm::error_or { + if(coupling.stl_view() == "hlbm"){ + return resolution_resolve.template operator()(); + } else if(coupling.stl_view() == "fplbm"){ + return resolution_resolve.template operator()(); + } else if(coupling.stl_view() == "psm"){ + return resolution_resolve.template operator()(); + } + }; - /** - * Previously the dispatch lambda had no return on the unmatched path, so - * an unknown coupling fell off the end of a non-void function. - */ - return kel::lbm::make_error( - std::string{"Unknown coupling '"} + std::string{coupling} - + "', expected one of hlbm, fplbm, psm"); + return coupling_resolve(); + + return kel::lbm::make_error("Invalid coupling"); } + int main(int argc, char** argv){ auto eov = k_main(argc, argv); if(eov.is_error()){ diff --git a/examples/throat_clog_2d/step.hpp b/examples/throat_clog_2d/step.hpp index 1605cf0..71df661 100644 --- a/examples/throat_clog_2d/step.hpp +++ b/examples/throat_clog_2d/step.hpp @@ -41,7 +41,7 @@ kel::lbm::error_or step( index.at({{i}}).set(idx[i]); } - hlbm_one_part.apply(fields,macros,particles,index,t_i,{0u}); + hlbm_one_part.apply(fields,macros,particles,index,t_i,{1u}); }); }).wait(); @@ -75,7 +75,7 @@ kel::lbm::error_or step( kel::lbm::data scale; { scale.set([&](){ - if(t_i.get() > 1024u){ + if(t_i.get() >= 0u){// > 1024u return 1.0f; } @@ -84,7 +84,7 @@ kel::lbm::error_or step( } { 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({{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; @@ -116,24 +116,9 @@ kel::lbm::error_or step( }); }).wait(); - /** - * Particle step, split so contact lands between the fluid gather and - * the integration. With 20 particles in a converging throat they jam - * against one another, so without a contact pass they would simply - * pass through each other and through the walls. - */ q.submit([&](acpp::sycl::handler& h){ - component> p_reset; - component> p_gather; - component> p_integrate; - - /** - * Stiffness is sized so an impact at the inflow speed stays well - * under a tenth of a radius of overlap; see dem_stiffness_for. - */ - dem_params p_dem{2.0e-2, 0.4, 0.0, 0.0}; - kel::lbm::data> p_extent{{dim_x,dim_y}}; - component> p_contact{p_dem,p_extent}; + kel::lbm::data>> grav; // body acceleration + component> fplbm_one_part{grav}; h.parallel_for(acpp::sycl::range<1u>{particle_amount}, [=](acpp::sycl::id<1u> idx){ kel::lbm::data> index; @@ -141,10 +126,7 @@ kel::lbm::error_or step( index.at({{i}}).set(idx[i]); } - p_reset.apply(particles,index,t_i); - p_gather.apply(fields,macros,particles,index,t_i,{4u}); - p_contact.apply(particles,index,t_i,{4u}); - p_integrate.apply(particles,index,t_i,{4u}); + fplbm_one_part.apply(fields,macros,particles,index,t_i,{1u}); }); }).wait(); @@ -220,7 +202,7 @@ kel::lbm::error_or step( index.at({{i}}).set(idx[i]); } - fplbm_one_part.apply(fields,macros,particles,index,t_i,{0u}); + fplbm_one_part.apply(fields,macros,particles,index,t_i,{2u}); }); }).wait(); -- cgit v1.3.1