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