ENH: surface writers now track their own write status

- instead of deciding beforehand if a surface format requires a separate
  geometry file (or if a geometry file should be written if no fields were
  written) now determine afterwards if something was written.

  This improves the overall reliability (consistency) and is more
  convenient for the caller as well.
This commit is contained in:
Mark Olesen
2019-03-13 14:13:04 +01:00
parent 15a224ec4c
commit 044876bfe9
18 changed files with 96 additions and 30 deletions

View File

@ -536,6 +536,7 @@ bool Foam::sampledSurfaces::performAction(unsigned request)
// Only seems to be needed for VTK legacy // Only seems to be needed for VTK legacy
countFields(); countFields();
// Update writers // Update writers
forAll(*this, surfi) forAll(*this, surfi)
@ -544,7 +545,6 @@ bool Foam::sampledSurfaces::performAction(unsigned request)
if (((request & actions_[surfi]) & ACTION_WRITE) && nFaces_[surfi]) if (((request & actions_[surfi]) & ACTION_WRITE) && nFaces_[surfi])
{ {
// Output writers
surfaceWriter& outWriter = writers_[surfi]; surfaceWriter& outWriter = writers_[surfi];
if (outWriter.needsUpdate()) if (outWriter.needsUpdate())
@ -556,24 +556,14 @@ bool Foam::sampledSurfaces::performAction(unsigned request)
outWriter.beginTime(obr_.time()); outWriter.beginTime(obr_.time());
// Write original ids
// Write geometry if no fields would otherwise be written
if (!outWriter.nFields() || outWriter.separateGeometry())
{
outWriter.write();
continue;
}
// Write original ids - as label or scalar field
const word fieldName("Ids");
if (s.hasFaceIds() && !s.interpolate()) if (s.hasFaceIds() && !s.interpolate())
{ {
writeSurface writeSurface
( (
outWriter, outWriter,
Field<label>(s.originalIds()), Field<label>(s.originalIds()),
fieldName "Ids"
); );
} }
} }
@ -610,8 +600,16 @@ bool Foam::sampledSurfaces::performAction(unsigned request)
// Finish this time step // Finish this time step
forAll(writers_, surfi) forAll(writers_, surfi)
{ {
if ((request & actions_[surfi]) & ACTION_WRITE) if (((request & actions_[surfi]) & ACTION_WRITE) && nFaces_[surfi])
{ {
// Write geometry if no fields were written so that we still
// can have something to look at
if (!writers_[surfi].wroteData())
{
writers_[surfi].write();
}
writers_[surfi].endTime(); writers_[surfi].endTime();
} }
} }

View File

@ -352,19 +352,19 @@ public:
void verbose(const bool verbosity = true); void verbose(const bool verbosity = true);
//- Read the sampledSurfaces dictionary //- Read the sampledSurfaces dictionary
virtual bool read(const dictionary&); virtual bool read(const dictionary& dict);
//- Execute, currently does nothing //- Sample and store if the sampleOnExecute is enabled.
virtual bool execute(); virtual bool execute();
//- Sample and write //- Sample and write
virtual bool write(); virtual bool write();
//- Update for changes of mesh - expires the surfaces //- Update for changes of mesh - expires the surfaces
virtual void updateMesh(const mapPolyMesh&); virtual void updateMesh(const mapPolyMesh& mpm);
//- Update for mesh point-motion - expires the surfaces //- Update for mesh point-motion - expires the surfaces
virtual void movePoints(const polyMesh&); virtual void movePoints(const polyMesh& mesh);
//- Update for changes of mesh due to readUpdate - expires the surfaces //- Update for changes of mesh due to readUpdate - expires the surfaces
virtual void readUpdate(const polyMesh::readUpdateState state); virtual void readUpdate(const polyMesh::readUpdateState state);
@ -372,7 +372,7 @@ public:
//- Get merge tolerance //- Get merge tolerance
static scalar mergeTol(); static scalar mergeTol();
//- Set tolerance (and return old tolerance) //- Set tolerance and return old tolerance
static scalar mergeTol(const scalar tol); static scalar mergeTol(const scalar tol);
}; };

View File

@ -159,6 +159,7 @@ Foam::fileName Foam::surfaceWriters::boundaryDataWriter::write()
//pts.writeEndDivider(os); //pts.writeEndDivider(os);
} }
wroteGeom_ = true;
return surfaceDir; return surfaceDir;
} }
@ -264,6 +265,7 @@ Foam::fileName Foam::surfaceWriters::boundaryDataWriter::writeTemplate
OFstream(outputFile)() << tfield(); OFstream(outputFile)() << tfield();
} }
wroteGeom_ = true;
return surfaceDir; return surfaceDir;
} }

