plt_loader_write.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 // C++ includes
21 #include <fstream>
22 
23 // Local includes
24 #include "libmesh/plt_loader.h"
25 
26 namespace libMesh
27 {
28 
29 
30 
31 // extern "C" {
32 
33 // void open_ (const int *, const char *, int *);
34 // void idata_ (const int *, int *, int *);
35 // void fdata_ (const int *, float*, int *);
36 // void ddata_ (const int *, double *, int *);
37 // void close_ (const int *);
38 // }
39 
40 
41 
42 //-----------------------------------------------------------------------------
43 // PltLoader write members
44 // void PltLoader::write_plot3d (const std::string & basename,
45 // const bool reverse,
46 // const bool gridonly) const
47 // {
48 // const std::string gname = basename + ".g";
49 // const std::string qname = basename + ".q";
50 
51 // // FORTAN file unit numbers
52 // const int gunit = 25;
53 // const int qunit = 26;
54 
55 // // Tell the user what files we are creating
56 // if (this->verbose())
57 // {
58 // libMesh::out << "Plot3D output will be written to " << gname;
59 
60 // if (!gridonly)
61 // libMesh::out << " and " << qname;
62 
63 // libMesh::out << std::endl;
64 // }
65 
66 
67 // // Open the FORTRAN unformatted file
68 // {
69 // libmesh_assert_equal_to (gname.size(), qname.size());
70 // int len = gname.size();
71 
72 // open_ (&gunit, gname.c_str(), &len);
73 
74 // if (!gridonly)
75 // open_ (&qunit, qname.c_str(), &len);
76 // }
77 
78 // // Write the headers
79 // {
80 // std::vector<int> ints;
81 // ints.reserve (3*this->n_zones());
82 
83 // for (unsigned int zn=0; zn<this->n_zones(); zn++)
84 // {
85 // ints.push_back(this->imax(zn));
86 // ints.push_back(this->jmax(zn));
87 // ints.push_back(this->kmax(zn));
88 // }
89 
90 // int nb = this->n_zones();
91 // int one = 1;
92 // int len = ints.size();
93 
94 // libmesh_assert_equal_to (static_cast<unsigned int>(len), 3*this->n_zones());
95 
96 // idata_ (&gunit, &nb, &one);
97 // idata_ (&gunit, ints.data(), &len);
98 
99 // if (!gridonly)
100 // {
101 // idata_ (&qunit, &nb, &one);
102 // idata_ (&qunit, ints.data(), &len);
103 // }
104 // }
105 
106 
107 // // Variables to write to plot3D file
108 // std::vector<unsigned int> write_vars;
109 // write_vars.reserve (5);
110 
111 // std::fill (write_vars.begin(), write_vars.end(), 0);
112 
113 
114 // //------------------------------------------------------------------------
115 // // Ask the user which variables to write
116 // if (!gridonly)
117 // {
118 // libMesh::out << "Variables:" << std::endl;
119 
120 // for (unsigned int v=0; v<this->n_vars(); v++)
121 // libMesh::out << " " << v << ") \"" << this->var_name(v) << "\""
122 // << std::endl;
123 // libMesh::out << std::endl;
124 
125 // int n_write_vars = 0;
126 
127 // while (true)
128 // {
129 // libMesh::out << "How many variables to write to the Plot3D file? 1<=n<=" << this->n_vars()
130 // << " "
131 // << std::endl
132 // << "(-1 writes them all): ";
133 
134 // std::cin >> n_write_vars;
135 
136 // if (n_write_vars == -1)
137 // break;
138 
139 // if ((n_write_vars >= 1) &&
140 // (n_write_vars <= static_cast<int>(this->n_vars())))
141 // break;
142 // };
143 
144 
145 // // The user wants all the variables
146 // if ((n_write_vars == -1) ||
147 // (n_write_vars == static_cast<int>(this->n_vars())))
148 // {
149 // for (unsigned int wv=0; wv<this->n_vars(); wv++)
150 // write_vars.push_back (wv);
151 // }
152 
153 // // The user wants a subset of the variables
154 // else
155 // {
156 // libmesh_assert_greater_equal (n_write_vars, 1);
157 // libmesh_assert_less (n_write_vars, static_cast<int>(this->n_vars()));
158 
159 // libMesh::out << "Select the " << n_write_vars << " variables to write to the Plot3D file: "
160 // << std::endl;
161 
162 // for (int wv=0; wv<n_write_vars; wv++)
163 // {
164 // int num=0;
165 
166 // std::cin >> num;
167 
168 // libmesh_assert_less (num, static_cast<int>(this->n_vars()));
169 
170 // write_vars.push_back (num);
171 // }
172 // }
173 
174 // libMesh::out << std::endl;
175 // } // if (!gridonly)
176 
177 
178 
179 // //------------------------------------------------------------------------
180 // // Write the coordinates & data for each block
181 // for (unsigned int zn=0; zn<this->n_zones(); zn++)
182 // {
183 // // Write the coordinates
184 // {
185 // std::vector<float> coords; // the nodal coordinates
186 // coords.reserve (3*this->imax(zn)*this->jmax(zn)*this->kmax(zn));
187 
188 // for (unsigned int v=0; v<3; v++)
189 // {
190 // unsigned int l=0;
191 
192 // for (unsigned int k=0; k<this->kmax(zn); k++)
193 // for (unsigned int j=0; j<this->jmax(zn); j++)
194 // for (unsigned int i=0; i<this->imax(zn); i++)
195 // {
196 // libmesh_assert_less (l, _data[zn][v].size());
197 // coords.push_back (_data[zn][v][l++]);
198 // }
199 // }
200 
201 // // Write to the grid file
202 // {
203 // int len = coords.size();
204 // libmesh_assert_equal_to (static_cast<unsigned int>(len),
205 // 3*this->imax(zn)*this->jmax(zn)*this->kmax(zn));
206 
207 // fdata_ (&gunit, coords.data(), &len);
208 // }
209 // }
210 
211 
212 // //------------------------------------------------------------------------
213 // // Write the data
214 // if (!gridonly)
215 // {
216 // std::vector<float> data; // arbitrary data
217 // std::vector<float> conds(4); // plot3D conditions [FSMACH, ALPHA, RE, TIME]
218 // data.reserve (write_vars.size()*this->imax(zn)*this->jmax(zn)*this->kmax(zn));
219 // std::fill (conds.begin(), conds.end(), 0.);
220 
221 // if (zn == 0)
222 // libMesh::out << " Writing ";
223 
224 // for (std::size_t i=0; i<write_vars.size(); i++)
225 // {
226 // // Number of the variable to write
227 // const unsigned int v = write_vars[i];
228 
229 // libmesh_assert_less (v, this->n_vars());
230 
231 // // Tell the user what variable we are writing, but only
232 // // once per file.
233 // if (zn == 0)
234 // libMesh::out << "\"" << this->var_name(v) << "\" ";
235 
236 // unsigned int l=0;
237 
238 // for (unsigned int k=0; k<this->kmax(zn); k++)
239 // for (unsigned int j=0; j<this->jmax(zn); j++)
240 // for (unsigned int i=0; i<this->imax(zn); i++)
241 // {
242 // libmesh_assert_less (l, _data[zn][v].size());
243 // data.push_back ((v < this->n_vars()) ?
244 // _data[zn][v][l++] : 0.);
245 // }
246 // }
247 
248 // if (zn == 0)
249 // libMesh::out << "to " << qname << std::endl;
250 
251 // // Write to the solution file
252 // {
253 // int len = conds.size();
254 
255 // fdata_ (&qunit, conds.data(), &len);
256 // }
257 
258 // // Write to the solution file
259 // {
260 // int len = data.size();
261 // libmesh_assert_equal_to (static_cast<unsigned int>(len),
262 // write_vars.size()*this->imax(zn)*this->jmax(zn)*this->kmax(zn));
263 
264 // fdata_ (&qunit, data.data(), &len);
265 // }
266 // }
267 // }
268 
269 // // Close the FORTAN files
270 // close_ (&gunit);
271 
272 // if (!gridonly)
273 // close_ (&qunit);
274 
275 // // Possibly reverse the orders
276 // if (reverse)
277 // {
278 // if (this->verbose())
279 // libMesh::out << "Reversing byte-ordering for output files."
280 // << std::endl;
281 
282 // Utility::reverse_endian (gname);
283 
284 // if (!gridonly)
285 // Utility::reverse_endian (qname);
286 // }
287 // }
288 
289 
290 
291 // void PltLoader::write_tri (const std::string & name,
292 // const bool reverse,
293 // const bool gridonly) const
294 // {
295 // // Check out
296 // // http://people.nas.nasa.gov/~aftosmis/cart3d/cart3dTriangulations.html
297 // // for the .tri, .triq format
298 
299 // // FORTRAN file unit numbers
300 // const int gunit = 25;
301 
302 // if (this->verbose())
303 // {
304 // libMesh::out << "Writing unformatted .tri file " << name
305 // << std::endl;
306 
307 // if (gridonly)
308 // libMesh::out << "Only writing the grid to " << name
309 // << std::endl;
310 // }
311 
312 
313 // // Open the FORTRAN unformatted file
314 // {
315 // int len = name.size();
316 
317 // open_ (&gunit, name.c_str(), &len);
318 // }
319 
320 
321 // // Write the header
322 // unsigned int n_nodes =0;
323 // unsigned int n_tri =0;
324 // unsigned int n_scalar=this->n_vars()-3;
325 
326 // {
327 // std::vector<int> ints;
328 
329 // for (unsigned int zone=0; zone<this->n_zones(); zone++)
330 // {
331 // libmesh_assert_equal_to (this->elem_type(zone), TRI);
332 // n_nodes += this->n_nodes(zone);
333 // n_tri += this->n_elem(zone);
334 // }
335 
336 // ints.push_back (n_nodes);
337 // ints.push_back (n_tri);
338 
339 // if (!gridonly)
340 // if (this->n_vars() > 3)
341 // ints.push_back(n_scalar);
342 
343 // int len = ints.size();
344 // idata_ (&gunit, ints.data(), &len);
345 // }
346 
347 // // Write the nodal values.
348 // {
349 // std::vector<float> coords;
350 // coords.reserve (3*n_nodes);
351 
352 // for (unsigned int zone=0; zone<this->n_zones(); zone++)
353 // for (unsigned int n=0; n<this->n_nodes(zone); n++)
354 // {
355 // coords.push_back (_data[zone][0][n]);
356 // coords.push_back (_data[zone][1][n]);
357 // coords.push_back (_data[zone][2][n]);
358 // }
359 // // Got all the nodes for all the zones
360 
361 
362 // int len = coords.size();
363 // fdata_ (&gunit, coords.data(), &len);
364 // }
365 
366 // // Write the connectivity
367 // {
368 // std::vector<int> conn;
369 // conn.reserve (3*n_tri);
370 
371 // for (unsigned int zone=0; zone<this->n_zones(); zone++)
372 // {
373 // // The connectivity for this zone
374 // const std::vector<int> & zconn = _conn[zone];
375 
376 // libmesh_assert (!zconn.empty());
377 
378 // // Append the connectivity for this zone to the connectivity
379 // // array
380 // conn.insert (conn.end(), zconn.begin(), zconn.end());
381 // }
382 
383 // int len = conn.size();
384 // libmesh_assert_equal_to (static_cast<unsigned int>(len), 3*n_tri);
385 // idata_ (&gunit, conn.data(), &len);
386 // }
387 
388 
389 // // Write the component index for each triangle
390 // {
391 // std::vector<int> comp;
392 // comp.reserve (n_tri);
393 
394 // for (unsigned int zone=0; zone<this->n_zones(); zone++)
395 // comp.insert (comp.end(), this->n_elem(zone), zone+1);
396 
397 // int len = comp.size();
398 // libmesh_assert_equal_to (static_cast<unsigned int>(len), n_tri);
399 // idata_ (&gunit, comp.data(), &len);
400 // }
401 
402 
403 // // Possibly write additional values for each node
404 // if (!gridonly)
405 // if (this->n_vars() > 3)
406 // {
407 // if (this->verbose())
408 // {
409 // libMesh::out << "Writing variables ";
410 
411 // for (unsigned int v=3; v<this->n_vars(); v++)
412 // libMesh::out << "\"" << this->var_name(v) << "\" ";
413 
414 // libMesh::out << "to the output file " << name
415 // << std::endl;
416 // }
417 
418 // std::vector<float> data;
419 
420 // data.reserve (n_nodes*(this->n_vars()-3));
421 
422 // for (unsigned int zone=0; zone<this->n_zones(); zone++)
423 // for (unsigned int n=0; n<this->n_nodes(zone); n++)
424 // for (unsigned int v=3; v<this->n_vars(); v++)
425 // data.push_back (_data[zone][v][n]);
426 
427 // int len = data.size();
428 // libmesh_assert_equal_to (static_cast<unsigned int>(len),
429 // n_nodes*(this->n_vars()-3));
430 // fdata_ (&gunit, data.data(), &len);
431 // }
432 
433 
434 // // Close the FORTRAN file
435 // close_ (&gunit);
436 
437 
438 // // Possibly reverse the orders
439 // if (reverse)
440 // {
441 // if (this->verbose())
442 // libMesh::out << "Reversing byte-ordering for output files."
443 // << std::endl;
444 
445 // Utility::reverse_endian (name);
446 // }
447 // }
448 
449 
450 
451 void PltLoader::write_dat (const std::string & name,
452  const unsigned int version_in) const
453 {
454  std::ofstream out_stream (name.c_str());
455 
456  out_stream << "TITLE=\""
457  << this->title()
458  << "\""
459  << '\n';
460 
461  out_stream << "VARIABLES = ";
462 
463  for (unsigned int v=0; v<this->n_vars(); v++)
464  out_stream << "\"" << this->var_name(v) << "\"\n";
465 
466  for (unsigned int z=0; z<this->n_zones(); z++)
467  {
468  out_stream << "ZONE T=\"" << this->zone_name(z) << "\"\n";
469  out_stream << " I=" << this->imax(z)
470  << ", J=" << this->jmax(z)
471  << ", K=" << this->kmax(z);
472 
473  // Write BLOCK data for this zone
474  if (this->zone_type(z) == BLOCK)
475  {
476  if (version_in < 10)
477  {
478  out_stream << ", F=BLOCK\n";
479  }
480  else
481  {
482  out_stream << ", ZONETYPE=Ordered\n"
483  << "DATAPACKING=BLOCK\n";
484  }
485 
486  out_stream << "DT=(";
487  for (unsigned int v=0; v<this->n_vars(); v++)
488  out_stream << "SINGLE ";
489  out_stream << ")\n";
490 
491  out_stream.precision(9);
492 
493  for (unsigned int v=0; v<this->n_vars(); v++)
494  {
495  unsigned int l=0;
496 
497  for (unsigned int k=0; k<this->kmax(z); k++)
498  for (unsigned int j=0; j<this->jmax(z); j++)
499  for (unsigned int i=0; i<this->imax(z); i++)
500  {
501  out_stream << std::scientific
502  << _data[z][v][l++] << " ";
503 
504  // Throw in a newline every 5 entries to
505  // avoid really long lines.
506  if (l%5 == 0)
507  out_stream << '\n';
508  }
509 
510  if (l%5 != 0)
511  out_stream << '\n';
512  }
513  } // end if (this->zone_type(z) == BLOCK)
514 
515  // Write POINT data for this zone
516  else if (this->zone_type(z) == POINT)
517  {
518  if (version_in < 10)
519  {
520  out_stream << ", F=POINT\n";
521  }
522  else
523  {
524  out_stream << ", ZONETYPE=Ordered\n"
525  << "DATAPACKING=POINT\n";
526  }
527 
528  out_stream << "DT=(";
529  for (unsigned int v=0; v<this->n_vars(); v++)
530  out_stream << "SINGLE ";
531  out_stream << ")\n";
532 
533  out_stream.precision(9);
534 
535  {
536  unsigned int l=0;
537 
538  for (unsigned int k=0; k<this->kmax(z); k++)
539  for (unsigned int j=0; j<this->jmax(z); j++)
540  for (unsigned int i=0; i<this->imax(z); i++)
541  {
542  for (unsigned int v=0; v<this->n_vars(); v++)
543  out_stream << std::scientific
544  << _data[z][v][l] << " ";
545 
546  out_stream << '\n';
547 
548  l++;
549  }
550  }
551  } // end else if (this->zone_type(z) == POINT)
552 
553  // Otherwise, unrecognized zone type
554  else
555  libmesh_error_msg("Unrecognized zone type: this->zone_type(z)==" << this->zone_type(z));
556  }
557 }
558 
559 } // namespace libMesh
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
unsigned int n_vars() const
Definition: plt_loader.h:180
std::vector< std::vector< std::vector< float > > > _data
Definition: plt_loader.h:430
const std::string & var_name(const unsigned int v) const
Definition: plt_loader.h:479
unsigned int imax(const unsigned int z) const
Definition: plt_loader.h:599
const std::string & zone_name(const unsigned int z) const
Definition: plt_loader.h:551
unsigned int jmax(const unsigned int z) const
Definition: plt_loader.h:621
const std::string & title() const
Definition: plt_loader.h:175
unsigned int n_zones() const
Definition: plt_loader.h:195
unsigned int kmax(const unsigned int z) const
Definition: plt_loader.h:643
void write_dat(const std::string &name, const unsigned int version=10) const
unsigned int zone_type(const unsigned int z) const
Definition: plt_loader.h:527