Flow123d  release_3.0.0-680-gbed5aba
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 base output parameters (rank and parallel) which determine if the output is performed
203  */
204  inline void get_output_params(bool &parallel, int &rank) {
205  parallel = this->parallel_;
206  rank = this->rank;
207  }
208 
209  /// Complete information about dummy fields, method has effect only for GMSH output.
210  virtual void add_dummy_fields();
211 
212 
213 protected:
214 
215  /**
216  * Change main filename to have prescribed extension.
217  */
218  void fix_main_file_extension(std::string extension);
219 
220 
221  /**
222  * \brief Return unique value current step for parallel or serial output.
223  *
224  * Respect value of \p parallel_ flag:
225  * - for serial output return actual value of \p current_step
226  * - for parallel output take unique value with account rank and number of processes
227  */
228  int get_parallel_current_step();
229 
230 
231  /**
232  * \brief Virtual method for writing data to output file
233  */
234  virtual int write_data(void) = 0;
235 
236  /**
237  * Cached MPI rank of process (is tested in methods)
238  */
239  int rank;
240 
241  /**
242  * Cached MPI number of processes (is tested in methods)
243  */
244  int n_proc;
245 
246  /**
247  * Registered output data. Single map for every value of DiscreteSpace
248  * corresponding to nodes, elements and corners.
249  */
250  OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES];
251 
252  /**
253  * Current step
254  */
256 
257  /**
258  * The newest time of registered data
259  */
260  double time;
261 
262  /**
263  * The last time, when data was wrote to this stream
264  */
265  double write_time;
266 
267  /**
268  * Record for current output stream
269  */
271 
272  /**
273  * Base output stream
274  */
275  ofstream _base_file;
276 
277  /**
278  * Name of base output file
279  */
281 
282  /**
283  * Name of the equation owning the output stream. Usually the balance equation.
284  * Used for forming default output file name and the name of observe output file.
285  */
286  std::string equation_name_;
287 
288  /**
289  * Number of decimal digits used for output of double values.
290  */
292 
293  /// Output mesh.
294  std::shared_ptr<OutputMeshBase> output_mesh_;
295 
296  std::shared_ptr<Observe> observe_;
297 
298  /// Auxiliary flag for refinement enabling, due to gmsh format.
300 
301  /// Parallel or serial version of file format (parallel has effect only for VTK)
302  bool parallel_;
303 
304  /// String representation of time unit.
305  string unit_string_;
306 
307  /// Vector of node coordinates. [spacedim x n_nodes]
308  std::shared_ptr<ElementDataCache<double>> nodes_;
309  /// Vector maps the nodes to their coordinates in vector @p nodes_.
310  std::shared_ptr<ElementDataCache<unsigned int>> connectivity_;
311  /// Vector of offsets of node indices of elements. Maps elements to their nodes in connectivity_.
312  std::shared_ptr<ElementDataCache<unsigned int>> offsets_;
313 
314 };
315 
316 
317 #endif /* OUTPUT_TIME_HH_ */
Class represents output mesh with continuous elements.
Definition: output_mesh.hh:166
double time
Definition: output_time.hh:260
Input::Record input_record_
Definition: output_time.hh:270
Base class for Output mesh.
Definition: output_mesh.hh:61
std::shared_ptr< ElementDataCache< unsigned int > > connectivity_
Vector maps the nodes to their coordinates in vector nodes_.
Definition: output_time.hh:310
std::shared_ptr< OutputMeshBase > output_mesh_
Output mesh.
Definition: output_time.hh:294
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:312
FilePath _base_filename
Definition: output_time.hh:280
Abstract linear system class.
Definition: balance.hh:35
std::shared_ptr< Observe > observe_
Definition: output_time.hh:296
Definition: mesh.h:80
std::shared_ptr< ElementDataCacheBase > OutputDataPtr
Definition: output_time.hh:122
std::map< DiscreteSpace, std::vector< FieldInterpolationData > > InterpolationMap
Definition: output_time.hh:127
void get_output_params(bool &parallel, int &rank)
Definition: output_time.hh:204
std::shared_ptr< ElementDataCache< double > > nodes_
Vector of node coordinates. [spacedim x n_nodes].
Definition: output_time.hh:308
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:302
Class represents output mesh with discontinuous elements.
Definition: output_mesh.hh:191
bool enable_refinement()
Return auxiliary flag enable_refinement_.
Definition: output_time.hh:164
int current_step
Definition: output_time.hh:255
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
string unit_string_
String representation of time unit.
Definition: output_time.hh:305
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:275
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
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:299
double write_time
Definition: output_time.hh:265
std::vector< OutputDataPtr > OutputDataFieldVec
Definition: output_time.hh:123
std::string equation_name_
Definition: output_time.hh:286