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