diff --git a/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriter.C b/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriter.C index adfe60f566..384192756d 100644 --- a/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriter.C +++ b/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriter.C @@ -25,9 +25,9 @@ License #include "nastranSurfaceWriter.H" #include "IOmanip.H" -#include "Tuple2.H" -#include "makeSurfaceWriterMethods.H" +#include "Pair.H" #include "HashSet.H" +#include "makeSurfaceWriterMethods.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -65,7 +65,7 @@ namespace Foam // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -void Foam::nastranSurfaceWriter::formatOS(OFstream& os) const +void Foam::nastranSurfaceWriter::formatOS(Ostream& os) const { os.setf(ios_base::scientific); @@ -76,19 +76,23 @@ void Foam::nastranSurfaceWriter::formatOS(OFstream& os) const label offset = 7; switch (writeFormat_) { - case (wfShort): + case wfShort: { prec = 8 - offset; break; } - case (wfFree): - case (wfLong): + + case wfFree: + case wfLong: { prec = 16 - offset; break; } + default: { + FatalErrorInFunction + << "Unknown writeFormat enumeration" << abort(FatalError); } } @@ -96,10 +100,10 @@ void Foam::nastranSurfaceWriter::formatOS(OFstream& os) const } -void Foam::nastranSurfaceWriter::writeKeyword +Foam::Ostream& Foam::nastranSurfaceWriter::writeKeyword ( - const word& keyword, - Ostream& os + Ostream& os, + const word& keyword ) const { os.setf(ios_base::left); @@ -111,11 +115,13 @@ void Foam::nastranSurfaceWriter::writeKeyword os << setw(8) << keyword; break; } + case wfLong: { os << setw(8) << word(keyword + '*'); break; } + case wfFree: { os << keyword; @@ -124,14 +130,16 @@ void Foam::nastranSurfaceWriter::writeKeyword } os.unsetf(ios_base::left); + + return os; } void Foam::nastranSurfaceWriter::writeCoord ( + Ostream& os, const point& p, - const label pointi, - OFstream& os + const label pointI ) const { // Fixed short/long formats: @@ -145,21 +153,14 @@ void Foam::nastranSurfaceWriter::writeCoord // 8 PS : single point constraints (blank) // 9 SEID : super-element ID - - writeKeyword("GRID", os); - - os << separator_; + writeKeyword(os, "GRID") << separator_; os.setf(ios_base::right); - writeValue(pointi + 1, os); - os << separator_; - writeValue("", os); - os << separator_; - writeValue(p.x(), os); - os << separator_; - writeValue(p.y(), os); - os << separator_; + writeValue(os, pointI+1) << separator_; + writeValue(os, "") << separator_; + writeValue(os, p.x()) << separator_; + writeValue(os, p.y()) << separator_; switch (writeFormat_) { @@ -168,27 +169,26 @@ void Foam::nastranSurfaceWriter::writeCoord os << setw(8) << p.z() << nl; os.unsetf(ios_base::right); - break; } + case wfLong: { os << nl; os.unsetf(ios_base::right); - writeKeyword("", os); + writeKeyword(os, ""); os.setf(ios_base::right); - writeValue(p.z(), os); - os << nl; + writeValue(os, p.z()) << nl; break; } + case wfFree: { - writeValue(p.z(), os); - os << nl; - + writeValue(os, p.z()) << nl; break; } + default: { FatalErrorInFunction @@ -202,11 +202,11 @@ void Foam::nastranSurfaceWriter::writeCoord void Foam::nastranSurfaceWriter::writeFace ( + Ostream& os, const word& faceType, - const labelList& facePts, - label& nFace, - const label PID, - OFstream& os + const labelUList& facePts, + const label nFace, + const label PID ) const { // Only valid surface elements are CTRIA3 and CQUAD4 @@ -223,15 +223,12 @@ void Foam::nastranSurfaceWriter::writeFace // For CTRIA3 elements, cols 7 onwards are not used - writeKeyword(faceType, os); - os << separator_; + writeKeyword(os, faceType) << separator_; os.setf(ios_base::right); - writeValue(nFace++, os); - os << separator_; - - writeValue(PID, os); + writeValue(os, nFace) << separator_; + writeValue(os, PID); switch (writeFormat_) { @@ -239,7 +236,7 @@ void Foam::nastranSurfaceWriter::writeFace { forAll(facePts, i) { - writeValue(facePts[i] + 1, os); + writeValue(os, facePts[i] + 1); } break; @@ -248,12 +245,12 @@ void Foam::nastranSurfaceWriter::writeFace { forAll(facePts, i) { - writeValue(facePts[i] + 1, os); + writeValue(os, facePts[i] + 1); if (i == 1) { os << nl; os.unsetf(ios_base::right); - writeKeyword("", os); + writeKeyword(os, ""); os.setf(ios_base::right); } } @@ -265,7 +262,7 @@ void Foam::nastranSurfaceWriter::writeFace forAll(facePts, i) { os << separator_; - writeValue(facePts[i] + 1, os); + writeValue(os, facePts[i] + 1); } break; @@ -284,9 +281,9 @@ void Foam::nastranSurfaceWriter::writeFace void Foam::nastranSurfaceWriter::writeGeometry ( + Ostream& os, const meshedSurf& surf, - List>& decomposedFaces, - OFstream& os + List>& decomposedFaces ) const { const pointField& points = surf.points(); @@ -301,7 +298,7 @@ void Foam::nastranSurfaceWriter::writeGeometry forAll(points, pointi) { - writeCoord(points[pointi], pointi, os); + writeCoord(os, points[pointi], pointi); } // Write faces @@ -312,8 +309,7 @@ void Foam::nastranSurfaceWriter::writeGeometry << "$ Faces" << nl << "$" << nl; - label nFace = 1; // the element-id - + label nFace = 0; // the element-id forAll(faces, facei) { const face& f = faces[facei]; @@ -322,12 +318,12 @@ void Foam::nastranSurfaceWriter::writeGeometry if (f.size() == 3) { - writeFace("CTRIA3", f, nFace, PID, os); + writeFace(os, "CTRIA3", f, ++nFace, PID); decomposedFaces[facei].append(f); } else if (f.size() == 4) { - writeFace("CQUAD4", f, nFace, PID, os); + writeFace(os, "CQUAD4", f, ++nFace, PID); decomposedFaces[facei].append(f); } else @@ -339,7 +335,7 @@ void Foam::nastranSurfaceWriter::writeGeometry forAll(triFaces, trii) { - writeFace("CTRIA3", triFaces[trii], nFace, PID, os); + writeFace(os, "CTRIA3", triFaces[trii], ++nFace, PID); decomposedFaces[facei].append(triFaces[trii]); } } @@ -347,7 +343,7 @@ void Foam::nastranSurfaceWriter::writeGeometry } -void Foam::nastranSurfaceWriter::writeFooter +Foam::Ostream& Foam::nastranSurfaceWriter::writeFooter ( Ostream& os, const meshedSurf& surf @@ -363,15 +359,14 @@ void Foam::nastranSurfaceWriter::writeFooter for (auto pid : pidsUsed) { - writeKeyword("PSHELL", os); - os << separator_; - writeValue(pid+1, os); // 1-offset for PID + writeKeyword(os, "PSHELL") << separator_; + writeValue(os, pid+1); // 1-offset for PID - for (label i = 0; i < 7; i++) + for (label i = 0; i < 7; ++i) { // Dummy values os << separator_; - writeValue(1, os); + writeValue(os, 1); } os << nl; } @@ -381,17 +376,18 @@ void Foam::nastranSurfaceWriter::writeFooter label MID = 1; - writeKeyword("MAT1", os); - os << separator_; - writeValue(MID, os); + writeKeyword(os, "MAT1") << separator_; + writeValue(os, MID); - for (label i = 0; i < 7; i++) + for (label i = 0; i < 7; ++i) { // Dummy values os << separator_; - writeValue("", os); + writeValue(os, ""); } os << nl; + + return os; } @@ -416,7 +412,10 @@ Foam::nastranSurfaceWriter::nastranSurfaceWriter(const dictionary& options) { if (options.found("format")) { - writeFormat_ = writeFormatNames_.read(options.lookup("format")); + writeFormat_ = writeFormatNames_.read + ( + options.lookup("format") + ); } if (writeFormat_ == wfFree) @@ -424,7 +423,7 @@ Foam::nastranSurfaceWriter::nastranSurfaceWriter(const dictionary& options) separator_ = ","; } - List> fieldSet(options.lookup("fields")); + List> fieldSet(options.lookup("fields")); forAll(fieldSet, i) { @@ -464,21 +463,16 @@ Foam::fileName Foam::nastranSurfaceWriter::write Info<< "Writing nastran file to " << os.name() << endl; } - os << "TITLE=OpenFOAM " << surfaceName.c_str() << " mesh" << nl + os << "TITLE=OpenFOAM " << surfaceName.c_str() + << " mesh" << nl << "$" << nl << "BEGIN BULK" << nl; List> decomposedFaces; - writeGeometry(surf, decomposedFaces, os); + writeGeometry(os, surf, decomposedFaces); - if (!isDir(outputDir)) - { - mkDir(outputDir); - } - - writeFooter(os, surf); - - os << "ENDDATA" << endl; + writeFooter(os, surf) + << "ENDDATA" << endl; return os.name(); } diff --git a/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriter.H b/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriter.H index cfee9c3952..921182af98 100644 --- a/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriter.H +++ b/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriter.H @@ -94,13 +94,13 @@ private: // Private data static const NamedEnum writeFormatNames_; - static const NamedEnum dataFormatNames_; + static const NamedEnum dataFormatNames_; //- Write option writeFormat writeFormat_; //- Mapping from field name to data format enumeration - HashTable fieldMap_; + HashTable fieldMap_; //- Scale to apply to values (default = 1.0) scalar scale_; @@ -111,63 +111,60 @@ private: // Private Member Functions - //- Initialise the output stream format params - void formatOS(OFstream& os) const; + //- Initialise the output stream format parameters + void formatOS(Ostream& os) const; - //- Write a co-ordinate + //- Write a coordinate void writeCoord ( + Ostream& os, const point& p, - const label pointi, - OFstream& os + const label pointI //!< 0-based Point Id ) const; //- Write a face element (CTRIA3 or CQUAD4) void writeFace ( + Ostream& os, const word& faceType, - const labelList& facePts, - label& nFace, - const label PID, - OFstream& os + const labelUList& facePts, + const label EID, //!< 1-based Element Id + const label PID //!< 1-based Property Id ) const; //- Main driver to write the surface mesh geometry void writeGeometry ( + Ostream& os, const meshedSurf& surf, - List>& decomposedFaces, - OFstream& os + List>& decomposedFaces ) const; //- Write the formatted keyword to the output stream - void writeKeyword + Ostream& writeKeyword ( - const word& keyword, - Ostream& os + Ostream& os, + const word& keyword ) const; //- Write the footer information - void writeFooter(Ostream& os, const meshedSurf& surf) const; + Ostream& writeFooter(Ostream& os, const meshedSurf& surf) const; //- Write a formatted value to the output stream template - void writeValue - ( - const Type& value, - Ostream& os - ) const; + Ostream& writeValue(Ostream& os, const Type& value) const; //- Write a face-based value template - void writeFaceValue + Ostream& writeFaceValue ( + Ostream& os, const dataFormat& format, const Type& value, - const label EID, - Ostream& os + const label EID //!< 1-based Element Id ) const; + //- Templated write operation template fileName writeTemplate diff --git a/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriterTemplates.C b/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriterTemplates.C index 4ee46f5eb0..048088791c 100644 --- a/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriterTemplates.C +++ b/src/sampling/sampledSurface/writers/nastran/nastranSurfaceWriterTemplates.C @@ -30,10 +30,10 @@ License // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // template -void Foam::nastranSurfaceWriter::writeValue +Foam::Ostream& Foam::nastranSurfaceWriter::writeValue ( - const Type& value, - Ostream& os + Ostream& os, + const Type& value ) const { switch (writeFormat_) @@ -54,16 +54,18 @@ void Foam::nastranSurfaceWriter::writeValue break; } } + + return os; } template -void Foam::nastranSurfaceWriter::writeFaceValue +Foam::Ostream& Foam::nastranSurfaceWriter::writeFaceValue ( + Ostream& os, const dataFormat& format, const Type& value, - const label EID, - Ostream& os + const label EID ) const { // Fixed short/long formats supporting PLOAD2 and PLOAD4: @@ -84,16 +86,13 @@ void Foam::nastranSurfaceWriter::writeFaceValue Type scaledValue = scale_*value; - // Write Keyword - writeKeyword(dataFormatNames_[format], os); - - os << separator_; + // Write keyword + writeKeyword(os, dataFormatNames_[format]) << separator_; // Write load set ID os.setf(ios_base::right); - writeValue(SID, os); - os << separator_; + writeValue(os, SID) << separator_; switch (format) { @@ -101,7 +100,7 @@ void Foam::nastranSurfaceWriter::writeFaceValue { if (pTraits::nComponents == 1) { - writeValue(scaledValue, os); + writeValue(os, scaledValue) << separator_; } else { @@ -110,22 +109,21 @@ void Foam::nastranSurfaceWriter::writeFaceValue << "and cannot be used for higher rank values" << endl; - writeValue(scalar(0), os); + writeValue(os, scalar(0)) << separator_; } - os << separator_; - writeValue(EID, os); + writeValue(os, EID); break; } case dfPLOAD4: { - writeValue(EID, os); + writeValue(os, EID); - for (direction dirI = 0; dirI < pTraits::nComponents; dirI++) + for (direction dirI = 0; dirI < pTraits::nComponents; ++dirI) { os << separator_; - writeValue(component(scaledValue, dirI), os); + writeValue(os, component(scaledValue, dirI)); } break; } @@ -141,6 +139,8 @@ void Foam::nastranSurfaceWriter::writeFaceValue os.unsetf(ios_base::right); os << nl; + + return os; } @@ -185,15 +185,15 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate Info<< "Writing nastran file to " << os.name() << endl; } - os << "TITLE=OpenFOAM " << surfaceName.c_str() << " " << fieldName - << " data" << nl + os << "TITLE=OpenFOAM " << surfaceName.c_str() + << " " << fieldName << " data" << nl << "$" << nl << "TIME " << timeValue << nl << "$" << nl << "BEGIN BULK" << nl; List> decomposedFaces; - writeGeometry(surf, decomposedFaces, os); + writeGeometry(os, surf, decomposedFaces); os << "$" << nl << "$ Field data" << nl @@ -201,7 +201,7 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate if (isNodeValues) { - label n = 0; + label elemId = 0; forAll(decomposedFaces, i) { @@ -217,28 +217,26 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate } v /= f.size(); - writeFaceValue(format, v, ++n, os); + writeFaceValue(os, format, v, ++elemId); } } } else { - label n = 0; + label elemId = 0; forAll(decomposedFaces, i) { const DynamicList& dFaces = decomposedFaces[i]; - forAll(dFaces, facei) { - writeFaceValue(format, values[facei], ++n, os); + writeFaceValue(os, format, values[facei], ++elemId); } } } - writeFooter(os, surf); - - os << "ENDDATA" << endl; + writeFooter(os, surf) + << "ENDDATA" << endl; return os.name(); }