diff options
| author | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-03-04 22:17:34 +0100 |
|---|---|---|
| committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-03-04 22:17:34 +0100 |
| commit | adc69d0f0065228393b055604bb595510bf9cd60 (patch) | |
| tree | f3c8760173a5d0c5ebd99949119882be3869c2a1 | |
| parent | f6d15fa2bd684e8120d8aa88bd01c41bc714f241 (diff) | |
| parent | c4226ba28f0bd783686e6b245f35738dc34cd644 (diff) | |
| download | libs-lbm-adc69d0f0065228393b055604bb595510bf9cd60.tar.gz | |
Merge branch 'dev'
| -rw-r--r-- | examples/poiseulle_particles_2d_gpu/sim.cpp | 76 | ||||
| -rw-r--r-- | lib/core/c++/boundary.hpp | 4 | ||||
| -rw-r--r-- | lib/core/c++/geometry.hpp | 13 | ||||
| -rw-r--r-- | lib/core/c++/geometry/poiseulle_channel.hpp | 15 | ||||
| -rw-r--r-- | lib/core/c++/lbm.hpp | 1 | ||||
| -rw-r--r-- | lib/core/c++/particle/blur.hpp | 1 | ||||
| -rw-r--r-- | lib/core/c++/write_csv.hpp | 184 | ||||
| -rw-r--r-- | scripts/do_stuff | 37 |
8 files changed, 291 insertions, 40 deletions
diff --git a/examples/poiseulle_particles_2d_gpu/sim.cpp b/examples/poiseulle_particles_2d_gpu/sim.cpp index 4bc9dfb..bb81383 100644 --- a/examples/poiseulle_particles_2d_gpu/sim.cpp +++ b/examples/poiseulle_particles_2d_gpu/sim.cpp @@ -131,7 +131,7 @@ saw::error_or<void> setup_initial_conditions( rho.at({}) = {1}; auto& vel = vel_f.at(index); if(info_f.at(index).get() == 2u){ - vel.at({{0u}}) = 0.01; + vel.at({{0u}}) = 0.0; } auto eq = equilibrium<T,Desc>(rho,vel); @@ -259,22 +259,6 @@ saw::error_or<void> step( }); }).wait(); - q.submit([&](acpp::sycl::handler& h){ - component<T,Desc,cmpt::Stream,encode::Sycl<saw::encode::Native>> stream; - - h.parallel_for(acpp::sycl::range<Desc::D>{dim_x,dim_y}, [=](acpp::sycl::id<Desc::D> idx){ - saw::data<sch::FixedArray<sch::UInt64,Desc::D>> index; - for(uint64_t i = 0u; i < Desc::D; ++i){ - index.at({{i}}).set(idx[i]); - } - - auto info = info_f.at(index); - - if(info.get() > 0u){ - stream.apply(fields,index,t_i); - } - }); - }).wait(); // Step /* @@ -353,6 +337,12 @@ saw::error_or<void> lbm_main(int argc, char** argv){ return eov; } } + { + auto eov = write_vtk_file(out_dir,"initial_state",0u,*lbm_data_ptr); + if(eov.is_error()){ + return eov; + } + } saw::data<sch::ChunkStruct<T,Desc>, encode::Sycl<saw::encode::Native>> lbm_sycl_data{sycl_q}; saw::data<sch::MacroStruct<T,Desc>, encode::Sycl<saw::encode::Native>> lbm_sycl_macro_data{sycl_q}; @@ -380,38 +370,76 @@ saw::error_or<void> lbm_main(int argc, char** argv){ } } sycl_q.wait(); - saw::data<sch::UInt64> time_steps{4096ul}; + saw::data<sch::UInt64> time_steps{32ul}; + + auto& info_f = lsd_view.template get<"info">(); for(saw::data<sch::UInt64> i{0u}; i < time_steps and krun; ++i){ + // BC + Collision { - auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + auto eov = step<T,Desc>(lsd_view,lsdm_view,*lbm_particle_data_ptr,i,dev); if(eov.is_error()){ return eov; } } + sycl_q.wait(); { { - auto eov = write_vtk_file(out_dir,"t",i.get(), *lbm_macro_data_ptr); + auto eov = dev.copy_to_host(lbm_sycl_macro_data,*lbm_macro_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_vtk_file(out_dir,"m",i.get(), *lbm_macro_data_ptr); if(eov.is_error()){ return eov; } } } { - auto eov = step<T,Desc>(lsd_view,lsdm_view,*lbm_particle_data_ptr,i,dev); - if(eov.is_error()){ - return eov; + { + auto eov = dev.copy_to_host(lbm_sycl_data,*lbm_data_ptr); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = write_csv_file(out_dir,"lbm",i.get(), *lbm_data_ptr); + if(eov.is_error()){ + return eov; + } } } + // Stream + sycl_q.submit([&](acpp::sycl::handler& h){ + component<T,Desc,cmpt::Stream,encode::Sycl<saw::encode::Native>> stream; + + h.parallel_for(acpp::sycl::range<Desc::D>{dim_x,dim_y}, [=](acpp::sycl::id<Desc::D> idx){ + saw::data<sch::FixedArray<sch::UInt64,Desc::D>> index; + for(uint64_t i = 0u; i < Desc::D; ++i){ + index.at({{i}}).set(idx[i]); + } + + auto info = info_f.at(index); + + if(info.get() > 0u){ + stream.apply(lsd_view,index,i); + } + }); + }).wait(); wait.poll(); if(print_status){ std::cout<<"Status: "<<i.get()<<" of "<<time_steps.get()<<" - "<<(i.template cast_to<sch::Float64>().get() * 100 / time_steps.get())<<"%"<<std::endl; print_status = false; } + print_progress_bar(i.get(), time_steps.get()-1u); } + + // After Loop sycl_q.wait(); { - auto eov = write_vtk_file(out_dir,"t",time_steps.get(), *lbm_macro_data_ptr); + auto eov = write_vtk_file(out_dir,"m",time_steps.get(), *lbm_macro_data_ptr); if(eov.is_error()){ return eov; } diff --git a/lib/core/c++/boundary.hpp b/lib/core/c++/boundary.hpp index d5f3022..0a4ff4d 100644 --- a/lib/core/c++/boundary.hpp +++ b/lib/core/c++/boundary.hpp @@ -135,7 +135,7 @@ public: for(saw::data<sch::UInt64> k{0u}; k < saw::data<sch::UInt64>{Descriptor::Q}; ++k){ auto c_k = dfi::directions[k.get()]; - if(c_k[0u]*known_dir <= 0){ + if(c_k[0u]*known_dir >= 0){ sum_df += dfs_old.at({k}); } } @@ -147,7 +147,7 @@ public: for(saw::data<sch::UInt64> k{0u}; k < saw::data<sch::UInt64>{Descriptor::Q}; ++k){ auto c_k = dfi::directions[k.get()]; - if(c_k[0u]*known_dir < 0){ + if(c_k[0u]*known_dir > 0){ sum_unknown_dfs += dfs_old.at({k}) * c_k[0u]; } } diff --git a/lib/core/c++/geometry.hpp b/lib/core/c++/geometry.hpp index c8a48a6..1c5d0a3 100644 --- a/lib/core/c++/geometry.hpp +++ b/lib/core/c++/geometry.hpp @@ -12,19 +12,6 @@ struct geometry { } }; */ -namespace cmpt { -struct PoiseulleChannel; -} - -template<typename Schema, typename Desc, typename Encode> -class component<Schema, Desc, cmpt::PoiseulleChannel, Encode> final { -private: -public: - template<typename CellFieldSchema> - void apply(saw::data<CellFieldSchema,Encode>& field, const saw::data<sch::FixedArraysch::UInt64,Desc::D>){ - auto& info_f = field.template get<"info">(); - } -}; // Ghost - 0 // Wall - 1 diff --git a/lib/core/c++/geometry/poiseulle_channel.hpp b/lib/core/c++/geometry/poiseulle_channel.hpp index f675a99..f719ec4 100644 --- a/lib/core/c++/geometry/poiseulle_channel.hpp +++ b/lib/core/c++/geometry/poiseulle_channel.hpp @@ -1,7 +1,22 @@ #pragma once +#include "../geometry.hpp" + namespace kel { namespace lbm { +namespace cmpt { +struct PoiseulleChannel; +} + +template<typename Schema, typename Desc, typename Encode> +class component<Schema, Desc, cmpt::PoiseulleChannel, Encode> final { +private: +public: + template<typename CellFieldSchema> + void apply(saw::data<CellFieldSchema,Encode>& field, const saw::data<sch::FixedArraysch::UInt64,Desc::D>){ + auto& info_f = field.template get<"info">(); + } +}; } } diff --git a/lib/core/c++/lbm.hpp b/lib/core/c++/lbm.hpp index a1e088e..d6a0976 100644 --- a/lib/core/c++/lbm.hpp +++ b/lib/core/c++/lbm.hpp @@ -17,6 +17,7 @@ #include "memory.hpp" #include "psm.hpp" #include "stream.hpp" +#include "write_csv.hpp" #include "write_vtk.hpp" #include "util.hpp" diff --git a/lib/core/c++/particle/blur.hpp b/lib/core/c++/particle/blur.hpp index a304e8d..7b93ae9 100644 --- a/lib/core/c++/particle/blur.hpp +++ b/lib/core/c++/particle/blur.hpp @@ -31,7 +31,6 @@ void blur_mask(saw::data<sch::Array<T,D>>& p_mask){ p_mask = blurred_mask; } - } } } diff --git a/lib/core/c++/write_csv.hpp b/lib/core/c++/write_csv.hpp new file mode 100644 index 0000000..a60e208 --- /dev/null +++ b/lib/core/c++/write_csv.hpp @@ -0,0 +1,184 @@ +#pragma once + +#include <forstio/error.hpp> + +#include <forstio/codec/data.hpp> +#include <forstio/codec/data_math.hpp> + +#include "descriptor.hpp" +#include "flatten.hpp" +#include "chunk.hpp" + +#include <fstream> +#include <filesystem> + +namespace kel { +namespace lbm { +namespace impl { + +template<typename CellFieldSchema> +struct lbm_csv_writer { +}; + +template<typename T, uint64_t D> +struct lbm_csv_writer<sch::Primitive<T,D>> { + static saw::error_or<void> apply(std::ostream& csv_file, const saw::data<sch::Primitive<T,D>>& field){ + if constexpr (std::is_same_v<T,sch::UnsignedInteger> and D == 1u) { + csv_file<<field.template cast_to<sch::UInt16>().get(); + }else{ + csv_file<<field.get(); + } + return saw::make_void(); + } +}; + +template<typename T, uint64_t D> +struct lbm_csv_writer<sch::FixedArray<T,D>> { + static saw::error_or<void> apply(std::ostream& csv_file, const saw::data<sch::FixedArray<T,D>>& field){ + saw::data<sch::FixedArray<sch::UInt64,D>> index; + for(saw::data<sch::UInt64> it{0}; it.get() < D; ++it){ + index.at({0u}).set(0u); + } + + // csv_file<<"VECTORS "<<name<<" float\n"; + for(uint64_t i = 0u; i < D; ++i){ + if(i > 0){ + csv_file<<","; + } + csv_file<<field.at({i}).get(); + } + return saw::make_void(); + } +}; + +template<typename T, uint64_t Ghost, uint64_t... D> +struct lbm_csv_writer<sch::Chunk<T,Ghost,D...>> { + + template<uint64_t d> + static saw::error_or<void> apply_d(std::ostream& csv_file, const saw::data<sch::Chunk<T,Ghost,D...>>& field, saw::data<sch::FixedArray<sch::UInt64,sizeof...(D)>>& 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<d-1u>(csv_file, field, index); + } + }else{ + auto eov = lbm_csv_writer<T>::apply(csv_file, field.at(index)); + csv_file<<"\n"; + if(eov.is_error()) return eov; + } + return saw::make_void(); + } + + static saw::error_or<void> apply(std::ostream& csv_file, const saw::data<sch::Chunk<T,Ghost,D...>>& field, std::string_view name){ + saw::data<sch::FixedArray<sch::UInt64,sizeof...(D)>> index; + for(saw::data<sch::UInt64> it{0}; it.get() < sizeof...(D); ++it){ + index.at({0u}).set(0u); + } + + { + auto eov = apply_d<sizeof...(D)>(csv_file, field, index); + if(eov.is_error()){ + return eov; + } + } + + return saw::make_void(); + } +}; + +template<typename T, uint64_t D> +struct lbm_csv_writer<sch::Vector<T,D>> { + static saw::error_or<void> apply(std::ostream& csv_file, const saw::data<sch::Vector<T,D>>& field){ + static_assert(D > 0, "Non-dimensionality is bad for velocity."); + + // csv_file<<"VECTORS "<<name<<" float\n"; + for(uint64_t i = 0u; i < D; ++i){ + if(i > 0){ + csv_file<<","; + } + { + auto eov = lbm_csv_writer<T>::apply(csv_file,field.at({{i}})); + if(eov.is_error()) return eov; + } + } + return saw::make_void(); + } +}; + +template<typename T> +struct lbm_csv_writer<sch::Scalar<T>> { + static saw::error_or<void> apply(std::ostream& csv_file, const saw::data<sch::Scalar<T>>& field){ + return lbm_csv_writer<T>::apply(csv_file,field.at({})); + } +}; + +template<typename... MemberT, saw::string_literal... Keys, uint64_t... Ghost, uint64_t... Dims> +struct lbm_csv_writer<sch::Struct<sch::Member<sch::Chunk<MemberT,Ghost,Dims...>,Keys>...>> final { + template<uint64_t i> + static saw::error_or<void> iterate_i( + const std::filesystem::path& csv_dir, const std::string_view& file_base_name, uint64_t d_t, + const saw::data<sch::Struct<sch::Member<sch::Chunk<MemberT,Ghost,Dims...>,Keys>...>>& field){ + + if constexpr ( i < sizeof...(MemberT) ) { + using MT = typename saw::parameter_pack_type<i,sch::Member<sch::Chunk<MemberT,Ghost,Dims...>,Keys>...>::type; + { + std::stringstream sstr; + sstr + <<file_base_name + <<"_" + <<MT::Key.view() + <<"_" + <<d_t + <<".csv" + ; + std::ofstream csv_file{csv_dir / sstr.str() }; + + if( not csv_file.is_open() ){ + return saw::make_error<saw::err::critical>("Could not open file."); + } + // + auto eov = lbm_csv_writer<typename MT::ValueType>::apply(csv_file,field.template get<MT::KeyLiteral>(), MT::KeyLiteral.view()); + if(eov.is_error()){ + return eov; + } + } + + return iterate_i<i+1u>(csv_dir, file_base_name, d_t,field); + } + + return saw::make_void(); + } + + + static saw::error_or<void> apply( + const std::filesystem::path& csv_dir, const std::string_view& file_base_name, uint64_t d_t, + const saw::data<sch::Struct<sch::Member<sch::Chunk<MemberT,Ghost,Dims...>,Keys>...>>& field){ + + auto& field_0 = field.template get<saw::parameter_key_pack_type<0u,Keys...>::literal>(); + auto meta = field_0.get_dims(); + + return iterate_i<0u>(csv_dir,file_base_name, d_t, field); + } +}; + +} + +template<typename Sch> +saw::error_or<void> write_csv_file(const std::filesystem::path& out_dir, const std::string_view& file_name, uint64_t d_t, const saw::data<Sch>& 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<saw::err::critical>("Could not create directory for write_csv_file function"); + } + } + auto eov = impl::lbm_csv_writer<Sch>::apply(csv_dir, file_name, d_t, field); + return eov; +} + +} +} diff --git a/scripts/do_stuff b/scripts/do_stuff new file mode 100644 index 0000000..6bdbfd7 --- /dev/null +++ b/scripts/do_stuff @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +import csv +import sys +from itertools import islice + +def sum_csv_line(filename, line_number): + """Return the sum of a specific line in a CSV file.""" + try: + with open(filename, newline="") as file: + reader = csv.reader(file) + row = next(islice(reader, line_number - 1, None)) + # Convert values to float and sum + return sum(float(x) for x in row) + except StopIteration: + raise ValueError(f"Line {line_number} exceeds file length") + except ValueError as e: + raise ValueError(f"Non-numeric value encountered: {e}") + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Usage: python script.py <filename> <line_number>") + sys.exit(1) + + filename = sys.argv[1] + try: + line_number = int(sys.argv[2]) + except ValueError: + print("Line number must be an integer.") + sys.exit(1) + + try: + total = sum_csv_line(filename, line_number) + print(f"Sum of line {line_number}: {total}") + except Exception as e: + print(f"Error: {e}") + sys.exit(1) |
