mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: Refactored functionObjectFile class
Class now provides helper functions to generate files on-the-fly by function objects, as opposed to attempting to control all files needed by the function object (earlier implementation lead to over-complication and was error prone)
This commit is contained in:
@ -3,7 +3,7 @@
|
|||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -39,6 +39,7 @@ Foam::label Foam::functionObjectFile::addChars = 7;
|
|||||||
void Foam::functionObjectFile::initStream(Ostream& os) const
|
void Foam::functionObjectFile::initStream(Ostream& os) const
|
||||||
{
|
{
|
||||||
os.setf(ios_base::scientific, ios_base::floatfield);
|
os.setf(ios_base::scientific, ios_base::floatfield);
|
||||||
|
os.precision(writePrecision_);
|
||||||
os.width(charWidth());
|
os.width(charWidth());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,78 +79,44 @@ Foam::fileName Foam::functionObjectFile::baseTimeDir() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Foam::functionObjectFile::createFiles()
|
Foam::autoPtr<Foam::OFstream> Foam::functionObjectFile::createFile
|
||||||
|
(
|
||||||
|
const word& name
|
||||||
|
) const
|
||||||
{
|
{
|
||||||
if (Pstream::master())
|
autoPtr<OFstream> osPtr;
|
||||||
|
|
||||||
|
if (Pstream::master() && writeToFile_)
|
||||||
{
|
{
|
||||||
const word startTimeName =
|
const word startTimeName =
|
||||||
obr_.time().timeName(obr_.time().startTime().value());
|
obr_.time().timeName(obr_.time().startTime().value());
|
||||||
|
|
||||||
forAll(names_, i)
|
fileName outputDir(baseFileDir()/prefix_/startTimeName);
|
||||||
|
|
||||||
|
mkDir(outputDir);
|
||||||
|
|
||||||
|
word fName(name);
|
||||||
|
|
||||||
|
// Check if file already exists
|
||||||
|
IFstream is(outputDir/(fName + ".dat"));
|
||||||
|
if (is.good())
|
||||||
{
|
{
|
||||||
if (!filePtrs_.set(i))
|
fName = fName + "_" + obr_.time().timeName();
|
||||||
{
|
|
||||||
fileName outputDir(baseFileDir()/prefix_/startTimeName);
|
|
||||||
mkDir(outputDir);
|
|
||||||
|
|
||||||
word fName(names_[i]);
|
|
||||||
|
|
||||||
// Check if file already exists
|
|
||||||
IFstream is(outputDir/(fName + ".dat"));
|
|
||||||
if (is.good())
|
|
||||||
{
|
|
||||||
fName = fName + "_" + obr_.time().timeName();
|
|
||||||
}
|
|
||||||
|
|
||||||
filePtrs_.set(i, new OFstream(outputDir/(fName + ".dat")));
|
|
||||||
|
|
||||||
initStream(filePtrs_[i]);
|
|
||||||
|
|
||||||
writeFileHeader(i);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
osPtr.set(new OFstream(outputDir/(fName + ".dat")));
|
||||||
|
|
||||||
|
initStream(osPtr());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return osPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Foam::functionObjectFile::writeFileHeader(const label i)
|
void Foam::functionObjectFile::resetFile(const word& fileName)
|
||||||
{}
|
|
||||||
|
|
||||||
|
|
||||||
void Foam::functionObjectFile::write()
|
|
||||||
{
|
{
|
||||||
createFiles();
|
fileName_ = fileName;
|
||||||
}
|
filePtr_ = createFile(fileName_);
|
||||||
|
|
||||||
|
|
||||||
void Foam::functionObjectFile::resetNames(const wordList& names)
|
|
||||||
{
|
|
||||||
names_.clear();
|
|
||||||
names_.append(names);
|
|
||||||
|
|
||||||
if (Pstream::master())
|
|
||||||
{
|
|
||||||
filePtrs_.clear();
|
|
||||||
filePtrs_.setSize(names_.size());
|
|
||||||
|
|
||||||
createFiles();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void Foam::functionObjectFile::resetName(const word& name)
|
|
||||||
{
|
|
||||||
names_.clear();
|
|
||||||
names_.append(name);
|
|
||||||
|
|
||||||
if (Pstream::master())
|
|
||||||
{
|
|
||||||
filePtrs_.clear();
|
|
||||||
filePtrs_.setSize(1);
|
|
||||||
|
|
||||||
createFiles();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -169,8 +136,10 @@ Foam::functionObjectFile::functionObjectFile
|
|||||||
:
|
:
|
||||||
obr_(obr),
|
obr_(obr),
|
||||||
prefix_(prefix),
|
prefix_(prefix),
|
||||||
names_(),
|
fileName_("undefined"),
|
||||||
filePtrs_()
|
filePtr_(),
|
||||||
|
writePrecision_(IOstream::defaultPrecision()),
|
||||||
|
writeToFile_(true)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
@ -178,46 +147,22 @@ Foam::functionObjectFile::functionObjectFile
|
|||||||
(
|
(
|
||||||
const objectRegistry& obr,
|
const objectRegistry& obr,
|
||||||
const word& prefix,
|
const word& prefix,
|
||||||
const word& name
|
const word& fileName,
|
||||||
|
const dictionary& dict
|
||||||
)
|
)
|
||||||
:
|
:
|
||||||
obr_(obr),
|
obr_(obr),
|
||||||
prefix_(prefix),
|
prefix_(prefix),
|
||||||
names_(),
|
fileName_(fileName),
|
||||||
filePtrs_()
|
filePtr_(),
|
||||||
|
writePrecision_(IOstream::defaultPrecision()),
|
||||||
|
writeToFile_(true)
|
||||||
{
|
{
|
||||||
names_.clear();
|
read(dict);
|
||||||
names_.append(name);
|
|
||||||
if (Pstream::master())
|
if (writeToFile_)
|
||||||
{
|
{
|
||||||
filePtrs_.clear();
|
filePtr_ = createFile(fileName_);
|
||||||
filePtrs_.setSize(1);
|
|
||||||
|
|
||||||
// Cannot create files - need to access virtual function
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Foam::functionObjectFile::functionObjectFile
|
|
||||||
(
|
|
||||||
const objectRegistry& obr,
|
|
||||||
const word& prefix,
|
|
||||||
const wordList& names
|
|
||||||
)
|
|
||||||
:
|
|
||||||
obr_(obr),
|
|
||||||
prefix_(prefix),
|
|
||||||
names_(names),
|
|
||||||
filePtrs_()
|
|
||||||
{
|
|
||||||
names_.clear();
|
|
||||||
names_.append(names);
|
|
||||||
if (Pstream::master())
|
|
||||||
{
|
|
||||||
filePtrs_.clear();
|
|
||||||
filePtrs_.setSize(names_.size());
|
|
||||||
|
|
||||||
// Cannot create files - need to access virtual function
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,72 +175,38 @@ Foam::functionObjectFile::~functionObjectFile()
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
const Foam::wordList& Foam::functionObjectFile::names() const
|
void Foam::functionObjectFile::read(const dictionary& dict)
|
||||||
{
|
{
|
||||||
return names_;
|
writePrecision_ =
|
||||||
|
dict.lookupOrDefault("writePrecision", IOstream::defaultPrecision());
|
||||||
|
|
||||||
|
// Only write on master process
|
||||||
|
writeToFile_ = dict.lookupOrDefault("writeToFile", true);
|
||||||
|
writeToFile_ = writeToFile_ && Pstream::master();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Foam::OFstream& Foam::functionObjectFile::file()
|
Foam::OFstream& Foam::functionObjectFile::file()
|
||||||
{
|
{
|
||||||
if (!Pstream::master())
|
if (!writeToFile_)
|
||||||
|
{
|
||||||
|
return Snull;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!filePtr_.valid())
|
||||||
{
|
{
|
||||||
FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()")
|
FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()")
|
||||||
<< "Request for file() can only be done by the master process"
|
<< "File pointer not allocated"
|
||||||
<< abort(FatalError);
|
<< abort(FatalError);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filePtrs_.size() != 1)
|
return filePtr_();
|
||||||
{
|
|
||||||
WarningIn("Foam::Ostream& Foam::functionObjectFile::file()")
|
|
||||||
<< "Requested single file, but multiple files are present"
|
|
||||||
<< endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!filePtrs_.set(0))
|
|
||||||
{
|
|
||||||
FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()")
|
|
||||||
<< "File pointer at index " << 0 << " not allocated"
|
|
||||||
<< abort(FatalError);
|
|
||||||
}
|
|
||||||
|
|
||||||
return filePtrs_[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Foam::PtrList<Foam::OFstream>& Foam::functionObjectFile::files()
|
bool Foam::functionObjectFile::writeToFile() const
|
||||||
{
|
{
|
||||||
if (!Pstream::master())
|
return writeToFile_;
|
||||||
{
|
|
||||||
FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::files()")
|
|
||||||
<< "Request for files() can only be done by the master process"
|
|
||||||
<< abort(FatalError);
|
|
||||||
}
|
|
||||||
|
|
||||||
return filePtrs_;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Foam::OFstream& Foam::functionObjectFile::file(const label i)
|
|
||||||
{
|
|
||||||
if (!Pstream::master())
|
|
||||||
{
|
|
||||||
FatalErrorIn
|
|
||||||
(
|
|
||||||
"Foam::OFstream& Foam::functionObjectFile::file(const label)"
|
|
||||||
)
|
|
||||||
<< "Request for file(i) can only be done by the master process"
|
|
||||||
<< abort(FatalError);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!filePtrs_.set(i))
|
|
||||||
{
|
|
||||||
FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()")
|
|
||||||
<< "File pointer at index " << i << " not allocated"
|
|
||||||
<< abort(FatalError);
|
|
||||||
}
|
|
||||||
|
|
||||||
return filePtrs_[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -57,6 +57,8 @@ namespace Foam
|
|||||||
|
|
||||||
class functionObjectFile
|
class functionObjectFile
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
// Private data
|
// Private data
|
||||||
|
|
||||||
//- Reference to the database
|
//- Reference to the database
|
||||||
@ -65,15 +67,24 @@ class functionObjectFile
|
|||||||
//- Prefix
|
//- Prefix
|
||||||
const word prefix_;
|
const word prefix_;
|
||||||
|
|
||||||
//- File names
|
//- Name of file
|
||||||
wordList names_;
|
word fileName_;
|
||||||
|
|
||||||
//- File pointer
|
//- File pointer
|
||||||
PtrList<OFstream> filePtrs_;
|
autoPtr<OFstream> filePtr_;
|
||||||
|
|
||||||
|
//- Write precision
|
||||||
|
label writePrecision_;
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
// Protected Data
|
||||||
|
|
||||||
|
//- Flag to enable/disable writing to file
|
||||||
|
bool writeToFile_;
|
||||||
|
|
||||||
|
|
||||||
// Protected Member Functions
|
// Protected Member Functions
|
||||||
|
|
||||||
//- Initialise the output stream for writing
|
//- Initialise the output stream for writing
|
||||||
@ -85,20 +96,11 @@ protected:
|
|||||||
//- Return the base directory for the current time value
|
//- Return the base directory for the current time value
|
||||||
virtual fileName baseTimeDir() const;
|
virtual fileName baseTimeDir() const;
|
||||||
|
|
||||||
//- Create the output file
|
//- Return an autoPtr to a new file
|
||||||
virtual void createFiles();
|
virtual autoPtr<OFstream> createFile(const word& name) const;
|
||||||
|
|
||||||
//- File header information
|
//- Reset internal file pointer to new file with new name
|
||||||
virtual void writeFileHeader(const label i = 0);
|
virtual void resetFile(const word& name);
|
||||||
|
|
||||||
//- Write function
|
|
||||||
virtual void write();
|
|
||||||
|
|
||||||
//- Reset the list of names from a wordList
|
|
||||||
virtual void resetNames(const wordList& names);
|
|
||||||
|
|
||||||
//- Reset the list of names to a single name entry
|
|
||||||
virtual void resetName(const word& name);
|
|
||||||
|
|
||||||
//- Return the value width when writing to stream with optional offset
|
//- Return the value width when writing to stream with optional offset
|
||||||
virtual Omanip<int> valueWidth(const label offset = 0) const;
|
virtual Omanip<int> valueWidth(const label offset = 0) const;
|
||||||
@ -118,26 +120,18 @@ public:
|
|||||||
//- Additional characters for writing
|
//- Additional characters for writing
|
||||||
static label addChars;
|
static label addChars;
|
||||||
|
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
//- Construct null
|
//- Construct null
|
||||||
functionObjectFile(const objectRegistry& obr, const word& prefix);
|
functionObjectFile(const objectRegistry& obr, const word& prefix);
|
||||||
|
|
||||||
//- Construct from components
|
//- Construct from components and read options from dictionary
|
||||||
functionObjectFile
|
functionObjectFile
|
||||||
(
|
(
|
||||||
const objectRegistry& obr,
|
const objectRegistry& obr,
|
||||||
const word& prefix,
|
const word& prefix,
|
||||||
const word& name
|
const word& fileName,
|
||||||
);
|
const dictionary& dict
|
||||||
|
|
||||||
//- Construct from components
|
|
||||||
functionObjectFile
|
|
||||||
(
|
|
||||||
const objectRegistry& obr,
|
|
||||||
const word& prefix,
|
|
||||||
const wordList& names
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
@ -147,17 +141,14 @@ public:
|
|||||||
|
|
||||||
// Member Functions
|
// Member Functions
|
||||||
|
|
||||||
//- Return const access to the names
|
//- Read
|
||||||
const wordList& names() const;
|
void read(const dictionary& dict);
|
||||||
|
|
||||||
//- Return access to the file (if only 1)
|
//- Return access to the file (if only 1)
|
||||||
OFstream& file();
|
OFstream& file();
|
||||||
|
|
||||||
//- Return access to the files
|
//- Return true if can write to file
|
||||||
PtrList<OFstream>& files();
|
bool writeToFile() const;
|
||||||
|
|
||||||
//- Return file 'i'
|
|
||||||
OFstream& file(const label i);
|
|
||||||
|
|
||||||
//- Write a commented string to stream
|
//- Write a commented string to stream
|
||||||
void writeCommented
|
void writeCommented
|
||||||
|
|||||||
Reference in New Issue
Block a user