BUG: eof errors in the STARCD, VTK file reader (#1059)

- previously simply read files until the input stream went bad and no
  more lines could be read.  With the more stringent checking of
  values read (commit 0ce7e364a4) this approach causes problems.

  Use the underlying tokenizer instead to decide about termination.
This commit is contained in:
Mark Olesen
2018-11-03 15:46:37 +01:00
parent 1f9533ed5c
commit fd54070c3a
14 changed files with 402 additions and 457 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -24,14 +24,9 @@ License
\*---------------------------------------------------------------------------*/
#include "STARCDCore.H"
#include "ListOps.H"
#include "clock.H"
#include "bitSet.H"
#include "DynamicList.H"
#include "StringStream.H"
#include "OSspecific.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::Enum
@ -85,9 +80,41 @@ Foam::fileFormats::STARCDCore::starToFoamFaceAddr =
{ starcdPyr, { 0, -1, 4, 2, 1, 3 } }
};
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
// Read and discard to newline
static inline void readToNewline(ISstream& is)
{
char ch = '\n';
do
{
is.get(ch);
}
while ((is) && ch != '\n');
}
} // End namespace Foam
// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
// Read two-line header
//
/*---------------------------------------------------------------------------*\
Line 1:
PROSTAR_(BOUNDARY|CELL|VERTEX) [newline]
Line 2:
<version> 0 0 0 0 0 0 0 [newline]
Body:
...
\*---------------------------------------------------------------------------*/
bool Foam::fileFormats::STARCDCore::readHeader
(
IFstream& is,
@ -100,23 +127,20 @@ bool Foam::fileFormats::STARCDCore::readHeader
<< abort(FatalError);
}
word magic;
word magic;
is >> magic;
readToNewline(is);
label majorVersion;
is >> majorVersion;
readToNewline(is);
string line;
is.getLine(line);
IStringStream(line)() >> magic;
is.getLine(line);
IStringStream(line)() >> majorVersion;
// add other checks ...
// Add other checks ...
if (magic != fileHeaders_[header])
{
Info<< "header mismatch " << fileHeaders_[header]
Info<< "Header mismatch " << fileHeaders_[header]
<< " " << is.name()
<< endl;
<< nl;
return false;
}
@ -133,14 +157,14 @@ void Foam::fileFormats::STARCDCore::writeHeader
{
os << fileHeaders_[header] << nl
<< 4000
<< " " << 0
<< " " << 0
<< " " << 0
<< " " << 0
<< " " << 0
<< " " << 0
<< " " << 0
<< endl;
<< ' ' << 0
<< ' ' << 0
<< ' ' << 0
<< ' ' << 0
<< ' ' << 0
<< ' ' << 0
<< ' ' << 0
<< nl;
}
@ -173,6 +197,7 @@ Foam::label Foam::fileFormats::STARCDCore::readPoints
)
{
label maxId = 0;
token tok;
if (!is.good())
{
@ -183,24 +208,26 @@ Foam::label Foam::fileFormats::STARCDCore::readPoints
readHeader(is, HEADER_VRT);
// reuse memory if possible
// Reuse memory if possible
DynamicList<point> dynPoints(std::move(points));
DynamicList<label> dynPointId(std::move(ids)); // STAR-CD index of points
DynamicList<label> dynPointId(std::move(ids)); // STARCD index of points
dynPoints.clear();
dynPointId.clear();
{
label lineLabel;
scalar x, y, z;
while ((is >> lineLabel).good())
while (is.read(tok).good() && tok.isLabel())
{
maxId = max(maxId, lineLabel);
const label starVertexId = tok.labelToken();
is >> x >> y >> z;
maxId = max(maxId, starVertexId);
dynPoints.append(point(x, y, z));
dynPointId.append(lineLabel);
dynPointId.append(starVertexId);
}
}
@ -223,18 +250,23 @@ void Foam::fileFormats::STARCDCore::writePoints
// Set the precision of the points data to 10
os.precision(10);
// force decimal point for Fortran input
// Force decimal point for Fortran input
os.setf(std::ios::showpoint);
forAll(points, ptI)
label starVertId = 1; // 1-based vertex labels
for (const point& p : points)
{
// convert [m] -> [mm] etc
// Convert [m] -> [mm] etc
os
<< ptI + 1 << ' '
<< scaleFactor * points[ptI].x() << ' '
<< scaleFactor * points[ptI].y() << ' '
<< scaleFactor * points[ptI].z() << '\n';
<< starVertId << ' '
<< scaleFactor * p.x() << ' '
<< scaleFactor * p.y() << ' '
<< scaleFactor * p.z() << nl;
++starVertId;
}
os.flush();
}

View File

@ -93,7 +93,7 @@ void Foam::vtkUnstructuredReader::warnUnhandledType
if (warningGiven.insert(type))
{
IOWarningInFunction(inFile)
<< "Skipping unknown cell type " << type << endl;
<< "Skipping unknown cell type " << type << nl;
}
}
@ -344,10 +344,9 @@ void Foam::vtkUnstructuredReader::extractCells
}
}
if (debug)
{
Info<< "Read " << celli << " cells;" << facei << " faces." << endl;
}
DebugInfo
<< "Read " << celli << " cells;" << facei << " faces." << nl;
cells_.setSize(celli);
cellMap_.setSize(celli);
faces_.setSize(facei);
@ -401,10 +400,9 @@ void Foam::vtkUnstructuredReader::readField
case VTK_STRING:
{
if (debug)
{
Info<< "Reading strings:" << size << endl;
}
DebugInfo
<< "Reading strings:" << size << nl;
auto fieldVals = autoPtr<stringIOList>::New
(
IOobject(arrayName, "", obj),
@ -425,9 +423,9 @@ void Foam::vtkUnstructuredReader::readField
default:
{
IOWarningInFunction(inFile)
<< "Unhandled type " << dataType << endl
<< "Unhandled type " << dataType << nl
<< "Skipping " << size
<< " words." << endl;
<< " words." << nl;
scalarField fieldVals;
readBlock(inFile, size, fieldVals);
}
@ -437,9 +435,9 @@ void Foam::vtkUnstructuredReader::readField
else
{
IOWarningInFunction(inFile)
<< "Unhandled type " << dataType << endl
<< "Unhandled type " << dataType << nl
<< "Skipping " << size
<< " words." << endl;
<< " words." << nl;
scalarField fieldVals;
readBlock(inFile, size, fieldVals);
}
@ -456,14 +454,14 @@ Foam::wordList Foam::vtkUnstructuredReader::readFieldArray
DynamicList<word> fields;
word dataName(inFile);
if (debug)
{
Info<< "dataName:" << dataName << endl;
}
DebugInfo
<< "dataName:" << dataName << nl;
label numArrays(readLabel(inFile));
if (debug)
{
Pout<< "numArrays:" << numArrays << endl;
Pout<< "numArrays:" << numArrays << nl;
}
for (label i = 0; i < numArrays; i++)
{
@ -472,11 +470,9 @@ Foam::wordList Foam::vtkUnstructuredReader::readFieldArray
label numTuples(readLabel(inFile));
word dataType(inFile);
if (debug)
{
Info<< "Reading field " << arrayName
<< " of " << numTuples << " tuples of rank " << numComp << endl;
}
DebugInfo
<< "Reading field " << arrayName
<< " of " << numTuples << " tuples of rank " << numComp << nl;
if (wantedSize != -1 && numTuples != wantedSize)
{
@ -536,20 +532,13 @@ Foam::vtkUnstructuredReader::vtkUnstructuredReader
void Foam::vtkUnstructuredReader::read(ISstream& inFile)
{
inFile.getLine(header_);
if (debug)
{
Info<< "Header : " << header_ << endl;
}
DebugInfo<< "Header : " << header_ << nl;
inFile.getLine(title_);
if (debug)
{
Info<< "Title : " << title_ << endl;
}
DebugInfo<< "Title : " << title_ << nl;
inFile.getLine(dataType_);
if (debug)
{
Info<< "dataType : " << dataType_ << endl;
}
DebugInfo<< "dataType : " << dataType_ << nl;
if (dataType_ == "BINARY")
{
@ -564,28 +553,21 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
// Temporary storage for vertices of cells.
labelList cellVerts;
while (inFile.good())
token tok;
while (inFile.read(tok).good() && tok.isWord())
{
word tag(inFile);
const word tag = tok.wordToken();
if (!inFile.good())
{
break;
}
if (debug)
{
Info<< "line:" << inFile.lineNumber()
<< " tag:" << tag << endl;
}
DebugInfo
<< "line:" << inFile.lineNumber()
<< " tag:" << tag << nl;
if (tag == "DATASET")
{
word geomType(inFile);
if (debug)
{
Info<< "geomType : " << geomType << endl;
}
DebugInfo<< "geomType : " << geomType << nl;
readMode = parseModeNames[geomType];
wantedSize = -1;
}
@ -593,11 +575,10 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
{
label nPoints(readLabel(inFile));
points_.setSize(nPoints); ///3);
if (debug)
{
Info<< "Reading " << nPoints << " numbers representing "
<< points_.size() << " coordinates." << endl;
}
DebugInfo
<< "Reading " << nPoints << " numbers representing "
<< points_.size() << " coordinates." << nl;
word primitiveTag(inFile);
if (primitiveTag != "float" && primitiveTag != "double")
@ -607,19 +588,18 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
<< primitiveTag
<< exit(FatalIOError);
}
forAll(points_, i)
for (point& p : points_)
{
inFile >> points_[i].x() >> points_[i].y() >> points_[i].z();
inFile >> p.x() >> p.y() >> p.z();
}
}
else if (tag == "CELLS")
{
label nCells(readLabel(inFile));
label nNumbers(readLabel(inFile));
if (debug)
{
Info<< "Reading " << nCells << " cells or faces." << endl;
}
DebugInfo
<< "Reading " << nCells << " cells or faces." << nl;
readBlock(inFile, nNumbers, cellVerts);
}
else if (tag == "CELL_TYPES")
@ -644,10 +624,9 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
{
label nLines(readLabel(inFile));
label nNumbers(readLabel(inFile));
if (debug)
{
Info<< "Reading " << nLines << " lines." << endl;
}
DebugInfo
<< "Reading " << nLines << " lines." << nl;
labelList lineVerts;
readBlock(inFile, nNumbers, lineVerts);
@ -674,10 +653,9 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
label nFaces(readLabel(inFile));
label nNumbers(readLabel(inFile));
if (debug)
{
Info<< "Reading " << nFaces << " faces." << endl;
}
DebugInfo
<< "Reading " << nFaces << " faces." << nl;
labelList faceVerts;
readBlock(inFile, nNumbers, faceVerts);
@ -740,12 +718,10 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
word dataType(is);
//label numComp(readLabel(inFile));
if (debug)
{
Info<< "Reading scalar " << dataName
<< " of type " << dataType
<< " from lookup table" << endl;
}
DebugInfo
<< "Reading scalar " << dataName
<< " of type " << dataType
<< " from lookup table" << nl;
word lookupTableTag(inFile);
if (lookupTableTag != "LOOKUP_TABLE")
@ -775,11 +751,9 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
IStringStream is(line);
word dataName(is);
word dataType(is);
if (debug)
{
Info<< "Reading vector " << dataName
<< " of type " << dataType << endl;
}
DebugInfo
<< "Reading vector " << dataName
<< " of type " << dataType << nl;
objectRegistry& reg = selectRegistry(readMode);
@ -827,12 +801,10 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
label dim(readLabel(is));
word dataType(is);
if (debug)
{
Info<< "Reading texture coords " << dataName
<< " dimension " << dim
<< " of type " << dataType << endl;
}
DebugInfo
<< "Reading texture coords " << dataName
<< " dimension " << dim
<< " of type " << dataType << nl;
scalarField coords(dim*points_.size());
readBlock(inFile, coords.size(), coords);
@ -841,10 +813,9 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
{
label nStrips(readLabel(inFile));
label nNumbers(readLabel(inFile));
if (debug)
{
Info<< "Reading " << nStrips << " triangle strips." << endl;
}
DebugInfo
<< "Reading " << nStrips << " triangle strips." << nl;
labelList faceVerts;
readBlock(inFile, nNumbers, faceVerts);
@ -897,11 +868,9 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
<< infoTag << exit(FatalIOError);
}
label nInfo(readLabel(inFile));
if (debug)
{
Info<< "Consuming " << nInfo << " metadata information."
<< endl;
}
DebugInfo
<< "Consuming " << nInfo << " metadata information." << nl;
string line;
// Consume rest of line
inFile.getLine(line);
@ -926,9 +895,8 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
label nSwapped = 0;
forAll(cells_, celli)
for (cellShape& shape : cells_)
{
cellShape& shape = cells_[celli];
if (shape.model() == prism)
{
const triPointRef bottom
@ -961,7 +929,7 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
if (nSwapped > 0)
{
WarningInFunction << "Swapped " << nSwapped << " prismatic cells"
<< endl;
<< nl;
}
}
@ -971,23 +939,23 @@ void Foam::vtkUnstructuredReader::read(ISstream& inFile)
<< " cellShapes:" << cells_.size()
<< " faces:" << faces_.size()
<< " lines:" << lines_.size()
<< nl << endl;
<< nl << nl;
Info<< "Cell fields:" << endl;
Info<< "Cell fields:" << nl;
printFieldStats<vectorIOField>(cellData_);
printFieldStats<scalarIOField>(cellData_);
printFieldStats<labelIOField>(cellData_);
printFieldStats<stringIOList>(cellData_);
Info<< nl << endl;
Info<< nl << nl;
Info<< "Point fields:" << endl;
Info<< "Point fields:" << nl;
printFieldStats<vectorIOField>(pointData_);
printFieldStats<scalarIOField>(pointData_);
printFieldStats<labelIOField>(pointData_);
printFieldStats<stringIOList>(pointData_);
Info<< nl << endl;
Info<< nl << nl;
Info<< "Other fields:" << endl;
Info<< "Other fields:" << nl;
printFieldStats<vectorIOField>(otherData_);
printFieldStats<scalarIOField>(otherData_);
printFieldStats<labelIOField>(otherData_);

View File

@ -41,9 +41,9 @@ void Foam::vtkUnstructuredReader::readBlock
) const
{
list.setSize(n);
forAll(list, i)
for (T& val : list)
{
inFile >> list[i];
inFile >> val;
}
}