Flow123d  release_3.0.0-1166-g21aa698
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 
59 
60 namespace IT=Input::Type;
61 
62 /**
63  * @brief Class for representation of a vector of fields of the same physical quantity.
64  *
65  * When solving a system of same equations with the number of components given at runtime
66  * (as in the case of transport equation for runtime given number of substances) we need means how to work with the whole
67  * vector of fields at once. This is the aim of this class. It provides the interface given by the parent class @p FieldCommonBase,
68  * but principally it is just a vector of Field<Value,dim> objects. The sub-fields or components of a @p MultiField are independent
69  * objects, how ever the setters propagates the values from the MultiFields to the individual fields. The only exception is the
70  * @p set_name method which in conjunction with @p MultiField::set_subfield_names can set unique name to each component.
71  *
72  * Template parameters are used for every subfield.
73  *
74  * TODO:
75  * - general mechanism how to convert a Field< dim, Vector> to MultiField< dim, Value>
76  * - implement set_from_input
77  * - implement set_Time
78  *
79  * - problem with "input" methods, since Field works with AbstratRecord, the MultiField - However - should use Array of Abstracts
80  * simplest solution - test that in EqDataBase and have more methods in FieldCommonBase, or somehow detach input handling from
81  * Fields
82  *
83  * Definition of MultiField must be in separate file.
84  * In other case source file field.cc is too big and compiler can throw compile error.
85  */
86 template<int spacedim, class Value>
87 class MultiField : public FieldCommon {
88 public:
93 
94  TYPEDEF_ERR_INFO( EI_MultiFieldName, const string );
95  TYPEDEF_ERR_INFO( EI_Size, unsigned int );
96  TYPEDEF_ERR_INFO( EI_ExpectedSize, unsigned int );
97  DECLARE_INPUT_EXCEPTION( Exc_InvalidMultiFieldSize, << "Invalid size " << EI_Size::val
98  << "of the MultiField " << EI_MultiFieldName::qval << ", expected size: " << EI_ExpectedSize::val );
99 
100  class MultiFieldFactory : public Field<spacedim, Value>::FactoryBase {
101  public:
102  /// Constructor.
103  MultiFieldFactory(unsigned int index)
104  : index_(index) {}
105 
107 
108  bool is_active_field_descriptor(const Input::Record &in_rec, const std::string &input_name) override;
109 
110  unsigned int index_;
111  };
112 
113  /**
114  * Default constructor.
115  * bc indicates boundary multifield.
116  */
117  MultiField(bool bc = false);
118 
119  /**
120  * Copy constructor.
121  */
122  MultiField(const MultiField &other);
123 
124  /**
125  * Assignment operator. Same properties as copy constructor, but class member name_ is not copied.
126  */
127  MultiField &operator=(const MultiField &other);
128 
129 
130  /**
131  * Returns input type of particular field instance, this is usually static member input_type of the corresponding FieldBase class (
132  * with same template parameters), however, for fields returning "Enum" we have to create whole unique Input::Type hierarchy for
133  * every instance since every such field use different Selection for initialization, even if all returns just unsigned int.
134  */
135  IT::Instance get_input_type() override;
136 
137  IT::Array get_multifield_input_type() override;
138 
139  /**
140  * 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
141  * FieldConstant and has value in given @p value_list. We check this in the set_time method. Through this mechanism we
142  * can switch of e.g. boundary data fields according to the type of the boundary condition.
143  */
144  auto disable_where(
145  const MultiField<spacedim, typename FieldValue<spacedim>::Enum > &control_field,
146  const vector<FieldEnum> &value_list) -> MultiField &;
147 
148  /**
149  * Abstract method to update field to the new time level.
150  * Implemented by in class template Field<...>.
151  *
152  * Return true if the value of the field was changed on some region.
153  * The returned value is also stored in @p changed_during_set_time data member.
154  *
155  * In first call initialize MultiField to the number of components given by the size of @p names
156  * and use this vector to name individual components. Should be called after the setters derived from
157  * FieldCommonBase.
158  */
159  bool set_time(const TimeStep &time, LimitSide limit_side) override;
160 
161  /**
162  * We have to override the @p set_mesh method in order to call set_mesh method for subfields.
163  */
164  void set_mesh(const Mesh &mesh) override;
165 
166  /**
167  * Polymorphic copy. Check correct type, allows copy of MultiField or Field.
168  */
169  void copy_from(const FieldCommon & other) override;
170 
171  /**
172  * Implementation of @p FieldCommonBase::output().
173  */
174  void field_output(std::shared_ptr<OutputTime> stream) override;
175 
176  /**
177  * Implementation of FieldCommonBase::observe_output().
178  */
179  void observe_output(std::shared_ptr<Observe> observe) override;
180 
181  /**
182  * Implementation of @p FieldCommonBase::is_constant().
183  */
184  bool is_constant(Region reg) override;
185 
186  /**
187  * @brief Indicates special field states.
188  *
189  * Return possible values from the enum @p FieldResult, see description there.
190  *
191  * Only difference to Field<>::field_result is meaning of @p result_constant. Here we return this value if
192  * all subfields are (possibly different) constants.
193  *
194  * Not used yet. Possibly tune behavior after usage.
195  */
196  FieldResult field_result( RegionSet region_set) const override;
197 
198  std::string get_value_attribute() const override;
199 
200  /**
201  * Virtual destructor.
202  */
203  inline virtual ~MultiField() {}
204 
205  /// Number of subfields that compose the multi-field.
206  inline unsigned int size() const
207  { return sub_fields_.size(); }
208 
209  /**
210  * Returns reference to the sub-field (component) of given index @p idx.
211  */
212  inline SubFieldType &operator[](unsigned int idx)
213  {
214  OLD_ASSERT(idx < sub_fields_.size(), "Index of subfield in MultiField '%s' is out of range.\n", this->input_name().c_str());
215  return sub_fields_[idx];
216  }
217 
218  /**
219  * Returns constant reference to the sub-field (component) of given index @p idx.
220  */
221  inline const SubFieldType &operator[](unsigned int idx) const
222  {
223  OLD_ASSERT(idx < sub_fields_.size(), "Index of subfield in MultiField '%s' is out of range.\n", this->input_name().c_str());
224  return sub_fields_[idx];
225  }
226 
227  /**
228  * Initialize components of MultiField.
229  *
230  * Must be call after setting components, mesh and limit side.
231  */
232  void setup_components();
233 
234  /**
235  * Returns vector of value in one given point @p on an element given by ElementAccessor @p elm.
236  * It returns reference to he actual value in order to avoid temporaries for vector and tensor values.
237  */
238 // virtual typename MultiFieldValue::return_type value(const Point &p, const ElementAccessor<spacedim> &elm) const;
239 
240  /**
241  * Returns std::vector of vector values in several points at once. The base class implements
242  * trivial implementation using the @p value(,,) method. This is not optimal as it involves lot of virtual calls,
243  * but this overhead can be negligible for more complex fields as Python of Formula.
244  */
245 // virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor<spacedim> &elm,
246 // std::vector<typename MultiFieldValue::return_type> &value_list) const;
247 
248  void set_input_list(const Input::Array &list, const TimeGovernor &tg) override;
249 
250 private:
251  /// Subfields (items) of MultiField
253 
254  /// Full list of input field descriptors from which the subfields of MultiField are set.
256 
257  /// TimeGovernor is necessary for set input list in setup_components method.
259 
260  /**
261  * If this pointer is set, turn off check of initialization in the
262  * @p set_time method on the regions where the method @p get_constant_enum_value
263  * of the control field returns value from @p no_check_values_. This
264  * field is private copy, its set_time method is called from the
265  * set_Time method of actual object.
266  */
268 };
269 
270 
271 #endif /* MULTI_FIELD_HH_ */
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:73
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:206
std::shared_ptr< FieldFE< spacedim, Value > > create_field(VectorMPI &vec_seq, Mesh &mesh, unsigned int n_comp)
Definition: field_fe.hh:246
Class template representing a field with values dependent on: point, element, and region...
Definition: field.hh:83
Field< spacedim, Value > SubFieldType
Definition: multi_field.hh:90
Definition: mesh.h:76
const MultiField< spacedim, typename FieldValue< spacedim >::Enum > * no_check_control_field_
Definition: multi_field.hh:267
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:92
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:255
Class for declaration of inputs sequences.
Definition: type_base.hh:346
#define OLD_ASSERT(...)
Definition: global_defs.h:131
Global macros to enhance readability and debugging, general constants.
FieldAlgorithmBase< spacedim, Value > SubFieldBaseType
Definition: multi_field.hh:89
std::vector< SubFieldType > sub_fields_
Subfields (items) of MultiField.
Definition: multi_field.hh:252
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
MultiFieldFactory(unsigned int index)
Constructor.
Definition: multi_field.hh:103
const TimeGovernor * tg_
TimeGovernor is necessary for set input list in setup_components method.
Definition: multi_field.hh:258
#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:91
std::shared_ptr< FieldBaseType > FieldBasePtr
Definition: field.hh:87
#define DECLARE_INPUT_EXCEPTION(ExcName, Format)
Macro for simple definition of input exceptions.
virtual ~MultiField()
Definition: multi_field.hh:203
const SubFieldType & operator[](unsigned int idx) const
Definition: multi_field.hh:221
Class for representation of a vector of fields of the same physical quantity.
Definition: multi_field.hh:87
SubFieldType & operator[](unsigned int idx)
Definition: multi_field.hh:212
Representation of one time step..
LimitSide
Definition: field_common.hh:60