Commit fe13ff56 authored by Andrew Heather's avatar Andrew Heather
Browse files

ENH: Updated ensight surface file reading

parent a415752e
#!/bin/sh
cd ${0%/*} || exit 1 # run from this directory
if [ -f "$FFTW_ARCH_PATH/include/fftw3.h" ] || \
[ "${FFTW_ARCH_PATH##*-}" = system -a -f "/usr/include/fftw3.h" ]
then
wmake
else
echo
echo "Skipping noise utility (no FFTW)"
echo
fi
#------------------------------------------------------------------------------
ensight/file/ensightFile.C
ensight/file/ensightGeoFile.C
ensight/readFile/ensightReadFile.C
ensight/part/ensightPart.C
ensight/part/ensightPartIO.C
ensight/part/ensightPartCells.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM 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 General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "ensightReadFile.H"
#include <sstream>
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::ensightReadFile::ensightReadFile
(
const fileName& pathname,
IOstream::streamFormat format
)
:
IFstream(pathname, format)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::ensightReadFile::~ensightReadFile()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::Istream& Foam::ensightReadFile::read
(
char* buf,
std::streamsize count
)
{
stdStream().read(buf, count);
return *this;
}
Foam::Istream& Foam::ensightReadFile::read(string& value)
{
if (format() == IOstream::BINARY)
{
char buf[80];
read(reinterpret_cast<char*>(buf), sizeof(buf));
string strBuf(value);
const size_t iEnd = strBuf.find('\0', 0);
if (iEnd == string::npos)
{
value = buf;
}
else
{
value = strBuf.substr(0, iEnd - 1);
}
}
else
{
value = "";
while (value.empty() && !eof())
{
getLine(value);
}
}
return *this;
}
Foam::Istream& Foam::ensightReadFile::read(label& value)
{
int ivalue;
if (format() == IOstream::BINARY)
{
read
(
reinterpret_cast<char*>(&ivalue),
sizeof(ivalue)
);
}
else
{
stdStream() >> ivalue;
}
value = ivalue;
return *this;
}
Foam::Istream& Foam::ensightReadFile::read(scalar& value)
{
float fvalue;
if (format() == IOstream::BINARY)
{
read
(
reinterpret_cast<char*>(&fvalue),
sizeof(fvalue)
);
value = fvalue;
}
else
{
stdStream() >> value;
}
return *this;
}
Foam::Istream& Foam::ensightReadFile::readKeyword(string& key)
{
read(key);
return *this;
}
Foam::Istream& Foam::ensightReadFile::readBinaryHeader()
{
if (format() == IOstream::BINARY)
{
string buffer;
read(buffer);
}
return *this;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM 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 General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::ensightReadFile
Description
Ensight output with specialized read() for strings, integers and floats.
Correctly handles binary read as well.
\*---------------------------------------------------------------------------*/
#ifndef ensightReadFile_H
#define ensightReadFile_H
#include "IFstream.H"
#include "IOstream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class ensightReadFile Declaration
\*---------------------------------------------------------------------------*/
class ensightReadFile
:
public IFstream
{
// Private Member Functions
//- Disallow default bitwise assignment
void operator=(const ensightReadFile&);
//- Disallow default copy constructor
ensightReadFile(const ensightReadFile&);
public:
// Constructors
//- Construct from pathname
ensightReadFile
(
const fileName& pathname,
IOstream::streamFormat format=IOstream::BINARY
);
//- Destructor
~ensightReadFile();
// Output
//- Inherit read from Istream
using Istream::read;
//- Binary read
virtual Istream& read(char* buf, std::streamsize count);
//- Read string as "%80s" or as binary
Istream& read(string& value);
//- Read integer as "%10d" or as binary
Istream& read(label& value);
//- Read float as "%12.5e" or as binary
Istream& read(scalar& value);
//- Read element keyword
virtual Istream& readKeyword(string& key);
//- Read "C Binary" for binary files (eg, geometry/measured)
Istream& readBinaryHeader();
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
......@@ -59,6 +59,48 @@ void Foam::ensightSurfaceReader::skip(const label n, IFstream& is) const
}
void Foam::ensightSurfaceReader::readGeometryHeader(ensightReadFile& is) const
{
// Binary flag string if applicable
is.readBinaryHeader();
string buffer;
// Ensight Geometry File
is.read(buffer);
if (debug) Info<< "buffer: " << buffer << endl;
// Description - 1
is.read(buffer);
if (debug) Info<< "buffer: " << buffer << endl;
// Node info
is.read(buffer);
if (debug) Info<< "buffer: " << buffer << endl;
// Element info
is.read(buffer);
if (debug) Info<< "buffer: " << buffer << endl;
// Part
is.read(buffer);
if (debug) Info<< "buffer: " << buffer << endl;
// Part number
label ibuffer;
is.read(ibuffer);
if (debug) Info<< "ibuffer: " << ibuffer << endl;
// Description - 2
is.read(buffer);
if (debug) Info<< "buffer: " << buffer << endl;
// Co-ordinates
is.read(buffer);
if (debug) Info<< "buffer: " << buffer << endl;
}
void Foam::ensightSurfaceReader::debugSection
(
const word& expected,
......@@ -74,6 +116,11 @@ void Foam::ensightSurfaceReader::debugSection
<< "' but read the word '" << actual << "'"
<< exit(FatalIOError);
}
if (debug)
{
Info<< "Read section header: " << expected << endl;
}
}
......@@ -91,6 +138,8 @@ void Foam::ensightSurfaceReader::readCase(IFstream& is)
<< exit(FatalError);
}
string buffer;
// Read the file
debugSection("FORMAT", is);
skip(3, is); // type: ensight gold
......@@ -124,8 +173,8 @@ void Foam::ensightSurfaceReader::readCase(IFstream& is)
if (debug)
{
Info<< "fieldNames: " << fieldNames << nl
<< "fieldFileNames: " << fieldFileNames << endl;
Info<< "fieldNames: " << fieldNames_ << nl
<< "fieldFileNames: " << fieldFileNames_ << endl;
}
// Start reading time information
......@@ -142,7 +191,7 @@ void Foam::ensightSurfaceReader::readCase(IFstream& is)
}
// Read the time values
skip(2, is); // time values:
skip(2, is);
timeValues_.setSize(nTimeSteps_);
for (label i = 0; i < nTimeSteps_; i++)
{
......@@ -161,6 +210,7 @@ void Foam::ensightSurfaceReader::readCase(IFstream& is)
Foam::ensightSurfaceReader::ensightSurfaceReader(const fileName& fName)
:
surfaceReader(fName),
streamFormat_(IOstream::ASCII),
baseDir_(fName.path()),
meshFileName_(),
fieldNames_(),
......@@ -193,31 +243,71 @@ const Foam::meshedSurface& Foam::ensightSurfaceReader::geometry()
if (!surfPtr_.valid())
{
IFstream is(baseDir_/meshFileName_);
IFstream isBinary(baseDir_/meshFileName_, IOstream::BINARY);
if (!is.good())
if (!isBinary.good())
{
FatalErrorInFunction
<< "Cannot read file " << is.name()
<< "Cannot read file " << isBinary.name()
<< exit(FatalError);
}
token t;
while (is.good())
streamFormat_ = IOstream::BINARY;
{
is >> t;
istream& is = isBinary.stdStream();
char buffer[80];
is.read(buffer, 80);
if (t.isWord())
char test[80];
label nChar = 0;
for (label i = 0; i < 80; ++i)
{
word wordToken = t.wordToken();
if (wordToken == "coordinates")
if (buffer[i] == '\0')
{
break;
}
test[i] = buffer[i];
nChar++;
}
string testStr(test, nChar);
if
(
(testStr.find("binary", 0) == string::npos)
&& (testStr.find("Binary", 0) == string::npos)
)
{
streamFormat_ = IOstream::ASCII;
}
}
label nPoints(readLabel(is));
if (debug)
{
Info<< "stream format: ";
if (streamFormat_ == IOstream::ASCII)
{
Info<< "ascii" << endl;
}
else
{
Info<< "binary" << endl;
}
}
ensightReadFile is(baseDir_/meshFileName_, streamFormat_);
if (debug)
{
Info<< "File: " << is.name() << endl;
}
readGeometryHeader(is);
label nPoints;
is.read(nPoints);
if (debug)
{
......@@ -231,7 +321,7 @@ const Foam::meshedSurface& Foam::ensightSurfaceReader::geometry()
{
forAll(points, pointI)
{
x[pointI] = readScalar(is);
is.read(x[pointI]);
}
points.replace(dir, x);
......@@ -241,39 +331,34 @@ const Foam::meshedSurface& Foam::ensightSurfaceReader::geometry()
// Read faces - may be a mix of tris, quads and polys
DynamicList<face> faces(ceil(nPoints/3));
while (is.good())
DynamicList<Tuple2<string, label> > schema(faces.size());
string faceType = "";
label nFace = 0;
while (is.good()) // (is.peek() != EOF)
{
token t(is);
is.read(faceType);
if (is.eof())
if (!is.good())
{
break;
}
word faceType(t.wordToken());
if (debug)
{
Info<< "faceType: " << faceType << endl;
}
label nFace(readLabel(is));
if (debug)
{
Info<< "nFace: " << nFace << endl;
}
if (faceType == "tria3")
{
is.read(nFace);
label np = 3;
for (label faceI = 0; faceI < nFace; faceI++)
for (label faceI = 0; faceI < nFace; ++faceI)
{
face f(np);
for (label fpI = 0; fpI < np; fpI++)
{
f[fpI] = readLabel(is);
is.read(f[fpI]);
}
faces.append(f);
......@@ -281,13 +366,15 @@ const Foam::meshedSurface& Foam::ensightSurfaceReader::geometry()
}
else if (faceType == "quad4")
{
is.read(nFace);
label np = 4;
for (label faceI = 0; faceI < nFace; faceI++)
for (label faceI = 0; faceI < nFace; ++faceI)
{
face f(np);
for (label fpI = 0; fpI < np; fpI++)
{
f[fpI] = readLabel(is);
is.read(f[fpI]);
}
faces.append(f);
......@@ -295,36 +382,48 @@ const Foam::meshedSurface& Foam::ensightSurfaceReader::geometry()
}
else if (faceType == "nsided")
{
is.read(nFace);
labelList np(nFace);
for (label faceI = 0; faceI < nFace; faceI++)
for (label faceI = 0; faceI < nFace; ++faceI)
{
np[faceI] = readLabel(is);
is.read(np[faceI]);
}
for (label faceI = 0; faceI < nFace; faceI++)
for (label faceI = 0; faceI < nFace; ++faceI)
{
face f(np[faceI]);
for (label fpI = 0; fpI < f.size(); fpI++)
for (label fpI = 0; fpI < f.size(); ++fpI)
{
f[fpI] = readLabel(is);
is.read(f[fpI]);
}
faces.append(f);
}
}
else if (faceType != "")
else
{
WarningInFunction
<< "Unknown face type: " << faceType
<< ". Aborting read and continuing with current elements "
<< "only" << endl;
if (debug)
{
WarningInFunction
<< "Unknown face type: " << faceType
<< ". Aborting read and continuing with current "
<< "elements only" << endl;
}
break;
}
schema.append(Tuple2<string, label>(faceType, nFace));
}
schema_.transfer(schema);
if (debug)
{
Info<< "read nFaces: " << faces.size() << endl;
Info<< "read nFaces: " << faces.size() << nl
<< "file schema: " << schema_ << endl;
}
// Convert from 1-based Ensight addressing to 0-based OF addressing
forAll(faces, faceI)
{
face& f = faces[faceI];
......