Flow123d  release_2.2.0-914-gf1a3a4f
observe.hh
Go to the documentation of this file.
1  /*
2  * observe.hh
3  *
4  * Created on: Jun 28, 2016
5  * Author: jb
6  */
7 
8 #ifndef SRC_IO_OBSERVE_HH_
9 #define SRC_IO_OBSERVE_HH_
10 
11 #include <string>
12 
13 #include "input/input_type.hh"
15 #include "io/element_data_cache.hh"
16 #include "tools/time_governor.hh"
17 #include "system/exceptions.hh"
18 #include <armadillo>
19 
20 
21 
22 /**
23  * Helper class stores base data of ObservePoint and allows to evaluate
24  * the nearest point to input_point_.
25  */
27 public:
28  /// Constructor
30  : distance_(numeric_limits<double>::infinity()) {};
31 
32  /// Final element of the observe point. The index in the mesh.
33  unsigned int element_idx_;
34 
35  /// Global coordinates of the observation point.
37 
38  /// Local (barycentric) coordinates on the element.
39  arma::vec local_coords_;
40 
41  /// Distance of found projection from the initial point.
42  /// If we find more candidates we pass in the closest one.
43  double distance_;
44 
45 };
46 
47 
48 /**
49  * Class representing single observe point, used internally by the class Observe.
50  * Members: input_pos_, snap_dim_, snap_region_name_ are set in constructor. Should be checked before passed in.
51  * Members: element_idx_, global_coords_, local_coords_ are derived, set in Observe::find_observe_points.
52  */
53 class ObservePoint {
54 public:
55  DECLARE_INPUT_EXCEPTION(ExcNoInitialPoint,
56  << "Failed to find the element containing the initial observe point.\n");
57  TYPEDEF_ERR_INFO(EI_RegionName, std::string);
58  DECLARE_INPUT_EXCEPTION(ExcNoObserveElement,
59  << "Failed to find the observe element with snap region: " << EI_RegionName::qval
60  << " close to the initial observe point. Change maximal distance of observe element." << "\n");
61 
62  static const Input::Type::Record & get_input_type();
63 
64  /**
65  * Return index of observation point in the mesh.
66  */
67  inline unsigned int element_idx() const
68  { return observe_data_.element_idx_; }
69 
70  /**
71  * Return global coordinates of the observation point.
72  */
73  inline arma::vec3 global_coords() const
74  { return observe_data_.global_coords_; }
75 
76 protected:
77  /**
78  * Default constructor just for testing.
79  */
80  ObservePoint();
81 
82  /**
83  * Constructor. Read from input.
84  */
85  ObservePoint(Input::Record in_rec, Mesh &mesh, unsigned int point_idx);
86 
87  /**
88  * Returns true if we have already found any observe element.
89  */
90  bool have_observe_element();
91 
92  /**
93  * Snap to the center of closest subelement with dimension snap_dim_.
94  * This makes final adjustment of global_coords_ and local_coords_.
95  */
96  void snap(Mesh &mesh);
97 
98 
99  /**
100  * Find the observe element and the definitive observe point.
101  *
102  * Algorithm:
103  * 1. find element containing the point (initial element)
104  * 2. check initial element for region match possibly set it as (observe element)
105  * 3. add neighbours into processing_list for the next level
106  * 4. while we have no observe element: pass through the processing list of the current level
107  * 5. if element match the region, project and clip the init point, update observe element.
108  * 6. snapping on the observe element
109  *
110  */
111  void find_observe_point(Mesh &mesh);
112 
113  /**
114  * Output the observe point information into a YAML formated stream, indent by
115  * given number of spaces + "- ".
116  */
117  void output(ostream &out, unsigned int indent_spaces, unsigned int precision);
118 
119  /// Project point to given element by dimension of this element.
120  ObservePointData point_projection(unsigned int i_elm, Element &elm);
121 
122  /// Index in the input array.
124 
125  /// Observation point name.
126  std::string name_;
127 
128  /**
129  * Snap to the center of the object of given dimension.
130  * Value 4 and greater means no snapping.
131  */
132  unsigned int snap_dim_;
133 
134  /**
135  * Region of the snapping element.
136  */
138 
139  /**
140  * Maximal distance of observe element from input point.
141  */
143 
144  /// Input coordinates of the initial position of the observation point.
146 
147  /// Helper object stored projection data
149 
150  /// Only Observe should use this class directly.
151  friend class Observe;
152 
153 };
154 
155 
156 /**
157  * This class takes care about the observe points in the output stream, storing observe values of the fields and
158  * their output in the YAML format.
159  */
160 
161 class Observe {
162 public:
163 
164  typedef std::shared_ptr<ElementDataCacheBase> OutputDataPtr;
166 
167  /**
168  * Construct the observation object.
169  *
170  * observe_name - base name of the output file, the equation name.
171  * mesh - the mesh used for search for the observe points
172  * in_array - the array of observe points
173  */
174  Observe(string observe_name, Mesh &mesh, Input::Array in_array, unsigned int precision);
175 
176  /// Destructor, must close the file.
177  ~Observe();
178 
179 
180  /**
181  * Provides a vector of element indices on which the observation values are evaluated.
182  * This can be used to evaluate derived fields only on these elements in the times not selected to
183  * full output.
184  */
186  { return observed_element_indices_;}
187 
188  /**
189  * Output file header.
190  */
191  void output_header();
192 
193  /**
194  * Write field values to the output file. Using the YAML format.
195  */
196  void output_time_frame(double time);
197 
198  /**
199  * Return \p points_ vector
200  */
201  inline const std::vector<ObservePoint> & points() const
202  { return points_; }
203 
204  /**
205  * Prepare data for computing observe values.
206  *
207  * Method:
208  * - check that all fields of one time frame are evaluated at the same time
209  * - find and return ElementDataCache of given field_name, create its if doesn't exist.
210  *
211  * @param field_name Quantity name of founding ElementDataCache
212  * @param field_time Actual computing time
213  * @param n_rows Count of rows of data cache (used only if new cache is created)
214  * @param n_cols Count of columns of data cache (used only if new cache is created)
215  */
216  template <typename T>
217  ElementDataCache<T> & prepare_compute_data(std::string field_name, double field_time, unsigned int n_rows, unsigned int n_cols);
218 
219 
220 
221 protected:
222  // MPI rank.
223  int rank_;
224 
225  /// Full information about observe points.
227  /// Elements of the o_points.
229 
230  /// Stored field values.
231  OutputDataFieldMap observe_field_values_;
232 
233 
234  /// Common evaluation time of the fields for single time frame.
236 
237  // Name of the observation stream. Base for the output filename.
238  std::string observe_name_;
239 
240  /// Output file stream.
241  std::ofstream observe_file_;
242 
243  /// String representation of the time unit.
244  std::string time_unit_str_;
245  /// Time unit in seconds.
247  /// Precision of float output
248  unsigned int precision_;
249 
250  // Warn for no observe fields only once.
251  bool no_fields_warning=false;
252 
253 };
254 
255 
256 
257 #endif /* SRC_IO_OBSERVE_HH_ */
std::vector< ObservePoint > points_
Full information about observe points.
Definition: observe.hh:226
double max_search_radius_
Definition: observe.hh:142
OutputDataFieldMap observe_field_values_
Stored field values.
Definition: observe.hh:231
std::ofstream observe_file_
Output file stream.
Definition: observe.hh:241
Accessor to input data conforming to declared Array.
Definition: accessors.hh:567
arma::vec3 input_point_
Input coordinates of the initial position of the observation point.
Definition: observe.hh:145
const std::vector< ObservePoint > & points() const
Definition: observe.hh:201
ObservePointData()
Constructor.
Definition: observe.hh:29
Definition: mesh.h:99
unsigned int element_idx_
Final element of the observe point. The index in the mesh.
Definition: observe.hh:30
std::string time_unit_str_
String representation of the time unit.
Definition: observe.hh:244
int rank_
Definition: observe.hh:223
std::string observe_name_
Definition: observe.hh:238
arma::vec local_coords_
Local (barycentric) coordinates on the element.
Definition: observe.hh:39
Basic time management class.
Input::Record in_rec_
Index in the input array.
Definition: observe.hh:123
unsigned int element_idx() const
Definition: observe.hh:67
arma::vec3 global_coords_
Global coordinates of the observation point.
Definition: observe.hh:36
unsigned int precision_
Precision of float output.
Definition: observe.hh:248
std::string name_
Observation point name.
Definition: observe.hh:126
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
std::vector< unsigned int > observed_element_indices_
Elements of the o_points.
Definition: observe.hh:228
double distance_
Definition: observe.hh:43
#define TYPEDEF_ERR_INFO(EI_Type, Type)
Macro to simplify declaration of error_info types.
Definition: exceptions.hh:194
double observe_values_time_
Common evaluation time of the fields for single time frame.
Definition: observe.hh:235
unsigned int snap_dim_
Definition: observe.hh:132
const std::vector< unsigned int > & observed_elements() const
Definition: observe.hh:185
ObservePointData observe_data_
Helper object stored projection data.
Definition: observe.hh:148
#define DECLARE_INPUT_EXCEPTION(ExcName, Format)
Macro for simple definition of input exceptions.
double time_unit_seconds_
Time unit in seconds.
Definition: observe.hh:246
std::map< string, OutputDataPtr > OutputDataFieldMap
Definition: observe.hh:165
std::shared_ptr< ElementDataCacheBase > OutputDataPtr
Definition: observe.hh:164
string snap_region_name_
Definition: observe.hh:137
Record type proxy class.
Definition: type_record.hh:182
arma::vec3 global_coords() const
Definition: observe.hh:73