From c0549d71b2109f10c1238db8b22362e7826ba61b Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sun, 5 Jul 2026 15:59:23 +0200 Subject: Just rename from lib to modules --- modules/core/c++/SConscript | 35 ++ modules/core/c++/abstract/data.hpp | 52 +++ modules/core/c++/abstract/templates.hpp | 8 + modules/core/c++/args.hpp | 27 ++ modules/core/c++/boundary.hpp | 249 ++++++++++++++ modules/core/c++/chunk.hpp | 103 ++++++ modules/core/c++/collision.hpp | 201 +++++++++++ modules/core/c++/common.hpp | 12 + modules/core/c++/component.hpp | 38 ++ modules/core/c++/config.hpp | 53 +++ modules/core/c++/converter.hpp | 87 +++++ modules/core/c++/descriptor.hpp | 438 ++++++++++++++++++++++++ modules/core/c++/dfs.hpp | 9 + modules/core/c++/environment.hpp | 80 +++++ modules/core/c++/equilibrium.hpp | 50 +++ modules/core/c++/flatten.hpp | 42 +++ modules/core/c++/fplbm.hpp | 191 +++++++++++ modules/core/c++/geometry.hpp | 21 ++ modules/core/c++/geometry/poiseulle_channel.hpp | 22 ++ modules/core/c++/grid.hpp | 34 ++ modules/core/c++/hlbm.hpp | 168 +++++++++ modules/core/c++/index.hpp | 30 ++ modules/core/c++/iterator.hpp | 99 ++++++ modules/core/c++/lbm.hpp | 65 ++++ modules/core/c++/lbm_unit.hpp | 70 ++++ modules/core/c++/macroscopic.hpp | 36 ++ modules/core/c++/math/math.hpp | 4 + modules/core/c++/math/n_closest.hpp | 54 +++ modules/core/c++/math/n_linear.hpp | 139 ++++++++ modules/core/c++/math/round.hpp | 26 ++ modules/core/c++/memory.hpp | 82 +++++ modules/core/c++/momentum_gather.hpp | 54 +++ modules/core/c++/particle.hpp | 37 ++ modules/core/c++/particle/aabb.hpp | 55 +++ modules/core/c++/particle/blur.hpp | 37 ++ modules/core/c++/particle/common.hpp | 3 + modules/core/c++/particle/geometry/circle.hpp | 85 +++++ modules/core/c++/particle/geometry/cube.hpp | 22 ++ modules/core/c++/particle/particle.hpp | 246 +++++++++++++ modules/core/c++/particle/particle_opa.hpp | 49 +++ modules/core/c++/particle/porosity.hpp | 129 +++++++ modules/core/c++/particle/schema.hpp | 68 ++++ modules/core/c++/psm.hpp | 83 +++++ modules/core/c++/rar.hpp | 25 ++ modules/core/c++/schema.hpp | 11 + modules/core/c++/simulation/common.hpp | 7 + modules/core/c++/simulation/hlbm.hpp | 12 + modules/core/c++/statistics.hpp | 11 + modules/core/c++/stream.hpp | 61 ++++ modules/core/c++/util.hpp | 93 +++++ modules/core/c++/write_csv.hpp | 184 ++++++++++ modules/core/c++/write_json.hpp | 27 ++ modules/core/c++/write_vtk.hpp | 256 ++++++++++++++ 53 files changed, 4080 insertions(+) create mode 100644 modules/core/c++/SConscript create mode 100644 modules/core/c++/abstract/data.hpp create mode 100644 modules/core/c++/abstract/templates.hpp create mode 100644 modules/core/c++/args.hpp create mode 100644 modules/core/c++/boundary.hpp create mode 100644 modules/core/c++/chunk.hpp create mode 100644 modules/core/c++/collision.hpp create mode 100644 modules/core/c++/common.hpp create mode 100644 modules/core/c++/component.hpp create mode 100644 modules/core/c++/config.hpp create mode 100644 modules/core/c++/converter.hpp create mode 100644 modules/core/c++/descriptor.hpp create mode 100644 modules/core/c++/dfs.hpp create mode 100644 modules/core/c++/environment.hpp create mode 100644 modules/core/c++/equilibrium.hpp create mode 100644 modules/core/c++/flatten.hpp create mode 100644 modules/core/c++/fplbm.hpp create mode 100644 modules/core/c++/geometry.hpp create mode 100644 modules/core/c++/geometry/poiseulle_channel.hpp create mode 100644 modules/core/c++/grid.hpp create mode 100644 modules/core/c++/hlbm.hpp create mode 100644 modules/core/c++/index.hpp create mode 100644 modules/core/c++/iterator.hpp create mode 100644 modules/core/c++/lbm.hpp create mode 100644 modules/core/c++/lbm_unit.hpp create mode 100644 modules/core/c++/macroscopic.hpp create mode 100644 modules/core/c++/math/math.hpp create mode 100644 modules/core/c++/math/n_closest.hpp create mode 100644 modules/core/c++/math/n_linear.hpp create mode 100644 modules/core/c++/math/round.hpp create mode 100644 modules/core/c++/memory.hpp create mode 100644 modules/core/c++/momentum_gather.hpp create mode 100644 modules/core/c++/particle.hpp create mode 100644 modules/core/c++/particle/aabb.hpp create mode 100644 modules/core/c++/particle/blur.hpp create mode 100644 modules/core/c++/particle/common.hpp create mode 100644 modules/core/c++/particle/geometry/circle.hpp create mode 100644 modules/core/c++/particle/geometry/cube.hpp create mode 100644 modules/core/c++/particle/particle.hpp create mode 100644 modules/core/c++/particle/particle_opa.hpp create mode 100644 modules/core/c++/particle/porosity.hpp create mode 100644 modules/core/c++/particle/schema.hpp create mode 100644 modules/core/c++/psm.hpp create mode 100644 modules/core/c++/rar.hpp create mode 100644 modules/core/c++/schema.hpp create mode 100644 modules/core/c++/simulation/common.hpp create mode 100644 modules/core/c++/simulation/hlbm.hpp create mode 100644 modules/core/c++/statistics.hpp create mode 100644 modules/core/c++/stream.hpp create mode 100644 modules/core/c++/util.hpp create mode 100644 modules/core/c++/write_csv.hpp create mode 100644 modules/core/c++/write_json.hpp create mode 100644 modules/core/c++/write_vtk.hpp (limited to 'modules/core/c++') diff --git a/modules/core/c++/SConscript b/modules/core/c++/SConscript new file mode 100644 index 0000000..91f5b3e --- /dev/null +++ b/modules/core/c++/SConscript @@ -0,0 +1,35 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +core_env = env.Clone(); + +core_env.sources = sorted(glob.glob(dir_path + "/*.cpp")); +core_env.headers = sorted(glob.glob(dir_path + "/*.hpp")); + +core_env.particle_headers = sorted(glob.glob(dir_path + "/particle/*.hpp")); +core_env.particle_geometry_headers = sorted(glob.glob(dir_path + "/particle/geometry/*.hpp")); + +core_env.math_headers = sorted(glob.glob(dir_path + "/math/*.hpp")); + +env.sources += core_env.sources; +env.headers += core_env.headers; + +## Static lib +objects = [] +core_env.add_source_files(objects, core_env.sources, shared=False); +env.library_static = core_env.StaticLibrary('#build/kel-lbm', [objects]); + +env.Install('$prefix/lib/', env.library_static); +env.Install('$prefix/include/kel/lbm/', core_env.headers); +env.Install('$prefix/include/kel/lbm/particle/', core_env.particle_headers); +env.Install('$prefix/include/kel/lbm/particle/geometry/', core_env.particle_geometry_headers); +env.Install('$prefix/include/kel/lbm/math/', core_env.math_headers); diff --git a/modules/core/c++/abstract/data.hpp b/modules/core/c++/abstract/data.hpp new file mode 100644 index 0000000..ed23268 --- /dev/null +++ b/modules/core/c++/abstract/data.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include "templates.hpp" + +namespace kel { +namespace sch { +struct Void {}; + +struct UnsignedInteger {}; +struct SignedInteger {}; +struct FloatingPoint {}; + +template +struct MixedPrecision { + using Meta = Void; + using StorageType = StorageT; + using InterfaceType = InterfaceT; +}; + +template +struct Primitive { + using Meta = Void; + using Type = PrimType; + static constexpr uint64_t Bytes = N; +}; + +template +struct FixedArray { + using Meta = Void; + using Inner = T; + static constexpr std::array Dimensions{Dims...}; +}; + +template +struct Array { + using Meta = FixedArray; + using Inner = T; + static constexpr std::array Dimensions{Dims}; +}; + +template +struct Tuple { +}; + +} + +template +struct schema { + using Type = Sch; +}; + +} diff --git a/modules/core/c++/abstract/templates.hpp b/modules/core/c++/abstract/templates.hpp new file mode 100644 index 0000000..d9d9faa --- /dev/null +++ b/modules/core/c++/abstract/templates.hpp @@ -0,0 +1,8 @@ +#pragma once + +namespace kel { +namespace lbm { +template +constexpr bool always_false = false; +} +} diff --git a/modules/core/c++/args.hpp b/modules/core/c++/args.hpp new file mode 100644 index 0000000..a0aa941 --- /dev/null +++ b/modules/core/c++/args.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +namespace kel { +namespace lbm { +namespace sch { +using namespace saw::schema; +} + +template +saw::error_or> setup_lbm_env(int argc, char** argv){ + saw::data args_data{argc,argv}; + + saw::codec args_codec; + + saw::data args_decoded; + auto eov = args_codec.decode(args_data,args_decoded); + if(eov.is_error()){ + return eov; + } + + return args_decoded; +} +} +} diff --git a/modules/core/c++/boundary.hpp b/modules/core/c++/boundary.hpp new file mode 100644 index 0000000..01ae7b5 --- /dev/null +++ b/modules/core/c++/boundary.hpp @@ -0,0 +1,249 @@ +#pragma once + +#include "component.hpp" +#include "equilibrium.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +struct BounceBack {}; + +template +struct AntiBounceBack {}; + +template +struct ZouHeHorizontal{}; + +struct Equilibrium {}; + +template +struct ZouHePressureY{}; + +template +struct ZouHeVelocityX {}; +} + +/** + * Full-Way BounceBack. + */ +template +class component { +private: +public: + component() = default; + + using Component = cmpt::BounceBack; + + /** + * 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 = "full_way_bounce_back"; + static constexpr saw::string_literal after = ""; + static constexpr saw::string_literal before = "streaming"; + + /** + * Raw setup + */ + template + void apply(const saw::data& field, const saw::data>& index, saw::data time_step)const{ + bool is_even = ((time_step.get() % 2u) == 0u); + + // This is a ref + + // auto& dfs_f = (is_even) ? field.template get<"dfs">() : field.template get<"dfs_old">(); + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + + using dfi = df_info; + + // Technically use .copy() + auto& dfs_old = dfs_old_f.at(index); + auto df_cpy = dfs_old; + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + dfs_old.at({i}) = df_cpy.at({dfi::opposite_index.at(i)}); + } + } +}; + +/** + * Anti Full-Way BounceBack. + */ +template +class component, Encode> { +private: +public: + component() = default; + + using Component = cmpt::AntiBounceBack; + + /** + * 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 = "full_way_anti_bounce_back"; + static constexpr saw::string_literal after = ""; + static constexpr saw::string_literal before = "streaming"; + + /** + * Raw setup + */ + template + void apply(const saw::data& field, const saw::data>& index, saw::data time_step)const{ + bool is_even = ((time_step.get() % 2u) == 0u); + + // This is a ref + + // auto& dfs_f = (is_even) ? field.template get<"dfs">() : field.template get<"dfs_old">(); + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + + using dfi = df_info; + + // Technically use .copy() + auto& dfs_old = dfs_old_f.at(index); + auto df_cpy = dfs_old; + + static_assert(Descriptor::D == 2u and Descriptor::Q == 9u, "Some parts are hard coded sadly"); + + dfs_old.at({0u}) = df_cpy.at({0u}); + dfs_old.at({1u}) = df_cpy.at({1u}); + dfs_old.at({2u}) = df_cpy.at({2u}); + dfs_old.at({3u}) = df_cpy.at({4u}); + dfs_old.at({4u}) = df_cpy.at({3u}); + dfs_old.at({5u}) = df_cpy.at({7u}); + dfs_old.at({6u}) = df_cpy.at({8u}); + dfs_old.at({7u}) = df_cpy.at({5u}); + dfs_old.at({8u}) = df_cpy.at({6u}); + } +}; + +template +class component final { +private: + saw::data> density_; + saw::data> velocity_; +public: + component( + saw::data> density__, + saw::data> velocity__ + ): + density_{density__}, + velocity_{velocity__} + {} + + template + void apply(const saw::data& field, const saw::data>& index, saw::data time_step) const { + + bool is_even = ((time_step.get() % 2u) == 0u); + using dfi = df_info; + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + + auto eq = equilibrium(density_,velocity_); + + dfs_old_f.at(index) = eq; + } +}; + +/** + * This is massively hacky and expects a lot of conditions + * Either this or mirrored along the horizontal line works + * + * 0 - 2 - 2 + * 0 - 3 - 1 + * 0 - 3 - 1 + * ......... + * 0 - 3 - 1 + * 0 - 2 - 2 + * + */ +template +class component, Encode> final { +private: + saw::data rho_setting_; +public: + component(const saw::data& rho_setting__): + rho_setting_{rho_setting__} + {} + + template + void apply(const saw::data& field, saw::data> index, saw::data time_step) const { + using dfi = df_info; + + bool is_even = ((time_step.get() % 2) == 0); + + auto& info_f = field.template get<"info">(); + + // auto& dfs_f = (is_even) ? field.template get<"dfs">() : field.template get<"dfs_old">(); + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + + auto& dfs_old = dfs_old_f.at(index); + + auto rho_vel_x = [&]() -> saw::data { + if constexpr (Dir){ + auto S = dfs_old.at({0u}) + + dfs_old.at({3u}) + dfs_old.at({4u}) + + (dfs_old.at({1u}) + dfs_old.at({5u}) + dfs_old.at({7u})) * 2u; + return rho_setting_ - S; + } + else if constexpr (not Dir) { + auto S = dfs_old.at({0u}) + + dfs_old.at({3u}) + dfs_old.at({4u}) + + (dfs_old.at({2u}) + dfs_old.at({6u}) + dfs_old.at({8u})) * 2u; + return S - rho_setting_; + } + return {}; + }(); + + static_assert(Descriptor::D == 2u and Descriptor::Q == 9u, "Some parts are hard coded sadly"); + + if constexpr (Dir) { + dfs_old.at({2u}) = dfs_old.at({1u}) + saw::data{2.0 / 3.0} * rho_vel_x; + dfs_old.at({6u}) = dfs_old.at({5u}) + saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({3u}) - dfs_old.at({4u})); + dfs_old.at({8u}) = dfs_old.at({7u}) + saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({4u}) - dfs_old.at({3u})); + }else if constexpr (not Dir){ + dfs_old.at({1u}) = dfs_old.at({2u}) - saw::data{2.0 / 3.0} * rho_vel_x; + dfs_old.at({5u}) = dfs_old.at({6u}) - saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({4u}) - dfs_old.at({3u})); + dfs_old.at({7u}) = dfs_old.at({8u}) - saw::data{1.0 / 6.0} * rho_vel_x + saw::data{0.5} * (dfs_old.at({3u}) - dfs_old.at({4u})); + } + } +}; + +/* +template +class component, Encode> final { +private: + saw::data> velocity_; +public: + component( + saw::data> velocity__ + ): + velocity_{velocity__} + {} + + template + void apply(const saw::data& field, const saw::data>& index, saw::data time_step) const { + + bool is_even = ((time_step.get() % 2u) == 0u); + using dfi = df_info; + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + + } +}; +*/ + +} +} diff --git a/modules/core/c++/chunk.hpp b/modules/core/c++/chunk.hpp new file mode 100644 index 0000000..635af91 --- /dev/null +++ b/modules/core/c++/chunk.hpp @@ -0,0 +1,103 @@ +#pragma once + +#include "common.hpp" +#include "flatten.hpp" + +namespace kel { +namespace lbm { +namespace sch { +namespace impl { +template> +struct chunk_schema_type_helper; + +template +struct chunk_schema_type_helper, saw::tmpl_value_group> final { + using Schema = typename chunk_schema_type_helper, saw::tmpl_value_group>::Schema; +}; + +template +struct chunk_schema_type_helper, saw::tmpl_value_group> final { + using Schema = FixedArray; +}; +} + + +template +struct Chunk { + using InnerSchema = typename impl::chunk_schema_type_helper>::Schema; + using StoredValueSchema = Sch; +}; + +// Not needed for now +template +using SuperChunk = Array; +} +} +} + +namespace saw { + +template +class data,Encode> final { +public: + using Schema = kel::lbm::sch::Chunk; +private: + using InnerSchema = typename Schema::InnerSchema; + using ValueSchema = typename InnerSchema::ValueType; + + data values_; +public: + data& ghost_at(const data>& index){ + return values_.at(index); + } + + const data& ghost_at(const data>& index) const { + return values_.at(index); + } + + static constexpr auto get_ghost_dims() { + return data::get_dims(); + } + + static constexpr auto to_ghost_index(const data>& index){ + std::decay_t ind; + for(uint64_t i = 0u; i < sizeof...(Sides); ++i){ + ind.at({i}) = index.at({i}) + Ghost; + } + return ind; + } + + data& at(const data>& index){ + std::decay_t ind = to_ghost_index(index); + return values_.at(ind); + } + + const data& at(const data>& index) const { + std::decay_t ind = to_ghost_index(index); + return values_.at(ind); + } + + /* + const data& neighbour_at(const data>& index) const { + } + */ + + static constexpr auto get_dims(){ + return data,Encode>{{Sides...}}; + } + + auto flat_data(){ + return values_.flat_data(); + } + + static constexpr auto flat_size() { + return data::flat_dims(); + } +}; + +template +struct meta_schema> { + using MetaSchema = typename meta_schema::MetaSchema; +}; + +} diff --git a/modules/core/c++/collision.hpp b/modules/core/c++/collision.hpp new file mode 100644 index 0000000..023f61f --- /dev/null +++ b/modules/core/c++/collision.hpp @@ -0,0 +1,201 @@ +#pragma once + +#include "macroscopic.hpp" +#include "component.hpp" +#include "equilibrium.hpp" +#include "chunk.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +struct BGK {}; +struct BGKGuo {}; +} + +/** + * Standard BGK collision operator for LBM + */ +template +class component { +private: + saw::data relaxation_; + saw::data frequency_; +public: + component(typename saw::native_data_type::type relaxation__): + relaxation_{{relaxation__}}, + frequency_{saw::data{1} / relaxation_} + {} + + component(const saw::data& relaxation__): + relaxation_{relaxation__}, + frequency_{saw::data{1} / relaxation_} + {} + + 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"; + + /** + * Raw setup + */ + template + void apply(const saw::data& field, saw::data> index, saw::data time_step) const { + bool is_even = ((time_step.get() % 2) == 0); + + // auto& dfs_f = (is_even) ? field.template get<"dfs">() : field.template get<"dfs_old">(); + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + + saw::data> rho; + saw::data> vel; + compute_rho_u(dfs_old_f.at(index),rho,vel); + auto eq = equilibrium(rho,vel); + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + dfs_old_f.at(index).at({i}) = dfs_old_f.at(index).at({i}) + frequency_ * (eq.at(i) - dfs_old_f.at(index).at({i})); + } + } + + template + void apply(const saw::data& field, const saw::data& macros, saw::data> index, saw::data time_step) const { + bool is_even = ((time_step.get() % 2) == 0); + + // auto& dfs_f = (is_even) ? field.template get<"dfs">() : field.template get<"dfs_old">(); + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + + saw::data>& rho = rho_f.at(index); + saw::data>& vel = vel_f.at(index); + + compute_rho_u(dfs_old_f.at(index),rho,vel); + + auto eq = equilibrium(rho,vel); + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + dfs_old_f.at(index).at({i}) = dfs_old_f.at(index).at({i}) + frequency_ * (eq.at(i) - dfs_old_f.at(index).at({i})); + } + } +}; + +template +class component { +private: + typename saw::native_data_type::type relaxation_; + saw::data frequency_; + saw::data> global_force_; +public: + component(typename saw::native_data_type::type relaxation__): + relaxation_{relaxation__}, + frequency_{typename saw::native_data_type::type(1) / relaxation_}, + global_force_{} + {} + + component( + typename saw::native_data_type::type relaxation__, + saw::data>& global_force__ + ): + relaxation_{relaxation__}, + frequency_{typename saw::native_data_type::type(1) / relaxation_}, + global_force_{global_force__} + {} + + using Component = cmpt::BGKGuo; + + using access = cmpt::access_tuple< + cmpt::access<"dfs", 1, true, true, true>, + cmpt::access<"force", 0, true, false> + >; + + /** + * 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"; + + template + void apply(const saw::data& field, const saw::data& macros, saw::data> index, saw::data time_step) const { + // void apply(saw::data& field, saw::data> index, saw::data time_step){ + bool is_even = ((time_step.get() % 2) == 0); + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + auto& dfs = dfs_old_f.at(index); + + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + + saw::data>& rho = rho_f.at(index); + + auto& force_f = macros.template get<"force">(); + auto& force = force_f.at(index); + + auto total_force = force + global_force_; + + saw::data> half; + half.at({}).set(0.5); + auto& vel = vel_f.at(index); + vel = vel + total_force * ( half / rho ); + + compute_rho_u(dfs_old_f.at(index),rho,vel); + auto eq = equilibrium(rho,vel); + + using dfi = df_info; + + + saw::data> dfi_inv_cs2; + dfi_inv_cs2.at({}).set(dfi::inv_cs2); + + + // auto vel = vel_f.at(index); + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + // saw::data ci_min_u{0}; + saw::data> ci; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + ci.at({{d}}).set(static_cast::type>(dfi::directions[i][d])); + } + auto ci_dot_u = saw::math::dot(ci,vel); + + // saw::data> F_i; + // F_i = f * ((c_i - u) * ics2 + * c_i * ics2 * ics2) * w_i; + saw::data> w; + w.at({}).set(dfi::weights[i]); + + /* + saw::data> F_i_sum; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + saw::data> F_i_d; + F_i_d.at({}) = F_i.at({{d}}); + F_i_sum = F_i_sum + F_i_d; + } + */ + auto term1 = (ci-vel) * dfi_inv_cs2; + auto term2 = ci * (ci_dot_u * dfi_inv_cs2 * dfi_inv_cs2); + + auto force_projection = saw::math::dot(term1 + term2, total_force); + + auto F_i = w * force_projection; + + dfs.at({i}) = dfs.at({i}) + + frequency_ + * (eq.at(i) - dfs.at({i}) ) + + F_i.at({}) + * (saw::data{1} - saw::data{0.5f} * frequency_); + } + } +}; +} +} diff --git a/modules/core/c++/common.hpp b/modules/core/c++/common.hpp new file mode 100644 index 0000000..280d953 --- /dev/null +++ b/modules/core/c++/common.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace kel { +namespace lbm { +namespace sch { +using namespace saw::schema; +} +} +} diff --git a/modules/core/c++/component.hpp b/modules/core/c++/component.hpp new file mode 100644 index 0000000..1e5dbbf --- /dev/null +++ b/modules/core/c++/component.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include "descriptor.hpp" + +namespace kel { +namespace lbm { + +namespace cmpt { + +/** + * Maybe for the future + */ +template +class access { +public: + static constexpr saw::string_literal name = Name; + static constexpr uint64_t distance = Dist; + static constexpr bool read = Read; + static constexpr bool write = Write; + static constexpr bool skip_sync = SkipSync; +}; + +/** + * Maybe for the future + */ +template +class access_tuple { +public: +}; +} + +/** + * Component class which forms the basis of each processing step + */ +template +class component {}; +} +} diff --git a/modules/core/c++/config.hpp b/modules/core/c++/config.hpp new file mode 100644 index 0000000..64f7a0f --- /dev/null +++ b/modules/core/c++/config.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include +#include + +#include +#include +#include +#include + +namespace kel { +namespace lbm { +namespace sch { +using namespace saw::schema; +template +using LbmConfig = Struct< + Member, + Member, + Member +>; +} + +template +saw::error_or>> load_lbm_config(std::string_view file_name){ + std::ifstream file{std::string{file_name}}; + + if(!file.is_open()){ + return saw::make_error("Couldn't open file"); + } + + saw::data, saw::encode::Json> lbm_json_conf{saw::heap(1u)}; + + uint8_t ele{}; + while(file.readsome(reinterpret_cast(&ele), 1u) > 0u){ + auto err = lbm_json_conf.get_buffer().push(ele,1u); + if(err.failed()){ + return err; + } + } + + saw::data> lbm_conf; + saw::codec, saw::encode::Json> json_codec; + { + auto eov = json_codec.decode(lbm_json_conf, lbm_conf); + if(eov.is_error()){ + return std::move(eov.get_error()); + } + } + + return lbm_conf; +} +} +} diff --git a/modules/core/c++/converter.hpp b/modules/core/c++/converter.hpp new file mode 100644 index 0000000..4370a2c --- /dev/null +++ b/modules/core/c++/converter.hpp @@ -0,0 +1,87 @@ +#pragma once + +#include "lbm_unit.hpp" +#include "descriptor.hpp" + +namespace kel { +namespace lbm { + +namespace sch { +using namespace saw::schema; +} + +/** + * Helps converting from SI types to LBM types + */ +template +class converter { +private: + saw::data, sch::LbmMeter >::Schema > meter_conv_; + saw::data, sch::LbmSecond >::Schema > second_conv_; +public: + converter() = delete; + converter( + saw::data, sch::LbmMeter >::Schema > meter_conv__, + saw::data, sch::LbmSecond >::Schema > second_conv__ + ): + meter_conv_{meter_conv__}, + second_conv_{second_conv__} + {} + + /** + * Get the conversion parameter with the conversion type + */ + auto conversion_x() const { + return meter_conv_; + } + + /** + * Get the conversion parameter with the conversion type + */ + auto conversion_t() const { + return second_conv_; + } + + auto delta_x() const { + return meter_conv_*saw::data>{1.0}; + } + + auto delta_t() const { + return second_conv_*saw::data>{1.0}; + } + + auto delta_v() const { + return (meter_conv_ / second_conv_) * saw::data>{1.0}; + } + + auto delta_a() const { + return (meter_conv_ / (second_conv_*second_conv_)) * saw::data>{1.0}; + } + + saw::data> meter_si_to_lbm(const saw::data>& m_si) const { + return m_si / meter_conv_; + } + + saw::data> second_si_to_lbm(const saw::data>& s_si) const { + return s_si / second_conv_; + } + + saw::data> velocity_si_to_lbm(const saw::data>& vel_si) const { + return vel_si * second_conv_ / meter_conv_; + } + + saw::data> acceleration_si_to_lbm(const saw::data>& acc_si) const { + return acc_si * (second_conv_ * second_conv_) / meter_conv_; + } + + saw::data> kinematic_viscosity_si_to_lbm (const saw::data>& kin_si) const { + return (kin_si / (meter_conv_ * meter_conv_)) * second_conv_; + } + + template + saw::data> kinematic_viscosity_si_to_tau(const saw::data>& kin_si) const { + return saw::data>{saw::data, sch::LbmKinematicViscosity>::Schema >{df_info::inv_cs2} * kinematic_viscosity_si_to_lbm(kin_si) + saw::data>{0.5}}; + } +}; +} +} diff --git a/modules/core/c++/descriptor.hpp b/modules/core/c++/descriptor.hpp new file mode 100644 index 0000000..9f7399a --- /dev/null +++ b/modules/core/c++/descriptor.hpp @@ -0,0 +1,438 @@ +#pragma once + +#include +#include +#include + +namespace kel { +namespace lbm { +namespace sch { +using namespace saw::schema; + +template +struct Descriptor { + static constexpr uint64_t D = DV; + static constexpr uint64_t Q = QV; +}; + +using D2Q9 = Descriptor<2u,9u>; +//using D2Q5 = Descriptor<2u,5u>; +using D3Q27 = Descriptor<3u,27u>; + +template +struct Cell { + using Descriptor = Desc; + static constexpr uint64_t SC = SC_V; + static constexpr uint64_t DC = DC_V; + static constexpr uint64_t QC = QC_V; + static constexpr uint64_t Size = SC + Desc::D * DC + Desc::Q * QC; +}; + +template +struct CellField{ + using Descriptor = Desc; +}; + +template +struct CellField< + Desc, + Struct< + Member... + > +> { + using Descriptor = Desc; +}; + +template +struct CellFieldStruct { + using Descriptor = Desc; + // using MetaSchema = FixedArray; +}; + +} + +template +class df_info{}; + +/* +namespace impl { +template +struct df_ct_helper { + template + static constexpr uint64_t apply_i(const std::array,Desc::Q>& dirs, const std::array& inp){ + if constexpr ( i < Desc::Q ){ + for(uint64_t j = 0u; j < Desc::D; ++j){ + } + } + return 0u; + } +}; +} +*/ + +template +class df_info> { +public: + using Descriptor = sch::Descriptor<1,3>; + + static constexpr uint64_t D = 1u; + static constexpr uint64_t Q = 3u; + + static constexpr std::array,Q> directions = {{ + { 0}, + {-1}, + { 1} + }}; + + static constexpr std::array::type, Q> weights = { + 2./3., + 1./6., + 1./6. + }; + + static constexpr std::array opposite_index = { + 0,2,1 + }; + + static constexpr typename saw::native_data_type::type inv_cs2 = 3.0; + static constexpr typename saw::native_data_type::type cs2 = 1./3.; +}; + +/** + * D2Q5 Descriptor + */ +template +class df_info> { +public: + using Descriptor = sch::Descriptor<2,5>; + + static constexpr uint64_t D = 2u; + static constexpr uint64_t Q = 5u; + + static constexpr std::array, Q> directions = {{ + { 0, 0}, + {-1, 0}, + { 1, 0}, + { 0,-1}, + { 0, 1}, + }}; + + static constexpr std::array::type,Q> weights = { + 1./3., + 1./6., + 1./6., + 1./6., + 1./6. + }; + + static constexpr std::array opposite_index = { + 0, + 2, + 1, + 4, + 3 + }; + + static constexpr typename saw::native_data_type::type inv_cs2 = 3.0; + static constexpr typename saw::native_data_type::type cs2 = 1./3.; +}; + +template +class df_info> { +public: + using Descriptor = sch::Descriptor<2,9>; + + static constexpr uint64_t D = 2u; + static constexpr uint64_t Q = 9u; + + static constexpr std::array, Q> directions = {{ + { 0, 0}, // 0 + {-1, 0}, // 1 + { 1, 0}, // 2 + { 0,-1}, // 3 + { 0, 1}, // 4 + {-1,-1}, // 5 + { 1, 1}, // 6 + {-1, 1}, // 7 + { 1,-1} // 8 + }}; + + static constexpr std::array::type,Q> weights = { + 4./9., + 1./9., + 1./9., + 1./9., + 1./9., + 1./36., + 1./36., + 1./36., + 1./36. + }; + + static constexpr std::array opposite_index = { + 0, + 2, + 1, + 4, + 3, + 6, + 5, + 8, + 7 + }; + + static constexpr typename saw::native_data_type::type inv_cs2 = 3.0; + static constexpr typename saw::native_data_type::type cs2 = 1./3.; +}; + + +template +class df_info> { +public: + using Descriptor = sch::Descriptor<3,27>; + + static constexpr uint64_t D = 3u; + static constexpr uint64_t Q = 27u; + + static constexpr std::array, Q> directions = {{ + { 0, 0, 0}, // 0 + // Into 1D + {-1, 0, 0}, // 1 + { 1, 0, 0}, // 2 + // Expand into 2D + { 0, -1, 0}, // 3 + {-1, -1, 0}, // 4 + { 1, -1, 0}, // 5 + { 0, 1, 0}, // 6 + {-1, 1, 0}, // 7 + { 1, 1, 0}, // 8 + // Expand into 3D + { 0, 0, -1}, // 9 + {-1, 0, -1}, // 10 + { 1, 0, -1}, // 11 + { 0, -1, -1},// 12 + {-1, -1, -1},// 13 + { 1, -1, -1},// 14 + { 0, 1, -1}, // 15 + {-1, 1, -1}, // 16 + { 1, 1, -1}, // 17 + { 0, 0, 1}, // 18 + {-1, 0, 1}, // 19 + { 1, 0, 1}, // 20 + { 0, -1, 1}, // 21 + {-1, -1, 1}, // 22 + { 1, -1, 1}, // 23 + { 0, 1, 1}, // 24 + {-1, 1, 1}, // 25 + { 1, 1, 1} // 26 + }}; + + static constexpr std::array::type,Q> weights = { + 8./27., + // 1D + 1./36., + 1./36., + // 2D + 1./36., + 1./54., + 1./54., + 1./36., + 1./54., + 1./54., + // 3D + 1./36., + 1./54., + 1./54., + 1./54., + 1./216., + 1./216., + 1./54., + 1./216., + 1./216., + 1./36., + 1./54., + 1./54., + 1./54., + 1./216., + 1./216., + 1./54., + 1./216., + 1./216. + }; + + static constexpr std::array opposite_index = { + 0,2,1, + 6,8,7,3,5,4, + 18,20,19,24,26,25,21,23,22,9,11,10,15,17,16,12,14,13 + }; + + static constexpr typename saw::native_data_type::type inv_cs2 = 3.0; + static constexpr typename saw::native_data_type::type cs2 = 1./3.; +}; + + +template +class cell_schema_builder { +private: + saw::schema_factory factory_struct_; +public: + cell_schema_builder() = default; + + cell_schema_builder(saw::schema_factory inp): + factory_struct_{inp} + {} + + /* + template + constexpr auto require() const noexcept { + return {factory_struct_.add_maybe()}; + } + */ +}; + +} +} + +namespace saw { +template +struct meta_schema> { + using MetaSchema = schema::Void; + using Schema = kel::lbm::sch::Cell; +}; + +template +struct meta_schema> { + using MetaSchema = schema::FixedArray; + using Schema = kel::lbm::sch::CellField; +}; + +template +struct meta_schema>> { + using MetaSchema = schema::FixedArray; + using Schema = kel::lbm::sch::CellFieldStruct>; +}; + +template +class data, Encode> final { +public: + using Schema = kel::lbm::sch::Cell; + using MetaSchema = typename meta_schema::MetaSchema; +private: + data, Encode> inner_; +public: + data() = default; + + data& operator()(const data& index){ + return inner_.at(index); + } + + const data& operator()(const data& index)const{ + return inner_.at(index); + } + + const data, Encode> copy() const { + return *this; + } +}; + +/* + * Create a cellfield + */ +template +class data, Encode> final { +public: + using Schema = kel::lbm::sch::CellField; + using MetaSchema = typename meta_schema::MetaSchema; +private: + data, Encode> inner_; +public: + data() = default; + data(const data& inner_meta__): + inner_{inner_meta__} + {} + + const data meta() const { + return inner_.dims(); + } + + template + data get_dim_size() const { + static_assert(i < Desc::D, "Not enough dimensions"); + return inner_.template get_dim_size(); + } + + const data& operator()(const data, Encode>& index)const{ + return inner_.at(index); + } + + data& operator()(const data, Encode>& index){ + return inner_.at(index); + } + + const data& at(const data, Encode>& index)const{ + return inner_.at(index); + } + + data& at(const data, Encode>& index){ + return inner_.at(index); + } + + data internal_size() const { + return inner_.internal_size(); + } + + data* internal_data() { + return inner_.internal_data(); + } +}; + +/** + * Is basically a struct, but additionally has members for meta() calls. It technically is a Field of a struct, but organized through a struct of fields. + */ +template +class data>, Encode> final { +public: + using Schema = kel::lbm::sch::CellFieldStruct>; + /// @TODO Add MetaSchema to Schema + using MetaSchema = typename meta_schema::MetaSchema; +private: + static_assert(sizeof...(CellFieldsT) > 0u, ""); + data, Encode> inner_; + + template + saw::error_or helper_constructor(const data>& grid_size){ + using MemT = saw::parameter_pack_type::type; + { + inner_.template get() = {grid_size}; + } + if constexpr (sizeof...(CellFieldsT) > (i+1u)){ + return helper_constructor(grid_size); + } + return saw::make_void(); + } +public: + data() = delete; + + data(const data>& grid_size__){ + auto eov = helper_constructor<0u>(grid_size__); + (void)eov; + } + + const data meta() const { + using MemT = saw::parameter_pack_type<0u,CellFieldsT...>::type; + return inner_.template get().meta(); + } + + template + auto& get() { + return inner_.template get(); + } + + template + const auto& get() const { + return inner_.template get(); + } +}; + + +} diff --git a/modules/core/c++/dfs.hpp b/modules/core/c++/dfs.hpp new file mode 100644 index 0000000..175e9f2 --- /dev/null +++ b/modules/core/c++/dfs.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include "common.hpp" + +namespace kel { +namespace lbm { +// Make a view class which handles the AA push pull pattern, or alternatively the esoteric twist +} +} diff --git a/modules/core/c++/environment.hpp b/modules/core/c++/environment.hpp new file mode 100644 index 0000000..d8aa9ae --- /dev/null +++ b/modules/core/c++/environment.hpp @@ -0,0 +1,80 @@ +#pragma once + +#include +#include +#include + +namespace kel { +namespace lbm { + +struct environment { + std::string name; + + std::filesystem::path lbm_dir; + std::filesystem::path data_dir; +}; + +saw::error_or setup_lbm_env(const std::string_view name){ + namespace fs = std::filesystem; + + const char* home_dir = std::getenv("HOME"); + if(not home_dir){ + return saw::make_error("Couldn't find home dir"); + } + + environment env; + + env.lbm_dir = std::filesystem::path{home_dir} / ".lbm"; + + { + // Set default data location to "${lbm_dir}/data" + env.data_dir = env.lbm_dir / "data"; + // LBM Data Location + auto lbm_dir_config = env.lbm_dir / "data_dir_location.txt"; + if( fs::exists(lbm_dir_config) && fs::is_regular_file(lbm_dir_config) ){ + std::ifstream file{lbm_dir_config}; + + if(file.is_open()){ + std::stringstream buffer; + buffer < output_directory(){ + namespace fs = std::filesystem; + + const char* home_dir = std::getenv("HOME"); + if(not home_dir){ + return saw::make_error("Couldn't find home dir"); + } + + auto lbm_dir = std::filesystem::path{home_dir} / ".lbm"; + + { + auto lbm_dir_config = lbm_dir / "data_dir_location.txt"; + if( fs::exists(lbm_dir_config) && fs::is_regular_file(lbm_dir_config) ){ + std::ifstream file{lbm_dir_config}; + + if(file.is_open()){ + std::stringstream buffer; + buffer < +saw::data> equilibrium(saw::data> rho, saw::data> vel){ + using dfi = df_info; + + saw::data> eq; + // Brain broken, here's an owl + // ^ + // 0.0 + // / \ + // | | + // + // Velocity * Velocity meaning || vel ||_2^2 or _2 + saw::data vel_vel{0.0}; + for(uint64_t j = 0u; j < Descriptor::D; ++j){ + vel_vel = vel_vel + vel.at({{j}}) * vel.at({{j}}); + } + + /** + * Calculate equilibrium + */ + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + saw::data vel_c{}; + for(uint64_t j = 0u; j < Descriptor::D; ++j){ + // _2 + vel_c = vel_c + (vel.at({{j}}) * saw::data{static_cast::type>(dfi::directions[i][j])}); + } + + auto vel_c_cs2 = vel_c * saw::data{dfi::inv_cs2}; + + eq.at(i).set( + dfi::weights[i] * rho.at({}).get() * + ( + 1.0 + + vel_c_cs2.get() + - dfi::inv_cs2 * 0.5 * vel_vel.get() + + vel_c_cs2.get() * vel_c_cs2.get() * 0.5 + ) + ); + } + + return eq; +} +} +} diff --git a/modules/core/c++/flatten.hpp b/modules/core/c++/flatten.hpp new file mode 100644 index 0000000..1609589 --- /dev/null +++ b/modules/core/c++/flatten.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +namespace kel { +namespace lbm { +namespace sch { +using namespace saw::schema; +} + +template +struct flatten_index { +public: + template + static constexpr saw::data stride(const saw::data>& meta) { + if constexpr (i > 0u){ + return stride(meta) * meta.at({i-1u}); + } + + return 1u; + } +private: + /// 2,3,4 => 2,6,24 + /// i + j * 2 + k * 3*2 + /// 1 + 2 * 2 + 3 * 3*2 = 1+4+18 = 23 + template + static void apply_i(saw::data& flat_ind, const saw::data>& index, const saw::data>& meta){ + if constexpr ( D > i ) { + flat_ind = flat_ind + index.at({i}) * stride(meta); + apply_i(flat_ind,index,meta); + } + } +public: + static saw::data apply(const saw::data>& index, const saw::data>& meta){ + saw::data flat_ind{0u}; + apply_i<0u>(flat_ind, index, meta); + return flat_ind; + } +}; +} +} diff --git a/modules/core/c++/fplbm.hpp b/modules/core/c++/fplbm.hpp new file mode 100644 index 0000000..c974472 --- /dev/null +++ b/modules/core/c++/fplbm.hpp @@ -0,0 +1,191 @@ +#pragma once + +#include "common.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +struct FpLbm {}; +struct FpLbmOneParticleNoVelocity{}; +} + +template +class component final { +private: + saw::data relaxation_; + saw::data frequency_; +public: + component(typename saw::native_data_type::type relaxation__): + relaxation_{{relaxation__}}, + frequency_{saw::data{1} / relaxation_} + {} + + component(const saw::data& relaxation__): + relaxation_{relaxation__}, + frequency_{saw::data{1} / relaxation_} + {} + + using Component = cmpt::FpLbm; + + + template + void apply(const saw::data& field, const saw::data& macros, saw::data> index, saw::data time_step) const { + // void apply(saw::data& field, saw::data> index, saw::data time_step){ + bool is_even = ((time_step.get() % 2) == 0); + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + auto& dfs = dfs_old_f.at(index); + + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + + saw::data>& rho = rho_f.at(index); + saw::data>& vel = vel_f.at(index); + + compute_rho_u(dfs_old_f.at(index),rho,vel); + auto eq = equilibrium(rho,vel); + + using dfi = df_info; + + auto& force_f = macros.template get<"force">(); + auto& force = force_f.at(index); + + auto& porosity_f = macros.template get<"porosity">(); + auto& porosity = porosity_f.at(index); + + saw::data> dfi_inv_cs2; + dfi_inv_cs2.at({}).set(dfi::inv_cs2); + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + // saw::data ci_min_u{0}; + saw::data> ci; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + ci.at({{d}}).set(static_cast::type>(dfi::directions[i][d])); + } + auto ci_dot_u = saw::math::dot(ci,vel); + + // saw::data> F_i; + // F_i = f * (c_i - u * ics2 + * c_i * ics2 * ics2) * w_i; + saw::data> w; + w.at({}).set(dfi::weights[i]); + + auto F_i_d = saw::math::dot(force * w, (ci - vel * dfi_inv_cs2 + ci * ci_dot_u * dfi_inv_cs2 * dfi_inv_cs2 )); + /* + saw::data> F_i_sum; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + saw::data> F_i_d; + F_i_d.at({}) = F_i.at({{d}}); + F_i_sum = F_i_sum + F_i_d; + } + */ + + dfs.at({i}) = dfs.at({i}) + frequency_ * (eq.at(i) - dfs.at({i}) ) + F_i_d.at({}) * (saw::data{1} - saw::data{0.5f} * frequency_); + } + } +}; + +template +class component final { +private: + saw::data relaxation_; + saw::data frequency_; +public: + component(typename saw::native_data_type::type relaxation__): + relaxation_{{relaxation__}}, + frequency_{saw::data{1} / relaxation_} + {} + + component(const saw::data& relaxation__): + relaxation_{relaxation__}, + frequency_{saw::data{1} / relaxation_} + {} + + using Component = cmpt::FpLbmOneParticleNoVelocity; + + template + void apply(const saw::data& field, const saw::data& macros, saw::data> index, saw::data time_step) const { + // void apply(saw::data& field, saw::data> index, saw::data time_step){ + bool is_even = ((time_step.get() % 2) == 0); + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + auto& dfs = dfs_old_f.at(index); + + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + auto& por_f = macros.template get<"porosity">(); + + // Temporary find a better way + auto& force_f = macros.template get<"force">(); + + + /** + * The other methods all work with a flipped porosity. + * For compat reasons this is also flipped + */ + auto porosity = por_f.at(index); + saw::data> one; + one.at({}) = 1.0; + auto flip_porosity = one - porosity; + + saw::data>& rho = rho_f.at(index); + + saw::data> half; + half.at({}).set(0.5); + saw::data>& vel = vel_f.at(index);// + total_force * ( half / rho ); + + compute_rho_u(dfs_old_f.at(index),rho,vel); + auto eq = equilibrium(rho,vel); + + using dfi = df_info; + + saw::data> min_two; + min_two.at({}).set(-2); + + // Maybe ? + auto& force = force_f.at(index); + force = vel * rho * min_two * flip_porosity; + + saw::data> dfi_inv_cs2; + dfi_inv_cs2.at({}).set(dfi::inv_cs2); + + // auto vel = vel_f.at(index); + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + // saw::data ci_min_u{0}; + saw::data> ci; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + ci.at({{d}}).set(static_cast::type>(dfi::directions[i][d])); + } + auto ci_dot_u = saw::math::dot(ci,vel); + + // saw::data> F_i; + // F_i = f * ((c_i - u) * ics2 + * c_i * ics2 * ics2) * w_i; + saw::data> w; + w.at({}).set(dfi::weights[i]); + + /* + saw::data> F_i_sum; + for(uint64_t d = 0u; d < Descriptor::D; ++d){ + saw::data> F_i_d; + F_i_d.at({}) = F_i.at({{d}}); + F_i_sum = F_i_sum + F_i_d; + } + */ + auto term1 = (ci-vel) * dfi_inv_cs2; + auto term2 = ci * (ci_dot_u * dfi_inv_cs2 * dfi_inv_cs2); + + auto force_projection = saw::math::dot(term1 + term2, force); + + auto F_i = w * force_projection; + + dfs.at({i}) = dfs.at({i}) + + frequency_ + * (eq.at(i) - dfs.at({i}) ) + + F_i.at({}) + * (saw::data{1} - saw::data{0.5f} * frequency_); + } + } + +}; +} +} diff --git a/modules/core/c++/geometry.hpp b/modules/core/c++/geometry.hpp new file mode 100644 index 0000000..1c5d0a3 --- /dev/null +++ b/modules/core/c++/geometry.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "component.hpp" + +namespace kel { +namespace lbm { +/* +template +struct geometry { + void apply(const saw::data& field, const saw::data>& start, const saw::data>& end, const saw::data& type){ + + } +}; +*/ + +// Ghost - 0 +// Wall - 1 +// Fluid - 2 +// Other Stuff - 3-x +} +} diff --git a/modules/core/c++/geometry/poiseulle_channel.hpp b/modules/core/c++/geometry/poiseulle_channel.hpp new file mode 100644 index 0000000..f719ec4 --- /dev/null +++ b/modules/core/c++/geometry/poiseulle_channel.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "../geometry.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +struct PoiseulleChannel; +} + +template +class component final { +private: +public: + template + void apply(saw::data& field, const saw::data){ + auto& info_f = field.template get<"info">(); + } +}; + +} +} diff --git a/modules/core/c++/grid.hpp b/modules/core/c++/grid.hpp new file mode 100644 index 0000000..c9a3b05 --- /dev/null +++ b/modules/core/c++/grid.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "common.hpp" + +namespace kel { +namespace lbm { + +/** + * I'm mixing up the geometry values regarding inflow etc. And a bit of logic + */ +template +class domain_registry final { +private: + saw::data> infos_; +public: + template + saw::data get_template_id() { + static saw::data id{std::numeric_limits::type>::max()}; + + if( id == std::numeric_limits::type>::max() ){ + auto err_or_id = search_or_register_id(T::name); + if(err_or_id.is_error()){ + // Unsure about recovery from this + exit(-1); + } + } + } +}; + +template +void clean_grid(saw::data& info_field){ +} +} +} diff --git a/modules/core/c++/hlbm.hpp b/modules/core/c++/hlbm.hpp new file mode 100644 index 0000000..18c32a5 --- /dev/null +++ b/modules/core/c++/hlbm.hpp @@ -0,0 +1,168 @@ +#pragma once + +#include "macroscopic.hpp" +#include "component.hpp" +#include "equilibrium.hpp" + +#include "particle/particle.hpp" + +#include + +namespace kel { +namespace lbm { +namespace cmpt { +struct HlbmInit {}; +struct Hlbm {}; +struct HlbmParticle {}; + +struct HlbmOneParticleMomentumExchange {}; +} + +template +class component final { +private: + typename saw::native_data_type::type relaxation_; + saw::data frequency_; +public: + component(typename saw::native_data_type::type relaxation__): + relaxation_{relaxation__}, + frequency_{typename saw::native_data_type::type(1) / relaxation_} + {} + + template + void apply(const saw::data& field, const saw::data& macros, saw::data> index, saw::data time_step) const { + auto& porosity_f = macros.template get<"porosity">(); + auto& particle_N_f = field.template get<"particle_N">(); + auto& particle_D_f = field.template get<"particle_D">(); + + auto& por = porosity_f.at(index); + por = {}; + + auto& pnf = particle_N_f.at(index); + pnf = {}; + + auto& pnd = particle_D_f.at(index); + pnd = {}; + } +}; + +/** + * HLBM collision operator for LBM + */ +template +class component final { +private: + typename saw::native_data_type::type relaxation_; + saw::data frequency_; +public: + component(typename saw::native_data_type::type relaxation__): + relaxation_{relaxation__}, + frequency_{typename saw::native_data_type::type(1) / relaxation_} + {} + + template + void apply(const saw::data& field, const saw::data& macros, saw::data> index, saw::data time_step) const { + + bool is_even = ((time_step.get() % 2) == 0); + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + auto& particle_N_f = field.template get<"particle_N">(); + auto& particle_D_f = field.template get<"particle_D">(); + + auto& porosity_f = macros.template get<"porosity">(); + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + + saw::data>& rho = rho_f.at(index); + saw::data>& vel = vel_f.at(index); + + compute_rho_u(dfs_old_f.at(index), rho, vel); + + auto& porosity = porosity_f.at(index); + saw::data> one; + one.at({}) = 1.0; + auto flip_porosity = one - porosity; + + auto& N = particle_N_f.at(index); + auto& D = particle_D_f.at(index); + // Convex combination of velocities + vel = vel * porosity + [&]() -> saw::data> { + return (D.at({}).get() > 0.0 ? N * flip_porosity / D : N); + }(); + // Equilibrium + auto eq = equilibrium(rho,vel); + + for(uint64_t i = 0u; i < Desc::Q; ++i){ + dfs_old_f.at(index).at({i}) = dfs_old_f.at(index).at({i}) + frequency_ * (eq.at(i) - dfs_old_f.at(index).at({i})); + } + + porosity.at({}) = 1.0; + D.at({}) = 0.0; + N = {}; + } +}; + +namespace impl { + +} + +template +class component final { +private: + template + void apply_i(const saw::data& field, const saw::data& macros, const saw::data& part_groups, saw::data> index, saw::data time_step) const { + + } +public: + /* + template + void apply_i() + */ + + template + void apply(const saw::data& field, const saw::data& macros, const saw::data& part_group, saw::data> index, saw::data time_step) const { + /// Figure out how to access the particle list + // auto& p = particles.at(i); + + /// Iterate over the grid bounds + // auto& grid = p.template get<"grid">(); + + auto& part_spheroid_group = part_group; + auto& mvel = macros.template get<"velocity">(); + { + auto& parts = part_spheroid_group.template get<"particles">(); + auto parts_size = parts.meta().at({0u}); + + auto& pi = parts.at(index); + auto& pirb = pi.template get<"rigid_body">(); + auto& pirb_pos = pirb.template get<"position">(); + + saw::data> start; + saw::data> stop; + + auto aabb = particle_aabb::calculate(part_spheroid_group,index,mvel.meta()); + /// Ok, I iterate over the space which covers our particle? So lower bounds to upper bounds + + iterator::apply([&](const auto& index){ + // ask for the d_k value here. + // For every value im iterating over I need sth + std::cout<<"Pos: "< +class component final { +public: + template + void apply(const saw::data& field, const saw::data& macros, const saw::data& part_group, saw::data> index, saw::data time_step) const { + // + } +}; +} +} diff --git a/modules/core/c++/index.hpp b/modules/core/c++/index.hpp new file mode 100644 index 0000000..00e597e --- /dev/null +++ b/modules/core/c++/index.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "common.hpp" + +namespace kel { +namespace lbm { + +template +saw::data> lower_index_from_pos(const saw::data>& pos){ + saw::data> ind; + + for(saw::data i{0u}; i < ind.size(); ++i){ + ind.at(i).set(std::max(pos.at(i).template cast_to().get(),0)); + } + + return ind; +} + +template +saw::data> upper_index_from_pos(const saw::data>& pos){ + auto ind = lower_index_from_pos(pos); + + for(saw::data i{0u}; i < ind.size(); ++i){ + ++ind.at(i); + } + return ind; +} + +} +} diff --git a/modules/core/c++/iterator.hpp b/modules/core/c++/iterator.hpp new file mode 100644 index 0000000..7fd6f58 --- /dev/null +++ b/modules/core/c++/iterator.hpp @@ -0,0 +1,99 @@ +#pragma once + +#include "descriptor.hpp" + +namespace kel { +namespace lbm { +template +void iterate_over(Func&& func, const saw::data>& start, const saw::data>& end, const saw::data>& dist = {{{0u,0u}}}){ + // static_assert(D == 2u, "Currently a lazy implementation for AND combinations of intervalls."); + for(saw::data i{start.at({0u}) + dist.at({0u})}; (i+dist.at({0u})) < end.at({0u}); ++i){ + for(saw::data j{start.at({1u}) + dist.at({1u})}; (j+dist.at({1u})) < end.at({1u}); ++j){ + func({{i,j}}); + } + } + return; +} + +/** + * + */ +template +struct iterator { +private: + template + static void iterate_over_i(Func& func, + const saw::data>& start, + const saw::data>& end, + const saw::data>& dist, + saw::data>& iter + ){ + static_assert(i <= D, "Eh. Too tired to think of a good message"); + if constexpr ( i == D ){ + func(iter); + }else{ + for(iter.at({i}) = start.at({i})+dist.at({i}); (iter.at({i})+dist.at({i}) < end.at({i})); ++iter.at({i})){ + iterate_over_i(func,start,end,dist,iter); + } + } + } +public: + template + static void apply( + Func&& func, + const saw::data>& start, + const saw::data>& end, + const saw::data>& dist = {} + ){ + saw::data> iter; + iterate_over_i<0u,Func>(func, start, end, dist, iter); + } +}; + +template +struct ct_tuple_iterator; + +template +struct ct_tuple_iterator> final { +private: + template + static constexpr saw::error_or apply_i( + Func& func, + saw::data>& tup + ){ + if constexpr ( i < sizeof...(Tup) ){ + auto eov = func(tup.template get()); + if(eov.is_error()){ + return eov; + } + return apply_i(func,tup); + } + + return saw::make_void(); + } +public: + template + static constexpr saw::error_or apply( + Func&& func, + saw::data>& tup + ){ + return apply_i(func,tup); + } +}; + +/* Ambiguous +template +void iterate_over(Func&& func, const saw::data>& start, const saw::data>& end, const saw::data>& dist = {{{0u,0u,0u}}}){ + // static_assert(D == 2u, "Currently a lazy implementation for AND combinations of intervalls."); + for(saw::data i{start.at({0u}) + dist.at({0u})}; (i+dist.at({0u})) < end.at({0u}); ++i){ + for(saw::data j{start.at({1u}) + dist.at({1u})}; (j+dist.at({1u})) < end.at({1u}); ++j){ + for(saw::data k{start.at({2u}) + dist.at({2u})}; (j+dist.at({2u})) < end.at({2u}); ++j){ + func({{k,j,i}}); + } + } + } + return; +} +*/ +} +} diff --git a/modules/core/c++/lbm.hpp b/modules/core/c++/lbm.hpp new file mode 100644 index 0000000..23d30b1 --- /dev/null +++ b/modules/core/c++/lbm.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include "args.hpp" +#include "schema.hpp" +#include "descriptor.hpp" +#include "boundary.hpp" +#include "converter.hpp" +#include "config.hpp" +#include "collision.hpp" +#include "component.hpp" +#include "environment.hpp" +#include "equilibrium.hpp" +#include "flatten.hpp" +#include "fplbm.hpp" +#include "chunk.hpp" +#include "iterator.hpp" +#include "hlbm.hpp" +#include "macroscopic.hpp" +#include "momentum_gather.hpp" +#include "memory.hpp" +#include "psm.hpp" +#include "stream.hpp" +#include "write_csv.hpp" +#include "write_vtk.hpp" +#include "util.hpp" + +#include "math/math.hpp" + +#include +#include + +namespace kel { +namespace lbm { +template +void print_lbm_meta( + const converter& conv, + const saw::data>& kin_vis_si, + const saw::data>& char_vel, + const saw::data>& char_len +){ + std::cout + <<"[Meta]\n" + <<"======\n" + <<"Re: "<<(char_vel * char_len / kin_vis_si)<<"\n" + <<"Ma: "<<(char_vel * saw::data, sch::SiVelocity>::Schema>{std::sqrt(df_info::inv_cs2)})<<"\n" + <<"\n" + <<"[SI]\n" + <<"====\n" + <<"Δx: "<, sch::LbmKinematicViscosity>::Schema >{df_info::inv_cs2} * conv.kinematic_viscosity_si_to_lbm(kin_vis_si) + saw::data>{0.5})<<"\n" + < + +#include + +namespace kel { +namespace lbm { +namespace lbm_type { +struct meter { + static constexpr std::string_view name = "meter_lbm"; + static constexpr std::string_view short_name = "m_lbm"; +}; + +struct second { + static constexpr std::string_view name = "second_lbm"; + static constexpr std::string_view short_name = "s_lbm"; +}; +} + +namespace si_type { +struct meter { + static constexpr std::string_view name = "meter_si"; + static constexpr std::string_view short_name = "m_si"; +}; + +struct second { + static constexpr std::string_view name = "second_si"; + static constexpr std::string_view short_name = "s_si"; +}; +} + +namespace sch { +using namespace saw::schema; +template +using SiMeter = Unit>; + +template +using LbmMeter = Unit>; + +template +using SiSecond = Unit>; + +template +using LbmSecond = Unit>; + +template +using SiVelocity = Unit, UnitElement>; + +template +using LbmVelocity = Unit, UnitElement>; + +template +using SiAcceleration = Unit, UnitElement>; + +template +using LbmAcceleration = Unit, UnitElement>; + +template +using SiKinematicViscosity = Unit, UnitElement>; + +template +using LbmKinematicViscosity = Unit, UnitElement>; + +} +} +} diff --git a/modules/core/c++/macroscopic.hpp b/modules/core/c++/macroscopic.hpp new file mode 100644 index 0000000..19ab3e9 --- /dev/null +++ b/modules/core/c++/macroscopic.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "descriptor.hpp" + +namespace kel { +namespace lbm { +/** + * Calculate the macroscopic variables rho and u in Lattice Units. + */ +template +void compute_rho_u ( + const saw::data>& dfs, + saw::ref>> rho, + saw::ref>> vel + ) +{ + using dfi = df_info; + + rho().at({}).set(0); + for(uint64_t i = 0; i < Desc::D; ++i){ + vel().at({{i}}).set(0); + } + + for(size_t j = 0; j < Desc::Q; ++j){ + rho().at({}) = rho().at({}) + dfs.at({j}); + for(size_t i = 0; i < Desc::D; ++i){ + vel().at({{i}}) = vel().at({{i}}) + saw::data{static_cast::type>(dfi::directions[j][i])} * dfs.at({j}); + } + } + + for(size_t i = 0; i < Desc::D; ++i){ + vel().at({{i}}) = vel().at({{i}}) / rho().at({}); + } +} +} +} diff --git a/modules/core/c++/math/math.hpp b/modules/core/c++/math/math.hpp new file mode 100644 index 0000000..3920bec --- /dev/null +++ b/modules/core/c++/math/math.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "n_linear.hpp" +#include "n_closest.hpp" diff --git a/modules/core/c++/math/n_closest.hpp b/modules/core/c++/math/n_closest.hpp new file mode 100644 index 0000000..ac0fe2f --- /dev/null +++ b/modules/core/c++/math/n_closest.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include "../common.hpp" +#include "../iterator.hpp" + +namespace kel { +namespace lbm { + +template +saw::data n_closest_read(const saw::data,Encode>& f, const saw::data>& frac_ind){ + + auto shift_frac_ind = frac_ind; + for(uint64_t i{0u}; i < D; ++i){ + + shift_frac_ind.at({{i}}) = shift_frac_ind.at({{i}}) + saw::data{0.5}; + if(shift_frac_ind.at({{i}}).get() < 0){ + shift_frac_ind.at({{i}}) = {}; + } + } + + saw::data> shift_ind; + for(uint64_t i{0u}; i < D; ++i){ + shift_ind.at({i}) = frac_ind.at({{i}}).template cast_to(); + } + + return f.at(shift_ind); +} + +template +void n_closest_add(const saw::data,Encode>& f, const saw::data>& frac_ind, const saw::data& val){ + auto shift_frac_ind = frac_ind; + for(uint64_t i{0u}; i < D; ++i){ + + shift_frac_ind.at({{i}}) = shift_frac_ind.at({{i}}) + saw::data{0.5}; + if(shift_frac_ind.at({{i}}).get() < 0){ + shift_frac_ind.at({{i}}) = {}; + } + } + + auto f_meta = f.meta(); + saw::data> shift_ind; + for(uint64_t i{0u}; i < D; ++i){ + shift_ind.at({i}) = frac_ind.at({{i}}).template cast_to(); + if(shift_ind.at({i}) < f_meta.at({i})){ + shift_ind.at({i}) = f_meta.at({i}) - 1u; + } + } + auto& f_i = f.at(shift_ind); + + f_i = f_i + val; +} + +} +} diff --git a/modules/core/c++/math/n_linear.hpp b/modules/core/c++/math/n_linear.hpp new file mode 100644 index 0000000..b378440 --- /dev/null +++ b/modules/core/c++/math/n_linear.hpp @@ -0,0 +1,139 @@ +#pragma once + +#include "../common.hpp" +#include "../iterator.hpp" + +namespace kel { +namespace lbm { +namespace impl { +template +struct n_linear_interpolate_helper final { + template + auto apply(const saw::data& field, const saw::data>& pos){ + return pos; + } +}; +} + +template +saw::data,sch::Vector>> position_to_index_and_fraction(const saw::data>& pos){ + saw::data,sch::Vector>> sep; + + auto& ind = sep.template get<0u>(); + auto& frac = sep.template get<1u>(); + + auto pos_cpy = pos; + // Guarantee that the pos is at least 0 + for(uint64_t i = 0u; i < D; ++i){ + pos_cpy.at({{i}}).set(std::max(pos.at({{i}}).get(), static_cast::type>(0))); + } + + // Now we can cast to uint64_t + for(uint64_t i = 0u; i < D; ++i){ + ind.at({{i}}) = pos_cpy.at({{i}}).template cast_to(); + } + + frac = pos_cpy - ind.template cast_to(); + + return sep; +} + +template +auto floor_index_from_position(const saw::data>& pos){ + return position_to_index_and_fraction(pos).template get<0u>(); +} + +template +saw::data,sch::Vector>> position_to_index_and_fraction_bounded( + const saw::data>& pos, + const saw::data>& bound) +{ + auto infr = position_to_index_and_fraction(pos); + auto& ind = infr.template get<0u>(); + auto& fra = infr.template get<1u>(); + for(uint64_t i = 0u; i < D; ++i){ + // If index is higher than bound. Set to bound and reset fraction + if((ind.at({{i}}).get()+1u) >= bound.at({{i}}).get()){ + ind.at({{i}}).set(bound.at({{i}}).get()-1u); + fra.at({{i}}) = {}; + } + } + return infr; +} + + +template +auto n_linear_interpolate( + const saw::data& field, const saw::data>& pos){ + + // Pos + auto pos_bound = pos; + + // Dimensions + auto meta = field.dims(); + + // Lower Index + saw::data> ind; + + for(saw::data i{0u}; i < saw::data{D}; ++i){ + // Native Positive i + auto npos_i = pos.at({i}).get(); + + { + // Ok I want to stay in bounds + npos_i = std::min(npos_i,meta.at(i).get()-1.0); + npos_i = std::max(npos_i,1.0); + } + + // Native Index i + auto nind_i = static_cast(std::floor(npos_i))-1ul; + + // Set index to i + ind.at(i).set(nind_i); + } + saw::data> pos_frac; + for(saw::data i{0u}; i < saw::data{D}; ++i){ + pos_frac.at({i}) = pos_bound.at({i}) - ind.at(i).template cast_to(); + } + + // Base value + saw::data res; + + // constexpr uint64_t d_corners = 1ul << D; + + saw::data> ones_ind; + for(saw::data i{0u}; i < saw::data{D}; ++i){ + ones_ind.at({i}).set(1u); + } + + iterator::apply([&](auto ind){ + // Iterates over (0,0,0) to (1,1,1) + saw::data weight{1.0}; + + for(saw::data d{0u}; d < saw::data{D}; ++d){ + + saw::data t = pos_frac.at({d}); + + if(ind.at(d).get() == 0u){ + weight = weight * (saw::data{1} - t); + }else{ + weight = weight * t; + } + } + + }, {}, ones_ind); + + /// TODO I need to actually calc stuff + return field.at({}); +} + +template +saw::data> bilinear_interpolate(const saw::data& field, const saw::data>& pos){ + saw::data> res; + + { + } + return {}; +} +} +} diff --git a/modules/core/c++/math/round.hpp b/modules/core/c++/math/round.hpp new file mode 100644 index 0000000..d3a2586 --- /dev/null +++ b/modules/core/c++/math/round.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "../common.hpp" + +namespace kel { +namespace lbm { + +template +saw::data> round_to_unsigned(const saw::data>& inp){ + saw::data> rv; + + auto zero = static_cast::type>(0); + auto half = static_cast::type>(0.5); + + for(uint64_t i{0u}; i < D; ++i){ + auto val = inp.at({{i}}).get()+half; + val = std::max(zero,val); + + rv.at({i}).set(static_cast(val)); + } + + return rv; +} + +} +} diff --git a/modules/core/c++/memory.hpp b/modules/core/c++/memory.hpp new file mode 100644 index 0000000..e97c7fc --- /dev/null +++ b/modules/core/c++/memory.hpp @@ -0,0 +1,82 @@ +#pragma once + +#include "common.hpp" +#include "chunk.hpp" + +namespace kel { +namespace lbm { + +namespace impl { + +template +struct memory_size_helper; + +template +struct memory_size_helper> { + static constexpr uint64_t bytes = N; +}; + +template +struct memory_size_helper> { + static constexpr uint64_t bytes = saw::ct_multiply::value * memory_size_helper::bytes; +}; + +template +struct memory_size_helper> { + static constexpr uint64_t bytes = saw::ct_multiply::value * memory_size_helper::bytes; +}; + +template +struct memory_size_helper> { + static constexpr uint64_t bytes = memory_size_helper::InnerSchema>::bytes; +}; + +template +struct memory_size_helper> { + + template + static constexpr uint64_t apply_i() { + if constexpr ( i < sizeof...(Members) ){ + using M_I = typename saw::parameter_pack_type::type; + return apply_i() + memory_size_helper::bytes; + } + return 0u; + } + + static constexpr uint64_t bytes = apply_i<0u>(); +}; + +template +class memory_estimate_helper final { +private: + + template + static void apply_i(saw::data& bytes){ + + if constexpr ( i < sizeof...(T)){ + using T_I = typename saw::parameter_pack_type::type; + + bytes.set(bytes.get() + memory_size_helper::bytes); + + apply_i(bytes); + } + } + +public: + + static void apply(saw::data& bytes){ + apply_i<0u>(bytes); + } +}; +} + +template +saw::data memory_estimate(){ + saw::data bytes; + + impl::memory_estimate_helper::apply(bytes); + + return bytes; +} +} +} diff --git a/modules/core/c++/momentum_gather.hpp b/modules/core/c++/momentum_gather.hpp new file mode 100644 index 0000000..7b1fc96 --- /dev/null +++ b/modules/core/c++/momentum_gather.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include "common.hpp" +#include "macroscopic.hpp" +#include "component.hpp" +#include "equilibrium.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +// Gather the force, Luke! +struct ForceGather {}; +} + +template +class component { +private: +public: + template + void apply(const saw::data& field, const saw::data& macros, + saw::data> index, saw::data time_step) const { + + using dfi = df_info; + bool is_even = ((time_step.get() % 2) == 0); + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + + auto& porous_f = macros.template get<"porosity">(); + auto& porous = porous_f.at(index); + + auto& force_f = macros.template get<"force">(); + auto& force = force_f.at(index); + + for(uint64_t k{0u}; k < Descriptor::D; ++k){ + force.at({{k}}).set(0); + } + + auto& dfs = dfs_old_f.at(index); + for(uint64_t i{0u}; i < Descriptor::Q; ++i){ + uint64_t i_opp = dfi::opposite_index[i]; + auto dfs_diff = dfs.at({i}) - dfs.at({i_opp}); + for(uint64_t k{0u}; k < Descriptor::D; ++k){ + force.at({{k}}) = force.at({{k}}) + dfs_diff * saw::data{dfi::directions[i][k]}; + } + } + + saw::data> one; + one.at({}) = 1.0; + auto flip_porous = one - porous; + force = force * flip_porous; + } +}; +} +} diff --git a/modules/core/c++/particle.hpp b/modules/core/c++/particle.hpp new file mode 100644 index 0000000..691a74b --- /dev/null +++ b/modules/core/c++/particle.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "component.hpp" +#include "particle/particle.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +struct Particle {}; +} + +template +class component { +private: + saw::data> dt_; +public: + component(saw::data> dt__): + dt_{dt__} + {} + + template + void apply(const saw::data& particles, const saw::data& macros, saw::data index, saw::data time_step) const { + + auto& p = particles.at(index); + + // Compute forces + + // Update particle velocity + verlet_step_lambda(p,{1.0}); + + // Update porosity over lattice nodes + + + } +}; +} +} diff --git a/modules/core/c++/particle/aabb.hpp b/modules/core/c++/particle/aabb.hpp new file mode 100644 index 0000000..1773dea --- /dev/null +++ b/modules/core/c++/particle/aabb.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include "common.hpp" +#include "schema.hpp" + +namespace kel { +namespace lbm { +template +class particle_aabb final { + static_assert(saw::always_false, "Not supported"); +}; + +template +class particle_aabb< + sch::ParticleGroup> +> final { +public: + using Schema = sch::ParticleGroup>; + + using AABB = sch::Struct< + sch::Member,"a">, + sch::Member,"b"> + >; +public: + template + static constexpr saw::data calculate(const saw::data& p_grp, const saw::data>& i, const saw::data>& meta){ + + saw::data aabb; + auto& parts = p_grp.template get<"particles">(); + + auto& pi = parts.at(i); + auto& pirb = pi.template get<"rigid_body">(); + auto& pirb_pos = pirb.template get<"position">(); + + auto& a = aabb.template get<"a">(); + auto& b = aabb.template get<"b">(); + + const saw::data>& rad_d = p_grp.template get<"collision">().template get<"radius">().at({0u}); + + saw::data> lower; + saw::data> upper; + + for(uint64_t i{0u}; i < D; ++i){ + lower.at({{i}}) = pirb_pos.at({{i}}) >= rad_d.at({}) ? (pirb_pos.at({{i}}) - rad_d.at({})) : saw::data{0}; + a.at({i}) = lower.at({{i}}).template cast_to(); + upper.at({{i}}) = pirb_pos.at({{i}}) + rad_d.at({}); + b.at({i}) = (upper.at({{i}})+saw::data{1}).template cast_to(); + } + + return aabb; + + } +}; +} +} diff --git a/modules/core/c++/particle/blur.hpp b/modules/core/c++/particle/blur.hpp new file mode 100644 index 0000000..b7a1988 --- /dev/null +++ b/modules/core/c++/particle/blur.hpp @@ -0,0 +1,37 @@ +#pragma once + +namespace kel { +namespace lbm { +template +void blur_mask(saw::data>& p_mask){ + + saw::data mid{2.0/3.0}; + saw::data edge{1.0/6.0}; + + // saw::data,2u> blur_weights{{2.0/3.0},{1.0/6.0}}; + + auto meta = p_mask.dims(); + saw::data> blurred_mask{meta}; + + /* 1D blur into N-D Blur*/ + for(saw::data i{0u}; i < saw::data{D}; ++i){ + iterator::apply([&](const auto& index){ + blurred_mask.at(index) = p_mask.at(index) * mid; + + if(index.at({i}).get() > 0u){ + auto l_ind = index; + l_ind.at({i}) = ind.at({i}) - 1u; + blurred_mask.at(index) = blurred_mask.at(index) + p_mask.at(l_ind) * edge; + } + if((index.at({i}).get() + 1u) < meta.at({i})){ + auto r_ind = index; + r_ind.at({i}) = ind.at({i}) + 1u; + blurred_mask.at(index) = blurred_mask.at(index) + p_mask.at(r_ind) * edge; + } + },{},meta); + + p_mask = blurred_mask; + } +} +} +} diff --git a/modules/core/c++/particle/common.hpp b/modules/core/c++/particle/common.hpp new file mode 100644 index 0000000..9e673c2 --- /dev/null +++ b/modules/core/c++/particle/common.hpp @@ -0,0 +1,3 @@ +#pragma once + +#include "../common.hpp" diff --git a/modules/core/c++/particle/geometry/circle.hpp b/modules/core/c++/particle/geometry/circle.hpp new file mode 100644 index 0000000..cf9b87e --- /dev/null +++ b/modules/core/c++/particle/geometry/circle.hpp @@ -0,0 +1,85 @@ +#pragma once + +#include "../particle.hpp" + +namespace kel { +namespace lbm { + +template +class particle_circle_geometry { +private: +public: + particle_circle_geometry() + {} + + template + saw::data> generate_mask(uint64_t resolution, uint64_t boundary_nodes = 0) const { + saw::data> mask; + + auto& grid = mask.template get<"grid">(); + auto& com = mask.template get<"center_of_mass">(); + com = {}; + + //uint64_t rad_i = static_cast(resolution * radius_.get())+1u; + uint64_t diameter_i = resolution; + uint64_t size = diameter_i + 2*boundary_nodes; + grid = {{{size,size}}}; + + saw::data delta_x{static_cast::type>(2.0 / static_cast(diameter_i))}; + saw::data center = (saw::data{1.0} + saw::data{2.0} * saw::data{static_cast::type>(boundary_nodes)/diameter_i}); + + saw::data> boundary_arr; + for(uint64_t i = 0u; i < D; ++i){ + boundary_arr.set(boundary_nodes); + } + + iterator::apply([&](const auto& index){ + grid.at(index).set(0); + }, {}, grid.dims()); + + iterator::apply([&](const auto& index){ + saw::data> f_ind; + for(uint64_t i = 0u; i < D; ++i){ + f_ind.at({{i}}) = (index.at({{i}}) + 0.5) * delta_x - center; + } + + auto norm_f_ind = saw::math::norm(f_ind); + }, {}, grid.dims(),boundary_arr); + + for(uint64_t i = 0; i < size; ++i){ + for(uint64_t j = 0; j < size; ++j){ + grid.at({{i,j}}).set(0); + if(i >= boundary_nodes and j >= boundary_nodes and i < (diameter_i + boundary_nodes) and j < (diameter_i + boundary_nodes) ){ + saw::data fi = saw::data{static_cast::type>(0.5+i)} * delta_x - center; + saw::data fj = saw::data{static_cast::type>(0.5+j)} * delta_x - center; + + auto norm_f_ij = fi*fi + fj*fj; + if(norm_f_ij.get() <= 1){ + grid.at({{i,j}}).set(1); + } + } + } + } + + // I don't fully remember what this did + saw::data total_mass{}; + iterator::apply::apply([&](const saw::data>& index){ + auto ind_vec = saw::math::vectorize_data(index).template cast_to(); + for(uint64_t i{0u}; i < D; ++i){ + ind_vec.at({{i}}) = ind_vec.at({{i}}) * grid.at(index); + } + com = com + ind_vec; + + total_mass = total_mass + grid.at(index); + },{}, grid.dims()); + + for(uint64_t i{0u}; i < D; ++i){ + com.at({{i}}) = com.at({{i}}) / total_mass; + } + + return mask; + } +}; + +} +} diff --git a/modules/core/c++/particle/geometry/cube.hpp b/modules/core/c++/particle/geometry/cube.hpp new file mode 100644 index 0000000..6392de8 --- /dev/null +++ b/modules/core/c++/particle/geometry/cube.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "../particle.hpp" + +namespace kel { +namespace lbm { +template +class particle_cubic_geometry { +private: +public: + template + saw::data> generate_mask(uint64_t resolution, uint64_t bound_nodes = 0u){ + + saw::data> mask; + + auto& grid = mask.template get<"grid">(); + auto& com = mask.template get<"center_of_mass">(); + + + } +} +} diff --git a/modules/core/c++/particle/particle.hpp b/modules/core/c++/particle/particle.hpp new file mode 100644 index 0000000..8e75e5a --- /dev/null +++ b/modules/core/c++/particle/particle.hpp @@ -0,0 +1,246 @@ +#pragma once + +#include +#include +#include + +#include "../iterator.hpp" + +#include "schema.hpp" +#include "aabb.hpp" +#include "particle_opa.hpp" + +namespace kel { +namespace lbm { + +template +saw::data>> create_spheroid_particle_group( + saw::data> radius_p, + saw::data> density_p, + const saw::data& mask_resolution +){ + saw::data>> part; + + auto& rad_s = part.template get<"collision">().at({0u}).template get<"radius">(); + rad_s = radius_p; + + auto& mask = part.template get<"mask">(); + auto& density = part.template get<"density">().at({{0u}}); + + auto& total_mass = part.template get<"total_mass">().at({{0u}}); + // Paranoia + total_mass.at({}) = {}; + + static_assert(D >= 1u and D <= 3u, "Dimensions only supported for Dim 1,2 & 3."); + density = density_p; + + saw::data> mask_dims; + for(uint64_t i = 0u; i < D; ++i){ + mask_dims.at({i}) = mask_resolution; + } + saw::data rad_d = radius_p.at({}); + saw::data dia_d = rad_d * 2; + + mask = {mask_dims}; + + auto& mask_step = part.template get<"mask_step">().at({{0u}}); + mask_step.at({}) = dia_d / mask_resolution.template cast_to(); + + auto& com = part.template get<"center_of_mass">().at({{0u}}); + // Paranoia + for(uint64_t i = 0u; i < D; ++i){ + com.at({{i}}) = {}; + } + saw::data ele_ctr{0u}; + + // Radius ^ 2 + saw::data> rad_2_d; + rad_2_d.at({}) = rad_d * rad_d; + + saw::data> center; + for(uint64_t i = 0u; i < D; ++i){ + center.at({{i}}) = rad_d; + } + + iterator::apply([&](const auto& index){ + ++ele_ctr; + + saw::data> offset_index = saw::math::vectorize_data(index).template cast_to() - center; + + auto& dpi = mask.at(index); + + for(uint64_t i = 0u; i < D; ++i){ + com.at({{i}}) = com.at({{i}}) + index.at({i}).template cast_to() * dpi; + } + + total_mass.at({}) = total_mass.at({}) + dpi; + + },{},mask_dims); + + for(uint64_t i = 0u; i < D; ++i){ + com.at({{i}}) = com.at({{i}}) / total_mass.at({}); + } + return part; +} + +/* +template +saw::data>> create_spheroid_particle( + saw::data> pos_p, + saw::data> vec_p, + saw::data> acc_p, + saw::data> rot_pos_p, + saw::data> rot_vel_p, + saw::data> rot_acc_p, + saw::data> rad_p, + saw::data> density_p, + saw::data> dt + ){ + + saw::data> part; + auto& body = part.template get<"rigid_body">(); + auto& mass = part.template get<"mass">(); + + auto& pos = body.template get<"position">(); + auto& pos_old = body.template get<"position_old">(); + auto& acc = body.template get<"acceleration">(); + + auto& rot = body.template get<"rotation">(); + auto& rot_old = body.template get<"rotation_old">(); + + auto& coll = part.template get<"collision">(); + auto& rad = coll.template get<"radius">(); + + pos = pos_p; + pos_old = pos - vec_p * dt; + acc = acc_p; + rad = rad_p; + + if constexpr ( D == 1u){ + saw::data> c; + c.at({}).set(2.0); + mass = rad_p * c * density_p; + } else if constexpr ( D == 2u){ + saw::data> pi; + pi.at({}).set(3.14159); + mass = rad_p * rad_p * pi * density_p; + } else if constexpr ( D == 3u ){ + saw::data> c; + c.at({}).set(3.14159 * 4.0 / 3.0); + mass = rad_p * rad_p * rad_p * c * density_p; + } else { + static_assert(D == 0u or D > 3u, "Dimensions only supported for Dim 1,2 & 3."); + } + + return part; +} +*/ + +template +constexpr auto verlet_step_lambda = [](saw::data>& particle, saw::data> time_step_delta){ + auto& body = particle.template get<"rigid_body">(); + + auto& pos = body.template get<"position">(); + auto& pos_old = body.template get<"position_old">(); + + auto& pos_acc = body.template get<"acceleration">(); + + auto& rot = body.template get<"rotation">(); + auto& rot_old = body.template get<"rotation_old">(); + + auto& rot_acc = body.template get<"angular_acceleration">(); + + auto tsd_squared = time_step_delta * time_step_delta; + + saw::data> pos_new; + // Actual step + saw::data> two; + two.at({}).set(2.0); + pos_new = pos * two - pos_old + pos_acc * tsd_squared; + + // Angular + saw::data::Schema> rot_new; + rot_new = rot * two - rot_old + rot_acc * tsd_squared; + + // Swap - Could be std::swap? + pos_old = pos; + pos = pos_new; + + rot_old = rot; + rot = rot_new; +}; + +template +constexpr auto handle_collision = []( + saw::data>& left, const saw::data>& mass_l, + saw::data>& right, const saw::data>& mass_r, + saw::data> unit_pos_rel, saw::data> vel_rel, + saw::data> d_t +){ + auto& rb_l = left.template get<"rigid_body">(); + auto& pos_l = rb_l.template get<"position">(); + auto& pos_old_l = rb_l.template get<"position_old">(); + auto vel_l = (pos_l-pos_old_l)/d_t; + + auto& rb_r = right.template get<"rigid_body">(); + auto& pos_r = rb_r.template get<"position">(); + auto& pos_old_r = rb_r.template get<"position_old">(); + auto vel_r = (pos_r-pos_old_r)/d_t; + + auto vel_pos_rel_dot = saw::math::dot(unit_pos_rel,vel_rel); + + if( vel_pos_rel_dot.at({{0u}}).get() < 0.0 ){ + pos_l = pos_l + vel_rel * unit_pos_rel * d_t; + pos_r = pos_r - vel_rel * unit_pos_rel * d_t; + } +}; + + +template +constexpr auto broadphase_collision_distance_squared = []( + saw::data>& left, + const saw::data& coll_l, + saw::data>& right, + const saw::data& coll_r +) -> std::pair>>{ + + auto rad_l = coll_l.template get<"radius">(); + auto rad_r = coll_r.template get<"radius">(); + + auto& rb_l = left.template get<"rigid_body">(); + auto& rb_r = right.template get<"rigid_body">(); + + auto& pos_l = rb_l.template get<"position">(); + auto& pos_r = rb_r.template get<"position">(); + + auto pos_dist = pos_l - pos_r; + + auto norm_2 = saw::math::dot(pos_dist,pos_dist); + + saw::data> two; + two.at({}) = 2.0; + auto rad_ab_2 = rad_l * rad_l + rad_r * rad_r + rad_r * rad_l * two; + + return std::make_pair((norm_2.at({}).get() < rad_ab_2.at({}).get()), norm_2); +}; +/** +* +* +*/ +template +constexpr auto broadphase_collision_check = []( + saw::data>& left, + const saw::data& coll_l, + saw::data>& right, + const saw::data& coll_r +) -> bool{ + return broadphase_collision_distance_squared(left,coll_l,right,coll_r).first; +}; + + + +namespace impl { +} + +} +} diff --git a/modules/core/c++/particle/particle_opa.hpp b/modules/core/c++/particle/particle_opa.hpp new file mode 100644 index 0000000..a5e6c28 --- /dev/null +++ b/modules/core/c++/particle/particle_opa.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include "../component.hpp" +#include "common.hpp" +#include "schema.hpp" +#include "porosity.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +struct OneParticleAt {}; +} + +template + +class component final { +private: + saw::data> pos_; + saw::data> rad_; + saw::data> eps_; +public: + component( + const saw::data> pos__, + const saw::data> rad__, + const saw::data> eps__ + ): + pos_{pos__}, + rad_{rad__}, + eps_{eps__} + {} + + template + void apply(const saw::data& macros, const saw::data> index, saw::data time_step) const { + using dfi = df_info; + + auto& porous_f = macros.template get<"porosity">(); + + auto& porous = porous_f.at(index); + + auto pos_ind = saw::math::vectorize_data(index); + + // Write out value + auto diff = pos_ind.template cast_to() - pos_; + auto diff_dot = saw::math::dot(diff,diff); + porous = particle_porosity>::calculate(diff, rad_, eps_); + } +}; +} +} diff --git a/modules/core/c++/particle/porosity.hpp b/modules/core/c++/particle/porosity.hpp new file mode 100644 index 0000000..2f39d2e --- /dev/null +++ b/modules/core/c++/particle/porosity.hpp @@ -0,0 +1,129 @@ +#pragma once + +#include "common.hpp" +#include "schema.hpp" +#include "../math/n_closest.hpp" + +namespace kel { +namespace lbm { +namespace por { +template +struct ParticleSpheroid {}; +} + +template +class particle_porosity { +public: + + static saw::data> calculate(const saw::data>& part_group, uint64_t p_i, const saw::data>& lbm_pos){ + auto& mask = part_group.template get<"mask">(); + + auto& particles = part_group.template get<"particles">(); + auto& part_i = particles.at({p_i}); + + auto& part_i_rb = part_i.template get<"rigid_body">(); + auto& pirb = part_i_rb.template get<"position">(); + + auto dist = lbm_pos - pirb; + + // index 0 is at + + return {}; + } +}; + +template +class particle_porosity> final { +public: + static saw::data> calculate(const saw::data>& lbm_rel_dist, saw::data> rad, saw::data> eps){ + saw::data> por; + + auto s_dist_2 = saw::math::dot(lbm_rel_dist,lbm_rel_dist); + auto s_dist = saw::math::sqrt(s_dist_2); + + saw::data> eps_h; + eps_h.at({}) = eps.at({}).get() / 2; + + auto rad_low = rad - eps_h; + auto rad_high = rad + eps_h; + + if(s_dist.at({}) <= rad_low.at({})){ + por.at({}).set(0); + return por; + } + if(s_dist.at({}) >= rad_high.at({})){ + por.at({}).set(1); + return por; + } + + { + typename saw::native_data_type::type inner = (std::numbers::pi / ( 2 * eps.at({}).get() )) * (s_dist - rad_low).at({}).get(); + auto cos_inner = std::sin(inner); + auto cos_i_2 = cos_inner * cos_inner; + por.at({}).set(cos_i_2); + } + + return por; + } +}; + + +template +class particle_porosity> final { +public: + static saw::data> calculate(const saw::data > >& part_group, uint64_t i, const saw::data>& lbm_pos) { + saw::data> por; + + auto& parts = part_group.template get<"particles">(); + auto& pi = parts.at({i}); + auto& pi_rb = pi.template get<"rigid_body">(); + + auto& pi_rb_pos = pi_rb.template get<"position">(); + + // Basically the queried position minus the center of the particle + auto dist = lbm_pos - pi_rb_pos; + + saw::data> dist_len = saw::math::dot(dist,dist); + dist_len.at({}).set(std::sqrt(dist_len.at({}).get())); + + auto& coll = part_group.template get<"collision">(); + const saw::data>& rad_d = coll.template get<"radius">(); + const auto& eps = part_group.template get<"epsilon">(); + // Move this somewhere + + saw::data> eps_h; + eps_h.at({}) = eps.at({}).get() / 2; + + saw::data> rad_d_eps_p = rad_d + eps_h; + saw::data> rad_d_eps_n = rad_d - eps_h; + + // saw::data> rad_d_eps_p_2 = rad_d_eps_p * rad_d_eps_p; + // saw::data> rad_d_eps_n_2 = rad_d_eps_n * rad_d_eps_n; + // saw::data> rad_2; + // rad_2 = rad_d * rad_d; + + saw::data> inside; + if(dist_len.at({}) <= rad_d_eps_n.at({})){ + inside.at({}).set(0); + return inside; + } + if(dist_len.at({}) > rad_d_eps_p.at({})){ + inside.at({}).set(1); + return inside; + } + + /* + * cos^2 ( ||x-X(t)||_2 - (R-eps/2) ) + */ + { + typename saw::native_data_type::type inner = (std::numbers::pi / (eps)) * (dist_len - rad_d_eps_n).at({}).get(); + auto cos_inner = std::cos(inner); + auto cos_i_2 = cos_inner * cos_inner; + inside.at({}).set(cos_i_2); + } + return inside; + } +}; + +} +} diff --git a/modules/core/c++/particle/schema.hpp b/modules/core/c++/particle/schema.hpp new file mode 100644 index 0000000..714a16f --- /dev/null +++ b/modules/core/c++/particle/schema.hpp @@ -0,0 +1,68 @@ +#pragma once + +#include "common.hpp" + +namespace kel { +namespace lbm { + +namespace coll { +template +struct Spheroid { + using ValueSchema = T; + using Schema = sch::Struct< + sch::Member,"radius"> + >; +}; +} + +namespace sch { +using namespace saw::schema; + +namespace impl { +template +struct rotation_type_helper; + +template +struct rotation_type_helper { + using Schema = Scalar; +}; + +template +struct rotation_type_helper { + using Schema = Vector; +}; +} + +template +using ParticleRigidBody = Struct< + Member, "position">, + Member, "position_old">, + Member::Schema, "rotation">, + Member::Schema, "rotation_old">, + + Member, "acceleration">, + Member::Schema, "angular_acceleration"> +>; + + +template +using Particle = Struct< + Member, "rigid_body"> + // Problem is that dynamic data would two layered + // Member, "mask">, +>; + +template> +using ParticleGroup = Struct< + Member, "mask">, + Member, "collision">, + Member,1u>, "epsilon">, + Member,1u>, "mask_step">, + Member,1u>, "density">, + Member,1u>, "center_of_mass">, + Member,1u>, "total_mass">, + Member,1u>, "particles"> +>; +} +} +} diff --git a/modules/core/c++/psm.hpp b/modules/core/c++/psm.hpp new file mode 100644 index 0000000..6dc146f --- /dev/null +++ b/modules/core/c++/psm.hpp @@ -0,0 +1,83 @@ +#pragma once + +#include "macroscopic.hpp" +#include "component.hpp" +#include "equilibrium.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +struct PSM {}; +} + +/** + * PSM collision operator for LBM + */ +template +class component { +private: + saw::data relaxation_; + saw::data frequency_; +public: + component( + typename saw::native_data_type::type relaxation__ + ): + relaxation_{relaxation__} + { + saw::data one; + one = 1.0; + frequency_ = one / relaxation_; + } + + template + void apply(const saw::data& field, const saw::data& macros, saw::data> index, saw::data time_step) const { + + using dfi = df_info; + bool is_even = ((time_step.get() % 2) == 0); + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + auto& porous_f = macros.template get<"porosity">(); + + auto& rho_f = macros.template get<"density">(); + auto& vel_f = macros.template get<"velocity">(); + + saw::data>& rho = rho_f.at(index); + saw::data>& vel = vel_f.at(index); + + compute_rho_u(dfs_old_f.at(index),rho,vel); + + auto eq = equilibrium(rho,vel); + + saw::data one{1.0}; + auto& porous = porous_f.at(index); + auto flip_porous = one - porous.at({}); + + auto& dfs = dfs_old_f.at(index); + + auto dfs_cpy = dfs; + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + uint64_t i_opp = dfi::opposite_index[i]; + dfs.at({i}) = dfs_cpy.at({i}) + frequency_ * (eq.at(i) - dfs_cpy.at({i})) * porous.at({}) + (dfs_cpy.at({i_opp}) - dfs_cpy.at({i}) ) * flip_porous; + } + + auto& force_f = macros.template get<"force">(); + auto& force = force_f.at(index); + for(uint64_t k{0u}; k < Descriptor::D; ++k){ + force.at({{k}}).set(0); + } + + for(uint64_t i{0u}; i < Descriptor::Q; ++i){ + uint64_t i_opp = dfi::opposite_index[i]; + auto dfs_diff = dfs.at({i}) - dfs.at({i_opp}); + for(uint64_t k{0u}; k < Descriptor::D; ++k){ + force.at({{k}}) = force.at({{k}}) + dfs_diff; + } + } + + force = force * porous; + } +}; + +} +} diff --git a/modules/core/c++/rar.hpp b/modules/core/c++/rar.hpp new file mode 100644 index 0000000..dc2b61c --- /dev/null +++ b/modules/core/c++/rar.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "common.hpp" + +namespace kel { +namespace lbm { +saw::error_or run_and_record(int argc, char** argv){ + using RarGit = Struct< + Member, + Member + >; + using RarCommand = Struct< + Member, + Member, "arguments"> + >; + using Rar = Struct< + Member, "commands">, + Member, + Member + >; + + return saw::make_void(); +} +} +} diff --git a/modules/core/c++/schema.hpp b/modules/core/c++/schema.hpp new file mode 100644 index 0000000..7712f99 --- /dev/null +++ b/modules/core/c++/schema.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace kel { +namespace lbm { +namespace sch { +using namespace saw::schema; +} +} +} diff --git a/modules/core/c++/simulation/common.hpp b/modules/core/c++/simulation/common.hpp new file mode 100644 index 0000000..f675a99 --- /dev/null +++ b/modules/core/c++/simulation/common.hpp @@ -0,0 +1,7 @@ +#pragma once + +namespace kel { +namespace lbm { + +} +} diff --git a/modules/core/c++/simulation/hlbm.hpp b/modules/core/c++/simulation/hlbm.hpp new file mode 100644 index 0000000..93df9a7 --- /dev/null +++ b/modules/core/c++/simulation/hlbm.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "common.hpp" + +namespace kel { +namespace lbm { + +class simulation {}; + + +} +} diff --git a/modules/core/c++/statistics.hpp b/modules/core/c++/statistics.hpp new file mode 100644 index 0000000..c07ccb7 --- /dev/null +++ b/modules/core/c++/statistics.hpp @@ -0,0 +1,11 @@ +#pragma once + +namespace kel { +namespace lbm { +template +class statistics { +private: +public: +}; +} +} diff --git a/modules/core/c++/stream.hpp b/modules/core/c++/stream.hpp new file mode 100644 index 0000000..dc7cfb3 --- /dev/null +++ b/modules/core/c++/stream.hpp @@ -0,0 +1,61 @@ +#pragma once + +#include "component.hpp" + +namespace kel { +namespace lbm { +namespace cmpt { +struct Stream {}; +} + +template +class component final { +private: +public: + static constexpr saw::string_literal name = "streaming"; + static constexpr saw::string_literal after = "collide"; + static constexpr saw::string_literal before = ""; + + template + void apply(const saw::data& field, saw::data> index, saw::data time_step) const { + using dfi = df_info; + auto g_index = index; + + for(uint64_t i = 0u; i < Descriptor::D; ++i){ + ++g_index.at({{i}}); + } + + bool is_even = ((time_step.get() % 2) == 0); + + auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">(); + auto& dfs_new_f = (is_even) ? field.template get<"dfs">() : field.template get<"dfs_old">(); + auto info_f = field.template get<"info">(); + + auto info_meta = info_f.get_dims(); + + /* + bool border = false; + for(uint64_t i = 0u; i < Descriptor::D; ++i){ + auto ind_i = index.at({i}); + border |= (ind_i.get()) == 0u or (ind_i == info_meta.at({i})); + } + + if (not border){ + } + */ + + auto& dfs_new = dfs_new_f.ghost_at(g_index); + + for(uint64_t i = 0u; i < Descriptor::Q; ++i){ + auto dir = dfi::directions[dfi::opposite_index[i]]; + auto g_index_nb = g_index; + for(uint64_t z = 0u; z < Descriptor::D; ++z){ + g_index_nb.at({z}).set(g_index.at({z}).get() + dir[z]); + } + + dfs_new.at({i}) = dfs_old_f.ghost_at(g_index_nb).at({i}); + } + } +}; +} +} diff --git a/modules/core/c++/util.hpp b/modules/core/c++/util.hpp new file mode 100644 index 0000000..0bdebd1 --- /dev/null +++ b/modules/core/c++/util.hpp @@ -0,0 +1,93 @@ +#pragma once + +#include +#include + +#include +#include +#include + +namespace kel { +namespace lbm { +/* +template +struct is_neighbour { + static bool operator()(saw::data& latt, saw::data& index) { + using dfi = df_info; + + if(index.at({0u}).get() == 0u or index.at({1u}).get() == 0u){ + return false; + } + + for(saw::data k{0u}; k.get() < Descriptor::Q; ++k){ + // TODO + saw::data> + } + + auto& cell = latt(index); + } +}; +*/ + +/* Might be stupidly complex +class cell_info_registry final { +public: + static uint8_t next_reg_id = 0u; + + template + static uint8_t get_registration_id() { + static uint8_t reg_id = std::numeric_limit::max(); + + if(reg_id == std::numeric_limit::max()){ + reg_id = next_reg_id; + ++next_reg_id; + } + + return reg_id; + } +}; +*/ + +void print_progress_bar(const uint32_t progress, const uint32_t progress_target){ + std::cout<<"\r"; + // <<"Progress: " + // <<((100 * progress) / progress_target) + // <<"% ["; + + const uint32_t progress_min = std::min(progress,progress_target); + constexpr uint64_t max_perc_progress = 100u; + uint64_t perc_prog = (max_perc_progress*progress_min) / progress_target; + + std::string_view prog_str{"Progress: "}; + // Progress string + (100 width and perc char) + ([] brackets) + space + pointer + uint64_t i{prog_str.size() + 4u + 2u + 1u + 1u}; + + std::cout< uint64_t{ + struct winsize w; + if(ioctl(STDOUT_FILENO, TIOCGWINSZ,&w) == -1){ + // Standardized Terminal size + return 80u; + } + return w.ws_col; + }(); + max_display = std::max(max_display,i) - i; + uint64_t progress_display = (max_display * progress_min) / progress_target; + + for(i = 0u; i < progress_display; ++i){ + std::cout<<"#"; + } + for(; i < max_display; ++i){ + std::cout<<"-"; + } + + std::cout<<"]"; + + std::cout< + +#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; +} + +} +} diff --git a/modules/core/c++/write_json.hpp b/modules/core/c++/write_json.hpp new file mode 100644 index 0000000..19bbfa6 --- /dev/null +++ b/modules/core/c++/write_json.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include +#include + +#include + + +namespace kel { +namespace lbm { + +template +saw::error_or write_json(const std::filesystem::path& file_name, const saw::data& field){ + saw::data j_data; + { + saw::codec j_codec; + auto eov = j_codec.encode(field, j_data); + if(eov.is_error()){ + return eov; + } + } + return saw::make_void(); +} +} +} diff --git a/modules/core/c++/write_vtk.hpp b/modules/core/c++/write_vtk.hpp new file mode 100644 index 0000000..e852172 --- /dev/null +++ b/modules/core/c++/write_vtk.hpp @@ -0,0 +1,256 @@ +#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_vtk_writer { +}; + +template +struct lbm_vtk_writer> { + static saw::error_or apply_header(std::ostream& vtk_file, std::string_view name){ + vtk_file<<"SCALARS "< apply(std::ostream& vtk_file, const saw::data>& field){ + if constexpr (std::is_same_v and D == 1u) { + vtk_file<().get()<<"\n"; + }else{ + vtk_file< +struct lbm_vtk_writer> { + static saw::error_or apply_header(std::ostream& vtk_file, std::string_view name){ + vtk_file<<"VECTORS "< apply(std::ostream& vtk_file, const saw::data>& field){ + saw::data> index; + for(saw::data it{0}; it.get() < D; ++it){ + index.at({0u}).set(0u); + } + + // vtk_file<<"VECTORS "< 0){ + vtk_file<<" "; + } + vtk_file< +struct lbm_vtk_writer> { + + template + static saw::error_or apply_d(std::ostream& vtk_file, const saw::data>& field, saw::data>& index){ + // VTK wants to iterate over z,y,x instead of x,y,z + // 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(vtk_file, field, index); + } + }else{ + auto eov = lbm_vtk_writer::apply(vtk_file, field.at(index)); + if(eov.is_error()) return eov; + } + return saw::make_void(); + } + + static saw::error_or apply(std::ostream& vtk_file, const saw::data>& field, std::string_view name){ + { + auto eov = lbm_vtk_writer::apply_header(vtk_file,name); + if(eov.is_error()){ + return eov; + } + } + saw::data> index; + for(saw::data it{0}; it.get() < sizeof...(D); ++it){ + index.at({0u}).set(0u); + } + + { + auto eov = apply_d(vtk_file, field, index); + if(eov.is_error()){ + return eov; + } + } + + vtk_file<<"\n"; + + return saw::make_void(); + } +}; + +template +struct lbm_vtk_writer> { + static saw::error_or apply_header(std::ostream& vtk_file, std::string_view name){ + vtk_file<<"VECTORS "< apply(std::ostream& vtk_file, const saw::data>& field){ + static_assert(D > 0, "Non-dimensionality is bad for velocity."); + static_assert(D <= 3, "4th dimension as well. Mostly due to vtk."); + + // vtk_file<<"VECTORS "< 0){ + vtk_file<<" "; + } + vtk_file< +struct lbm_vtk_writer> { + static saw::error_or apply_header(std::ostream& vtk_file, std::string_view name){ + vtk_file<<"SCALARS "< apply(std::ostream& vtk_file, const saw::data>& field){ + // vtk_file<<"VECTORS "< +struct lbm_vtk_writer,Keys>...>> final { + template + static saw::error_or iterate_i(std::ostream& vtk_file, + const saw::data,Keys>...>>& field){ + + if constexpr ( i < sizeof...(MemberT) ) { + using MT = typename saw::parameter_pack_type,Keys>...>::type; + { + auto eov = lbm_vtk_writer::apply(vtk_file,field.template get(), MT::KeyLiteral.view()); + if(eov.is_error()){ + return eov; + } + } + + return iterate_i(vtk_file, field); + } + + return saw::make_void(); + } + + + static saw::error_or apply(std::ostream& vtk_file, + const saw::data,Keys>...>>& field){ + + 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& field_0 = field.template get::literal>(); + auto meta = field_0.get_dims(); + + saw::data pd_size{1u}; + // DIMENSIONS + + { + vtk_file << "DIMENSIONS"; + pd_size.set(saw::ct_multiply::value); + + static_assert(saw::ct_multiply::value > 0u, "Invalid Dim size resulting in length 0u"); + + for(saw::data i{0u}; i.get() < sizeof...(Dims); ++i){ + vtk_file << " " << meta.at(i).get(); + } + for(saw::data i{sizeof...(Dims)}; i.get() < 3u; ++i){ + vtk_file << " 1"; + } + + vtk_file << "\n"; + } + if constexpr (sizeof...(MemberT) > 0u){ + // POINT DATA + { + vtk_file << "\nPOINT_DATA " << pd_size.get() <<"\n"; + } + + // HEADER TO BODY + { + vtk_file << "\n"; + } + } + + return iterate_i<0u>(vtk_file, field); + } +}; + +} + +template +saw::error_or write_vtk_file(const std::filesystem::path& out_dir, const std::string_view& file_name, uint64_t d_t, const saw::data& field){ + + auto vtk_dir = out_dir / "vtk"; + { + std::error_code ec; + std::filesystem::create_directories(vtk_dir,ec); + if(ec != std::errc{}){ + return saw::make_error("Could not create directory for write_vtk_file function"); + } + } + std::string ft_name{file_name}; + + std::stringstream sstr; + sstr + <("Could not open file."); + } + + auto eov = impl::lbm_vtk_writer::apply(vtk_file, field); + return eov; +} + +} +} -- cgit v1.2.3