From f8022a6131126bb43a96908933c8fa0f3fc2f009 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 5 May 2026 09:58:32 +0200 Subject: Forced Particle LBM --- lib/core/c++/fplbm.hpp | 177 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 lib/core/c++/fplbm.hpp (limited to 'lib/core/c++/fplbm.hpp') diff --git a/lib/core/c++/fplbm.hpp b/lib/core/c++/fplbm.hpp new file mode 100644 index 0000000..61def6b --- /dev/null +++ b/lib/core/c++/fplbm.hpp @@ -0,0 +1,177 @@ +#pragma once + +#include "common.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +struct FpLbm {}; +struct FpLbmOneParticleNoVelocity{}; +} + +template +class component final { +private: + saw::data relaxation_; + saw::data frequency_; +public: + component(typename saw::native_data_type::type relaxation__): + relaxation_{{relaxation__}}, + frequency_{saw::data{1} / relaxation_} + {} + + component(const saw::data& relaxation__): + relaxation_{relaxation__}, + frequency_{saw::data{1} / relaxation_} + {} + + using Component = cmpt::FpLbm; + + + template + void apply(const saw::data& field, const saw::data& macros, saw::data> index, saw::data time_step) const { + // void apply(saw::data& field, saw::data> index, saw::data time_step){ + bool is_even = ((time_step.get() % 2) == 0); + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + auto& dfs = dfs_old_f.at(index); + + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + + saw::data>& rho = rho_f.at(index); + saw::data>& vel = vel_f.at(index); + + compute_rho_u(dfs_old_f.at(index),rho,vel); + auto eq = equilibrium(rho,vel); + + using dfi = df_info; + + auto& force_f = macros.template get<"force">(); + auto& force = force_f.at(index); + + auto& porosity_f = macros.template get<"porosity">(); + auto& porosity = porosity_f.at(index); + + saw::data> dfi_inv_cs2; + dfi_inv_cs2.at({}).set(dfi::inv_cs2); + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + // saw::data ci_min_u{0}; + saw::data> ci; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + ci.at({{d}}).set(static_cast::type>(dfi::directions[i][d])); + } + auto ci_dot_u = saw::math::dot(ci,vel); + + // saw::data> F_i; + // F_i = f * (c_i - u * ics2 + * c_i * ics2 * ics2) * w_i; + saw::data> w; + w.at({}).set(dfi::weights[i]); + + auto F_i_d = saw::math::dot(force * w, (ci - vel * dfi_inv_cs2 + ci * ci_dot_u * dfi_inv_cs2 * dfi_inv_cs2 )); + /* + saw::data> F_i_sum; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + saw::data> F_i_d; + F_i_d.at({}) = F_i.at({{d}}); + F_i_sum = F_i_sum + F_i_d; + } + */ + + dfs.at({i}) = dfs.at({i}) + frequency_ * (eq.at(i) - dfs.at({i}) ) + F_i_d.at({}) * (saw::data{1} - saw::data{0.5f} * frequency_); + } + } +}; + +template +class component final { +private: + saw::data relaxation_; + saw::data frequency_; +public: + component(typename saw::native_data_type::type relaxation__): + relaxation_{{relaxation__}}, + frequency_{saw::data{1} / relaxation_} + {} + + component(const saw::data& relaxation__): + relaxation_{relaxation__}, + frequency_{saw::data{1} / relaxation_} + {} + + using Component = cmpt::FpLbmOneParticleNoVelocity; + + template + void apply(const saw::data& field, const saw::data& macros, saw::data> index, saw::data time_step) const { + // void apply(saw::data& field, saw::data> index, saw::data time_step){ + bool is_even = ((time_step.get() % 2) == 0); + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + auto& dfs = dfs_old_f.at(index); + + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + auto& por_f = macros.template get<"porosity">(); + + saw::data>& rho = rho_f.at(index); + + saw::data> half; + half.at({}).set(0.5); + saw::data>& vel = vel_f.at(index);// + total_force * ( half / rho ); + + compute_rho_u(dfs_old_f.at(index),rho,vel); + auto eq = equilibrium(rho,vel); + + using dfi = df_info; + + saw::data> min_two; + min_two.at({}).set(-2); + + auto force = vel * rho * min_two * por_f.at(index); + + saw::data> dfi_inv_cs2; + dfi_inv_cs2.at({}).set(dfi::inv_cs2); + + + // auto vel = vel_f.at(index); + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + // saw::data ci_min_u{0}; + saw::data> ci; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + ci.at({{d}}).set(static_cast::type>(dfi::directions[i][d])); + } + auto ci_dot_u = saw::math::dot(ci,vel); + + // saw::data> F_i; + // F_i = f * ((c_i - u) * ics2 + * c_i * ics2 * ics2) * w_i; + saw::data> w; + w.at({}).set(dfi::weights[i]); + + /* + saw::data> F_i_sum; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + saw::data> F_i_d; + F_i_d.at({}) = F_i.at({{d}}); + F_i_sum = F_i_sum + F_i_d; + } + */ + auto term1 = (ci-vel) * dfi_inv_cs2; + auto term2 = ci * (ci_dot_u * dfi_inv_cs2 * dfi_inv_cs2); + + auto force_projection = saw::math::dot(term1 + term2, force); + + auto F_i = w * force_projection; + + dfs.at({i}) = dfs.at({i}) + + frequency_ + * (eq.at(i) - dfs.at({i}) ) + + F_i.at({}) + * (saw::data{1} - saw::data{0.5f} * frequency_); + } + } + +}; +} +} -- cgit v1.2.3