Skip to content

Commit

Permalink
field initialization refactor (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
UCaromel authored Sep 21, 2024
1 parent 9a2b4d6 commit ecef2fe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/core/data/field/field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Field : public NdArrayView<dim, Data_t>

bool isUsable() const { return Super::data() != nullptr; }
bool isSettable() const { return !isUsable(); }


template<typename... Args>
NO_DISCARD auto& operator()(Args&&... args)
Expand Down
36 changes: 36 additions & 0 deletions src/core/data/field/initializers/field_user_initializer.hpp
Original file line number Diff line number Diff line change
@@ -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 <tuple>
#include <memory>

namespace PHARE::core
{
class FieldUserFunctionInitializer
{
public:
template<typename Field, typename GridLayout>
void static initialize(Field& field, GridLayout const& layout,
initializer::InitFunction<GridLayout::dimension> const& init)
{
auto const indices = layout.ghostStartToEndIndices(field, /*includeEnd=*/true);
auto const coords = layout.template indexesToCoordVectors</*WithField=*/true>(
indices, field, [](auto& gridLayout, auto& field_, auto const&... args) {
return gridLayout.fieldNodeCoordinates(field_, gridLayout.origin(), args...);
});

std::shared_ptr<Span<double>> gridPtr // keep grid data alive
= std::apply([&](auto&... args) { return init(args...); }, coords);
Span<double>& 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
28 changes: 4 additions & 24 deletions src/core/data/vecfield/vecfield_initializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <array>

Expand Down Expand Up @@ -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<typename Field, typename GridLayout>
void initializeComponent_(Field& field, GridLayout const& layout,
initializer::InitFunction<dimension> const& init)
{
auto const indices = layout.ghostStartToEndIndices(field, /*includeEnd=*/true);
auto const coords = layout.template indexesToCoordVectors</*WithField=*/true>(
indices, field, [](auto& gridLayout, auto& field_, auto const&... args) {
return gridLayout.fieldNodeCoordinates(field_, gridLayout.origin(), args...);
});

std::shared_ptr<Span<double>> gridPtr // keep grid data alive
= std::apply([&](auto&... args) { return init(args...); }, coords);
Span<double>& 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<dimension> x_;
initializer::InitFunction<dimension> y_;
initializer::InitFunction<dimension> z_;
Expand Down

0 comments on commit ecef2fe

Please sign in to comment.