Skip to content
Snippets Groups Projects
Commit 0dcd72bb authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: improve volRegion handling of moving meshes (#1194)

- implemented as lazy evaluation with an additional update() method.
  This avoids unnecessary changes until the values are actually
  required.

- apply mesh motion changes for momentum, volFieldValue,
  specieReactionRates function objects
parent 91bf6288
No related branches found
No related tags found
No related merge requests found
Showing
with 161 additions and 52 deletions
......@@ -109,6 +109,8 @@ void Foam::functionObjects::volRegion::calculateCache()
<< " Region has no cells"
<< exit(FatalError);
}
requireUpdate_ = false;
}
......@@ -137,6 +139,7 @@ Foam::functionObjects::volRegion::volRegion
)
:
volMesh_(mesh),
requireUpdate_(true),
cellIds_(),
nCells_(0),
V_(Zero),
......@@ -158,10 +161,7 @@ Foam::functionObjects::volRegion::volRegion
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::volRegion::read
(
const dictionary& dict
)
bool Foam::functionObjects::volRegion::read(const dictionary& dict)
{
switch (regionType_)
{
......@@ -195,6 +195,15 @@ bool Foam::functionObjects::volRegion::read
const Foam::labelList& Foam::functionObjects::volRegion::cellIDs() const
{
#ifdef FULLDEBUG
if (requireUpdate_)
{
FatalErrorInFunction
<< "Retrieving cached values that are not up-to-date" << nl
<< exit(FatalError);
}
#endif
switch (regionType_)
{
case vrtCellSet:
......@@ -213,15 +222,27 @@ const Foam::labelList& Foam::functionObjects::volRegion::cellIDs() const
}
bool Foam::functionObjects::volRegion::update()
{
if (requireUpdate_)
{
calculateCache();
return true;
}
return false;
}
void Foam::functionObjects::volRegion::updateMesh(const mapPolyMesh&)
{
calculateCache();
requireUpdate_ = true;
}
void Foam::functionObjects::volRegion::movePoints(const polyMesh&)
{
calculateCache();
requireUpdate_ = true;
}
......
......@@ -30,6 +30,11 @@ Group
Description
Volume (cell) region selection class.
The adjustments for mesh changes have been implemented with a lazy
evaluation, to avoid unnecessary recalculation until the values are
actually required. The update() method is used to ensure the cache
values are up-to-date.
Examples of function object specification:
\verbatim
volRegion0
......@@ -96,6 +101,9 @@ class volRegion
const fvMesh& volMesh_;
//- Flag to indicate whether the volRegion requires updating
bool requireUpdate_;
//- The cell ids, from cellSet
labelList cellIds_;
......@@ -178,6 +186,10 @@ public:
//- Return total volume of the selected region
inline scalar V() const;
//- Update the cached values as required
// \return False if the values were already up to date
bool update();
//- Read from dictionary
virtual bool read(const dictionary& dict);
......
......@@ -28,18 +28,45 @@ License
inline const Foam::functionObjects::volRegion::regionTypes&
Foam::functionObjects::volRegion::regionType() const
{
#ifdef FULLDEBUG
if (requireUpdate_)
{
FatalErrorInFunction
<< "Retrieving cached values that are not up-to-date" << nl
<< exit(FatalError);
}
#endif
return regionType_;
}
inline Foam::label Foam::functionObjects::volRegion::nCells() const
{
#ifdef FULLDEBUG
if (requireUpdate_)
{
FatalErrorInFunction
<< "Retrieving cached values that are not up-to-date" << nl
<< exit(FatalError);
}
#endif
return nCells_;
}
inline Foam::scalar Foam::functionObjects::volRegion::V() const
{
#ifdef FULLDEBUG
if (requireUpdate_)
{
FatalErrorInFunction
<< "Retrieving cached values that are not up-to-date" << nl
<< exit(FatalError);
}
#endif
return V_;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -229,12 +229,6 @@ Foam::functionObjects::fieldValues::volFieldValue::volFieldValue
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::fieldValues::volFieldValue::~volFieldValue()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::fieldValues::volFieldValue::read
......@@ -251,6 +245,8 @@ bool Foam::functionObjects::fieldValues::volFieldValue::read
bool Foam::functionObjects::fieldValues::volFieldValue::write()
{
volRegion::update(); // Ensure cached values are valid
fieldValue::write();
if (Pstream::master())
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -272,7 +272,7 @@ public:
//- Destructor
virtual ~volFieldValue();
virtual ~volFieldValue() = default;
// Public Member Functions
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2015-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -260,14 +260,12 @@ Foam::functionObjects::fieldValues::volFieldValue::filterField
const Field<Type>& field
) const
{
if (isNull(cellIDs()))
if (volRegion::vrtAll == this->volRegion::regionType())
{
return field;
}
else
{
return tmp<Field<Type>>::New(field, cellIDs());
}
return tmp<Field<Type>>::New(field, cellIDs());
}
......
......@@ -44,6 +44,16 @@ namespace functionObjects
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
void Foam::functionObjects::momentum::purgeFields()
{
objectRegistry& obr = const_cast<objectRegistry&>(obr_);
obr.erase(scopedName("momentum"));
obr.erase(scopedName("angularMomentum"));
obr.erase(scopedName("angularVelocity"));
}
template<class GeoField>
Foam::autoPtr<GeoField>
Foam::functionObjects::momentum::newField
......@@ -75,6 +85,13 @@ void Foam::functionObjects::momentum::calc()
{
initialise();
// Ensure volRegion is properly up-to-date.
// Purge old fields if anything has changed (eg, mesh size etc)
if (volRegion::update())
{
purgeFields();
}
// When field writing is not enabled we need our local storage
// for the momentum and angular velocity fields
autoPtr<volVectorField> tmomentum, tAngularMom, tAngularVel;
......@@ -292,22 +309,27 @@ void Foam::functionObjects::momentum::initialise()
void Foam::functionObjects::momentum::writeValues(Ostream& os)
{
Log << type() << " " << name() << " write:" << nl;
if (log)
{
Info<< type() << " " << name() << " write:" << nl;
Log << " Sum of Momentum";
Info<< " Sum of Momentum";
if (regionType_ != vrtAll)
{
Log << ' ' << regionTypeNames_[regionType_]
<< ' ' << regionName_;
}
if (regionType_ != vrtAll)
{
Info<< ' ' << regionTypeNames_[regionType_]
<< ' ' << regionName_;
}
Log << nl
<< " linear : " << sumMomentum_ << nl;
Info<< " (volume " << volRegion::V() << ')' << nl
<< " linear : " << sumMomentum_ << nl;
if (hasCsys_)
{
Log << " angular : " << sumAngularMom_ << nl;
if (hasCsys_)
{
Info<< " angular : " << sumAngularMom_ << nl;
}
Info<< endl;
}
if (writeToFile())
......@@ -323,8 +345,6 @@ void Foam::functionObjects::momentum::writeValues(Ostream& os)
os << tab << volRegion::V() << endl;
}
Log << endl;
}
......@@ -496,7 +516,7 @@ bool Foam::functionObjects::momentum::write()
{
if (writeMomentum_ || (hasCsys_ && (writeVelocity_ || writePosition_)))
{
Log <<"Writing fields" << nl;
Log << "Writing fields" << nl;
const volVectorField* fieldPtr;
......@@ -572,12 +592,14 @@ bool Foam::functionObjects::momentum::write()
void Foam::functionObjects::momentum::updateMesh(const mapPolyMesh& mpm)
{
volRegion::updateMesh(mpm);
purgeFields(); // Mesh motion makes calculated fields dubious
}
void Foam::functionObjects::momentum::movePoints(const polyMesh& pm)
{
volRegion::movePoints(pm);
purgeFields(); // Mesh motion makes calculated fields dubious
}
......
......@@ -134,6 +134,9 @@ class momentum
{
// Private Member Functions
//- Remove calculated fields from the registry
void purgeFields();
//- Calculate the fields and integral values
void calc();
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016-2017 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -82,14 +82,6 @@ specieReactionRates
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class ChemistryModelType>
Foam::functionObjects::specieReactionRates<ChemistryModelType>::
~specieReactionRates()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class ChemistryModelType>
......@@ -117,8 +109,11 @@ bool Foam::functionObjects::specieReactionRates<ChemistryModelType>::write()
const label nSpecie = chemistryModel_.nSpecie();
const label nReaction = chemistryModel_.nReaction();
// Region volume
const scalar V = this->V();
volRegion::update(); // Ensure cached values are valid
const scalar volTotal = this->volRegion::V();
const bool useAll = (volRegion::vrtAll == this->volRegion::regionType());
for (label ri=0; ri<nReaction; ri++)
{
......@@ -134,7 +129,7 @@ bool Foam::functionObjects::specieReactionRates<ChemistryModelType>::write()
scalar sumVRRi = 0;
if (isNull(cellIDs()))
if (useAll)
{
sumVRRi = fvc::domainIntegrate(RR).value();
}
......@@ -146,7 +141,7 @@ bool Foam::functionObjects::specieReactionRates<ChemistryModelType>::write()
);
}
file() << token::TAB << sumVRRi/V;
file() << token::TAB << sumVRRi / volTotal;
}
file() << nl;
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -101,13 +101,13 @@ public:
//- Destructor
virtual ~specieReactionRates();
virtual ~specieReactionRates() = default;
// Member Functions
//- Read the specieReactionRates data
virtual bool read(const dictionary&);
virtual bool read(const dictionary& dict);
//- Do nothing
virtual bool execute();
......
......@@ -75,6 +75,8 @@ functions
}
#};
}
#include "momentum"
}
// ************************************************************************* //
// -*- C++ -*-
// Calculate momentum fields
momentum
{
type momentum;
libs ("libfieldFunctionObjects.so");
log true;
writeControl writeTime;
// executeInterval 10;
// writeToFile true;
writeMomentum true;
writePosition true;
writeVelocity true;
// Cells to select (all/cellSet/cellZone)
regionType all;
// name c0;
cylindrical false;
origin (0 0 0);
rotation
{
type cylindrical;
axis (1 0 0); //< local Z
}
}
// ************************************************************************* //
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment