Flow123d  release_1.8.2-1603-g0109a2b
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 const Record & OutputMSH::get_input_type() {
30  return Record("gmsh", "Parameters of gmsh output format.")
31  // It is derived from abstract class
33  .close();
34 }
35 
36 const int OutputMSH::registrar = Input::register_class< OutputMSH, const Input::Record & >("gmsh") +
38 
39 
41 {
42  ofstream &file = this->_base_file;
43 
44  // Write simple header
45  file << "$MeshFormat" << endl;
46  file << "2" << " 0 " << sizeof(double) << endl;
47  file << "$EndMeshFormat" << endl;
48 }
49 
51 {
52  ofstream &file = this->_base_file;
53  Mesh* mesh = this->_mesh;
54 
55  // Write information about nodes
56  file << "$Nodes" << endl;
57  file << mesh->node_vector.size() << endl;
58  FOR_NODES(mesh, nod) {
59  file << NODE_FULL_ITER(mesh, nod).id() << " " << nod->getX() << " " << nod->getY() << " " << nod->getZ() << endl;
60  }
61  file << "$EndNodes" << endl;
62 }
63 
65 {
66  ofstream &file = this->_base_file;
67  Mesh* mesh = this->_mesh;
68  unsigned int i;
69  const static unsigned int gmsh_simplex_types_[4] = {0, 1, 2, 4};
70 
71  // Write information about elements
72  file << "$Elements" << endl;
73  file << mesh->n_elements() << endl;
74  FOR_ELEMENTS(mesh, elm) {
75  // element_id element_type 3_other_tags material region partition
76  file << ELEM_FULL_ITER(mesh, elm).id()
77  << " " << gmsh_simplex_types_[ elm->dim() ]
78  << " 3 " << elm->region().id() << " " << elm->region().id() << " " << elm->pid;
79 
80  FOR_ELEMENT_NODES(elm, i)
81  file << " " << NODE_FULL_ITER(mesh, elm->node[i]).id();
82  file << endl;
83  }
84  file << "$EndElements" << endl;
85 }
86 
87 
88 template<class element>
90 {
91  ofstream &file = this->_base_file;
92 
93  /* Set precision to max */
94  file.precision(std::numeric_limits<double>::digits10);
95 
96  for(unsigned int i=0; i < output_data->n_values; i ++) {
97  file << vec(i).id() << " ";
98  output_data->print(file, i);
99  file << std::endl;
100  }
101 
102 }
103 
104 
106 {
107  Mesh *mesh = this->_mesh;
108  ofstream &file = this->_base_file;
109 
110  /* Set precision to max */
111  file.precision(std::numeric_limits<double>::digits10);
112 
113  /* Write ascii data */
114  unsigned int i_node;
115  unsigned int i_corner = 0;
116  FOR_ELEMENTS(mesh, ele) {
117  file << ele.id() << " " << ele->n_nodes() << " ";
118 
119  FOR_ELEMENT_NODES(ele, i_node) {
120  output_data->print(file, i_corner++);
121  }
122 
123  file << std::endl;
124  }
125 }
126 
127 
128 void OutputMSH::write_msh_node_data(double time, int step)
129 {
130  ofstream &file = this->_base_file;
131  Mesh *mesh = this->_mesh;
132 
133  double time_fixed = isfinite(time)?time:0;
134 
135  for(OutputDataPtr output_data : this->output_data_vec_[NODE_DATA])
136  {
137  file << "$NodeData" << endl;
138 
139  file << "1" << endl; // one string tag
140  file << "\"" << output_data->output_field_name <<"\"" << endl;
141 
142  file << "1" << endl; // one real tag
143  file << time_fixed << endl; // first real tag = time
144 
145  file << "3" << endl; // 3 integer tags
146  file << step << endl; // step number (start = 0)
147  file << output_data->n_elem_ << endl; // number of components
148  file << output_data->n_values << endl; // number of values
149 
150  this->write_msh_ascii_cont_data(mesh->node_vector, output_data);
151 
152  file << "$EndNodeData" << endl;
153  }
154  for(OutputDataPtr output_data : this->output_data_vec_[CORNER_DATA] )
155  {
156  file << "$ElementNodeData" << endl;
157 
158  file << "1" << endl; // one string tag
159  file << "\"" << output_data->output_field_name <<"\"" << endl;
160 
161  file << "1" << endl; // one real tag
162  file << time_fixed << endl; // first real tag = time
163 
164  file << "3" << endl; // 3 integer tags
165  file << step << endl; // step number (start = 0)
166  file << output_data->n_elem_ << endl; // number of components
167  file << mesh->n_elements() << endl; // number of values
168 
169  this->write_msh_ascii_discont_data(output_data);
170 
171  file << "$EndElementNodeData" << endl;
172  }
173 }
174 
175 void OutputMSH::write_msh_elem_data(double time, int step)
176 {
177  ofstream &file = this->_base_file;
178 
179  double time_fixed = isfinite(time) ? time : 0;
180 
181  for(OutputDataPtr output_data : this->output_data_vec_[ELEM_DATA] )
182  {
183  file << "$ElementData" << endl;
184 
185  file << "1" << endl; // one string tag
186  file << "\"" << output_data->output_field_name <<"\"" << endl;
187 
188  file << "1" << endl; // one real tag
189  file << time_fixed << endl; // first real tag = time
190 
191  file << "3" << endl; // 3 integer tags
192  file << step << endl; // step number (start = 0)
193  file << output_data->n_elem_ << endl; // number of components
194  file << output_data->n_values << endl; // number of values
195 
196  this->write_msh_ascii_cont_data(this->_mesh->element, output_data);
197 
198  file << "$EndElementData" << endl;
199  }
200 }
201 
203 {
204  xprintf(MsgLog, "%s: Writing output file %s ... ", __func__,
205  this->_base_filename.c_str());
206 
207  this->write_msh_header();
208 
209  this->write_msh_geometry();
210 
211  this->write_msh_topology();
212 
213  xprintf(MsgLog, "O.K.\n");
214 
215  return 1;
216 }
217 
219 {
220  xprintf(MsgLog, "%s: Writing output file %s ... ", __func__,
221  this->_base_filename.c_str());
222 
223  // Write header with mesh, when it hasn't been written to output file yet
224  if(this->header_written == false) {
225  this->write_head();
226  this->header_written = true;
227  }
228 
229  this->write_msh_node_data(this->time, this->current_step);
230  this->write_msh_elem_data(this->time, this->current_step);
231 
232  // Flush stream to be sure everything is in the file now
233  this->_base_file.flush();
234 
235  xprintf(MsgLog, "O.K.\n");
236 
237  return 1;
238 }
239 
240 
241 
243 {
244  return 1;
245 }
246 
247 
248 
250 {
251  this->fix_main_file_extension(".msh");
252  this->header_written = false;
253 
254  if(this->rank == 0) {
255  this->_base_file.open(this->_base_filename.c_str());
256  INPUT_CHECK( this->_base_file.is_open() , "Can not open output file: %s\n", this->_base_filename.c_str() );
257  xprintf(MsgLog, "Writing flow output file: %s ... \n", this->_base_filename.c_str());
258  }
259 }
260 
262 {
263  this->write_tail();
264 }
265 
double time
Definition: output_time.hh:200
Mesh * _mesh
Definition: output_time.hh:232
void write_msh_header(void)
This function write header of GMSH (.msh) file format.
Definition: output_msh.cc:40
void write_msh_elem_data(double time, int step)
This function write all data on elements to output file. This function is used for static and dynamic...
Definition: output_msh.cc:175
#define FOR_ELEMENT_NODES(i, j)
Definition: elements.h:139
static const Input::Type::Record & get_input_type()
The definition of input record for gmsh file format.
Definition: output_msh.cc:29
#define NODE_FULL_ITER(_mesh_, i)
Definition: mesh.h:70
static Input::Type::Abstract & get_input_format_type()
The specification of output file format.
Definition: output_time.cc:55
OutputMSH()
The constructor of this class.
void fix_main_file_extension(std::string extension)
Definition: output_time.cc:103
unsigned int size() const
Returns number of keys in the Record.
Definition: type_record.hh:578
#define FOR_ELEMENTS(_mesh_, __i)
Definition: mesh.h:408
static const int registrar
Registrar of class to factory.
Definition: output_msh.hh:79
void write_msh_geometry(void)
This function writes geometry (position of nodes) to GMSH (.msh) file format.
Definition: output_msh.cc:50
#define ELEM_FULL_ITER(_mesh_, i)
Definition: mesh.h:82
int write_tail(void)
This method should write tail of GMSH (.msh) file format.
Definition: output_msh.cc:242
Small extension of Vector<T> container with support to Id numbers.
Definition: sys_vector.hh:154
Definition: mesh.h:99
#define INPUT_CHECK(i,...)
Debugging macros.
Definition: global_defs.h:51
Definition: system.hh:59
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:193
unsigned int size() const
Returns size of the container. This is independent of the allocated space.
Definition: sys_vector.hh:391
unsigned int n_elements() const
Definition: mesh.h:142
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:89
int current_step
Definition: output_time.hh:195
void write_msh_topology(void)
This function writes topology (connection of nodes) to the GMSH (.msh) file format.
Definition: output_msh.cc:64
Accessor to the data with type Type::Record.
Definition: accessors.hh:277
#define xprintf(...)
Definition: system.hh:87
void write_msh_ascii_discont_data(OutputDataPtr output_data)
This function writes discontinuous ascii data to GMSH (.msh) output file.
Definition: output_msh.cc:105
bool header_written
Definition: output_msh.hh:81
void write_msh_node_data(double time, int step)
This function write all data on nodes to output file. This function is used for static and dynamic da...
Definition: output_msh.cc:128
const Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:271
~OutputMSH()
The destructor of this class.
Definition: output_msh.cc:261
ofstream _base_file
Definition: output_time.hh:222
The class for outputting data during time.
Definition: output_time.hh:42
#define FOR_NODES(_mesh_, i)
Definition: mesh.h:62
string _base_filename
Definition: output_time.hh:227
int write_data(void)
This method writes data to GMSH (.msh) file format for current time.
Definition: output_msh.cc:218
OutputDataFieldVec output_data_vec_[N_DISCRETE_SPACES]
Definition: output_time.hh:190
std::shared_ptr< OutputDataBase > OutputDataPtr
Definition: output_time.hh:183
Record type proxy class.
Definition: type_record.hh:171
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:202
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:246
NodeVector node_vector
Vector of nodes of the mesh.
Definition: mesh.h:223
ElementVector element
Vector of elements of the mesh.
Definition: mesh.h:225