From b40a5809aef31d5cf23947755b94156b96bd8f9f Mon Sep 17 00:00:00 2001 From: Emily Bourne Date: Fri, 21 Jun 2024 12:08:58 +0000 Subject: [PATCH] Templatization of functions to build GhostedVx and GhostedStaggeredVx meshes in collisions intra (geometryXVx) See merge request gysela-developpers/gyselalibxx!523 -------------------------------------------- --- src/geometryXVx/rhs/collisions_intra.cpp | 118 +++++++++++++++-------- src/geometryXVx/rhs/collisions_intra.hpp | 21 ++-- 2 files changed, 88 insertions(+), 51 deletions(-) diff --git a/src/geometryXVx/rhs/collisions_intra.cpp b/src/geometryXVx/rhs/collisions_intra.cpp index 98f151c2d..4f26de5e1 100644 --- a/src/geometryXVx/rhs/collisions_intra.cpp +++ b/src/geometryXVx/rhs/collisions_intra.cpp @@ -8,6 +8,80 @@ #include "collisions_intra.hpp" #include "collisions_utils.hpp" +template +KOKKOS_FUNCTION ddc::DiscreteElement CollisionsIntra::to_index( + ddc::DiscreteElement const& index) +{ + static_assert( + std::is_same_v || std::is_same_v); + if constexpr (std::is_same_v) { + return ddc::DiscreteElement(index.uid() + 1); + } else { + return ddc::DiscreteElement(index.uid() + 1); + } +} + +template +std::enable_if_t> CollisionsIntra:: + build_ghosted_staggered_vx_point_sampling(ddc::DiscreteDomain const& dom) +{ + static_assert( + std::is_same_v, + "The function is only designed to work with the IDimVx dimension"); + + CoordVx const v0 = ddc::coordinate(dom.front()); + CoordVx const v1 = ddc::coordinate(dom.front() + 1); + CoordVx const vN = ddc::coordinate(dom.back()); + CoordVx const vNm1 = ddc::coordinate(dom.back() - 1); + int const ncells(dom.size() - 1); + + // ghosted points + int const npoints(ncells + 3); + std::vector breaks(npoints); + breaks[0] = v0 - (v1 - v0); + breaks[npoints - 1] = vN + (vN - vNm1); + ddc::for_each(dom, [&](IndexVx const iv) { + breaks[to_index(iv).uid()] = ddc::coordinate(iv); + }); + ddc::init_discrete_space(breaks); + + // ghosted staggered points + int const npoints_stag(ncells + 2); + std::vector breaks_stag(npoints_stag); + breaks_stag[0] = v0 - (v1 - v0) / 2.; + breaks_stag[npoints_stag - 1] = vN + (vN - vNm1) / 2.; + IDomainVx const gridv_less(dom.remove_last(IVectVx(1))); + ddc::for_each(gridv_less, [&](IndexVx const iv) { + breaks_stag[iv.uid() + 1] = CoordVx((ddc::coordinate(iv) + ddc::coordinate(iv + 1)) / 2.); + }); + ddc::init_discrete_space(breaks_stag); +} + +template +std::enable_if_t> CollisionsIntra:: + build_ghosted_staggered_vx_point_sampling(ddc::DiscreteDomain const& dom) +{ + static_assert( + std::is_same_v, + "The function is only designed to work with the IDimVx dimension"); + + CoordVx const v0 = ddc::coordinate(dom.front()); + CoordVx const vN = ddc::coordinate(dom.back()); + int const ncells(dom.size() - 1); + double const step(ddc::step()); + + // ghosted points + ddc::init_discrete_space( + GhostedVx::init(v0 - step, vN + step, ddc::DiscreteVector(ncells + 3))); + + // ghosted staggered points + ddc::init_discrete_space( + GhostedVxStaggered:: + init(v0 - step / 2, + vN + step / 2, + ddc::DiscreteVector(ncells + 2))); +} + CollisionsIntra::CollisionsIntra(IDomainSpXVx const& mesh, double nustar0) : m_nustar0(nustar0) , m_fthresh(1.e-30) @@ -33,45 +107,7 @@ CollisionsIntra::CollisionsIntra(IDomainSpXVx const& mesh, double nustar0) throw std::invalid_argument("Collision operator should not be used with nustar0=0."); } - CoordVx const vx0 = ddc::coordinate(ddc::select(mesh).front()); - CoordVx const vx1 = ddc::coordinate(ddc::select(mesh).front() + 1); - CoordVx const vxN = ddc::coordinate(ddc::select(mesh).back()); - CoordVx const vxNm1 = ddc::coordinate(ddc::select(mesh).back() - 1); - int const ncells(ddc::select(mesh).size() - 1); - if constexpr (uniform_edge_v) { - double const step(ddc::step()); - ddc::init_discrete_space( - GhostedVx:: - init(vx0 - step, vxN + step, ddc::DiscreteVector(ncells + 3))); - } else { - int const npoints(ncells + 3); - std::vector breaks(npoints); - breaks[0] = vx0 - (vx1 - vx0); - breaks[npoints - 1] = vxN + (vxN - vxNm1); - ddc::for_each(ddc::select(mesh), [&](IndexVx const ivx) { - breaks[ghosted_from_index(ivx).uid()] = ddc::coordinate(ivx); - }); - ddc::init_discrete_space(breaks); - } - - if constexpr (uniform_edge_v) { - double const step(ddc::step()); - ddc::init_discrete_space( - GhostedVxStaggered:: - init(vx0 - step / 2, - vxN + step / 2, - ddc::DiscreteVector(ncells + 2))); - } else { - int const npoints(ncells + 2); - std::vector breaks(npoints); - breaks[0] = vx0 - (vx1 - vx0) / 2.; - breaks[npoints - 1] = vxN + (vxN - vxNm1) / 2.; - IDomainVx const gridvx_less(ddc::select(mesh).remove_last(IVectVx(1))); - ddc::for_each(gridvx_less, [&](IndexVx const ivx) { - breaks[ivx.uid() + 1] = CoordVx((ddc::coordinate(ivx) + ddc::coordinate(ivx + 1)) / 2.); - }); - ddc::init_discrete_space(breaks); - } + build_ghosted_staggered_vx_point_sampling(ddc::select(mesh)); m_nustar_profile = m_nustar_profile_alloc.span_view(); compute_nustar_profile(m_nustar_profile, m_nustar0); @@ -112,8 +148,8 @@ void CollisionsIntra::compute_matrix_coeff( IndexX const ix = ddc::select(ispxvx); IndexVx const ivx = ddc::select(ispxvx); - IndexVx_ghosted ivx_ghosted(ghosted_from_index(ivx)); - IndexVx_ghosted_staggered ivx_ghosted_staggered(ghosted_staggered_from_index(ivx)); + IndexVx_ghosted ivx_ghosted(to_index(ivx)); + IndexVx_ghosted_staggered ivx_ghosted_staggered(to_index(ivx)); IndexVx_ghosted ivx_next_ghosted(ivx_ghosted + 1); IndexVx_ghosted ivx_prev_ghosted(ivx_ghosted - 1); IndexVx_ghosted_staggered ivx_prev_ghosted_staggered(ivx_ghosted_staggered - 1); diff --git a/src/geometryXVx/rhs/collisions_intra.hpp b/src/geometryXVx/rhs/collisions_intra.hpp index 6dc11ad31..dc2f366e9 100644 --- a/src/geometryXVx/rhs/collisions_intra.hpp +++ b/src/geometryXVx/rhs/collisions_intra.hpp @@ -95,17 +95,18 @@ class CollisionsIntra : public IRightHandSide private: - KOKKOS_FUNCTION static IndexVx_ghosted ghosted_from_index(IndexVx const& index) - { - return IndexVx_ghosted(index.uid() + 1); - } - KOKKOS_FUNCTION static IndexVx_ghosted_staggered ghosted_staggered_from_index( - IndexVx const& index) - { - return IndexVx_ghosted_staggered(index.uid() + 1); - } + template + KOKKOS_FUNCTION static ddc::DiscreteElement to_index( + ddc::DiscreteElement const& index); + + template + std::enable_if_t> build_ghosted_staggered_vx_point_sampling( + ddc::DiscreteDomain const& dom); + + template + std::enable_if_t> build_ghosted_staggered_vx_point_sampling( + ddc::DiscreteDomain const& dom); -private: double m_nustar0; double m_fthresh; DFieldSpX m_nustar_profile_alloc;