OGS
SecondaryVariable.h
Go to the documentation of this file.
1
11#pragma once
12
13#include "BaseLib/Algorithm.h"
17
18namespace NumLib
19{
20class LocalToGlobalIndexMap;
21}
22
23namespace ProcessLib
24{
28{
39 using Function = std::function<GlobalVector const&(
40 const double t,
41 std::vector<GlobalVector*> const& x,
42 std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_table,
43 std::unique_ptr<GlobalVector>& result_cache)>;
44
45 template <typename F1, typename F2>
46 SecondaryVariableFunctions(const unsigned num_components_, F1&& eval_field_,
47 F2&& eval_residuals_)
48 : num_components(num_components_),
49 eval_field(std::forward<F1>(eval_field_)),
50 eval_residuals(std::forward<F2>(eval_residuals_))
51 {
52 // Used to detect nasty implicit conversions.
53 static_assert(
54 std::is_same_v<
55 GlobalVector const&,
56 typename std::invoke_result_t<
57 F1, double const, std::vector<GlobalVector*> const&,
58 std::vector<NumLib::LocalToGlobalIndexMap const*> const&,
59 std::unique_ptr<GlobalVector>&>>,
60 "The function eval_field_ does not return a const reference"
61 " to a GlobalVector");
62
63 static_assert(
64 std::is_same_v<
65 GlobalVector const&,
66 typename std::invoke_result_t<
67 F2, double const, std::vector<GlobalVector*> const&,
68 std::vector<NumLib::LocalToGlobalIndexMap const*> const&,
69 std::unique_ptr<GlobalVector>&>>,
70 "The function eval_residuals_ does not return a const reference"
71 " to a GlobalVector");
72 }
73
74 template <typename F1>
75 SecondaryVariableFunctions(const unsigned num_components_, F1&& eval_field_,
76 std::nullptr_t)
77 : num_components(num_components_),
78 eval_field(std::forward<F1>(eval_field_))
79 {
80 // Used to detect nasty implicit conversions.
81 static_assert(
82 std::is_same_v<
83 GlobalVector const&,
84 typename std::invoke_result_t<
85 F1, double const, std::vector<GlobalVector*> const&,
86 std::vector<NumLib::LocalToGlobalIndexMap const*> const&,
87 std::unique_ptr<GlobalVector>&>>,
88 "The function eval_field_ does not return a const reference"
89 " to a GlobalVector");
90 }
91
92 const unsigned num_components;
93
96
102};
103
106{
107 std::string const name;
108
111};
112
115{
116public:
118 void addNameMapping(std::string const& internal_name,
119 std::string const& external_name);
120
132 void addSecondaryVariable(std::string const& internal_name,
134
136 SecondaryVariable const& get(std::string const& external_name) const;
137
138 std::map<std::string, std::string>::const_iterator begin() const;
139 std::map<std::string, std::string>::const_iterator end() const;
140
141private:
144 std::map<std::string, std::string> _map_external_to_internal;
145
149 std::map<std::string, SecondaryVariable> _configured_secondary_variables;
150};
151
163template <typename LocalAssemblerCollection>
165 const unsigned num_components,
166 NumLib::Extrapolator& extrapolator,
167 LocalAssemblerCollection const& local_assemblers,
169 LocalAssemblerCollection>::IntegrationPointValuesMethod
170 integration_point_values_method)
171{
172 auto const eval_field =
173 [num_components, &extrapolator, &local_assemblers,
174 integration_point_values_method](
175 const double t,
176 std::vector<GlobalVector*> const& x,
177 std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_tables,
178 std::unique_ptr<GlobalVector>& /*result_cache*/
179 ) -> GlobalVector const&
180 {
181 auto const extrapolatables = NumLib::makeExtrapolatable(
182 local_assemblers, integration_point_values_method);
183 extrapolator.extrapolate(num_components, extrapolatables, t, x,
184 dof_tables);
185 return extrapolator.getNodalValues();
186 };
187
188 auto const eval_residuals =
189 [num_components, &extrapolator, &local_assemblers,
190 integration_point_values_method](
191 const double t,
192 std::vector<GlobalVector*> const& x,
193 std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_tables,
194 std::unique_ptr<GlobalVector>& /*result_cache*/
195 ) -> GlobalVector const&
196 {
197 auto const extrapolatables = NumLib::makeExtrapolatable(
198 local_assemblers, integration_point_values_method);
199 extrapolator.calculateResiduals(num_components, extrapolatables, t, x,
200 dof_tables);
201 return extrapolator.getElementResiduals();
202 };
203 return {num_components, eval_field, eval_residuals};
204}
205
212template <typename LocalAssemblerCollection, typename IPDataAccessor>
214 const unsigned num_components,
215 NumLib::Extrapolator& extrapolator,
216 LocalAssemblerCollection const& local_assemblers,
217 IPDataAccessor&& accessor)
218{
219 using LocalAssemblerInterface = std::remove_cvref_t<
220 decltype(*std::declval<LocalAssemblerCollection>()[0])>;
221 static_assert(std::is_invocable_r_v<std::vector<double>, IPDataAccessor,
223
224 if (num_components == 1)
225 {
226 auto method_wrapped =
227 [accessor](
228 LocalAssemblerInterface const& loc_asm, const double /*t*/,
229 std::vector<GlobalVector*> const& /*x*/,
230 std::vector<
231 NumLib::LocalToGlobalIndexMap const*> const& /*dof_table*/,
232 std::vector<double>& cache) -> std::vector<double> const&
233 {
234 cache = accessor(loc_asm);
235 return cache;
236 };
237
238 return makeExtrapolator(num_components, extrapolator, local_assemblers,
239 method_wrapped);
240 }
241
242 auto method_wrapped =
243 [accessor, num_components](
244 LocalAssemblerInterface const& loc_asm, const double /*t*/,
245 std::vector<GlobalVector*> const& /*x*/,
246 std::vector<
247 NumLib::LocalToGlobalIndexMap const*> const& /*dof_table*/,
248 std::vector<double>& cache) -> std::vector<double> const&
249 {
250 cache = accessor(loc_asm);
251 transposeInPlace(cache, cache.size() / num_components);
252 return cache;
253 };
254
255 return makeExtrapolator(num_components, extrapolator, local_assemblers,
256 method_wrapped);
257}
258
259} // namespace ProcessLib
Global vector based on Eigen vector.
Definition EigenVector.h:25
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 GlobalVector const & getElementResiduals() const =0
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)
std::vector< double > transposeInPlace(StoreValuesFunction const &store_values_function)
SecondaryVariableFunctions makeExtrapolator2(const unsigned num_components, NumLib::Extrapolator &extrapolator, LocalAssemblerCollection const &local_assemblers, IPDataAccessor &&accessor)
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_)
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
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.
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.