Commit 8f0dfea0 authored by Mattijs Janssens's avatar Mattijs Janssens
Browse files

Merge branch 'feature-functionObjects' into 'develop'

Feature function objects

Lots of updates migrated from internal development line

### Updated functionObjectFile
Old code maintained a list of file pointers, and provided a clunky interface to output to file.  Although OK for function objects that only created a single file, those that created multiple files were required to refer to each by an index and making it very easy to introduce errors.
The new code simplifies the functionObjectFile class so that it provides helper functions to create and write to files, and no longer maintains a list of file pointers.  Instead, each function object must create and take responsibility for all of the file streams that it requires.
Changes propagated across dependencies:
* fieldValues
* forces/forceCoeffs
* yPlus
* fieldMinMax
* residuals
* regionSizeDistribution
* fieldMinMax
* cloudInfo

### Created new functionObjectState
Previously, if function objects required to store data on disk to enable smooth restarts, e.g. fieldAverage, the object would need to provide the mechanism for reading/writing state information itself.  This class abstracts out the reading/writing of state information in a straightforward manner, whereby an object can retrieve its own data, or retrieve e.g. the latest available data from another object (e.g. see fieldValueDelta).
* fieldAverage
* fieldMinMax
* forces
* forceCoeffs

### Created new runTimePostProcessing function object
New function object to generate images at run-time, or in 'post-processing' mode via the execFlowFunctionObjects utility
* Constant DataEntry - added construct from components
* Set and surface writers updated to enable retrieval of filename of generated output
* Additional changes to surface writers from internal line:
  + Ensight collate times option
  + Nastran output updated based on user feedback
  + new boundaryData output


