21 #ifndef FUB_AMREX_AVERAGE_STATE_HPP
22 #define FUB_AMREX_AVERAGE_STATE_HPP
35 template <
typename InitialValue,
typename BinaryOp>
37 InitialValue&& init, BinaryOp&& binary_op) {
38 const auto ncomp =
static_cast<std::size_t
>(mf.nComp());
39 std::vector<double> state_buffer(ncomp);
41 ForEachFab(mf, [&](const ::amrex::MFIter& mfi) {
43 AsIndexBox<AMREX_SPACEDIM>(box & mfi.tilebox());
44 const ::amrex::FArrayBox& data = mf[mfi];
46 ::amrex::IntVect
index{
static_cast<int>(is)...};
47 for (std::size_t comp = 0; comp < ncomp; ++comp) {
48 state_buffer[comp] = data(
index,
static_cast<int>(comp));
50 std::invoke(std::forward<BinaryOp>(binary_op),
51 std::forward<InitialValue>(init), buffer);
56 template <
typename Equation>
59 const auto ncomp =
static_cast<std::size_t
>(mf.nComp());
60 std::vector<double> state_buffer(ncomp);
61 const std::ptrdiff_t num_cells = box.numPts();
64 AsIndexBox<AMREX_SPACEDIM>(box & mfi.tilebox());
65 const ::amrex::FArrayBox& data = mf[mfi];
66 for (std::size_t comp = 0; comp < ncomp; ++comp) {
68 ::amrex::IntVect
index{
static_cast<int>(is)...};
69 state_buffer[comp] += data(
index,
static_cast<int>(comp)) /
70 static_cast<double>(num_cells);
74 std::vector<double> global_state_buffer(ncomp);
75 MPI_Allreduce(state_buffer.data(), global_state_buffer.data(),
76 static_cast<int>(ncomp), MPI_DOUBLE, MPI_SUM,
77 ::amrex::ParallelDescriptor::Communicator());
81 template <
typename Equation>
84 const auto ncomp =
static_cast<std::size_t
>(mf.nComp());
85 std::vector<double> state_buffer(ncomp);
86 const std::ptrdiff_t num_cells = box.numPts();
87 ForEachFab(mf, [&](const ::amrex::MFIter& mfi) {
89 AsIndexBox<AMREX_SPACEDIM>(box & mfi.tilebox());
90 const ::amrex::FArrayBox& data = mf[mfi];
91 for (std::size_t comp = 0; comp < ncomp; ++comp) {
93 ::amrex::IntVect
index{
static_cast<int>(is)...};
94 state_buffer[comp] += data(
index,
static_cast<int>(comp)) /
95 static_cast<double>(num_cells);
99 std::vector<double> global_state_buffer(ncomp);
100 MPI_Allreduce(state_buffer.data(), global_state_buffer.data(),
101 static_cast<int>(ncomp), MPI_DOUBLE, MPI_SUM,
102 ::amrex::ParallelDescriptor::Communicator());
A span is a view over a contiguous sequence of objects, the storage of which is owned by some other o...
Definition: span.hpp:81
void ForEachFab(Tag, const ::amrex::FabArrayBase &fabarray, F function)
Iterate through all local FArrayBox objects in a MultiFab.
Definition: ForEachFab.hpp:34
The amrex namespace.
Definition: AverageState.hpp:33
void AccumulateState(const ::amrex::MultiFab &mf, const ::amrex::Box &box, InitialValue &&init, BinaryOp &&binary_op)
Definition: AverageState.hpp:36
void AverageState(Complete< Equation > &state, const ::amrex::MultiFab &mf, const ::amrex::Geometry &, const ::amrex::Box &box)
Definition: AverageState.hpp:57
void ForEachIndex(const ::amrex::Box &box, F function)
Definition: ForEachIndex.hpp:29
constexpr SequentialTag seq
Definition: Execution.hpp:31
void CopyFromBuffer(Complete< Equation > &state, span< const double > buffer)
Definition: State.hpp:884
IndexBox< Rank > Box(const BasicView< State, Layout, Rank > &view)
Definition: State.hpp:486
std::ptrdiff_t index
Definition: type_traits.hpp:179
Definition: PatchDataView.hpp:56