21 #ifndef FUB_SAMRAI_REGISTER_VARIABLES_HPP
22 #define FUB_SAMRAI_REGISTER_VARIABLES_HPP
26 #include <SAMRAI/pdat/CellVariable.h>
27 #include <SAMRAI/pdat/FaceVariable.h>
28 #include <SAMRAI/pdat/OuterfaceVariable.h>
42 std::string MakeVariableName(
const std::string& prefix,
43 const std::string& name);
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) {
56 throw std::runtime_error(
57 "A variable with name '" + name +
58 "' was previously registered with differing depth or dimension.");
60 return std::make_shared<VariableType>(dim, name, depth);
63 template <
typename State,
typename VariableType,
typename Equation>
64 void RegisterVariablesHelper(std::vector<int>& data_ids,
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) {
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);
83 vardb->registerVariableAndContext(variable, context, ghost_layer_width);
84 data_ids.push_back(data_id);
101 template <
typename Equation>
103 std::string prefix = std::string()) {
104 using Complete =
typename Equation::Complete;
109 SAMRAI::tbox::Dimension dim(desc.dim);
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");
115 desc.n_cons_variables =
116 std::tuple_size_v<std::decay_t<decltype(ConsTraits::names)>>;
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: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