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