See merge request !5
parents d25649c4 6ab06c57
......@@ -209,6 +209,7 @@ $(dll)/codedBase/codedBase.C
db/functionObjects/functionObject/functionObject.C
db/functionObjects/functionObjectList/functionObjectList.C
db/functionObjects/functionObjectFile/functionObjectFile.C
db/functionObjects/functionObjectState/functionObjectState.C
db/functionObjects/outputFilterOutputControl/outputFilterOutputControl.C
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -39,6 +39,7 @@ Foam::label Foam::functionObjectFile::addChars = 7;
void Foam::functionObjectFile::initStream(Ostream& os) const
{
os.setf(ios_base::scientific, ios_base::floatfield);
os.precision(writePrecision_);
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 =
obr_.time().timeName(obr_.time().startTime().value());
forAll(names_, i)
{
if (!filePtrs_.set(i))
{
fileName outputDir(baseFileDir()/prefix_/startTimeName);
mkDir(outputDir);
fileName outputDir(baseFileDir()/prefix_/startTimeName);
word fName(names_[i]);
mkDir(outputDir);
// Check if file already exists
IFstream is(outputDir/(fName + ".dat"));
if (is.good())
{
fName = fName + "_" + obr_.time().timeName();
}
word fName(name);
filePtrs_.set(i, new OFstream(outputDir/(fName + ".dat")));
initStream(filePtrs_[i]);
writeFileHeader(i);
}
// Check if file already exists
IFstream is(outputDir/(fName + ".dat"));
if (is.good())
{
fName = fName + "_" + obr_.time().timeName();
}
}
}
void Foam::functionObjectFile::writeFileHeader(const label i)
{}
void Foam::functionObjectFile::write()
{
createFiles();
}
osPtr.set(new OFstream(outputDir/(fName + ".dat")));
void Foam::functionObjectFile::resetNames(const wordList& names)
{
names_.clear();
names_.append(names);
if (Pstream::master())
{
filePtrs_.clear();
filePtrs_.setSize(names_.size());
createFiles();
initStream(osPtr());
}
return osPtr;
}
void Foam::functionObjectFile::resetName(const word& name)
void Foam::functionObjectFile::resetFile(const word& fileName)
{
names_.clear();
names_.append(name);
if (Pstream::master())
{
filePtrs_.clear();
filePtrs_.setSize(1);
createFiles();
}
fileName_ = fileName;
filePtr_ = createFile(fileName_);
}
......@@ -169,8 +136,10 @@ Foam::functionObjectFile::functionObjectFile
:
obr_(obr),
prefix_(prefix),
names_(),
filePtrs_()
fileName_("undefined"),
filePtr_(),
writePrecision_(IOstream::defaultPrecision()),
writeToFile_(true)
{}
......@@ -178,46 +147,22 @@ Foam::functionObjectFile::functionObjectFile
(
const objectRegistry& obr,
const word& prefix,
const word& name
const word& fileName,
const dictionary& dict
)
:
obr_(obr),
prefix_(prefix),
names_(),
filePtrs_()
fileName_(fileName),
filePtr_(),
writePrecision_(IOstream::defaultPrecision()),
writeToFile_(true)
{
names_.clear();
names_.append(name);
if (Pstream::master())
{
filePtrs_.clear();
filePtrs_.setSize(1);
// Cannot create files - need to access virtual function
}
}
read(dict);
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())
if (writeToFile_)
{
filePtrs_.clear();
filePtrs_.setSize(names_.size());
// Cannot create files - need to access virtual function
filePtr_ = createFile(fileName_);
}
}
......@@ -230,72 +175,38 @@ Foam::functionObjectFile::~functionObjectFile()
// * * * * * * * * * * * * * * * 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()
{
if (!Pstream::master())
{
FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()")
<< "Request for file() can only be done by the master process"
<< abort(FatalError);
}
if (filePtrs_.size() != 1)
if (!writeToFile_)
{
WarningIn("Foam::Ostream& Foam::functionObjectFile::file()")
<< "Requested single file, but multiple files are present"
<< endl;
return Snull;
}
if (!filePtrs_.set(0))
if (!filePtr_.valid())
{
FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()")
<< "File pointer at index " << 0 << " not allocated"
<< "File pointer not allocated"
<< abort(FatalError);
}
return filePtrs_[0];
return filePtr_();
}
Foam::PtrList<Foam::OFstream>& Foam::functionObjectFile::files()
bool Foam::functionObjectFile::writeToFile() const
{
if (!Pstream::master())
{
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];
return writeToFile_;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -57,6 +57,8 @@ namespace Foam
class functionObjectFile
{
private:
// Private data
//- Reference to the database
......@@ -65,15 +67,24 @@ class functionObjectFile
//- Prefix
const word prefix_;
//- File names
wordList names_;
//- Name of file
word fileName_;
//- File pointer
PtrList<OFstream> filePtrs_;
autoPtr<OFstream> filePtr_;
//- Write precision
label writePrecision_;
protected:
// Protected Data
//- Flag to enable/disable writing to file
bool writeToFile_;
// Protected Member Functions
//- Initialise the output stream for writing
......@@ -85,20 +96,11 @@ protected:
//- Return the base directory for the current time value
virtual fileName baseTimeDir() const;
//- Create the output file
virtual void createFiles();
//- File header information
virtual void writeFileHeader(const label i = 0);
//- Write function
virtual void write();
//- Reset the list of names from a wordList
virtual void resetNames(const wordList& names);
//- Return an autoPtr to a new file
virtual autoPtr<OFstream> createFile(const word& name) const;
//- Reset the list of names to a single name entry
virtual void resetName(const word& name);
//- Reset internal file pointer to new file with new name
virtual void resetFile(const word& name);
//- Return the value width when writing to stream with optional offset
virtual Omanip<int> valueWidth(const label offset = 0) const;
......@@ -118,26 +120,18 @@ public:
//- Additional characters for writing
static label addChars;
// Constructors
//- Construct null
functionObjectFile(const objectRegistry& obr, const word& prefix);
//- Construct from components
functionObjectFile
(
const objectRegistry& obr,
const word& prefix,
const word& name
);
//- Construct from components
//- Construct from components and read options from dictionary
functionObjectFile
(
const objectRegistry& obr,
const word& prefix,
const wordList& names
const word& fileName,
const dictionary& dict
);
......@@ -147,17 +141,14 @@ public:
// Member Functions
//- Return const access to the names
const wordList& names() const;
//- Read
void read(const dictionary& dict);
//- Return access to the file (if only 1)
OFstream& file();
//- Return access to the files
PtrList<OFstream>& files();
//- Return file 'i'
OFstream& file(const label i);
//- Return true if can write to file
bool writeToFile() const;
//- Write a commented string to stream
void writeCommented
......
......@@ -2,8 +2,8 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -29,6 +29,28 @@ License
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
void Foam::functionObjectList::createStateDict() const
{
// Cannot set the state dictionary on construction since Time has not
// been fully initialised
stateDictPtr_.reset
(
new IOdictionary
(
IOobject
(
"functionObjectProperties",
time_.timeName(),
"uniform"/word("functionObjects"),
time_,
IOobject::READ_IF_PRESENT,
IOobject::NO_WRITE
)
)
);
}
Foam::functionObject* Foam::functionObjectList::remove
(
const word& key,
......@@ -70,6 +92,7 @@ Foam::functionObjectList::functionObjectList
indices_(),
time_(t),
parentDict_(t.controlDict()),
stateDictPtr_(),
execution_(execution),
updated_(false)
{}
......@@ -87,6 +110,7 @@ Foam::functionObjectList::functionObjectList
indices_(),
time_(t),
parentDict_(parentDict),
stateDictPtr_(),
execution_(execution),
updated_(false)
{}
......@@ -100,6 +124,28 @@ Foam::functionObjectList::~functionObjectList()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::IOdictionary& Foam::functionObjectList::stateDict()
{
if (!stateDictPtr_.valid())
{
createStateDict();
}
return stateDictPtr_();
}
const Foam::IOdictionary& Foam::functionObjectList::stateDict() const
{
if (!stateDictPtr_.valid())
{
createStateDict();
}
return stateDictPtr_();
}
void Foam::functionObjectList::clear()
{
PtrList<functionObject>::clear();
......@@ -165,6 +211,22 @@ bool Foam::functionObjectList::execute(const bool forceWrite)
}
}
// Force writing of state dictionary after function object execution
if (time_.outputTime())
{
label oldPrecision = IOstream::precision_;
IOstream::precision_ = 16;
stateDictPtr_->writeObject
(
IOstream::ASCII,
IOstream::currentVersion,
time_.writeCompression()
);
IOstream::precision_ = oldPrecision;
}
return ok;
}
......@@ -234,6 +296,11 @@ bool Foam::functionObjectList::adjustTimeStep()
bool Foam::functionObjectList::read()
{
if (!stateDictPtr_.valid())
{
createStateDict();
}
bool ok = true;
updated_ = execution_;
......
......@@ -2,8 +2,8 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -43,6 +43,7 @@ SourceFiles
#include "functionObject.H"
#include "SHA1Digest.H"
#include "HashTable.H"
#include "IOdictionary.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -74,6 +75,9 @@ class functionObjectList
// functionObject specifications.
const dictionary& parentDict_;
//- Function object properties - stores state information
mutable autoPtr<IOdictionary> stateDictPtr_;
//- Switch for the execution of the functionObjects
bool execution_;
......@@ -83,6 +87,9 @@ class functionObjectList
// Private Member Functions
//- Create state dictionary
void createStateDict() const;
//- Remove and return the function object pointer by name,
// and returns the old index via the parameter.
// Returns a NULL pointer (and index -1) if it didn't exist.
......@@ -136,6 +143,12 @@ public:
//- Access to the functionObjects
using PtrList<functionObject>::operator[];
//- Return the state dictionary
IOdictionary& stateDict();
//- Return const access to the state dictionary
const IOdictionary& stateDict() const;
//- Clear the list of function objects
virtual void clear();
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/