Flow123d  jenkins-Flow123d-linux-release-multijob-282
field_algo_base.hh
Go to the documentation of this file.
1 /*
2  * field_algo_base.hh
3  *
4  * Created on: Aug 31, 2012
5  * Author: jb
6  */
7 
8 /**
9  * TODO:
10  * - better tests:
11  * - common set of quantities with different kind of values (scalar, vector, tensor, discrete, ..),
12  * common points and elements for evaluation
13  * - for individual Field implementations have:
14  * - different input
15  * - possibly different EPETCT_EQ tests, but rather have majority common
16  *
17  *
18  */
19 
20 
21 #ifndef field_algo_base_HH_
22 #define field_algo_base_HH_
23 
24 #include <string>
25 #include <memory>
26 
27 #include <boost/type_traits.hpp>
28 
29 #include "input/input_type.hh"
30 #include "input/accessors.hh"
31 
32 #include "mesh/accessors.hh"
33 #include "mesh/point.hh"
34 #include "fields/field_values.hh"
35 #include "tools/time_governor.hh"
36 
37 
38 
39 /// Result type have sense only for larger Value types like vectors and tensors.
40 typedef enum {
41  result_none, // field not set
42  result_zero, // zero scalar, vector, or tensor
43  result_one, // unit scalar (1.0), identity tensor
45 } FieldResult;
46 
47 
48 
49 
50 /**
51  * Base class for space-time function classes.
52  */
53 template <int spacedim, class Value>
55 public:
56  // expose template parameters
57  typedef typename Space<spacedim>::Point Point;
58  typedef Value ValueType;
59  static const unsigned int spacedim_=spacedim;
60 
61 
62  /**
63  * Kind of default constructor , with possible setting of the initial time.
64  * Fields that returns variable size vectors accepts number of components @p n_comp.
65  */
66  FieldAlgorithmBase(unsigned int n_comp=0);
67 
68  /**
69  * Returns template parameters as string in order to distinguish name of AbstractRecords
70  * for initialization of different instances of the FieldBase template.
71  */
72  static std::string template_name();
73 
74  /**
75  * Declaration of input type data member.
76  */
78 
79  /**
80  * Returns whole tree of input types for FieldBase with all descendants based on element input type (namely for FieldConstant)
81  * given by element_input_type pointer. USE ONLY IF YOU CAN NOT USE
82  * static member FieldBase<...>::input_type.
83  */
84  static Input::Type::AbstractRecord get_input_type(const typename Value::ElementInputType *element_input_type=nullptr);
85 
86  /**
87  * This static method gets accessor to abstract record with function input,
88  * dispatch to correct constructor and initialize appropriate function object from the input.
89  * Returns shared pointer to FunctionBase<>.
90  */
91  static std::shared_ptr< FieldAlgorithmBase<spacedim, Value> >
92  function_factory(const Input::AbstractRecord &rec, unsigned int n_comp=0);
93 
94  /**
95  * Function can provide way to initialize itself from the input data.
96  *
97  * TODO: make protected, should be called through function factory
98  */
99  virtual void init_from_input(const Input::Record &rec);
100 
101  /**
102  * Set new time value. Some Fields may and some may not implement time dependent values and
103  * possibly various types of interpolation. There can not be unified approach to interpolation (at least not on this abstraction level)
104  * since some fields (FieldFormula, FieldPython) provides naturally time dependent functions other fields like (FieldConstant, ...), however,
105  * can be equipped by various time interpolation schemes. In future, we obviously need time interpolation of higher order so that
106  * we can use ODE integrators of higher order.
107  *
108  * The method returns true if the value of the field has changed in the new time step.
109  */
110  virtual bool set_time(const TimeStep &time);
111 
112  /**
113  * Is used only by some Field implementations, but can be used to check validity of incoming ElementAccessor in value methods.
114  *
115  * Optional parameter @p boundary_domain can be used to specify, that the field will be evaluated only on the boundary part of the mesh.
116  * TODO: make separate mesh for the boundary, then we can drop this parameter.
117  */
118  virtual void set_mesh(const Mesh *mesh, bool boundary_domain);
119 
120  /**
121  * Sets @p component_idx_
122  */
123  void set_component_idx(unsigned int idx)
124  { this->component_idx_ = idx; }
125 
126  /**
127  * Returns number of rows, i.e. number of components for variable size vectors. For values of fixed size returns zero.
128  */
129  unsigned int n_comp() const;
130 
131  /**
132  * Special field values spatially constant. Could allow optimization of tensor multiplication and
133  * tensor or vector addition. field_result_ should be set in constructor and in set_time method of particular Field implementation.
134  */
136  { return field_result_;}
137 
138  /**
139  * Method for getting some information about next time where the function change its character.
140  * Used to add appropriate TimeMarks.
141  * TODO: think what kind of information we may need, is the next time value enough?
142  */
143  virtual double next_change_time()
144  { ASSERT(0, "Not implemented yet."); return 0.0; }
145 
146  /**
147  * Returns one value in one given point @p on an element given by ElementAccessor @p elm.
148  * It returns reference to he actual value in order to avoid temporaries for vector and tensor values.
149  *
150  * This method just call the later one @p value(Point, ElementAccessor, Value) and drops the FieldResult.
151  *
152  * Usual implementation of this method fills @p member r_value_ through unified envelope @p value_ as general tensor
153  * and then returns member @p r_value_. However, some particular Fields may have result stored elsewhere, in such a case
154  * the reference to the result can be returned directly without using the member @p value_. Keeping such wide space for optimization
155  * has drawback in slow generic implementation of the @p value_list method that fills whole vector of values for vector of points.
156  * Its generic implementation has to copy all values instead of directly store them into the vector of result values.
157  *
158  * So the best practice when implementing @p value and @value_list methods in particular FieldBase descendant is
159  * implement some thing like value(point, elm, Value::return_type &value) and using
160  * s having in part
161  *
162  */
163  virtual typename Value::return_type const &value(const Point &p, const ElementAccessor<spacedim> &elm)=0;
164 
165  /**
166  * Returns std::vector of scalar values in several points at once. The base class implements
167  * trivial implementation using the @p value(,,) method. This is not optimal as it involves lot of virtual calls,
168  * but this overhead can be negligible for more complex fields as Python of Formula.
169  *
170  * FieldAlgorithmBase provides a slow implementation using the value() method. Derived Field can implement its value_list method
171  * as call of FieldAlgoritmBase<...>::value_list().
172  */
173  virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor<spacedim> &elm,
175 
176  /**
177  * Virtual destructor.
178  */
179  virtual ~FieldAlgorithmBase() {}
180 
181 
182 protected:
183  /// Actual time level; initial value is -infinity.
185  /// Last value, prevents passing large values (vectors) by value.
186  Value value_;
187  typename Value::return_type r_value_;
188  /// Indicator of particular values (zero, one) constant over space.
190  /// Specify if the field is part of a MultiField and which component it is
191  unsigned int component_idx_;
192 };
193 
194 
195 #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.
virtual bool set_time(const TimeStep &time)
FieldResult field_result_
Indicator of particular values (zero, one) constant over space.
virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list)=0
Definition: mesh.h:109
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm)=0
Value::return_type r_value_
Basic time management class.
FieldResult field_result() const
#define ASSERT(...)
Definition: global_defs.h:121
static Input::Type::AbstractRecord get_input_type(const typename Value::ElementInputType *element_input_type=nullptr)
arma::vec::fixed< spacedim > Point
Definition: point.hh:23
Accessor to the data with type Type::Record.
Definition: accessors.hh:327
Class for declaration of polymorphic Record.
Definition: type_record.hh:487
static std::shared_ptr< FieldAlgorithmBase< spacedim, Value > > function_factory(const Input::AbstractRecord &rec, unsigned int n_comp=0)
Space< spacedim >::Point Point
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:448
FieldResult
Result type have sense only for larger Value types like vectors and tensors.
FieldAlgorithmBase(unsigned int n_comp=0)
Value value_
Last value, prevents passing large values (vectors) by value.
static Input::Type::AbstractRecord input_type
virtual void set_mesh(const Mesh *mesh, bool boundary_domain)
virtual ~FieldAlgorithmBase()
void set_component_idx(unsigned int idx)
static const unsigned int spacedim_
static std::string template_name()
Representation of one time step.
unsigned int n_comp() const
virtual void init_from_input(const Input::Record &rec)