Flow123d  release_3.0.0-973-g92f55e826
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  */
80 
81  /**
82  * Return the input record for the output mesh of the output stream.
83  */
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  */
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;
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  OutputDataPtr 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 
223 protected:
224 
225  /**
226  * Change main filename to have prescribed extension.
227  */
228  void fix_main_file_extension(std::string extension);
229 
230 
231  /**
232  * \brief Return unique value current step for parallel or serial output.
233  *
234  * Respect value of \p parallel_ flag:
235  * - for serial output return actual value of \p current_step
236  * - for parallel output take unique value with account rank and number of processes
237  */
239 
240 
241  /**
242  * \brief Virtual method for writing data to output file
243  */
244  virtual int write_data(void) = 0;
245 
246  /**
247  * \brief Collect data of individual processes to serial data on master (0th) process
248  */
249  void gather_output_data(void);
250 
251  /**
252  * Cached MPI rank of process (is tested in methods)
253  */
254  int rank_;
255 
256  /**
257  * Cached MPI number of processes (is tested in methods)
258  */
259  int n_proc_;
260 
261  /**
262  * Registered output data. Single map for every value of DiscreteSpace
263  * corresponding to nodes, elements and corners.
264  */
266 
267  /**
268  * Current step
269  */
271 
272  /**
273  * The newest time of registered data
274  */
275  double time;
276 
277  /**
278  * The last time, when data was wrote to this stream
279  */
280  double write_time;
281 
282  /**
283  * Record for current output stream
284  */
286 
287  /**
288  * Base output stream
289  */
290  ofstream _base_file;
291 
292  /**
293  * Name of base output file
294  */
296 
297  /**
298  * Name of the equation owning the output stream. Usually the balance equation.
299  * Used for forming default output file name and the name of observe output file.
300  */
301  std::string equation_name_;
302 
303  /**
304  * Number of decimal digits used for output of double values.
305  */
307 
308  /// Output mesh.
309  std::shared_ptr<OutputMeshBase> output_mesh_;
310 
311  std::shared_ptr<Observe> observe_;
312 
313  /// Auxiliary flag for refinement enabling, due to gmsh format.
315 
316  /// Parallel or serial version of file format (parallel has effect only for VTK)
317  bool parallel_;
318 
319  /// String representation of time unit.
320  string unit_string_;
321 
322  /// Vector of node coordinates. [spacedim x n_nodes]
323  std::shared_ptr<ElementDataCache<double>> nodes_;
324  /// Vector maps the nodes to their coordinates in vector @p nodes_.
325  std::shared_ptr<ElementDataCache<unsigned int>> connectivity_;
326  /// Vector of offsets of node indices of elements. Maps elements to their nodes in connectivity_.
327  std::shared_ptr<ElementDataCache<unsigned int>> offsets_;
328 
329 };
330 
331 
332 #endif /* OUTPUT_TIME_HH_ */
OutputTime::init_from_input
virtual void init_from_input(const std::string &equation_name, const Input::Record &in_rec, std::string unit_str)
Constructor of OutputTime object. It opens base file for writing.
Definition: output_time.cc:84
OutputTime::FieldInterpolationData
std::pair< std::string, unsigned int > FieldInterpolationData
pair of field name and shape (= Scalar 1, Vector 3, Tensor 9)
Definition: output_time.hh:126
Observe
Definition: observe.hh:182
OutputTime::rank_
int rank_
Definition: output_time.hh:254
OutputTime::gather_output_data
void gather_output_data(void)
Collect data of individual processes to serial data on master (0th) process.
Definition: output_time.cc:258
OutputTime::get_time_set_array
Input::Iterator< Input::Array > get_time_set_array()
Definition: output_time.cc:121
OutputTime::unit_string_
string unit_string_
String representation of time unit.
Definition: output_time.hh:320
OutputTime::current_step
int current_step
Definition: output_time.hh:270
Input
Abstract linear system class.
Definition: balance.hh:37
OutputTime::~OutputTime
virtual ~OutputTime()
Destructor of OutputTime. It doesn't do anything, because all necessary destructors will be called in...
Definition: output_time.cc:107
OutputTime::clear_data
void clear_data(void)
Clear data for output computed by method compute_field_data.
Definition: output_time.cc:242
OutputTime::output_data_vec_
OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES]
Definition: output_time.hh:265
ElementDataCache
Definition: element_data_cache.hh:44
file_path.hh
OutputTime::enable_refinement_
bool enable_refinement_
Auxiliary flag for refinement enabling, due to gmsh format.
Definition: output_time.hh:314
OutputTime::write_time_frame
void write_time_frame()
Definition: output_time.cc:199
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
OutputTime::OutputDataFieldVec
std::vector< OutputDataPtr > OutputDataFieldVec
Definition: output_time.hh:123
OutputTime::set_stream_precision
void set_stream_precision(std::ofstream &stream)
Definition: output_time.cc:100
std::vector< OutputDataPtr >
OutputTime::_base_file
ofstream _base_file
Definition: output_time.hh:290
OutputTime::ELEM_DATA
@ ELEM_DATA
Definition: output_time.hh:107
OutputMeshDiscontinuous
Class represents output mesh with discontinuous elements.
Definition: output_mesh.hh:260
OutputTime::rank
int rank() const
Definition: output_time.hh:211
OutputTime::get_output_mesh_record
Input::Iterator< Input::Record > get_output_mesh_record()
Definition: output_time.cc:126
Input::Iterator
Definition: accessors.hh:143
OutputTime::write_data
virtual int write_data(void)=0
Virtual method for writing data to output file.
OutputTime::write_time
double write_time
Definition: output_time.hh:280
OutputTime::input_record_
Input::Record input_record_
Definition: output_time.hh:285
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
OutputTime::MESH_DEFINITION
@ MESH_DEFINITION
Definition: output_time.hh:109
OutputTime::equation_name_
std::string equation_name_
Definition: output_time.hh:301
accessors.hh
OutputTime::_base_filename
FilePath _base_filename
Definition: output_time.hh:295
Input::Type::Abstract
Class for declaration of polymorphic Record.
Definition: type_abstract.hh:62
OutputTime
The class for outputting data during time.
Definition: output_time.hh:50
OutputTime::fix_main_file_extension
void fix_main_file_extension(std::string extension)
Definition: output_time.cc:151
ElementDataCacheBase
Definition: element_data_cache_base.hh:33
OutputTime::time
double time
Definition: output_time.hh:275
OutputTime::connectivity_
std::shared_ptr< ElementDataCache< unsigned int > > connectivity_
Vector maps the nodes to their coordinates in vector nodes_.
Definition: output_time.hh:325
std::map
Definition: doxy_dummy_defs.hh:11
OutputMeshBase
Base class for Output mesh.
Definition: output_mesh.hh:67
OutputTime::nodes_
std::shared_ptr< ElementDataCache< double > > nodes_
Vector of node coordinates. [spacedim x n_nodes].
Definition: output_time.hh:323
OutputTime::observe_
std::shared_ptr< Observe > observe_
Definition: output_time.hh:311
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
OutputTime::n_proc_
int n_proc_
Definition: output_time.hh:259
OutputTime::set_output_data_caches
virtual void set_output_data_caches(std::shared_ptr< OutputMeshBase > mesh_ptr)
Definition: output_time.cc:131
OutputTime::is_parallel
bool is_parallel() const
Definition: output_time.hh:204
OutputTime::UNDEFINED
@ UNDEFINED
Definition: output_time.hh:110
OutputTime::get_input_format_type
static Input::Type::Abstract & get_input_format_type()
The specification of output file format.
Definition: output_time.cc:65
OutputTime::update_time
void update_time(double field_time)
Definition: output_time.cc:144
Mesh
Definition: mesh.h:80
OutputTime::get_input_type
static const Input::Type::Record & get_input_type()
The specification of output stream.
Definition: output_time.cc:38
OutputTime::get_output_mesh_ptr
std::shared_ptr< OutputMeshBase > get_output_mesh_ptr()
Definition: output_time.cc:139
OutputTime::NODE_DATA
@ NODE_DATA
Definition: output_time.hh:105
OutputTime::OutputTime
OutputTime()
Default constructor. Only for testing.
Definition: output_time.cc:72
OutputTime::offsets_
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:327
OutputTime::OutputDataPtr
std::shared_ptr< ElementDataCacheBase > OutputDataPtr
Definition: output_time.hh:122
OutputTime::is_output_data_caches_init
bool is_output_data_caches_init()
Definition: output_time.hh:159
OutputTime::n_proc
int n_proc() const
Definition: output_time.hh:218
OutputTime::CORNER_DATA
@ CORNER_DATA
Definition: output_time.hh:106
OutputTime::DiscreteSpaceFlags
unsigned int DiscreteSpaceFlags
Definition: output_time.hh:117
OutputTime::output_mesh_
std::shared_ptr< OutputMeshBase > output_mesh_
Output mesh.
Definition: output_time.hh:309
OutputTime::create_output_stream
static std::shared_ptr< OutputTime > create_output_stream(const std::string &equation_name, const Input::Record &in_rec, std::string unit_str)
This method delete all object instances of class OutputTime stored in output_streams vector.
Definition: output_time.cc:185
OutputTime::observe
std::shared_ptr< Observe > observe(Mesh *mesh)
Definition: output_time.cc:230
DiscreteSpace
Definition: discrete_space.hh:38
OutputTime::prepare_compute_data
OutputDataPtr prepare_compute_data(std::string field_name, DiscreteSpace space_type, unsigned int n_rows, unsigned int n_cols)
Definition: output_time.impl.hh:78
OutputTime::get_parallel_current_step
int get_parallel_current_step()
Return unique value current step for parallel or serial output.
Definition: output_time.cc:251
OutputMesh
Class represents output mesh with continuous elements.
Definition: output_mesh.hh:232
OutputTime::InterpolationMap
std::map< DiscreteSpace, std::vector< FieldInterpolationData > > InterpolationMap
Definition: output_time.hh:127
OutputTime::N_DISCRETE_SPACES
static const unsigned int N_DISCRETE_SPACES
Definition: output_time.hh:103
OutputTime::precision_
int precision_
Definition: output_time.hh:306
OutputTime::parallel_
bool parallel_
Parallel or serial version of file format (parallel has effect only for VTK)
Definition: output_time.hh:317
OutputTime::NATIVE_DATA
@ NATIVE_DATA
Definition: output_time.hh:108
OutputTime::enable_refinement
bool enable_refinement()
Return auxiliary flag enable_refinement_.
Definition: output_time.hh:164