ENH: use float-narrowing for ensight set writer

- replaces hand-rolled checks

STYLE: minor cleanup of ensightPTraits
This commit is contained in:
Mark Olesen 2021-11-11 11:19:39 +01:00
parent e62a260bdd
commit adb01bddf4
3 changed files with 67 additions and 87 deletions

View File

@ -62,16 +62,15 @@ template<>
const char* const const char* const
Foam::ensightPTraits<Foam::sphericalTensor>::typeName = "scalar"; Foam::ensightPTraits<Foam::sphericalTensor>::typeName = "scalar";
template<> template<>
const Foam::direction const Foam::direction
Foam::ensightPTraits<Foam::sphericalTensor>::componentOrder[] = {0}; Foam::ensightPTraits<Foam::sphericalTensor>::componentOrder[] = {0};
template<> template<>
const char* const const char* const
Foam::ensightPTraits<Foam::symmTensor>::typeName = "tensor symm"; Foam::ensightPTraits<Foam::symmTensor>::typeName = "tensor symm";
template<> template<>
const Foam::direction const Foam::direction
Foam::ensightPTraits<Foam::symmTensor>::componentOrder[] = {0, 3, 5, 1, 2, 4}; Foam::ensightPTraits<Foam::symmTensor>::componentOrder[] = {0, 3, 5, 1, 2, 4};

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2013-2015 OpenFOAM Foundation Copyright (C) 2013-2015 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,7 +28,7 @@ Class
Foam::ensightPTraits Foam::ensightPTraits
Description Description
Conversion of OpenFOAM pTraits into the Ensight equivalent Ensight names and component order for base types.
For the purpose of traits, integers (label) are treated like For the purpose of traits, integers (label) are treated like
floating point (scalar). Spherical tensors are mapped as a scalar. floating point (scalar). Spherical tensors are mapped as a scalar.
@ -38,7 +38,6 @@ Description
#ifndef ensightPTraits_H #ifndef ensightPTraits_H
#define ensightPTraits_H #define ensightPTraits_H
#include "pTraits.H"
#include "fieldTypes.H" #include "fieldTypes.H"
#include "direction.H" #include "direction.H"
@ -51,13 +50,10 @@ namespace Foam
Class ensightPTraits Declaration Class ensightPTraits Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
template<class PrimitiveType> template<class Type>
class ensightPTraits struct ensightPTraits
{ {
public: //- The type name used in ensight case files
// Static data members
static const char* const typeName; static const char* const typeName;
//- Ordering table: return OpenFOAM component given Ensight component //- Ordering table: return OpenFOAM component given Ensight component
@ -66,6 +62,9 @@ public:
static const direction componentOrder[]; static const direction componentOrder[];
}; };
// Specializations
template<> template<>
const char* const ensightPTraits<label>::typeName; const char* const ensightPTraits<label>::typeName;

View File

