From c410ddfebd40aedf75d0927a0e6581bf48b5b1c3 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 7 Oct 2025 17:52:16 +0200 Subject: Fixing center of mass calculations --- c++/particle/geometry/circle.hpp | 27 +++++++++++++++++++-------- c++/particle/particle.hpp | 1 + 2 files changed, 20 insertions(+), 8 deletions(-) (limited to 'c++') diff --git a/c++/particle/geometry/circle.hpp b/c++/particle/geometry/circle.hpp index ef67fdd..331f06d 100644 --- a/c++/particle/geometry/circle.hpp +++ b/c++/particle/geometry/circle.hpp @@ -18,19 +18,20 @@ public: auto& grid = mask.template get<"grid">(); auto& com = mask.template get<"center_of_mass">(); + com = {}; //uint64_t rad_i = static_cast(resolution * radius_.get())+1u; - uint64_t rad_i = resolution; - uint64_t size = rad_i + 2*boundary_nodes; + uint64_t diameter_i = resolution; + uint64_t size = diameter_i + 2*boundary_nodes; grid = {{{size,size}}}; - saw::data delta_x{static_cast::type>(2.0 / static_cast(rad_i))}; - saw::data center = (saw::data{1.0} + saw::data{2.0} * saw::data{static_cast::type>(boundary_nodes)/rad_i}); + saw::data delta_x{static_cast::type>(2.0 / static_cast(diameter_i))}; + saw::data center = (saw::data{1.0} + saw::data{2.0} * saw::data{static_cast::type>(boundary_nodes)/diameter_i}); for(uint64_t i = 0; i < size; ++i){ for(uint64_t j = 0; j < size; ++j){ grid.at({{i,j}}).set(0); - if(i >= boundary_nodes and j >= boundary_nodes and i < (rad_i + boundary_nodes) and j < (rad_i + boundary_nodes) ){ + if(i >= boundary_nodes and j >= boundary_nodes and i < (diameter_i + boundary_nodes) and j < (diameter_i + boundary_nodes) ){ saw::data fi = saw::data{static_cast::type>(0.5+i)} * delta_x - center; saw::data fj = saw::data{static_cast::type>(0.5+j)} * delta_x - center; @@ -42,10 +43,20 @@ public: } } - saw::data> com{{0,0}}; - iterate_over([&](saw::data>& index){ + saw::data total_mass{}; + iterate_over([&](const saw::data>& index){ + auto ind_vec = saw::math::vectorize_data(index).template cast_to(); + for(uint64_t i{0u}; i < 2u; ++i){ + ind_vec.at({{i}}) = ind_vec.at({{i}}) * grid.at(index); + } + com = com + ind_vec; + + total_mass = total_mass + grid.at(index); + },{{0u,0u}}, grid.dims()); - },{{0u,0u}}, grid.meta()); + for(uint64_t i{0u}; i < 2u; ++i){ + com.at({{i}}) = com.at({{i}}) / total_mass; + } return mask; } diff --git a/c++/particle/particle.hpp b/c++/particle/particle.hpp index dd96f50..39aadfb 100644 --- a/c++/particle/particle.hpp +++ b/c++/particle/particle.hpp @@ -2,6 +2,7 @@ #include #include +#include namespace kel { namespace lbm { -- cgit v1.2.3