diff options
| author | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-12-19 14:11:57 +0100 |
|---|---|---|
| committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-12-19 14:11:57 +0100 |
| commit | 6a918f0827a6e8806f77e39e0348b1a2abed71f2 (patch) | |
| tree | 30e70bf0533e91a4aa9246de1dd3aa256c64d0c1 /lib/core/c++/write_vtk.hpp | |
| parent | 434607c33deab6ad91cfeb203050138c108958ed (diff) | |
| download | libs-lbm-6a918f0827a6e8806f77e39e0348b1a2abed71f2.tar.gz | |
Rewriting large portion for simpler approach
Diffstat (limited to 'lib/core/c++/write_vtk.hpp')
| -rw-r--r-- | lib/core/c++/write_vtk.hpp | 129 |
1 files changed, 125 insertions, 4 deletions
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 <forstio/codec/data_math.hpp> #include "descriptor.hpp" +#include "flatten.hpp" #include <fstream> #include <filesystem> @@ -186,19 +187,139 @@ struct lbm_vtk_writer<sch::Array<sch::Struct<sch::Member<StructT,StructN>...> , return saw::make_void(); } }; + +template<typename StructT, uint64_t Dim> +struct lbm_vtk_writer_raw { + template<uint64_t i, uint64_t Dep> + static saw::error_or<void> write_i_iterate_d(std::ostream& vtk_file, const saw::data<StructT>* field, const saw::data<sch::FixedArray<sch::UInt64,Dim>>& meta, saw::data<sch::FixedArray<sch::UInt64,Dim>>& index){ + constexpr auto Lit = saw::schema_type_at_index<i, StructT>::Type::KeyLiteral; + using Type = typename saw::schema_type_at_index<i, StructT>::Type::ValueType; + + if constexpr (Dep == 0u){ + auto flat_index = flatten_index<sch::UInt64,Dim>::apply(index, meta); + return lbm_vtk_writer<Type>::apply(vtk_file, field[flat_index.get()].template get<Lit>()); + } 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<i,Dep-1u>(vtk_file, field, meta, index); + if(eov.is_error()){ + return eov; + } + } + } + return saw::make_void(); + } + + template<uint64_t i> + static saw::error_or<void> write_i(std::ostream& vtk_file, const saw::data<StructT>* field, const saw::data<sch::FixedArray<sch::UInt64,Dim>>& meta){ + constexpr auto Lit = saw::schema_type_at_index<i, StructT>::Type::KeyLiteral; + + // auto meta = field.get_dims(); + saw::data<sch::FixedArray<sch::UInt64,Dim>> index; + for(saw::data<sch::UInt64> it{0}; it.get() < Dim; ++it){ + index.at({0u}).set(0u); + } + // vtk_file write? + // Data header + { + + auto eov = lbm_vtk_writer<typename saw::schema_type_at_index<i,StructT>::Type::ValueType>::apply_header(vtk_file, Lit.view()); + if(eov.is_error()){ + return eov; + } + + } + return write_i_iterate_d<i,Dim>(vtk_file, field, meta, index); + } + template<uint64_t i> + static saw::error_or<void> iterate_i(std::ostream& vtk_file, const saw::data<StructT>* field, + const saw::data<sch::FixedArray<sch::UInt64,Dim>>& meta){ + { + auto eov = write_i<i>(vtk_file, field, meta); + if(eov.is_error()){ + return eov; + } + } + if constexpr ( (i+1u) < saw::schema_width<StructT>::value ){ + return iterate_i<i+1u>(vtk_file, field, meta); + } + return saw::make_void(); + } + + static saw::error_or<void> apply(std::ostream& vtk_file, const saw::data<StructT>* field, + const saw::data<sch::FixedArray<sch::UInt64,Dim>>& 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<sch::UInt64> pd_size{1u}; + // DIMENSIONS + + { + vtk_file << "DIMENSIONS"; + for(saw::data<sch::UInt64> i{0u}; i.get() < Dim; ++i){ + pd_size = pd_size * meta.at(i); + vtk_file << " " << meta.at(i).get(); + } + for(saw::data<sch::UInt64> i{Dim}; i.get() < 3u; ++i){ + vtk_file << " 1"; + } + + vtk_file << "\n"; + } + + if constexpr (saw::schema_width<StructT>::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<typename Struct> -saw::error_or<void> write_vtk_file(const std::filesystem::path& file_name, const saw::data<Struct>& field){ +template<typename Sch> +saw::error_or<void> write_vtk_file(const std::filesystem::path& file_name, const saw::data<Sch>& field){ std::ofstream vtk_file{file_name}; - if(!vtk_file.is_open()){ + if( not vtk_file.is_open() ){ return saw::make_error<saw::err::critical>("Could not open file."); } + auto eov = impl::lbm_vtk_writer<Sch>::apply(vtk_file, field); + return eov; +} + +template<typename StructInRaw, uint64_t D> +saw::error_or<void> write_vtk_file( + const std::filesystem::path& file_name, + const saw::data<StructInRaw>* field, + const saw::data<sch::FixedArray<sch::UInt64,D>>& meta + ){ + std::ofstream vtk_file{file_name}; + + if( not vtk_file.is_open() ){ + return saw::make_error<saw::err::critical>("Could not open file."); + } - auto eov = impl::lbm_vtk_writer<Struct>::apply(vtk_file, field); + auto eov = impl::lbm_vtk_writer_raw<StructInRaw,D>::apply(vtk_file, field, meta); return eov; } } |
