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