Flow123d  jenkins-Flow123d-windows32-release-multijob-51
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 <vector>
12 using namespace std;
13 
14 #include "system/exceptions.hh"
15 #include "fields/field_values.hh"
16 #include "input/accessors.hh"
17 #include "coupling/time_marks.hh"
19 
20 #include "fields/field_flag.hh"
21 #include "fields/unit_si.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  * Set description of the field, used for description of corresponding key in documentation.
77  */
78  FieldCommon & description(const string & description)
79  { shared_->input_description_ = description; return *this;}
80  /**
81  * Set default value for the field's key from which the default constant valued field will be constructed.
82  *
83  * During the first call of the @p set_time method, we check that the field is defined on all regions.
84  * On regions where it is not set yet, we use given @p dflt string to get particular instance of
85  * FieldBase<> (see @p check_initialized_region_fields_).
86  * The default string is interpreted in the same way as if it appears in the input file
87  * as the value of the field. In particular it can be whole record with @p TYPE of the field etc.
88  * Most common choice is however mere constant.
89  */
90  FieldCommon & input_default(const string &input_default)
91  { shared_->input_default_ = input_default; return *this;}
92  /**
93  * @brief Set basic units of the field.
94  *
95  * Currently, we use it only during output and we represents units just by a string.
96  *
97  * TODO:
98  * Particular class for representing and conversion of various units would be more appropriate.
99  * This can allow specification of the units on the inptu, automatic conversion and the same on the output.
100  * Possibly this allow using Boost::Units library, however, it seems to introduce lot of boilerplate code.
101  * But can increase correctness of the calculations.
102  */
103  FieldCommon & units(const UnitSI & units)
104  { shared_->units_ = units; return *this;}
105 
106  /**
107  * For the fields returning "Enum", we have to pass the Input::Type::Selection object to
108  * the field implementations.
109  *
110  * We must save raw pointer since selection may not be yet initialized (during static initialization phase).
111  */
113  {
114  shared_->input_element_selection_=element_selection;
115  return *this;
116  }
117 
118  /**
119  * Output discrete space used in the output() method. Can be different for different field copies.
120  * one can choose between:
121  * data constant on elements, linear data given in nodes, and discontinuous linear data.
122  *
123  * If not set explicitly by this method, the default value is OutputTime::ELEM_DATA
124  */
126  { type_of_output_data_ = rt; return *this; }
127 
128  /**
129  * Set given mask to the field flags, ignoring default setting.
130  * Default setting is declare_input & equation_input & allow_output.
131  */
132  FieldCommon & flags(FieldFlag::Flags::Mask mask)
133  { flags_ = FieldFlag::Flags(mask); return *this; }
134 
135  /**
136  * Add given mask to the field flags.
137  */
138  FieldCommon & flags_add(FieldFlag::Flags::Mask mask)
139  { flags().add(mask); return *this; }
140 
141  /**
142  * Set number of components for run-time sized vectors. This is used latter when we construct
143  * objects derived from FieldBase<...>.
144  *
145  * n_comp_ is constant zero for fixed values, this zero is set by Field<...> constructors
146  */
147  void set_n_components( unsigned int n_comp)
148  { shared_->n_comp_ = (shared_->n_comp_ ? n_comp : 0);}
149 
150 
151  /**
152  * Set internal mesh pointer.
153  */
154  virtual void set_mesh(const Mesh &mesh) {};
155  /**
156  * Set the data list from which field will read its input. It is list of "field descriptors".
157  * When reading from the input list we consider only field descriptors containing key of
158  * named by the field name. These field descriptors has to have times forming ascending sequence.
159  *
160  * The list is used by set_time method to set field on individual regions to actual FieldBase descendants.
161  */
162  void set_input_list(const Input::Array &list);
163 
164  /**
165  * Set side of limit when calling @p set_time
166  * with jump time. This method invalidate result of
167  * @p changed() so it should be called just before @p set_time.
168  * Can be different for different field copies.
169  */
170  virtual void set_limit_side(LimitSide side) = 0;
171 
172  /**
173  * Getters.
174  */
175  const std::string &input_name() const
176  { return shared_->input_name_;}
177 
178  const std::string &name() const
179  { return name_;}
180 
181  const std::string description() const
182  {return shared_->input_description_;}
183 
184  const std::string &input_default() const
185  { return shared_->input_default_;}
186 
187  const UnitSI &units() const
188  { return shared_->units_;}
189 
191  { return type_of_output_data_; }
192 
193  bool is_bc() const
194  { return shared_->bc_;}
195 
196  unsigned int n_comp() const
197  { return shared_->n_comp_;}
198 
199  const Mesh * mesh() const
200  { return shared_->mesh_;}
201 
203  { return limit_side_;}
204 
206  { return flags_; }
207 
208  /**
209  * Returns time set by last call of set_time method.
210  * Can be different for different field copies.
211  */
212  double time() const
213  { return last_time_; }
214 
215 
216  /**
217  * Common part of the field descriptor. To get finished record
218  * one has to add keys for individual fields. This is done automatically
219  * using FieldSet::get_input_type().
220  */
221  static IT::Record field_descriptor_record(const string& record_name);
222 
223  /**
224  * Returns input type for particular field instance, this is reference to a static member input_type of the corresponding @p FieldBase
225  * class (i.e. with the same template parameters). This is used in FieldSet::make_field_descriptor_type.
226  */
227  virtual IT::AbstractRecord &get_input_type() =0;
228 
229  /**
230  * Pass through the input array @p input_list_, collect all times where the field could change and
231  * put appropriate time marks into global TimeMarks object.
232  * Introduced time marks have both given @p mark_type and @p type_input() type.
233  *
234  * Further development:
235  * - we have to distinguish "jump" times and "smooth" times
236  */
237  void mark_input_times(TimeMark::Type mark_type);
238 
239  /**
240  * Abstract method to update field to the new time level.
241  * Implemented by in class template Field<...>.
242  *
243  * Return true if the value of the field was changed on some region.
244  * The returned value is also stored in @p changed_during_set_time data member.
245  *
246  * Default values helps when creating steady field. Note that default TimeGovernor constructor
247  * set time to 0.0.
248  *
249  * Different field copies can be set to different times.
250  */
251  virtual bool set_time(const TimeGovernor &time) =0;
252 
253  /**
254  * Check that @p other is instance of the same Field<..> class and
255  * perform assignment. Polymorphic copy.
256  */
257  virtual void copy_from(const FieldCommon & other) =0;
258 
259  /**
260  * Output the field.
261  * The parameter @p output_fields is checked for value named by the field name. If the key exists,
262  * then the output of the field is performed. If the key do not appear in the input, no output is done.
263  */
264  virtual void output(OutputTime *stream) =0;
265 
266 
267  /**
268  * If the field on given region @p reg exists and is of type FieldConstant<...> the method method returns true
269  * otherwise it returns false.
270  * Then one call ElementAccessor<spacedim>(mesh(), reg ) to construct an ElementAccessor @p elm
271  * pointing to "virtual" element on which Field::value returns constant value.
272  *
273  * Current implementation use virtual functions and can be prohibitively slow if called for every element. If this
274  * becomes necessary it is possible to incorporate such test into set_time method and in this method just return precomputed result.
275  */
276  virtual bool is_constant(Region reg) =0;
277 
278  /**
279  * Returns true if set_time_result_ is not @p TimeStatus::constant.
280  * Returns the same value as last set_time method.
281  */
282  bool changed() const
283  {
284  ASSERT( set_time_result_ != TimeStatus::unknown, "Invalid time status.\n");
285  return ( (set_time_result_ == TimeStatus::changed) );
286  }
287 
288  /**
289  * Virtual destructor.
290  */
291  virtual ~FieldCommon();
292 
293 
294 protected:
295  /**
296  * Private default constructor. Should be used only through
297  * Field<...>
298  */
299  FieldCommon();
300 
301  /**
302  * Private copy constructor. Should be used only through
303  * Field<...>
304  */
305  FieldCommon(const FieldCommon & other);
306 
307  /**
308  * Invalidate last time in order to force set_time method
309  * update region_fields_.
310  */
312  {
313  last_time_ = -numeric_limits<double>::infinity();
314  }
315 
316  /**
317  * Setters for essential field properties.
318  */
319  /**
320  * Data shared among copies of the same field.
321  *
322  * This allow field copies in different equations with different time setting, but
323  * sharing common input field descriptor array and common history.
324  */
325  struct SharedData {
326  /**
327  * True for boundary fields.
328  */
329  bool bc_;
330  /**
331  * Number of components for fields that return variable size vectors. Zero in other cases.
332  */
333  unsigned int n_comp_;
334  /**
335  * Name of the particular field. Used to name the key in the Field list Record.
336  */
337  std::string input_name_;
338  /**
339  * Description of corresponding key in the Field list Record.
340  */
341  std::string input_description_;
342  /**
343  * Units of the field values. Currently just a string description.
344  */
346  /**
347  * For Enum valued fields this is the input type selection that should be used
348  * to read possible values of the field (e.g. for FieldConstant the key 'value' has this selection input type).
349  *
350  * Is empty selection for for non-enum values fields.
351  *
352  * In fact we must use raw pointer since selection may not be constructed yet (static variable).
353  */
355  /**
356  * Possible default value of the field.
357  */
359  /**
360  * Pointer to the mesh on which the field lives.
361  */
362  const Mesh *mesh_;
363 
364  /**
365  * List of input field descriptors from which the field is set.
366  */
368 
369  /**
370  * Iterator to current input field descriptor.
371  */
373 
374  /**
375  * True after check_initialized_region_fields_ is called. That happen at first call of the set_time method.
376  */
378 
379  /**
380  * For which values of an enum valued field we do not
381  * check the field. User is responsible, that the value will not be called
382  * on such regions.
383  */
385 
386 
387  };
388 
389  /**
390  * Name that identifies the field in the field_set. By default this is same as
391  * shared_->input_name_.
392  */
393  std::string name_;
394 
395  /**
396  * Data shared among copies of the same input field.
397  */
398  std::shared_ptr<SharedData> shared_;
399 
400  /**
401  * Which value is returned for times where field is discontinuous.
402  */
404 
405  /**
406  * Result of last set time method
407  */
408  enum class TimeStatus {
409  changed, //< Field changed during last set time call.
410  constant, //< Field doesn't change.
411  unknown //< Before first call of set_time.
412  };
413 
414  /// Status of @p history.
416 
417  /**
418  * Last set time. Can be different for different field copies.
419  */
420  double last_time_ = -numeric_limits<double>::infinity();
421 
422  /**
423  * Output data type used in the output() method. Can be different for different field copies.
424  */
426 
427  /**
428  * Maximum number of FieldBase objects we store per one region.
429  */
430  static const unsigned int history_length_limit_=3;
431 
432  /// Field flags. Default setting is "an equation input field, that can read from user input, and can be written to output"
434 
435  /**
436  * Stream output operator
437  */
438  friend std::ostream &operator<<(std::ostream &stream, const FieldCommon &field) {
439 
440  string limit_side_str =
441  (field.limit_side_ == LimitSide::left ? "left" :
442  (field.limit_side_ == LimitSide::right ? "right" :
443  "unknown"));
444 
445  stream
446  << "field name:" << field.name()
447  << " limit side:" << limit_side_str
448  << " n. comp.:" << field.n_comp()
449  << " last time:" << field.last_time_;
450  return stream;
451  }
452 };
453 
454 
455 
456 
457 
458 
459 
460 #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).
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
const std::string description() const
Basic time management class.
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:121
const std::string & name() const
FieldFlag::Flags & flags()
FieldCommon & input_default(const string &input_default)
Definition: field_common.hh:90
const UnitSI & units() const
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:463
Input::Iterator< Input::Record > list_it_
#define TYPEDEF_ERR_INFO(EI_Type, Type)
Macro to simplify declaration of error_info types.
Definition: exceptions.hh:171
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:78
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
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
Class for representation SI units of Fields.
Definition: unit_si.hh:31
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