Flow123d  release_3.0.0-684-g928e266
output_msh.cc
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_msh.cc
15  * @brief The functions for outputs to GMSH files.
16  */
17 
18 #include "mesh/side_impl.hh"
19 #include "output_msh.hh"
20 #include "output_mesh.hh"
21 #include "output_element.hh"
22 #include "mesh/mesh.h"
24 #include "input/factory.hh"
25 #include "tools/unit_si.hh"
26 
27 
29 
30 
31 using namespace Input::Type;
32 
33 
34 /**
35  * Auxiliary implementation of ElementDataCacheBase that performs output of single zero data for the fields that are
36  * off for current time frame.
37  */
39 public:
40 
41  DummyOutputData(std::string field_name_in, ElementDataCacheBase::NumCompValueType n_elem_in)
42  {
43  this->field_input_name_ = field_name_in;
44  this->n_elem_ = n_elem_in;
45  this->n_values_ = 1;
46  }
47 
48  virtual ~DummyOutputData() override
49  {}
50 
51  void print_ascii(ostream &out_stream, unsigned int idx) override
52  {
53  for(unsigned int i=0; i< n_elem_;i++) out_stream << 0 << " ";
54  }
55 
56  void print_ascii_all(ostream &out_stream) override
57  {
58  for(unsigned int i=0; i< n_elem_;i++) out_stream << 0 << " ";
59  }
60 
61  void print_binary_all(ostream &out_stream, bool print_data_size = true) override
62  {
63  ASSERT(false).error("Not implemented.");
64  }
65 
66  void print_all_yaml(ostream &out_stream, unsigned int precision) override
67  {}
68 
69  void get_min_max_range(double &min, double &max) override
70  {}
71 
72  void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row) override
73  {}
74 
75  void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row) override
76  {}
77 };
78 
79 
80 
81 
82 
83 
84 
85 const Record & OutputMSH::get_input_type() {
86  return Record("gmsh", "Parameters of gmsh output format.")
87  // It is derived from abstract class
89  .close();
90 }
91 
92 const int OutputMSH::registrar = Input::register_class< OutputMSH >("gmsh") +
94 
95 
97 {
98  this->enable_refinement_ = false;
99  this->header_written = false;
100 
102 
103 
104 }
105 
107 {
108  this->write_tail();
109 }
110 
111 
112 
113 
115 {
116  ofstream &file = this->_base_file;
117 
118  // Write simple header
119  file << "$MeshFormat" << endl;
120  file << "2" << " 0 " << sizeof(double) << endl;
121  file << "$EndMeshFormat" << endl;
122 }
123 
125 {
126  ofstream &file = this->_base_file;
127 
128  // Write information about nodes
129  file << "$Nodes" << endl;
130  file << this->nodes_->n_values() << endl;
131  auto &id_node_vec = *( this->node_ids_->get_component_data(0).get() );
132  unsigned int i_node=0;
133  for(unsigned int i_node=0; i_node < id_node_vec.size(); ++i_node) {
134  file << id_node_vec[i_node] << " ";
135  this->nodes_->print_ascii(file, i_node);
136  file << endl;
137  }
138  file << "$EndNodes" << endl;
139 }
140 
142 {
143  ofstream &file = this->_base_file;
144  const static unsigned int gmsh_simplex_types_[4] = {0, 1, 2, 4};
145  auto &id_elem_vec = *( this->elem_ids_->get_component_data(0).get() );
146  auto &id_node_vec = *( this->node_ids_->get_component_data(0).get() );
147  auto &connectivity_vec = *( this->connectivity_->get_component_data(0).get() );
148  auto &offsets_vec = *( this->offsets_->get_component_data(0).get() );
149  auto &regions_vec = *( this->region_ids_->get_component_data(0).get() );
150  auto &partition_vec = *( this->partitions_->get_component_data(0).get() );
151 
152  unsigned int n_nodes, i_node=0;
153 
154  // Write information about elements
155  file << "$Elements" << endl;
156  file << this->offsets_->n_values() << endl;
158  for(unsigned int i_elm=0; i_elm < id_elem_vec.size(); ++i_elm) {
159  n_nodes = (i_elm==0) ? (offsets_vec[0]) : (offsets_vec[i_elm]-offsets_vec[i_elm-1]);
160  // element_id element_type 3_other_tags material region partition
161  file << id_elem_vec[i_elm]
162  << " " << gmsh_simplex_types_[ n_nodes-1 ]
163  << " 3 " << regions_vec[i_elm] << " " << regions_vec[i_elm] << " " << partition_vec[i_elm];
164 
165  for(unsigned int i=0; i<n_nodes; i++, i_node++) {
166  file << " " << id_node_vec[connectivity_vec[i_node]];
167  }
168  file << endl;
169  }
170  file << "$EndElements" << endl;
171 }
172 
173 
174 void OutputMSH::write_msh_ascii_data(std::shared_ptr<ElementDataCache<unsigned int>> id_cache, OutputDataPtr output_data, bool discont)
175 {
176  ofstream &file = this->_base_file;
177  auto &id_vec = *( id_cache->get_component_data(0).get() );
178 
179  if (discont) { // corner data
180  auto &offsets_vec = *( this->offsets_->get_component_data(0).get() );
181  unsigned int n_nodes, i_corner=0;
182  for(unsigned int i=0; i < id_vec.size(); ++i) {
183  n_nodes = (i==0) ? (offsets_vec[0]) : (offsets_vec[i]-offsets_vec[i-1]);
184  file << id_vec[i] << " " << n_nodes << " ";
185  for (unsigned int j=0; j<n_nodes; j++)
186  output_data->print_ascii(file, i_corner++);
187  file << std::endl;
188  }
189  } else { // element / node data
190  for(unsigned int i=0; i < output_data->n_values(); ++i) {
191  file << id_vec[i] << " ";
192  output_data->print_ascii(file, i);
193  file << std::endl;
194  }
195 
196  }
197 }
198 
199 
201 {
202  ofstream &file = this->_base_file;
203  double time_fixed = isfinite(this->time)?this->time:0;
204  time_fixed /= UnitSI().s().convert_unit_from(this->unit_string_);
205 
206  file << "$NodeData" << endl;
207 
208  file << "1" << endl; // one string tag
209  file << "\"" << output_data->field_input_name() <<"\"" << endl;
210 
211  file << "1" << endl; // one real tag
212  file << time_fixed << endl; // first real tag = time
213 
214  file << "3" << endl; // 3 integer tags
215  file << this->current_step << endl; // step number (start = 0)
216  file << output_data->n_elem() << endl; // number of components
217  file << output_data->n_values() << endl; // number of values
218 
219  this->write_msh_ascii_data(this->node_ids_, output_data);
220 
221  file << "$EndNodeData" << endl;
222 }
223 
224 
226 {
227  ofstream &file = this->_base_file;
228  double time_fixed = isfinite(this->time)?this->time:0;
229 
230  file << "$ElementNodeData" << endl;
231 
232  file << "1" << endl; // one string tag
233  file << "\"" << output_data->field_input_name() <<"\"" << endl;
234 
235  file << "1" << endl; // one real tag
236  file << time_fixed << endl; // first real tag = time
237 
238  file << "3" << endl; // 3 integer tags
239  file << this->current_step << endl; // step number (start = 0)
240  file << output_data->n_elem() << endl; // number of components
241  file << this->offsets_->n_values() << endl; // number of values
242 
243  this->write_msh_ascii_data(this->elem_ids_, output_data, true);
244 
245  file << "$EndElementNodeData" << endl;
246 }
247 
249 {
250  ofstream &file = this->_base_file;
251  double time_fixed = isfinite(this->time)?this->time:0;
252 
253  file << "$ElementData" << endl;
254 
255  file << "1" << endl; // one string tag
256  file << "\"" << output_data->field_input_name() <<"\"" << endl;
257 
258  file << "1" << endl; // one real tag
259  file << time_fixed << endl; // first real tag = time
260 
261  file << "3" << endl; // 3 integer tags
262  file << this->current_step << endl; // step number (start = 0)
263  file << output_data->n_elem() << endl; // number of components
264  file << output_data->n_values() << endl; // number of values
265 
266  this->write_msh_ascii_data(this->elem_ids_, output_data);
267 
268  file << "$EndElementData" << endl;
269 }
270 
272 {
273  LogOut() << __func__ << ": Writing output file " << this->_base_filename << " ... ";
274 
275  this->write_msh_header();
276 
277  this->write_msh_geometry();
278 
279  this->write_msh_topology();
280 
281  LogOut() << "O.K.";
282 
283  return 1;
284 }
285 
287 {
288  // Write header with mesh, when it hasn't been written to output file yet
289  if(this->header_written == false) {
290  if(this->rank == 0) {
291  this->fix_main_file_extension(".msh");
292  try {
293  this->_base_filename.open_stream( this->_base_file );
294  this->set_stream_precision(this->_base_file);
295  } INPUT_CATCH(FilePath::ExcFileOpen, FilePath::EI_Address_String, input_record_)
296  }
297 
298  this->write_head();
299  this->header_written = true;
300  }
301 
302  LogOut() << __func__ << ": Writing output file " << this->_base_filename << " ... ";
303 
304 
305  auto &node_data_list = this->output_data_vec_[NODE_DATA];
306  for(auto data_it = node_data_list.begin(); data_it != node_data_list.end(); ++data_it) {
307  write_node_data(*data_it);
308  }
309  auto &corner_data_list = this->output_data_vec_[CORNER_DATA];
310  for(auto data_it = corner_data_list.begin(); data_it != corner_data_list.end(); ++data_it) {
311  write_corner_data(*data_it);
312  }
313  auto &elem_data_list = this->output_data_vec_[ELEM_DATA];
314  for(auto data_it = elem_data_list.begin(); data_it != elem_data_list.end(); ++data_it) {
315  write_elem_data(*data_it);
316  }
317 
318  // Flush stream to be sure everything is in the file now
319  this->_base_file.flush();
320 
321  LogOut() << "O.K.";
322 
323  return 1;
324 }
325 
326 
327 
329 {
330  return 1;
331 }
332 
333 
334 
336 {
338  for (auto type_idx : space_types) {
339  auto &dummy_data_list = dummy_data_list_[type_idx];
340  auto &data_list = this->output_data_vec_[type_idx];
341 
342  // Collect all output fields
343  if (dummy_data_list.size() == 0)
344  for(auto out_ptr : data_list)
345  dummy_data_list.push_back( std::make_shared<DummyOutputData>(out_ptr->field_input_name(), out_ptr->n_elem()));
346 
347  auto data_it = data_list.begin();
348  for(auto dummy_it = dummy_data_list.begin(); dummy_it != dummy_data_list.end(); ++dummy_it) {
349  if ( data_it == data_list.end() ) {
350  data_list.push_back( *dummy_it );
351  } else if ((*dummy_it)->field_input_name() == (*data_it)->field_input_name()) {
352  ++data_it;
353  } else {
354  data_list.push_back( *dummy_it );
355  }
356  }
357  }
358 }
359 
360 
361 
362 void OutputMSH::set_output_data_caches(std::shared_ptr<OutputMeshBase> mesh_ptr) {
364 
365  mesh_ptr->create_id_caches();
366  this->node_ids_ = mesh_ptr->node_ids_;
367  this->elem_ids_ = mesh_ptr->elem_ids_;
368  this->region_ids_ = mesh_ptr->region_ids_;
369  this->partitions_ = mesh_ptr->partitions_;
370 }
371 
Classes for auxiliary output mesh.
void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row) override
Definition: output_msh.cc:72
std::vector< std::vector< OutputDataPtr > > dummy_data_list_
Definition: output_msh.hh:93
virtual void set_output_data_caches(std::shared_ptr< OutputMeshBase > mesh_ptr)
Definition: output_time.cc:131
void print_ascii_all(ostream &out_stream) override
Definition: output_msh.cc:56
double time
Definition: output_time.hh:260
void print_ascii(ostream &out_stream, unsigned int idx) override
Definition: output_msh.cc:51
void write_msh_header(void)
This function write header of GMSH (.msh) file format.
Definition: output_msh.cc:114
static const Input::Type::Record & get_input_type()
The definition of input record for gmsh file format.
Definition: output_msh.cc:85
Input::Record input_record_
Definition: output_time.hh:270
void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row) override
Definition: output_msh.cc:75
OutputMSH()
The constructor of this class. We open the output file in first call of write_data.
Definition: output_msh.cc:96
void fix_main_file_extension(std::string extension)
Definition: output_time.cc:151
unsigned int size() const
Returns number of keys in the Record.
Definition: type_record.hh:598
std::shared_ptr< ElementDataCache< unsigned int > > connectivity_
Vector maps the nodes to their coordinates in vector nodes_.
Definition: output_time.hh:310
void write_msh_ascii_data(std::shared_ptr< ElementDataCache< unsigned int >> id_cache, OutputDataPtr output_data, bool discont=false)
This function writes ascii data to GMSH (.msh) output file.
Definition: output_msh.cc:174
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
static const int registrar
Registrar of class to factory.
Definition: output_msh.hh:85
void write_msh_geometry(void)
This function writes geometry (position of nodes) to GMSH (.msh) file format.
Definition: output_msh.cc:124
FilePath _base_filename
Definition: output_time.hh:280
double convert_unit_from(std::string actual_unit) const
Convert and check user-defined unit.
Definition: unit_si.cc:217
Abstract linear system class.
Definition: balance.hh:35
void write_corner_data(OutputDataPtr output_data)
writes ElementNode data ascii GMSH (.msh) output file.
Definition: output_msh.cc:225
#define INPUT_CATCH(ExceptionType, AddressEITag, input_accessor)
Definition: accessors.hh:64
int write_tail(void)
This method should write tail of GMSH (.msh) file format.
Definition: output_msh.cc:328
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:122
std::shared_ptr< ElementDataCache< double > > nodes_
Vector of node coordinates. [spacedim x n_nodes].
Definition: output_time.hh:308
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
std::shared_ptr< ElementDataCache< unsigned int > > node_ids_
Vector gets ids of nodes.
Definition: output_msh.hh:152
#define LogOut()
Macro defining &#39;log&#39; record of log.
Definition: logger.hh:249
std::shared_ptr< ElementDataCache< int > > partitions_
Vector gets partitions of elements.
Definition: output_msh.hh:158
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:303
void write_node_data(OutputDataPtr output_data)
This function write all data on nodes to output file. This function is used for static and dynamic da...
Definition: output_msh.cc:200
DummyOutputData(std::string field_name_in, ElementDataCacheBase::NumCompValueType n_elem_in)
Definition: output_msh.cc:41
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:195
void open_stream(Stream &stream) const
Definition: file_path.cc:211
void add_dummy_fields() override
Complete information about dummy fields that are not in output_data_list_.
Definition: output_msh.cc:335
int current_step
Definition: output_time.hh:255
void write_msh_topology(void)
This function writes topology (connection of nodes) to the GMSH (.msh) file format.
Definition: output_msh.cc:141
UnitSI & s(int exp=1)
Definition: unit_si.cc:76
static const unsigned int N_DISCRETE_SPACES
Definition: output_time.hh:103
bool header_written
Definition: output_msh.hh:87
void set_stream_precision(std::ofstream &stream)
Definition: output_time.cc:100
string unit_string_
String representation of time unit.
Definition: output_time.hh:305
~OutputMSH()
The destructor of this class.
Definition: output_msh.cc:106
ofstream _base_file
Definition: output_time.hh:275
void write_elem_data(OutputDataPtr output_data)
This function write all data on elements to output file. This function is used for static and dynamic...
Definition: output_msh.cc:248
void set_output_data_caches(std::shared_ptr< OutputMeshBase > mesh_ptr) override
Definition: output_msh.cc:362
virtual ~DummyOutputData() override
Definition: output_msh.cc:48
int write_data(void)
This method writes data to GMSH (.msh) file format for current time.
Definition: output_msh.cc:286
Class OutputElement and its iterator OutputElementIterator on the output mesh.
OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES]
Definition: output_time.hh:250
bool enable_refinement_
Auxiliary flag for refinement enabling, due to gmsh format.
Definition: output_time.hh:299
std::shared_ptr< ElementDataCache< unsigned int > > elem_ids_
Vector gets ids of elements.
Definition: output_msh.hh:154
void print_all_yaml(ostream &out_stream, unsigned int precision) override
Definition: output_msh.cc:66
Class for representation SI units of Fields.
Definition: unit_si.hh:40
void print_binary_all(ostream &out_stream, bool print_data_size=true) override
Definition: output_msh.cc:61
void get_min_max_range(double &min, double &max) override
Definition: output_msh.cc:69
int write_head(void)
This method writes head of GMSH (.msh) file format.
Definition: output_msh.cc:271
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:180
std::shared_ptr< ElementDataCache< unsigned int > > region_ids_
Vector gets ids of regions.
Definition: output_msh.hh:156