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++/write_vtk.hpp | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 c++/write_vtk.hpp (limited to 'c++/write_vtk.hpp') 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 <<" "<