From 9e37ff62b668694f705a8d132469f40ead9f6f0f Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Thu, 5 Jun 2025 13:09:07 +0200 Subject: Dangling changes --- c++/lbm.hpp | 1 + c++/macroscopic.hpp | 6 ++-- c++/particle/particle.hpp | 33 +++++++++++++++--- examples/cavity_2d.cpp | 30 +++++++---------- examples/particle_ibm.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++ tests/particles.cpp | 44 +++++++++++++++++++++++- tests/vtk_write.cpp | 4 +++ 7 files changed, 178 insertions(+), 26 deletions(-) create mode 100644 examples/particle_ibm.cpp diff --git a/c++/lbm.hpp b/c++/lbm.hpp index 1baaa0e..e5799b5 100644 --- a/c++/lbm.hpp +++ b/c++/lbm.hpp @@ -3,6 +3,7 @@ #include "descriptor.hpp" #include "converter.hpp" #include "config.hpp" +#include "write_vtk.hpp" #include #include diff --git a/c++/macroscopic.hpp b/c++/macroscopic.hpp index e126bbe..8a25248 100644 --- a/c++/macroscopic.hpp +++ b/c++/macroscopic.hpp @@ -49,14 +49,14 @@ void compute_rho_u ( } for(size_t j = 0; j < Desc::Q; ++j){ - rho = rho + dfs(j); + rho() = rho() + dfs(j); for(size_t i = 0; i < Desc::D; ++i){ - vel[i] = vel[i] + saw::data{dfi::directions[j][i]} * dfs(j); + vel().at({i}) = vel().at({i}) + saw::data{static_cast::type>(dfi::directions[j][i])} * dfs(j); } } for(size_t i = 0; i < Desc::D; ++i){ - vel[i] = vel[i] / rho; + vel().at({i}) = vel().at({i}) / rho(); } } } diff --git a/c++/particle/particle.hpp b/c++/particle/particle.hpp index 1c7b241..35196ce 100644 --- a/c++/particle/particle.hpp +++ b/c++/particle/particle.hpp @@ -30,10 +30,10 @@ using Particle = Struct< >; } -template +template > class particle_system { private: - saw::data> particles_; + saw::data>> particles_; void verlet_step(saw::data>& particle, saw::data time_step_delta){ auto& body = particle.template get<"rigid_body">(); @@ -56,10 +56,26 @@ private: pos = pos_new; } public: + /** + * Add particle to this class and return an id representing this particle + */ + saw::error_or> add_particle(saw::data> particle__){ + auto size = particles_.size(); + auto eov = particles_.add(std::move(particle__)); + if(eov.is_error()){ + return std::move(eov.get_error()); + } - void step(T time_step_delta){ - for(auto& iter : particles_){ - verlet_step(time_step_delta); + return size; + } + + saw::data>& get_particle(saw::data id){ + + } + + void step(saw::data time_step_delta){ + for(saw::data i{0u}; i < particles_.size(); ++i){ + verlet_step(particles_.at(i), time_step_delta); } } @@ -68,6 +84,13 @@ public: for(auto& iter : particles_){ } } + + /** + * Mostly meant for unforeseen use cases. + */ + saw::data>& at(saw::data i){ + return particles_.at(i); + } }; } } diff --git a/examples/cavity_2d.cpp b/examples/cavity_2d.cpp index d40e06e..a10276a 100644 --- a/examples/cavity_2d.cpp +++ b/examples/cavity_2d.cpp @@ -44,6 +44,11 @@ using CellStruct = Struct< Member, "info"> >; +template +using MacroStruct = Struct< + Member, "velocity">, + Member +>; using CavityFieldD2Q9 = CellField>; } @@ -390,6 +395,8 @@ int main(){ uint64_t print_every = 256u; uint64_t file_no = 0u; + saw::data,sch::D2Q9::D>> macros{dim}; + for(uint64_t step = 0; step < lattice_steps; ++step){ if(step % print_every == 0u){ @@ -481,28 +488,17 @@ int main(){ /// std::cout<<"Average rho: "<<(sum / ((dim_x-4)*(dim_y-4)))<() : cell.template get<"dfs">(); - typename saw::native_data_type::type rho; - std::array::type, sch::D2Q9::D> vel; + auto& rho = macros.at({{i,j}}).template get<"pressure">(); + auto& vel = macros.at({{i,j}}).template get<"velocity">(); compute_rho_u(dfs,rho,vel); - - vtk_file << static_cast(vel[0u]) << " " << static_cast(vel[1u])<<" 0.0\n"; }, lattice); - + + std::string vtk_f_name{"tmp/cavity_2d_"}; + vtk_f_name += std::to_string(file_no) + ".vtk"; + write_vtk_file(vtk_f_name, macros); ++file_no; } diff --git a/examples/particle_ibm.cpp b/examples/particle_ibm.cpp new file mode 100644 index 0000000..a2b510e --- /dev/null +++ b/examples/particle_ibm.cpp @@ -0,0 +1,86 @@ +#include "../c++/descriptor.hpp" + +#include + +namespace kel { +namespace lbm { +namespace sch { +using namespace saw::schema; + +/** + * Basic distribution function + * Base type + * D + * Q + * Scalar factor + * D factor + * Q factor + */ +using T = Float32; +using D2Q5 = Descriptor<2,5>; +using D2Q9 = Descriptor<2,9>; + +template +using DfCell = Cell; + +template +using CellInfo = Cell; + +/** + * Basic type for simulation + */ +template +using CellStruct = Struct< + Member, "dfs">, + Member, "dfs_old">, + Member, "info">, + Member, + Member +>; + + +using CavityFieldD2Q9 = CellField>; +} +} +} + +int main(int argc, char** argv){ + using namespace kel::lbm; + + std::string_view cfg_file_name = "config.json"; + if(argc > 1){ + cfg_file_name = argv[1]; + } + + auto eo_conf = load_lbm_config>(cfg_file_name); + if(eo_conf.is_error()){ + auto& err = eo_conf.get_error(); + std::cerr<<"[Error]: "< conv{ + {conf.template get<"delta_x">()}, + {conf.template get<"delta_t">()} + }; + + print_lbm_meta>(conv, {conf.template get<"kinematic_viscosity">()}); + + + saw::data> dim{{128, 128}}; + + saw::data lattice{dim}; + + + + + return 0; +} diff --git a/tests/particles.cpp b/tests/particles.cpp index 873b8ad..260caf0 100644 --- a/tests/particles.cpp +++ b/tests/particles.cpp @@ -36,7 +36,49 @@ SAW_TEST("Verlet integration test"){ using namespace kel; lbm::particle_system> system; - // system.step(); + { + saw::data> particle; + auto& rb = particle.template get<"rigid_body">(); + auto& acc = rb.template get<"acceleration">(); + auto& pos = rb.template get<"position">(); + auto& pos_old = rb.template get<"position_old">(); + pos = {{1e-1,1e-1}}; + pos_old = {{0.0, 0.0}}; + acc = {{0.0,-1e1}}; + + auto eov = system.add_particle(std::move(particle)); + SAW_EXPECT(eov.is_value(), "Expected no error :)"); + } + { + auto& p = system.at({0u}); + auto& rb = p.template get<"rigid_body">(); + auto& pos = rb.template get<"position">(); + + for(saw::data i{0u}; i < saw::data{2u}; ++i){ + std::cout<{1e-2}); + + { + auto& p = system.at({0u}); + auto& rb = p.template get<"rigid_body">(); + auto& pos = rb.template get<"position">(); + + for(saw::data i{0u}; i < saw::data{2u}; ++i){ + std::cout<, 2>>::apply(sstream, cells); SAW_EXPECT(eov.is_value(), "vtk writer failed to write"); + // I want to print it to see it for myself. For now I have no tooling to more easily view associated and potentially generated files std::cout<::type; } } -- cgit v1.2.3