From eb97ceef955fa1c5e3794c74fc9894fda1ce6f21 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 2 Jun 2026 18:15:13 +0200 Subject: Doing aabb computations --- examples/settling_spheres_2d_hlbm_gpu/sim.cpp | 469 ++++++++++++++++++++++++++ 1 file changed, 469 insertions(+) create mode 100644 examples/settling_spheres_2d_hlbm_gpu/sim.cpp (limited to 'examples/settling_spheres_2d_hlbm_gpu/sim.cpp') diff --git a/examples/settling_spheres_2d_hlbm_gpu/sim.cpp b/examples/settling_spheres_2d_hlbm_gpu/sim.cpp new file mode 100644 index 0000000..eb3f377 --- /dev/null +++ b/examples/settling_spheres_2d_hlbm_gpu/sim.cpp @@ -0,0 +1,469 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace kel { +namespace lbm { + +constexpr uint64_t dim_x = 512ul; +constexpr uint64_t dim_y = dim_x * 2ul; + +constexpr uint64_t particle_amount = 16ul; + +namespace sch { +using namespace saw::schema; + +using InfoChunk = Chunk; + +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">, + Member, "p_N">, + Member, "p_D"> +>; + +template +using VelChunk = Chunk, 0u, dim_x, dim_y>; + +template +using RhoChunk = Chunk, 0u, dim_x, dim_y>; + +template +using PorChunk = Chunk, 0u, dim_x, dim_y>; + +template +using MacroStruct = Struct< + Member, "velocity">, + Member, "density">, + Member, "porosity"> +>; + +template +using ParticleSpheroidGroup = ParticleGroup< + T, + Desc::D, + sch::ParticleCollisionSpheroid +>; + +template +using ParticleGroups = Tuple< + ParticleSpheroidGroup +>; + + +} + +template +saw::error_or setup_initial_conditions( + saw::data>& fields, + saw::data>& macros, + saw::data>& particles +){ + 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}} + ); + // + 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& 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() + ); + + // Particles + { + saw::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( + dense_p, + {64u} + ); + + { + auto& p = spheroid_group.template get<"particles">(); + + /// 16 if I remember correctly? + p = {{{particle_amount}}}; + + iterator<1u>::apply( + [&](auto& index){ + // Set Pos here? + auto& p_ind = p.at(index); + + auto& p_rb = p_ind.template get<"rigid_body">(); + auto& p_pos = p_rb.template get<"position">(); + + saw::data> p; + uint64_t x = index.at({0u}).get() % 8u; + uint64_t y = index.at({1u}).get() / 8u; + p.at({{0u}}).set( 0.2 + 0.6 / (x-1u) ); + p.at({{1u}}).set( 0.8 + y * 0.1 ); + + p_pos = p; + + auto& p_pos_old = p_rb.template get<"position_old">(); + p_pos_old = p_pos; + }, + {}, + p.meta() + ); + } + } + // Particle in hacky flavour + {} + + return saw::make_void(); +} + +template +saw::error_or step( + saw::data>,encode::Sycl>& fields, + saw::data>,encode::Sycl>& macros, + saw::data>,encode::Sycl>& p_group, + saw::data t_i, + device& dev +){ + auto& q = dev.get_handle(); + auto& info_f = fields.template get<"info">(); + + auto& parts = p_group.template get<"particles">(); + auto& p_mask = p_group.template get<"mask">(); + auto& vels = macros.template get<"velocity">(); + auto& forces = macros.template get<"force">(); + + auto p_meta = parts.meta(); + q.submit([&](acpp::sycl::handler& h){ + + h.parallel_for(acpp::sycl::range{dim_x,dim_y}, [=](acpp::sycl::id idx){ + saw::data> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + // Reset the force to zero + forces.at(index) = {}; + }); + }).wait(); + + q.submit([&](acpp::sycl::handler& h){ + h.parallel_for(acpp::sycl::range<1u>{p_meta.at({0u}).get()}, [=](acpp::sycl::id<1u> idx){ + + saw::data> index; + for(uint64_t i = 0u; i < 1u; ++i){ + index.at({{i}}).set(idx[i]); + } + + auto& pi = parts.at(index); + auto& pirb = pi.template get<"rigid_body">(); + + auto& p_pos = pirb.template get<"position">(); + auto& p_rot = pirb.template get<"rotation">(); + auto& p_acc = pirb.template get<"acceleration">(); + + + + saw::data> delta_t; + delta_t.at({}).set(1.0f); + verlet_step_lambda(p,delta_t); + }); + }).wait(); + + // 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; + + h.parallel_for(acpp::sycl::range{dim_x,dim_y}, [=](acpp::sycl::id idx){ + saw::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; + default: + break; + } + }); + }).wait(); + + // Step + /* + q.submit([&](acpp::sycl::handler& h){ + // h.depends_on(collision_ev); + }).wait(); + */ + + return saw::make_void(); +} +} +} + +template +saw::error_or lbm_main(int argc, char** argv){ + using namespace kel::lbm; + + 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& lbm_env = eo_lbm_env.get_value(); + + auto out_dir = lbm_env.data_dir / "settling_cubes_2d_ibm_gpu"; + + { + std::error_code ec; + std::filesystem::create_directories(out_dir,ec); + if(ec != std::errc{}){ + return saw::make_error("Could not create output directory"); + } + } + + converter conv { + // delta_x + {{1.0}}, + // delta_t + {{1.0}} + }; + + print_lbm_meta(conv,{0.05},{0.01},{0.4 * dim_y}); + + // saw::data> meta{{dim_x,dim_y}}; + auto lbm_data_ptr = saw::heap>>(); + auto lbm_macro_data_ptr = saw::heap>>(); + auto lbm_particle_data_ptr = saw::heap>>(); + // auto lbm_particles_ptr = saw::heap,part_count>>>(); + // saw::data> p_mask; + + std::cout<<"Estimated Bytes of LBM Fields: "<,sch::MacroStruct>().get()<(*lbm_data_ptr,*lbm_macro_data_ptr,*lbm_particle_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; + } + } + + saw::data, encode::Sycl> lbm_sycl_data{sycl_q}; + saw::data, encode::Sycl> lbm_sycl_macro_data{sycl_q}; + saw::data, encode::Sycl> lbm_sycl_particle_data{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; + } + } + { + auto eov = dev.copy_to_device(*lbm_particle_data_ptr,lbm_sycl_particle_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); + auto lsdp_view = make_view(lbm_sycl_particle_data); + + { + auto eov = write_vtk_file(out_dir,"ms",0u, *lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + saw::data time_steps{16u*4096ul}; + + auto& info_f = lsd_view.template get<"info">(); + + for(saw::data i{0u}; i < time_steps and krun; ++i){ + // BC + Collision + { + auto eov = step(lsd_view,lsdm_view,lsdp_view,i,dev); + if(eov.is_error()){ + return eov; + } + } + sycl_q.wait(); + if(i.get()%1u ==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; + } + } + } + /* + { + { + auto eov = dev.copy_to_host(lbm_sycl_data,*lbm_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_csv_file(out_dir,"lbm",i.get(), *lbm_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){ + saw::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<<" - "< Date: Wed, 3 Jun 2026 20:23:27 +0200 Subject: Dangling --- examples/settling_spheres_2d_hlbm_gpu/sim.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'examples/settling_spheres_2d_hlbm_gpu/sim.cpp') diff --git a/examples/settling_spheres_2d_hlbm_gpu/sim.cpp b/examples/settling_spheres_2d_hlbm_gpu/sim.cpp index eb3f377..a004a92 100644 --- a/examples/settling_spheres_2d_hlbm_gpu/sim.cpp +++ b/examples/settling_spheres_2d_hlbm_gpu/sim.cpp @@ -74,7 +74,7 @@ template saw::error_or setup_initial_conditions( saw::data>& fields, saw::data>& macros, - saw::data>& particles + saw::data>& p_group ){ auto& info_f = fields.template get<"info">(); // Set everything as walls @@ -207,8 +207,6 @@ saw::error_or step( auto& p_rot = pirb.template get<"rotation">(); auto& p_acc = pirb.template get<"acceleration">(); - - saw::data> delta_t; delta_t.at({}).set(1.0f); verlet_step_lambda(p,delta_t); @@ -218,7 +216,7 @@ saw::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{0.8}; + component> collision{0.8}; component> bb; h.parallel_for(acpp::sycl::range{dim_x,dim_y}, [=](acpp::sycl::id idx){ @@ -236,7 +234,7 @@ saw::error_or step( bb.apply(fields,index,t_i); break; case 2u: - // collision.apply(fields,macros,index,t_i); + collision.apply(fields,macros,index,t_i); break; default: break; @@ -268,7 +266,7 @@ saw::error_or lbm_main(int argc, char** argv){ } auto& lbm_env = eo_lbm_env.get_value(); - auto out_dir = lbm_env.data_dir / "settling_cubes_2d_ibm_gpu"; + auto out_dir = lbm_env.data_dir / "settling_spheres_2d_hlbm_gpu"; { std::error_code ec; -- cgit v1.2.3