diff options
| -rw-r--r-- | examples/poiseulle_3d/.nix/derivation.nix | 2 | ||||
| -rw-r--r-- | examples/poiseulle_3d/poiseulle_3d.cpp | 147 | ||||
| -rw-r--r-- | lib/c++/descriptor.hpp | 88 | ||||
| -rw-r--r-- | lib/c++/iterator.hpp | 36 | ||||
| -rw-r--r-- | lib/tests/descriptor.cpp | 4 | ||||
| -rw-r--r-- | lib/tests/iterator.cpp | 26 |
6 files changed, 262 insertions, 41 deletions
diff --git a/examples/poiseulle_3d/.nix/derivation.nix b/examples/poiseulle_3d/.nix/derivation.nix index a98e736..a06e14b 100644 --- a/examples/poiseulle_3d/.nix/derivation.nix +++ b/examples/poiseulle_3d/.nix/derivation.nix @@ -24,6 +24,8 @@ stdenv.mkDerivation { forstio.codec forstio.codec-unit forstio.codec-json + forstio.remote + forstio.remote-sycl kel-lbm ]; diff --git a/examples/poiseulle_3d/poiseulle_3d.cpp b/examples/poiseulle_3d/poiseulle_3d.cpp index 9e7bba7..68e1bb7 100644 --- a/examples/poiseulle_3d/poiseulle_3d.cpp +++ b/examples/poiseulle_3d/poiseulle_3d.cpp @@ -1,15 +1,160 @@ #include <kel/lbm/lbm.hpp> +#include <forstio/remote/sycl/sycl.hpp> + namespace kel { namespace lbm { namespace sch { +using namespace saw::schema; using T = Float32; -using D3Q27 = Descriptor<3u,27u>; +using Desc = Descriptor<3u,27u>; + +using DfCell = Cell<T, Desc, 0u, 0u, 1u>; + +using CellInfo = Cell<UInt8, Desc, 1u, 0u, 0u>; + +using CellStruct = Struct< + Member<DfCell, "dfs">, + Member<DfCell, "dfs_old">, + //Member<CellForceField<Desc>, "force">, + Member<CellInfo, "info"> +>; +} + +template<typename StructFieldSchema> +saw::error_or<void> set_geometry(saw::data<StructFieldSchema>& latt){ + using namespace kel::lbm; + + auto meta = latt.meta(); + + /** + * Set ghost + */ + iterator<sch::Desc::D>::apply([&](const saw::data<sch::FixedArray<sch::UInt64,sch::Desc::D>>& index){ + auto& cell = latt.at(index); + auto& info = cell.template get<"info">(); + + info({0u}).set(0u); + }, {{0u,0u}}, meta); + + saw::data<sch::FixedArray<sch::UInt64,sch::Desc::D>> distance; + + /** + * Set wall + */ + iterator<1u>::apply([&](const saw::data<sch::FixedArray<sch::UInt64,1u>>& index){ + distance.at(index.at({0u})) = {1u}; + },{{0u}},{{sch::Desc::D}}); + /* + */ + iterator<sch::Desc::D>::apply([&](const saw::data<sch::FixedArray<sch::UInt64,sch::Desc::D>>& index){ + auto& cell = latt.at(index); + auto& info = cell.template get<"info">(); + + info({0u}).set(2u); + }, {}, meta, distance); + + /** + * Set fluid + */ + iterator<1u>::apply([&](const saw::data<sch::FixedArray<sch::UInt64,1u>>& index){ + distance.at(index.at({0u})) = {2u}; + },{{0u}},{{sch::Desc::D}}); + /* + */ + iterator<sch::Desc::D>::apply([&](const saw::data<sch::FixedArray<sch::UInt64,sch::Desc::D>>& index){ + auto& cell = latt.at(index); + auto& info = cell.template get<"info">(); + + info({0u}).set(1u); + + }, {}, meta, distance); + + /** + * Set inflow + */ + + saw::data<sch::FixedArray<sch::UInt64,sch::Desc::D>> shifted_point; + iterator<1u>::apply([&](const saw::data<sch::FixedArray<sch::UInt64,1u>>& index){ + distance.at(index.at({0u})) = (index.at({0u}).get() == 0u)?1u:2u; + shifted_point.at(index.at({0u})) = (index.at({0u}).get() == 0u)?3u:meta.at({index.at({0u})}); + },{{0u}},{{sch::Desc::D}}); + /* + */ + iterator<sch::Desc::D>::apply([&](const saw::data<sch::FixedArray<sch::UInt64,sch::Desc::D>>& index){ + auto& cell = latt.at(index); + auto& info = cell.template get<"info">(); + + info({0u}).set(3u); + + }, {}, shifted_point, distance); + + /** + * Set outflow + */ + iterator<1u>::apply([&](const saw::data<sch::FixedArray<sch::UInt64,1u>>& index){ + distance.at(index.at({0u})) = (index.at({0u}).get() == 0u)?1u:2u; + shifted_point.at(index.at({0u})) = (index.at({0u}).get() == 0u)?(meta.at({0u})-3u):0u; + },{{0u}},{{sch::Desc::D}}); + /* + */ + iterator<sch::Desc::D>::apply([&](const saw::data<sch::FixedArray<sch::UInt64,sch::Desc::D>>& index){ + auto& cell = latt.at(index); + auto& info = cell.template get<"info">(); + info({0u}).set(4u); + }, shifted_point, meta, distance); + + return saw::make_void(); +} + +saw::error_or<void> set_initial_conditions(saw::data<StructFieldSchema>& latt){ + return saw::make_void(); } + +saw::error_or<void> lbm_step(saw::data<StructFieldSchema>& latt){ + return saw::make_void(); +} + saw::error_or<void> real_main(int argc, char** argv){ + using dfi = df_info<sch::T,sch::Desc>; + + converter<sch::T> conv { + {0.1f}, + {0.1f} + }; + print_lbm_meta<sch::T,sch::Desc>(conv, {1e-3f}); + + /** + * Set the meta and initialize the lattice + */ + saw::data<sch::FixedArray<sch::UInt64,sch::Desc::D>> meta{{2048u,128u,128u}}; + saw::data<sch::CellField<sch::Desc,sch::CellStruct>, encode::Sycl<encode::Native>> lattice{meta}; + + /** + * Set the geometry + */ + auto eov = set_geometry(lattice); + if(eov.is_error()){ + return eov; + } + + /** + * Set initial conditions in the simulations + */ + auto eov = set_initial_conditions(lattice); + if(eov.is_error()){ + return eov; + } + + for(uint64_t i = 0u; i < 1024u*32; ++i){ + auto eov = lbm_step(lattice); + if(eov.is_error()){ + return eov; + } + } return saw::make_void(); } diff --git a/lib/c++/descriptor.hpp b/lib/c++/descriptor.hpp index 18ea440..1e3402f 100644 --- a/lib/c++/descriptor.hpp +++ b/lib/c++/descriptor.hpp @@ -165,7 +165,7 @@ public: static constexpr typename saw::native_data_type<T>::type cs2 = 1./3.; }; -/* + template<typename T> class df_info<T,sch::Descriptor<3, 27>> { public: @@ -175,35 +175,35 @@ public: static constexpr uint64_t Q = 27u; static constexpr std::array<std::array<int32_t, D>, Q> directions = {{ - { 0, 0, 0}, - {-1, 0, 0}, - { 1, 0, 0}, + { 0, 0, 0}, // 0 + {-1, 0, 0}, // 1 + { 1, 0, 0}, // 2 // Expand into 2D - { 0, -1, 0}, - {-1, -1, 0}, - { 1, -1, 0}, - { 0, 1, 0}, - {-1, 1, 0}, - { 1, 1, 0}, + { 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}, - {-1, 0, -1}, - { 1, 0, -1}, - { 0, -1, -1}, - {-1, -1, -1}, - { 1, -1, -1}, - { 0, 1, -1}, - {-1, 1, -1}, - { 1, 1, -1}, - { 0, 0, 1}, - {-1, 0, 1}, - { 1, 0, 1}, - { 0, -1, 1}, - {-1, -1, 1}, - { 1, -1, 1}, - { 0, 1, 1}, - {-1, 1, 1}, - { 1, 1, 1} + { 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<typename saw::native_data_type<T>::type,Q> weights = { @@ -215,25 +215,37 @@ public: 1./36., 1./36., 1./36., + 1./36., + 8./27., + 1./9., + 1./9., + 1./9., + 1./9., + 1./36., + 1./36., + 1./36., + 1./36., + 8./27., + 1./9., + 1./9., + 1./9., + 1./9., + 1./36., + 1./36., + 1./36., 1./36. }; static constexpr std::array<uint64_t,Q> opposite_index = { - 0, - 2, - 1, - 4, - 3, - 6, - 5, - 8, - 7 + 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<T>::type inv_cs2 = 3.0; static constexpr typename saw::native_data_type<T>::type cs2 = 1./3.; }; -*/ + template<typename Schema> class cell_schema_builder { diff --git a/lib/c++/iterator.hpp b/lib/c++/iterator.hpp index 78babff..866543a 100644 --- a/lib/c++/iterator.hpp +++ b/lib/c++/iterator.hpp @@ -14,6 +14,42 @@ void iterate_over(Func&& func, const saw::data<sch::FixedArray<sch::UInt64,2u>>& } return; } + +/** + * + */ +template<uint64_t D> +struct iterator { +private: + template<uint64_t i, typename Func> + static void iterate_over_i(Func& func, + const saw::data<sch::FixedArray<sch::UInt64,D>>& start, + const saw::data<sch::FixedArray<sch::UInt64,D>>& end, + const saw::data<sch::FixedArray<sch::UInt64,D>>& dist, + saw::data<sch::FixedArray<sch::UInt64,D>>& 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<i+1u,Func>(func,start,end,dist,iter); + } + } + } +public: + template<typename Func> + static void apply( + Func&& func, + const saw::data<sch::FixedArray<sch::UInt64,D>>& start, + const saw::data<sch::FixedArray<sch::UInt64,D>>& end, + const saw::data<sch::FixedArray<sch::UInt64,D>>& dist = {} + ){ + saw::data<sch::FixedArray<sch::UInt64,D>> iter; + iterate_over_i<0u,Func>(func, start, end, dist, iter); + } +}; + /* Ambiguous template<typename Func> void iterate_over(Func&& func, const saw::data<sch::FixedArray<sch::UInt64,3u>>& start, const saw::data<sch::FixedArray<sch::UInt64,3u>>& end, const saw::data<sch::FixedArray<sch::UInt64,3u>>& dist = {{{0u,0u,0u}}}){ diff --git a/lib/tests/descriptor.cpp b/lib/tests/descriptor.cpp index a8337e6..7f743ce 100644 --- a/lib/tests/descriptor.cpp +++ b/lib/tests/descriptor.cpp @@ -32,4 +32,8 @@ SAW_TEST("Opposites and Dirs D2Q9"){ using namespace kel; check_opposite_dirs<lbm::sch::Descriptor<2,9>>(); } +SAW_TEST("Opposites and Dirs D3Q27"){ + using namespace kel; + check_opposite_dirs<lbm::sch::Descriptor<3,27>>(); +} } diff --git a/lib/tests/iterator.cpp b/lib/tests/iterator.cpp index cd1cb7c..261765a 100644 --- a/lib/tests/iterator.cpp +++ b/lib/tests/iterator.cpp @@ -9,7 +9,7 @@ namespace sch { using namespace kel::lbm::sch; } -SAW_TEST("Iterate"){ +SAW_TEST("Old Iterate"){ using namespace kel; saw::data<sch::FixedArray<sch::UInt64,2u>> start{{0u,0u}}; @@ -20,7 +20,7 @@ SAW_TEST("Iterate"){ }, start, end); } -SAW_TEST("Iterate with Distance 1"){ +SAW_TEST("Old Iterate with Distance 1"){ using namespace kel; saw::data<sch::FixedArray<sch::UInt64,2u>> start{{0u,0u}}; @@ -30,4 +30,26 @@ SAW_TEST("Iterate with Distance 1"){ std::cout<<"Index: "<<index.at({0u}).get()<<" "<<index.at({1u}).get()<<std::endl; }, start, end, {{1u,1u}}); } + +SAW_TEST("Iterate - 2D"){ + using namespace kel; + + saw::data<sch::FixedArray<sch::UInt64,2u>> start{{0u,0u}}; + saw::data<sch::FixedArray<sch::UInt64,2u>> end{{3u,3u}}; + + lbm::iterator<2u>::apply([](const saw::data<sch::FixedArray<sch::UInt64,2u>>& index){ + std::cout<<"Index: "<<index.at({0u}).get()<<" "<<index.at({1u}).get()<<std::endl; + }, start, end); +} + +SAW_TEST("Iterate with Distance 1 - 2D"){ + using namespace kel; + + saw::data<sch::FixedArray<sch::UInt64,2u>> start{{0u,0u}}; + saw::data<sch::FixedArray<sch::UInt64,2u>> end{{4u,4u}}; + + lbm::iterator<2u>::apply([](const saw::data<sch::FixedArray<sch::UInt64,2u>>& index){ + std::cout<<"Index: "<<index.at({0u}).get()<<" "<<index.at({1u}).get()<<std::endl; + }, start, end, {{1u,1u}}); +} } |
