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