📄 plt_loader_read.c
字号:
// $Id: plt_loader_read.C 2789 2008-04-13 02:24:40Z roystgnr $// Copyright (C) 2002-2007 Benjamin S. Kirk // This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version. // This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details. // You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA// C++ includes#include <iostream>#include <fstream>#include <cstring>// Local includes#include "utility.h"#include "plt_loader.h"//-----------------------------------------------------------------------------// PltLoader reading membersvoid PltLoader::read (const std::string& name){ std::ifstream in (name.c_str(), std::ios::in|std::ios::binary); if (!in.good()) { std::cerr << "Error reading input file " << name << std::endl; libmesh_error(); } if (this->verbose()) std::cout << std::endl << "Reading input file " << name << std::endl << "-------------------------------------------------------------------------" << std::endl; this->read_header (in); this->read_data (in); if (this->verbose()) std::cout << std::endl << "-------------------------------------------------------------------------" << std::endl;}void PltLoader::read_header (std::istream& in){ libmesh_assert (in.good()); //---------------------------------------------------- // Read the TECPLOT header // Read the version number { in.read (buf, 8); // Using erase for GCC 2.95.3 this->version().erase(); for (unsigned int i=0; i<8; i++) this->version() += buf[i]; if (this->verbose()) std::cout << "Tecplot Version: " << this->version() << std::endl; } //---------------------------------------------------- // Read plt files written by older versions of Tecplot if (this->version().rfind("V7") < this->version().size()) { if (this->verbose()) std::cout << "Reading legacy .plt format (<= v9) ..." << std::endl; // Read the value of 1 to determine byte ordering { int one = 0; in.read (buf, SIZEOF_INT); std::memcpy (&one, buf, SIZEOF_INT); if (one != 1) { if (this->verbose()) std::cout << "Tecplot data is Foreign!" << std::endl; this->is_foreign() = true; // Make sure one reversed is one Utility::ReverseBytes rb(this->is_foreign()); libmesh_assert (rb(one) == 1); } } // A byte-reverser in case the data is foreign Utility::ReverseBytes rb(this->is_foreign()); // Read the title { int i=0; // Using erase for GCC 2.95.3 this->title().erase(); do { in.read (buf, SIZEOF_INT); std::memcpy (&i, buf, SIZEOF_INT); rb(i); // Don't add trailing \0 if (i) this->title() += static_cast<char>(i); } while (i); } // Read the number of variables in the data set { int nv; in.read (buf, SIZEOF_INT); std::memcpy (&nv, buf, SIZEOF_INT); rb(nv); this->set_n_vars (nv); } // Read the variable names for (unsigned int v=0; v<this->n_vars(); v++) { int i=0; // Using erase for GCC 2.95.3 this->var_name(v).erase(); do { in.read (buf, SIZEOF_INT); std::memcpy (&i, buf, SIZEOF_INT); rb(i); // Don't add trailing \0 if (i) this->var_name(v) += static_cast<char>(i); } while (i); } // Read zones from the header. // Continue reading until the end-of-header // marker (357.) is found. int nz=0; std::vector<std::string> zname; std::vector<int> ztype, zimax, zjmax, zkmax; { float f=0.; do { // find the next Zone marker do { f = 0.; in.read (buf, SIZEOF_FLOAT); std::memcpy (&f, buf, SIZEOF_FLOAT); rb(f); } while ((f != 299.) && (f != 357.) && in.good()); // Did we overrun the file? if (!in.good()) { std::cerr << "ERROR: Unexpected end-of-file!" << std::endl; libmesh_error(); } // Found a Zone marker else if (f == 299.) { // Incriment the Zone counter nz++; // Read the zone name { int i=0; std::string name; do { in.read (buf, SIZEOF_INT); std::memcpy (&i, buf, SIZEOF_INT); rb(i); // Don't add trailing \0 if (i) name += static_cast<char>(i); } while (i); zname.push_back(name); } // Read the zone format { int zt; in.read (buf, SIZEOF_INT); std::memcpy (&zt, buf, SIZEOF_INT); rb(zt); ztype.push_back(zt); //std::cout << "zone type=" << ztype.back() << std::endl; } // Read the zone color { int zc=0; in.read (buf, SIZEOF_INT); std::memcpy (&zc, buf, SIZEOF_INT); rb(zc); //std::cout << "zone color=" << zc << std::endl; } // Read in the block dimensions { int imax=0, jmax=0, kmax=0; in.read (buf, SIZEOF_INT); std::memcpy (&imax, buf, SIZEOF_INT); rb(imax); in.read (buf, SIZEOF_INT); std::memcpy (&jmax, buf, SIZEOF_INT); rb(jmax); in.read (buf, SIZEOF_INT); std::memcpy (&kmax, buf, SIZEOF_INT); rb(kmax); zimax.push_back (imax); zjmax.push_back (jmax); zkmax.push_back (kmax); } } // else if (f == 299.) } while ((f != 357.) && in.good()); } // Set the header data this->set_n_zones (nz); for (unsigned int z=0; z<this->n_zones(); z++) { this->zone_type(z) = ztype[z]; this->zone_name(z) = zname[z]; this->imax(z) = zimax[z]; this->jmax(z) = zjmax[z]; this->kmax(z) = zkmax[z]; } } //---------------------------------------------------- // Read plt files written by newer versions of Tecplot else if (this->version().rfind("V1") < this->version().size()) { if (this->verbose()) std::cout << "Reading new .plt format (>= v10)..." << std::endl; // Read the value of 1 to determine byte ordering { int one = 0; in.read (buf, SIZEOF_INT); std::memcpy (&one, buf, SIZEOF_INT); if (one != 1) { if (this->verbose()) std::cerr << "Tecplot data is Foreign!" << std::endl; this->is_foreign() = true; // Make sure one reversed is one Utility::ReverseBytes rb(this->is_foreign()); libmesh_assert (rb(one) == 1); } } // A byte-reverser in case the data is foreign Utility::ReverseBytes rb(this->is_foreign()); // Read the title { int i=0; // Using erase() for GCC 2.95.3 this->title().erase(); do { in.read (buf, SIZEOF_INT); std::memcpy (&i, buf, SIZEOF_INT); rb(i); // Don't add trailing \0 if (i) this->title() += static_cast<char>(i); } while (i); } // Read the number of variables in the data set { int nv; in.read (buf, SIZEOF_INT); std::memcpy (&nv, buf, SIZEOF_INT); rb(nv); this->set_n_vars (nv); } // Read the variable names for (unsigned int v=0; v<this->n_vars(); v++) { int i=0; // Using erase() for GCC 2.95.3 this->var_name(v).erase(); do { in.read (buf, SIZEOF_INT); std::memcpy (&i, buf, SIZEOF_INT); rb(i); // Don't add trailing \0 if (i) this->var_name(v) += static_cast<char>(i); } while (i); } // Read zones from the header. // Continue reading until the end-of-header // marker (357.) is found. int nz=0; std::vector<std::string> zname; std::vector<int> zpack, ztype, zimax, zjmax, zkmax, znelem, znnodes; { float f=0.; do { // find the next Zone marker do { f = 0.; in.read (buf, SIZEOF_FLOAT); std::memcpy (&f, buf, SIZEOF_FLOAT); rb(f); } while ((f != 299.) && (f != 357.) && in.good()); // Did we overrun the file? if (!in.good()) { std::cerr << "ERROR: Unexpected end-of-file!" << std::endl; libmesh_error(); } // Found a Zone marker else if (f == 299.) { // Incriment the Zone counter nz++; // Read the zone name { int i=0; std::string name; do { in.read (buf, SIZEOF_INT); std::memcpy (&i, buf, SIZEOF_INT); rb(i); // Don't add trailing \0 if (i) name += static_cast<char>(i); } while (i); zname.push_back(name); } // Read the zone color { int zc=0; in.read (buf, SIZEOF_INT); std::memcpy (&zc, buf, SIZEOF_INT); rb(zc); } // Read the zone format { int zt; in.read (buf, SIZEOF_INT); std::memcpy (&zt, buf, SIZEOF_INT); rb(zt); ztype.push_back(zt); } // Read the data packing flag { int dp=0; in.read (buf, SIZEOF_INT); std::memcpy (&dp, buf, SIZEOF_INT); rb(dp); zpack.push_back (dp); } // Will we specify the variable location? { int svl=0; int vl=0; in.read (buf, SIZEOF_INT); std::memcpy (&svl, buf, SIZEOF_INT); rb(svl); if (svl) for (unsigned int v=0; v<this->n_vars(); v++) { in.read (buf, SIZEOF_INT); std::memcpy (&vl, buf, SIZEOF_INT); rb(vl); libmesh_assert (vl == 0); // Only know about node-based data // right now } } // Get the number of user-defined face-neighbors { int fn=0; in.read (buf, SIZEOF_INT); std::memcpy (&fn, buf, SIZEOF_INT); rb(fn); } // Read in the block dimensions { if (ztype.back() != ORDERED) { int np=0, ne=0; in.read (buf, SIZEOF_INT); std::memcpy (&np, buf, SIZEOF_INT); rb(np); in.read (buf, SIZEOF_INT); std::memcpy (&ne, buf, SIZEOF_INT); rb(ne); zimax.push_back (np); zjmax.push_back (ne); zjmax.push_back (0); } int imax=0, jmax=0, kmax=0; in.read (buf, SIZEOF_INT); std::memcpy (&imax, buf, SIZEOF_INT); rb(imax); in.read (buf, SIZEOF_INT); std::memcpy (&jmax, buf, SIZEOF_INT); rb(jmax); in.read (buf, SIZEOF_INT); std::memcpy (&kmax, buf, SIZEOF_INT); rb(kmax); // These are only useful for orderd data. Otherwise // we grabbed the relevant values above. if (ztype.back() != ORDERED) { zimax.push_back (imax); zjmax.push_back (jmax); zkmax.push_back (kmax); } } } // else if (f == 299.) } while ((f != 357.) && in.good()); } // Set the header data this->set_n_zones (nz); for (unsigned int z=0; z<this->n_zones(); z++) { this->zone_type(z) = ztype[z]; this->zone_name(z) = zname[z]; this->zone_pack(z) = zpack[z]; this->imax(z) = zimax[z]; this->jmax(z) = zjmax[z]; this->kmax(z) = zkmax[z]; } } //---------------------------------------------------- // Unrecognized Tecplot Version! else { std::cerr << "ERROR: This plot file was written by an unrecognized version of Tecplot!:" << std::endl << this->version() << std::endl; libmesh_error(); } // Print the data to the screen. if (this->verbose()) { std::cout << "Tecplot Header: " << this->title() << std::endl; std::cout << "Variables: "; for (unsigned int v=0; v<this->n_vars(); v++) std::cout << "\"" << this->var_name (v) << "\"" << " "; std::cout << std::endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -