Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
flux_method/GodunovMethod.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_IDEAL_GAS_FLUX_GODUNOV_METHOD_HPP
22 #define FUB_IDEAL_GAS_FLUX_GODUNOV_METHOD_HPP
23 
24 #include "fub/Direction.hpp"
25 #include "fub/Duration.hpp"
26 #include "fub/Equation.hpp"
28 #include "fub/ForEach.hpp"
29 #include "fub/core/span.hpp"
31 
32 #include <numeric>
33 
34 namespace fub {
35 
36 template <typename EquationT,
37  typename RiemannSolverT = ExactRiemannSolver<EquationT>>
38 class Godunov {
39 public:
40  using Equation = EquationT;
41  using RiemannSolver = RiemannSolverT;
42  using Complete = typename Equation::Complete;
43  using Conservative = typename Equation::Conservative;
46 
47  Godunov(const Equation& equation) : equation_{equation} {}
48  Godunov(const Equation& equation, const RiemannSolver& solver)
49  : equation_{equation}, riemann_solver_{solver} {}
50 
51  static constexpr int GetStencilWidth() noexcept;
52 
53  const Equation& GetEquation() const noexcept;
54  Equation& GetEquation() noexcept;
55 
56  const RiemannSolver& GetRiemannSolver() const noexcept;
57  RiemannSolver& GetRiemannSolver() noexcept;
58 
59  void ComputeNumericFlux(Conservative& numeric_flux,
60  span<const Complete, 2> states, Duration /* dt */,
61  double /* dx */, Direction dir);
62 
63  void ComputeNumericFlux(ConservativeArray& numeric_flux,
64  span<const CompleteArray, 2> states,
65  Duration /* dt */, double /* dx */, Direction dir);
66 
67  void ComputeNumericFlux(ConservativeArray& numeric_flux,
68  Array1d face_fraction,
69  span<const CompleteArray, 2> states,
70  span<const Array1d, 2> volume_fraction,
71  Duration /* dt */, double /* dx */, Direction dir);
72 
73  double ComputeStableDt(span<const Complete, 2> states, double dx,
74  Direction dir);
75 
76  Array1d ComputeStableDt(span<const CompleteArray, 2> states, double dx,
77  Direction dir);
78 
79  Array1d ComputeStableDt(span<const CompleteArray, 2> states,
80  Array1d face_fraction,
81  span<const Array1d, 2> volume_fraction, double dx,
82  Direction dir);
83 
84 private:
89 };
90 
91 /// \ingroup FluxMethod
92 template <typename Equation, typename RPSolver = ExactRiemannSolver<Equation>>
93 struct GodunovMethod : public FluxMethod<Godunov<Equation, RPSolver>> {
95 };
96 
97 template <typename Equation>
98 GodunovMethod(const Equation& eq)->GodunovMethod<Equation>;
99 
100 ///////////////////////////////////////////////////////////////////////////////
101 // Implementation
102 
103 template <typename Equation, typename RiemannSolver>
105  return 1;
106 }
107 
108 template <typename Equation, typename RiemannSolver>
110  return equation_;
111 }
112 
113 template <typename Equation, typename RiemannSolver>
115  return equation_;
116 }
117 
118 template <typename Equation, typename RiemannSolver>
120  noexcept {
121  return riemann_solver_;
122 }
123 
124 template <typename Equation, typename RiemannSolver>
126  return riemann_solver_;
127 }
128 
129 template <typename Equation, typename RiemannSolver>
131  Conservative& numeric_flux, span<const Complete, 2> states,
132  Duration /* dt */, double /* dx */, Direction dir) {
133  riemann_solver_.SolveRiemannProblem(riemann_solution_, states[0], states[1],
134  dir);
135  Flux(equation_, numeric_flux, riemann_solution_, dir);
136 }
137 
138 template <typename Equation, typename RiemannSolver>
140  ConservativeArray& numeric_flux, span<const CompleteArray, 2> states,
141  Duration /* dt */, double /* dx */, Direction dir) {
142  riemann_solver_.SolveRiemannProblem(riemann_solution_arr_, states[0],
143  states[1], dir);
144  Flux(equation_, numeric_flux, riemann_solution_arr_, dir);
145 }
146 
147 template <typename Equation, typename RiemannSolver>
149  ConservativeArray& numeric_flux, Array1d face_fraction,
151  span<const Array1d, 2> /* volume_fraction */, Duration /* dt */,
152  double /* dx */, Direction dir) {
153  MaskArray mask = face_fraction > 0.0;
154  riemann_solver_.SolveRiemannProblem(riemann_solution_arr_, states[0],
155  states[1], mask, dir);
156  Flux(equation_, numeric_flux, riemann_solution_arr_, dir);
157  const Array1d zero = Array1d::Zero();
158  ForEachComponent([&](auto&& nf, Array1d f) { nf = mask.select(f, zero); },
159  numeric_flux, numeric_flux);
160 }
161 
162 template <typename Equation, typename RiemannSolver>
164  span<const Complete, 2> states, double dx, Direction dir) {
165  auto signals = riemann_solver_.ComputeSignals(states[0], states[1], dir);
166  const double s_max = std::accumulate(
167  signals.begin(), signals.end(), 0.0,
168  [](double x, double y) { return std::max(x, std::abs(y)); });
169  return dx / s_max;
170 }
171 
172 template <typename Equation, typename RiemannSolver>
174  span<const CompleteArray, 2> states, double dx, Direction dir) {
175  auto signals = riemann_solver_.ComputeSignals(states[0], states[1], dir);
176  Array1d zero = Array1d::Zero();
177  const Array1d s_max =
178  std::accumulate(signals.begin(), signals.end(), zero,
179  [](const Array1d& x, const Array1d& y) -> Array1d {
180  return x.max(y.abs());
181  });
182  return Array1d(dx) / s_max;
183 }
184 
185 template <typename Equation, typename RiemannSolver>
187  span<const CompleteArray, 2> states, Array1d face_fraction,
188  span<const Array1d, 2>, double dx, Direction dir) {
189  std::array<Complete, 2> state{};
190  Array1d dts = Array1d::Constant(std::numeric_limits<double>::infinity());
191  for (int i = 0; i < face_fraction.size(); ++i) {
192  if (face_fraction[i] > 0.0) {
193  Load(state[0], states[0], i);
194  Load(state[1], states[1], i);
195  dts[i] = ComputeStableDt(state, dx, dir);
196  }
197  }
198  return dts;
199 }
200 
201 } // namespace fub
202 
203 #endif
This class applies a base flux nethod on a view of states.
Definition: flux_method/FluxMethod.hpp:57
Definition: flux_method/GodunovMethod.hpp:38
EquationT Equation
Definition: flux_method/GodunovMethod.hpp:40
const RiemannSolver & GetRiemannSolver() const noexcept
Definition: flux_method/GodunovMethod.hpp:119
static constexpr int GetStencilWidth() noexcept
Definition: flux_method/GodunovMethod.hpp:104
Complete riemann_solution_
Definition: flux_method/GodunovMethod.hpp:87
void ComputeNumericFlux(Conservative &numeric_flux, span< const Complete, 2 > states, Duration, double, Direction dir)
Definition: flux_method/GodunovMethod.hpp:130
Godunov(const Equation &equation, const RiemannSolver &solver)
Definition: flux_method/GodunovMethod.hpp:48
RiemannSolverT RiemannSolver
Definition: flux_method/GodunovMethod.hpp:41
CompleteArray riemann_solution_arr_
Definition: flux_method/GodunovMethod.hpp:88
Godunov(const Equation &equation)
Definition: flux_method/GodunovMethod.hpp:47
double ComputeStableDt(span< const Complete, 2 > states, double dx, Direction dir)
Definition: flux_method/GodunovMethod.hpp:163
Equation equation_
Definition: flux_method/GodunovMethod.hpp:85
typename Equation::Complete Complete
Definition: flux_method/GodunovMethod.hpp:42
const Equation & GetEquation() const noexcept
Definition: flux_method/GodunovMethod.hpp:109
RiemannSolver riemann_solver_
Definition: flux_method/GodunovMethod.hpp:86
typename Equation::Conservative Conservative
Definition: flux_method/GodunovMethod.hpp:43
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
std::decay_t< decltype(std::declval< T >().GetEquation())> Equation
A template typedef to detect the member function.
Definition: Meta.hpp:59
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
void Flux(Eq &&equation, Conservative< Equation > &flux, const Complete< Equation > &state, Direction dir, [[maybe_unused]] double x=0.0)
Definition: Equation.hpp:108
std::chrono::duration< double > Duration
Definition: Duration.hpp:31
Array< double, 1 > Array1d
Definition: Eigen.hpp:53
void Load(State &state, const BasicView< const State, Layout, Rank > &view, const std::array< std::ptrdiff_t, State::Equation::Rank()> &index)
Definition: State.hpp:640
GodunovMethod(const Equation &eq) -> GodunovMethod< Equation >
Direction
This is a type safe type to denote a dimensional split direction.
Definition: Direction.hpp:30
void ForEachComponent(F function, Ts &&... states)
Definition: State.hpp:624
Array< bool, 1 > MaskArray
Definition: Eigen.hpp:59
This type has a constructor which takes an equation and might allocate any dynamically sized member v...
Definition: State.hpp:335
This type has a constructor which takes an equation and might allocate any dynamically sized member v...
Definition: State.hpp:251
Definition: flux_method/GodunovMethod.hpp:93