ENH: consolidate surfaceFormats for reading/writing triSurface (issue #294)

- eliminates previous code duplication and improves maintainability
This commit is contained in:
Mark Olesen
2017-11-20 14:55:36 +01:00
parent 5947f9a337
commit 3ef8906a66
111 changed files with 2646 additions and 4610 deletions

View File

@ -40,164 +40,84 @@ namespace Foam
defineSurfaceWriterWriteFields(nastranSurfaceWriter);
}
const Foam::Enum
<
Foam::nastranSurfaceWriter::writeFormat
Foam::nastranSurfaceWriter::loadFormat
>
Foam::nastranSurfaceWriter::writeFormatNames_
Foam::nastranSurfaceWriter::loadFormatNames_
{
{ writeFormat::wfShort, "short" },
{ writeFormat::wfLong, "long" },
{ writeFormat::wfFree, "free" },
};
const Foam::Enum
<
Foam::nastranSurfaceWriter::dataFormat
>
Foam::nastranSurfaceWriter::dataFormatNames_
{
{ dataFormat::dfPLOAD2, "PLOAD2" },
{ dataFormat::dfPLOAD4, "PLOAD4" },
{ loadFormat::PLOAD2, "PLOAD2" },
{ loadFormat::PLOAD4, "PLOAD4" },
};
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::nastranSurfaceWriter::formatOS(Ostream& os) const
{
os.setf(ios_base::scientific);
// Capitalise the E marker
os.setf(ios_base::uppercase);
label prec = 0;
label offset = 7;
switch (writeFormat_)
{
case wfShort:
{
prec = 8 - offset;
break;
}
case wfFree:
case wfLong:
{
prec = 16 - offset;
break;
}
default:
{
FatalErrorInFunction
<< "Unknown writeFormat enumeration" << abort(FatalError);
}
}
os.precision(prec);
}
Foam::Ostream& Foam::nastranSurfaceWriter::writeKeyword
(
Ostream& os,
const word& keyword
) const
{
os.setf(ios_base::left);
switch (writeFormat_)
{
case wfShort:
{
os << setw(8) << keyword;
break;
}
case wfLong:
{
os << setw(8) << word(keyword + '*');
break;
}
case wfFree:
{
os << keyword;
break;
}
}
os.unsetf(ios_base::left);
return os;
return fileFormats::NASCore::writeKeyword(os, keyword, writeFormat_);
}
void Foam::nastranSurfaceWriter::writeCoord
(
Ostream& os,
const point& p,
const point& pt,
const label pointI
) const
{
// Fixed short/long formats:
// 1 GRID
// 2 ID : point ID - requires starting index of 1
// 3 CP : co-ordinate system ID (blank)
// 4 X1 : point x cp-ordinate
// 5 X2 : point x cp-ordinate
// 6 X3 : point x cp-ordinate
// 7 CD : co-ordinate system for displacements (blank)
// 8 PS : single point constraints (blank)
// 3 CP : coordinate system ID (blank)
// 4 X1 : point x coordinate
// 5 X2 : point x coordinate
// 6 X3 : point x coordinate
// 7 CD : coordinate system for displacements (blank)
// 8 PS : single point constraints (blank)
// 9 SEID : super-element ID
writeKeyword(os, "GRID") << separator_;
os.setf(ios_base::right);
os.setf(std::ios_base::right);
writeValue(os, pointI+1) << separator_;
writeValue(os, "") << separator_;
writeValue(os, p.x()) << separator_;
writeValue(os, p.y()) << separator_;
writeValue(os, pt.x()) << separator_;
writeValue(os, pt.y()) << separator_;
switch (writeFormat_)
{
case wfShort:
case fieldFormat::SHORT :
{
os << setw(8) << p.z()
<< nl;
os.unsetf(ios_base::right);
os << setw(8) << pt.z() << nl;
os.unsetf(std::ios_base::right);
break;
}
case wfLong:
case fieldFormat::LONG :
{
os << nl;
os.unsetf(ios_base::right);
os.unsetf(std::ios_base::right);
writeKeyword(os, "");
os.setf(ios_base::right);
os.setf(std::ios_base::right);
writeValue(os, p.z()) << nl;
writeValue(os, pt.z()) << nl;
break;
}
case wfFree:
case fieldFormat::FREE :
{
writeValue(os, p.z()) << nl;
writeValue(os, pt.z()) << nl;
break;
}
default:
{
FatalErrorInFunction
<< "Unknown writeFormat enumeration" << abort(FatalError);
}
}
os.unsetf(ios_base::right);
os.unsetf(std::ios_base::right);
}
@ -226,23 +146,24 @@ void Foam::nastranSurfaceWriter::writeFace
writeKeyword(os, faceType) << separator_;
os.setf(ios_base::right);
os.setf(std::ios_base::right);
writeValue(os, nFace) << separator_;
writeValue(os, PID);
switch (writeFormat_)
{
case wfShort:
case fieldFormat::SHORT :
{
forAll(facePts, i)
for (const label pointi : facePts)
{
writeValue(os, facePts[i] + 1);
writeValue(os, pointi + 1);
}
break;
}
case wfLong:
case fieldFormat::LONG :
{
forAll(facePts, i)
{
@ -250,33 +171,29 @@ void Foam::nastranSurfaceWriter::writeFace
if (i == 1)
{
os << nl;
os.unsetf(ios_base::right);
os.unsetf(std::ios_base::right);
writeKeyword(os, "");
os.setf(ios_base::right);
os.setf(std::ios_base::right);
}
}
break;
}
case wfFree:
case fieldFormat::FREE :
{
forAll(facePts, i)
for (const label pointi : facePts)
{
os << separator_;
writeValue(os, facePts[i] + 1);
writeValue(os, pointi + 1);
}
break;
}
default:
{
FatalErrorInFunction
<< "Unknown writeFormat enumeration" << abort(FatalError);
}
}
os << nl;
os.unsetf(ios_base::right);
os.unsetf(std::ios_base::right);
}
@ -375,7 +292,7 @@ Foam::Ostream& Foam::nastranSurfaceWriter::writeFooter
// use single material ID
label MID = 1;
const label MID = 1;
writeKeyword(os, "MAT1") << separator_;
writeValue(os, MID);
@ -397,49 +314,48 @@ Foam::Ostream& Foam::nastranSurfaceWriter::writeFooter
Foam::nastranSurfaceWriter::nastranSurfaceWriter()
:
surfaceWriter(),
writeFormat_(wfShort),
writeFormat_(fieldFormat::SHORT),
fieldMap_(),
scale_(1.0)
scale_(1.0),
separator_()
{}
Foam::nastranSurfaceWriter::nastranSurfaceWriter(const dictionary& options)
:
surfaceWriter(),
writeFormat_(writeFormat::wfLong),
fieldMap_(),
scale_(options.lookupOrDefault("scale", 1.0)),
separator_("")
{
writeFormat_ = writeFormatNames_.lookupOrDefault
writeFormat_
(
"format",
options,
writeFormat::wfLong
);
if (writeFormat_ == wfFree)
fileFormats::NASCore::fieldFormatNames.lookupOrDefault
(
"format",
options,
fieldFormat::LONG
)
),
fieldMap_(),
scale_(options.lookupOrDefault<scalar>("scale", 1.0)),
separator_()
{
if (writeFormat_ == fieldFormat::FREE)
{
separator_ = ",";
}
List<Pair<word>> fieldSet(options.lookup("fields"));
List<Pair<word>> fieldPairs(options.lookup("fields"));
forAll(fieldSet, i)
for (const Pair<word>& item : fieldPairs)
{
dataFormat format = dataFormatNames_[fieldSet[i].second()];
fieldMap_.insert(fieldSet[i].first(), format);
// (field name => load format)
fieldMap_.insert
(
item.first(),
loadFormatNames_[item.second()]
);
}
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::nastranSurfaceWriter::~nastranSurfaceWriter()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::fileName Foam::nastranSurfaceWriter::write
@ -456,7 +372,7 @@ Foam::fileName Foam::nastranSurfaceWriter::write
}
OFstream os(outputDir/surfaceName + ".nas");
formatOS(os);
fileFormats::NASCore::setPrecision(os, writeFormat_);
if (verbose)
{
@ -472,7 +388,7 @@ Foam::fileName Foam::nastranSurfaceWriter::write
writeGeometry(os, surf, decomposedFaces);
writeFooter(os, surf)
<< "ENDDATA" << endl;
<< "ENDDATA" << nl;
return os.name();
}

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2012-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015-2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -32,16 +32,16 @@ Description
{
nastran
{
// From OpenFOAM field name to Nastran field name
// From OpenFOAM field name to NASTRAN field name
fields
(
(pMean PLOAD2)
(p PLOAD4)
(pMean PLOAD2)
(p PLOAD4)
);
// Optional scale
scale 2.0;
scale 2.0;
// Optional format
format free; //short, long, free
format free; // short, long, free
}
};
\endverbatim
@ -56,7 +56,7 @@ SourceFiles
#define nastranSurfaceWriter_H
#include "surfaceWriter.H"
#include "Enum.H"
#include "NASCore.H"
#include "OFstream.H"
#include "HashTable.H"
@ -75,17 +75,14 @@ class nastranSurfaceWriter
{
public:
enum writeFormat
{
wfShort,
wfLong,
wfFree
};
//- File field formats
using fieldFormat = Foam::fileFormats::NASCore::fieldFormat;
enum dataFormat
//- Output load format
enum loadFormat
{
dfPLOAD2,
dfPLOAD4
PLOAD2,
PLOAD4
};
@ -93,14 +90,13 @@ private:
// Private data
static const Enum<writeFormat> writeFormatNames_;
static const Enum<dataFormat> dataFormatNames_;
static const Enum<loadFormat> loadFormatNames_;
//- Write option
writeFormat writeFormat_;
//- Field format (width and separator)
fieldFormat writeFormat_;
//- Mapping from field name to data format enumeration
HashTable<dataFormat> fieldMap_;
HashTable<loadFormat> fieldMap_;
//- Scale to apply to values (default = 1.0)
scalar scale_;
@ -111,14 +107,11 @@ private:
// Private Member Functions
//- Initialise the output stream format parameters
void formatOS(Ostream& os) const;
//- Write a coordinate
void writeCoord
(
Ostream& os,
const point& p,
const point& pt,
const label pointI //!< 0-based Point Id
) const;
@ -159,7 +152,7 @@ private:
Ostream& writeFaceValue
(
Ostream& os,
const dataFormat& format,
const loadFormat format,
const Type& value,
const label EID //!< 1-based Element Id
) const;
@ -195,7 +188,7 @@ public:
//- Destructor
virtual ~nastranSurfaceWriter();
virtual ~nastranSurfaceWriter() = default;
// Member Functions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2012-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015-2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -38,17 +38,19 @@ Foam::Ostream& Foam::nastranSurfaceWriter::writeValue
{
switch (writeFormat_)
{
case wfShort:
case fieldFormat::SHORT :
{
os << setw(8) << value;
break;
}
case wfLong:
case fieldFormat::LONG :
{
os << setw(16) << value;
break;
}
case wfFree:
case fieldFormat::FREE :
{
os << value;
break;
@ -63,7 +65,7 @@ template<class Type>
Foam::Ostream& Foam::nastranSurfaceWriter::writeFaceValue
(
Ostream& os,
const dataFormat& format,
const loadFormat format,
const Type& value,
const label EID
) const
@ -87,16 +89,16 @@ Foam::Ostream& Foam::nastranSurfaceWriter::writeFaceValue
Type scaledValue = scale_*value;
// Write keyword
writeKeyword(os, dataFormatNames_[format]) << separator_;
writeKeyword(os, loadFormatNames_[format]) << separator_;
// Write load set ID
os.setf(ios_base::right);
os.setf(std::ios_base::right);
writeValue(os, SID) << separator_;
switch (format)
{
case dfPLOAD2:
case loadFormat::PLOAD2 :
{
if (pTraits<Type>::nComponents == 1)
{
@ -105,7 +107,7 @@ Foam::Ostream& Foam::nastranSurfaceWriter::writeFaceValue
else
{
WarningInFunction
<< dataFormatNames_[format] << " requires scalar values "
<< loadFormatNames_[format] << " requires scalar values "
<< "and cannot be used for higher rank values"
<< endl;
@ -116,27 +118,20 @@ Foam::Ostream& Foam::nastranSurfaceWriter::writeFaceValue
break;
}
case dfPLOAD4:
case loadFormat::PLOAD4 :
{
writeValue(os, EID);
for (direction dirI = 0; dirI < pTraits<Type>::nComponents; ++dirI)
for (direction d = 0; d < pTraits<Type>::nComponents; ++d)
{
os << separator_;
writeValue(os, component(scaledValue, dirI));
writeValue(os, component(scaledValue, d));
}
break;
}
default:
{
FatalErrorInFunction
<< "Unhandled enumeration " << dataFormatNames_[format]
<< exit(FatalError);
}
}
os.unsetf(ios_base::right);
os.unsetf(std::ios_base::right);
os << nl;
@ -167,7 +162,7 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate
return fileName::null;
}
const dataFormat& format(fieldMap_[fieldName]);
const loadFormat& format(fieldMap_[fieldName]);
if (!isDir(outputDir/fieldName))
{
@ -178,7 +173,7 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate
const scalar timeValue = 0.0;
OFstream os(outputDir/fieldName/surfaceName + ".nas");
formatOS(os);
fileFormats::NASCore::setPrecision(os, writeFormat_);
if (verbose)
{
@ -199,21 +194,19 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate
<< "$ Field data" << nl
<< "$" << nl;
label elemId = 0;
if (isNodeValues)
{
label elemId = 0;
forAll(decomposedFaces, i)
for (const DynamicList<face>& dFaces : decomposedFaces)
{
const DynamicList<face>& dFaces = decomposedFaces[i];
forAll(dFaces, facei)
for (const face& f : dFaces)
{
Type v = Zero;
const face& f = dFaces[facei];
forAll(f, fptI)
for (const label verti : f)
{
v += values[f[fptI]];
v += values[verti];
}
v /= f.size();
@ -223,11 +216,8 @@ Foam::fileName Foam::nastranSurfaceWriter::writeTemplate
}
else
{
label elemId = 0;
forAll(decomposedFaces, i)
for (const DynamicList<face>& dFaces : decomposedFaces)
{
const DynamicList<face>& dFaces = decomposedFaces[i];
forAll(dFaces, facei)
{
writeFaceValue(os, format, values[facei], ++elemId);

View File

@ -55,9 +55,8 @@ void Foam::vtkSurfaceWriter::writeGeometry
// Write vertex coords
os << "POINTS " << points.size() << " double" << nl;
forAll(points, pointi)
for (const point& pt : points)
{
const point& pt = points[pointi];
os << float(pt.x()) << ' '
<< float(pt.y()) << ' '
<< float(pt.z()) << nl;
@ -67,22 +66,20 @@ void Foam::vtkSurfaceWriter::writeGeometry
// Write faces
label nNodes = 0;
forAll(faces, facei)
for (const face& f : faces)
{
nNodes += faces[facei].size();
nNodes += f.size();
}
os << "POLYGONS " << faces.size() << ' '
<< faces.size() + nNodes << nl;
forAll(faces, facei)
for (const face& f : faces)
{
const face& f = faces[facei];
os << f.size();
forAll(f, fp)
for (const label verti : f)
{
os << ' ' << f[fp];
os << ' ' << verti;
}
os << nl;
}
@ -130,9 +127,8 @@ namespace Foam
{
os << "3 " << values.size() << " float" << nl;
forAll(values, elemI)
for (const vector& v : values)
{
const vector& v = values[elemI];
os << float(v[0]) << ' '
<< float(v[1]) << ' '
<< float(v[2]) << nl;
@ -149,9 +145,8 @@ namespace Foam
{
os << "1 " << values.size() << " float" << nl;
forAll(values, elemI)
for (const sphericalTensor& v : values)
{
const sphericalTensor& v = values[elemI];
os << float(v[0]) << nl;
}
}
@ -166,9 +161,8 @@ namespace Foam
{
os << "6 " << values.size() << " float" << nl;
forAll(values, elemI)
for (const symmTensor& v : values)
{
const symmTensor& v = values[elemI];
os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
<< ' '
<< float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
@ -187,9 +181,8 @@ namespace Foam
{
os << "9 " << values.size() << " float" << nl;
forAll(values, elemI)
for (const tensor& v : values)
{
const tensor& v = values[elemI];
os << float(v[0]) << ' ' << float(v[1]) << ' ' << float(v[2])
<< ' '
<< float(v[3]) << ' ' << float(v[4]) << ' ' << float(v[5])
@ -211,12 +204,12 @@ Foam::vtkSurfaceWriter::vtkSurfaceWriter()
{}
Foam::vtkSurfaceWriter::vtkSurfaceWriter(const dictionary& dict)
Foam::vtkSurfaceWriter::vtkSurfaceWriter(const dictionary& options)
:
surfaceWriter(),
writePrecision_
(
dict.lookupOrDefault
options.lookupOrDefault
(
"writePrecision",
IOstream::defaultPrecision()
@ -225,12 +218,6 @@ Foam::vtkSurfaceWriter::vtkSurfaceWriter(const dictionary& dict)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::vtkSurfaceWriter::~vtkSurfaceWriter()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::fileName Foam::vtkSurfaceWriter::write
@ -260,7 +247,7 @@ Foam::fileName Foam::vtkSurfaceWriter::write
}
// create write methods
// Create write methods
defineSurfaceWriterWriteFields(Foam::vtkSurfaceWriter);

View File

@ -93,7 +93,7 @@ public:
//- Destructor
virtual ~vtkSurfaceWriter();
virtual ~vtkSurfaceWriter() = default;
// Member Functions