Flow123d  release_2.2.0-41-g0958a8d
field_set.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_set.hh
15  * @brief
16  */
17 
18 #ifndef FIELD_SET_HH_
19 #define FIELD_SET_HH_
20 
21 
22 #include <system/exceptions.hh>
23 #include "fields/field.hh"
24 #include "fields/field_flag.hh"
25 
26 
27 
28 /**
29  * @brief Container for various descendants of FieldCommonBase.
30  *
31  * Provides various collective operations.
32  * Typical usage:
33  *
34  * class EqData : public FieldSet
35  * {
36  * EqData() {
37  * *this += scalar_field
38  * .name("scalar_field")
39  * .description("Some description for input and output documentation.")
40  * .input_default("{0.0}")
41  * .units("m");
42  * *this += vector_field
43  * .name("vector_field")
44  * .description("Some description for input and output documentation.")
45  * .units("m");
46  * }
47  *
48  * Field<3, FieldValue<3>::Scalar> scalar_field;
49  * Field<3, FieldValue<3>::VectorFixed> vector_field;
50  * };
51  *
52  * This way the fields are destructed just before their pointers stored in the FieldSet.
53  *
54  * TODO:
55  * Some set_XY functions set also to the fields added to the FieldSet in future.
56  * This behavior should be removed, since it is misleading in combination with mask subsets. If one set
57  * something to mask subset, it does not influence fields added to the original field set even if
58  * they match the mask of the subset.
59  *
60  */
61 class FieldSet : public FieldFlag {
62 public:
63  DECLARE_EXCEPTION(ExcUnknownField, << "Field set has no field with name: " << FieldCommon::EI_Field::qval);
64 
65  /**
66  * Add an existing Field to the list. It stores just pointer to the field.
67  * Be careful to not destroy passed Field before the FieldSet.
68  *
69  * Using operator allows elegant setting and adding of a field to the field set:
70  * @code
71  * Field<...> init_quantity; // member of a FieldSet descendant
72  *
73  * field_set +=
74  * some_field
75  * .disable_where(type, {dirichlet, neumann}) // this must come first since it is not member of FieldCommonBase
76  * .name("init_temperature")
77  * .description("Initial temperature");
78  *
79  */
80  FieldSet &operator +=(FieldCommon &add_field);
81 
82  /**
83  * Add other FieldSet to current one.
84  */
85  FieldSet &operator +=(const FieldSet &other);
86 
87  /**
88  * Make new FieldSet as a subset of *this. The new FieldSet contains fields with names given by the @p names parameter.
89  */
90  FieldSet subset(std::vector<std::string> names) const;
91 
92  /**
93  * Make new FieldSet as a subset of *this.
94  * The new FieldSet contains all fields that match given @p mask.
95  */
96  FieldSet subset( FieldFlag::Flags::Mask mask) const;
97 
98  /**
99  * Number of fields in the FieldSet.
100  */
101  inline unsigned int size() const {
102  return field_list.size();
103  }
104 
105  /**
106  * Returns input type for a field descriptor, that can contain any of the fields in the set.
107  * Typical usage is from derived class, where we add fields in the constructor
108  * and make auxiliary temporary instance
109  * to get the record of the field descriptor.
110  * The returned Record has name equation_name + "_Data".
111  *
112  * Simplest example:
113  *
114  * @code
115  * class EqData : public FieldSet {
116  * public:
117  * // fields
118  * Field<..> field_a;
119  * Field<..> field_b
120  * EqData() {
121  * add(field_a);
122  * add(field_b);
123  * }
124  * }
125  *
126  * Input::Type::Record SomEquation::input_type=
127  * Record("SomeEquation","equation's description")
128  * .declare_key("data",Input::Type::Array(
129  * EqData().make_field_descriptor_type("SomeEquation")),"List of field descriptors.");
130  * @endcode
131  *
132  */
133  Input::Type::Record make_field_descriptor_type(const std::string &equation_name) const;
134 
135  /**
136  * Make Selection with strings for all field names in the FieldSet.
137  */
138  //Input::Type::Selection make_output_field_selection(const string &name, const string &desc);
139 
140  /**
141  * Use @p FieldCommonBase::copy_from() to set field of the field set given by the first parameter
142  * @p dest_field_name. The source field is given as the second parameter @p source. The field
143  * copies share the same input descriptor list and the same instances of FieldBase classes
144  * but each copy can be set to different time and different limit side.
145  *
146  * See @p FieldCommonBase::copy_from documentation for details.
147  */
148  void set_field(const std::string &dest_field_name, FieldCommon &source);
149 
150  /**
151  * Return pointer to the field given by name @p field_name. Return nullptr if not found.
152  */
153  FieldCommon *field(const std::string &field_name) const;
154 
155  /**
156  * Returns reference to the field given by @p field_name.
157  * Throws if the field with given name is not found.
158  */
159  FieldCommon &operator[](const std::string &field_name) const;
160 
161  /**
162  * Collective interface to @p FieldCommonBase::set_components().
163  * It is safe to call this for field sets containing also fields
164  * with return value other then variable vector as long as all variable
165  * vector fields should be set to the same number of components.
166  */
167  void set_components(const std::vector<string> &names) {
168  for(FieldCommon *field : field_list) field->set_components(names);
169  }
170  /**
171  * Collective interface to @p FieldCommonBase::set_mesh().
172  */
173  void set_mesh(const Mesh &mesh) {
174  for(FieldCommon *field : field_list) field->set_mesh(mesh);
175  }
176 
177  /**
178  * Collective interface to @p FieldCommon::set_mesh().
179  */
180  void set_input_list(Input::Array input_list) {
181  for(FieldCommon *field : field_list) field->set_input_list(input_list);
182  }
183 
184  /**
185  * Collective interface to @p FieldCommonBase::flags_add().
186  * @param mask mask to set for all fields in the field set.
187  */
188  void flags_add( FieldFlag::Flags::Mask mask) {
189  for(FieldCommon *field : field_list) field->flags_add(mask);
190  }
191 
192  /**
193  * Collective interface to @p FieldCommonBase::set_mesh().
194  */
195  bool set_time(const TimeStep &time, LimitSide limit_side);
196 
197  /**
198  * Collective interface to @p FieldCommonBase::output_type().
199  * @param rt Discrete function space (element, node or corner data).
200  */
202  for(FieldCommon *field : field_list) field->output_type(rt);
203  }
204 
205  /**
206  * Collective interface to @p FieldCommonBase::mark_input_times().
207  */
208  void mark_input_times(const TimeGovernor &tg) {
209  for(auto field : field_list) field->mark_input_times(tg);
210  }
211 
212  /**
213  * Collective interface to @p FieldCommonBase::set_mesh().
214  */
215  bool changed() const;
216 
217  /**
218  * Collective interface to @p FieldCommonBase::set_mesh().
219  */
220  bool is_constant(Region reg) const;
221 
222  /**
223  * Collective interface to @p FieldCommonBase::is_jump_time().
224  */
225  bool is_jump_time() const;
226 
227 
228 
229  /**
230  * OBSOLETE
231  *
232  * Adds given field into list of fields for group operations on fields.
233  * Parameters are: @p field pointer, @p name of the key in the input, @p desc - description of the key, and optional parameter
234  * @p d_val with default value. This method is rather called through the macro ADD_FIELD
235  */
236  FieldCommon &add_field( FieldCommon *field, const string &name,
237  const string &desc, const string & d_val="");
238 
239 protected:
240 
241 
242  /// List of all fields.
244 
245  /**
246  * Stream output operator
247  */
248  friend std::ostream &operator<<(std::ostream &stream, const FieldSet &set);
249 };
250 
251 
252 /**
253  * (OBSOLETE)
254  * Macro to simplify call of FieldSet::add_field method. Two forms are supported:
255  *
256  *
257  *
258  * ADD_FIELD(some_field, description);
259  * ADD_FIELD(some_field, description, Default);
260  *
261  * The first form adds name "some_field" to the field member some_field, also adds description of the field. No default
262  * value is specified, so the user must initialize the field on all regions (This is checked in the Field<..>::set_time method)
263  *
264  * The second form adds also default value to the field, that is Default(".."), or Default::read_time(), other default value specifications are
265  * meaningless. The automatic conversion to FieldConst is used, e.g. Default::("0.0") is automatically converted to
266  * { TYPE="FieldConst", value=[ 0.0 ] } for a vector valued field, so you get zero vector on output on regions with default value.
267  */
268 
269 #define ADD_FIELD(name, ...) this->add_field(&name, string(#name), __VA_ARGS__)
270 
271 
272 
273 
274 #endif /* FIELD_SET_HH_ */
void output_type(OutputTime::DiscreteSpace rt)
Definition: field_set.hh:201
std::vector< FieldCommon * > field_list
List of all fields.
Definition: field_set.hh:243
void set_input_list(Input::Array input_list)
Definition: field_set.hh:180
bool is_jump_time() const
Definition: field_set.cc:172
virtual void set_mesh(const Mesh &mesh)
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:60
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:61
FieldCommon & operator[](const std::string &field_name) const
Definition: field_set.cc:140
Accessor to input data conforming to declared Array.
Definition: accessors.hh:567
FieldCommon & flags_add(FieldFlag::Flags::Mask mask)
virtual void set_input_list(const Input::Array &list)=0
Definition: mesh.h:97
void mark_input_times(const TimeGovernor &tg)
Definition: field_common.cc:77
Basic time management functionality for unsteady (and steady) solvers (class Equation).
FieldCommon * field(const std::string &field_name) const
Definition: field_set.cc:132
FieldCommon & add_field(FieldCommon *field, const string &name, const string &desc, const string &d_val="")
Definition: field_set.cc:181
void set_field(const std::string &dest_field_name, FieldCommon &source)
Definition: field_set.cc:124
void mark_input_times(const TimeGovernor &tg)
Definition: field_set.hh:208
DECLARE_EXCEPTION(ExcUnknownField,<< "Field set has no field with name: "<< FieldCommon::EI_Field::qval)
void set_components(const std::vector< string > &names)
Definition: field_set.hh:167
bool set_time(const TimeStep &time, LimitSide limit_side)
Definition: field_set.cc:149
bool is_constant(Region reg) const
Definition: field_set.cc:165
void set_mesh(const Mesh &mesh)
Definition: field_set.hh:173
friend std::ostream & operator<<(std::ostream &stream, const FieldSet &set)
Definition: field_set.cc:189
void set_components(const std::vector< string > &names)
Record type proxy class.
Definition: type_record.hh:182
Representation of one time step..
void flags_add(FieldFlag::Flags::Mask mask)
Definition: field_set.hh:188
bool changed() const
Definition: field_set.cc:157
LimitSide
Definition: field_common.hh:47
FieldCommon & output_type(OutputTime::DiscreteSpace rt)
Input::Type::Record make_field_descriptor_type(const std::string &equation_name) const
Definition: field_set.cc:61