Flow123d  release_3.0.0-1193-g9220a69
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, unsigned int n_comp_in)
42  {
43  this->field_input_name_ = field_name_in;
44  this->n_comp_ = n_comp_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_comp_;i++) out_stream << 0 << " ";
54  }
55 
56  void print_ascii_all(ostream &out_stream) override
57  {
58  for(unsigned int i=0; i< n_comp_;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_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end) 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  std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global) override
79  {
80  return std::make_shared<DummyOutputData>(this->field_input_name_, this->n_comp_);
81  }
82 
83  std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector<unsigned int> &offset_vec) override
84  {
85  return std::make_shared<DummyOutputData>(this->field_input_name_, this->n_comp_);
86  }
87 
88  std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector<unsigned int> &offset_vec) override
89  {
90  return std::make_shared<DummyOutputData>(this->field_input_name_, this->n_comp_);
91  }
92 
93  std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector<unsigned int> &conn_vec, unsigned int data_size) override
94  {
95  return std::make_shared<DummyOutputData>(this->field_input_name_, this->n_comp_);
96  }
97 
98 };
99 
100 
101 
102 
103 
104 
105 
106 const Record & OutputMSH::get_input_type() {
107  return Record("gmsh", "Parameters of gmsh output format.")
108  // It is derived from abstract class
110  .close();
111 }
112 
113 const int OutputMSH::registrar = Input::register_class< OutputMSH >("gmsh") +
115 
116 
118 {
119  this->enable_refinement_ = false;
120  this->header_written = false;
121 
123 
124 
125 }
126 
128 {
129  this->write_tail();
130 }
131 
132 
133 
134 
136 {
137  ofstream &file = this->_base_file;
138 
139  // Write simple header
140  file << "$MeshFormat" << endl;
141  file << "2" << " 0 " << sizeof(double) << endl;
142  file << "$EndMeshFormat" << endl;
143 }
144 
146 {
147  ofstream &file = this->_base_file;
148 
149  // Write information about nodes
150  file << "$Nodes" << endl;
151  file << this->nodes_->n_values() << endl;
152  auto &id_node_vec = *( this->node_ids_->get_component_data(0).get() );
153  unsigned int i_node=0;
154  for(unsigned int i_node=0; i_node < id_node_vec.size(); ++i_node) {
155  file << id_node_vec[i_node] << " ";
156  this->nodes_->print_ascii(file, i_node);
157  file << endl;
158  }
159  file << "$EndNodes" << endl;
160 }
161 
163 {
164  ofstream &file = this->_base_file;
165  const static unsigned int gmsh_simplex_types_[4] = {0, 1, 2, 4};
166  auto &id_elem_vec = *( this->elem_ids_->get_component_data(0).get() );
167  auto &id_node_vec = *( this->node_ids_->get_component_data(0).get() );
168  auto &connectivity_vec = *( this->connectivity_->get_component_data(0).get() );
169  auto &offsets_vec = *( this->offsets_->get_component_data(0).get() );
170  auto &regions_vec = *( this->region_ids_->get_component_data(0).get() );
171  auto &partition_vec = *( this->partitions_->get_component_data(0).get() );
172 
173  unsigned int n_nodes, i_node=0;
174 
175  // Write information about elements
176  file << "$Elements" << endl;
177  file << this->offsets_->n_values() << endl;
179  for(unsigned int i_elm=0; i_elm < id_elem_vec.size(); ++i_elm) {
180  n_nodes = (i_elm==0) ? (offsets_vec[0]) : (offsets_vec[i_elm]-offsets_vec[i_elm-1]);
181  // element_id element_type 3_other_tags material region partition
182  file << id_elem_vec[i_elm]
183  << " " << gmsh_simplex_types_[ n_nodes-1 ]
184  << " 3 " << regions_vec[i_elm] << " " << regions_vec[i_elm] << " " << partition_vec[i_elm];
185 
186  for(unsigned int i=0; i<n_nodes; i++, i_node++) {
187  file << " " << id_node_vec[connectivity_vec[i_node]];
188  }
189  file << endl;
190  }
191  file << "$EndElements" << endl;
192 }
193 
194 
195 void OutputMSH::write_msh_ascii_data(std::shared_ptr<ElementDataCache<unsigned int>> id_cache, OutputDataPtr output_data, bool discont)
196 {
197  ofstream &file = this->_base_file;
198  auto &id_vec = *( id_cache->get_component_data(0).get() );
199 
200  if (discont) { // corner data
201  auto &offsets_vec = *( this->offsets_->get_component_data(0).get() );
202  unsigned int n_nodes, i_corner=0;
203  for(unsigned int i=0; i < id_vec.size(); ++i) {
204  n_nodes = (i==0) ? (offsets_vec[0]) : (offsets_vec[i]-offsets_vec[i-1]);
205  file << id_vec[i] << " " << n_nodes << " ";
206  for (unsigned int j=0; j<n_nodes; j++)
207  output_data->print_ascii(file, i_corner++);
208  file << std::endl;
209  }
210  } else { // element / node data
211  for(unsigned int i=0; i < output_data->n_values(); ++i) {
212  file << id_vec[i] << " ";
213  output_data->print_ascii(file, i);
214  file << std::endl;
215  }
216 
217  }
218 }
219 
220 
222 {
223  ofstream &file = this->_base_file;
224  double time_fixed = isfinite(this->time)?this->time:0;
225  time_fixed /= UnitSI().s().convert_unit_from(this->unit_string_);
226 
227  file << "$NodeData" << endl;
228 
229  file << "1" << endl; // one string tag
230  file << "\"" << output_data->field_input_name() <<"\"" << endl;
231 
232  file << "1" << endl; // one real tag
233  file << time_fixed << endl; // first real tag = time
234 
235  file << "3" << endl; // 3 integer tags
236  file << this->current_step << endl; // step number (start = 0)
237  file << output_data->n_comp() << endl; // number of components
238  file << output_data->n_values() << endl; // number of values
239 
240  this->write_msh_ascii_data(this->node_ids_, output_data);
241 
242  file << "$EndNodeData" << endl;
243 }
244 
245 
247 {
248  ofstream &file = this->_base_file;
249  double time_fixed = isfinite(this->time)?this->time:0;
250 
251  file << "$ElementNodeData" << endl;
252 
253  file << "1" << endl; // one string tag
254  file << "\"" << output_data->field_input_name() <<"\"" << endl;
255 
256  file << "1" << endl; // one real tag
257  file << time_fixed << endl; // first real tag = time
258 
259  file << "3" << endl; // 3 integer tags
260  file << this->current_step << endl; // step number (start = 0)
261  file << output_data->n_comp() << endl; // number of components
262  file << this->offsets_->n_values() << endl; // number of values
263 
264  this->write_msh_ascii_data(this->elem_ids_, output_data, true);
265 
266  file << "$EndElementNodeData" << endl;
267 }
268 
270 {
271  ofstream &file = this->_base_file;
272  double time_fixed = isfinite(this->time)?this->time:0;
273 
274  file << "$ElementData" << endl;
275 
276  file << "1" << endl; // one string tag
277  file << "\"" << output_data->field_input_name() <<"\"" << endl;
278 
279  file << "1" << endl; // one real tag
280  file << time_fixed << endl; // first real tag = time
281 
282  file << "3" << endl; // 3 integer tags
283  file << this->current_step << endl; // step number (start = 0)
284  file << output_data->n_comp() << endl; // number of components
285  file << output_data->n_values() << endl; // number of values
286 
287  this->write_msh_ascii_data(this->elem_ids_, output_data);
288 
289  file << "$EndElementData" << endl;
290 }
291 
293 {
294  LogOut() << __func__ << ": Writing output file " << this->_base_filename << " ... ";
295 
296  this->write_msh_header();
297 
298  this->write_msh_geometry();
299 
300  this->write_msh_topology();
301 
302  LogOut() << "O.K.";
303 
304  return 1;
305 }
306 
308 {
309  /* Output of serial format is implemented only in the first process */
310  if (this->rank_ != 0) {
311  return 0;
312  }
313 
314  // Write header with mesh, when it hasn't been written to output file yet
315  if(this->header_written == false) {
316  this->fix_main_file_extension(".msh");
317  try {
318  this->_base_filename.open_stream( this->_base_file );
319  this->set_stream_precision(this->_base_file);
320  } INPUT_CATCH(FilePath::ExcFileOpen, FilePath::EI_Address_String, input_record_)
321 
322  this->write_head();
323  this->header_written = true;
324  }
325 
326  LogOut() << __func__ << ": Writing output file " << this->_base_filename << " ... ";
327 
328 
329  auto &node_data_list = this->output_data_vec_[NODE_DATA];
330  for(auto data_it = node_data_list.begin(); data_it != node_data_list.end(); ++data_it) {
331  write_node_data(*data_it);
332  }
333  auto &corner_data_list = this->output_data_vec_[CORNER_DATA];
334  for(auto data_it = corner_data_list.begin(); data_it != corner_data_list.end(); ++data_it) {
335  write_corner_data(*data_it);
336  }
337  auto &elem_data_list = this->output_data_vec_[ELEM_DATA];
338  for(auto data_it = elem_data_list.begin(); data_it != elem_data_list.end(); ++data_it) {
339  write_elem_data(*data_it);
340  }
341 
342  // Flush stream to be sure everything is in the file now
343  this->_base_file.flush();
344 
345  LogOut() << "O.K.";
346 
347  return 1;
348 }
349 
350 
351 
353 {
354  return 1;
355 }
356 
357 
358 
360 {
362  for (auto type_idx : space_types) {
363  auto &dummy_data_list = dummy_data_list_[type_idx];
364  auto &data_list = this->output_data_vec_[type_idx];
365 
366  // Collect all output fields
367  if (dummy_data_list.size() == 0)
368  for(auto out_ptr : data_list)
369  dummy_data_list.push_back( std::make_shared<DummyOutputData>(out_ptr->field_input_name(), out_ptr->n_comp()));
370 
371  auto data_it = data_list.begin();
372  for(auto dummy_it = dummy_data_list.begin(); dummy_it != dummy_data_list.end(); ++dummy_it) {
373  if ( data_it == data_list.end() ) {
374  data_list.push_back( *dummy_it );
375  } else if ((*dummy_it)->field_input_name() == (*data_it)->field_input_name()) {
376  ++data_it;
377  } else {
378  data_list.push_back( *dummy_it );
379  }
380  }
381  }
382 }
383 
384 
385 
386 void OutputMSH::set_output_data_caches(std::shared_ptr<OutputMeshBase> mesh_ptr) {
388 
389  mesh_ptr->get_master_mesh()->create_id_caches();
390  this->node_ids_ = mesh_ptr->get_master_mesh()->node_ids_;
391  this->elem_ids_ = mesh_ptr->get_master_mesh()->elem_ids_;
392  this->region_ids_ = mesh_ptr->get_master_mesh()->region_ids_;
393  this->partitions_ = mesh_ptr->get_master_mesh()->partitions_;
394 }
395 
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: long_idx.hh:22
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:278
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:135
std::shared_ptr< ElementDataCacheBase > element_node_cache_fixed_size(std::vector< unsigned int > &offset_vec) override
Definition: output_msh.cc:83
static const Input::Type::Record & get_input_type()
The definition of input record for gmsh file format.
Definition: output_msh.cc:106
Input::Record input_record_
Definition: output_time.hh:288
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:117
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
DummyOutputData(std::string field_name_in, unsigned int n_comp_in)
Definition: output_msh.cc:41
std::shared_ptr< ElementDataCacheBase > element_node_cache_optimize_size(std::vector< unsigned int > &offset_vec) override
Definition: output_msh.cc:88
std::shared_ptr< ElementDataCache< unsigned int > > connectivity_
Vector maps the nodes to their coordinates in vector nodes_.
Definition: output_time.hh:328
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:195
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:330
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:145
FilePath _base_filename
Definition: output_time.hh:298
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:37
void write_corner_data(OutputDataPtr output_data)
writes ElementNode data ascii GMSH (.msh) output file.
Definition: output_msh.cc:246
#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:352
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:326
#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:304
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:221
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:196
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:359
int current_step
Definition: output_time.hh:273
void write_msh_topology(void)
This function writes topology (connection of nodes) to the GMSH (.msh) file format.
Definition: output_msh.cc:162
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:323
~OutputMSH()
The destructor of this class.
Definition: output_msh.cc:127
ofstream _base_file
Definition: output_time.hh:293
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:269
std::shared_ptr< ElementDataCacheBase > compute_node_data(std::vector< unsigned int > &conn_vec, unsigned int data_size) override
Definition: output_msh.cc:93
void set_output_data_caches(std::shared_ptr< OutputMeshBase > mesh_ptr) override
Definition: output_msh.cc:386
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:307
Class OutputElement and its iterator OutputElementIterator on the output mesh.
OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES]
Definition: output_time.hh:268
bool enable_refinement_
Auxiliary flag for refinement enabling, due to gmsh format.
Definition: output_time.hh:317
std::shared_ptr< ElementDataCache< unsigned int > > elem_ids_
Vector gets ids of elements.
Definition: output_msh.hh:154
Class for representation SI units of Fields.
Definition: unit_si.hh:40
void print_yaml_subarray(ostream &out_stream, unsigned int precision, unsigned int begin, unsigned int end) override
Definition: output_msh.cc:66
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:292
#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
std::shared_ptr< ElementDataCacheBase > gather(Distribution *distr, LongIdx *local_to_global) override
Definition: output_msh.cc:78