#pragma once #include #include #include #include "descriptor.hpp" #include "flatten.hpp" #include "chunk.hpp" #include #include namespace kel { namespace lbm { namespace impl { template struct lbm_csv_writer { }; template struct lbm_csv_writer> { static saw::error_or apply(std::ostream& csv_file, const saw::data>& field){ if constexpr (std::is_same_v and D == 1u) { csv_file<().get(); }else{ csv_file< struct lbm_csv_writer> { static saw::error_or apply(std::ostream& csv_file, const saw::data>& field){ saw::data> index; for(saw::data it{0}; it.get() < D; ++it){ index.at({0u}).set(0u); } // csv_file<<"VECTORS "< 0){ csv_file<<","; } csv_file< struct lbm_csv_writer> { template static saw::error_or apply_d(std::ostream& csv_file, const saw::data>& field, saw::data>& index){ // VTK wants to iterate over z,y,x instead of x,y,z // So we do the same with CSV to stay consistent for now // We could reorder the dimensions, but eh if constexpr ( d > 0u){ for(index.at({d-1u}) = 0u; index.at({d-1u}) < field.get_dims().at({d-1u}); ++index.at({d-1u})){ auto eov = apply_d(csv_file, field, index); } }else{ auto eov = lbm_csv_writer::apply(csv_file, field.at(index)); csv_file<<"\n"; if(eov.is_error()) return eov; } return saw::make_void(); } static saw::error_or apply(std::ostream& csv_file, const saw::data>& field, std::string_view name){ saw::data> index; for(saw::data it{0}; it.get() < sizeof...(D); ++it){ index.at({0u}).set(0u); } { auto eov = apply_d(csv_file, field, index); if(eov.is_error()){ return eov; } } return saw::make_void(); } }; template struct lbm_csv_writer> { static saw::error_or apply(std::ostream& csv_file, const saw::data>& field){ static_assert(D > 0, "Non-dimensionality is bad for velocity."); // csv_file<<"VECTORS "< 0){ csv_file<<","; } { auto eov = lbm_csv_writer::apply(csv_file,field.at({{i}})); if(eov.is_error()) return eov; } } return saw::make_void(); } }; template struct lbm_csv_writer> { static saw::error_or apply(std::ostream& csv_file, const saw::data>& field){ return lbm_csv_writer::apply(csv_file,field.at({})); } }; template struct lbm_csv_writer,Keys>...>> final { template static saw::error_or iterate_i( const std::filesystem::path& csv_dir, const std::string_view& file_base_name, uint64_t d_t, const saw::data,Keys>...>>& field){ if constexpr ( i < sizeof...(MemberT) ) { using MT = typename saw::parameter_pack_type,Keys>...>::type; { std::stringstream sstr; sstr <("Could not open file."); } // auto eov = lbm_csv_writer::apply(csv_file,field.template get(), MT::KeyLiteral.view()); if(eov.is_error()){ return eov; } } return iterate_i(csv_dir, file_base_name, d_t,field); } return saw::make_void(); } static saw::error_or apply( const std::filesystem::path& csv_dir, const std::string_view& file_base_name, uint64_t d_t, const saw::data,Keys>...>>& field){ auto& field_0 = field.template get::literal>(); auto meta = field_0.get_dims(); return iterate_i<0u>(csv_dir,file_base_name, d_t, field); } }; } template saw::error_or write_csv_file(const std::filesystem::path& out_dir, const std::string_view& file_name, uint64_t d_t, const saw::data& field){ auto csv_dir = out_dir / "csv"; { std::error_code ec; std::filesystem::create_directories(csv_dir,ec); if(ec != std::errc{}){ return saw::make_error("Could not create directory for write_csv_file function"); } } auto eov = impl::lbm_csv_writer::apply(csv_dir, file_name, d_t, field); return eov; } } }