Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
SplitSystemSourceLevelIntegrator.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 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_HYPERBOLIC_SPLIT_SYSTEM_SOURCE_SOLVER_HPP
22 #define FUB_HYPERBOLIC_SPLIT_SYSTEM_SOURCE_SOLVER_HPP
23 
24 #include "fub/Meta.hpp"
25 #include "fub/ext/outcome.hpp"
26 #include "fub/split_method/StrangSplitting.hpp"
27 
28 #include <algorithm>
29 
30 namespace fub {
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 // Declaration
34 
35 /// \ingroup LevelIntegrator
36 template <typename SystemSolver, typename SourceTerm,
37  typename SplittingMethod = StrangSplitting>
38 class SplitSystemSourceLevelIntegrator : private SystemSolver {
39 public:
41 
42  static const int Rank = std::max(SystemSolver::Rank, SourceTerm::Rank);
43 
44  /// \brief Constructs a system source solver from given sub solvers.
45  SplitSystemSourceLevelIntegrator(SystemSolver system_solver,
46  SourceTerm source_term,
47  SplittingMethod split = SplittingMethod());
48 
49  // Accessors
50 
51  SystemSolver& GetSystem() noexcept { return *this; }
52  const SystemSolver& GetSystem() const noexcept { return *this; }
53 
54  SourceTerm& GetSource() noexcept { return source_term_; }
55  const SourceTerm& GetSource() const noexcept { return source_term_; }
56 
57  using SystemSolver::GetContext;
58  using SystemSolver::GetCounterRegistry;
59  using SystemSolver::GetCycles;
60  using SystemSolver::GetGriddingAlgorithm;
61  using SystemSolver::GetMpiCommunicator;
62  using SystemSolver::GetTimePoint;
63 
64  using SystemSolver::FillGhostLayerSingleLevel;
65  using SystemSolver::FillGhostLayerTwoLevels;
66 
67  using SystemSolver::GetRatioToCoarserLevel;
68  using SystemSolver::LevelExists;
69 
70  using SystemSolver::CoarsenConservatively;
72  using SystemSolver::ComputeNumericFluxes;
73  using SystemSolver::CopyDataToScratch;
74  using SystemSolver::CopyScratchToData;
75  using SystemSolver::UpdateConservatively;
76 
77  using SystemSolver::AccumulateCoarseFineFluxes;
78  using SystemSolver::ApplyFluxCorrection;
79  using SystemSolver::ResetCoarseFineFluxes;
80 
81  /// \brief Invokes any "pre-advance" logic of both sub solvers.
82  void PreAdvanceHierarchy();
83 
84  /// \brief Invokes any "post-advance" logic of both sub solvers.
86 
87  void PreAdvanceLevel(int level, Duration dt, std::pair<int, int> subcycle);
88 
89  [[nodiscard]] Result<void, TimeStepTooLarge>
90  PostAdvanceLevel(int level, Duration dt, std::pair<int, int> subcycle);
91 
92  // Modifiers
93 
94  /// \brief Resets the hierarchy configuration of both sub solvers.
95  template <typename... Args>
96  void ResetHierarchyConfiguration(const Args&... args);
97 
98  /// \brief Returns the minimum of stable time step sizes for both sub solvers.
99  [[nodiscard]] Duration ComputeStableDt(int level);
100 
101  [[nodiscard]] Result<void, TimeStepTooLarge>
102  AdvanceLevelNonRecursively(int level, Duration dt,
103  std::pair<int, int> subcycle = {0, 1});
104 
105 private:
106  SourceTerm source_term_;
107  SplittingMethod splitting_;
108 };
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 // Implementation
112 
113 template <typename SystemSolver, typename SourceTerm, typename SplittingMethod>
115  SplitSystemSourceLevelIntegrator(SystemSolver system_solver,
116  SourceTerm source_term,
117  SplittingMethod split)
118  : SystemSolver(std::move(system_solver)),
119  source_term_{std::move(source_term)}, splitting_{split} {}
120 
121 template <typename SystemSolver, typename SourceTerm, typename SplittingMethod>
122 template <typename... Args>
124  SystemSolver, SourceTerm,
125  SplittingMethod>::ResetHierarchyConfiguration(const Args&... args) {
126  ResetHierarchyConfigurationIfDetected(GetSystem(), args...);
127  ResetHierarchyConfigurationIfDetected(source_term_, args...);
128 }
129 
130 template <typename SystemSolver, typename SourceTerm, typename SplittingMethod>
131 void SplitSystemSourceLevelIntegrator<SystemSolver, SourceTerm,
132  SplittingMethod>::PreAdvanceHierarchy() {
135  }
137  source_term_.PreAdvanceHierarchy();
138  }
139 }
140 
141 template <typename SystemSolver, typename SourceTerm, typename SplittingMethod>
143  SystemSolver, SourceTerm,
144  SplittingMethod>::PostAdvanceHierarchy([[maybe_unused]] Duration dt) {
145  if constexpr (is_detected<meta::PostAdvanceHierarchy, SourceTerm&,
146  Duration>()) {
147  source_term_.PostAdvanceHierarchy(dt);
149  source_term_.PostAdvanceHierarchy();
150  }
151 
152  if constexpr (is_detected<meta::PostAdvanceHierarchy, SystemSolver&,
153  Duration>()) {
155  } else if constexpr (is_detected<meta::PostAdvanceHierarchy,
156  SystemSolver&>()) {
158  }
159 }
160 
161 template <typename SystemSolver, typename SourceTerm, typename SplittingMethod>
162 Duration
163 SplitSystemSourceLevelIntegrator<SystemSolver, SourceTerm,
164  SplittingMethod>::ComputeStableDt(int level) {
165  return std::min(SystemSolver::ComputeStableDt(level),
166  source_term_.ComputeStableDt(level));
167 }
168 
169 template <typename SystemSolver, typename SourceTerm, typename SplittingMethod>
171  SystemSolver, SourceTerm,
172  SplittingMethod>::PreAdvanceLevel(int this_level, Duration dt,
173  std::pair<int, int> subcycle) {
174  SystemSolver::PreAdvanceLevel(this_level, dt, subcycle);
175  if constexpr (is_detected<meta::PreAdvanceLevel, SourceTerm&, int, Duration,
176  std::pair<int, int>>()) {
177  source_term_.PreAdvanceLevel(this_level, dt, subcycle);
178  }
179 }
180 
181 template <typename SystemSolver, typename SourceTerm, typename SplittingMethod>
184  PostAdvanceLevel(int this_level, Duration dt,
185  std::pair<int, int> subcycle) {
186  if constexpr (is_detected<meta::PostAdvanceLevel, SourceTerm&, int, Duration,
187  std::pair<int, int>>()) {
188  source_term_.PostAdvanceLevel(this_level, dt, subcycle);
189  }
190  return SystemSolver::PostAdvanceLevel(this_level, dt, subcycle);
191 }
192 
193 template <typename SystemSolver, typename SourceTerm, typename SplittingMethod>
196  AdvanceLevelNonRecursively(int this_level, Duration dt,
197  std::pair<int, int> subcycle) {
198  auto AdvanceSystem = [&](Duration dt) -> Result<void, TimeStepTooLarge> {
199  return SystemSolver::AdvanceLevelNonRecursively(this_level, dt, subcycle);
200  };
201 
202  auto AdvanceSource = [&](Duration dt) -> Result<void, TimeStepTooLarge> {
203  return source_term_.AdvanceLevel(SystemSolver::GetContext(), this_level,
204  dt);
205  };
206  return splitting_.Advance(dt, AdvanceSource, AdvanceSystem);
207 }
208 
209 } // namespace fub
210 
211 #endif
Definition: SplitSystemSourceLevelIntegrator.hpp:38
void ResetHierarchyConfiguration(const Args &... args)
Resets the hierarchy configuration of both sub solvers.
Definition: SplitSystemSourceLevelIntegrator.hpp:125
SourceTerm source_term_
Definition: SplitSystemSourceLevelIntegrator.hpp:106
Result< void, TimeStepTooLarge > PostAdvanceLevel(int level, Duration dt, std::pair< int, int > subcycle)
Definition: SplitSystemSourceLevelIntegrator.hpp:184
const SourceTerm & GetSource() const noexcept
Definition: SplitSystemSourceLevelIntegrator.hpp:55
SplitSystemSourceLevelIntegrator(SystemSolver system_solver, SourceTerm source_term, SplittingMethod split=SplittingMethod())
Constructs a system source solver from given sub solvers.
Definition: SplitSystemSourceLevelIntegrator.hpp:115
void PreAdvanceLevel(int level, Duration dt, std::pair< int, int > subcycle)
Definition: SplitSystemSourceLevelIntegrator.hpp:172
void PreAdvanceHierarchy()
Invokes any "pre-advance" logic of both sub solvers.
Definition: SplitSystemSourceLevelIntegrator.hpp:132
meta::GriddingAlgorithm< SystemSolver & > GriddingAlgorithm
Definition: SplitSystemSourceLevelIntegrator.hpp:40
void PostAdvanceHierarchy(Duration dt)
Invokes any "post-advance" logic of both sub solvers.
Definition: SplitSystemSourceLevelIntegrator.hpp:144
Result< void, TimeStepTooLarge > AdvanceLevelNonRecursively(int level, Duration dt, std::pair< int, int > subcycle={0, 1})
Definition: SplitSystemSourceLevelIntegrator.hpp:196
SourceTerm & GetSource() noexcept
Definition: SplitSystemSourceLevelIntegrator.hpp:54
SystemSolver & GetSystem() noexcept
Definition: SplitSystemSourceLevelIntegrator.hpp:51
Duration ComputeStableDt(int level)
Returns the minimum of stable time step sizes for both sub solvers.
Definition: SplitSystemSourceLevelIntegrator.hpp:164
const SystemSolver & GetSystem() const noexcept
Definition: SplitSystemSourceLevelIntegrator.hpp:52
static const int Rank
Definition: SplitSystemSourceLevelIntegrator.hpp:42
SplittingMethod splitting_
Definition: SplitSystemSourceLevelIntegrator.hpp:107
void CompleteFromCons(Equation &&equation, Complete< std::decay_t< Equation >> &complete, const Conservative< std::decay_t< Equation >> &cons)
Definition: CompleteFromCons.hpp:42
std::decay_t< decltype(*std::declval< T >().GetGriddingAlgorithm())> GriddingAlgorithm
A template typedef to detect the member function.
Definition: Meta.hpp:56
decltype(std::declval< T >().ResetHierarchyConfiguration(std::declval< Args >()...)) ResetHierarchyConfiguration
A template typedef to detect the member function.
Definition: Meta.hpp:52
decltype(std::declval< Context >().PreAdvanceHierarchy(std::declval< Args >()...)) PreAdvanceHierarchy
A template typedef to detect the member function.
Definition: Meta.hpp:36
decltype(std::declval< Context >().PreAdvanceLevel(std::declval< Args >()...)) PreAdvanceLevel
A template typedef to detect the member function.
Definition: Meta.hpp:44
decltype(std::declval< Context >().PostAdvanceHierarchy(std::declval< Args >()...)) PostAdvanceHierarchy
A template typedef to detect the member function.
Definition: Meta.hpp:40
decltype(std::declval< Context >().PostAdvanceLevel(std::declval< Args >()...)) PostAdvanceLevel
A template typedef to detect the member function.
Definition: Meta.hpp:48
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
std::chrono::duration< double > Duration
Definition: Duration.hpp:31
void ResetHierarchyConfigurationIfDetected(T &&obj, Grid &&grid)
Invokes member function obj.ResetHierarchyConfiguration(grid)
Definition: Meta.hpp:77
boost::outcome_v2::result< T, E > Result
Definition: outcome.hpp:32
This is std::true_type if Op<Args...> is a valid SFINAE expression.
Definition: type_traits.hpp:92