Flow123d
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 "io/output.h"
31 #include "io/output_msh.h"
32 #include "system/xio.h"
33 #include "mesh/mesh.h"
34 
35 
36 using namespace Input::Type;
37 
39  = Record("gmsh", "Parameters of gmsh output format.")
40  // It is derived from abstract class
42 
43 
45 {
46  ofstream &file = this->get_base_file();
47 
48  // Write simple header
49  file << "$MeshFormat" << endl;
50  file << "2" << " 0 " << sizeof(double) << endl;
51  file << "$EndMeshFormat" << endl;
52 }
53 
55 {
56  ofstream &file = this->get_base_file();
57  Mesh* mesh = this->get_mesh();
58 
59  // Write information about nodes
60  file << "$Nodes" << endl;
61  file << mesh->node_vector.size() << endl;
62  FOR_NODES(mesh, nod) {
63  file << NODE_FULL_ITER(mesh, nod).id() << " " << nod->getX() << " " << nod->getY() << " " << nod->getZ() << endl;
64  }
65  file << "$EndNodes" << endl;
66 }
67 
69 {
70  ofstream &file = this->get_base_file();
71  Mesh* mesh = this->get_mesh();
72  unsigned int i;
73  const static unsigned int gmsh_simplex_types_[4] = {0, 1, 2, 4};
74 
75  // Write information about elements
76  file << "$Elements" << endl;
77  file << mesh->n_elements() << endl;
78  FOR_ELEMENTS(mesh, elm) {
79  // element_id element_type 3_other_tags material region partition
80  file << ELEM_FULL_ITER(mesh, elm).id()
81  << " " << gmsh_simplex_types_[ elm->dim() ]
82  << " 3 " << elm->region().id() << " " << elm->region().id() << " " << elm->pid;
83 
84  FOR_ELEMENT_NODES(elm, i)
85  file << " " << NODE_FULL_ITER(mesh, elm->node[i]).id();
86  file << endl;
87  }
88  file << "$EndElements" << endl;
89 }
90 
91 
93 {
94  ofstream &file = this->get_base_file();
95 
96  /* Set precision to max */
97  //file.precision(std::numeric_limits<float>::digits10);
98  file.precision(std::numeric_limits<double>::digits10);
99 
100 
101  for(unsigned int i=0; i < output_data->n_values; i ++) {
102  file << i+1 << " ";
103  output_data->print(file, i);
104  file << std::endl;
105  }
106 
107 }
108 
109 
111 {
112  Mesh *mesh = this->get_mesh();
113  ofstream &file = this->get_base_file();
114 
115  /* Set precision to max */
116  //file.precision(std::numeric_limits<float>::digits10);
117  file.precision(std::numeric_limits<double>::digits10);
118 
119  /* Write ascii data */
120  unsigned int i_node;
121  unsigned int i_corner=0;
122  FOR_ELEMENTS(mesh, ele) {
123  file << ele.id() << " " << ele->n_nodes() << " ";
124 
125  FOR_ELEMENT_NODES(ele, i_node) {
126  output_data->print(file, i_corner++);
127  }
128 
129  file << std::endl;
130  }
131 }
132 
133 
134 void OutputMSH::write_msh_node_data(double time, int step)
135 {
136  ofstream &file = this->get_base_file();
137  Mesh *mesh = this->get_mesh();
138  OutputDataBase *output_data;
139 
140  double time_fixed = isfinite(time)?time:0;
141 
142  if(this->node_data.empty() == false) {
143  for(vector<OutputDataBase*>::iterator data = this->node_data.begin();
144  data != this->node_data.end();
145  ++data)
146  {
147  output_data = *data;
148 
149  file << "$NodeData" << endl;
150 
151  file << "1" << endl; // one string tag
152  file << "\"" << output_data->output_field_name <<"\"" << endl;
153 
154  file << "1" << endl; // one real tag
155  file << time_fixed << endl; // first real tag = time
156 
157  file << "3" << endl; // 3 integer tags
158  file << step << endl; // step number (start = 0)
159  file << output_data->n_elem_ << endl; // number of components
160  file << output_data->n_values << endl; // number of values
161 
162  this->write_msh_ascii_cont_data(output_data);
163 
164  file << "$EndNodeData" << endl;
165  }
166  } else if(this->corner_data.empty() == false) {
167  for(vector<OutputDataBase*>::iterator data = this->corner_data.begin();
168  data != this->corner_data.end();
169  ++data)
170  {
171  output_data = *data;
172 
173  file << "$ElementNodeData" << endl;
174 
175  file << "1" << endl; // one string tag
176  file << "\"" << output_data->output_field_name <<"\"" << endl;
177 
178  file << "1" << endl; // one real tag
179  file << time_fixed << endl; // first real tag = time
180 
181  file << "3" << endl; // 3 integer tags
182  file << step << endl; // step number (start = 0)
183  file << output_data->n_elem_ << endl; // number of components
184  file << mesh->n_elements() << endl; // number of values
185 
186  this->write_msh_ascii_discont_data(output_data);
187 
188  file << "$EndElementNodeData" << endl;
189  }
190  }
191 }
192 
193 void OutputMSH::write_msh_elem_data(double time, int step)
194 {
195  OutputDataBase* output_data;
196  ofstream &file = this->get_base_file();
197 
198  double time_fixed = isfinite(time)?time:0;
199 
200  if(this->elem_data.empty() == false) {
201  for(vector<OutputDataBase*>::iterator data = this->elem_data.begin();
202  data != this->elem_data.end();
203  ++data)
204  {
205  output_data = *data;
206  file << "$ElementData" << endl;
207 
208  file << "1" << endl; // one string tag
209  file << "\"" << output_data->output_field_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 << 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_cont_data(output_data);
220 
221  file << "$EndElementData" << endl;
222  }
223  }
224 }
225 
227 {
228  xprintf(MsgLog, "%s: Writing output file %s ... ", __func__, this->base_filename()->c_str());
229 
230  this->write_msh_header();
231 
232  this->write_msh_geometry();
233 
234  this->write_msh_topology();
235 
236  xprintf(MsgLog, "O.K.\n");
237 
238  return 1;
239 }
240 
242 {
243  xprintf(MsgLog, "%s: Writing output file %s ... ", __func__, this->base_filename()->c_str());
244 
245  // Write header with mesh, when it hasn't been written to output file yet
246  if(this->header_written == false) {
247  this->write_head();
248  this->header_written = true;
249  }
250 
251  this->write_msh_node_data(this->time, this->current_step);
252  this->write_msh_elem_data(this->time, this->current_step);
253 
254  // Flush stream to be sure everything is in the file now
255  this->get_base_file().flush();
256 
257  xprintf(MsgLog, "O.K.\n");
258 
259  return 1;
260 }
261 
263 {
264  return 1;
265 }
266 
268 {
270  this->header_written = false;
271 }
272 
274 {
275  this->write_tail();
276 }
277