Flow123d  jenkins-Flow123d-windows32-release-multijob-28
field_common.hh
Go to the documentation of this file.
1 /*
2  * field.hh
3  *
4  * Created on: Feb 13, 2014
5  * Author: jb
6  */
7 
8 #ifndef FIELD_COMMON_HH_
9 #define FIELD_COMMON_HH_
10 
11 //#include <memory>
12 #include <vector>
13 using namespace std;
14 
15 #include "system/exceptions.hh"
16 #include "fields/field_values.hh"
17 #include "input/accessors.hh"
18 #include "coupling/time_marks.hh"
20 
21 #include "fields/field_flag.hh"
22 #include "io/output_time.hh"
23 
24 
25 namespace IT=Input::Type;
26 
27 /**
28  * Left and right time limit, used in the @p set_time() method.
29  * Assigned values allow to index an array.
30  */
31 enum class LimitSide {
32  left=0,
33  right=1,
34  unknown=2 // undefined value
35 };
36 
37 
38 
39 /**
40  * @brief Common abstract parent of all Field<...> classes.
41  *
42  * We need common ancestor in order to keep a list of all fields in one EqData object and allow
43  * collective operations like @p set_time or @p init_from_input.
44  */
45 class FieldCommon {
46 
47 public:
48  TYPEDEF_ERR_INFO(EI_Time, double);
49  TYPEDEF_ERR_INFO(EI_Field, std::string);
50  DECLARE_INPUT_EXCEPTION(ExcNonascendingTime,
51  << "Non-ascending time: " << EI_Time::val << " for field " << EI_Field::qval << ".\n");
52  DECLARE_INPUT_EXCEPTION(ExcMissingDomain,
53  << "Missing domain specification (region, r_id, or r_set) in the field descriptor:");
54  DECLARE_EXCEPTION(ExcFieldMeshDifference,
55  << "Two copies of the field " << EI_Field::qval << "call set_mesh with different arguments.\n");
56 
57 
58 
59  /**
60  * Set name of the field. In fact there are two attributes set by this method.
61  *
62  * The first is name used to identify the field as part of a FieldSet or MultiField objects.
63  * This name is permanent and can be set only by this method. Can be accessed by @p name() method.
64  * This name is also used at output.
65  *
66  * The second is @p input_name_ that determines appropriate key name in the input field descriptor.
67  * This name is also set by this method, but is stored in the internal shared space which
68  * is overwritten during call of copy_from method or assignment operator. Can be accessed by @p input_name() mathod.
69  *
70  */
71  FieldCommon &name(const string & name)
72  { name_=shared_->input_name_ = name;
73  return *this;
74  }
75  /**
76  * Mark field to be used only as a copy of other field (do not produce key in record, do not set input list).
77  */
78  //FieldCommonBase &just_copy()
79  //{is_copy_=true; return *this;}
80  /**
81  * Set description of the field, used for description of corresponding key in documentation.
82  */
83  FieldCommon & description(const string & description)
84  { shared_->input_description_ = description; return *this;}
85  /**
86  * Set default value for the field's key from which the default constant valued field will be constructed.
87  *
88  * During the first call of the @p set_time method, we check that the field is defined on all regions.
89  * On regions where it is not set yet, we use given @p dflt string to get particular instance of
90  * FieldBase<> (see @p check_initialized_region_fields_).
91  * The default string is interpreted in the same way as if it appears in the input file
92  * as the value of the field. In particular it can be whole record with @p TYPE of the field etc.
93  * Most common choice is however mere constant.
94  */
95  FieldCommon & input_default(const string &input_default)
96  { shared_->input_default_ = input_default; return *this;}
97  /**
98  * @brief Set basic units of the field.
99  *
100  * Currently, we use it only during output and we represents units just by a string.
101  *
102  * TODO:
103  * Particular class for representing and conversion of various units would be more appropriate.
104  * This can allow specification of the units on the inptu, automatic conversion and the same on the output.
105  * Possibly this allow using Boost::Units library, however, it seems to introduce lot of boilerplate code.
106  * But can increase correctness of the calculations.
107  */
108  FieldCommon & units(const string & units)
109  { shared_->units_ = units; return *this;}
110 
111  /**
112  * For the fields returning "Enum", we have to pass the Input::Type::Selection object to
113  * the field implementations.
114  *
115  * We must save raw pointer since selection may not be yet initialized (during static initialization phase).
116  */
118  {
119  shared_->input_element_selection_=element_selection;
120  return *this;
121  }
122 
123  /**
124  * Output discrete space used in the output() method. Can be different for different field copies.
125  * one can choose between:
126  * data constant on elements, linear data given in nodes, and discontinuous linear data.
127  *
128  * If not set explicitly by this method, the default value is OutputTime::ELEM_DATA
129  */
131  { type_of_output_data_ = rt; return *this; }
132 
133  /**
134  * Set given mask to the field flags, ignoring default setting.
135  * Default setting is declare_input & equation_input & allow_output.
136  */
137  FieldCommon & flags(FieldFlag::Flags::Mask mask)
138  { flags_ = FieldFlag::Flags(mask); return *this; }
139 
140  /**
141  * Add given mask to the field flags.
142  */
143  FieldCommon & flags_add(FieldFlag::Flags::Mask mask)
144  { flags().add(mask); return *this; }
145 
146  /**
147  * Set number of components for run-time sized vectors. This is used latter when we construct
148  * objects derived from FieldBase<...>.
149  *
150  * n_comp_ is constant zero for fixed values, this zero is set by Field<...> constructors
151  */
152  void set_n_components( unsigned int n_comp)
153  { shared_->n_comp_ = (shared_->n_comp_ ? n_comp : 0);}
154 
155 
156  /**
157  * Set internal mesh pointer.
158  */
159  virtual void set_mesh(const Mesh &mesh) {};
160  /**
161  * Set the data list from which field will read its input. It is list of "field descriptors".
162  * When reading from the input list we consider only field descriptors containing key of
163  * named by the field name. These field descriptors has to have times forming ascending sequence.
164  *
165  * The list is used by set_time method to set field on individual regions to actual FieldBase descendants.
166  */
167  void set_input_list(const Input::Array &list);
168 
169  /**
170  * Set side of limit when calling @p set_time
171  * with jump time. This method invalidate result of
172  * @p changed() so it should be called just before @p set_time.
173  * Can be different for different field copies.
174  */
175  virtual void set_limit_side(LimitSide side) = 0;
176 
177  /**
178  * Getters.
179  */
180  const std::string &input_name() const
181  { return shared_->input_name_;}
182 
183  const std::string &name() const
184  { return name_;}
185 
186  const std::string description() const
187  {return shared_->input_description_;}
188 
189  const std::string &input_default() const
190  { return shared_->input_default_;}
191 
192  const std::string &units() const
193  { return shared_->units_;}
194 
196  { return type_of_output_data_; }
197 
198  bool is_bc() const
199  { return shared_->bc_;}
200 
201  unsigned int n_comp() const
202  { return shared_->n_comp_;}
203 
204  const Mesh * mesh() const
205  { return shared_->mesh_;}
206 
208  { return limit_side_;}
209 
211  { return flags_; }
212 
213  //bool is_just_copy() const
214  //{ return is_copy_;}
215 
216  /**
217  * Returns time set by last call of set_time method.
218  * Can be different for different field copies.
219  */
220  double time() const
221  { return last_time_; }
222 
223 
224  /**
225  * Common part of the field descriptor. To get finished record
226  * one has to add keys for individual fields. This is done automatically
227  * using FieldSet::get_input_type().
228  */
229  static IT::Record field_descriptor_record(const string& record_name);
230 
231  /**
232  * Returns input type for particular field instance, this is reference to a static member input_type of the corresponding @p FieldBase
233  * class (i.e. with the same template parameters). This is used in FieldSet::make_field_descriptor_type.
234  */
235  virtual IT::AbstractRecord &get_input_type() =0;
236 
237  /**
238  * Abstract method for initialization of the field on one region.
239  */
240  //virtual void set_from_input(const RegionSet &domain, const Input::AbstractRecord &rec) =0;
241 
242  /**
243  * Pass through the input array @p input_list_, collect all times where the field could change and
244  * put appropriate time marks into global TimeMarks object.
245  * Introduced time marks have both given @p mark_type and @p type_input() type.
246  *
247  * Further development:
248  * - we have to distinguish "jump" times and "smooth" times
249  */
250  void mark_input_times(TimeMark::Type mark_type);
251 
252  /**
253  * Abstract method to update field to the new time level.
254  * Implemented by in class template Field<...>.
255  *
256  * Return true if the value of the field was changed on some region.
257  * The returned value is also stored in @p changed_during_set_time data member.
258  *
259  * Default values helps when creating steady field. Note that default TimeGovernor constructor
260  * set time to 0.0.
261  *
262  * Different field copies can be set to different times.
263  */
264  virtual bool set_time(const TimeGovernor &time) =0;
265 
266  /**
267  * Check that @p other is instance of the same Field<..> class and
268  * perform assignment. Polymorphic copy.
269  */
270  virtual void copy_from(const FieldCommon & other) =0;
271 
272  /**
273  * Output the field.
274  * The parameter @p output_fields is checked for value named by the field name. If the key exists,
275  * then the output of the field is performed. If the key do not appear in the input, no output is done.
276  */
277  virtual void output(OutputTime *stream) =0;
278 
279 
280  /**
281  * If the field on given region @p reg exists and is of type FieldConstant<...> the method method returns true
282  * otherwise it returns false.
283  * Then one call ElementAccessor<spacedim>(mesh(), reg ) to construct an ElementAccessor @p elm
284  * pointing to "virtual" element on which Field::value returns constant value.
285  *
286  * Current implementation use virtual functions and can be prohibitively slow if called for every element. If this
287  * becomes necessary it is possible to incorporate such test into set_time method and in this method just return precomputed result.
288  */
289  virtual bool is_constant(Region reg) =0;
290 
291  /**
292  * Returns true if set_time_result_ is not @p TimeStatus::constant.
293  * Returns the same value as last set_time method.
294  */
295  bool changed() const
296  {
297  ASSERT( set_time_result_ != TimeStatus::unknown, "Invalid time status.\n");
298  return ( (set_time_result_ == TimeStatus::changed) );
299  }
300 
301  /**
302  * Virtual destructor.
303  */
304  virtual ~FieldCommon();
305 
306 
307 protected:
308  /**
309  * Private default constructor. Should be used only through
310  * Field<...>
311  */
312  FieldCommon();
313 
314  /**
315  * Private copy constructor. Should be used only through
316  * Field<...>
317  */
318  FieldCommon(const FieldCommon & other);
319 
320  //FieldCommonBase &FieldCommonBase::operator=(const FieldCommonBase &other) delete;
321 
322  /**
323  * Invalidate last time in order to force set_time method
324  * update region_fields_.
325  */
327  {
328  last_time_ = -numeric_limits<double>::infinity();
329  }
330 
331  /**
332  * Setters for essential field properties.
333  */
334  /**
335  * Data shared among copies of the same field.
336  *
337  * This allow field copies in different equations with different time setting, but
338  * sharing common input field descriptor array and common history.
339  */
340  struct SharedData {
341  /**
342  * True for boundary fields.
343  */
344  bool bc_;
345  /**
346  * Number of components for fields that return variable size vectors. Zero in other cases.
347  */
348  unsigned int n_comp_;
349  /**
350  * Name of the particular field. Used to name the key in the Field list Record.
351  */
352  std::string input_name_;
353  /**
354  * Description of corresponding key in the Field list Record.
355  */
356  std::string input_description_;
357  /**
358  * Units of the field values. Currently just a string description.
359  */
360  std::string units_;
361  /**
362  * For Enum valued fields this is the input type selection that should be used
363  * to read possible values of the field (e.g. for FieldConstant the key 'value' has this selection input type).
364  *
365  * Is empty selection for for non-enum values fields.
366  *
367  * In fact we must use raw pointer since selection may not be constructed yet (static variable).
368  */
370  /**
371  * Possible default value of the field.
372  */
374  /**
375  * Pointer to the mesh on which the field lives.
376  */
377  const Mesh *mesh_;
378 
379  /**
380  * List of input field descriptors from which the field is set.
381  */
383 
384  /**
385  * Iterator to current input field descriptor.
386  */
388 
389  /**
390  * True after check_initialized_region_fields_ is called. That happen at first call of the set_time method.
391  */
393 
394  /**
395  * For which values of an enum valued field we do not
396  * check the field. User is responsible, that the value will not be called
397  * on such regions.
398  */
400 
401 
402  };
403 
404  /**
405  * Name that identifies the field in the field_set. By default this is same as
406  * shared_->input_name_.
407  */
408  std::string name_;
409 
410  /**
411  * Data shared among copies of the same input field.
412  */
413  std::shared_ptr<SharedData> shared_;
414 
415  /**
416  * Which value is returned for times where field is discontinuous.
417  */
419 
420  /**
421  * Result of last set time method
422  */
423  enum class TimeStatus {
424  changed, //< Field changed during last set time call.
425  constant, //< Field doesn't change.
426  unknown //< Before first call of set_time.
427  };
428 
429  /// Status of @p history.
431 
432  /**
433  * Last set time. Can be different for different field copies.
434  */
435  double last_time_ = -numeric_limits<double>::infinity();
436 
437  /**
438  * Output data type used in the output() method. Can be different for different field copies.
439  */
441 
442  /**
443  * Maximum number of FieldBase objects we store per one region.
444  */
445  static const unsigned int history_length_limit_=3;
446 
447  /// Flag field that has to be set as a copy of other field using copy_from method.
448  //bool is_copy_=false;
449 
450  /// Field flags. Default setting is "an equation input field, that can read from user input, and can be written to output"
452 
453  /**
454  * Stream output operator
455  */
456  friend std::ostream &operator<<(std::ostream &stream, const FieldCommon &field) {
457 
458  string limit_side_str =
459  (field.limit_side_ == LimitSide::left ? "left" :
460  (field.limit_side_ == LimitSide::right ? "right" :
461  "unknown"));
462 
463  stream
464  << "field name:" << field.name()
465  << " limit side:" << limit_side_str
466  << " n. comp.:" << field.n_comp()
467  << " last time:" << field.last_time_;
468  return stream;
469  }
470 };
471 
472 
473 
474 
475 
476 
477 
478 #endif /* FIELD_COMMON_HH_ */
virtual void set_mesh(const Mesh &mesh)
std::string input_description_
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:45
unsigned long int Type
Definition: time_marks.hh:59
Accessor to input data conforming to declared Array.
Definition: accessors.hh:521
static constexpr Mask allow_output
The field can output. Is part of generated output selection. (default on)
Definition: field_flag.hh:27
FieldCommon & flags_add(FieldFlag::Flags::Mask mask)
std::string name_
#define DECLARE_EXCEPTION(ExcName, Format)
Macro for simple definition of exceptions.
Definition: exceptions.hh:135
LimitSide limit_side_
Definition: mesh.h:108
double last_time_
const std::string & input_default() const
Basic time management functionality for unsteady (and steady) solvers (class Equation).
const std::string description() const
Basic time management class.
FieldCommon & units(const string &units)
Set basic units of the field.
std::shared_ptr< SharedData > shared_
OutputTime::DiscreteSpace output_type() const
double time() const
friend std::ostream & operator<<(std::ostream &stream, const FieldCommon &field)
#define ASSERT(...)
Definition: global_defs.h:120
const std::string & name() const
FieldFlag::Flags & flags()
FieldCommon & input_default(const string &input_default)
Definition: field_common.hh:95
FieldCommon & input_selection(const Input::Type::Selection *element_selection)
const IT::Selection * input_element_selection_
Class for declaration of polymorphic Record.
Definition: type_record.hh:467
Input::Iterator< Input::Record > list_it_
#define TYPEDEF_ERR_INFO(EI_Type, Type)
Macro to simplify declaration of error_info types.
Definition: exceptions.hh:173
The class for outputing data during time.
Definition: output_time.hh:37
std::vector< FieldEnum > no_check_values_
FieldCommon & description(const string &description)
Definition: field_common.hh:83
LimitSide limit_side() const
const Mesh * mesh() const
FieldCommon & name(const string &name)
Definition: field_common.hh:71
unsigned int n_comp() const
DECLARE_INPUT_EXCEPTION(ExcFV_Input,<< "Wrong field value input: "<< EI_InputMsg::val)
bool changed() const
void set_n_components(unsigned int n_comp)
Record type proxy class.
Definition: type_record.hh:161
void set_history_changed()
FlagArray< FieldFlag > Flags
Definition: field_flag.hh:16
const std::string & units() const
FieldCommon & flags(FieldFlag::Flags::Mask mask)
const std::string & input_name() const
static constexpr Mask equation_input
The field is data parameter of the owning equation. (default on)
Definition: field_flag.hh:23
bool is_bc() const
TimeStatus set_time_result_
Status of history.
Template for classes storing finite set of named values.
LimitSide
Definition: field_common.hh:31
FieldCommon & output_type(OutputTime::DiscreteSpace rt)
static constexpr Mask declare_input
The field can be set from input. The key in input field descriptor is declared. (default on) ...
Definition: field_flag.hh:25