Flow123d  jenkins-Flow123d-linux-release-multijob-282
output_msh.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id: output_msh.cc 2505 2013-09-13 14:52:27Z jiri.hnidek $
21  * $Revision$
22  * $LastChangedBy$
23  * $LastChangedDate$
24  *
25  * @file output_msh.cc
26  * @brief The functions for outputs to GMSH files.
27  *
28  */
29 
30 #include "output_msh.hh"
31 #include "mesh/mesh.h"
32 #include "output_data_base.hh"
33 
34 
35 using namespace Input::Type;
36 
38  = Record("gmsh", "Parameters of gmsh output format.")
39  // It is derived from abstract class
41 
42 
44 {
45  ofstream &file = this->_base_file;
46 
47  // Write simple header
48  file << "$MeshFormat" << endl;
49  file << "2" << " 0 " << sizeof(double) << endl;
50  file << "$EndMeshFormat" << endl;
51 }
52 
54 {
55  ofstream &file = this->_base_file;
56  Mesh* mesh = this->_mesh;
57 
58  // Write information about nodes
59  file << "$Nodes" << endl;
60  file << mesh->node_vector.size() << endl;
61  FOR_NODES(mesh, nod) {
62  file << NODE_FULL_ITER(mesh, nod).id() << " " << nod->getX() << " " << nod->getY() << " " << nod->getZ() << endl;
63  }
64  file << "$EndNodes" << endl;
65 }
66 
68 {
69  ofstream &file = this->_base_file;
70  Mesh* mesh = this->_mesh;
71  unsigned int i;
72  const static unsigned int gmsh_simplex_types_[4] = {0, 1, 2, 4};
73 
74  // Write information about elements
75  file << "$Elements" << endl;
76  file << mesh->n_elements() << endl;
77  FOR_ELEMENTS(mesh, elm) {
78  // element_id element_type 3_other_tags material region partition
79  file << ELEM_FULL_ITER(mesh, elm).id()
80  << " " << gmsh_simplex_types_[ elm->dim() ]
81  << " 3 " << elm->region().id() << " " << elm->region().id() << " " << elm->pid;
82 
83  FOR_ELEMENT_NODES(elm, i)
84  file << " " << NODE_FULL_ITER(mesh, elm->node[i]).id();
85  file << endl;
86  }
87  file << "$EndElements" << endl;
88 }
89 
90 
91 template<class element>
93 {
94  ofstream &file = this->_base_file;
95 
96  /* Set precision to max */
97  file.precision(std::numeric_limits<double>::digits10);
98 
99  for(unsigned int i=0; i < output_data->n_values; i ++) {
100  file << vec(i).id() << " ";
101  output_data->print(file, i);
102  file << std::endl;
103  }
104 
105 }
106 
107 
109 {
110  Mesh *mesh = this->_mesh;
111  ofstream &file = this->_base_file;
112 
113  /* Set precision to max */
114  file.precision(std::numeric_limits<double>::digits10);
115 
116  /* Write ascii data */
117  unsigned int i_node;
118  unsigned int i_corner = 0;
119  FOR_ELEMENTS(mesh, ele) {
120  file << ele.id() << " " << ele->n_nodes() << " ";
121 
122  FOR_ELEMENT_NODES(ele, i_node) {
123  output_data->print(file, i_corner++);
124  }
125 
126  file << std::endl;
127  }
128 }
129 
130 
131 void OutputMSH::write_msh_node_data(double time, int step)
132 {
133  ofstream &file = this->_base_file;
134  Mesh *mesh = this->_mesh;
135 
136  double time_fixed = isfinite(time)?time:0;
137 
138  for(OutputDataPtr output_data : this->output_data_vec_[NODE_DATA])
139  {
140  file << "$NodeData" << endl;
141 
142  file << "1" << endl; // one string tag
143  file << "\"" << output_data->output_field_name <<"\"" << endl;
144 
145  file << "1" << endl; // one real tag
146  file << time_fixed << endl; // first real tag = time
147 
148  file << "3" << endl; // 3 integer tags
149  file << step << endl; // step number (start = 0)
150  file << output_data->n_elem_ << endl; // number of components
151  file << output_data->n_values << endl; // number of values
152 
153  this->write_msh_ascii_cont_data(mesh->node_vector, output_data);
154 
155  file << "$EndNodeData" << endl;
156  }
157  for(OutputDataPtr output_data : this->output_data_vec_[CORNER_DATA] )
158  {
159  file << "$ElementNodeData" << endl;
160 
161  file << "1" << endl; // one string tag
162  file << "\"" << output_data->output_field_name <<"\"" << endl;
163 
164  file << "1" << endl; // one real tag
165  file << time_fixed << endl; // first real tag = time
166 
167  file << "3" << endl; // 3 integer tags
168  file << step << endl; // step number (start = 0)
169  file << output_data->n_elem_ << endl; // number of components
170  file << mesh->n_elements() << endl; // number of values
171 
172  this->write_msh_ascii_discont_data(output_data);
173 
174  file << "$EndElementNodeData" << endl;
175  }
176 }
177 
178 void OutputMSH::write_msh_elem_data(double time, int step)
179 {
180  ofstream &file = this->_base_file;
181 
182  double time_fixed = isfinite(time) ? time : 0;
183 
184  for(OutputDataPtr output_data : this->output_data_vec_[ELEM_DATA] )
185  {
186  file << "$ElementData" << endl;
187 
188  file << "1" << endl; // one string tag
189  file << "\"" << output_data->output_field_name <<"\"" << endl;
190 
191  file << "1" << endl; // one real tag
192  file << time_fixed << endl; // first real tag = time
193 
194  file << "3" << endl; // 3 integer tags
195  file << step << endl; // step number (start = 0)
196  file << output_data->n_elem_ << endl; // number of components
197  file << output_data->n_values << endl; // number of values
198 
199  this->write_msh_ascii_cont_data(this->_mesh->element, output_data);
200 
201  file << "$EndElementData" << endl;
202  }
203 }
204 
206 {
207  xprintf(MsgLog, "%s: Writing output file %s ... ", __func__,
208  this->_base_filename.c_str());
209 
210  this->write_msh_header();
211 
212  this->write_msh_geometry();
213 
214  this->write_msh_topology();
215 
216  xprintf(MsgLog, "O.K.\n");
217 
218  return 1;
219 }
220 
222 {
223  xprintf(MsgLog, "%s: Writing output file %s ... ", __func__,
224  this->_base_filename.c_str());
225 
226  // Write header with mesh, when it hasn't been written to output file yet
227  if(this->header_written == false) {
228  this->write_head();
229  this->header_written = true;
230  }
231 
232  this->write_msh_node_data(this->time, this->current_step);
233  this->write_msh_elem_data(this->time, this->current_step);
234 
235  // Flush stream to be sure everything is in the file now
236  this->_base_file.flush();
237 
238  xprintf(MsgLog, "O.K.\n");
239 
240  return 1;
241 }
242 
243 
244 
246 {
247  return 1;
248 }
249 
250 
251 
253 {
254  this->fix_main_file_extension(".msh");
255  this->header_written = false;
256 
257  if(this->rank == 0) {
258  this->_base_file.open(this->_base_filename.c_str());
259  INPUT_CHECK( this->_base_file.is_open() , "Can not open output file: %s\n", this->_base_filename.c_str() );
260  xprintf(MsgLog, "Writing flow output file: %s ... \n", this->_base_filename.c_str());
261  }
262 }
263 
265 {
266  this->write_tail();
267 }
268 
void write_msh_header(void)
This function write header of GMSH (.msh) file format.
Definition: output_msh.cc:43
static Input::Type::AbstractRecord input_format_type
The specification of output file format.
Definition: output_time.hh:62
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:178
#define FOR_ELEMENT_NODES(i, j)
Definition: elements.h:150
#define NODE_FULL_ITER(_mesh_, i)
Definition: mesh.h:80
OutputMSH()
The constructor of this class.
void fix_main_file_extension(std::string extension)
Definition: output_time.cc:110
#define FOR_ELEMENTS(_mesh_, __i)
Definition: mesh.h:364
void write_msh_geometry(void)
This function writes geometry (position of nodes) to GMSH (.msh) file format.
Definition: output_msh.cc:53
#define ELEM_FULL_ITER(_mesh_, i)
Definition: mesh.h:92
int write_tail(void)
This method should write tail of GMSH (.msh) file format.
Definition: output_msh.cc:245
Small extension of Vector&lt;T&gt; container with support to Id numbers.
Definition: sys_vector.hh:164
static Input::Type::Record input_type
The definition of input record for gmsh file format.
Definition: output_msh.hh:60
???
Definition: mesh.h:109
Record & derive_from(AbstractRecord &parent)
Definition: type_record.cc:197
Definition: system.hh:72
unsigned int size() const
Returns size of the container. This is independent of the allocated space.
Definition: sys_vector.hh:401
unsigned int n_elements() const
Definition: mesh.h:141
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:92
void write_msh_topology(void)
This function writes topology (connection of nodes) to the GMSH (.msh) file format.
Definition: output_msh.cc:67
Accessor to the data with type Type::Record.
Definition: accessors.hh:327
#define xprintf(...)
Definition: system.hh:100
void write_msh_ascii_discont_data(OutputDataPtr output_data)
This function writes discontinuous ascii data to GMSH (.msh) output file.
Definition: output_msh.cc:108
bool header_written
Definition: output_msh.hh:89
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:131
~OutputMSH()
The destructor of this class.
Definition: output_msh.cc:264
ofstream _base_file
Definition: output_time.hh:212
The class for outputting data during time.
Definition: output_time.hh:32
#define FOR_NODES(_mesh_, i)
Definition: mesh.h:72
string _base_filename
Definition: output_time.hh:217
#define INPUT_CHECK(i,...)
Debugging macros.
Definition: global_defs.h:61
int write_data(void)
This method writes data to GMSH (.msh) file format for current time.
Definition: output_msh.cc:221
std::shared_ptr< OutputDataBase > OutputDataPtr
Definition: output_time.hh:173
Record type proxy class.
Definition: type_record.hh:169
int write_head(void)
This method writes head of GMSH (.msh) file format.
Definition: output_msh.cc:205
NodeVector node_vector
Vector of nodes of the mesh.
Definition: mesh.h:203