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