Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
SAMRAI/IntegratorContext.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 Maikel Nadolski
2 // Copyright (c) 2019 Patrick Denzler
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 // SOFTWARE.
21 
22 #ifndef FUB_SAMRAI_INTEGRATOR_CONTEXT_HPP
23 #define FUB_SAMRAI_INTEGRATOR_CONTEXT_HPP
24 
25 #include "fub/Direction.hpp"
26 #include "fub/Duration.hpp"
27 #include "fub/HyperbolicMethod.hpp"
28 #include "fub/TimeStepError.hpp"
29 #include "fub/counter/CounterRegistry.hpp"
30 #include "fub/ext/outcome.hpp"
31 
33 
34 #include <SAMRAI/xfer/CoarsenAlgorithm.h>
35 #include <SAMRAI/xfer/RefineAlgorithm.h>
36 
37 #include <array>
38 #include <memory>
39 #include <vector>
40 
41 namespace fub {
42 namespace samrai {
43 
44 class IntegratorContext;
45 
47 
48 /// \ingroup IntegratorContext
49 ///
50 /// \brief This class is used by the HypebrolicSplitLevelIntegrator and
51 /// delegates AMR related tasks to the AMReX library.
53 public:
55  std::vector<int> scratch_ids;
56  std::vector<int> flux_ids;
57  std::vector<int> coarse_fine_ids;
58  };
59 
61  RegisterVariables(const DataDescription& desc, int scratch_ghost_cell_width,
62  int flux_ghost_cell_width);
63 
64  template <typename Method>
66  RegisterVariables(const DataDescription& desc, const Method& method) {
67  return IntegratorContext::RegisterVariables(desc, method.GetStencilWidth(),
68  1);
69  }
70 
71  /// @{
72  /// \name Constructors and Assignments
73 
74  /// \brief Constructs a context object from given a gridding algorithm and a
75  /// numerical method.
76  IntegratorContext(std::shared_ptr<GriddingAlgorithm> gridding,
77  HyperbolicMethod method,
78  AuxialiaryDataDescription aux_desc);
79 
80  /// \brief Deeply copies a context and all its distributed data for all MPI
81  /// ranks.
83 
84  /// \brief Deeply copies a context and all its distributed data for all MPI
85  /// ranks.
87 
88  IntegratorContext(IntegratorContext&&) noexcept = default;
89 
90  IntegratorContext& operator=(IntegratorContext&&) noexcept = default;
91 
92  ~IntegratorContext() = default;
93  /// @}
94 
95  /// @{
96  /// \name Member Accessors
97 
98  /// \brief Returns the current boundary condition for the specified level.
99  [[nodiscard]] const AnyBoundaryCondition& GetBoundaryCondition() const;
101 
102  /// \brief Returns a shared pointer to the underlying GriddingAlgorithm which
103  /// owns the simulation.
104  [[nodiscard]] const std::shared_ptr<GriddingAlgorithm>&
105  GetGriddingAlgorithm() const noexcept;
106 
107  /// \brief Returns a reference to const PatchHierarchy which is a member of
108  /// the GriddingAlgorithm.
109  [[nodiscard]] const PatchHierarchy& GetPatchHierarchy() const noexcept;
110 
111  /// \brief Returns a reference to PatchHierarchy which is a member of the
112  /// GriddingAlgorithm.
113  [[nodiscard]] PatchHierarchy& GetPatchHierarchy() noexcept;
114 
115  /// \brief Returns the MPI communicator which is associated with this context.
116  [[nodiscard]] MPI_Comm GetMpiCommunicator() const noexcept;
117  /// @}
118 
119  /// @{
120  /// \name Access Level-specific data
121 
122  /// \brief Returns the MultiFab associated with level data on the specifed
123  /// level number.
124  [[nodiscard]] SAMRAI::hier::PatchLevel& GetPatchLevel(int level);
125  [[nodiscard]] const SAMRAI::hier::PatchLevel& GetPatchLevel(int level) const;
126 
127  [[nodiscard]] span<const int> GetDataIds() const;
128  [[nodiscard]] span<const int> GetScratchIds() const;
129  [[nodiscard]] span<const int> GetFluxIds() const;
130 
131  /// \brief Returns the current time level for data at the specified refinement
132  /// level and direction.
133  [[nodiscard]] Duration GetTimePoint(int level = 0) const;
134 
135  /// \brief Returns the current number of cycles for data at the specified
136  /// refinement level and direction.
137  [[nodiscard]] std::ptrdiff_t GetCycles(int level = 0) const;
138 
139  /// \brief Returns the geometry object for the specified refinement level.
140  [[nodiscard]] const SAMRAI::geom::CartesianGridGeometry&
141  GetGeometry(int level) const;
142  /// @}
143 
144  /// @{
145  /// \name Observers
146 
147  /// \brief Returns true if the data exists for the specified level number.
148  [[nodiscard]] bool LevelExists(int level) const noexcept;
149 
150  /// \brief Returns the refinement ratio in the specified direction.
151  [[nodiscard]] int GetRatioToCoarserLevel(int level, Direction dir) const
152  noexcept;
153 
154  /// \brief Returns the refinement ratio for all directions.
155  [[nodiscard]] SAMRAI::hier::IntVector GetRatioToCoarserLevel(int level) const
156  noexcept;
157  /// @}
158 
159  /// @{
160  /// \name Modifiers
161 
162  /// \brief Replaces the underlying gridding algorithm with the specified one.
163  void ResetHierarchyConfiguration(std::shared_ptr<GriddingAlgorithm> gridding);
164 
165  /// \brief Whenever the gridding algorithm changes the data hierarchy this
166  /// function will regrid all distributed helper variables managed by the
167  /// context.
168  ///
169  /// \param[in] level The level number of the coarsest level which changed its
170  /// shape. Regrid all levels finer than level.
171  void ResetHierarchyConfiguration(int level = 0);
172 
173  /// \brief Sets the cycle count for a specific level number and direction.
174  void SetCycles(std::ptrdiff_t cycle, int level);
175 
176  /// \brief Sets the time point for a specific level number and direction.
177  void SetTimePoint(Duration t, int level);
178  /// @}
179 
180  /// @{
181  /// \name Member functions relevant for the level integrator algorithm.
182 
183  /// \brief On each first subcycle this will regrid the data if neccessary.
184  int PreAdvanceLevel(int level_num, Duration dt, std::pair<int, int> subcycle);
185 
186  /// \brief Increases the internal time stamps and cycle counters for the
187  /// specified level number and direction.
188  [[nodiscard]] Result<void, TimeStepTooLarge>
189  PostAdvanceLevel(int level_num, Duration dt, std::pair<int, int> subcycle);
190 
191  void ApplyBoundaryCondition(int level, Direction dir);
192 
193  /// \brief Fills the ghost layer of the scratch data and interpolates in the
194  /// coarse fine layer.
195  void FillGhostLayerTwoLevels(int level, int coarse);
196 
197  /// \brief Fills the ghost layer of the scratch data and does nothing in the
198  /// coarse fine layer.
199  void FillGhostLayerSingleLevel(int level);
200 
201  /// \brief Returns a estimate for a stable time step size which can be taken
202  /// for specified level number in direction dir.
203  [[nodiscard]] Duration ComputeStableDt(int level, Direction dir);
204 
205  /// \brief Fill the flux MultiFab with numeric fluxes based on current states
206  /// in scratch.
207  void ComputeNumericFluxes(int level, Duration dt, Direction dir);
208 
209  /// \brief Apply a conservative time update for each conservative variable on
210  /// the specified level number and direction.
211  void UpdateConservatively(int level, Duration dt, Direction dir);
212 
213  /// \brief Reconstruct complete state variables from conservative ones.
214  void CompleteFromCons(int level, Duration dt);
215 
216  /// \brief Accumulate fluxes on the coarse fine interfaces for a specified
217  /// fine level number.
218  void AccumulateCoarseFineFluxes(int level, double time_scale, Direction dir);
219 
220  /// \brief Replace the coarse fluxes by accumulated fine fluxes on the coarse
221  /// fine interfaces.
222  void ApplyFluxCorrection(int fine, int coarse, Duration dt);
223 
224  /// \brief Resets all accumulates fluxes to zero.
225  void ResetCoarseFineFluxes(int fine, int coarse);
226 
227  /// \brief Coarsen scratch data from a fine level number to a coarse level
228  /// number.
229  void CoarsenConservatively(int fine, int coarse);
230  ///@}
231 
232  void CopyDataToScratch(int level);
233 
234  void CopyScratchToData(int level);
235 
236  [[nodiscard]] const std::shared_ptr<CounterRegistry>&
237  GetCounterRegistry() const noexcept;
238 
239 private:
241  std::shared_ptr<GriddingAlgorithm> gridding_;
244 
245  std::vector<Duration> time_points_;
247  std::vector<std::ptrdiff_t> cycles_;
248 
249  using RefineAlgorithm = SAMRAI::xfer::RefineAlgorithm;
250  using RefineSchedule = SAMRAI::xfer::RefineSchedule;
251  using CoarsenAlgorithm = SAMRAI::xfer::CoarsenAlgorithm;
252  using CoarsenSchedule = SAMRAI::xfer::CoarsenSchedule;
253 
254  std::shared_ptr<RefineAlgorithm> fill_scratch_;
257 
258  std::vector<std::shared_ptr<RefineSchedule>> fill_scratch_two_level_schedule_;
259  std::vector<std::shared_ptr<RefineSchedule>> fill_scratch_one_level_schedule_;
260  std::vector<std::shared_ptr<CoarsenSchedule>> coarsen_scratch_schedule_;
261  std::vector<std::shared_ptr<CoarsenSchedule>> coarsen_fluxes_schedule_;
262 
263  std::vector<std::shared_ptr<SAMRAI::xfer::RefinePatchStrategy>> boundaries_;
264 };
265 
266 } // namespace samrai
267 } // namespace fub
268 
269 #endif
This is a polymorphic value type that wraps any BoundaryCondition object.
Definition: AnyBoundaryCondition.hpp:55
Definition: SAMRAI/GriddingAlgorithm.hpp:53
This class is used by the HypebrolicSplitLevelIntegrator and delegates AMR related tasks to the AMReX...
Definition: SAMRAI/IntegratorContext.hpp:52
std::shared_ptr< CoarsenAlgorithm > coarsen_fluxes_
Definition: SAMRAI/IntegratorContext.hpp:255
void SetCycles(std::ptrdiff_t cycle, int level)
Sets the cycle count for a specific level number and direction.
SAMRAI::xfer::CoarsenSchedule CoarsenSchedule
Definition: SAMRAI/IntegratorContext.hpp:252
span< const int > GetScratchIds() const
Returns the MultiFab associated with level data on the specifed level number.
IntegratorContext & operator=(const IntegratorContext &)=default
Deeply copies a context and all its distributed data for all MPI ranks.
std::ptrdiff_t GetCycles(int level=0) const
Returns the current number of cycles for data at the specified refinement level and direction.
void CoarsenConservatively(int fine, int coarse)
Coarsen scratch data from a fine level number to a coarse level number.
void ResetHierarchyConfiguration(std::shared_ptr< GriddingAlgorithm > gridding)
Replaces the underlying gridding algorithm with the specified one.
const PatchHierarchy & GetPatchHierarchy() const noexcept
Returns a reference to const PatchHierarchy which is a member of the GriddingAlgorithm.
const SAMRAI::geom::CartesianGridGeometry & GetGeometry(int level) const
Returns the geometry object for the specified refinement level.
std::shared_ptr< CoarsenAlgorithm > coarsen_scratch_
Definition: SAMRAI/IntegratorContext.hpp:256
IntegratorContext(std::shared_ptr< GriddingAlgorithm > gridding, HyperbolicMethod method, AuxialiaryDataDescription aux_desc)
Constructs a context object from given a gridding algorithm and a numerical method.
SAMRAI::xfer::RefineSchedule RefineSchedule
Definition: SAMRAI/IntegratorContext.hpp:250
IntegratorContext(const IntegratorContext &)=default
Deeply copies a context and all its distributed data for all MPI ranks.
void ResetCoarseFineFluxes(int fine, int coarse)
Resets all accumulates fluxes to zero.
Result< void, TimeStepTooLarge > PostAdvanceLevel(int level_num, Duration dt, std::pair< int, int > subcycle)
Increases the internal time stamps and cycle counters for the specified level number and direction.
int ghost_cell_width_
Definition: SAMRAI/IntegratorContext.hpp:240
std::vector< std::shared_ptr< CoarsenSchedule > > coarsen_fluxes_schedule_
Definition: SAMRAI/IntegratorContext.hpp:261
static AuxialiaryDataDescription RegisterVariables(const DataDescription &desc, int scratch_ghost_cell_width, int flux_ghost_cell_width)
SAMRAI::hier::PatchLevel & GetPatchLevel(int level)
Returns the MultiFab associated with level data on the specifed level number.
span< const int > GetFluxIds() const
Returns the MultiFab associated with level data on the specifed level number.
std::vector< std::shared_ptr< CoarsenSchedule > > coarsen_scratch_schedule_
Definition: SAMRAI/IntegratorContext.hpp:260
SAMRAI::xfer::RefineAlgorithm RefineAlgorithm
Definition: SAMRAI/IntegratorContext.hpp:249
void AccumulateCoarseFineFluxes(int level, double time_scale, Direction dir)
Accumulate fluxes on the coarse fine interfaces for a specified fine level number.
std::vector< std::shared_ptr< RefineSchedule > > fill_scratch_two_level_schedule_
Definition: SAMRAI/IntegratorContext.hpp:258
bool LevelExists(int level) const noexcept
Returns true if the data exists for the specified level number.
void ApplyFluxCorrection(int fine, int coarse, Duration dt)
Replace the coarse fluxes by accumulated fine fluxes on the coarse fine interfaces.
Duration GetTimePoint(int level=0) const
Returns the current time level for data at the specified refinement level and direction.
SAMRAI::xfer::CoarsenAlgorithm CoarsenAlgorithm
Definition: SAMRAI/IntegratorContext.hpp:251
std::vector< Duration > time_points_
Definition: SAMRAI/IntegratorContext.hpp:245
void SetTimePoint(Duration t, int level)
Sets the time point for a specific level number and direction.
Duration ComputeStableDt(int level, Direction dir)
Returns a estimate for a stable time step size which can be taken for specified level number in direc...
IntegratorContext(IntegratorContext &&) noexcept=default
Constructs a context object from given a gridding algorithm and a numerical method.
void FillGhostLayerTwoLevels(int level, int coarse)
Fills the ghost layer of the scratch data and interpolates in the coarse fine layer.
const AnyBoundaryCondition & GetBoundaryCondition() const
Returns the current boundary condition for the specified level.
std::vector< std::shared_ptr< RefineSchedule > > fill_scratch_one_level_schedule_
Definition: SAMRAI/IntegratorContext.hpp:259
HyperbolicMethod method_
Definition: SAMRAI/IntegratorContext.hpp:242
span< const int > GetDataIds() const
Returns the MultiFab associated with level data on the specifed level number.
void ComputeNumericFluxes(int level, Duration dt, Direction dir)
Fill the flux MultiFab with numeric fluxes based on current states in scratch.
static AuxialiaryDataDescription RegisterVariables(const DataDescription &desc, const Method &method)
Definition: SAMRAI/IntegratorContext.hpp:66
std::shared_ptr< RefineAlgorithm > fill_scratch_
Definition: SAMRAI/IntegratorContext.hpp:254
int PreAdvanceLevel(int level_num, Duration dt, std::pair< int, int > subcycle)
On each first subcycle this will regrid the data if neccessary.
int GetRatioToCoarserLevel(int level, Direction dir) const noexcept
Returns the refinement ratio in the specified direction.
const std::shared_ptr< GriddingAlgorithm > & GetGriddingAlgorithm() const noexcept
Returns a shared pointer to the underlying GriddingAlgorithm which owns the simulation.
void UpdateConservatively(int level, Duration dt, Direction dir)
Apply a conservative time update for each conservative variable on the specified level number and dir...
std::vector< std::ptrdiff_t > cycles_
Definition: SAMRAI/IntegratorContext.hpp:247
std::shared_ptr< GriddingAlgorithm > gridding_
Definition: SAMRAI/IntegratorContext.hpp:241
MPI_Comm GetMpiCommunicator() const noexcept
Returns the MPI communicator which is associated with this context.
void FillGhostLayerSingleLevel(int level)
Fills the ghost layer of the scratch data and does nothing in the coarse fine layer.
std::vector< std::shared_ptr< SAMRAI::xfer::RefinePatchStrategy > > boundaries_
Definition: SAMRAI/IntegratorContext.hpp:263
AuxialiaryDataDescription aux_desc_
Definition: SAMRAI/IntegratorContext.hpp:243
void ApplyBoundaryCondition(int level, Direction dir)
On each first subcycle this will regrid the data if neccessary.
const std::shared_ptr< CounterRegistry > & GetCounterRegistry() const noexcept
void CopyDataToScratch(int level)
std::vector< Duration > regrid_time_points_
Definition: SAMRAI/IntegratorContext.hpp:246
void CopyScratchToData(int level)
void CompleteFromCons(int level, Duration dt)
Reconstruct complete state variables from conservative ones.
Definition: SAMRAI/PatchHierarchy.hpp:69
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
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
std::chrono::duration< double > Duration
Definition: Duration.hpp:31
Direction
This is a type safe type to denote a dimensional split direction.
Definition: Direction.hpp:30
boost::outcome_v2::result< T, E > Result
Definition: outcome.hpp:32
Definition: HyperbolicMethod.hpp:150
Definition: TimeStepError.hpp:71
Definition: RegisterVariables.hpp:35
Definition: SAMRAI/IntegratorContext.hpp:54
std::vector< int > scratch_ids
Definition: SAMRAI/IntegratorContext.hpp:55
std::vector< int > coarse_fine_ids
Definition: SAMRAI/IntegratorContext.hpp:57
std::vector< int > flux_ids
Definition: SAMRAI/IntegratorContext.hpp:56