mirror of
https://github.com/OpenFOAM/OpenFOAM-6.git
synced 2025-12-08 06:57:46 +00:00
functionObjects: rewritten to all be derived from 'functionObject'
- Avoids the need for the 'OutputFilterFunctionObject' wrapper
- Time-control for execution and writing is now provided by the
'timeControlFunctionObject' which instantiates the processing
'functionObject' and controls its operation.
- Alternative time-control functionObjects can now be written and
selected at run-time without the need to compile wrapped version of
EVERY existing functionObject which would have been required in the
old structure.
- The separation of 'execute' and 'write' functions is now formalized in the
'functionObject' base-class and all derived classes implement the
two functions.
- Unnecessary implementations of functions with appropriate defaults
in the 'functionObject' base-class have been removed reducing
clutter and simplifying implementation of new functionObjects.
- The 'coded' 'functionObject' has also been updated, simplified and tested.
- Further simplification is now possible by creating some general
intermediate classes derived from 'functionObject'.
This commit is contained in:
@ -0,0 +1,353 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
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/>.
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "fieldAverage.H"
|
||||
#include "volFields.H"
|
||||
#include "fieldAverageItem.H"
|
||||
#include "addToRunTimeSelectionTable.H"
|
||||
|
||||
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
namespace functionObjects
|
||||
{
|
||||
defineTypeNameAndDebug(fieldAverage, 0);
|
||||
addToRunTimeSelectionTable(functionObject, fieldAverage, dictionary);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
|
||||
|
||||
void Foam::functionObjects::fieldAverage::resetFields()
|
||||
{
|
||||
forAll(faItems_, i)
|
||||
{
|
||||
if (faItems_[i].mean())
|
||||
{
|
||||
if (obr_.found(faItems_[i].meanFieldName()))
|
||||
{
|
||||
obr_.checkOut(*obr_[faItems_[i].meanFieldName()]);
|
||||
}
|
||||
}
|
||||
|
||||
if (faItems_[i].prime2Mean())
|
||||
{
|
||||
if (obr_.found(faItems_[i].prime2MeanFieldName()))
|
||||
{
|
||||
obr_.checkOut(*obr_[faItems_[i].prime2MeanFieldName()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Foam::functionObjects::fieldAverage::initialize()
|
||||
{
|
||||
resetFields();
|
||||
|
||||
Info<< type() << " " << name() << ":" << nl;
|
||||
|
||||
// Add mean fields to the field lists
|
||||
forAll(faItems_, fieldi)
|
||||
{
|
||||
addMeanField<scalar>(fieldi);
|
||||
addMeanField<vector>(fieldi);
|
||||
addMeanField<sphericalTensor>(fieldi);
|
||||
addMeanField<symmTensor>(fieldi);
|
||||
addMeanField<tensor>(fieldi);
|
||||
}
|
||||
|
||||
// Add prime-squared mean fields to the field lists
|
||||
forAll(faItems_, fieldi)
|
||||
{
|
||||
addPrime2MeanField<scalar, scalar>(fieldi);
|
||||
addPrime2MeanField<vector, symmTensor>(fieldi);
|
||||
}
|
||||
|
||||
// ensure first averaging works unconditionally
|
||||
prevTimeIndex_ = -1;
|
||||
|
||||
Info<< endl;
|
||||
|
||||
initialised_ = true;
|
||||
}
|
||||
|
||||
|
||||
void Foam::functionObjects::fieldAverage::restart()
|
||||
{
|
||||
Info<< " Restarting averaging at time " << obr_.time().timeName()
|
||||
<< nl << endl;
|
||||
|
||||
totalIter_.clear();
|
||||
totalIter_.setSize(faItems_.size(), 1);
|
||||
|
||||
totalTime_.clear();
|
||||
totalTime_.setSize(faItems_.size(), obr_.time().deltaTValue());
|
||||
|
||||
initialize();
|
||||
}
|
||||
|
||||
|
||||
void Foam::functionObjects::fieldAverage::calcAverages()
|
||||
{
|
||||
if (!initialised_)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
const label currentTimeIndex = obr_.time().timeIndex();
|
||||
const scalar currentTime = obr_.time().value();
|
||||
|
||||
if (prevTimeIndex_ == currentTimeIndex)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
prevTimeIndex_ = currentTimeIndex;
|
||||
}
|
||||
|
||||
if (periodicRestart_ && currentTime > restartPeriod_*periodIndex_)
|
||||
{
|
||||
restart();
|
||||
periodIndex_++;
|
||||
}
|
||||
|
||||
Info<< type() << " " << name() << " output:" << nl;
|
||||
|
||||
Info<< " Calculating averages" << nl;
|
||||
|
||||
addMeanSqrToPrime2Mean<scalar, scalar>();
|
||||
addMeanSqrToPrime2Mean<vector, symmTensor>();
|
||||
|
||||
calculateMeanFields<scalar>();
|
||||
calculateMeanFields<vector>();
|
||||
calculateMeanFields<sphericalTensor>();
|
||||
calculateMeanFields<symmTensor>();
|
||||
calculateMeanFields<tensor>();
|
||||
|
||||
calculatePrime2MeanFields<scalar, scalar>();
|
||||
calculatePrime2MeanFields<vector, symmTensor>();
|
||||
|
||||
forAll(faItems_, fieldi)
|
||||
{
|
||||
totalIter_[fieldi]++;
|
||||
totalTime_[fieldi] += obr_.time().deltaTValue();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Foam::functionObjects::fieldAverage::writeAverages() const
|
||||
{
|
||||
Info<< " Writing average fields" << endl;
|
||||
|
||||
writeFields<scalar>();
|
||||
writeFields<vector>();
|
||||
writeFields<sphericalTensor>();
|
||||
writeFields<symmTensor>();
|
||||
writeFields<tensor>();
|
||||
}
|
||||
|
||||
|
||||
void Foam::functionObjects::fieldAverage::writeAveragingProperties() const
|
||||
{
|
||||
IOdictionary propsDict
|
||||
(
|
||||
IOobject
|
||||
(
|
||||
name() + "Properties",
|
||||
obr_.time().timeName(),
|
||||
"uniform",
|
||||
obr_,
|
||||
IOobject::NO_READ,
|
||||
IOobject::NO_WRITE,
|
||||
false
|
||||
)
|
||||
);
|
||||
|
||||
forAll(faItems_, fieldi)
|
||||
{
|
||||
const word& fieldName = faItems_[fieldi].fieldName();
|
||||
propsDict.add(fieldName, dictionary());
|
||||
propsDict.subDict(fieldName).add("totalIter", totalIter_[fieldi]);
|
||||
propsDict.subDict(fieldName).add("totalTime", totalTime_[fieldi]);
|
||||
}
|
||||
|
||||
propsDict.regIOobject::write();
|
||||
}
|
||||
|
||||
|
||||
void Foam::functionObjects::fieldAverage::readAveragingProperties()
|
||||
{
|
||||
totalIter_.clear();
|
||||
totalIter_.setSize(faItems_.size(), 1);
|
||||
|
||||
totalTime_.clear();
|
||||
totalTime_.setSize(faItems_.size(), obr_.time().deltaTValue());
|
||||
|
||||
if (restartOnRestart_ || restartOnOutput_)
|
||||
{
|
||||
Info<< " Starting averaging at time " << obr_.time().timeName()
|
||||
<< nl;
|
||||
}
|
||||
else
|
||||
{
|
||||
IOobject propsDictHeader
|
||||
(
|
||||
name() + "Properties",
|
||||
obr_.time().timeName(obr_.time().startTime().value()),
|
||||
"uniform",
|
||||
obr_,
|
||||
IOobject::MUST_READ_IF_MODIFIED,
|
||||
IOobject::NO_WRITE,
|
||||
false
|
||||
);
|
||||
|
||||
if (!propsDictHeader.headerOk())
|
||||
{
|
||||
Info<< " Starting averaging at time " << obr_.time().timeName()
|
||||
<< nl;
|
||||
return;
|
||||
}
|
||||
|
||||
IOdictionary propsDict(propsDictHeader);
|
||||
|
||||
Info<< " Restarting averaging for fields:" << nl;
|
||||
forAll(faItems_, fieldi)
|
||||
{
|
||||
const word& fieldName = faItems_[fieldi].fieldName();
|
||||
if (propsDict.found(fieldName))
|
||||
{
|
||||
dictionary fieldDict(propsDict.subDict(fieldName));
|
||||
|
||||
totalIter_[fieldi] = readLabel(fieldDict.lookup("totalIter"));
|
||||
totalTime_[fieldi] = readScalar(fieldDict.lookup("totalTime"));
|
||||
Info<< " " << fieldName
|
||||
<< " iters = " << totalIter_[fieldi]
|
||||
<< " time = " << totalTime_[fieldi] << nl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
Foam::functionObjects::fieldAverage::fieldAverage
|
||||
(
|
||||
const word& name,
|
||||
const Time& t,
|
||||
const dictionary& dict
|
||||
)
|
||||
:
|
||||
functionObject(name),
|
||||
obr_
|
||||
(
|
||||
t.lookupObject<objectRegistry>
|
||||
(
|
||||
dict.lookupOrDefault("region", polyMesh::defaultRegion)
|
||||
)
|
||||
),
|
||||
prevTimeIndex_(-1),
|
||||
restartOnRestart_(false),
|
||||
restartOnOutput_(false),
|
||||
periodicRestart_(false),
|
||||
restartPeriod_(GREAT),
|
||||
initialised_(false),
|
||||
faItems_(),
|
||||
totalIter_(),
|
||||
totalTime_(),
|
||||
periodIndex_(1)
|
||||
{
|
||||
if (!isA<fvMesh>(obr_))
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "objectRegistry is not an fvMesh" << exit(FatalError);
|
||||
}
|
||||
|
||||
read(dict);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
|
||||
|
||||
Foam::functionObjects::fieldAverage::~fieldAverage()
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
bool Foam::functionObjects::fieldAverage::read(const dictionary& dict)
|
||||
{
|
||||
initialised_ = false;
|
||||
|
||||
Info<< type() << " " << name() << ":" << nl;
|
||||
|
||||
dict.readIfPresent("restartOnRestart", restartOnRestart_);
|
||||
dict.readIfPresent("restartOnOutput", restartOnOutput_);
|
||||
dict.readIfPresent("periodicRestart", periodicRestart_);
|
||||
dict.lookup("fields") >> faItems_;
|
||||
|
||||
if (periodicRestart_)
|
||||
{
|
||||
dict.lookup("restartPeriod") >> restartPeriod_;
|
||||
}
|
||||
|
||||
readAveragingProperties();
|
||||
|
||||
Info<< endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Foam::functionObjects::fieldAverage::execute(const bool postProcess)
|
||||
{
|
||||
calcAverages();
|
||||
Info<< endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Foam::functionObjects::fieldAverage::write(const bool postProcess)
|
||||
{
|
||||
writeAverages();
|
||||
writeAveragingProperties();
|
||||
|
||||
if (restartOnOutput_)
|
||||
{
|
||||
restart();
|
||||
}
|
||||
|
||||
Info<< endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
Reference in New Issue
Block a user