OGS
SecondaryVariable.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include "BaseLib/Algorithm.h"
16 
17 namespace NumLib
18 {
19 class LocalToGlobalIndexMap;
20 }
21 
22 namespace ProcessLib
23 {
27 {
38  using Function = std::function<GlobalVector const&(
39  const double t,
40  std::vector<GlobalVector*> const& x,
41  std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_table,
42  std::unique_ptr<GlobalVector>& result_cache)>;
43 
44  template <typename F1, typename F2>
45  SecondaryVariableFunctions(const unsigned num_components_, F1&& eval_field_,
46  F2&& eval_residuals_)
47  : num_components(num_components_),
48  eval_field(std::forward<F1>(eval_field_)),
49  eval_residuals(std::forward<F2>(eval_residuals_))
50  {
51  // Used to detect nasty implicit conversions.
52  static_assert(
53  std::is_same_v<
54  GlobalVector const&,
55  typename std::invoke_result_t<
56  F1, double const, std::vector<GlobalVector*> const&,
57  std::vector<NumLib::LocalToGlobalIndexMap const*> const&,
58  std::unique_ptr<GlobalVector>&>>,
59  "The function eval_field_ does not return a const reference"
60  " to a GlobalVector");
61 
62  static_assert(
63  std::is_same_v<
64  GlobalVector const&,
65  typename std::invoke_result_t<
66  F2, double const, std::vector<GlobalVector*> const&,
67  std::vector<NumLib::LocalToGlobalIndexMap const*> const&,
68  std::unique_ptr<GlobalVector>&>>,
69  "The function eval_residuals_ does not return a const reference"
70  " to a GlobalVector");
71  }
72 
73  template <typename F1>
74  SecondaryVariableFunctions(const unsigned num_components_, F1&& eval_field_,
75  std::nullptr_t)
76  : num_components(num_components_),
77  eval_field(std::forward<F1>(eval_field_))
78  {
79  // Used to detect nasty implicit conversions.
80  static_assert(
81  std::is_same_v<
82  GlobalVector const&,
83  typename std::invoke_result_t<
84  F1, double const, std::vector<GlobalVector*> const&,
85  std::vector<NumLib::LocalToGlobalIndexMap const*> const&,
86  std::unique_ptr<GlobalVector>&>>,
87  "The function eval_field_ does not return a const reference"
88  " to a GlobalVector");
89  }
90 
91  const unsigned num_components;
92 
95 
101 };
102 
104 struct SecondaryVariable final
105 {
106  std::string const name;
107 
110 };
111 
114 {
115 public:
117  void addNameMapping(std::string const& internal_name,
118  std::string const& external_name);
119 
131  void addSecondaryVariable(std::string const& internal_name,
133 
135  SecondaryVariable const& get(std::string const& external_name) const;
136 
137  std::map<std::string, std::string>::const_iterator begin() const;
138  std::map<std::string, std::string>::const_iterator end() const;
139 
140 private:
143  std::map<std::string, std::string> _map_external_to_internal;
144 
148  std::map<std::string, SecondaryVariable> _configured_secondary_variables;
149 };
150 
162 template <typename LocalAssemblerCollection>
164  const unsigned num_components,
165  NumLib::Extrapolator& extrapolator,
166  LocalAssemblerCollection const& local_assemblers,
168  LocalAssemblerCollection>::IntegrationPointValuesMethod
169  integration_point_values_method)
170 {
171  auto const eval_field =
172  [num_components, &extrapolator, &local_assemblers,
173  integration_point_values_method](
174  const double t,
175  std::vector<GlobalVector*> const& x,
176  std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_table,
177  std::unique_ptr<GlobalVector> & /*result_cache*/
178  ) -> GlobalVector const& {
179  auto const extrapolatables = NumLib::makeExtrapolatable(
180  local_assemblers, integration_point_values_method);
181  extrapolator.extrapolate(num_components, extrapolatables, t, x,
182  dof_table);
183  return extrapolator.getNodalValues();
184  };
185 
186  auto const eval_residuals =
187  [num_components, &extrapolator, &local_assemblers,
188  integration_point_values_method](
189  const double t,
190  std::vector<GlobalVector*> const& x,
191  std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_table,
192  std::unique_ptr<GlobalVector> & /*result_cache*/
193  ) -> GlobalVector const& {
194  auto const extrapolatables = NumLib::makeExtrapolatable(
195  local_assemblers, integration_point_values_method);
196  extrapolator.calculateResiduals(num_components, extrapolatables, t, x,
197  dof_table);
198  return extrapolator.getElementResiduals();
199  };
200  return {num_components, eval_field, eval_residuals};
201 }
202 
203 } // namespace ProcessLib
Global vector based on Eigen vector.
Definition: EigenVector.h:26
virtual GlobalVector const & getElementResiduals() const =0
virtual GlobalVector const & getNodalValues() const =0
virtual void extrapolate(const unsigned num_components, ExtrapolatableElementCollection const &extrapolatables, const double t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table)=0
Extrapolates the given property from the given local assemblers.
virtual void calculateResiduals(const unsigned num_components, ExtrapolatableElementCollection const &extrapolatables, const double t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table)=0
Handles configuration of several secondary variables from the project file.
SecondaryVariable const & get(std::string const &external_name) const
Returns the secondary variable with the given external name.
std::map< std::string, std::string > _map_external_to_internal
void addNameMapping(std::string const &internal_name, std::string const &external_name)
Register a variable with the given internal and external names.
std::map< std::string, std::string >::const_iterator begin() const
void addSecondaryVariable(std::string const &internal_name, SecondaryVariableFunctions &&fcts)
std::map< std::string, std::string >::const_iterator end() const
std::map< std::string, SecondaryVariable > _configured_secondary_variables
ExtrapolatableLocalAssemblerCollection< LocalAssemblerCollection > makeExtrapolatable(LocalAssemblerCollection const &local_assemblers, IntegrationPointValuesMethod integration_point_values_method)
SecondaryVariableFunctions makeExtrapolator(const unsigned num_components, NumLib::Extrapolator &extrapolator, LocalAssemblerCollection const &local_assemblers, typename NumLib::ExtrapolatableLocalAssemblerCollection< LocalAssemblerCollection >::IntegrationPointValuesMethod integration_point_values_method)
SecondaryVariableFunctions(const unsigned num_components_, F1 &&eval_field_, F2 &&eval_residuals_)
SecondaryVariableFunctions(const unsigned num_components_, F1 &&eval_field_, std::nullptr_t)
Function const eval_field
Computes the value of the field at every node of the underlying mesh.
const unsigned num_components
Number of components of the variable.
std::function< GlobalVector const &(const double t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table, std::unique_ptr< GlobalVector > &result_cache)> Function
Stores information about a specific secondary variable.
SecondaryVariableFunctions fcts
Functions used for computing the secondary variable.
std::string const name
Name of the variable; used, e.g., for output.