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