View File

@ -185,6 +185,7 @@ Foam::fileName Foam::surfaceWriters::ensightWriter::write()
// { // {
// return writeUncollated(); // return writeUncollated();
// } // }
return writeUncollated(); return writeUncollated();
} }

View File

@ -34,7 +34,8 @@ Foam::fileName Foam::surfaceWriters::ensightWriter::writeCollated()
// Geometry: rootdir/surfaceName/surfaceName.case // Geometry: rootdir/surfaceName/surfaceName.case
// Geometry: rootdir/surfaceName/surfaceName.mesh // Geometry: rootdir/surfaceName/surfaceName.mesh
return fileName(); wroteGeom_ = true;
return fileName::null;
} }
@ -368,7 +369,7 @@ Foam::fileName Foam::surfaceWriters::ensightWriter::writeCollated
} }
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }

View File

@ -97,6 +97,7 @@ Foam::fileName Foam::surfaceWriters::ensightWriter::writeUncollated()
osGeom << ensPart; osGeom << ensPart;
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }
@ -245,6 +246,7 @@ Foam::fileName Foam::surfaceWriters::ensightWriter::writeUncollated
} }
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }

View File

@ -142,6 +142,7 @@ Foam::fileName Foam::surfaceWriters::foamWriter::write()
OFstream(surfaceDir/"faceCentres")() << faceCentres; OFstream(surfaceDir/"faceCentres")() << faceCentres;
} }
wroteGeom_ = true;
return surfaceDir; return surfaceDir;
} }
@ -155,6 +156,12 @@ Foam::fileName Foam::surfaceWriters::foamWriter::writeTemplate
const Field<Type>& localValues const Field<Type>& localValues
) )
{ {
// Separate geometry
if (!wroteGeom_)
{
write();
}
checkOpen(); checkOpen();
// Geometry should already have been written // Geometry should already have been written
@ -197,6 +204,7 @@ Foam::fileName Foam::surfaceWriters::foamWriter::writeTemplate
OFstream(outputFile)() << tfield(); OFstream(outputFile)() << tfield();
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }

View File

@ -437,6 +437,7 @@ Foam::fileName Foam::surfaceWriters::nastranWriter::write()
<< "ENDDATA" << nl; << "ENDDATA" << nl;
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }

View File

@ -255,6 +255,7 @@ Foam::fileName Foam::surfaceWriters::nastranWriter::writeTemplate
<< "ENDDATA" << endl; << "ENDDATA" << endl;
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }

View File

@ -87,6 +87,12 @@ bool Foam::surfaceWriters::nullWriter::needsUpdate() const
} }
bool Foam::surfaceWriters::nullWriter::wroteData() const
{
return true;
}
bool Foam::surfaceWriters::nullWriter::enabled() const bool Foam::surfaceWriters::nullWriter::enabled() const
{ {
return false; return false;
@ -116,6 +122,7 @@ void Foam::surfaceWriters::nullWriter::open(const fileName& outputPath)
Foam::fileName Foam::surfaceWriters::nullWriter::write() Foam::fileName Foam::surfaceWriters::nullWriter::write()
{ {
wroteGeom_ = true;
return fileName::null; return fileName::null;
} }

View File

@ -94,10 +94,13 @@ public:
// Capability // Capability
//- Never needs an update. //- False: never needs an update.
virtual bool needsUpdate() const; virtual bool needsUpdate() const;
//- The null writer is always disabled, which lets the caller //- True: like a /dev/null device.
virtual bool wroteData() const;
//- False: The null writer is never enabled, which lets the caller
//- skip various (possibly expensive) preparatory operations. //- skip various (possibly expensive) preparatory operations.
virtual bool enabled() const; virtual bool enabled() const;

View File

@ -135,6 +135,7 @@ Foam::fileName Foam::surfaceWriters::proxyWriter::write()
); );
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }

View File

@ -178,6 +178,7 @@ Foam::fileName Foam::surfaceWriters::rawWriter::write()
os << nl; os << nl;
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }

View File

@ -197,6 +197,7 @@ Foam::fileName Foam::surfaceWriters::rawWriter::writeTemplate
} }
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }

View File

@ -145,6 +145,7 @@ Foam::fileName Foam::surfaceWriters::starcdWriter::write()
).write(outputFile, "inp"); ).write(outputFile, "inp");
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }
@ -158,6 +159,12 @@ Foam::fileName Foam::surfaceWriters::starcdWriter::writeTemplate
const Field<Type>& localValues const Field<Type>& localValues
) )
{ {
// Separate geometry
if (!wroteGeom_)
{
write();
}
checkOpen(); checkOpen();
// Field: rootdir/<TIME>/<field>_surfaceName.usr // Field: rootdir/<TIME>/<field>_surfaceName.usr
@ -206,6 +213,7 @@ Foam::fileName Foam::surfaceWriters::starcdWriter::writeTemplate
} }
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }

