Flow123d  master-94c4283
field_algo_base.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file field_algo_base.hh
15  * @brief
16  * @todo
17  * - better tests:
18  * - common set of quantities with different kind of values (scalar, vector, tensor, discrete, ..),
19  * common points and elements for evaluation
20  * - for individual Field implementations have:
21  * - different input
22  * - possibly different EPETCT_EQ tests, but rather have majority common
23  */
24 
25 #ifndef field_algo_base_HH_
26 #define field_algo_base_HH_
27 
28 #include <string.h> // for memcpy
29 #include <type_traits> // for is_same
30 #include <limits> // for numeric_limits
31 #include <memory> // for shared_ptr
32 #include <ostream> // for operator<<
33 #include <string> // for string
34 #include <utility> // for make_pair, pair
35 #include <vector> // for vector
36 #include <armadillo> // for operator%, operator<<
37 #include "fields/field_values.hh" // for FieldValue<>::Enum, FieldV...
38 #include "fields/field_flag.hh"
40 #include "input/type_selection.hh" // for Selection
41 #include "mesh/point.hh" // for Space
42 #include "mesh/accessors.hh"
43 #include "system/asserts.hh" // for Assert, ASSERT_PERMANENT
44 #include "tools/time_governor.hh" // for TimeStep
45 
46 class Mesh;
47 class UnitSI;
48 class DOFHandlerMultiDim;
49 class FieldSet;
50 class FieldCommon;
51 namespace Input {
52  class AbstractRecord;
53  class Record;
54  namespace Type {
55  class Abstract;
56  class Instance;
57  class Record;
58  }
59 }
60 template <int spacedim> class ElementAccessor;
61 
62 
63 
64 /**
65  * Indication of special field states. Returned by Field<>::field_result.
66  * Individual states have values corresponding to week ordering of the states according
67  * to the exactness of the value. May possibly be helpful in implementation, e.g.
68  * one can use (field_result >= result_constant) to check that the field is constant on given region.
69  */
70 typedef enum {
71  result_none=0, // field not set
72  result_other=1, // field initialized but no particular result information
73  result_constant=2, // spatially constant result
74  result_zeros=10, // zero scalar, vector, or tensor
75  result_ones=20, // all elements equal to 1.0
76  result_eye=21 // identity tensor
77 
78 } FieldResult;
79 
80 /// Helper struct stores data for initizalize descentants of \p FieldAlgorithmBase.
82  /// Full constructor
83  FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si, std::pair<double, double> limits, FieldFlag::Flags flags)
84  : field_name_(field_name), n_comp_(n_comp), unit_si_(unit_si), limits_(limits), flags_(flags) {}
85  /// Simplified constructor, set limit values automatically (used in unit tests)
86  FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si)
87  : field_name_(field_name), n_comp_(n_comp), unit_si_(unit_si),
88  limits_( std::make_pair(-std::numeric_limits<double>::max(), std::numeric_limits<double>::max()) ),
89  flags_(FieldFlag::declare_input & FieldFlag::equation_input & FieldFlag::allow_output) {}
90 
91  std::string field_name_;
92  unsigned int n_comp_;
93  const UnitSI &unit_si_;
94  std::pair<double, double> limits_;
96 };
97 
98 
99 
100 
101 /**
102  * Base class for space-time function classes.
103  */
104 template <int spacedim, class Value>
106 public:
107  // expose template parameters
108  typedef typename Space<spacedim>::Point Point;
109  static const unsigned int spacedim_=spacedim;
111 
112 
113  TYPEDEF_ERR_INFO( EI_Field, std::string);
114  DECLARE_EXCEPTION( ExcInputInitUnsupported, << "The field " << EI_Field::qval << " do not support initialization from input.\n" );
115 
116  /**
117  * Kind of default constructor , with possible setting of the initial time.
118  * Fields that returns variable size vectors accepts number of components @p n_comp.
119  */
120  FieldAlgorithmBase(unsigned int n_comp=0);
121 
122  /**
123  * Returns template parameters as string in order to distinguish name of Abstracts
124  * for initialization of different instances of the FieldBase template.
125  */
126  static std::string template_name();
127 
128  /**
129  * Returns whole tree of input types for FieldBase with all descendants based on element input type (namely for FieldConstant)
130  * given by element_input_type pointer.
131  */
133 
134  /**
135  * Returns parameterized whole tree of input types for FieldBase with all descendants based on element input type (namely
136  * for FieldConstant) given by element_input_type pointer.
137  */
139 
140  /**
141  * Returns auxiliary record with keys common to all field algorithms.
142  */
144 
145  /**
146  * This static method gets accessor to abstract record with function input,
147  * dispatch to correct constructor and initialize appropriate function object from the input.
148  * Returns shared pointer to FunctionBase<>.
149  */
150  static std::shared_ptr< FieldAlgorithmBase<spacedim, Value> >
151  function_factory(const Input::AbstractRecord &rec, const struct FieldAlgoBaseInitData& init_data);
152 
153  /**
154  * Function can provide way to initialize itself from the input data.
155  *
156  * TODO: make protected, should be called through function factory
157  */
158  virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
159 
160  /**
161  * Set new time value. Some Fields may and some may not implement time dependent values and
162  * possibly various types of interpolation. There can not be unified approach to interpolation (at least not on this abstraction level)
163  * since some fields (FieldFormula, FieldPython) provides naturally time dependent functions other fields like (FieldConstant, ...), however,
164  * can be equipped by various time interpolation schemes. In future, we obviously need time interpolation of higher order so that
165  * we can use ODE integrators of higher order.
166  *
167  * The method returns true if the value of the field has changed in the new time step.
168  */
169  virtual bool set_time(const TimeStep &time);
170 
171  /**
172  * Is used only by some Field implementations, but can be used to check validity of incoming ElementAccessor in value methods.
173  *
174  * Optional parameter @p boundary_domain can be used to specify, that the field will be evaluated only on the boundary part of the mesh.
175  * TODO: make separate mesh for the boundary, then we can drop this parameter.
176  */
177  virtual void set_mesh(const Mesh *mesh, bool boundary_domain);
178 
179  /**
180  * Sets @p component_idx_
181  */
182  void set_component_idx(unsigned int idx)
183  { this->component_idx_ = idx; }
184 
185  /**
186  * Returns number of rows, i.e. number of components for variable size vectors. For values of fixed size returns zero.
187  */
188  unsigned int n_comp() const;
189 
190  /**
191  * Special field values spatially constant. Could allow optimization of tensor multiplication and
192  * tensor or vector addition. field_result_ should be set in constructor and in set_time method of particular Field implementation.
193  */
195  { return field_result_;}
196 
197  /**
198  * Method for getting some information about next time where the function change its character.
199  * Used to add appropriate TimeMarks.
200  * TODO: think what kind of information we may need, is the next time value enough?
201  */
202  virtual double next_change_time()
203  { ASSERT_PERMANENT(false).error("Not implemented yet."); return 0.0; }
204 
205  /// Allows reinit data members or structures in descendants during reinit of FieldValueCache of 'parental' Field<>
206  virtual void cache_reinit(const ElementCacheMap &cache_map);
207 
209  ElementCacheMap &cache_map, unsigned int region_patch_idx);
210 
211  /**
212  * Postponed setter of Dof handler for FieldFE. For other types of fields has no effect.
213  */
214  virtual void set_native_dh(std::shared_ptr<DOFHandlerMultiDim>)
215  {}
216 
217  /**
218  * Return true if field is only dependent on time.
219  */
220  inline bool is_constant_in_space() const {
221  return is_constant_in_space_;
222  }
223 
224  /**
225  * Set reference of FieldSet to FieldFormula instance.
226  */
229  }
230 
231  /**
232  * Virtual destructor.
233  */
234  virtual ~FieldAlgorithmBase() {}
235 
236 
237 protected:
238  /// Init value of @p unit_conversion_coefficient_ from input
239  void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
240  /// Actual time level; initial value is -infinity.
242  /// Last value, prevents passing large values (vectors) by value.
244  typename Value::return_type r_value_;
245  /// Indicator of particular values (zero, one) constant over space.
247  /// Specify if the field is part of a MultiField and which component it is
248  unsigned int component_idx_;
249  /// Coeficient of conversion of user-defined unit
251  /// Flag detects that field is only dependent on time
253 };
254 
255 
256 #endif /* FUNCTION_BASE_HH_ */
result_none
@ result_none
Definition: field_algo_base.hh:71
Space::Point
Armor::ArmaVec< double, spacedim > Point
Definition: point.hh:42
result_zeros
@ result_zeros
Definition: field_algo_base.hh:74
FieldAlgorithmBase::n_comp
unsigned int n_comp() const
Definition: field_algo_base.impl.hh:133
FieldAlgorithmBase::unit_conversion_coefficient_
double unit_conversion_coefficient_
Coeficient of conversion of user-defined unit.
Definition: field_algo_base.hh:250
FieldAlgorithmBase::field_result
FieldResult field_result() const
Definition: field_algo_base.hh:194
time_governor.hh
Basic time management class.
FieldAlgorithmBase::set_component_idx
void set_component_idx(unsigned int idx)
Definition: field_algo_base.hh:182
FlagArray< FieldFlag >
FieldAlgoBaseInitData::field_name_
std::string field_name_
Definition: field_algo_base.hh:91
FieldAlgorithmBase::~FieldAlgorithmBase
virtual ~FieldAlgorithmBase()
Definition: field_algo_base.hh:234
Input
Abstract linear system class.
Definition: balance.hh:40
string.h
FieldAlgorithmBase::get_field_algo_common_keys
static const Input::Type::Record & get_field_algo_common_keys()
Definition: field_algo_base.impl.hh:91
point.hh
ElementCacheMap
Directing class of FieldValueCache.
Definition: field_value_cache.hh:152
FieldAlgorithmBase::cache_reinit
virtual void cache_reinit(const ElementCacheMap &cache_map)
Allows reinit data members or structures in descendants during reinit of FieldValueCache of 'parental...
Definition: field_algo_base.impl.hh:149
FieldAlgorithmBase::get_input_type_instance
static const Input::Type::Instance & get_input_type_instance(Input::Type::Selection value_selection=Input::Type::Selection())
Definition: field_algo_base.impl.hh:78
asserts.hh
Definitions of ASSERTS.
value
static constexpr bool value
Definition: json.hpp:87
FieldResult
FieldResult
Definition: field_algo_base.hh:70
result_other
@ result_other
Definition: field_algo_base.hh:72
FieldAlgoBaseInitData::unit_si_
const UnitSI & unit_si_
Definition: field_algo_base.hh:93
FieldAlgorithmBase::is_constant_in_space_
bool is_constant_in_space_
Flag detects that field is only dependent on time.
Definition: field_algo_base.hh:252
std::vector< const FieldCommon * >
ElementAccessor
Definition: dh_cell_accessor.hh:32
FieldFlag
Definition: field_flag.hh:23
FieldAlgorithmBase::get_input_type
static Input::Type::Abstract & get_input_type()
Definition: field_algo_base.impl.hh:66
type_selection.hh
FieldAlgorithmBase::Point
Space< spacedim >::Point Point
Definition: field_algo_base.hh:108
field_flag.hh
result_constant
@ result_constant
Definition: field_algo_base.hh:73
FieldAlgoBaseInitData::n_comp_
unsigned int n_comp_
Definition: field_algo_base.hh:92
FieldAlgorithmBase::spacedim_
static const unsigned int spacedim_
Definition: field_algo_base.hh:109
FieldAlgoBaseInitData::FieldAlgoBaseInitData
FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si, std::pair< double, double > limits, FieldFlag::Flags flags)
Full constructor.
Definition: field_algo_base.hh:83
accessors.hh
FieldAlgorithmBase::FieldAlgorithmBase
FieldAlgorithmBase(unsigned int n_comp=0)
Definition: field_algo_base.impl.hh:47
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
FieldAlgorithmBase::cache_update
virtual void cache_update(FieldValueCache< typename Value::element_type > &data_cache, ElementCacheMap &cache_map, unsigned int region_patch_idx)
Definition: field_algo_base.impl.hh:139
ASSERT_PERMANENT
#define ASSERT_PERMANENT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:348
FieldAlgorithmBase::component_idx_
unsigned int component_idx_
Specify if the field is part of a MultiField and which component it is.
Definition: field_algo_base.hh:248
DOFHandlerMultiDim
Provides the numbering of the finite element degrees of freedom on the computational mesh.
Definition: dofhandler.hh:151
FieldAlgoBaseInitData::limits_
std::pair< double, double > limits_
Definition: field_algo_base.hh:94
FieldAlgoBaseInitData
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
Definition: field_algo_base.hh:81
FieldAlgorithmBase::init_from_input
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Definition: field_algo_base.impl.hh:112
TimeStep
Representation of one time step..
Definition: time_governor.hh:123
FieldAlgorithmBase::value_
Value value_
Last value, prevents passing large values (vectors) by value.
Definition: field_algo_base.hh:243
FieldAlgorithmBase::init_unit_conversion_coefficient
void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Init value of unit_conversion_coefficient_ from input.
Definition: field_algo_base.impl.hh:154
field_values.hh
Input::AbstractRecord
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
FieldAlgorithmBase::set_dependency
virtual std::vector< const FieldCommon * > set_dependency(FMT_UNUSED FieldSet &field_set)
Definition: field_algo_base.hh:227
FieldCommon
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:77
Input::Type::Abstract
Class for declaration of polymorphic Record.
Definition: type_abstract.hh:62
UnitSI
Class for representation SI units of Fields.
Definition: unit_si.hh:40
FieldAlgorithmBase::next_change_time
virtual double next_change_time()
Definition: field_algo_base.hh:202
result_ones
@ result_ones
Definition: field_algo_base.hh:75
Input::Type::Instance
Helper class that stores data of generic types.
Definition: type_generic.hh:89
FieldAlgorithmBase::set_mesh
virtual void set_mesh(const Mesh *mesh, bool boundary_domain)
Definition: field_algo_base.impl.hh:127
FieldSet
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:159
Input::Type::Selection
Template for classes storing finite set of named values.
Definition: type_selection.hh:65
FieldAlgorithmBase::template_name
static std::string template_name()
Definition: field_algo_base.impl.hh:59
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
Value
@ Value
Definition: finite_element.hh:43
FieldAlgoBaseInitData::flags_
FieldFlag::Flags flags_
Definition: field_algo_base.hh:95
FieldAlgorithmBase::field_result_
FieldResult field_result_
Indicator of particular values (zero, one) constant over space.
Definition: field_algo_base.hh:246
Mesh
Definition: mesh.h:362
FieldAlgorithmBase::TYPEDEF_ERR_INFO
TYPEDEF_ERR_INFO(EI_Field, std::string)
FieldAlgorithmBase
Definition: field_algo_base.hh:105
std
Definition: doxy_dummy_defs.hh:5
field_value_cache.hh
Armor::Array
Definition: armor.hh:597
FieldAlgorithmBase::time_
TimeStep time_
Actual time level; initial value is -infinity.
Definition: field_algo_base.hh:241
FieldAlgorithmBase::r_value_
Value::return_type r_value_
Definition: field_algo_base.hh:244
FieldAlgorithmBase::set_native_dh
virtual void set_native_dh(std::shared_ptr< DOFHandlerMultiDim >)
Definition: field_algo_base.hh:214
FieldAlgorithmBase::is_enum_valued
static constexpr bool is_enum_valued
Definition: field_algo_base.hh:110
FieldAlgorithmBase::is_constant_in_space
bool is_constant_in_space() const
Definition: field_algo_base.hh:220
FieldAlgorithmBase::DECLARE_EXCEPTION
DECLARE_EXCEPTION(ExcInputInitUnsupported,<< "The field "<< EI_Field::qval<< " do not support initialization from input.\n")
FieldAlgoBaseInitData::FieldAlgoBaseInitData
FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si)
Simplified constructor, set limit values automatically (used in unit tests)
Definition: field_algo_base.hh:86
FieldAlgorithmBase::set_time
virtual bool set_time(const TimeStep &time)
Definition: field_algo_base.impl.hh:119
FieldAlgorithmBase::function_factory
static std::shared_ptr< FieldAlgorithmBase< spacedim, Value > > function_factory(const Input::AbstractRecord &rec, const struct FieldAlgoBaseInitData &init_data)
Definition: field_algo_base.impl.hh:101
result_eye
@ result_eye
Definition: field_algo_base.hh:76
FMT_UNUSED
#define FMT_UNUSED
Definition: posix.h:75