From afc0998f10882af12c136cbffee85f40573f2b40 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Wed, 25 Mar 2026 13:40:32 +0100 Subject: Moving to finally implementing particles on gpu --- lib/core/c++/particle/particle.hpp | 90 ++++++++++++++++++++++++++++++-------- lib/core/tests/particles.cpp | 29 ++++-------- 2 files changed, 81 insertions(+), 38 deletions(-) (limited to 'lib') diff --git a/lib/core/c++/particle/particle.hpp b/lib/core/c++/particle/particle.hpp index f8104fd..a95a173 100644 --- a/lib/core/c++/particle/particle.hpp +++ b/lib/core/c++/particle/particle.hpp @@ -43,19 +43,59 @@ using ParticleCollisionSpheroid = Struct< Member, "radius"> >; -tem - -template> +template using Particle = Struct< - Member, "rigid_body">, - Member, + Member, "rigid_body"> // Problem is that dynamic data would two layered // Member, "mask">, - Member, - Member, "mass"> >; + +template> +using ParticleGroup = Struct< + Member, "mask">, + Member, + Member, "mass">, + Member>, "particles"> +>; + +template +using ParticleGroupTuple = Tuple< + PartGroups... +>; + +} + +template +saw::data>> create_spheroid_particle_group( + saw::data> rad_p, + saw::data> density_p +){ + saw::data,sch::ParticleCollisionSpheroid> part; + + auto& mask = part.template get<"mask">(); + auto& collision = part.template get<"collision">(); + auto& mass = part.template get<"mass">(); + + if constexpr ( D == 1u ){ + saw::data> c; + c.at({}).set(2.0); + mass = rad_p * c * density_p; + } else if constexpr ( D == 2u){ + saw::data> pi; + pi.at({}).set(3.14159); + mass = rad_p * rad_p * pi * density_p; + } else if constexpr ( D == 3u ){ + saw::data> c; + c.at({}).set(3.14159 * 4.0 / 3.0); + mass = rad_p * rad_p * rad_p * c * density_p; + } else { + static_assert(D == 0u or D > 3u, "Dimensions only supported for Dim 1,2 & 3."); + } + + return part; } +/* template saw::data>> create_spheroid_particle( saw::data> pos_p, @@ -106,6 +146,7 @@ saw::data>> create_spheroid return part; } +*/ template constexpr auto verlet_step_lambda = [](saw::data>& particle, saw::data> time_step_delta){ @@ -142,18 +183,20 @@ constexpr auto verlet_step_lambda = [](saw::data>& particle, }; template -constexpr auto handle_collision = [](saw::data>& left, saw::data>& right, saw::data> unit_pos_rel, saw::data> vel_rel, saw::data> d_t) -{ +constexpr auto handle_collision = []( + saw::data>& left, const saw::data>& mass_l, + saw::data>& right, const saw::data>& mass_r, + saw::data> unit_pos_rel, saw::data> vel_rel, + saw::data> d_t +){ auto& rb_l = left.template get<"rigid_body">(); auto& pos_l = rb_l.template get<"position">(); auto& pos_old_l = rb_l.template get<"position_old">(); - auto& mass_l = left.template get<"mass">(); auto vel_l = (pos_l-pos_old_l)/d_t; auto& rb_r = right.template get<"rigid_body">(); auto& pos_r = rb_r.template get<"position">(); auto& pos_old_r = rb_r.template get<"position_old">(); - auto& mass_r = left.template get<"mass">(); auto vel_r = (pos_r-pos_old_r)/d_t; auto vel_pos_rel_dot = saw::math::dot(unit_pos_rel,vel_rel); @@ -165,10 +208,16 @@ constexpr auto handle_collision = [](saw::data>& left, saw::d }; -template -constexpr auto broadphase_collision_distance_squared = [](saw::data>& left, saw::data>& right) -> std::pair>>{ - auto rad_l = left.template get<"collision">().template get<"radius">(); - auto rad_r = right.template get<"collision">().template get<"radius">(); +template +constexpr auto broadphase_collision_distance_squared = []( + saw::data>& left, + const saw::data& coll_l, + saw::data>& right, + const saw::data& coll_r +) -> std::pair>>{ + + auto rad_l = coll_l.template get<"radius">(); + auto rad_r = coll_r.template get<"radius">(); auto& rb_l = left.template get<"rigid_body">(); auto& rb_r = right.template get<"rigid_body">(); @@ -190,9 +239,14 @@ constexpr auto broadphase_collision_distance_squared = [](saw::data -constexpr auto broadphase_collision_check = [](saw::data>& left, saw::data>& right) -> bool{ - return broadphase_collision_distance_squared(left,right).first; +template +constexpr auto broadphase_collision_check = []( + saw::data>& left, + const saw::data& coll_l, + saw::data>& right, + const saw::data& coll_r +) -> bool{ + return broadphase_collision_distance_squared(left,coll_l,right,coll_r).first; }; } } diff --git a/lib/core/tests/particles.cpp b/lib/core/tests/particles.cpp index a9984ec..1c18fbb 100644 --- a/lib/core/tests/particles.cpp +++ b/lib/core/tests/particles.cpp @@ -29,7 +29,7 @@ SAW_TEST("Verlet step 2D - Planar"){ SAW_EXPECT(pos.at({{0}}).get() == 0.25, std::string{"Incorrect Pos X: "} + std::to_string(pos.at({{0}}).get())); SAW_EXPECT(pos.at({{1}}).get() == 0.0, std::string{"Incorrect Pos Y: "} + std::to_string(pos.at({{1}}).get())); } - +/* SAW_TEST("No Collision Spheroid 2D"){ using namespace kel; @@ -37,10 +37,6 @@ SAW_TEST("No Collision Spheroid 2D"){ { auto& body = part_a.template get<"rigid_body">(); auto& pos = body.template get<"position">(); - auto& coll = part_a.template get<"collision">(); - auto& radius = coll.template get<"radius">(); - - radius.at({}).set(1.0); pos.at({{0u}}) = 0.1; pos.at({{1u}}) = 0.2; @@ -50,11 +46,7 @@ SAW_TEST("No Collision Spheroid 2D"){ { auto& body = part_b.template get<"rigid_body">(); auto& pos = body.template get<"position">(); - auto& coll = part_b.template get<"collision">(); - auto& radius = coll.template get<"radius">(); - radius.at({}).set(1.0); - pos.at({{0u}}) = -2.1; pos.at({{1u}}) = 0.0; } @@ -62,7 +54,8 @@ SAW_TEST("No Collision Spheroid 2D"){ bool have_collided = lbm::broadphase_collision_check(part_a,part_b); SAW_EXPECT(not have_collided, "Particles shouldn't collide"); } - +*/ +/* SAW_TEST("Collision Spheroid 2D"){ using namespace kel; @@ -95,7 +88,8 @@ SAW_TEST("Collision Spheroid 2D"){ bool have_collided = lbm::broadphase_collision_check(part_a,part_b); SAW_EXPECT(have_collided, "Particles should collide"); } - +*/ +/* SAW_TEST("Moving particles 2D"){ using namespace kel; @@ -149,7 +143,7 @@ SAW_TEST("Moving particles 2D"){ SAW_EXPECT(has_collided, "Expecting collision within those steps"); } - +*/ SAW_TEST("Particle Matrix Rotation"){ using namespace kel; @@ -158,20 +152,14 @@ SAW_TEST("Particle Matrix Rotation"){ auto& body = part_a.template get<"rigid_body">(); auto& pos = body.template get<"position">(); auto& pos_old = body.template get<"position_old">(); - auto& coll = part_a.template get<"collision">(); - auto& radius = coll.template get<"radius">(); auto& acc = body.template get<"acceleration">(); - radius.at({}).set(1.0); - pos.at({{0u}}) = -5.0; pos.at({{1u}}) = 0.2; pos_old = pos; } - - - } +/* SAW_TEST("Particle Collision Impulse"){ using namespace kel; @@ -210,8 +198,9 @@ SAW_TEST("Particle Collision Impulse"){ acc.at({{0u}}) = -0.1; } } - +*/ /* + SAW_TEST("Minor Test for mask"){ using namespace kel; -- cgit v1.2.3 From 0a11f4fbdbdf62d153904c9ed7de6c82a8916b7d Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Thu, 26 Mar 2026 12:08:19 +0100 Subject: Dangling changes. Restructuring particle setup --- lib/core/c++/boundary.hpp | 8 ++++---- lib/core/c++/particle/particle.hpp | 29 ++++++++++++++++++----------- 2 files changed, 22 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/core/c++/boundary.hpp b/lib/core/c++/boundary.hpp index 4dbbdf8..01ae7b5 100644 --- a/lib/core/c++/boundary.hpp +++ b/lib/core/c++/boundary.hpp @@ -211,12 +211,12 @@ public: if constexpr (Dir) { dfs_old.at({2u}) = dfs_old.at({1u}) + saw::data{2.0 / 3.0} * rho_vel_x; - dfs_old.at({6u}) = dfs_old.at({5u}) + saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({4u}) - dfs_old.at({3u})); - dfs_old.at({8u}) = dfs_old.at({7u}) + saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({3u}) - dfs_old.at({4u})); + dfs_old.at({6u}) = dfs_old.at({5u}) + saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({3u}) - dfs_old.at({4u})); + dfs_old.at({8u}) = dfs_old.at({7u}) + saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({4u}) - dfs_old.at({3u})); }else if constexpr (not Dir){ dfs_old.at({1u}) = dfs_old.at({2u}) - saw::data{2.0 / 3.0} * rho_vel_x; - dfs_old.at({5u}) = dfs_old.at({6u}) - saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({3u}) - dfs_old.at({4u})); - dfs_old.at({7u}) = dfs_old.at({8u}) - saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({4u}) - dfs_old.at({3u})); + dfs_old.at({5u}) = dfs_old.at({6u}) - saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({4u}) - dfs_old.at({3u})); + dfs_old.at({7u}) = dfs_old.at({8u}) - saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({3u}) - dfs_old.at({4u})); } } }; diff --git a/lib/core/c++/particle/particle.hpp b/lib/core/c++/particle/particle.hpp index a95a173..c0d115f 100644 --- a/lib/core/c++/particle/particle.hpp +++ b/lib/core/c++/particle/particle.hpp @@ -1,8 +1,10 @@ #pragma once -#include -#include #include +#include +#include + +#include "../iterator.hpp" namespace kel { namespace lbm { @@ -52,25 +54,20 @@ using Particle = Struct< template> using ParticleGroup = Struct< - Member, "mask">, + Member, "mask">, Member, Member, "mass">, Member>, "particles"> >; - -template -using ParticleGroupTuple = Tuple< - PartGroups... ->; - } template saw::data>> create_spheroid_particle_group( saw::data> rad_p, - saw::data> density_p + saw::data> density_p, + const saw::data& mask_resolution ){ - saw::data,sch::ParticleCollisionSpheroid> part; + saw::data>> part; auto& mask = part.template get<"mask">(); auto& collision = part.template get<"collision">(); @@ -92,6 +89,16 @@ saw::data>> create_sph static_assert(D == 0u or D > 3u, "Dimensions only supported for Dim 1,2 & 3."); } + saw::data> mask_dims; + for(uint64_t i = 0u; i < D; ++i){ + mask_dims.at({i}) = mask_resolution; + } + + mask = {mask_dims}; + iterator::apply([&](const auto& index){ + + },{},mask_dims); + return part; } -- cgit v1.2.3