From 2e6ae2ef9bb230930f5213c256e624080b08faca Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Wed, 16 Apr 2025 16:49:14 +0200 Subject: Trying to make a cleaner vtk writer --- c++/collision.hpp | 17 ++++++- c++/examples/cavity_2d.cpp | 12 ++++- c++/write_vtk.hpp | 112 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 c++/write_vtk.hpp diff --git a/c++/collision.hpp b/c++/collision.hpp index 87187c2..ba35be1 100644 --- a/c++/collision.hpp +++ b/c++/collision.hpp @@ -14,15 +14,30 @@ class component { public: using Component = cmpt::BGK; + /** + * Thoughts regarding automating order setup + */ using access = cmpt::access_tuple< cmpt::access<"dfs", 1, true, true, true> >; + /** + * Thoughts regarding automating order setup + */ static constexpr saw::string_literal name = "collision"; static constexpr saw::string_literal after = ""; static constexpr saw::string_literal before = "streaming"; - void apply(saw::data& field){ + /** + * Raw setup + */ + void apply(saw::data& field, saw::data> index, uint64_t time_step){ + bool is_even = ((time_step % 2) == 0); + auto& cell = field.at(index); + + auto& dfs_old = is_even ? cell.template get<"dfs_old">() : cell.template get<"dfs">(); + auto& dfs = (!is_even) ? cell.template get<"dfs_old">() : cell.template get<"dfs">(); + } }; diff --git a/c++/examples/cavity_2d.cpp b/c++/examples/cavity_2d.cpp index 88a7702..0a8e9b1 100644 --- a/c++/examples/cavity_2d.cpp +++ b/c++/examples/cavity_2d.cpp @@ -71,6 +71,9 @@ void compute_rho_u ( vel[1] /= rho; } +/** + * Unsure if feasible. I would have a rho normalization on the dfs and then I would use the const rho_computation + */ template void compute_const_rho_u ( saw::data>& dfs, @@ -80,15 +83,20 @@ void compute_const_rho_u ( { using dfi = df_info; + saw::native_data_type::type real_rho = 0; std::fill(vel.begin(), vel.end(), 0); for(size_t i = 0; i < Desc::Q; ++i){ + real_rho += dfs(i).get(); vel[0] += dfi::directions[i][0] * dfs(i).get(); vel[1] += dfi::directions[i][1] * dfs(i).get(); } + for(size_t i = 0; i < Desc::Q; ++i){ + dfs(i).set(dfs(i).get() * (rho/real_rho)); + } - vel[0] /= rho; - vel[1] /= rho; + vel[0] *= real_rho / (rho*rho) + vel[1] *= real_rho / (rho*rho) } /** diff --git a/c++/write_vtk.hpp b/c++/write_vtk.hpp new file mode 100644 index 0000000..7bde854 --- /dev/null +++ b/c++/write_vtk.hpp @@ -0,0 +1,112 @@ +#pragma once + +#include + +namespace kel { +namespace lbm { +namespace impl { + +template +struct lbm_vtk_writer...> { + + saw::error_or apply(){ + return saw::make_void(); + } +}; + +template +struct lbm_vtk_writer { +}; + +template +struct lbm_vtk_writer...>>> { + template + saw::error_or write_i(std::ofstream& vtk_file, const saw::data...>>>& field){ + + // vtk_file<< + + return saw::make_void(); + } + + template + saw::error_or iterate_i(std::ofstream& vtk_file, const saw::data...>>>& field){ + { + auto eov = write_i(vtk_file, field); + if(eov.is_error()){ + return eov; + } + } + if constexpr ( (i+1u) < sizeof...(StructT) ){ + return iterate_i(vtk_file, field); + } + return saw::make_void(); + } + + saw::error_or apply(std::ofstream& vtk_file, + const saw::data...>>>& field){ + + auto meta = field.meta(); + // DIMENSIONS + { + vtk_file << "DIMENSIONS "; + for(saw::data i{0u}; i < Desc::D; ++i){ + vtk_file << " " << meta.at(i).get(); + } + for(saw::data i{Desc::D}; i < 3u; ++i){ + vtk_file << " 1"; + } + + vtk_file << "\n"; + } + + // HEADER TO BODY + { + vtk_file << "\n"; + } + + if constexpr (sizeof...(StructT) > 0u){ + return iterate_i<0u>(vtk_file, field); + } + + return saw::make_void(); + } +}; +} + +template +saw::error_or write_vtk(const std::string_view file_name, const saw::data& field){ + + std::string vtk_file_name{file_name}; + std::ofstream vtk_file{vtk_file_name}; + + if(!vtk_file.is_open()){ + return saw::make_error("Could not open file."); + } + + 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" + ; + + + auto eov = lbm_vtk_writer::apply(vtk_file, field); + return eov; + /* + vtk_file <<"# vtk DataFile Version 3.0\n"; + vtk_file <<"Velocity Cavity2D example\n"; + vtk_file <<"ASCII\n"; + vtk_file <<"DATASET STRUCTURED_POINTS\n"; + vtk_file <<"DIMENSIONS "<< dim_x <<" "<