From 71812c21c57fba3fa75901a999fe4395aca27b54 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 8 Jan 2021 13:46:17 +0100 Subject: [PATCH] ENH: add 'arch' information to output file headers (ASCII) - this was previously suppressed for ASCII format as being 'clutter', but without it there is no context for interpreting the type of data contained in ASCII files: potentially leading to integer overflows when reading in ParaView etc. --- src/OpenFOAM/db/IOobject/IOobject.H | 21 ++++--- .../db/IOobject/IOobjectWriteHeader.C | 54 ++++++++++++------ .../decomposedBlockData/decomposedBlockData.C | 55 ++++++++++--------- .../decomposedBlockData/decomposedBlockData.H | 6 +- .../collatedFileOperation/OFstreamCollator.C | 11 ++-- 5 files changed, 88 insertions(+), 59 deletions(-) diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H index a4ed735066..f5c4c93ddd 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.H +++ b/src/OpenFOAM/db/IOobject/IOobject.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016-2020 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -511,8 +511,8 @@ public: // Writing //- Write the standard OpenFOAM file/dictionary banner - // Optionally without -*- C++ -*- editor hint (eg, for logs) - static Ostream& writeBanner(Ostream& os, bool noHint=false); + // Optionally without editor syntax hint (eg, for logs) + static Ostream& writeBanner(Ostream& os, const bool noSyntaxHint=false); //- Write the standard file section divider static Ostream& writeDivider(Ostream& os); @@ -520,11 +520,18 @@ public: //- Write the standard end file divider static Ostream& writeEndDivider(Ostream& os); - //- Write header - bool writeHeader(Ostream& os) const; + //- Write header with current type(). + // Optionally without arch information when ASCII + bool writeHeader(Ostream& os, const bool noArchAscii=false) const; - //- Write header. Allow override of type - bool writeHeader(Ostream& os, const word& objectType) const; + //- Write header with override of type. + // Optionally without arch information when ASCII + bool writeHeader + ( + Ostream& os, + const word& objectType, + const bool noArchAscii = false + ) const; // Error Handling diff --git a/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C b/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C index b0ce0aa935..9f596f4148 100644 --- a/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C +++ b/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016-2017 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -42,7 +42,8 @@ License | \\/ M anipulation | | \*---------------------------------------------------------------------------*/ -Foam::Ostream& Foam::IOobject::writeBanner(Ostream& os, bool noHint) +Foam::Ostream& +Foam::IOobject::writeBanner(Ostream& os, const bool noSyntaxHint) { // The version padded with spaces to fit after "Version: " // - initialized with zero-length string to detect if it has been populated @@ -66,7 +67,7 @@ Foam::Ostream& Foam::IOobject::writeBanner(Ostream& os, bool noHint) os << "/*--------------------------------"; - if (noHint) + if (noSyntaxHint) { // Without syntax hint os << "---------"; @@ -116,7 +117,12 @@ Foam::Ostream& Foam::IOobject::writeEndDivider(Ostream& os) } -bool Foam::IOobject::writeHeader(Ostream& os, const word& type) const +bool Foam::IOobject::writeHeader +( + Ostream& os, + const word& objectType, + const bool noArchAscii +) const { if (!os.good()) { @@ -127,25 +133,37 @@ bool Foam::IOobject::writeHeader(Ostream& os, const word& type) const return false; } - writeBanner(os) - << "FoamFile\n{\n" - << " version " << os.version() << ";\n" - << " format " << os.format() << ";\n" - << " class " << type << ";\n"; + IOobject::writeBanner(os) + << "FoamFile" << nl + << '{' << nl + << " version " << os.version() << ';' << nl + << " format " << os.format() << ';' << nl; - if (os.format() == IOstream::BINARY) + if (os.format() == IOstream::BINARY || !noArchAscii) { - os << " arch " << foamVersion::buildArch << ";\n"; + // Arch information (BINARY: always, ASCII: can disable) + os << " arch " << foamVersion::buildArch << ';' << nl; } - if (!note().empty()) { - os << " note " << note() << ";\n"; + os << " note " << note() << ';' << nl; } - os << " location " << instance()/db().dbDir()/local() << ";\n" - << " object " << name() << ";\n" - << "}" << nl; + os << " class "; + if (objectType.empty()) + { + // Empty type not allowed - use 'dictionary' fallback + os << "dictionary"; + } + else + { + os << objectType; + } + os << ';' << nl; + + os << " location " << instance()/db().dbDir()/local() << ';' << nl + << " object " << name() << ';' << nl + << '}' << nl; writeDivider(os) << nl; @@ -153,9 +171,9 @@ bool Foam::IOobject::writeHeader(Ostream& os, const word& type) const } -bool Foam::IOobject::writeHeader(Ostream& os) const +bool Foam::IOobject::writeHeader(Ostream& os, const bool noArchAscii) const { - return writeHeader(os, type()); + return writeHeader(os, type(), noArchAscii); } diff --git a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C index 64b271ab78..d95db4561c 100644 --- a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C +++ b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017-2018 OpenFOAM Foundation - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -188,41 +188,47 @@ void Foam::decomposedBlockData::writeHeader Ostream& os, const IOstream::versionNumber version, const IOstream::streamFormat format, - const word& type, + const word& objectType, const string& note, const fileName& location, - const word& name + const word& objectName ) { IOobject::writeBanner(os) - << "FoamFile\n{\n" - << " version " << version << ";\n" - << " format " << format << ";\n" - << " class " << type << ";\n"; - - // This may be useful to have as well - if (os.format() == IOstream::BINARY) - { - os << " arch " << foamVersion::buildArch << ";\n"; - } + << "FoamFile" << nl + << '{' << nl + << " version " << version << ';' << nl + << " format " << format << ';' << nl + << " arch " << foamVersion::buildArch << ';' << nl; if (Pstream::parRun()) { - os << " blocks " << Pstream::nProcs() << ";\n"; + os << " blocks " << Pstream::nProcs() << ';' << nl; } - - if (note.size()) + if (!note.empty()) { - os << " note " << note << ";\n"; + os << " note " << note << ';' << nl; } - if (location.size()) + os << " class "; + if (objectType.empty()) { - os << " location " << location << ";\n"; + // Empty type not allowed - use 'dictionary' fallback + os << "dictionary"; + } + else + { + os << objectType; + } + os << ';' << nl; + + if (!location.empty()) + { + os << " location " << location << ';' << nl; } - os << " object " << name << ";\n" - << "}" << nl; + os << " object " << objectName << ';' << nl + << '}' << nl; IOobject::writeDivider(os) << nl; } @@ -976,9 +982,8 @@ bool Foam::decomposedBlockData::writeData(Ostream& os) const // Scatter header information string versionString(os.version().str()); - Pstream::scatter(versionString, Pstream::msgType(), comm_); - label formatValue(os.format()); + Pstream::scatter(versionString, Pstream::msgType(), comm_); Pstream::scatter(formatValue, Pstream::msgType(), comm_); //word masterName(name()); @@ -997,8 +1002,8 @@ bool Foam::decomposedBlockData::writeData(Ostream& os) const writeHeader ( os, - IOstream::versionNumber(versionString), - IOstream::streamFormat(formatValue), + IOstreamOption::versionNumber(versionString), + IOstreamOption::streamFormat(formatValue), io.headerClassName(), io.note(), masterLocation, diff --git a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.H b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.H index 8ce5aeccba..8e790bbc6b 100644 --- a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.H +++ b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017-2018 OpenFOAM Foundation - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -156,10 +156,10 @@ public: Ostream& os, const IOstream::versionNumber version, const IOstream::streamFormat format, - const word& type, + const word& objectType, const string& note, const fileName& location, - const word& name + const word& objectName ); //- Read selected block (non-seeking) + header information diff --git a/src/OpenFOAM/global/fileOperations/collatedFileOperation/OFstreamCollator.C b/src/OpenFOAM/global/fileOperations/collatedFileOperation/OFstreamCollator.C index 8fa91a0baa..fc3480bd80 100644 --- a/src/OpenFOAM/global/fileOperations/collatedFileOperation/OFstreamCollator.C +++ b/src/OpenFOAM/global/fileOperations/collatedFileOperation/OFstreamCollator.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2017-2018 OpenFOAM Foundation - Copyright (C) 2019-2020 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -94,16 +94,15 @@ bool Foam::OFstreamCollator::writeFile // We don't have IOobject so cannot use IOobject::writeHeader if (!append) { - OSstream& os = osPtr(); decomposedBlockData::writeHeader ( - os, + *osPtr, ver, fmt, typeName, - "", - fName, - fName.name() + "", // note + fName, // location + fName.name() // object name ); } }