Flow123d  release_3.0.0-1212-g8801db3
output_time.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 output_time.hh
15  * @brief
16  */
17 
18 #ifndef OUTPUT_TIME_HH_
19 #define OUTPUT_TIME_HH_
20 
21 #include <fstream> // for ofstream
22 #include <memory> // for shared_ptr
23 #include <string> // for string, allocator
24 #include <vector> // for vector
25 #include "input/accessors.hh" // for Iterator, Array (ptr only), Record
26 #include "system/file_path.hh" // for FilePath
27 
29 class Mesh;
30 class Observe;
31 class OutputMesh;
32 class OutputMeshBase;
34 namespace Input {
35  namespace Type {
36  class Abstract;
37  class Record;
38  }
39 }
40 template <typename T> class ElementDataCache;
41 
42 
43 /**
44  * \brief The class for outputting data during time.
45  *
46  * This class is descendant of Output class. This class is used for outputting
47  * data varying in time. Own output to specific file formats is done at other
48  * places to. See output_vtk.cc and output_msh.cc.
49  */
50 class OutputTime {
51 
52 public:
53  /// Default constructor. Only for testing.
54  OutputTime();
55 
56 
57  /**
58  * \brief Constructor of OutputTime object. It opens base file for writing.
59  *
60  * \param[in] equation_name The name of equation, used for forming output file name.
61  * \param[in] in_rec The reference on the input record
62  */
63  virtual void init_from_input(const std::string &equation_name, const Input::Record &in_rec, std::string unit_str);
64 
65  /**
66  * Common method to set scientific format and precision for output of floating point values to ASCII streams.
67  */
68  void set_stream_precision(std::ofstream &stream);
69 
70  /**
71  * \brief Destructor of OutputTime. It doesn't do anything, because all
72  * necessary destructors will be called in destructor of Output
73  */
74  virtual ~OutputTime();
75 
76  /**
77  * Return the input array for the output time set of the output stream.
78  */
79  Input::Iterator<Input::Array> get_time_set_array();
80 
81  /**
82  * Return the input record for the output mesh of the output stream.
83  */
84  Input::Iterator<Input::Record> get_output_mesh_record();
85 
86  /**
87  * \brief The specification of output stream
88  *
89  * \return This variable defines record for output stream
90  */
91  static const Input::Type::Record & get_input_type();
92 
93  /**
94  * \brief The specification of output file format
95  */
96  static Input::Type::Abstract & get_input_format_type();
97 
98  /**
99  * Types of reference data
100  *
101  * NATIVE_DATA represents output of FieldFE in our own format, Paraview ignores this format.
102  */
103  static const unsigned int N_DISCRETE_SPACES = 4;
105  NODE_DATA = 0,
106  CORNER_DATA = 1,
107  ELEM_DATA = 2,
108  NATIVE_DATA = 3,
109  MESH_DEFINITION = 9,
110  UNDEFINED = 10
111  };
112 
113  /**
114  * Maps names of output fields required by user to their indices in
115  * output_data_vec_.
116  */
117  typedef unsigned int DiscreteSpaceFlags;
118 
119  /**
120  * Map field name to its OutputData object.
121  */
122  typedef std::shared_ptr<ElementDataCacheBase> OutputDataPtr;
124 
125  /// pair of field name and shape (= Scalar 1, Vector 3, Tensor 9)
126  typedef std::pair< std::string, unsigned int > FieldInterpolationData;
128 
129  /**
130  * \brief This method delete all object instances of class OutputTime stored
131  * in output_streams vector
132  */
133  //static void destroy_all(void);
134 
135  /**
136  * \brief This method tries to create new instance of OutputTime according
137  * record in configuration file.
138  */
139  static std::shared_ptr<OutputTime> create_output_stream(const std::string &equation_name, const Input::Record &in_rec, std::string unit_str);
140 
141  /**
142  * Write all data registered as a new time frame.
143  */
144  void write_time_frame();
145 
146  /**
147  * Getter of the observe object.
148  */
149  std::shared_ptr<Observe> observe(Mesh *mesh);
150 
151  /**
152  * \brief Clear data for output computed by method @p compute_field_data.
153  */
154  void clear_data(void);
155 
156  /**
157  * Return if shared pointer to output data caches are created.
158  */
160  return (bool)(nodes_);
161  }
162 
163  /// Return auxiliary flag enable_refinement_.
164  inline bool enable_refinement() {
165  return enable_refinement_;
166  }
167 
168  /**
169  * Set shared pointers of output data caches.
170  *
171  * Set shared pointer of \p output_mesh_ (temporary solution).
172  */
173  virtual void set_output_data_caches(std::shared_ptr<OutputMeshBase> mesh_ptr);
174 
175  /**
176  * Get shared pointer of \p output_mesh_.
177  */
178  std::shared_ptr<OutputMeshBase> get_output_mesh_ptr();
179 
180  /**
181  * Update the last time is actual \p time is less than \p field_time
182  */
183  void update_time(double field_time);
184 
185  /**
186  * Prepare data for computing field values.
187  *
188  * Method:
189  * - compute discontinuous mesh if CORNER_DATA is calculated
190  * - find and return ElementDataCache of given field_name, create its if doesn't exist
191  *
192  * @param field_name Quantity name of founding ElementDataCache
193  * @param space_type Output discrete space
194  * @param n_rows Count of rows of data cache (used only if new cache is created)
195  * @param n_cols Count of columns of data cache (used only if new cache is created)
196  * @param size Size of data cache (used only if new cache is created and only for native data)
197  */
198  template <typename T>
199  ElementDataCache<T> & prepare_compute_data(std::string field_name, DiscreteSpace space_type, unsigned int n_rows, unsigned int n_cols);
200 
201  /**
202  * Return if output is serial or parallel
203  */
204  inline bool is_parallel() const {
205  return this->parallel_;
206  }
207 
208  /**
209  * Return rank of actual process
210  */
211  inline int rank() const {
212  return this->rank_;
213  }
214 
215  /**
216  * Return number of processes
217  */
218  inline int n_proc() const {
219  return this->n_proc_;
220  }
221 
222  /// Complete information about dummy fields, method has effect only for GMSH output.
223  virtual void add_dummy_fields();
224 
225 
226 protected:
227 
228  /**
229  * Change main filename to have prescribed extension.
230  */
231  void fix_main_file_extension(std::string extension);
232 
233 
234  /**
235  * \brief Return unique value current step for parallel or serial output.
236  *
237  * Respect value of \p parallel_ flag:
238  * - for serial output return actual value of \p current_step
239  * - for parallel output take unique value with account rank and number of processes
240  */
241  int get_parallel_current_step();
242 
243 
244  /**
245  * \brief Virtual method for writing data to output file
246  */
247  virtual int write_data(void) = 0;
248 
249  /**
250  * \brief Collect data of individual processes to serial data on master (0th) process
251  */
252  void gather_output_data(void);
253 
254  /**
255  * Cached MPI rank of process (is tested in methods)
256  */
257  int rank_;
258 
259  /**
260  * Cached MPI number of processes (is tested in methods)
261  */
262  int n_proc_;
263 
264  /**
265  * Registered output data. Single map for every value of DiscreteSpace
266  * corresponding to nodes, elements and corners.
267  */
268  OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES];
269 
270  /**
271  * Current step
272  */
274 
275  /**
276  * The newest time of registered data
277  */
278  double time;
279 
280  /**
281  * The last time, when data was wrote to this stream
282  */
283  double write_time;
284 
285  /**
286  * Record for current output stream
287  */
289 
290  /**
291  * Base output stream
292  */
293  ofstream _base_file;
294 
295  /**
296  * Name of base output file
297  */
299 
300  /**
301  * Name of the equation owning the output stream. Usually the balance equation.
302  * Used for forming default output file name and the name of observe output file.
303  */
304  std::string equation_name_;
305 
306  /**
307  * Number of decimal digits used for output of double values.
308  */
310 
311  /// Output mesh.
312  std::shared_ptr<OutputMeshBase> output_mesh_;
313 
314  std::shared_ptr<Observe> observe_;
315 
316  /// Auxiliary flag for refinement enabling, due to gmsh format.
318 
319  /// Parallel or serial version of file format (parallel has effect only for VTK)
320  bool parallel_;
321 
322  /// String representation of time unit.
323  string unit_string_;
324 
325  /// Vector of node coordinates. [spacedim x n_nodes]
326  std::shared_ptr<ElementDataCache<double>> nodes_;
327  /// Vector maps the nodes to their coordinates in vector @p nodes_.
328  std::shared_ptr<ElementDataCache<unsigned int>> connectivity_;
329  /// Vector of offsets of node indices of elements. Maps elements to their nodes in connectivity_.
330  std::shared_ptr<ElementDataCache<unsigned int>> offsets_;
331 
332 };
333 
334 
335 #endif /* OUTPUT_TIME_HH_ */
Class represents output mesh with continuous elements.
Definition: output_mesh.hh:232
double time
Definition: output_time.hh:278
Input::Record input_record_
Definition: output_time.hh:288
Base class for Output mesh.
Definition: output_mesh.hh:67
std::shared_ptr< ElementDataCache< unsigned int > > connectivity_
Vector maps the nodes to their coordinates in vector nodes_.
Definition: output_time.hh:328
std::shared_ptr< OutputMeshBase > output_mesh_
Output mesh.
Definition: output_time.hh:312
std::shared_ptr< ElementDataCache< unsigned int > > offsets_
Vector of offsets of node indices of elements. Maps elements to their nodes in connectivity_.
Definition: output_time.hh:330
FilePath _base_filename
Definition: output_time.hh:298
Abstract linear system class.
Definition: balance.hh:37
std::shared_ptr< Observe > observe_
Definition: output_time.hh:314
Definition: mesh.h:76
std::shared_ptr< ElementDataCacheBase > OutputDataPtr
Definition: output_time.hh:122
std::map< DiscreteSpace, std::vector< FieldInterpolationData > > InterpolationMap
Definition: output_time.hh:127
std::shared_ptr< ElementDataCache< double > > nodes_
Vector of node coordinates. [spacedim x n_nodes].
Definition: output_time.hh:326
bool is_output_data_caches_init()
Definition: output_time.hh:159
unsigned int DiscreteSpaceFlags
Definition: output_time.hh:117
bool parallel_
Parallel or serial version of file format (parallel has effect only for VTK)
Definition: output_time.hh:320
int rank() const
Definition: output_time.hh:211
Class represents output mesh with discontinuous elements.
Definition: output_mesh.hh:260
bool enable_refinement()
Return auxiliary flag enable_refinement_.
Definition: output_time.hh:164
int current_step
Definition: output_time.hh:273
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
string unit_string_
String representation of time unit.
Definition: output_time.hh:323
std::pair< std::string, unsigned int > FieldInterpolationData
pair of field name and shape (= Scalar 1, Vector 3, Tensor 9)
Definition: output_time.hh:126
ofstream _base_file
Definition: output_time.hh:293
The class for outputting data during time.
Definition: output_time.hh:50
Class for declaration of polymorphic Record.
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
int n_proc() const
Definition: output_time.hh:218
bool is_parallel() const
Definition: output_time.hh:204
Record type proxy class.
Definition: type_record.hh:182
bool enable_refinement_
Auxiliary flag for refinement enabling, due to gmsh format.
Definition: output_time.hh:317
double write_time
Definition: output_time.hh:283
std::vector< OutputDataPtr > OutputDataFieldVec
Definition: output_time.hh:123
std::string equation_name_
Definition: output_time.hh:304