Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
RegisterVariables.hpp
Go to the documentation of this file.
1 // Copyright (c) 2018 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_SAMRAI_REGISTER_VARIABLES_HPP
22 #define FUB_SAMRAI_REGISTER_VARIABLES_HPP
23 
24 #include "fub/Equation.hpp"
25 
26 #include <SAMRAI/pdat/CellVariable.h>
27 #include <SAMRAI/pdat/FaceVariable.h>
28 #include <SAMRAI/pdat/OuterfaceVariable.h>
29 
30 #include <vector>
31 
32 namespace fub {
33 namespace samrai {
34 
36  int dim;
37  std::vector<int> data_ids;
39 };
40 
41 namespace detail {
42 std::string MakeVariableName(const std::string& prefix,
43  const std::string& name);
44 
45 template <typename VariableType>
46 std::shared_ptr<VariableType> GetVariable(SAMRAI::tbox::Dimension dim,
47  const std::string& name, int depth) {
48  SAMRAI::hier::VariableDatabase* vardb =
49  SAMRAI::hier::VariableDatabase::getDatabase();
50  if (auto variable = vardb->getVariable(name)) {
51  if (auto specialised = std::dynamic_pointer_cast<VariableType>(variable);
52  specialised && specialised->getDepth() == depth &&
53  specialised->getDim() == dim) {
54  return specialised;
55  }
56  throw std::runtime_error(
57  "A variable with name '" + name +
58  "' was previously registered with differing depth or dimension.");
59  }
60  return std::make_shared<VariableType>(dim, name, depth);
61 }
62 
63 template <typename State, typename VariableType, typename Equation>
64 void RegisterVariablesHelper(std::vector<int>& data_ids,
65  const Equation& equation,
66  const SAMRAI::tbox::Dimension& dim,
67  const SAMRAI::hier::IntVector& ghost_layer_width,
68  const std::string& prefix,
69  const std::string& context_name) {
70  constexpr auto names = StateTraits<State>::names;
71  const auto sizes = StateToTuple(Depths(equation, Type<State>{}));
72  SAMRAI::hier::VariableDatabase* vardb =
73  SAMRAI::hier::VariableDatabase::getDatabase();
74  std::shared_ptr<SAMRAI::hier::VariableContext> context =
75  vardb->getContext(context_name);
76  data_ids.reserve(std::tuple_size_v<std::decay_t<decltype(names)>>);
77  boost::mp11::tuple_for_each(Zip(names, sizes), [&](auto xs) {
78  const int depth = std::get<1>(xs);
79  const char* name = std::get<0>(xs);
80  const std::string variable_name = MakeVariableName(prefix, name);
81  auto variable = GetVariable<VariableType>(dim, variable_name, depth);
82  const int data_id =
83  vardb->registerVariableAndContext(variable, context, ghost_layer_width);
84  data_ids.push_back(data_id);
85  });
86 }
87 } // namespace detail
88 
89 /// This function registers all neccessary variables and contexts with SAMRAI.
90 /// The returned DataDescription shall be passed to solver classes.
91 ///
92 /// \note It is currently assumed, that state and scratch variables are cell
93 /// centered and flux and coarse_fine_interfaces are face centered.
94 ///
95 /// \param[in] equation The equation describes shape and size for each variable.
96 ///
97 /// \param[in] prefix An optional prefix which will be prepend to all variable
98 /// names.
99 ///
100 /// \return Returns the patch data ids for all registered variables.
101 template <typename Equation>
103  std::string prefix = std::string()) {
104  using Complete = typename Equation::Complete;
105  using Conservative = typename Equation::Conservative;
106  using ConsTraits = typename Conservative::Traits;
107 
108  DataDescription desc{equation.Rank(), {}, 0};
109  SAMRAI::tbox::Dimension dim(desc.dim);
110 
111  const SAMRAI::hier::IntVector zero = SAMRAI::hier::IntVector::getZero(dim);
112  detail::RegisterVariablesHelper<Complete, SAMRAI::pdat::CellVariable<double>>(
113  desc.data_ids, equation, dim, zero, prefix, "current");
114 
115  desc.n_cons_variables =
116  std::tuple_size_v<std::decay_t<decltype(ConsTraits::names)>>;
117 
118  return desc;
119 }
120 
121 } // namespace samrai
122 } // namespace fub
123 
124 #endif
std::decay_t< decltype(std::declval< T >().GetEquation())> Equation
A template typedef to detect the member function.
Definition: Meta.hpp:59
DataDescription RegisterVariables(const Equation &equation, std::string prefix=std::string())
This function registers all neccessary variables and contexts with SAMRAI.
Definition: RegisterVariables.hpp:102
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
constexpr struct fub::DepthsFn Depths
constexpr auto Zip(Ts &&... ts)
Definition: State.hpp:65
auto StateToTuple(const State &x)
Definition: State.hpp:322
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
StateTraits< ConservativeBase< Equation > > Traits
Definition: State.hpp:254
Definition: State.hpp:35
Definition: State.hpp:162
Definition: RegisterVariables.hpp:35
int n_cons_variables
Definition: RegisterVariables.hpp:38
int dim
Definition: RegisterVariables.hpp:36
std::vector< int > data_ids
Definition: RegisterVariables.hpp:37