Commit c9cf232a authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: add some convenience methods in ensightFile, ensightGeoFile

- Some commonly used write methods that are independent of
  the calling context (ie, 2D/3D data, geometry, fields)

- Provide singleton null() for ensightFile, ensightGeoFile.
  Can be used for MPI slaves that need a file reference for their
  methods, but will never write to it, but it is also reasonable
  to use an autoPtr with rawRef() for that as well.
parent 405210a4
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -138,18 +138,6 @@ protected:
//- Check for fully defined fields
bool isFieldDefined(const List<scalar>&) const;
//- Write the part header
void writeHeader(ensightFile&, bool withDescription=false) const;
//- Write a scalar field for idList
// A null reference for idList writes the perNode values
void writeFieldList
(
ensightFile& os,
const List<scalar>& field,
const labelUList& idList
) const;
//- Track points used
virtual localPoints calcLocalPoints() const
{
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -30,72 +30,6 @@ Description
#include "dictionary.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::ensightPart::writeHeader
(
ensightFile& os,
bool withDescription
) const
{
os.write("part");
os.newline();
os.write(number() + 1); // Ensight starts with 1
os.newline();
if (withDescription)
{
os.write(name());
os.newline();
}
}
void Foam::ensightPart::writeFieldList
(
ensightFile& os,
const List<scalar>& field,
const labelUList& idList
) const
{
if (notNull(idList))
{
forAll(idList, i)
{
if (idList[i] >= field.size() || std::isnan(field[idList[i]]))
{
os.writeUndef();
}
else
{
os.write(field[idList[i]]);
}
os.newline();
}
}
else
{
// no idList => perNode
forAll(field, i)
{
if (std::isnan(field[i]))
{
os.writeUndef();
}
else
{
os.write(field[i]);
}
os.newline();
}
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::ensightPart::reconstruct(Istream& is)
......@@ -176,12 +110,8 @@ void Foam::ensightPart::writeGeometry
const localPoints ptList = calcLocalPoints();
const labelUList& pointMap = ptList.list;
writeHeader(os, true);
// write points
os.writeKeyword("coordinates");
os.write(ptList.nPoints);
os.newline();
os.beginPart(number(), name());
os.beginCoordinates(ptList.nPoints);
for (direction cmpt=0; cmpt < point::nComponents; ++cmpt)
{
......
......@@ -41,7 +41,7 @@ void Foam::ensightPart::writeField
{
if (this->size() && field.size())
{
writeHeader(os);
os.beginPart(number());
if (perNode)
{
......@@ -49,7 +49,8 @@ void Foam::ensightPart::writeField
for (direction d=0; d < pTraits<Type>::nComponents; ++d)
{
label cmpt = ensightPTraits<Type>::componentOrder[d];
writeFieldList(os, field.component(cmpt), labelUList::null());
os.writeList(field.component(cmpt));
}
}
else
......@@ -65,7 +66,8 @@ void Foam::ensightPart::writeField
for (direction d=0; d < pTraits<Type>::nComponents; ++d)
{
label cmpt = ensightPTraits<Type>::componentOrder[d];
writeFieldList(os, field.component(cmpt), idList);
os.writeList(field.component(cmpt), idList);
}
}
}
......
......@@ -25,6 +25,7 @@ License
#include "ensightFile.H"
#include "error.H"
#include "UList.H"
#include <cstring>
#include <sstream>
......@@ -307,4 +308,76 @@ Foam::Ostream& Foam::ensightFile::writeBinaryHeader()
}
//
// Convenience Output Methods
//
void Foam::ensightFile::beginPart(const label index)
{
write("part");
newline();
write(index+1); // Ensight starts with 1
newline();
}
void Foam::ensightFile::beginParticleCoordinates(const label nparticles)
{
write("particle coordinates");
newline();
write(nparticles, 8); // unusual width
newline();
}
void Foam::ensightFile::writeList
(
const UList<scalar>& field
)
{
forAll(field, i)
{
if (std::isnan(field[i]))
{
writeUndef();
}
else
{
write(field[i]);
}
newline();
}
}
void Foam::ensightFile::writeList
(
const UList<scalar>& field,
const labelUList& idList
)
{
if (notNull(idList))
{
forAll(idList, i)
{
if (idList[i] >= field.size() || std::isnan(field[idList[i]]))
{
writeUndef();
}
else
{
write(field[idList[i]]);
}
newline();
}
}
else
{
// no idList => perNode
writeList(field);
}
}
// ************************************************************************* //
......@@ -38,6 +38,7 @@ Description
#include "ensightFileName.H"
#include "ensightVarName.H"
#include "UList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -80,9 +81,13 @@ class ensightFile
public:
// Forward declarations
class FileName;
class VarName;
// Static Member Functions
//- Return a null ensightFile
inline static const ensightFile& null()
{
return NullObjectRef<ensightFile>();
}
// Constructors
......@@ -172,6 +177,28 @@ public:
//- Add carriage return to ascii stream
void newline();
// Convenience Output Methods
//- Begin a part (0-based index).
void beginPart(const label index);
//- Begin a "particle coordinates" block (measured data)
void beginParticleCoordinates(const label nparticles);
//- Write a list of floats as "%12.5e" or as binary
// With carriage return after each value (ascii stream)
void writeList(const UList<scalar>& field);
//- Write an indirect list of scalars as "%12.5e" or as binary
// With carriage return after each value (ascii stream)
void writeList
(
const UList<scalar>& field,
const labelUList& idList
);
};
......
......@@ -35,17 +35,25 @@ License
void Foam::ensightGeoFile::initialize()
{
writeBinaryHeader();
// Description line 1
write("Ensight Geometry File");
newline();
// Description line 2
#ifdef OPENFOAM_PLUS
string desc2("Written by OpenFOAM+ " STRING_QUOTE(OPENFOAM_PLUS));
write(string("Written by OpenFOAM+ " STRING_QUOTE(OPENFOAM_PLUS)));
#else
string desc2("Written by OpenFOAM-" + string(Foam::FOAMversion));
write(string("Written by OpenFOAM-" + string(Foam::FOAMversion)));
#endif
writeBinaryHeader();
write("Ensight Geometry File"); newline(); // description line 1
write(desc2); newline(); // description line 2
write("node id assign"); newline();
write("element id assign"); newline();
newline();
write("node id assign");
newline();
write("element id assign");
newline();
}
......@@ -87,10 +95,36 @@ Foam::ensightGeoFile::~ensightGeoFile()
Foam::Ostream& Foam::ensightGeoFile::writeKeyword(const keyType& key)
{
// ensure we get ensightFile::write(const string&)
write(static_cast<const string&>(key)); newline();
write(static_cast<const string&>(key));
newline();
return *this;
}
//
// Convenience Output Methods
//
void Foam::ensightGeoFile::beginPart
(
const label index,
const string& description
)
{
beginPart(index);
write(description);
newline();
}
void Foam::ensightGeoFile::beginCoordinates(const label npoints)
{
write("coordinates");
newline();
write(npoints);
newline();
}
// ************************************************************************* //
......@@ -61,6 +61,15 @@ class ensightGeoFile
public:
// Static Member Functions
//- Return a null ensightGeoFile
inline static const ensightGeoFile& null()
{
return NullObjectRef<ensightGeoFile>();
}
// Constructors
//- Construct from pathname.
......@@ -89,6 +98,18 @@ public:
//- Write keyword with trailing newline
virtual Ostream& writeKeyword(const keyType& key);
// Convenience Output Methods
using ensightFile::beginPart;
//- Begin a "part" (0-based index), with a description.
void beginPart(const label index, const string& description);
//- Begin a "coordinates" block
void beginCoordinates(const label npoints);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -28,7 +28,7 @@ Description
Specification of a valid Ensight file-name.
Spaces must be quoted,
no '*' wildcards, not '%' (structured block continuation).
no '*' wildcards, no '%' (structured block continuation).
Overall line length within case file is limited to 1024, but this is not
yet addresssed.
......@@ -80,15 +80,16 @@ public:
//- Is this character valid for an ensight file-name
inline static bool valid(char);
// Member operators
// Assignment
// Assignment (disabled)
void operator=(const fileName&) = delete;
void operator=(const word&) = delete;
void operator=(const string&) = delete;
void operator=(const std::string&) = delete;
void operator=(const char*) = delete;
void operator=(const fileName&) = delete;
void operator=(const word&) = delete;
void operator=(const string&) = delete;
void operator=(const std::string&) = delete;
void operator=(const char*) = delete;
};
......
......@@ -63,10 +63,8 @@ class VarName
//- Strip invalid characters
inline void stripInvalid();
public:
// Constructors
//- Construct as copy
......@@ -87,12 +85,11 @@ public:
// Member operators
// Assignment
void operator=(const word&) = delete;
void operator=(const string&) = delete;
void operator=(const std::string&) = delete;
void operator=(const char*) = delete;
// Assignment (disabled)
void operator=(const word&) = delete;
void operator=(const string&) = delete;
void operator=(const std::string&) = delete;
void operator=(const char*) = delete;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment