Flow123d  JS_before_hm-1008-g3dab983
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  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  */
238  int get_parallel_current_step();
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  */
265  OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES];
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_ */
Class represents output mesh with continuous elements.
Definition: output_mesh.hh:235
double time
Definition: output_time.hh:275
Input::Record input_record_
Definition: output_time.hh:285
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:325
std::shared_ptr< OutputMeshBase > output_mesh_
Output mesh.
Definition: output_time.hh:309
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
FilePath _base_filename
Definition: output_time.hh:295
Abstract linear system class.
Definition: balance.hh:40
std::shared_ptr< Observe > observe_
Definition: output_time.hh:311
Definition: mesh.h:78
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:323
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:317
int rank() const
Definition: output_time.hh:211
Class represents output mesh with discontinuous elements.
Definition: output_mesh.hh:263
bool enable_refinement()
Return auxiliary flag enable_refinement_.
Definition: output_time.hh:164
int current_step
Definition: output_time.hh:270
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
string unit_string_
String representation of time unit.
Definition: output_time.hh:320
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:290
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:314
double write_time
Definition: output_time.hh:280
std::vector< OutputDataPtr > OutputDataFieldVec
Definition: output_time.hh:123
std::string equation_name_
Definition: output_time.hh:301