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

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo