Flow123d  master-1cc3435
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"
39 #include "fem/element_cache_map.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_PERMANENT
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 struct AssemblyInternals;
52 namespace Input {
53  class AbstractRecord;
54  class Record;
55  namespace Type {
56  class Abstract;
57  class Instance;
58  class Record;
59  }
60 }
61 template <int spacedim> class ElementAccessor;
62 
63 
64 
65 /**
66  * Indication of special field states. Returned by Field<>::field_result.
67  * Individual states have values corresponding to week ordering of the states according
68  * to the exactness of the value. May possibly be helpful in implementation, e.g.
69  * one can use (field_result >= result_constant) to check that the field is constant on given region.
70  */
71 typedef enum {
72  result_none=0, // field not set
73  result_other=1, // field initialized but no particular result information
74  result_constant=2, // spatially constant result
75  result_zeros=10, // zero scalar, vector, or tensor
76  result_ones=20, // all elements equal to 1.0
77  result_eye=21 // identity tensor
78 
80 
81 /// Helper struct stores data for initizalize descentants of \p FieldAlgorithmBase.
83  /// Full constructor
84  FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si, std::pair<double, double> limits, FieldFlag::Flags flags)
85  : field_name_(field_name), n_comp_(n_comp), unit_si_(unit_si), limits_(limits), flags_(flags) {}
86  /// Simplified constructor, set limit values automatically (used in unit tests)
87  FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si)
88  : field_name_(field_name), n_comp_(n_comp), unit_si_(unit_si),
89  limits_( std::make_pair(-std::numeric_limits<double>::max(), std::numeric_limits<double>::max()) ),
90  flags_(FieldFlag::declare_input & FieldFlag::equation_input & FieldFlag::allow_output) {}
91 
92  std::string field_name_;
93  unsigned int n_comp_;
94  const UnitSI &unit_si_;
95  std::pair<double, double> limits_;
97 };
98 
99 
100 
101 
102 /**
103  * Base class for space-time function classes.
104  */
105 template <int spacedim, class Value>
107 public:
108  // expose template parameters
109  typedef typename Space<spacedim>::Point Point;
110  static const unsigned int spacedim_=spacedim;
112 
113 
114  TYPEDEF_ERR_INFO( EI_Field, std::string);
115  DECLARE_EXCEPTION( ExcInputInitUnsupported, << "The field " << EI_Field::qval << " do not support initialization from input.\n" );
116 
117  /**
118  * Kind of default constructor , with possible setting of the initial time.
119  * Fields that returns variable size vectors accepts number of components @p n_comp.
120  */
121  FieldAlgorithmBase(unsigned int n_comp=0);
122 
123  /**
124  * Returns template parameters as string in order to distinguish name of Abstracts
125  * for initialization of different instances of the FieldBase template.
126  */
127  static std::string template_name();
128 
129  /**
130  * Returns whole tree of input types for FieldBase with all descendants based on element input type (namely for FieldConstant)
131  * given by element_input_type pointer.
132  */
134 
135  /**
136  * Returns parameterized whole tree of input types for FieldBase with all descendants based on element input type (namely
137  * for FieldConstant) given by element_input_type pointer.
138  */
140 
141  /**
142  * Returns auxiliary record with keys common to all field algorithms.
143  */
145 
146  /**
147  * This static method gets accessor to abstract record with function input,
148  * dispatch to correct constructor and initialize appropriate function object from the input.
149  * Returns shared pointer to FunctionBase<>.
150  */
151  static std::shared_ptr< FieldAlgorithmBase<spacedim, Value> >
152  function_factory(const Input::AbstractRecord &rec, const struct FieldAlgoBaseInitData& init_data);
153 
154  /**
155  * Function can provide way to initialize itself from the input data.
156  *
157  * TODO: make protected, should be called through function factory
158  */
159  virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
160 
161  /**
162  * Set new time value. Some Fields may and some may not implement time dependent values and
163  * possibly various types of interpolation. There can not be unified approach to interpolation (at least not on this abstraction level)
164  * since some fields (FieldFormula, FieldPython) provides naturally time dependent functions other fields like (FieldConstant, ...), however,
165  * can be equipped by various time interpolation schemes. In future, we obviously need time interpolation of higher order so that
166  * we can use ODE integrators of higher order.
167  *
168  * The method returns true if the value of the field has changed in the new time step.
169  */
170  virtual bool set_time(const TimeStep &time);
171 
172  /**
173  * Is used only by some Field implementations, but can be used to check validity of incoming ElementAccessor in value methods.
174  */
175  virtual void set_mesh(const Mesh *mesh);
176 
177  /**
178  * Sets @p component_idx_
179  */
180  void set_component_idx(unsigned int idx)
181  { this->component_idx_ = idx; }
182 
183  /**
184  * Returns number of rows, i.e. number of components for variable size vectors. For values of fixed size returns zero.
185  */
186  unsigned int n_comp() const;
187 
188  /**
189  * Special field values spatially constant. Could allow optimization of tensor multiplication and
190  * tensor or vector addition. field_result_ should be set in constructor and in set_time method of particular Field implementation.
191  */
193  { return field_result_;}
194 
195  /**
196  * Method for getting some information about next time where the function change its character.
197  * Used to add appropriate TimeMarks.
198  * TODO: think what kind of information we may need, is the next time value enough?
199  */
200  virtual double next_change_time()
201  { ASSERT_PERMANENT(false).error("Not implemented yet."); return 0.0; }
202 
203  /// Allows reinit data members or structures in descendants during reinit of FieldValueCache of 'parental' Field<>
204  virtual void cache_reinit(AssemblyInternals &asm_internals);
205 
207  ElementCacheMap &cache_map, unsigned int region_patch_idx);
208 
209  /**
210  * Postponed setter of Dof handler for FieldFE. For other types of fields has no effect.
211  */
212  virtual void set_native_dh(std::shared_ptr<DOFHandlerMultiDim>)
213  {}
214 
215  /**
216  * Return true if field is only dependent on time.
217  */
218  inline bool is_constant_in_space() const {
219  return is_constant_in_space_;
220  }
221 
222  /**
223  * Set reference of FieldSet to FieldFormula instance.
224  */
227  }
228 
229  /**
230  * Virtual destructor.
231  */
232  virtual ~FieldAlgorithmBase() {}
233 
234 
235 protected:
236  /// Init value of @p unit_conversion_coefficient_ from input
237  void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
238  /// Actual time level; initial value is -infinity.
240  /// Last value, prevents passing large values (vectors) by value.
242  typename Value::return_type r_value_;
243  /// Indicator of particular values (zero, one) constant over space.
245  /// Specify if the field is part of a MultiField and which component it is
246  unsigned int component_idx_;
247  /// Coeficient of conversion of user-defined unit
249  /// Flag detects that field is only dependent on time
251 };
252 
253 
254 #endif /* FUNCTION_BASE_HH_ */
Definitions of ASSERTS.
#define ASSERT_PERMANENT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:348
Provides the numbering of the finite element degrees of freedom on the computational mesh.
Definition: dofhandler.hh:151
Directing class of FieldValueCache.
static const Input::Type::Instance & get_input_type_instance(Input::Type::Selection value_selection=Input::Type::Selection())
virtual double next_change_time()
void set_component_idx(unsigned int idx)
double unit_conversion_coefficient_
Coeficient of conversion of user-defined unit.
static std::shared_ptr< FieldAlgorithmBase< spacedim, Value > > function_factory(const Input::AbstractRecord &rec, const struct FieldAlgoBaseInitData &init_data)
virtual void set_native_dh(std::shared_ptr< DOFHandlerMultiDim >)
virtual std::vector< const FieldCommon * > set_dependency(FMT_UNUSED FieldSet &field_set)
virtual void cache_reinit(AssemblyInternals &asm_internals)
Allows reinit data members or structures in descendants during reinit of FieldValueCache of 'parental...
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
unsigned int n_comp() const
virtual void cache_update(FieldValueCache< typename Value::element_type > &data_cache, ElementCacheMap &cache_map, unsigned int region_patch_idx)
static const unsigned int spacedim_
virtual ~FieldAlgorithmBase()
static std::string template_name()
virtual bool set_time(const TimeStep &time)
bool is_constant_in_space_
Flag detects that field is only dependent on time.
static Input::Type::Abstract & get_input_type()
virtual void set_mesh(const Mesh *mesh)
void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Init value of unit_conversion_coefficient_ from input.
FieldResult field_result_
Indicator of particular values (zero, one) constant over space.
TYPEDEF_ERR_INFO(EI_Field, std::string)
static constexpr bool is_enum_valued
unsigned int component_idx_
Specify if the field is part of a MultiField and which component it is.
Space< spacedim >::Point Point
static const Input::Type::Record & get_field_algo_common_keys()
TimeStep time_
Actual time level; initial value is -infinity.
bool is_constant_in_space() const
DECLARE_EXCEPTION(ExcInputInitUnsupported,<< "The field "<< EI_Field::qval<< " do not support initialization from input.\n")
Value value_
Last value, prevents passing large values (vectors) by value.
FieldResult field_result() const
Value::return_type r_value_
FieldAlgorithmBase(unsigned int n_comp=0)
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:78
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:161
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
Class for declaration of polymorphic Record.
Helper class that stores data of generic types.
Definition: type_generic.hh:89
Record type proxy class.
Definition: type_record.hh:182
Template for classes storing finite set of named values.
Definition: mesh.h:362
Armor::ArmaVec< double, spacedim > Point
Definition: point.hh:42
Representation of one time step..
Class for representation SI units of Fields.
Definition: unit_si.hh:40
FieldResult
@ result_zeros
@ result_eye
@ result_other
@ result_ones
@ result_constant
@ result_none
@ Value
static constexpr bool value
Definition: json.hpp:87
Abstract linear system class.
Definition: balance.hh:40
#define FMT_UNUSED
Definition: posix.h:75
Holds common data shared between GenericAssemblz and Assembly<dim> classes.
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
FieldFlag::Flags flags_
FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si)
Simplified constructor, set limit values automatically (used in unit tests)
const UnitSI & unit_si_
std::pair< double, double > limits_
FieldAlgoBaseInitData(std::string field_name, unsigned int n_comp, const UnitSI &unit_si, std::pair< double, double > limits, FieldFlag::Flags flags)
Full constructor.
Basic time management class.