Flow123d  last_with_con_2.0.0-4-g42e6930
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 "output_msh.hh"
19 #include "mesh/mesh.h"
20 #include "output_data_base.hh"
21 #include "input/factory.hh"
22 
23 
25 
26 
27 using namespace Input::Type;
28 
29 
30 /**
31  * Auxiliary implementation of OutputDataBase that performs output of single zero data for the fields that are
32  * off for current time frame.
33  */
35 public:
36 
37  DummyOutputData(std::string field_name_in, OutputDataBase::NumCompValueType n_elem_in)
38  {
39  this->output_field_name = field_name_in;
40  this->n_elem_ = n_elem_in;
41  this->n_values = 1;
42  }
43 
44  virtual ~DummyOutputData() override
45  {}
46 
47  void print(ostream &out_stream, unsigned int idx) override
48  {
49  for(unsigned int i=0; i< n_elem_;i++) out_stream << 0 << " ";
50  }
51 
52  void print_all(ostream &out_stream) override
53  {
54  for(unsigned int i=0; i< n_elem_;i++) out_stream << 0 << " ";
55  }
56 
57  void print_all_yaml(ostream &out_stream, unsigned int precision) override
58  {}
59 };
60 
61 
62 
63 
64 
65 
66 
68  return Record("gmsh", "Parameters of gmsh output format.")
69  // It is derived from abstract class
71  .close();
72 }
73 
74 const int OutputMSH::registrar = Input::register_class< OutputMSH >("gmsh") +
76 
77 
79 {
80  this->enable_refinement_ = false;
81  this->header_written = false;
82 
84 
85 
86 }
87 
89 {
90  this->write_tail();
91 }
92 
93 
94 
95 
97 {
98  ofstream &file = this->_base_file;
99 
100  // Write simple header
101  file << "$MeshFormat" << endl;
102  file << "2" << " 0 " << sizeof(double) << endl;
103  file << "$EndMeshFormat" << endl;
104 }
105 
107 {
108  ofstream &file = this->_base_file;
109  Mesh* mesh = this->_mesh;
110 
111  // Write information about nodes
112  file << "$Nodes" << endl;
113  file << mesh->node_vector.size() << endl;
114  FOR_NODES(mesh, nod) {
115  file << NODE_FULL_ITER(mesh, nod).id() << " " << nod->getX() << " " << nod->getY() << " " << nod->getZ() << endl;
116  }
117  file << "$EndNodes" << endl;
118 }
119 
121 {
122  ofstream &file = this->_base_file;
123  Mesh* mesh = this->_mesh;
124  unsigned int i;
125  const static unsigned int gmsh_simplex_types_[4] = {0, 1, 2, 4};
126 
127  // Write information about elements
128  file << "$Elements" << endl;
129  file << mesh->n_elements() << endl;
130  FOR_ELEMENTS(mesh, elm) {
131  // element_id element_type 3_other_tags material region partition
132  file << ELEM_FULL_ITER(mesh, elm).id()
133  << " " << gmsh_simplex_types_[ elm->dim() ]
134  << " 3 " << elm->region().id() << " " << elm->region().id() << " " << elm->pid;
135 
136  FOR_ELEMENT_NODES(elm, i)
137  file << " " << NODE_FULL_ITER(mesh, elm->node[i]).id();
138  file << endl;
139  }
140  file << "$EndElements" << endl;
141 }
142 
143 
144 template<class element>
146 {
147  ofstream &file = this->_base_file;
148 
149  /* Set precision to max */
150  file.precision(std::numeric_limits<double>::digits10);
151 
152  for(unsigned int i=0; i < output_data->n_values; i ++) {
153  file << vec(i).id() << " ";
154  output_data->print(file, i);
155  file << std::endl;
156  }
157 
158 }
159 
160 
162 {
163  Mesh *mesh = this->_mesh;
164  ofstream &file = this->_base_file;
165 
166  /* Set precision to max */
167  file.precision(std::numeric_limits<double>::digits10);
168 
169  /* Write ascii data */
170  unsigned int i_node;
171  unsigned int i_corner = 0;
172  FOR_ELEMENTS(mesh, ele) {
173  file << ele.id() << " " << ele->n_nodes() << " ";
174 
175  FOR_ELEMENT_NODES(ele, i_node) {
176  output_data->print(file, i_corner++);
177  }
178 
179  file << std::endl;
180  }
181 }
182 
183 
184 
186 {
187  ofstream &file = this->_base_file;
188  double time_fixed = isfinite(this->time)?this->time:0;
189 
190 
191  file << "$NodeData" << endl;
192 
193  file << "1" << endl; // one string tag
194  file << "\"" << output_data->output_field_name <<"\"" << endl;
195 
196  file << "1" << endl; // one real tag
197  file << time_fixed << endl; // first real tag = time
198 
199  file << "3" << endl; // 3 integer tags
200  file << this->current_step << endl; // step number (start = 0)
201  file << output_data->n_elem_ << endl; // number of components
202  file << output_data->n_values << endl; // number of values
203 
204  this->write_msh_ascii_cont_data(this->_mesh->node_vector, output_data);
205 
206  file << "$EndNodeData" << endl;
207 }
208 
209 
211 {
212  ofstream &file = this->_base_file;
213  double time_fixed = isfinite(this->time)?this->time:0;
214 
215  file << "$ElementNodeData" << endl;
216 
217  file << "1" << endl; // one string tag
218  file << "\"" << output_data->output_field_name <<"\"" << endl;
219 
220  file << "1" << endl; // one real tag
221  file << time_fixed << endl; // first real tag = time
222 
223  file << "3" << endl; // 3 integer tags
224  file << this->current_step << endl; // step number (start = 0)
225  file << output_data->n_elem_ << endl; // number of components
226  file << this->_mesh->n_elements() << endl; // number of values
227 
228  this->write_msh_ascii_discont_data(output_data);
229 
230  file << "$EndElementNodeData" << endl;
231 }
232 
234 {
235  ofstream &file = this->_base_file;
236  double time_fixed = isfinite(this->time)?this->time:0;
237 
238  file << "$ElementData" << endl;
239 
240  file << "1" << endl; // one string tag
241  file << "\"" << output_data->output_field_name <<"\"" << endl;
242 
243  file << "1" << endl; // one real tag
244  file << time_fixed << endl; // first real tag = time
245 
246  file << "3" << endl; // 3 integer tags
247  file << this->current_step << endl; // step number (start = 0)
248  file << output_data->n_elem_ << endl; // number of components
249  file << output_data->n_values << endl; // number of values
250 
251  this->write_msh_ascii_cont_data(this->_mesh->element, output_data);
252 
253  file << "$EndElementData" << endl;
254 }
255 
257 {
258  auto &dummy_data_list = dummy_data_list_[type_idx];
259  auto &data_list = this->output_data_vec_[type_idx];
260 
261  if (dummy_data_list.size() == 0) {
262  // Collect all output fields
263  // If more EquationOutput object with different initial times output into same
264  // output stream, we may need to possibly update this list on every output frame.
265  for(auto out_ptr : data_list)
266  dummy_data_list.push_back( std::make_shared<DummyOutputData>(out_ptr->output_field_name, out_ptr->n_elem_));
267  }
268 
269 
270  auto data_it = data_list.begin();
271  for(auto dummy_it = dummy_data_list.begin(); dummy_it != dummy_data_list.end(); ++dummy_it) {
272  DebugOut().fmt("dummy field: {} data field: {}\n", (*dummy_it)->output_field_name, (*data_it)->output_field_name);
273  if ((*dummy_it)->output_field_name == (*data_it)->output_field_name) {
274  (this->*format_fce)(*data_it); ++data_it;
275  } else {
276  (this->*format_fce)(*dummy_it);
277  }
278  }
279  ASSERT( data_it == data_list.end() )(data_it - data_list.begin())(data_list.size());
280 }
281 
283 {
284  LogOut() << __func__ << ": Writing output file " << this->_base_filename << " ... ";
285 
286  this->write_msh_header();
287 
288  this->write_msh_geometry();
289 
290  this->write_msh_topology();
291 
292  LogOut() << "O.K.";
293 
294  return 1;
295 }
296 
298 {
299  // Write header with mesh, when it hasn't been written to output file yet
300  if(this->header_written == false) {
301  if(this->rank == 0) {
302  this->fix_main_file_extension(".msh");
303  this->_base_filename.open_stream( this->_base_file );
304  }
305 
306  this->write_head();
307  this->header_written = true;
308  }
309 
310  LogOut() << __func__ << ": Writing output file " << this->_base_filename << " ... ";
311 
312 
316 
317  // Flush stream to be sure everything is in the file now
318  this->_base_file.flush();
319 
320  LogOut() << "O.K.";
321 
322  return 1;
323 }
324 
325 
326 
328 {
329  return 1;
330 }
331 
332 
333 
std::vector< std::vector< OutputDataPtr > > dummy_data_list_
Definition: output_msh.hh:83
double time
Definition: output_time.hh:208
Mesh * _mesh
Definition: output_time.hh:245
void write_msh_header(void)
This function write header of GMSH (.msh) file format.
Definition: output_msh.cc:96
Common parent class for templated OutputData.
#define FOR_ELEMENT_NODES(i, j)
Definition: elements.h:188
static const Input::Type::Record & get_input_type()
The definition of input record for gmsh file format.
Definition: output_msh.cc:67
#define NODE_FULL_ITER(_mesh_, i)
Definition: mesh.h:66
OutputMSH()
The constructor of this class. We open the output file in first call of write_data.
Definition: output_msh.cc:78
void fix_main_file_extension(std::string extension)
Definition: output_time.cc:157
unsigned int size() const
Returns number of keys in the Record.
Definition: type_record.hh:582
#define FOR_ELEMENTS(_mesh_, __i)
Definition: mesh.h:410
static const int registrar
Registrar of class to factory.
Definition: output_msh.hh:75
void write_msh_geometry(void)
This function writes geometry (position of nodes) to GMSH (.msh) file format.
Definition: output_msh.cc:106
FilePath _base_filename
Definition: output_time.hh:234
void write_corner_data(OutputDataPtr output_data)
writes ElementNode data ascii GMSH (.msh) output file.
Definition: output_msh.cc:210
#define ELEM_FULL_ITER(_mesh_, i)
Definition: mesh.h:78
int write_tail(void)
This method should write tail of GMSH (.msh) file format.
Definition: output_msh.cc:327
static Input::Type::Abstract & get_input_format_type()
The specification of output file format.
Definition: output_time.cc:58
Small extension of Vector<T> container with support to Id numbers.
Definition: sys_vector.hh:154
Definition: mesh.h:95
void print(ostream &out_stream, unsigned int idx) override
Definition: output_msh.cc:47
DummyOutputData(std::string field_name_in, OutputDataBase::NumCompValueType n_elem_in)
Definition: output_msh.cc:37
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
#define LogOut()
Macro defining &#39;log&#39; record of log.
Definition: logger.hh:237
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:286
void write_field_data(OutputTime::DiscreteSpace type_idx, void(OutputMSH::*format_fce)(OutputDataPtr))
Definition: output_msh.cc:256
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:185
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:195
unsigned int size() const
Returns size of the container. This is independent of the allocated space.
Definition: sys_vector.hh:391
void open_stream(Stream &stream) const
Definition: file_path.cc:211
unsigned int n_elements() const
Definition: mesh.h:138
void write_msh_ascii_cont_data(flow::VectorId< element > &vec, OutputDataPtr output_data)
This function writes continuous ascii data to GMSH (.msh) output file.
Definition: output_msh.cc:145
int current_step
Definition: output_time.hh:203
void write_msh_topology(void)
This function writes topology (connection of nodes) to the GMSH (.msh) file format.
Definition: output_msh.cc:120
static const unsigned int N_DISCRETE_SPACES
Definition: output_time.hh:89
void write_msh_ascii_discont_data(OutputDataPtr output_data)
This function writes discontinuous ascii data to GMSH (.msh) output file.
Definition: output_msh.cc:161
bool header_written
Definition: output_msh.hh:77
~OutputMSH()
The destructor of this class.
Definition: output_msh.cc:88
ofstream _base_file
Definition: output_time.hh:229
#define FOR_NODES(_mesh_, i)
Definition: mesh.h:58
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:233
virtual ~DummyOutputData() override
Definition: output_msh.cc:44
int write_data(void)
This method writes data to GMSH (.msh) file format for current time.
Definition: output_msh.cc:297
OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES]
Definition: output_time.hh:198
std::shared_ptr< OutputDataBase > OutputDataPtr
Definition: output_time.hh:191
Record type proxy class.
Definition: type_record.hh:171
bool enable_refinement_
Auxliary flag for refinement enabling, due to gmsh format.
Definition: output_time.hh:255
void print_all_yaml(ostream &out_stream, unsigned int precision) override
Definition: output_msh.cc:57
#define DebugOut()
Macro defining &#39;debug&#39; record of log.
Definition: logger.hh:240
void print_all(ostream &out_stream) override
Definition: output_msh.cc:52
This class is used for output data to VTK file format.
Definition: output_msh.hh:27
int write_head(void)
This method writes head of GMSH (.msh) file format.
Definition: output_msh.cc:282
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:180
NodeVector node_vector
Vector of nodes of the mesh.
Definition: mesh.h:219
ElementVector element
Vector of elements of the mesh.
Definition: mesh.h:221