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