View File

@ -138,6 +138,7 @@ Foam::surfaceWriter::surfaceWriter()
surfComp_(), surfComp_(),
useComponents_(false), useComponents_(false),
upToDate_(false), upToDate_(false),
wroteGeom_(false),
parallel_(true), parallel_(true),
useTimeDir_(false), useTimeDir_(false),
isPointData_(false), isPointData_(false),
@ -244,6 +245,7 @@ void Foam::surfaceWriter::endTime()
void Foam::surfaceWriter::open(const fileName& outputPath) void Foam::surfaceWriter::open(const fileName& outputPath)
{ {
outputPath_ = outputPath; outputPath_ = outputPath;
wroteGeom_ = false;
} }
@ -302,6 +304,7 @@ void Foam::surfaceWriter::open
void Foam::surfaceWriter::close() void Foam::surfaceWriter::close()
{ {
outputPath_.clear(); outputPath_.clear();
wroteGeom_ = false;
} }
@ -369,11 +372,18 @@ bool Foam::surfaceWriter::needsUpdate() const
} }
bool Foam::surfaceWriter::wroteData() const
{
return wroteGeom_;
}
bool Foam::surfaceWriter::expire() bool Foam::surfaceWriter::expire()
{ {
const bool changed = upToDate_; const bool changed = upToDate_;
upToDate_ = false; upToDate_ = false;
wroteGeom_ = false;
nFields_ = 0; nFields_ = 0;
merged_.clear(); merged_.clear();
@ -443,6 +453,11 @@ bool Foam::surfaceWriter::merge() const
} }
upToDate_ = true; upToDate_ = true;
if (changed)
{
wroteGeom_ = false;
}
return changed; return changed;
} }

View File

@ -56,10 +56,14 @@ Description
verbose | Additional output verbosity | no | no verbose | Additional output verbosity | no | no
\endtable \endtable
Note
For surface formats that require geometry in a separate file,
it is the responsibility of the implementation (not the caller)
to ensure that this occurs.
SourceFiles SourceFiles
sampledWriter.C surfaceWriter.C
sampledWriterI.H surfaceWriterI.H
sampledWriterImpl.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -83,7 +87,7 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declarations // Forward Declarations
class Time; class Time;
class surfaceWriter; class surfaceWriter;
@ -118,10 +122,13 @@ protected:
//- The topology/surface is up-to-date? //- The topology/surface is up-to-date?
mutable bool upToDate_; mutable bool upToDate_;
//- Writing in parallel (vai master) //- Track if geometry has been written since the last open
mutable bool wroteGeom_;
//- Writing in parallel (via master)
bool parallel_; bool parallel_;
//- Writer should do something funny //- Insert additional time sub-directory in the output path
bool useTimeDir_; bool useTimeDir_;
//- Is point vs cell data //- Is point vs cell data
@ -185,13 +192,17 @@ protected:
const Field<Type>& localValues //!< Local field values to write const Field<Type>& localValues //!< Local field values to write
) )
{ {
if (!wroteGeom_)
{
return this->write();
}
return fileName::null; return fileName::null;
} }
public: public:
// Public data // Public Data
//- The default merge dimension (1e-8) //- The default merge dimension (1e-8)
static scalar defaultMergeDim; static scalar defaultMergeDim;
@ -294,6 +305,9 @@ public:
//- Does the writer need an update (eg, lagging behind surface changes) //- Does the writer need an update (eg, lagging behind surface changes)
virtual bool needsUpdate() const; virtual bool needsUpdate() const;
//- Geometry or fields written since the last open?
virtual bool wroteData() const;
//- Mark that surface changed and the writer will need an update, //- Mark that surface changed and the writer will need an update,
//- and set nFields = 0. //- and set nFields = 0.
// May also free up unneeded data. // May also free up unneeded data.

View File

@ -248,6 +248,7 @@ Foam::fileName Foam::surfaceWriters::vtkWriter::write()
writer_->writeGeometry(); writer_->writeGeometry();
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }
@ -288,6 +289,7 @@ Foam::fileName Foam::surfaceWriters::vtkWriter::writeTemplate
writer_->write(fieldName, tfield()); writer_->write(fieldName, tfield());
} }
wroteGeom_ = true;
return outputFile; return outputFile;
} }