Flow123d  JS_before_hm-1598-g3b021b4
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  */
83  Input::Iterator<Input::Array> get_time_set_array();
84 
85  /**
86  * Return the input record for the output mesh of the output stream.
87  */
88  Input::Iterator<Input::Record> get_output_mesh_record();
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  */
100  static Input::Type::Abstract & get_input_format_type();
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;
109  NODE_DATA = 0,
110  CORNER_DATA = 1,
111  ELEM_DATA = 2,
112  NATIVE_DATA = 3,
113  MESH_DEFINITION = 9,
114  UNDEFINED = 10
115  };
116 
117  /**
118  * Maps names of output fields required by user to their indices in
119  * output_data_vec_.
120  */
121  typedef unsigned int DiscreteSpaceFlags;
122 
123  /**
124  * Map field name to its OutputData object.
125  */
126  typedef std::shared_ptr<ElementDataCacheBase> OutputDataPtr;
128 
129  /// pair of field name and shape (= Scalar 1, Vector 3, Tensor 9)
130  typedef std::pair< std::string, unsigned int > FieldInterpolationData;
132 
133  /**
134  * \brief This method delete all object instances of class OutputTime stored
135  * in output_streams vector
136  */
137  //static void destroy_all(void);
138 
139  /**
140  * \brief This method tries to create new instance of OutputTime according
141  * record in configuration file.
142  */
143  static std::shared_ptr<OutputTime> create_output_stream(const std::string &equation_name,
144  const Input::Record &in_rec,
145  const std::shared_ptr<TimeUnitConversion>& time_unit_conv);
146 
147  /**
148  * Write all data registered as a new time frame.
149  */
150  void write_time_frame();
151 
152  /**
153  * Getter of the observe object.
154  */
155  std::shared_ptr<Observe> observe(Mesh *mesh);
156 
157  /**
158  * \brief Clear data for output computed by method @p compute_field_data.
159  */
160  void clear_data(void);
161 
162  /**
163  * Return if shared pointer to output data caches are created.
164  */
166  return (bool)(nodes_);
167  }
168 
169  /// Return auxiliary flag enable_refinement_.
170  inline bool enable_refinement() {
171  return enable_refinement_;
172  }
173 
174  /**
175  * Set shared pointers of output data caches.
176  *
177  * Set shared pointer of \p output_mesh_ (temporary solution).
178  */
179  virtual void set_output_data_caches(std::shared_ptr<OutputMeshBase> mesh_ptr);
180 
181  /**
182  * Get shared pointer of \p output_mesh_.
183  */
184  std::shared_ptr<OutputMeshBase> get_output_mesh_ptr();
185 
186  /**
187  * Update the last time is actual \p time is less than \p field_time
188  */
189  void update_time(double field_time);
190 
191  /**
192  * Prepare data for computing field values.
193  *
194  * Method:
195  * - compute discontinuous mesh if CORNER_DATA is calculated
196  * - find and return ElementDataCache of given field_name, create its if doesn't exist
197  *
198  * @param field_name Quantity name of founding ElementDataCache
199  * @param space_type Output discrete space
200  * @param n_rows Count of rows of data cache (used only if new cache is created)
201  * @param n_cols Count of columns of data cache (used only if new cache is created)
202  * @param size Size of data cache (used only if new cache is created and only for native data)
203  */
204  template <typename T>
205  OutputDataPtr prepare_compute_data(std::string field_name, DiscreteSpace space_type, unsigned int n_rows, unsigned int n_cols);
206 
207  /**
208  * Return if output is serial or parallel
209  */
210  inline bool is_parallel() const {
211  return this->parallel_;
212  }
213 
214  /**
215  * Return rank of actual process
216  */
217  inline int rank() const {
218  return this->rank_;
219  }
220 
221  /**
222  * Return number of processes
223  */
224  inline int n_proc() const {
225  return this->n_proc_;
226  }
227 
228  /// Getter to registered time
229  inline double registered_time() const {
230  return registered_time_;
231  }
232 
233 
234 protected:
235 
236  /**
237  * Change main filename to have prescribed extension.
238  */
239  void fix_main_file_extension(std::string extension);
240 
241 
242  /**
243  * \brief Return unique value current step for parallel or serial output.
244  *
245  * Respect value of \p parallel_ flag:
246  * - for serial output return actual value of \p current_step
247  * - for parallel output take unique value with account rank and number of processes
248  */
249  int get_parallel_current_step();
250 
251 
252  /**
253  * \brief Virtual method for writing data to output file
254  */
255  virtual int write_data(void) = 0;
256 
257  /**
258  * \brief Collect data of individual processes to serial data on master (0th) process
259  */
260  void gather_output_data(void);
261 
262  /**
263  * Cached MPI rank of process (is tested in methods)
264  */
265  int rank_;
266 
267  /**
268  * Cached MPI number of processes (is tested in methods)
269  */
270  int n_proc_;
271 
272  /**
273  * Registered output data. Single map for every value of DiscreteSpace
274  * corresponding to nodes, elements and corners.
275  */
276  OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES];
277 
278  /**
279  * Current step
280  */
282 
283  /**
284  * The newest time of registered data
285  */
287 
288  /**
289  * The last time, when data was wrote to this stream
290  */
291  double write_time;
292 
293  /**
294  * Record for current output stream
295  */
297 
298  /**
299  * Base output stream
300  */
301  ofstream _base_file;
302 
303  /**
304  * Name of base output file
305  */
307 
308  /**
309  * Name of the equation owning the output stream. Usually the balance equation.
310  * Used for forming default output file name and the name of observe output file.
311  */
312  std::string equation_name_;
313 
314  /**
315  * Number of decimal digits used for output of double values.
316  */
318 
319  /// Output mesh.
320  std::shared_ptr<OutputMeshBase> output_mesh_;
321 
322  std::shared_ptr<Observe> observe_;
323 
324  /// Auxiliary flag for refinement enabling, due to gmsh format.
326 
327  /// Parallel or serial version of file format (parallel has effect only for VTK)
328  bool parallel_;
329 
330  /// Time unit conversion object from the equation time governor.
331  std::shared_ptr<TimeUnitConversion> time_unit_converter;
332 
333  /// Vector of node coordinates. [spacedim x n_nodes]
334  std::shared_ptr<ElementDataCache<double>> nodes_;
335  /// Vector maps the nodes to their coordinates in vector @p nodes_.
336  std::shared_ptr<ElementDataCache<unsigned int>> connectivity_;
337  /// Vector of offsets of node indices of elements. Maps elements to their nodes in connectivity_.
338  std::shared_ptr<ElementDataCache<unsigned int>> offsets_;
339 
340 };
341 
342 
343 #endif /* OUTPUT_TIME_HH_ */
Class represents output mesh with continuous elements.
Definition: output_mesh.hh:235
Input::Record input_record_
Definition: output_time.hh:296
Base class for Output mesh.
Definition: output_mesh.hh:70
std::shared_ptr< ElementDataCache< unsigned int > > connectivity_
Vector maps the nodes to their coordinates in vector nodes_.
Definition: output_time.hh:336
std::shared_ptr< OutputMeshBase > output_mesh_
Output mesh.
Definition: output_time.hh:320
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:338
FilePath _base_filename
Definition: output_time.hh:306
Abstract linear system class.
Definition: balance.hh:40
std::shared_ptr< Observe > observe_
Definition: output_time.hh:322
Definition: mesh.h:77
std::shared_ptr< ElementDataCacheBase > OutputDataPtr
Definition: output_time.hh:126
std::map< DiscreteSpace, std::vector< FieldInterpolationData > > InterpolationMap
Definition: output_time.hh:131
std::shared_ptr< ElementDataCache< double > > nodes_
Vector of node coordinates. [spacedim x n_nodes].
Definition: output_time.hh:334
bool is_output_data_caches_init()
Definition: output_time.hh:165
Helper class storing unit conversion coefficient and functionality for conversion of units...
unsigned int DiscreteSpaceFlags
Definition: output_time.hh:121
bool parallel_
Parallel or serial version of file format (parallel has effect only for VTK)
Definition: output_time.hh:328
int rank() const
Definition: output_time.hh:217
Class represents output mesh with discontinuous elements.
Definition: output_mesh.hh:263
bool enable_refinement()
Return auxiliary flag enable_refinement_.
Definition: output_time.hh:170
int current_step
Definition: output_time.hh:281
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
std::pair< std::string, unsigned int > FieldInterpolationData
pair of field name and shape (= Scalar 1, Vector 3, Tensor 9)
Definition: output_time.hh:130
ofstream _base_file
Definition: output_time.hh:301
The class for outputting data during time.
Definition: output_time.hh:51
std::shared_ptr< TimeUnitConversion > time_unit_converter
Time unit conversion object from the equation time governor.
Definition: output_time.hh:331
Class for declaration of polymorphic Record.
double registered_time() const
Getter to registered time.
Definition: output_time.hh:229
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
int n_proc() const
Definition: output_time.hh:224
bool is_parallel() const
Definition: output_time.hh:210
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:325
double write_time
Definition: output_time.hh:291
std::vector< OutputDataPtr > OutputDataFieldVec
Definition: output_time.hh:127
double registered_time_
Definition: output_time.hh:286
std::string equation_name_
Definition: output_time.hh:312