From 4a291705f46086d5adcf68de6d6d1441c4b9e4f9 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Thu, 8 May 2025 14:37:06 +0200 Subject: Built a working vtk file writer. Would prefer a lambda writer tbh --- c++/macroscopic.hpp | 31 +++++++++++++++++- c++/write_vtk.hpp | 92 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 92 insertions(+), 31 deletions(-) (limited to 'c++') diff --git a/c++/macroscopic.hpp b/c++/macroscopic.hpp index 43c727b..e126bbe 100644 --- a/c++/macroscopic.hpp +++ b/c++/macroscopic.hpp @@ -9,7 +9,7 @@ namespace lbm { */ template void compute_rho_u ( - saw::data>& dfs, + const saw::data>& dfs, typename saw::native_data_type::type& rho, std::array::type, 2>& vel ) @@ -30,5 +30,34 @@ void compute_rho_u ( vel[i] /= rho; } } + +/** + * Calculate the macroscopic variables rho and u in Lattice Units. + */ +template +void compute_rho_u ( + const saw::data>& dfs, + saw::ref> rho, + saw::ref>> vel + ) +{ + using dfi = df_info; + + rho().set(0); + for(uint64_t i = 0; i < Desc::D; ++i){ + vel().at({i}).set(0); + } + + for(size_t j = 0; j < Desc::Q; ++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); + } + } + + for(size_t i = 0; i < Desc::D; ++i){ + vel[i] = vel[i] / rho; + } +} } } diff --git a/c++/write_vtk.hpp b/c++/write_vtk.hpp index 294ec6d..5cbc6c0 100644 --- a/c++/write_vtk.hpp +++ b/c++/write_vtk.hpp @@ -16,59 +16,91 @@ template struct lbm_vtk_writer { }; -template -struct lbm_vtk_writer...>> { - - static saw::error_or apply(){ +template +struct lbm_vtk_writer> { + static saw::error_or apply_header(std::ostream& vtk_file, std::string_view name){ + vtk_file<<"SCALARS "< apply(std::ostream& vtk_file, const saw::data>& field){ + vtk_file< struct lbm_vtk_writer> { - static saw::error_or apply(std::ostream& vtk_file, std::string_view name){ + static saw::error_or apply_header(std::ostream& vtk_file, std::string_view name){ vtk_file<<"VECTORS "< -struct lbm_vtk_writer> { - static saw::error_or apply(std::ostream& vtk_file, std::string_view name){ - vtk_file<<"VECTORS "< apply(std::ostream& vtk_file, const saw::data>& field){ + static_assert(D > 0, "Non-dimensionality is bad for velocity."); + static_assert(D <= 3, "4th dimension as well. Mostly due to vtk."); + + // vtk_file<<"VECTORS "< 0){ + vtk_file<<" "; + } + vtk_file< -struct lbm_vtk_writer...>>> { - template - static saw::error_or write_i_iterate_d(std::ostream& vtk_file, const saw::data...>>>& field, saw::data>& index){ - if constexpr (Dep == Desc::D){ +template +struct lbm_vtk_writer...> , Dim>> { + template + static saw::error_or write_i_iterate_d(std::ostream& vtk_file, const saw::data...>,Dim>>& field, saw::data>& index){ + constexpr auto Lit = saw::parameter_key_pack_type::literal; + using Type = typename saw::parameter_pack_type::type; + + if constexpr (Dep == Dim){ + return lbm_vtk_writer::apply(vtk_file, field.at(index).template get()); + } else { + // Dep < Dim // I hope + static_assert(Dep < Dim, "Don't fall into this case"); + for(index.at({Dep}) = 0; index.at({Dep}) < field.get_dims().at({Dep}); ++index.at({Dep})){ + auto eov = write_i_iterate_d(vtk_file, field, 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){ + static saw::error_or write_i(std::ostream& vtk_file, const saw::data...>,Dim>>& field){ - auto meta = field.meta(); - saw::data> index; - for(saw::data it{0}; it.get() < Desc::D; ++it){ + 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>::apply(vtk_file, saw::parameter_key_pack_type::literal.view()); + + auto eov = lbm_vtk_writer::type>::apply_header(vtk_file, saw::parameter_key_pack_type::literal.view()); + if(eov.is_error()){ + return eov; + } + } return write_i_iterate_d(vtk_file, field, index); - - return saw::make_void(); } template - static saw::error_or iterate_i(std::ostream& vtk_file, const saw::data...>>>& field){ + static saw::error_or iterate_i(std::ostream& vtk_file, const saw::data...>, Dim>>& field){ + constexpr auto Lit = saw::parameter_key_pack_type::literal; { auto eov = write_i(vtk_file, field); if(eov.is_error()){ @@ -82,28 +114,28 @@ struct lbm_vtk_writer apply(std::ostream& vtk_file, - const saw::data...>>>& field){ + const saw::data...>, Dim>>& field){ vtk_file <<"# vtk DataFile Version 3.0\n" <<"LBM File\n" <<"ASCII\n" - <<"DATASET STRUCTURED POINTS\n" + <<"DATASET STRUCTURED_POINTS\n" <<"SPACING 1.0 1.0 1.0\n" <<"ORIGIN 0.0 0.0 0.0\n" ; - auto meta = field.meta(); + auto meta = field.get_dims(); saw::data pd_size{1u}; // DIMENSIONS { - vtk_file << "DIMENSIONS "; - for(saw::data i{0u}; i.get() < Desc::D; ++i){ + 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{Desc::D}; i.get() < 3u; ++i){ + for(saw::data i{Dim}; i.get() < 3u; ++i){ vtk_file << " 1"; } -- cgit v1.2.3