Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
AnyInitialData.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_AMREX_INITIAL_DATA_HPP
22 #define FUB_AMREX_INITIAL_DATA_HPP
23 
24 #include "fub/Meta.hpp"
25 #include "fub/core/type_traits.hpp"
26 #include "fub/Duration.hpp"
27 
28 #include <memory>
29 
30 namespace fub {
31 
32 /// \defgroup InitialData Initial Data Methods
33 /// \ingroup GriddingAlgorithm
34 /// This group summarizes all components which initialize simulation data.
35 
36 namespace detail {
37 template <typename GriddingAlgorithm> struct InitialDataStrategy {
38  using PatchLevel = typename GridTraits<GriddingAlgorithm>::PatchLevel;
39 
40  virtual ~InitialDataStrategy() = default;
41 
42  virtual void InitializeData(PatchLevel& patch_level,
43  const GriddingAlgorithm& grid, int level,
44  Duration time) = 0;
45 
46  virtual std::unique_ptr<InitialDataStrategy> Clone() const = 0;
47 };
48 } // namespace detail
49 
50 /// \ingroup InitialData PolymorphicValueType
51 /// \brief This class is a polymoprhic value type which stores components to
52 /// initialize a gridding algorithm during its initialization procedure.
53 template <typename GriddingAlgorithm> class AnyInitialData {
54 public:
56 
57  /// @{
58  /// \name Constructors
59 
60  /// \brief Constructs an empty object that does no initialization.
61  AnyInitialData() = default;
62 
63  /// \brief Stores and wraps the `initial_data` object
64  template <typename T, typename = std::enable_if_t<
66  AnyInitialData(T&& initial_data);
67 
68  /// \brief Copies the `other` implementation and invokes a memory allocation.
69  AnyInitialData(const AnyInitialData& other);
70 
71  /// \brief Copies the `other` implementation and invokes a memory allocation.
73 
74  /// \brief Moves the `other` implementation without allocation and leaves an
75  /// empty object.
76  AnyInitialData(AnyInitialData&&) noexcept = default;
77 
78  /// \brief Moves the `other` implementation without allocation and leaves an
79  /// empty object.
80  AnyInitialData& operator=(AnyInitialData&&) noexcept = default;
81  /// @}
82 
83  /// @{
84  /// \name Actions
85 
86  /// \brief Initializes a patch level within a gridding algorithm
87  ///
88  /// \note In cases where a gridding algorithm needs to rebuild a level, the
89  /// InitializeData function might be invoked multiple times for the level.
90  void InitializeData(PatchLevel& patch_level, const GriddingAlgorithm& grid,
91  int level, Duration time);
92  /// @}
93 
94 private:
95  std::unique_ptr<detail::InitialDataStrategy<GriddingAlgorithm>> initial_data_;
96 };
97 
98 ///////////////////////////////////////////////////////////////////////////////
99 // Implementation
100 
101 ///////////////////////////////////////////////////////////////////////////////
102 // Constructors
103 
104 namespace detail {
105 template <typename T, typename GriddingAlgorithm>
106 struct InitialDataWrapper : public InitialDataStrategy<GriddingAlgorithm> {
108 
109  InitialDataWrapper(const T& initial_data) : initial_data_{initial_data} {}
110  InitialDataWrapper(T&& initial_data)
111  : initial_data_{std::move(initial_data)} {}
112 
113  void InitializeData(PatchLevel& patch_level, const GriddingAlgorithm& grid,
114  int level, Duration time) override {
115  initial_data_.InitializeData(patch_level, grid, level, time);
116  }
117 
118  std::unique_ptr<InitialDataStrategy<GriddingAlgorithm>>
119  Clone() const override {
120  return std::make_unique<InitialDataWrapper<T, GriddingAlgorithm>>(
121  initial_data_);
122  }
123 
124  T initial_data_;
125 };
126 } // namespace detail
127 
128 template <typename GriddingAlgorithm>
129 template <typename T, typename>
131  : initial_data_{
132  std::make_unique<detail::InitialDataWrapper<std::decay_t<T>, GriddingAlgorithm>>(
133  std::forward<T>(initial_data))} {}
134 
135 template <typename GriddingAlgorithm>
137  : initial_data_(other.initial_data_->Clone()) {}
138 
139 template <typename GriddingAlgorithm>
142  AnyInitialData tmp(other);
143  return *this = std::move(tmp);
144 }
145 
146 ///////////////////////////////////////////////////////////////////////////////
147 // Actions
148 
149 template <typename GriddingAlgorithm>
151  PatchLevel& patch_level, const GriddingAlgorithm& grid, int level,
152  Duration time) {
153  if (initial_data_) {
154  return initial_data_->InitializeData(patch_level, grid, level, time);
155  }
156 }
157 
158 } // namespace fub
159 
160 #endif
This class is a polymoprhic value type which stores components to initialize a gridding algorithm dur...
Definition: AnyInitialData.hpp:53
AnyInitialData(AnyInitialData &&) noexcept=default
Moves the other implementation without allocation and leaves an empty object.
AnyInitialData & operator=(const AnyInitialData &other)
Copies the other implementation and invokes a memory allocation.
Definition: AnyInitialData.hpp:141
void InitializeData(PatchLevel &patch_level, const GriddingAlgorithm &grid, int level, Duration time)
Initializes a patch level within a gridding algorithm.
Definition: AnyInitialData.hpp:150
AnyInitialData()=default
Constructs an empty object that does no initialization.
typename GridTraits< GriddingAlgorithm >::PatchLevel PatchLevel
Definition: AnyInitialData.hpp:55
std::unique_ptr< detail::InitialDataStrategy< GriddingAlgorithm > > initial_data_
Definition: AnyInitialData.hpp:95
std::decay_t< decltype(*std::declval< T >().GetGriddingAlgorithm())> GriddingAlgorithm
A template typedef to detect the member function.
Definition: Meta.hpp:56
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
std::chrono::duration< double > Duration
Definition: Duration.hpp:31
std::is_same< std::decay_t< X >, Y > decays_to
Definition: type_traits.hpp:296
Definition: Meta.hpp:67
This file adds basic type traits utilities which are not yet implemented in all standard libraries.