diff --git a/src/fileFormats/ensight/file/ensightFile.C b/src/fileFormats/ensight/file/ensightFile.C index dca789e4c15d0f4a59757cb466a1b0ffbd90fec7..6ee8f464baf4340391d4c300e13fe03274cafb91 100644 --- a/src/fileFormats/ensight/file/ensightFile.C +++ b/src/fileFormats/ensight/file/ensightFile.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2016-2023 OpenCFD Ltd. + Copyright (C) 2016-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -221,7 +221,7 @@ Foam::Ostream& Foam::ensightFile::write } -Foam::Ostream& Foam::ensightFile::write(const int32_t val) +void Foam::ensightFile::writeInt(const int32_t val, const int fieldWidth) { if (format() == IOstreamOption::BINARY) { @@ -233,24 +233,22 @@ Foam::Ostream& Foam::ensightFile::write(const int32_t val) } else { - stdStream().width(10); + stdStream().width(fieldWidth); stdStream() << val; syncState(); } - - return *this; } -Foam::Ostream& Foam::ensightFile::write(const int64_t val) +void Foam::ensightFile::writeInt(const int64_t val, const int fieldWidth) { - int32_t ivalue(narrowInt32(val)); + int32_t work(narrowInt32(val)); - return write(ivalue); + writeInt(work, fieldWidth); } -Foam::Ostream& Foam::ensightFile::write(const float val) +void Foam::ensightFile::writeFloat(const float val, const int fieldWidth) { if (format() == IOstreamOption::BINARY) { @@ -262,40 +260,45 @@ Foam::Ostream& Foam::ensightFile::write(const float val) } else { - stdStream().width(12); + stdStream().width(fieldWidth); stdStream() << val; syncState(); } +} - return *this; + +void Foam::ensightFile::writeFloat(const double val, const int fieldWidth) +{ + float work(narrowFloat(val)); + + writeFloat(work, fieldWidth); } -Foam::Ostream& Foam::ensightFile::write(const double val) +Foam::Ostream& Foam::ensightFile::write(const int32_t val) { - float fvalue(narrowFloat(val)); + writeInt(val, 10); + return *this; +} + - return write(fvalue); +Foam::Ostream& Foam::ensightFile::write(const int64_t val) +{ + writeInt(val, 10); + return *this; } -Foam::Ostream& Foam::ensightFile::write -( - const label value, - const label fieldWidth -) +Foam::Ostream& Foam::ensightFile::write(const float val) { - if (format() == IOstreamOption::BINARY) - { - write(value); - } - else - { - stdStream().width(fieldWidth); - stdStream() << value; - syncState(); - } + writeFloat(val, 12); + return *this; +} + +Foam::Ostream& Foam::ensightFile::write(const double val) +{ + writeFloat(val, 12); return *this; } @@ -376,7 +379,7 @@ void Foam::ensightFile::beginParticleCoordinates(const label nparticles) { writeString("particle coordinates"); newline(); - write(nparticles, 8); // unusual width + writeInt(nparticles, 8); // Warning: unusual width newline(); } diff --git a/src/fileFormats/ensight/file/ensightFile.H b/src/fileFormats/ensight/file/ensightFile.H index 2e16f0e95d291c80f0214a7276060d3adb0f7b9e..79ed2f0a7b5b1a4f113832287c247b1456437e9b 100644 --- a/src/fileFormats/ensight/file/ensightFile.H +++ b/src/fileFormats/ensight/file/ensightFile.H @@ -163,6 +163,18 @@ public: //- Write string as "%79s" or as binary (max 80 chars) void writeString(const std::string& str); + //- Write integer value with specified width or as binary + void writeInt(const int32_t val, const int fieldWidth); + + //- Write (narrowed) integer value with specified width or as binary + void writeInt(const int64_t val, const int fieldWidth); + + //- Write floating-point with specified width or as binary + void writeFloat(const float val, const int fieldWidth); + + //- Write (narrowed) floating-point with specified width or as binary + void writeFloat(const double val, const int fieldWidth); + //- Write undef value void writeUndef(); @@ -197,15 +209,12 @@ public: //- Write string, uses writeString() virtual Ostream& write(const std::string& str) override; - //- Write integer as "%10d" or as binary + //- Write integer value as "%10d" or as binary virtual Ostream& write(const int32_t val) override; - //- Write integer as "%10d" or as binary + //- Write integer value as "%10d" or as binary (narrowed to int32_t) virtual Ostream& write(const int64_t val) override; - //- Write integer with specified width or as binary - Ostream& write(const label value, const label fieldWidth); - //- Write floating-point as "%12.5e" or as binary virtual Ostream& write(const float val) override; diff --git a/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C b/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C index d81b259444bb9c813aa19fb64312f365acb6a765..747bb0abebf7ccf0091c3f42726467e6f2c3aa4d 100644 --- a/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C +++ b/src/lagrangian/intermediate/conversion/ensight/ensightOutputCloud.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016-2022 OpenCFD Ltd. + Copyright (C) 2016-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -61,7 +61,7 @@ static inline label writeMeasured_ascii { for (const floatVector& p : points) { - os.write(++pointId, 8); // 1-index and an unusual width + os.writeInt(++pointId, 8); // 1-index and an unusual width os.write(p.x()); os.write(p.y()); os.write(p.z());