Flow123d  release_2.2.0-914-gf1a3a4f
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>
29 #include <memory>
30 
31 #include <boost/type_traits.hpp>
32 
35 
36 #include "mesh/accessors.hh"
37 #include "mesh/point.hh"
38 #include "fields/field_values.hh"
39 #include "fields/unit_si.hh"
40 #include "fields/field_flag.hh"
41 #include "tools/time_governor.hh"
42 
43 
44 
45 /**
46  * Indication of special field states. Returned by Field<>::field_result.
47  * Individual states have values corresponding to week ordering of the states according
48  * to the exactness of the value. May possibly be helpful in implementation, e.g.
49  * one can use (field_result >= result_constant) to check that the field is constant on given region.
50  */
51 typedef enum {
52  result_none=0, // field not set
53  result_other=1, // field initialized but no particular result information
54  result_constant=2, // spatially constant result
55  result_zeros=10, // zero scalar, vector, or tensor
56  result_ones=20, // all elements equal to 1.0
57  result_eye=21 // identity tensor
58 
59 } FieldResult;
60 
61 /// Helper struct stores data for initizalize descentants of \p FieldAlgorithmBase.
63  /// Full constructor
64  FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si, std::pair<double, double> limits, FieldFlag::Flags flags)
65  : field_name_(field_name), n_comp_(n_comp), unit_si_(unit_si), limits_(limits), flags_(flags) {}
66  /// Simplified constructor, set limit values automatically (used in unit tests)
67  FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si)
68  : field_name_(field_name), n_comp_(n_comp), unit_si_(unit_si),
69  limits_( std::make_pair(-std::numeric_limits<double>::max(), std::numeric_limits<double>::max()) ),
70  flags_(FieldFlag::declare_input & FieldFlag::equation_input & FieldFlag::allow_output) {}
71 
72  std::string field_name_;
73  unsigned int n_comp_;
74  const UnitSI &unit_si_;
75  std::pair<double, double> limits_;
77 };
78 
79 
80 
81 
82 /**
83  * Base class for space-time function classes.
84  */
85 template <int spacedim, class Value>
87 public:
88  // expose template parameters
89  typedef typename Space<spacedim>::Point Point;
90  static const unsigned int spacedim_=spacedim;
92 
93 
94  /**
95  * Kind of default constructor , with possible setting of the initial time.
96  * Fields that returns variable size vectors accepts number of components @p n_comp.
97  */
98  FieldAlgorithmBase(unsigned int n_comp=0);
99 
100  /**
101  * Returns template parameters as string in order to distinguish name of Abstracts
102  * for initialization of different instances of the FieldBase template.
103  */
104  static std::string template_name();
105 
106  /**
107  * Returns whole tree of input types for FieldBase with all descendants based on element input type (namely for FieldConstant)
108  * given by element_input_type pointer.
109  */
110  static Input::Type::Abstract & get_input_type();
111 
112  /**
113  * Returns parameterized whole tree of input types for FieldBase with all descendants based on element input type (namely
114  * for FieldConstant) given by element_input_type pointer.
115  */
116  static const Input::Type::Instance & get_input_type_instance( Input::Type::Selection value_selection=Input::Type::Selection() );
117 
118  /**
119  * Returns auxiliary record with keys common to all field algorithms.
120  */
121  static const Input::Type::Record & get_field_algo_common_keys();
122 
123  /**
124  * This static method gets accessor to abstract record with function input,
125  * dispatch to correct constructor and initialize appropriate function object from the input.
126  * Returns shared pointer to FunctionBase<>.
127  */
128  static std::shared_ptr< FieldAlgorithmBase<spacedim, Value> >
129  function_factory(const Input::AbstractRecord &rec, const struct FieldAlgoBaseInitData& init_data);
130 
131  /**
132  * Function can provide way to initialize itself from the input data.
133  *
134  * TODO: make protected, should be called through function factory
135  */
136  virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
137 
138  /**
139  * Set new time value. Some Fields may and some may not implement time dependent values and
140  * possibly various types of interpolation. There can not be unified approach to interpolation (at least not on this abstraction level)
141  * since some fields (FieldFormula, FieldPython) provides naturally time dependent functions other fields like (FieldConstant, ...), however,
142  * can be equipped by various time interpolation schemes. In future, we obviously need time interpolation of higher order so that
143  * we can use ODE integrators of higher order.
144  *
145  * The method returns true if the value of the field has changed in the new time step.
146  */
147  virtual bool set_time(const TimeStep &time);
148 
149  /**
150  * Is used only by some Field implementations, but can be used to check validity of incoming ElementAccessor in value methods.
151  *
152  * Optional parameter @p boundary_domain can be used to specify, that the field will be evaluated only on the boundary part of the mesh.
153  * TODO: make separate mesh for the boundary, then we can drop this parameter.
154  */
155  virtual void set_mesh(const Mesh *mesh, bool boundary_domain);
156 
157  /**
158  * Sets @p component_idx_
159  */
160  void set_component_idx(unsigned int idx)
161  { this->component_idx_ = idx; }
162 
163  /**
164  * Returns number of rows, i.e. number of components for variable size vectors. For values of fixed size returns zero.
165  */
166  unsigned int n_comp() const;
167 
168  /**
169  * Special field values spatially constant. Could allow optimization of tensor multiplication and
170  * tensor or vector addition. field_result_ should be set in constructor and in set_time method of particular Field implementation.
171  */
173  { return field_result_;}
174 
175  /**
176  * Method for getting some information about next time where the function change its character.
177  * Used to add appropriate TimeMarks.
178  * TODO: think what kind of information we may need, is the next time value enough?
179  */
180  virtual double next_change_time()
181  { ASSERT(false).error("Not implemented yet."); return 0.0; }
182 
183  /**
184  * Returns one value in one given point @p on an element given by ElementAccessor @p elm.
185  * It returns reference to he actual value in order to avoid temporaries for vector and tensor values.
186  *
187  * This method just call the later one @p value(Point, ElementAccessor, Value) and drops the FieldResult.
188  *
189  * Usual implementation of this method fills @p member r_value_ through unified envelope @p value_ as general tensor
190  * and then returns member @p r_value_. However, some particular Fields may have result stored elsewhere, in such a case
191  * the reference to the result can be returned directly without using the member @p value_. Keeping such wide space for optimization
192  * has drawback in slow generic implementation of the @p value_list method that fills whole vector of values for vector of points.
193  * Its generic implementation has to copy all values instead of directly store them into the vector of result values.
194  *
195  * So the best practice when implementing @p value and @value_list methods in particular FieldBase descendant is
196  * implement some thing like value(point, elm, Value::return_type &value) and using
197  * s having in part
198  *
199  */
200  virtual typename Value::return_type const &value(const Point &p, const ElementAccessor<spacedim> &elm)=0;
201 
202  /**
203  * Returns std::vector of scalar values in several points at once. The base class implements
204  * trivial implementation using the @p value(,,) method. This is not optimal as it involves lot of virtual calls,
205  * but this overhead can be negligible for more complex fields as Python of Formula.
206  *
207  * FieldAlgorithmBase provides a slow implementation using the value() method. Derived Field can implement its value_list method
208  * as call of FieldAlgoritmBase<...>::value_list().
209  */
210  virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor<spacedim> &elm,
212 
213  /**
214  * Virtual destructor.
215  */
216  virtual ~FieldAlgorithmBase() {}
217 
218 
219 protected:
220  /// Init value of @p unit_conversion_coefficient_ from input
221  void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
222  /// Actual time level; initial value is -infinity.
224  /// Last value, prevents passing large values (vectors) by value.
226  typename Value::return_type r_value_;
227  /// Indicator of particular values (zero, one) constant over space.
229  /// Specify if the field is part of a MultiField and which component it is
230  unsigned int component_idx_;
231  /// Coeficient of conversion of user-defined unit
233 };
234 
235 
236 #endif /* FUNCTION_BASE_HH_ */
TimeStep time_
Actual time level; initial value is -infinity.
virtual double next_change_time()
unsigned int component_idx_
Specify if the field is part of a MultiField and which component it is.
FieldResult field_result_
Indicator of particular values (zero, one) constant over space.
FieldFlag::Flags flags_
Definition: mesh.h:99
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:346
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.
FieldResult field_result() const
arma::vec::fixed< spacedim > Point
Definition: point.hh:33
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
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:459
FieldResult
const UnitSI & unit_si_
Value value_
Last value, prevents passing large values (vectors) by value.
virtual ~FieldAlgorithmBase()
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
Representation of one time step..
Template for classes storing finite set of named values.