Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
AverageState.hpp
Go to the documentation of this file.
1 // Copyright (c) 2020 Maikel Nadolski
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #ifndef FUB_AMREX_AVERAGE_STATE_HPP
22 #define FUB_AMREX_AVERAGE_STATE_HPP
23 
24 #include <AMReX.H>
25 
26 #include "fub/AMReX/ForEachFab.hpp"
28 
29 #include "fub/ForEach.hpp"
30 
31 #include <mpi.h>
32 
33 namespace fub::amrex {
34 
35 template <typename InitialValue, typename BinaryOp>
36 void AccumulateState(const ::amrex::MultiFab& mf, const ::amrex::Box& box,
37  InitialValue&& init, BinaryOp&& binary_op) {
38  const auto ncomp = static_cast<std::size_t>(mf.nComp());
39  std::vector<double> state_buffer(ncomp);
40  span<const double> buffer(state_buffer);
41  ForEachFab(mf, [&](const ::amrex::MFIter& mfi) {
42  IndexBox<AMREX_SPACEDIM> section =
43  AsIndexBox<AMREX_SPACEDIM>(box & mfi.tilebox());
44  const ::amrex::FArrayBox& data = mf[mfi];
45  ForEachIndex(section, [&](auto... is) {
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));
49  }
50  std::invoke(std::forward<BinaryOp>(binary_op),
51  std::forward<InitialValue>(init), buffer);
52  });
53  });
54 }
55 
56 template <typename Equation>
57 void AverageState(Complete<Equation>& state, const ::amrex::MultiFab& mf,
58  const ::amrex::Geometry&, const ::amrex::Box& box) {
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();
62  ForEachFab(execution::seq, mf, [&](const ::amrex::MFIter& mfi) {
63  IndexBox<AMREX_SPACEDIM> section =
64  AsIndexBox<AMREX_SPACEDIM>(box & mfi.tilebox());
65  const ::amrex::FArrayBox& data = mf[mfi];
66  for (std::size_t comp = 0; comp < ncomp; ++comp) {
67  ForEachIndex(section, [&](auto... is) {
68  ::amrex::IntVect index{static_cast<int>(is)...};
69  state_buffer[comp] += data(index, static_cast<int>(comp)) /
70  static_cast<double>(num_cells);
71  });
72  }
73  });
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());
78  CopyFromBuffer(state, global_state_buffer);
79 }
80 
81 template <typename Equation>
82 void AverageState(Conservative<Equation>& state, const ::amrex::MultiFab& mf,
83  const ::amrex::Geometry&, const ::amrex::Box& box) {
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) {
88  IndexBox<AMREX_SPACEDIM> section =
89  AsIndexBox<AMREX_SPACEDIM>(box & mfi.tilebox());
90  const ::amrex::FArrayBox& data = mf[mfi];
91  for (std::size_t comp = 0; comp < ncomp; ++comp) {
92  ForEachIndex(section, [&](auto... is) {
93  ::amrex::IntVect index{static_cast<int>(is)...};
94  state_buffer[comp] += data(index, static_cast<int>(comp)) /
95  static_cast<double>(num_cells);
96  });
97  }
98  });
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());
103  CopyFromBuffer(state, global_state_buffer);
104 }
105 
106 } // namespace fub::amrex
107 
108 #endif
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