@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -27,10 +28,10 @@ License
#include "ensightSetWriter.H" #include "ensightSetWriter.H"
#include "coordSet.H" #include "coordSet.H"
#include "OFstream.H"
#include "addToRunTimeSelectionTable.H"
#include "IOmanip.H" #include "IOmanip.H"
#include "ensightGeoFile.H"
#include "ensightPTraits.H" #include "ensightPTraits.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -85,14 +86,15 @@ void Foam::ensightSetWriter<Type>::write
<< nl << nl
<< "VARIABLE" << "VARIABLE"
<< nl; << nl;
forAll(valueSetNames, setI)
for (const word& valueName : valueSetNames)
{ {
fileName dataFile(base + ".***." + valueSetNames[setI]); fileName dataFile(base + ".***." + valueName);
os.setf(ios_base::left); os.setf(ios_base::left);
os << ensightPTraits<Type>::typeName os << ensightPTraits<Type>::typeName
<< " per node: 1 " << " per node: 1 "
<< setw(15) << valueSetNames[setI] << setw(15) << valueName
<< " " << dataFile.name().c_str() << " " << dataFile.name().c_str()
<< nl; << nl;
} }
@ -122,20 +124,13 @@ void Foam::ensightSetWriter<Type>::write
<< "coordinates" << nl << "coordinates" << nl
<< setw(10) << points.size() << nl; << setw(10) << points.size() << nl;
for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++) for (direction cmpt = 0; cmpt < vector::nComponents; ++cmpt)
{ {
forAll(points, pointi) for (const point& p : points)
{
const scalar comp = points[pointi][cmpt];
if (mag(comp) >= scalar(floatScalarVSMALL))
{ {
const float comp = narrowFloat(p[cmpt]);
os << setw(12) << comp << nl; os << setw(12) << comp << nl;
} }
else
{
os << setw(12) << scalar(0) << nl;
}
}
} }
os << "point" << nl os << "point" << nl
<< setw(10) << points.size() << nl; << setw(10) << points.size() << nl;
@ -146,34 +141,29 @@ void Foam::ensightSetWriter<Type>::write
} }
// Write data files // Write data files
forAll(valueSetNames, setI) forAll(valueSetNames, seti)
{ {
fileName dataFile(base + ".000." + valueSetNames[setI]); const word& valueName = valueSetNames[seti];
const Field<Type>& fld = *(valueSets[seti]);
fileName dataFile(base + ".000." + valueName);
OFstream os(dataFile); OFstream os(dataFile);
os.setf(ios_base::scientific, ios_base::floatfield); os.setf(ios_base::scientific, ios_base::floatfield);
os.precision(5); os.precision(5);
{
os << ensightPTraits<Type>::typeName << nl os << ensightPTraits<Type>::typeName << nl
<< "part" << nl << "part" << nl
<< setw(10) << 1 << nl << setw(10) << 1 << nl
<< "coordinates" << nl; << "coordinates" << nl;
for (direction i=0; i < pTraits<Type>::nComponents; ++i) for (direction d=0; d < pTraits<Type>::nComponents; ++d)
{ {
label cmpt = ensightPTraits<Type>::componentOrder[i]; const direction cmpt = ensightPTraits<Type>::componentOrder[d];
const scalarField fld(valueSets[setI]->component(cmpt)); for (const Type& val : fld)
forAll(fld, i)
{ {
if (mag(fld[i]) >= scalar(floatScalarVSMALL)) const float comp = narrowFloat(component(val, cmpt));
{ os << setw(12) << comp << nl;
os << setw(12) << fld[i] << nl;
}
else
{
os << setw(12) << scalar(0) << nl;
}
}
} }
} }
} }
@ -202,14 +192,15 @@ void Foam::ensightSetWriter<Type>::write
<< nl << nl
<< "VARIABLE" << "VARIABLE"
<< nl; << nl;
forAll(valueSetNames, setI)
for (const word& valueName : valueSetNames)
{ {
fileName dataFile(base + ".***." + valueSetNames[setI]); fileName dataFile(base + ".***." + valueName);
os.setf(ios_base::left); os.setf(ios_base::left);
os << ensightPTraits<Type>::typeName os << ensightPTraits<Type>::typeName
<< " per node: 1 " << " per node: 1 "
<< setw(15) << valueSetNames[setI] << setw(15) << valueName
<< " " << dataFile.name().c_str() << " " << dataFile.name().c_str()
<< nl; << nl;
} }
@ -233,30 +224,23 @@ void Foam::ensightSetWriter<Type>::write
<< "node id assign" << nl << "node id assign" << nl
<< "element id assign" << nl; << "element id assign" << nl;
forAll(tracks, trackI) forAll(tracks, tracki)
{ {
const coordSet& points = tracks[trackI]; const coordSet& points = tracks[tracki];
os << "part" << nl os << "part" << nl
<< setw(10) << trackI+1 << nl << setw(10) << tracki+1 << nl
<< "internalMesh" << nl << "internalMesh" << nl
<< "coordinates" << nl << "coordinates" << nl
<< setw(10) << points.size() << nl; << setw(10) << points.size() << nl;
for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++) for (direction cmpt = 0; cmpt < vector::nComponents; ++cmpt)
{ {
forAll(points, pointi) for (const point& p : points)
{
const scalar comp = points[pointi][cmpt];
if (mag(comp) >= scalar(floatScalarVSMALL))
{ {
const float comp = narrowFloat(p[cmpt]);
os << setw(12) << comp << nl; os << setw(12) << comp << nl;
} }
else
{
os << setw(12) << scalar(0) << nl;
}
}
} }
if (writeTracks) if (writeTracks)
@ -275,37 +259,35 @@ void Foam::ensightSetWriter<Type>::write
// Write data files // Write data files
forAll(valueSetNames, setI) forAll(valueSetNames, seti)
{ {
fileName dataFile(base + ".000." + valueSetNames[setI]); const word& valueName = valueSetNames[seti];
const List<Field<Type>>& fieldVals = valueSets[seti];
fileName dataFile(base + ".000." + valueName);
OFstream os(dataFile); OFstream os(dataFile);
os.setf(ios_base::scientific, ios_base::floatfield); os.setf(ios_base::scientific, ios_base::floatfield);
os.precision(5); os.precision(5);
{ {
os << ensightPTraits<Type>::typeName << nl; os << ensightPTraits<Type>::typeName << nl;
const List<Field<Type>>& fieldVals = valueSets[setI]; forAll(fieldVals, tracki)
forAll(fieldVals, trackI)
{ {
const Field<Type>& fld = fieldVals[tracki];
os << "part" << nl os << "part" << nl
<< setw(10) << trackI+1 << nl << setw(10) << tracki+1 << nl
<< "coordinates" << nl; << "coordinates" << nl;
for (direction i=0; i < pTraits<Type>::nComponents; ++i) for (direction d=0; d < pTraits<Type>::nComponents; ++d)
{ {
label cmpt = ensightPTraits<Type>::componentOrder[i]; const direction cmpt =
ensightPTraits<Type>::componentOrder[d];
const scalarField fld(fieldVals[trackI].component(cmpt)); for (const Type& val : fld)
forAll(fld, i)
{ {
if (mag(fld[i]) >= scalar(floatScalarVSMALL)) const float comp = narrowFloat(component(val, cmpt));
{ os << setw(12) << comp << nl;
os << setw(12) << fld[i] << nl;
}
else
{
os << setw(12) << scalar(0) << nl;
}
} }
} }
} }