From 8e3dd0942556a21d8710b50ef4ffe3abe4301a31 Mon Sep 17 00:00:00 2001 From: Andrew Heather Date: Thu, 18 Aug 2016 15:29:56 +0100 Subject: [PATCH] BUG: ensightSurfaceReader - enabled reading of data from sub-directory and updated field mask (See #215) --- .../readers/ensight/ensightSurfaceReader.C | 112 ++++++++++++------ .../readers/ensight/ensightSurfaceReader.H | 33 ++++-- .../ensight/ensightSurfaceReaderTemplates.C | 32 ++++- 3 files changed, 128 insertions(+), 49 deletions(-) diff --git a/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReader.C b/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReader.C index 3e1b422bde..f90d05b85d 100644 --- a/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReader.C +++ b/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReader.C @@ -34,7 +34,7 @@ namespace Foam } -void Foam::ensightSurfaceReader::skip(const label n, IFstream& is) const +void Foam::ensightSurfaceReader::skip(const label n, Istream& is) const { label i = 0; token t; @@ -59,6 +59,40 @@ void Foam::ensightSurfaceReader::skip(const label n, IFstream& is) const } +void Foam::ensightSurfaceReader::readLine(IFstream& is, string& buffer) const +{ + buffer = ""; + while (is.good() && buffer == "") + { + is.getLine(buffer); + } +} + + +void Foam::ensightSurfaceReader::debugSection +( + const word& expected, + IFstream& is +) const +{ + string actual = ""; + readLine(is, actual); + + if (expected != actual) + { + FatalIOErrorInFunction(is) + << "Expected section header '" << expected + << "' but read the word '" << actual << "'" + << exit(FatalIOError); + } + + if (debug) + { + Info<< "Read section header: " << expected << endl; + } +} + + void Foam::ensightSurfaceReader::readGeometryHeader(ensightReadFile& is) const { // Binary flag string if applicable @@ -101,29 +135,6 @@ void Foam::ensightSurfaceReader::readGeometryHeader(ensightReadFile& is) const } -void Foam::ensightSurfaceReader::debugSection -( - const word& expected, - IFstream& is -) const -{ - word actual(is); - - if (expected != actual) - { - FatalIOErrorInFunction(is) - << "Expected section header '" << expected - << "' but read the word '" << actual << "'" - << exit(FatalIOError); - } - - if (debug) - { - Info<< "Read section header: " << expected << endl; - } -} - - void Foam::ensightSurfaceReader::readCase(IFstream& is) { if (debug) @@ -141,31 +152,54 @@ void Foam::ensightSurfaceReader::readCase(IFstream& is) string buffer; // Read the file + debugSection("FORMAT", is); - skip(3, is); // type: ensight gold + readLine(is, buffer); // type: ensight gold debugSection("GEOMETRY", is); - readSkip(is, 2, meshFileName_); + readLine(is, buffer); + readFromLine(2, buffer, meshFileName_); // model: 1 xxx.0000.mesh debugSection("VARIABLE", is); + // Read the field description DynamicList fieldNames(10); - DynamicList fieldFileNames(10); + DynamicList fieldFileNames(10); word fieldName; - word fieldFileName; + string fieldFileName; while (is.good()) { - word primitiveType(is); // scalar, vector + readLine(is, buffer); - if (primitiveType == "TIME") + if (buffer == "TIME") { break; } - readSkip(is, 3, fieldName); // p, U etc + IStringStream iss(buffer); + + // Read the field name, e.g. p U etc + readFromLine(4, iss, fieldName); fieldNames.append(fieldName); - is >> fieldFileName; // surfaceName.****.fieldName + // Field file name may contain /'s e.g. + // surfaceName.****.fieldName + // This is not parser friendly - simply take remainder of buffer + label iPos = iss.stdStream().tellg(); + fieldFileName = buffer(iPos, buffer.size() - iPos); + size_t p0 = fieldFileName.find_first_not_of(' '); + if (p0 == string::npos) + { + WarningInFunction + << "Error reading field file name. " + << "Current buffer: " << buffer + << endl; + } + else + { + size_t p1 = fieldFileName.find_last_not_of(' '); + fieldFileName = fieldFileName.substr(p0, p1 - p0 + 1); + } fieldFileNames.append(fieldFileName); } fieldNames_.transfer(fieldNames); @@ -178,10 +212,14 @@ void Foam::ensightSurfaceReader::readCase(IFstream& is) } // Start reading time information - skip(3, is); // time set: 1 - readSkip(is, 3, nTimeSteps_); - readSkip(is, 3, timeStartIndex_); - readSkip(is, 2, timeIncrement_); + readLine(is, buffer); // time set: 1 + + readLine(is, buffer); + readFromLine(3, buffer, nTimeSteps_); + readLine(is, buffer); + readFromLine(3, buffer, timeStartIndex_); + readLine(is, buffer); + readFromLine(2, buffer, timeIncrement_); if (debug) { @@ -191,7 +229,7 @@ void Foam::ensightSurfaceReader::readCase(IFstream& is) } // Read the time values - skip(2, is); + readLine(is, buffer); // time values: timeValues_.setSize(nTimeSteps_); for (label i = 0; i < nTimeSteps_; i++) { diff --git a/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReader.H b/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReader.H index 8c99cfd466..0ba7e1c49b 100644 --- a/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReader.H +++ b/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReader.H @@ -69,7 +69,7 @@ protected: List fieldNames_; //- Field file names - List fieldFileNames_; + List fieldFileNames_; //- Number of time steps label nTimeSteps_; @@ -91,21 +91,38 @@ protected: // Protected Member Functions + //- Helper function to skip forward n steps in stream + void skip(const label n, Istream& is) const; + + //- Helper function to read an ascii line from file + void readLine(IFstream& is, string& buffer) const; + //- Read and check a section header void debugSection(const word& expected, IFstream& is) const; - //- Read the case file - void readCase(IFstream& is); - - //- Helper function to skip forward n steps in stream - void skip(const label n, IFstream& is) const; - //- Read (and throw away) geometry file header void readGeometryHeader(ensightReadFile& is) const; + //- Read the case file + void readCase(IFstream& is); + //- Helper function to return Type after skipping n tokens template - void readSkip(IFstream& is, const label nSkip, Type& value) const; + void readFromLine + ( + const label nSkip, + IStringStream& is, + Type& value + ) const; + + //- Helper function to return Type after skipping n tokens + template + void readFromLine + ( + const label nSkip, + const string& buffer, + Type& value + ) const; //- Helper function to return a field template diff --git a/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReaderTemplates.C b/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReaderTemplates.C index 04a593575a..dace070320 100644 --- a/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReaderTemplates.C +++ b/src/sampling/sampledSurface/readers/ensight/ensightSurfaceReaderTemplates.C @@ -29,10 +29,10 @@ License // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // template -void Foam::ensightSurfaceReader::readSkip +void Foam::ensightSurfaceReader::readFromLine ( - IFstream& is, const label nSkip, + IStringStream& is, Type& value ) const { @@ -42,6 +42,20 @@ void Foam::ensightSurfaceReader::readSkip } +template +void Foam::ensightSurfaceReader::readFromLine +( + const label nSkip, + const string& buffer, + Type& value +) const +{ + IStringStream is(buffer); + + readFromLine(nSkip, is, value); +} + + template Foam::tmp > Foam::ensightSurfaceReader::readField ( @@ -60,9 +74,19 @@ Foam::tmp > Foam::ensightSurfaceReader::readField fileName fieldFileName(fieldFileNames_[fieldIndex]); std::ostringstream oss; - oss << std::setfill('0') << std::setw(4) << fileIndex; + label nMask = 0; + for (size_t chari = 0; chari < fieldFileName.size(); chari++) + { + if (fieldFileName[chari] == '*') + { + nMask++; + } + } + + const std::string maskStr(nMask, '*'); + oss << std::setfill('0') << std::setw(nMask) << fileIndex; const word indexStr = oss.str(); - fieldFileName.replace("****", indexStr); + fieldFileName.replace(maskStr, indexStr); ensightReadFile is(baseDir_/fieldFileName, streamFormat_);