Flow123d  JS_before_hm-1626-gde32303
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
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 /// Declaration of exception.
102 TYPEDEF_ERR_INFO(EI_Field, std::string);
103 DECLARE_INPUT_EXCEPTION(ExcUndefElementValue,
104  << "Values of some elements of FieldFE " << EI_Field::qval << " is undefined.\n"
105  << "Please specify in default_value key.\n");
106 
107 
108 /**
109  * Base class for space-time function classes.
110  */
111 template <int spacedim, class Value>
113 public:
114  // expose template parameters
115  typedef typename Space<spacedim>::Point Point;
116  static const unsigned int spacedim_=spacedim;
117  static constexpr bool is_enum_valued = std::is_same<typename Value::element_type, FieldEnum>::value;
118 
119 
120  TYPEDEF_ERR_INFO( EI_Field, std::string);
121  DECLARE_EXCEPTION( ExcInputInitUnsupported, << "The field " << EI_Field::qval << " do not support initialization from input.\n" );
122 
123  /**
124  * Kind of default constructor , with possible setting of the initial time.
125  * Fields that returns variable size vectors accepts number of components @p n_comp.
126  */
127  FieldAlgorithmBase(unsigned int n_comp=0);
128 
129  /**
130  * Returns template parameters as string in order to distinguish name of Abstracts
131  * for initialization of different instances of the FieldBase template.
132  */
133  static std::string template_name();
134 
135  /**
136  * Returns whole tree of input types for FieldBase with all descendants based on element input type (namely for FieldConstant)
137  * given by element_input_type pointer.
138  */
139  static Input::Type::Abstract & get_input_type();
140 
141  /**
142  * Returns parameterized whole tree of input types for FieldBase with all descendants based on element input type (namely
143  * for FieldConstant) given by element_input_type pointer.
144  */
145  static const Input::Type::Instance & get_input_type_instance( Input::Type::Selection value_selection=Input::Type::Selection() );
146 
147  /**
148  * Returns auxiliary record with keys common to all field algorithms.
149  */
150  static const Input::Type::Record & get_field_algo_common_keys();
151 
152  /**
153  * This static method gets accessor to abstract record with function input,
154  * dispatch to correct constructor and initialize appropriate function object from the input.
155  * Returns shared pointer to FunctionBase<>.
156  */
157  static std::shared_ptr< FieldAlgorithmBase<spacedim, Value> >
158  function_factory(const Input::AbstractRecord &rec, const struct FieldAlgoBaseInitData& init_data);
159 
160  /**
161  * Function can provide way to initialize itself from the input data.
162  *
163  * TODO: make protected, should be called through function factory
164  */
165  virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
166 
167  /**
168  * Set new time value. Some Fields may and some may not implement time dependent values and
169  * possibly various types of interpolation. There can not be unified approach to interpolation (at least not on this abstraction level)
170  * since some fields (FieldFormula, FieldPython) provides naturally time dependent functions other fields like (FieldConstant, ...), however,
171  * can be equipped by various time interpolation schemes. In future, we obviously need time interpolation of higher order so that
172  * we can use ODE integrators of higher order.
173  *
174  * The method returns true if the value of the field has changed in the new time step.
175  */
176  virtual bool set_time(const TimeStep &time);
177 
178  /**
179  * Is used only by some Field implementations, but can be used to check validity of incoming ElementAccessor in value methods.
180  *
181  * Optional parameter @p boundary_domain can be used to specify, that the field will be evaluated only on the boundary part of the mesh.
182  * TODO: make separate mesh for the boundary, then we can drop this parameter.
183  */
184  virtual void set_mesh(const Mesh *mesh, bool boundary_domain);
185 
186  /**
187  * Sets @p component_idx_
188  */
189  void set_component_idx(unsigned int idx)
190  { this->component_idx_ = idx; }
191 
192  /**
193  * Returns number of rows, i.e. number of components for variable size vectors. For values of fixed size returns zero.
194  */
195  unsigned int n_comp() const;
196 
197  /**
198  * Special field values spatially constant. Could allow optimization of tensor multiplication and
199  * tensor or vector addition. field_result_ should be set in constructor and in set_time method of particular Field implementation.
200  */
202  { return field_result_;}
203 
204  /**
205  * Method for getting some information about next time where the function change its character.
206  * Used to add appropriate TimeMarks.
207  * TODO: think what kind of information we may need, is the next time value enough?
208  */
209  virtual double next_change_time()
210  { ASSERT(false).error("Not implemented yet."); return 0.0; }
211 
212  /**
213  * Returns one value in one given point @p on an element given by ElementAccessor @p elm.
214  * It returns reference to he actual value in order to avoid temporaries for vector and tensor values.
215  *
216  * This method just call the later one @p value(Point, ElementAccessor, Value) and drops the FieldResult.
217  *
218  * Usual implementation of this method fills @p member r_value_ through unified envelope @p value_ as general tensor
219  * and then returns member @p r_value_. However, some particular Fields may have result stored elsewhere, in such a case
220  * the reference to the result can be returned directly without using the member @p value_. Keeping such wide space for optimization
221  * has drawback in slow generic implementation of the @p value_list method that fills whole vector of values for vector of points.
222  * Its generic implementation has to copy all values instead of directly store them into the vector of result values.
223  *
224  * So the best practice when implementing @p value and @value_list methods in particular FieldBase descendant is
225  * implement some thing like value(point, elm, Value::return_type &value) and using
226  * s having in part
227  *
228  */
229  virtual typename Value::return_type const &value(const Point &p, const ElementAccessor<spacedim> &elm)=0;
230 
231  /**
232  * Returns std::vector of scalar values in several points at once. The base class implements
233  * trivial implementation using the @p value(,,) method. This is not optimal as it involves lot of virtual calls,
234  * but this overhead can be negligible for more complex fields as Python of Formula.
235  *
236  * FieldAlgorithmBase provides a slow implementation using the value() method. Derived Field can implement its value_list method
237  * as call of FieldAlgoritmBase<...>::value_list().
238  */
239  virtual void value_list(const Armor::array &point_list, const ElementAccessor<spacedim> &elm,
241 
242  /// Allows reinit data members or structures in descendants during reinit of FieldValueCache of 'parental' Field<>
243  virtual void cache_reinit(const ElementCacheMap &cache_map);
244 
245  virtual void cache_update(FieldValueCache<typename Value::element_type> &data_cache,
246  ElementCacheMap &cache_map, unsigned int region_patch_idx);
247 
248  /**
249  * Postponed setter of Dof handler for FieldFE. For other types of fields has no effect.
250  */
251  virtual void set_native_dh(std::shared_ptr<DOFHandlerMultiDim>)
252  {}
253 
254  /**
255  * Return true if field is only dependent on time.
256  */
257  inline bool is_constant_in_space() const {
258  return is_constant_in_space_;
259  }
260 
261  /**
262  * Set reference of FieldSet to FieldFormula instance.
263  */
266  }
267 
268  /**
269  * Virtual destructor.
270  */
271  virtual ~FieldAlgorithmBase() {}
272 
273 
274 protected:
275  /// Init value of @p unit_conversion_coefficient_ from input
276  void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
277  /// Actual time level; initial value is -infinity.
279  /// Last value, prevents passing large values (vectors) by value.
281  typename Value::return_type r_value_;
282  /// Indicator of particular values (zero, one) constant over space.
284  /// Specify if the field is part of a MultiField and which component it is
285  unsigned int component_idx_;
286  /// Coeficient of conversion of user-defined unit
288  /// Flag detects that field is only dependent on time
290 };
291 
292 
293 #endif /* FUNCTION_BASE_HH_ */
virtual std::vector< const FieldCommon * > set_dependency(FMT_UNUSED FieldSet &field_set)
TimeStep time_
Actual time level; initial value is -infinity.
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:74
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:159
virtual double next_change_time()
TYPEDEF_ERR_INFO(EI_KeyName, const string)
unsigned int component_idx_
Specify if the field is part of a MultiField and which component it is.
Armor::ArmaVec< double, spacedim > Point
Definition: point.hh:42
FieldResult field_result_
Indicator of particular values (zero, one) constant over space.
Abstract linear system class.
Definition: balance.hh:40
FieldFlag::Flags flags_
Definitions of ASSERTS.
Directing class of FieldValueCache.
Definition: mesh.h:77
Helper class that stores data of generic types.
Definition: type_generic.hh:89
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
virtual void set_native_dh(std::shared_ptr< DOFHandlerMultiDim >)
Value::return_type r_value_
Basic time management class.
static constexpr bool value
Definition: json.hpp:87
double unit_conversion_coefficient_
Coeficient of conversion of user-defined unit.
bool is_constant_in_space() const
#define FMT_UNUSED
Definition: posix.h:75
FieldResult field_result() const
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
Provides the numbering of the finite element degrees of freedom on the computational mesh...
Definition: dofhandler.hh:151
FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si)
Simplified constructor, set limit values automatically (used in unit tests)
Space< spacedim >::Point Point
Class for declaration of polymorphic Record.
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
FieldResult
const UnitSI & unit_si_
bool is_constant_in_space_
Flag detects that field is only dependent on time.
Value value_
Last value, prevents passing large values (vectors) by value.
virtual ~FieldAlgorithmBase()
DECLARE_EXCEPTION(ExcWrongDefaultJSON,<< "Consistency Error: Not valid JSON of Default value "<< EI_DefaultStr::qval<< " of type "<< EI_TypeName::qval<< ";\n"<< "During declaration of the key: "<< EI_KeyName::qval)
void set_component_idx(unsigned int idx)
std::pair< double, double > limits_
Record type proxy class.
Definition: type_record.hh:182
FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si, std::pair< double, double > limits, FieldFlag::Flags flags)
Full constructor.
Class for representation SI units of Fields.
Definition: unit_si.hh:40
DECLARE_INPUT_EXCEPTION(ExcInputMessage,<< EI_Message::val)
Simple input exception that accepts just string message.
Representation of one time step..
Template for classes storing finite set of named values.