From 72cac2ff853418151dc172e15bcc85f07cee0d1e Mon Sep 17 00:00:00 2001 From: Loic Gouarin Date: Mon, 7 Apr 2025 15:17:11 +0200 Subject: [PATCH 1/4] clean update_field --- include/samurai/algorithm/update.hpp | 71 ---------------------------- include/samurai/mr/adapt.hpp | 35 +------------- 2 files changed, 2 insertions(+), 104 deletions(-) diff --git a/include/samurai/algorithm/update.hpp b/include/samurai/algorithm/update.hpp index d6c6436ed..5ae2a8df5 100644 --- a/include/samurai/algorithm/update.hpp +++ b/include/samurai/algorithm/update.hpp @@ -746,77 +746,6 @@ namespace samurai } } - template - bool update_field(Tag& tag, Fields&... fields) - { - static constexpr std::size_t dim = Tag::dim; - using mesh_t = typename Tag::mesh_t; - using size_type = typename Tag::size_type; - using mesh_id_t = typename Tag::mesh_t::mesh_id_t; - using cl_type = typename Tag::mesh_t::cl_type; - - auto& mesh = tag.mesh(); - - cl_type cl; - - for_each_interval(mesh[mesh_id_t::cells], - [&](std::size_t level, const auto& interval, const auto& index) - { - auto itag = static_cast(interval.start + interval.index); - for (auto i = interval.start; i < interval.end; ++i) - { - if (tag[itag] & static_cast(CellFlag::refine)) - { - if (level < mesh.max_level()) - { - static_nested_loop( - [&](const auto& stencil) - { - auto new_index = 2 * index + stencil; - cl[level + 1][new_index].add_interval({2 * i, 2 * i + 2}); - }); - } - else - { - cl[level][index].add_point(i); - } - } - else if (tag[itag] & static_cast(CellFlag::keep)) - { - cl[level][index].add_point(i); - } - else if (tag[itag] & static_cast(CellFlag::coarsen)) - { - if (level > mesh.min_level()) - { - cl[level - 1][index >> 1].add_point(i >> 1); - } - else - { - cl[level][index].add_point(i); - } - } - itag++; - } - }); - - mesh_t new_mesh = {cl, mesh}; - -#ifdef SAMURAI_WITH_MPI - mpi::communicator world; - if (mpi::all_reduce(world, mesh == new_mesh, std::logical_and())) -#else - if (mesh == new_mesh) -#endif - { - return true; - } - - detail::update_fields(new_mesh, fields...); - tag.mesh().swap(new_mesh); - return false; - } - template bool update_field_mr(const Tag& tag, Field& field, Fields&... other_fields) { diff --git a/include/samurai/mr/adapt.hpp b/include/samurai/mr/adapt.hpp index 215a661b7..d210630e6 100644 --- a/include/samurai/mr/adapt.hpp +++ b/include/samurai/mr/adapt.hpp @@ -267,7 +267,7 @@ namespace samurai subset_1.apply_op(to_coarsen_mr(m_detail, m_tag, eps_l, min_level), to_refine_mr(m_detail, m_tag, - (pow(2.0, regularity_to_use)) * eps_l, + (pow(2.0, regularity_to_use))*eps_l, max_level)); // Refinement according to Harten update_tag_subdomains(level, m_tag, true); } @@ -303,39 +303,8 @@ namespace samurai keep_subset.apply_op(maximum(m_tag)); } - using ca_type = typename mesh_t::ca_type; - // return update_field_mr(m_tag, m_fields, other_fields...); - // for some reason I do not understand the above code produces the following error : - // C++ exception with description "Incompatible dimension of arrays, compile in DEBUG for more info" thrown in the test body - // on test adapt_test/2.mutliple_fields with: - // linux-mamba (clang-18, ubuntu-24.04, clang, clang-18, clang-18, clang++-18) - // while the code bellow do not. - const auto& min_indices = mesh.domain().min_indices(); - const auto& max_indices = mesh.domain().max_indices(); - - std::array nb_cells_finest_level; - - for (size_t d = 0; d != max_indices.size(); ++d) - { - nb_cells_finest_level[d] = max_indices[d] - min_indices[d]; - } - - ca_type new_ca = update_cell_array_from_tag(mesh[mesh_id_t::cells], m_tag); - make_graduation(new_ca, mesh.mpi_neighbourhood(), mesh.periodicity(), nb_cells_finest_level, mesh_t::config::graduation_width); - mesh_t new_mesh{new_ca, mesh}; -#ifdef SAMURAI_WITH_MPI - mpi::communicator world; - if (mpi::all_reduce(world, mesh == new_mesh, std::logical_and())) -#else - if (mesh == new_mesh) -#endif // SAMURAI_WITH_MPI - { - return true; - } - detail::update_fields(new_mesh, m_fields, other_fields...); - m_fields.mesh().swap(new_mesh); - return false; + return update_field_mr(m_tag, m_fields, other_fields...); } template From 7f1555770b9ea2da4d5f4909c316b872e36f2d2b Mon Sep 17 00:00:00 2001 From: Loic Gouarin Date: Mon, 7 Apr 2025 15:38:42 +0200 Subject: [PATCH 2/4] fix pre-commit --- include/samurai/mr/adapt.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/samurai/mr/adapt.hpp b/include/samurai/mr/adapt.hpp index d210630e6..86175b592 100644 --- a/include/samurai/mr/adapt.hpp +++ b/include/samurai/mr/adapt.hpp @@ -267,7 +267,7 @@ namespace samurai subset_1.apply_op(to_coarsen_mr(m_detail, m_tag, eps_l, min_level), to_refine_mr(m_detail, m_tag, - (pow(2.0, regularity_to_use))*eps_l, + (pow(2.0, regularity_to_use)) * eps_l, max_level)); // Refinement according to Harten update_tag_subdomains(level, m_tag, true); } From 62042e8d142cca5f1395c3e8e7a796e5127dbfe1 Mon Sep 17 00:00:00 2001 From: Loic Gouarin Date: Mon, 7 Apr 2025 17:49:25 +0200 Subject: [PATCH 3/4] fix update_field for AMR methods --- demos/FiniteVolume/AMR_Burgers_Hat.cpp | 2 +- demos/FiniteVolume/level_set.cpp | 2 +- demos/FiniteVolume/level_set_from_scratch.cpp | 8 +++++++- include/samurai/algorithm/graduation.hpp | 4 ++-- include/samurai/mesh.hpp | 13 ++++++------- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/demos/FiniteVolume/AMR_Burgers_Hat.cpp b/demos/FiniteVolume/AMR_Burgers_Hat.cpp index 4d65d2391..d8a4ae043 100644 --- a/demos/FiniteVolume/AMR_Burgers_Hat.cpp +++ b/demos/FiniteVolume/AMR_Burgers_Hat.cpp @@ -274,7 +274,7 @@ int main(int argc, char* argv[]) tag.resize(); AMR_criteria(phi, tag); samurai::graduation(tag, stencil_grad); - if (samurai::update_field(tag, phi)) + if (samurai::update_field_mr(tag, phi)) { break; } diff --git a/demos/FiniteVolume/level_set.cpp b/demos/FiniteVolume/level_set.cpp index 825698eb9..8b58b6728 100644 --- a/demos/FiniteVolume/level_set.cpp +++ b/demos/FiniteVolume/level_set.cpp @@ -336,7 +336,7 @@ int main(int argc, char* argv[]) AMR_criteria(phi, tag); samurai::graduation(tag, stencil_grad); samurai::update_ghost(phi, u); - if ((samurai::update_field(tag, phi, u))) + if ((samurai::update_field_mr(tag, phi, u))) { break; } diff --git a/demos/FiniteVolume/level_set_from_scratch.cpp b/demos/FiniteVolume/level_set_from_scratch.cpp index 38ff27524..d14f13c90 100644 --- a/demos/FiniteVolume/level_set_from_scratch.cpp +++ b/demos/FiniteVolume/level_set_from_scratch.cpp @@ -58,6 +58,7 @@ struct AMRConfig static constexpr std::size_t max_refinement_level = 20; static constexpr int max_stencil_width = 2; static constexpr int ghost_width = 2; + static constexpr int graduation_width = 2; static constexpr std::size_t prediction_order = 1; using interval_t = samurai::Interval; using mesh_id_t = SimpleID; @@ -86,6 +87,11 @@ class AMRMesh : public samurai::Mesh_base, Config> { } + inline AMRMesh(const ca_type& ca, const self_type& ref_mesh) + : base_type(ca, ref_mesh) + { + } + inline AMRMesh(const cl_type& cl, std::size_t min_level, std::size_t max_level) : base_type(cl, min_level, max_level) { @@ -408,7 +414,7 @@ bool update_mesh(Field& f, Field_u& u, Tag& tag) return true; } - samurai::update_field(tag, f, u); + samurai::update_field_mr(tag, f, u); tag.mesh().swap(new_mesh); return false; diff --git a/include/samurai/algorithm/graduation.hpp b/include/samurai/algorithm/graduation.hpp index d749f3201..1c729f8bd 100644 --- a/include/samurai/algorithm/graduation.hpp +++ b/include/samurai/algorithm/graduation.hpp @@ -315,9 +315,9 @@ namespace samurai } // end for } - template + template size_t make_graduation(CellArray& ca, - [[maybe_unused]] const std::vector>& mpi_neighbourhood, + [[maybe_unused]] const Subdomains& mpi_neighbourhood, const std::array& is_periodic, const std::array& nb_cells_finest_level, const size_t grad_width = 1) diff --git a/include/samurai/mesh.hpp b/include/samurai/mesh.hpp index cf8f74e24..8c788fd63 100644 --- a/include/samurai/mesh.hpp +++ b/include/samurai/mesh.hpp @@ -105,9 +105,9 @@ namespace samurai const lca_type& subdomain() const; const ca_type& get_union() const; bool is_periodic(std::size_t d) const; - const std::array& periodicity() const; + const auto& periodicity() const; // std::vector& neighbouring_ranks(); - std::vector& mpi_neighbourhood(); + const auto& mpi_neighbourhood() const; void swap(Mesh_base& mesh) noexcept; @@ -502,9 +502,8 @@ namespace samurai template template - inline auto Mesh_base::get_interval(std::size_t level, - const interval_t& interval, - const xt::xexpression& index) const -> const interval_t& + inline auto Mesh_base::get_interval(std::size_t level, const interval_t& interval, const xt::xexpression& index) const + -> const interval_t& { return m_cells[mesh_id_t::reference].get_interval(level, interval, index); } @@ -565,13 +564,13 @@ namespace samurai } template - inline auto Mesh_base::periodicity() const -> const std::array& + inline const auto& Mesh_base::periodicity() const { return m_periodic; } template - inline auto Mesh_base::mpi_neighbourhood() -> std::vector& + inline const auto& Mesh_base::mpi_neighbourhood() const { return m_mpi_neighbourhood; } From a26746f558d7ec278452dcc3b0040196492160c7 Mon Sep 17 00:00:00 2001 From: Loic Gouarin Date: Mon, 7 Apr 2025 17:52:36 +0200 Subject: [PATCH 4/4] fix pre-commit --- include/samurai/mesh.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/samurai/mesh.hpp b/include/samurai/mesh.hpp index 8c788fd63..0da094e48 100644 --- a/include/samurai/mesh.hpp +++ b/include/samurai/mesh.hpp @@ -502,8 +502,9 @@ namespace samurai template template - inline auto Mesh_base::get_interval(std::size_t level, const interval_t& interval, const xt::xexpression& index) const - -> const interval_t& + inline auto Mesh_base::get_interval(std::size_t level, + const interval_t& interval, + const xt::xexpression& index) const -> const interval_t& { return m_cells[mesh_id_t::reference].get_interval(level, interval, index); }