Flow123d  JS_before_hm-915-gc632be9
multi_field.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 multi_field.hh
15  * @brief
16  */
17 
18 #ifndef MULTI_FIELD_HH_
19 #define MULTI_FIELD_HH_
20 
21 using namespace std;
22 
23 
24 #include <boost/core/explicit_operator_bool.hpp> // for optional::oper...
25 #include <boost/exception/detail/error_info_impl.hpp> // for error_info
26 #include <boost/exception/info.hpp> // for operator<<
27 #include <boost/format/alt_sstream.hpp> // for basic_altstrin...
28 #include <boost/format/alt_sstream_impl.hpp> // for basic_altstrin...
29 #include <boost/format/parsing.hpp> // for basic_format::...
30 #include <boost/optional/optional.hpp> // for get_pointer
31 #include <boost/type_index/type_index_facade.hpp> // for operator==
32 #include <iostream> // for basic_ostream:...
33 #include <memory> // for shared_ptr
34 #include <string> // for string, basic_...
35 #include <vector> // for vector
36 #include "fields/field.hh" // for Field<>::Field...
37 #include "fields/field_algo_base.hh" // for FieldAlgorithm...
38 #include "fields/field_algo_base.impl.hh" // for FieldAlgorithm...
39 #include "fields/field_common.hh" // for FieldCommon
40 #include "fields/field_values.hh" // for FieldValue<>::...
41 #include "input/accessors.hh" // for ExcTypeMismatch
42 #include "input/accessors_impl.hh" // for Array::begin
43 #include "input/factory_impl.hh" // for Factory::create
44 #include "input/input_exception.hh" // for DECLARE_INPUT_...
45 #include "input/input_type_forward.hh" // for Type
46 #include "input/type_base.hh" // for Array
47 #include "input/type_generic.hh" // for Instance
48 #include "input/type_record.hh" // for Record::ExcRec...
49 #include "mesh/region.hh" // for Region (ptr only)
50 #include "system/exc_common.hh" // for ExcAssertMsg
51 #include "system/exceptions.hh" // for ExcMessage::~E...
52 #include "system/global_defs.h" // for OLD_ASSERT, msg
53 #include "tools/time_governor.hh" // for TimeStep
54 
55 class Mesh;
56 class Observe;
57 class OutputTime;
58 class EvalPoints;
59 class ElementCacheMap;
60 
61 
62 namespace IT=Input::Type;
63 
64 /**
65  * @brief Class for representation of a vector of fields of the same physical quantity.
66  *
67  * When solving a system of same equations with the number of components given at runtime
68  * (as in the case of transport equation for runtime given number of substances) we need means how to work with the whole
69  * vector of fields at once. This is the aim of this class. It provides the interface given by the parent class @p FieldCommonBase,
70  * but principally it is just a vector of Field<Value,dim> objects. The sub-fields or components of a @p MultiField are independent
71  * objects, how ever the setters propagates the values from the MultiFields to the individual fields. The only exception is the
72  * @p set_name method which in conjunction with @p MultiField::set_subfield_names can set unique name to each component.
73  *
74  * Template parameters are used for every subfield.
75  *
76  * TODO:
77  * - general mechanism how to convert a Field< dim, Vector> to MultiField< dim, Value>
78  * - implement set_from_input
79  * - implement set_Time
80  *
81  * - problem with "input" methods, since Field works with AbstratRecord, the MultiField - However - should use Array of Abstracts
82  * simplest solution - test that in EqDataBase and have more methods in FieldCommonBase, or somehow detach input handling from
83  * Fields
84  *
85  * Definition of MultiField must be in separate file.
86  * In other case source file field.cc is too big and compiler can throw compile error.
87  */
88 template<int spacedim, class Value>
89 class MultiField : public FieldCommon {
90 public:
95 
96  TYPEDEF_ERR_INFO( EI_MultiFieldName, const string );
97  TYPEDEF_ERR_INFO( EI_Size, unsigned int );
98  TYPEDEF_ERR_INFO( EI_ExpectedSize, unsigned int );
99  DECLARE_INPUT_EXCEPTION( Exc_InvalidMultiFieldSize, << "Invalid size " << EI_Size::val
100  << "of the MultiField " << EI_MultiFieldName::qval << ", expected size: " << EI_ExpectedSize::val );
101 
102  class MultiFieldFactory : public Field<spacedim, Value>::FactoryBase {
103  public:
104  /// Constructor.
105  MultiFieldFactory(unsigned int index)
106  : index_(index) {}
107 
108  virtual typename Field<spacedim, Value>::FieldBasePtr create_field(Input::Record rec, const FieldCommon &field);
109 
110  bool is_active_field_descriptor(const Input::Record &in_rec, const std::string &input_name) override;
111 
112  unsigned int index_;
113  };
114 
115  /**
116  * Default constructor.
117  * bc indicates boundary multifield.
118  */
119  MultiField(bool bc = false);
120 
121  /**
122  * Copy constructor.
123  */
124  MultiField(const MultiField &other);
125 
126  /**
127  * Assignment operator. Same properties as copy constructor, but class member name_ is not copied.
128  */
129  MultiField &operator=(const MultiField &other);
130 
131 
132  /**
133  * Returns input type of particular field instance, this is usually static member input_type of the corresponding FieldBase class (
134  * with same template parameters), however, for fields returning "Enum" we have to create whole unique Input::Type hierarchy for
135  * every instance since every such field use different Selection for initialization, even if all returns just unsigned int.
136  */
137  IT::Instance get_input_type() override;
138 
139  IT::Array get_multifield_input_type() override;
140 
141  /**
142  * By this method you can allow that the field need not to be set on regions (and times) where the given @p control_field is
143  * FieldConstant and has value in given @p value_list. We check this in the set_time method. Through this mechanism we
144  * can switch of e.g. boundary data fields according to the type of the boundary condition.
145  */
146  auto disable_where(
147  const MultiField<spacedim, typename FieldValue<spacedim>::Enum > &control_field,
148  const vector<FieldEnum> &value_list) -> MultiField &;
149 
150  /**
151  * Abstract method to update field to the new time level.
152  * Implemented by in class template Field<...>.
153  *
154  * Return true if the value of the field was changed on some region.
155  * The returned value is also stored in @p changed_during_set_time data member.
156  *
157  * In first call initialize MultiField to the number of components given by the size of @p names
158  * and use this vector to name individual components. Should be called after the setters derived from
159  * FieldCommonBase.
160  */
161  bool set_time(const TimeStep &time, LimitSide limit_side) override;
162 
163  /**
164  * We have to override the @p set_mesh method in order to call set_mesh method for subfields.
165  */
166  void set_mesh(const Mesh &mesh) override;
167 
168  /**
169  * Polymorphic copy. Check correct type, allows copy of MultiField or Field.
170  */
171  void copy_from(const FieldCommon & other) override;
172 
173  /**
174  * Implementation of @p FieldCommonBase::output().
175  */
176  void field_output(std::shared_ptr<OutputTime> stream) override;
177 
178  /**
179  * Implementation of FieldCommonBase::observe_output().
180  */
181  void observe_output(std::shared_ptr<Observe> observe) override;
182 
183  /**
184  * Implementation of @p FieldCommonBase::is_constant().
185  */
186  bool is_constant(Region reg) override;
187 
188  /**
189  * @brief Indicates special field states.
190  *
191  * Return possible values from the enum @p FieldResult, see description there.
192  *
193  * Only difference to Field<>::field_result is meaning of @p result_constant. Here we return this value if
194  * all subfields are (possibly different) constants.
195  *
196  * Not used yet. Possibly tune behavior after usage.
197  */
198  FieldResult field_result( RegionSet region_set) const override;
199 
200  std::string get_value_attribute() const override;
201 
202  /**
203  * Virtual destructor.
204  */
205  inline virtual ~MultiField() {}
206 
207  /// Number of subfields that compose the multi-field.
208  inline unsigned int size() const
209  { return sub_fields_.size(); }
210 
211  /**
212  * Returns reference to the sub-field (component) of given index @p idx.
213  */
214  inline SubFieldType &operator[](unsigned int idx)
215  {
216  ASSERT_LT(idx, sub_fields_.size())(this->input_name()).error("Index of subfield in MultiField is out of range.\n");
217  return sub_fields_[idx];
218  }
219 
220  /**
221  * Returns constant reference to the sub-field (component) of given index @p idx.
222  */
223  inline const SubFieldType &operator[](unsigned int idx) const
224  {
225  ASSERT_LT(idx, sub_fields_.size())(this->input_name()).error("Index of subfield in MultiField is out of range.\n");
226  return sub_fields_[idx];
227  }
228 
229  /**
230  * Initialize components of MultiField.
231  *
232  * Must be call after setting components, mesh and limit side.
233  */
234  void setup_components();
235 
236  /**
237  * Returns vector of value in one given point @p on an element given by ElementAccessor @p elm.
238  * It returns reference to he actual value in order to avoid temporaries for vector and tensor values.
239  */
240 // virtual typename MultiFieldValue::return_type value(const Point &p, const ElementAccessor<spacedim> &elm) const;
241 
242  /**
243  * Returns std::vector of vector values in several points at once. The base class implements
244  * trivial implementation using the @p value(,,) method. This is not optimal as it involves lot of virtual calls,
245  * but this overhead can be negligible for more complex fields as Python of Formula.
246  */
247 // virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor<spacedim> &elm,
248 // std::vector<typename MultiFieldValue::return_type> &value_list) const;
249 
250  void set_input_list(const Input::Array &list, const TimeGovernor &tg) override;
251 
252  /// Implements FieldCommon::cache_allocate
253  void cache_allocate(std::shared_ptr<EvalPoints> eval_points) override;
254 
255  /// Implements FieldCommon::cache_update
256  void cache_update(ElementCacheMap &cache_map) override;
257 
258  void set_fields(const RegionSet &domain,
260  double time = 0.0);
261 
262 private:
263  /// Subfields (items) of MultiField
265 
266  /// Full list of input field descriptors from which the subfields of MultiField are set.
268 
269  /// TimeGovernor is necessary for set input list in setup_components method.
271 
272  /**
273  * If this pointer is set, turn off check of initialization in the
274  * @p set_time method on the regions where the method @p get_constant_enum_value
275  * of the control field returns value from @p no_check_values_. This
276  * field is private copy, its set_time method is called from the
277  * set_Time method of actual object.
278  */
280 };
281 
282 
283 #endif /* MULTI_FIELD_HH_ */
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:75
Accessor to input data conforming to declared Array.
Definition: accessors.hh:567
unsigned int size() const
Number of subfields that compose the multi-field.
Definition: multi_field.hh:208
Class template representing a field with values dependent on: point, element, and region...
Definition: field.hh:92
Directing class of FieldValueCache.
Field< spacedim, Value > SubFieldType
Definition: multi_field.hh:92
Definition: mesh.h:78
const MultiField< spacedim, typename FieldValue< spacedim >::Enum > * no_check_control_field_
Definition: multi_field.hh:279
Helper class that stores data of generic types.
Definition: type_generic.hh:89
Basic time management functionality for unsteady (and steady) solvers (class Equation).
FieldValue_< 0, 1, typename Value::element_type > MultiFieldValue
Definition: multi_field.hh:94
Basic time management class.
Input::Array full_input_list_
Full list of input field descriptors from which the subfields of MultiField are set.
Definition: multi_field.hh:267
Class for declaration of inputs sequences.
Definition: type_base.hh:346
Global macros to enhance readability and debugging, general constants.
FieldAlgorithmBase< spacedim, Value > SubFieldBaseType
Definition: multi_field.hh:91
std::vector< SubFieldType > sub_fields_
Subfields (items) of MultiField.
Definition: multi_field.hh:264
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
MultiFieldFactory(unsigned int index)
Constructor.
Definition: multi_field.hh:105
const TimeGovernor * tg_
TimeGovernor is necessary for set input list in setup_components method.
Definition: multi_field.hh:270
#define TYPEDEF_ERR_INFO(EI_Type, Type)
Macro to simplify declaration of error_info types.
Definition: exceptions.hh:194
The class for outputting data during time.
Definition: output_time.hh:50
Space< spacedim >::Point Point
FieldResult
FieldAlgorithmBase< spacedim, Value >::Point Point
Definition: multi_field.hh:93
std::shared_ptr< FieldBaseType > FieldBasePtr
Definition: field.hh:96
#define DECLARE_INPUT_EXCEPTION(ExcName, Format)
Macro for simple definition of input exceptions.
Class holds local coordinations of evaluating points (bulk and sides) specified by element dimension...
Definition: eval_points.hh:43
virtual ~MultiField()
Definition: multi_field.hh:205
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than)
Definition: asserts.hh:296
const SubFieldType & operator[](unsigned int idx) const
Definition: multi_field.hh:223
Class for representation of a vector of fields of the same physical quantity.
Definition: multi_field.hh:89
SubFieldType & operator[](unsigned int idx)
Definition: multi_field.hh:214
Representation of one time step..
LimitSide
Definition: field_common.hh:62