From 6a918f0827a6e8806f77e39e0348b1a2abed71f2 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 19 Dec 2025 14:11:57 +0100 Subject: Rewriting large portion for simpler approach --- lib/core/c++/flatten.hpp | 44 ++++++++++ lib/core/c++/particle/particle.hpp | 19 ++++- lib/core/c++/write_vtk.hpp | 129 +++++++++++++++++++++++++++++- lib/core/tests/flatten.cpp | 22 +++++ lib/core/tests/particle_flow_coupling.cpp | 23 ------ lib/core/tests/particles.cpp | 105 ------------------------ lib/core/tests/particles.dummy | 114 ++++++++++++++++++++++++++ lib/core/tests/vtk_write.cpp | 28 +++++++ 8 files changed, 351 insertions(+), 133 deletions(-) create mode 100644 lib/core/c++/flatten.hpp create mode 100644 lib/core/tests/flatten.cpp delete mode 100644 lib/core/tests/particle_flow_coupling.cpp delete mode 100644 lib/core/tests/particles.cpp create mode 100644 lib/core/tests/particles.dummy (limited to 'lib') diff --git a/lib/core/c++/flatten.hpp b/lib/core/c++/flatten.hpp new file mode 100644 index 0000000..164bb77 --- /dev/null +++ b/lib/core/c++/flatten.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include + +namespace kel { +namespace lbm { +namespace sch { +using namespace saw::schema; +} + +template +struct flatten_index { +public: + template + static constexpr saw::data stride(const saw::data>& meta) { + if constexpr (i > 0u){ + return stride(meta) * meta.at({i-1u}); + } + + return 1u; + } + +private: + /// 2,3,4 => 2,6,24 + /// i + j * 2 + k * 3*2 + /// 1 + 2 * 2 + 3 * 3*2 = 1+4+18 = 23 + template + static void apply_i(saw::data& flat_ind, const saw::data>& index, const saw::data>& meta){ + if constexpr ( D > i ) { + flat_ind = flat_ind + index.at({i}) * stride(meta); + apply_i(flat_ind,index,meta); + } + } +public: + static saw::data apply(const saw::data>& index, const saw::data>& meta){ + saw::data flat_ind{0u}; + + apply_i<0u>(flat_ind, index, meta); + return flat_ind; + } +}; +} +} diff --git a/lib/core/c++/particle/particle.hpp b/lib/core/c++/particle/particle.hpp index 446e8a3..b647ebe 100644 --- a/lib/core/c++/particle/particle.hpp +++ b/lib/core/c++/particle/particle.hpp @@ -34,10 +34,11 @@ using ParticleCollisionSpheroid = Struct< Member >; -template +template> using Particle = Struct< Member, "rigid_body">, Member, "mask">, + Member, Member, Member >; @@ -65,6 +66,22 @@ constexpr auto verlet_step_lambda = [](saw::data>& particle, pos = pos_new; }; +/** +* +* +*/ +template +constexpr auto broadphase_collision_check = [](saw::data>& left, saw::data>& right){ + auto rad_l = left.template get<"collision">().template get<"radius">(); + auto rad_r = right.template get<"collision">().template get<"radius">(); + + auto& rb_l = left.template get<"rigid_body">(); + auto& rb_r = right.template get<"rigid_body">(); + + auto& pos_l = rb_l.template get<"position">(); + auto& pos_r = rb_r.template get<"position">(); +} + template > class particle_system { private: diff --git a/lib/core/c++/write_vtk.hpp b/lib/core/c++/write_vtk.hpp index 0647db5..7d42bc9 100644 --- a/lib/core/c++/write_vtk.hpp +++ b/lib/core/c++/write_vtk.hpp @@ -6,6 +6,7 @@ #include #include "descriptor.hpp" +#include "flatten.hpp" #include #include @@ -186,19 +187,139 @@ struct lbm_vtk_writer...> , return saw::make_void(); } }; + +template +struct lbm_vtk_writer_raw { + template + static saw::error_or write_i_iterate_d(std::ostream& vtk_file, const saw::data* field, const saw::data>& meta, saw::data>& index){ + constexpr auto Lit = saw::schema_type_at_index::Type::KeyLiteral; + using Type = typename saw::schema_type_at_index::Type::ValueType; + + if constexpr (Dep == 0u){ + auto flat_index = flatten_index::apply(index, meta); + return lbm_vtk_writer::apply(vtk_file, field[flat_index.get()].template get()); + } else { + // Dep < Dim // I hope + static_assert(Dep > 0u, "Don't fall into this case"); + for(index.at({Dep-1u}) = 0; index.at({Dep-1u}) < meta.at({Dep-1u}); ++index.at({Dep-1u})){ + auto eov = write_i_iterate_d(vtk_file, field, meta, index); + if(eov.is_error()){ + return eov; + } + } + } + return saw::make_void(); + } + + template + static saw::error_or write_i(std::ostream& vtk_file, const saw::data* field, const saw::data>& meta){ + constexpr auto Lit = saw::schema_type_at_index::Type::KeyLiteral; + + // auto meta = field.get_dims(); + saw::data> index; + for(saw::data it{0}; it.get() < Dim; ++it){ + index.at({0u}).set(0u); + } + // vtk_file write? + // Data header + { + + auto eov = lbm_vtk_writer::Type::ValueType>::apply_header(vtk_file, Lit.view()); + if(eov.is_error()){ + return eov; + } + + } + return write_i_iterate_d(vtk_file, field, meta, index); + } + template + static saw::error_or iterate_i(std::ostream& vtk_file, const saw::data* field, + const saw::data>& meta){ + { + auto eov = write_i(vtk_file, field, meta); + if(eov.is_error()){ + return eov; + } + } + if constexpr ( (i+1u) < saw::schema_width::value ){ + return iterate_i(vtk_file, field, meta); + } + return saw::make_void(); + } + + static saw::error_or apply(std::ostream& vtk_file, const saw::data* field, + const saw::data>& meta){ + + vtk_file + <<"# vtk DataFile Version 3.0\n" + <<"LBM File\n" + <<"ASCII\n" + <<"DATASET STRUCTURED_POINTS\n" + <<"SPACING 1.0 1.0 1.0\n" + <<"ORIGIN 0.0 0.0 0.0\n" + ; + + saw::data pd_size{1u}; + // DIMENSIONS + + { + vtk_file << "DIMENSIONS"; + for(saw::data i{0u}; i.get() < Dim; ++i){ + pd_size = pd_size * meta.at(i); + vtk_file << " " << meta.at(i).get(); + } + for(saw::data i{Dim}; i.get() < 3u; ++i){ + vtk_file << " 1"; + } + + vtk_file << "\n"; + } + + if constexpr (saw::schema_width::value > 0u){ + // POINT DATA + { + vtk_file << "POINT_DATA " << pd_size.get() <<"\n"; + } + + // HEADER TO BODY + { + vtk_file << "\n"; + } + + return iterate_i<0u>(vtk_file, field,meta); + } + + return saw::make_void(); + } +}; } -template -saw::error_or write_vtk_file(const std::filesystem::path& file_name, const saw::data& field){ +template +saw::error_or write_vtk_file(const std::filesystem::path& file_name, const saw::data& field){ std::ofstream vtk_file{file_name}; - if(!vtk_file.is_open()){ + if( not vtk_file.is_open() ){ return saw::make_error("Could not open file."); } + auto eov = impl::lbm_vtk_writer::apply(vtk_file, field); + return eov; +} + +template +saw::error_or write_vtk_file( + const std::filesystem::path& file_name, + const saw::data* field, + const saw::data>& meta + ){ + std::ofstream vtk_file{file_name}; + + if( not vtk_file.is_open() ){ + return saw::make_error("Could not open file."); + } - auto eov = impl::lbm_vtk_writer::apply(vtk_file, field); + auto eov = impl::lbm_vtk_writer_raw::apply(vtk_file, field, meta); return eov; } } diff --git a/lib/core/tests/flatten.cpp b/lib/core/tests/flatten.cpp new file mode 100644 index 0000000..84b3fa4 --- /dev/null +++ b/lib/core/tests/flatten.cpp @@ -0,0 +1,22 @@ +#include + +#include "../c++/flatten.hpp" + +namespace { +namespace sch { +using namespace saw::schema; +} + +SAW_TEST("Flatten Index Stride"){ + using namespace kel; + + constexpr saw::data zero = lbm::flatten_index::stride<0u>({{2u,4u,3u}}); + constexpr saw::data one = lbm::flatten_index::stride<1u>({{2u,4u,3u}}); + constexpr saw::data two = lbm::flatten_index::stride<2u>({{2u,4u,3u}}); + + SAW_EXPECT(zero.get() == 1u, "Zero is correct"); + SAW_EXPECT(one.get() == 2u, "Zero is correct"); + SAW_EXPECT(two.get() == 8u, "Zero is correct"); +} + +} diff --git a/lib/core/tests/particle_flow_coupling.cpp b/lib/core/tests/particle_flow_coupling.cpp deleted file mode 100644 index c3e3769..0000000 --- a/lib/core/tests/particle_flow_coupling.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include - -#include -#include "../c++/particle/geometry/circle.hpp" - - -namespace { -namespace sch { -using namespace kel::lbm::sch; - -using T = Float64; -} - -SAW_TEST("Particle Coupling"){ - using namespace kel; - lbm::particle_system> system; - - /// What are the steps?# - /// - /// Collide and Stream - -} -} diff --git a/lib/core/tests/particles.cpp b/lib/core/tests/particles.cpp deleted file mode 100644 index 79a6471..0000000 --- a/lib/core/tests/particles.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include - -#include -#include "../c++/particle/geometry/circle.hpp" - - -namespace { -namespace sch { -using namespace kel::lbm::sch; - -using T = Float64; -} -SAW_TEST("Verlet step 2D - Planar"){ - using namespace kel; - - - saw::data> particle; - auto& body = particle.template get<"rigid_body">(); - auto& pos = body.template get<"position">(); - auto& pos_old = body.template get<"position_old">(); - - // auto& rot = body.template get<"rotation">(); - auto& acc = body.template get<"acceleration">(); - - acc.at({{0}}).set({1.0}); - - lbm::verlet_step_lambda(particle,{0.5}); - - 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("Minor Test for mask"){ - using namespace kel; - - lbm::particle_circle_geometry geo; - - auto mask = geo.generate_mask(9u,1u); - - auto& grid = mask.template get<"grid">(); - - for(saw::data i{0u}; i < grid.template get_dim_size<0>(); ++i){ - for(saw::data j{0u}; j < grid.template get_dim_size<1>(); ++j){ - std::cout<> reference_mask{{{4+2,4+2}}}; - //reference_mask.at({{0,0}}); -} -*/ -/* -SAW_TEST("Verlet integration test 2D"){ - using namespace kel; - lbm::particle_system> system; - - { - 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-1}); - - { - 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< + +#include +//#include "../c++/particle/geometry/circle.hpp" + + +namespace { +namespace sch { +using namespace kel::lbm::sch; + +using T = Float64; +} +SAW_TEST("Verlet step 2D - Planar"){ +/* + using namespace kel; + + + saw::data> particle; + auto& body = particle.template get<"rigid_body">(); + auto& pos = body.template get<"position">(); + auto& pos_old = body.template get<"position_old">(); + + // auto& rot = body.template get<"rotation">(); + auto& acc = body.template get<"acceleration">(); + + acc.at({{0}}).set({1.0}); + + lbm::verlet_step_lambda(particle,{0.5}); + + 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("Collision spheroid Test"){ + using namespace kel; + + saw::data part_a; + saw::data part_b; +} + +/* +SAW_TEST("Minor Test for mask"){ + using namespace kel; + + lbm::particle_circle_geometry geo; + + auto mask = geo.generate_mask(9u,1u); + + auto& grid = mask.template get<"grid">(); + + for(saw::data i{0u}; i < grid.template get_dim_size<0>(); ++i){ + for(saw::data j{0u}; j < grid.template get_dim_size<1>(); ++j){ + std::cout<> reference_mask{{{4+2,4+2}}}; + //reference_mask.at({{0,0}}); +} +*/ +/* +SAW_TEST("Verlet integration test 2D"){ + using namespace kel; + lbm::particle_system> system; + + { + 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-1}); + + { + 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<::type; } + +SAW_TEST("VTK Write raw test example"){ + using namespace kel; + + // write_vtk(); + + std::stringstream sstream; + + saw::data, 2>> cells{{{2u,2u}}}; + + auto meta = cells.dims(); + + auto& cell_0 = cells.at({{{0,0}}}); + cell_0.template get<"velocity">()= {{0.5,-0.1}}; + cell_0.template get<"pressure">().set(1.1); + + auto eov = lbm::impl::lbm_vtk_writer_raw,2u>::apply(sstream, &cell_0, meta); + 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