From ecef2fe4f3bc83cc1aacc2b7bb3ecba502947a77 Mon Sep 17 00:00:00 2001 From: Caromel <132669192+UCaromel@users.noreply.github.com> Date: Sat, 21 Sep 2024 15:11:19 +0200 Subject: [PATCH] field initialization refactor (#890) --- src/core/data/field/field.hpp | 2 +- .../initializers/field_user_initializer.hpp | 36 +++++++++++++++++++ .../data/vecfield/vecfield_initializer.hpp | 28 +++------------ 3 files changed, 41 insertions(+), 25 deletions(-) create mode 100644 src/core/data/field/initializers/field_user_initializer.hpp diff --git a/src/core/data/field/field.hpp b/src/core/data/field/field.hpp index 542e1c191..100b967c0 100644 --- a/src/core/data/field/field.hpp +++ b/src/core/data/field/field.hpp @@ -71,7 +71,7 @@ class Field : public NdArrayView bool isUsable() const { return Super::data() != nullptr; } bool isSettable() const { return !isUsable(); } - + template NO_DISCARD auto& operator()(Args&&... args) diff --git a/src/core/data/field/initializers/field_user_initializer.hpp b/src/core/data/field/initializers/field_user_initializer.hpp new file mode 100644 index 000000000..d9c74275a --- /dev/null +++ b/src/core/data/field/initializers/field_user_initializer.hpp @@ -0,0 +1,36 @@ +#ifndef _PHARE_CORE_DATA_FIELD_INITIAZILIZERS_FIELD_USER_INITIALIZER_HPP_ +#define _PHARE_CORE_DATA_FIELD_INITIAZILIZERS_FIELD_USER_INITIALIZER_HPP_ + +#include "core/utilities/span.hpp" +#include "initializer/data_provider.hpp" + +#include +#include + +namespace PHARE::core +{ +class FieldUserFunctionInitializer +{ +public: + template + void static initialize(Field& field, GridLayout const& layout, + initializer::InitFunction const& init) + { + auto const indices = layout.ghostStartToEndIndices(field, /*includeEnd=*/true); + auto const coords = layout.template indexesToCoordVectors( + indices, field, [](auto& gridLayout, auto& field_, auto const&... args) { + return gridLayout.fieldNodeCoordinates(field_, gridLayout.origin(), args...); + }); + + std::shared_ptr> gridPtr // keep grid data alive + = std::apply([&](auto&... args) { return init(args...); }, coords); + Span& grid = *gridPtr; + + for (std::size_t cell_idx = 0; cell_idx < indices.size(); cell_idx++) + std::apply([&](auto&... args) { field(args...) = grid[cell_idx]; }, indices[cell_idx]); + } +}; + +} // namespace PHARE::core + +#endif diff --git a/src/core/data/vecfield/vecfield_initializer.hpp b/src/core/data/vecfield/vecfield_initializer.hpp index b73b37bb9..edeb83bc6 100644 --- a/src/core/data/vecfield/vecfield_initializer.hpp +++ b/src/core/data/vecfield/vecfield_initializer.hpp @@ -4,6 +4,7 @@ #include "core/data/grid/gridlayoutdefs.hpp" #include "core/data/vecfield/vecfield_component.hpp" #include "initializer/data_provider.hpp" +#include "core/data/field/initializers/field_user_initializer.hpp" #include @@ -31,33 +32,12 @@ namespace core static_assert(GridLayout::dimension == VecField::dimension, "dimension mismatch between vecfield and gridlayout"); - initializeComponent_(v.getComponent(Component::X), layout, x_); - initializeComponent_(v.getComponent(Component::Y), layout, y_); - initializeComponent_(v.getComponent(Component::Z), layout, z_); + FieldUserFunctionInitializer::initialize(v.getComponent(Component::X), layout, x_); + FieldUserFunctionInitializer::initialize(v.getComponent(Component::Y), layout, y_); + FieldUserFunctionInitializer::initialize(v.getComponent(Component::Z), layout, z_); } private: - template - void initializeComponent_(Field& field, GridLayout const& layout, - initializer::InitFunction const& init) - { - auto const indices = layout.ghostStartToEndIndices(field, /*includeEnd=*/true); - auto const coords = layout.template indexesToCoordVectors( - indices, field, [](auto& gridLayout, auto& field_, auto const&... args) { - return gridLayout.fieldNodeCoordinates(field_, gridLayout.origin(), args...); - }); - - std::shared_ptr> gridPtr // keep grid data alive - = std::apply([&](auto&... args) { return init(args...); }, coords); - Span& grid = *gridPtr; - - for (std::size_t cell_idx = 0; cell_idx < indices.size(); cell_idx++) - std::apply([&](auto&... args) { field(args...) = grid[cell_idx]; }, - indices[cell_idx]); - } - - - initializer::InitFunction x_; initializer::InitFunction y_; initializer::InitFunction z_;