STYLE: harmonize nastran writer with more standard method calls

- unhide indexing of element/property ids (pass as const parameters).
  Proper indexing is the responsiblity of the caller.
This commit is contained in:
Mark Olesen
2016-08-10 21:05:15 +02:00
parent 1fc2a73213
commit f8bb01e9f1
3 changed files with 119 additions and 130 deletions

View File

@ -25,9 +25,9 @@ License
#include "nastranSurfaceWriter.H" #include "nastranSurfaceWriter.H"
#include "IOmanip.H" #include "IOmanip.H"
#include "Tuple2.H" #include "Pair.H"
#include "makeSurfaceWriterMethods.H"
#include "HashSet.H" #include "HashSet.H"
#include "makeSurfaceWriterMethods.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -65,7 +65,7 @@ namespace Foam
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::nastranSurfaceWriter::formatOS(OFstream& os) const void Foam::nastranSurfaceWriter::formatOS(Ostream& os) const
{ {
os.setf(ios_base::scientific); os.setf(ios_base::scientific);
@ -76,19 +76,23 @@ void Foam::nastranSurfaceWriter::formatOS(OFstream& os) const
label offset = 7; label offset = 7;
switch (writeFormat_) switch (writeFormat_)
{ {
case (wfShort): case wfShort:
{ {
prec = 8 - offset; prec = 8 - offset;
break; break;
} }
case (wfFree):
case (wfLong): case wfFree:
case wfLong:
{ {
prec = 16 - offset; prec = 16 - offset;
break; break;
} }
default: 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 ) const
{ {
os.setf(ios_base::left); os.setf(ios_base::left);
@ -111,11 +115,13 @@ void Foam::nastranSurfaceWriter::writeKeyword
os << setw(8) << keyword; os << setw(8) << keyword;
break; break;
} }
case wfLong: case wfLong:
{ {
os << setw(8) << word(keyword + '*'); os << setw(8) << word(keyword + '*');
break; break;
} }
case wfFree: case wfFree:
{ {
os << keyword; os << keyword;
@ -124,14 +130,16 @@ void Foam::nastranSurfaceWriter::writeKeyword
} }
os.unsetf(ios_base::left); os.unsetf(ios_base::left);
return os;
} }
void Foam::nastranSurfaceWriter::writeCoord void Foam::nastranSurfaceWriter::writeCoord
( (
Ostream& os,
const point& p, const point& p,
const label pointi, const label pointI
OFstream& os
) const ) const
{ {
// Fixed short/long formats: // Fixed short/long formats:
@ -145,21 +153,14 @@ void Foam::nastranSurfaceWriter::writeCoord
// 8 PS : single point constraints (blank) // 8 PS : single point constraints (blank)
// 9 SEID : super-element ID // 9 SEID : super-element ID
writeKeyword(os, "GRID") << separator_;
writeKeyword("GRID", os);
os << separator_;
os.setf(ios_base::right); os.setf(ios_base::right);
writeValue(pointi + 1, os); writeValue(os, pointI+1) << separator_;
os << separator_; writeValue(os, "") << separator_;
writeValue("", os); writeValue(os, p.x()) << separator_;
os << separator_; writeValue(os, p.y()) << separator_;
writeValue(p.x(), os);
os << separator_;
writeValue(p.y(), os);
os << separator_;
switch (writeFormat_) switch (writeFormat_)
{ {
@ -168,27 +169,26 @@ void Foam::nastranSurfaceWriter::writeCoord
os << setw(8) << p.z() os << setw(8) << p.z()
<< nl; << nl;
os.unsetf(ios_base::right); os.unsetf(ios_base::right);
break; break;
} }
case wfLong: case wfLong:
{ {
os << nl; os << nl;
os.unsetf(ios_base::right); os.unsetf(ios_base::right);
writeKeyword("", os); writeKeyword(os, "");
os.setf(ios_base::right); os.setf(ios_base::right);
writeValue(p.z(), os);
os << nl;
writeValue(os, p.z()) << nl;
break; break;
} }
case wfFree: case wfFree:
{ {
writeValue(p.z(), os); writeValue(os, p.z()) << nl;
os << nl;
break; break;
} }
default: default:
{ {
FatalErrorInFunction FatalErrorInFunction
@ -202,11 +202,11 @@ void Foam::nastranSurfaceWriter::writeCoord
void Foam::nastranSurfaceWriter::writeFace void Foam::nastranSurfaceWriter::writeFace
( (
Ostream& os,
const word& faceType, const word& faceType,
const labelList& facePts, const labelUList& facePts,
label& nFace, const label nFace,
const label PID, const label PID
OFstream& os
) const ) const
{ {
// Only valid surface elements are CTRIA3 and CQUAD4 // 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 // For CTRIA3 elements, cols 7 onwards are not used
writeKeyword(faceType, os); writeKeyword(os, faceType) << separator_;
os << separator_;
os.setf(ios_base::right); os.setf(ios_base::right);
writeValue(nFace++, os); writeValue(os, nFace) << separator_;
os << separator_; writeValue(os, PID);
writeValue(PID, os);
switch (writeFormat_) switch (writeFormat_)
{ {
@ -239,7 +236,7 @@ void Foam::nastranSurfaceWriter::writeFace
{ {
forAll(facePts, i) forAll(facePts, i)
{ {
writeValue(facePts[i] + 1, os); writeValue(os, facePts[i] + 1);
} }
break; break;
@ -248,12 +245,12 @@ void Foam::nastranSurfaceWriter::writeFace
{ {
forAll(facePts, i) forAll(facePts, i)
{ {
writeValue(facePts[i] + 1, os); writeValue(os, facePts[i] + 1);
if (i == 1) if (i == 1)
{ {
os << nl; os << nl;
os.unsetf(ios_base::right); os.unsetf(ios_base::right);
writeKeyword("", os); writeKeyword(os, "");
os.setf(ios_base::right); os.setf(ios_base::right);
} }
} }
@ -265,7 +262,7 @@ void Foam::nastranSurfaceWriter::writeFace
forAll(facePts, i) forAll(facePts, i)
{ {
os << separator_; os << separator_;
writeValue(facePts[i] + 1, os); writeValue(os, facePts[i] + 1);
} }
break; break;
@ -284,9 +281,9 @@ void Foam::nastranSurfaceWriter::writeFace
void Foam::nastranSurfaceWriter::writeGeometry void Foam::nastranSurfaceWriter::writeGeometry
( (
Ostream& os,
const meshedSurf& surf, const meshedSurf& surf,
List<DynamicList<face>>& decomposedFaces, List<DynamicList<face>>& decomposedFaces
OFstream& os
) const ) const
{ {
const pointField& points = surf.points(); const pointField& points = surf.points();
@ -301,7 +298,7 @@ void Foam::nastranSurfaceWriter::writeGeometry
forAll(points, pointi) forAll(points, pointi)
{ {
writeCoord(points[pointi], pointi, os); writeCoord(os, points[pointi], pointi);
} }
// Write faces // Write faces
@ -312,8 +309,7 @@ void Foam::nastranSurfaceWriter::writeGeometry
<< "$ Faces" << nl << "$ Faces" << nl
<< "$" << nl; << "$" << nl;
label nFace = 1; // the element-id label nFace = 0; // the element-id
forAll(faces, facei) forAll(faces, facei)
{ {
const face& f = faces[facei]; const face& f = faces[facei];
@ -322,12 +318,12 @@ void Foam::nastranSurfaceWriter::writeGeometry
if (f.size() == 3) if (f.size() == 3)
{ {
writeFace("CTRIA3", f, nFace, PID, os); writeFace(os, "CTRIA3", f, ++nFace, PID);
decomposedFaces[facei].append(f); decomposedFaces[facei].append(f);
} }
else if (f.size() == 4) else if (f.size() == 4)
{ {
writeFace("CQUAD4", f, nFace, PID, os); writeFace(os, "CQUAD4", f, ++nFace, PID);
decomposedFaces[facei].append(f); decomposedFaces[facei].append(f);
} }
else else
@ -339,7 +335,7 @@ void Foam::nastranSurfaceWriter::writeGeometry
forAll(triFaces, trii) forAll(triFaces, trii)
{ {
writeFace("CTRIA3", triFaces[trii], nFace, PID, os); writeFace(os, "CTRIA3", triFaces[trii], ++nFace, PID);
decomposedFaces[facei].append(triFaces[trii]); decomposedFaces[facei].append(triFaces[trii]);
} }
} }
@ -347,7 +343,7 @@ void Foam::nastranSurfaceWriter::writeGeometry
} }
void Foam::nastranSurfaceWriter::writeFooter Foam::Ostream& Foam::nastranSurfaceWriter::writeFooter
( (
Ostream& os, Ostream& os,
const meshedSurf& surf const meshedSurf& surf
@ -363,15 +359,14 @@ void Foam::nastranSurfaceWriter::writeFooter
for (auto pid : pidsUsed) for (auto pid : pidsUsed)
{ {
writeKeyword("PSHELL", os); writeKeyword(os, "PSHELL") << separator_;
os << separator_; writeValue(os, pid+1); // 1-offset for PID
writeValue(pid+1, os); // 1-offset for PID
for (label i = 0; i < 7; i++) for (label i = 0; i < 7; ++i)
{ {
// Dummy values // Dummy values
os << separator_; os << separator_;
writeValue(1, os); writeValue(os, 1);
} }
os << nl; os << nl;
} }
@ -381,17 +376,18 @@ void Foam::nastranSurfaceWriter::writeFooter
label MID = 1; label MID = 1;
writeKeyword("MAT1", os); writeKeyword(os, "MAT1") << separator_;
os << separator_; writeValue(os, MID);
writeValue(MID, os);
for (label i = 0; i < 7; i++) for (label i = 0; i < 7; ++i)
{ {
// Dummy values // Dummy values
os << separator_; os << separator_;
writeValue("", os); writeValue(os, "");
} }
os << nl; os << nl;
return os;
} }
@ -416,7 +412,10 @@ Foam::nastranSurfaceWriter::nastranSurfaceWriter(const dictionary& options)
{ {
if (options.found("format")) if (options.found("format"))
{ {
writeFormat_ = writeFormatNames_.read(options.lookup("format")); writeFormat_ = writeFormatNames_.read
(
options.lookup("format")
);
} }
if (writeFormat_ == wfFree) if (writeFormat_ == wfFree)
@ -424,7 +423,7 @@ Foam::nastranSurfaceWriter::nastranSurfaceWriter(const dictionary& options)
separator_ = ","; separator_ = ",";
} }
List<Tuple2<word, word>> fieldSet(options.lookup("fields")); List<Pair<word>> fieldSet(options.lookup("fields"));
forAll(fieldSet, i) forAll(fieldSet, i)
{ {
@ -464,21 +463,16 @@ Foam::fileName Foam::nastranSurfaceWriter::write
Info<< "Writing nastran file to " << os.name() << endl; 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 << "$" << nl
<< "BEGIN BULK" << nl; << "BEGIN BULK" << nl;
List<DynamicList<face>> decomposedFaces; List<DynamicList<face>> decomposedFaces;
writeGeometry(surf, decomposedFaces, os); writeGeometry(os, surf, decomposedFaces);
if (!isDir(outputDir)) writeFooter(os, surf)
{ << "ENDDATA" << endl;
mkDir(outputDir);
}
writeFooter(os, surf);
os << "ENDDATA" << endl;
return os.name(); return os.name();
} }

View File

@ -94,13 +94,13 @@ private:
// Private data // Private data
static const NamedEnum<writeFormat, 3> writeFormatNames_; static const NamedEnum<writeFormat, 3> writeFormatNames_;
static const NamedEnum<dataFormat, 2> dataFormatNames_; static const NamedEnum<dataFormat, 2> dataFormatNames_;
//- Write option //- Write option
writeFormat writeFormat_; writeFormat writeFormat_;
//- Mapping from field name to data format enumeration //- Mapping from field name to data format enumeration
HashTable<dataFormat, word> fieldMap_; HashTable<dataFormat> fieldMap_;
//- Scale to apply to values (default = 1.0) //- Scale to apply to values (default = 1.0)
scalar scale_; scalar scale_;
@ -111,63 +111,60 @@ private:
// Private Member Functions // Private Member Functions
//- Initialise the output stream format params //- Initialise the output stream format parameters
void formatOS(OFstream& os) const; void formatOS(Ostream& os) const;
//- Write a co-ordinate //- Write a coordinate
void writeCoord void writeCoord
( (
Ostream& os,
const point& p, const point& p,
const label pointi, const label pointI //!< 0-based Point Id
OFstream& os
) const; ) const;
//- Write a face element (CTRIA3 or CQUAD4) //- Write a face element (CTRIA3 or CQUAD4)
void writeFace void writeFace
( (
Ostream& os,
const word& faceType, const word& faceType,
const labelList& facePts, const labelUList& facePts,
label& nFace, const label EID, //!< 1-based Element Id
const label PID, const label PID //!< 1-based Property Id
OFstream& os
) const; ) const;
//- Main driver to write the surface mesh geometry //- Main driver to write the surface mesh geometry
void writeGeometry void writeGeometry
( (
Ostream& os,
const meshedSurf& surf, const meshedSurf& surf,
List<DynamicList<face>>& decomposedFaces, List<DynamicList<face>>& decomposedFaces
OFstream& os
) const; ) const;
//- Write the formatted keyword to the output stream //- Write the formatted keyword to the output stream
void writeKeyword Ostream& writeKeyword
( (
const word& keyword, Ostream& os,
Ostream& os const word& keyword
) const; ) const;
//- Write the footer information //- 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 //- Write a formatted value to the output stream
template<class Type> template<class Type>
void writeValue Ostream& writeValue(Ostream& os, const Type& value) const;
(
const Type& value,
Ostream& os
) const;
//- Write a face-based value //- Write a face-based value
template<class Type> template<class Type>
void writeFaceValue Ostream& writeFaceValue
( (
Ostream& os,
const dataFormat& format, const dataFormat& format,
const Type& value, const Type& value,
const label EID, const label EID //!< 1-based Element Id
Ostream& os
) const; ) const;
//- Templated write operation //- Templated write operation
template<class Type> template<class Type>
fileName writeTemplate fileName writeTemplate

View File

@ -30,10 +30,10 @@ License
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class Type> template<class Type>
void Foam::nastranSurfaceWriter::writeValue Foam::Ostream& Foam::nastranSurfaceWriter::writeValue
( (
const Type& value, Ostream& os,
Ostream& os const Type& value
) const ) const
{ {
switch (writeFormat_) switch (writeFormat_)
@ -54,16 +54,18 @@ void Foam::nastranSurfaceWriter::writeValue
break; break;
} }
} }
return os;
} }
template<class Type> template<class Type>
void Foam::nastranSurfaceWriter::writeFaceValue Foam::Ostream& Foam::nastranSurfaceWriter::writeFaceValue
( (
Ostream& os,
const dataFormat& format, const dataFormat& format,
const Type& value, const Type& value,
const label EID, const label EID
Ostream& os
) const ) const
{ {
// Fixed short/long formats supporting PLOAD2 and PLOAD4: // Fixed short/long formats supporting PLOAD2 and PLOAD4:
@ -84,16 +86,13 @@ void Foam::nastranSurfaceWriter::writeFaceValue
Type scaledValue = scale_*value; Type scaledValue = scale_*value;
// Write Keyword // Write keyword
writeKeyword(dataFormatNames_[format], os); writeKeyword(os, dataFormatNames_[format]) << separator_;
os << separator_;
// Write load set ID // Write load set ID
os.setf(ios_base::right); os.setf(ios_base::right);
writeValue(SID, os);
os << separator_; writeValue(os, SID) << separator_;
switch (format) switch (format)
{ {
@ -101,7 +100,7 @@ void Foam::nastranSurfaceWriter::writeFaceValue
{ {
if (pTraits<Type>::nComponents == 1) if (pTraits<Type>::nComponents == 1)
{ {
writeValue(scaledValue, os); writeValue(os, scaledValue) << separator_;
} }
else else
{ {
@ -110,22 +109,21 @@ void Foam::nastranSurfaceWriter::writeFaceValue
<< "and cannot be used for higher rank values" << "and cannot be used for higher rank values"
<< endl; << endl;
writeValue(scalar(0), os); writeValue(os, scalar(0)) << separator_;
} }
os << separator_; writeValue(os, EID);
writeValue(EID, os);
break; break;
} }
case dfPLOAD4: case dfPLOAD4:
{ {
writeValue(EID, os); writeValue(os, EID);
for (direction dirI = 0; dirI < pTraits<Type>::nComponents; dirI++) for (direction dirI = 0; dirI < pTraits<Type>::nComponents; ++dirI)
{ {
os << separator_; os << separator_;
writeValue(component(scaledValue, dirI), os); writeValue(os, component(scaledValue, dirI));
} }
break; break;
} }
@ -141,6 +139,8 @@ void Foam::nastranSurfaceWriter::writeFaceValue
os.unsetf(ios_base::right); os.unsetf(ios_base::right);
os << nl; os << nl;
return os;
} }
@ -185,15 +185,15 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate
Info<< "Writing nastran file to " << os.name() << endl; Info<< "Writing nastran file to " << os.name() << endl;
} }
os << "TITLE=OpenFOAM " << surfaceName.c_str() << " " << fieldName os << "TITLE=OpenFOAM " << surfaceName.c_str()
<< " data" << nl << " " << fieldName << " data" << nl
<< "$" << nl << "$" << nl
<< "TIME " << timeValue << nl << "TIME " << timeValue << nl
<< "$" << nl << "$" << nl
<< "BEGIN BULK" << nl; << "BEGIN BULK" << nl;
List<DynamicList<face>> decomposedFaces; List<DynamicList<face>> decomposedFaces;
writeGeometry(surf, decomposedFaces, os); writeGeometry(os, surf, decomposedFaces);
os << "$" << nl os << "$" << nl
<< "$ Field data" << nl << "$ Field data" << nl
@ -201,7 +201,7 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate
if (isNodeValues) if (isNodeValues)
{ {
label n = 0; label elemId = 0;
forAll(decomposedFaces, i) forAll(decomposedFaces, i)
{ {
@ -217,28 +217,26 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate
} }
v /= f.size(); v /= f.size();
writeFaceValue(format, v, ++n, os); writeFaceValue(os, format, v, ++elemId);
} }
} }
} }
else else
{ {
label n = 0; label elemId = 0;
forAll(decomposedFaces, i) forAll(decomposedFaces, i)
{ {
const DynamicList<face>& dFaces = decomposedFaces[i]; const DynamicList<face>& dFaces = decomposedFaces[i];
forAll(dFaces, facei) forAll(dFaces, facei)
{ {
writeFaceValue(format, values[facei], ++n, os); writeFaceValue(os, format, values[facei], ++elemId);
} }
} }
} }
writeFooter(os, surf); writeFooter(os, surf)
<< "ENDDATA" << endl;
os << "ENDDATA" << endl;
return os.name(); return os.name();
} }