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
This commit is contained in:
Mark Olesen
2019-02-06 10:25:47 +01:00
parent fb561daf7a
commit 8f572a5e71
12 changed files with 162 additions and 53 deletions

View File

@ -109,6 +109,8 @@ void Foam::functionObjects::volRegion::calculateCache()
<< " Region has no cells" << " Region has no cells"
<< exit(FatalError); << exit(FatalError);
} }
requireUpdate_ = false;
} }
@ -137,6 +139,7 @@ Foam::functionObjects::volRegion::volRegion
) )
: :
volMesh_(mesh), volMesh_(mesh),
requireUpdate_(true),
cellIds_(), cellIds_(),
nCells_(0), nCells_(0),
V_(Zero), V_(Zero),
@ -158,10 +161,7 @@ Foam::functionObjects::volRegion::volRegion
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::volRegion::read bool Foam::functionObjects::volRegion::read(const dictionary& dict)
(
const dictionary& dict
)
{ {
switch (regionType_) switch (regionType_)
{ {
@ -195,6 +195,15 @@ bool Foam::functionObjects::volRegion::read
const Foam::labelList& Foam::functionObjects::volRegion::cellIDs() const 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_) switch (regionType_)
{ {
case vrtCellSet: case vrtCellSet:
@ -213,15 +222,27 @@ const Foam::labelList& Foam::functionObjects::volRegion::cellIDs() const
} }
void Foam::functionObjects::volRegion::updateMesh(const mapPolyMesh&) bool Foam::functionObjects::volRegion::update()
{
if (requireUpdate_)
{ {
calculateCache(); calculateCache();
return true;
}
return false;
}
void Foam::functionObjects::volRegion::updateMesh(const mapPolyMesh&)
{
requireUpdate_ = true;
} }
void Foam::functionObjects::volRegion::movePoints(const polyMesh&) void Foam::functionObjects::volRegion::movePoints(const polyMesh&)
{ {
calculateCache(); requireUpdate_ = true;
} }

View File

@ -30,6 +30,11 @@ Group
Description Description
Volume (cell) region selection class. 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: Examples of function object specification:
\verbatim \verbatim
volRegion0 volRegion0
@ -96,6 +101,9 @@ class volRegion
const fvMesh& volMesh_; const fvMesh& volMesh_;
//- Flag to indicate whether the volRegion requires updating
bool requireUpdate_;
//- The cell ids, from cellSet //- The cell ids, from cellSet
labelList cellIds_; labelList cellIds_;
@ -178,6 +186,10 @@ public:
//- Return total volume of the selected region //- Return total volume of the selected region
inline scalar V() const; 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 //- Read from dictionary
virtual bool read(const dictionary& dict); virtual bool read(const dictionary& dict);

View File

@ -28,18 +28,45 @@ License
inline const Foam::functionObjects::volRegion::regionTypes& inline const Foam::functionObjects::volRegion::regionTypes&
Foam::functionObjects::volRegion::regionType() const 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_; return regionType_;
} }
inline Foam::label Foam::functionObjects::volRegion::nCells() const 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_; return nCells_;
} }
inline Foam::scalar Foam::functionObjects::volRegion::V() const 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_; return V_;
} }

View File

@ -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) 2011-2017 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -229,12 +229,6 @@ Foam::functionObjects::fieldValues::volFieldValue::volFieldValue
} }
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::fieldValues::volFieldValue::~volFieldValue()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::fieldValues::volFieldValue::read bool Foam::functionObjects::fieldValues::volFieldValue::read
@ -251,6 +245,8 @@ bool Foam::functionObjects::fieldValues::volFieldValue::read
bool Foam::functionObjects::fieldValues::volFieldValue::write() bool Foam::functionObjects::fieldValues::volFieldValue::write()
{ {
volRegion::update(); // Ensure cached values are valid
fieldValue::write(); fieldValue::write();
if (Pstream::master()) if (Pstream::master())

View File

@ -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) 2011-2017 OpenFOAM Foundation \\ / 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 License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -272,7 +272,7 @@ public:
//- Destructor //- Destructor
virtual ~volFieldValue(); virtual ~volFieldValue() = default;
// Public Member Functions // Public Member Functions

View File

@ -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) 2011-2017 OpenFOAM Foundation \\ / 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 License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -260,15 +260,13 @@ Foam::functionObjects::fieldValues::volFieldValue::filterField
const Field<Type>& field const Field<Type>& field
) const ) const
{ {
if (isNull(cellIDs())) if (volRegion::vrtAll == this->volRegion::regionType())
{ {
return field; return field;
} }
else
{
return tmp<Field<Type>>::New(field, cellIDs()); return tmp<Field<Type>>::New(field, cellIDs());
} }
}
// ************************************************************************* // // ************************************************************************* //

View File

@ -44,6 +44,16 @@ namespace functionObjects
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * * 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> template<class GeoField>
Foam::autoPtr<GeoField> Foam::autoPtr<GeoField>
Foam::functionObjects::momentum::newField Foam::functionObjects::momentum::newField
@ -75,6 +85,13 @@ void Foam::functionObjects::momentum::calc()
{ {
initialise(); 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 // When field writing is not enabled we need our local storage
// for the momentum and angular velocity fields // for the momentum and angular velocity fields
autoPtr<volVectorField> tmomentum, tAngularMom, tAngularVel; autoPtr<volVectorField> tmomentum, tAngularMom, tAngularVel;
@ -292,22 +309,27 @@ void Foam::functionObjects::momentum::initialise()
void Foam::functionObjects::momentum::writeValues(Ostream& os) 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) if (regionType_ != vrtAll)
{ {
Log << ' ' << regionTypeNames_[regionType_] Info<< ' ' << regionTypeNames_[regionType_]
<< ' ' << regionName_; << ' ' << regionName_;
} }
Log << nl Info<< " (volume " << volRegion::V() << ')' << nl
<< " linear : " << sumMomentum_ << nl; << " linear : " << sumMomentum_ << nl;
if (hasCsys_) if (hasCsys_)
{ {
Log << " angular : " << sumAngularMom_ << nl; Info<< " angular : " << sumAngularMom_ << nl;
}
Info<< endl;
} }
if (writeToFile()) if (writeToFile())
@ -323,8 +345,6 @@ void Foam::functionObjects::momentum::writeValues(Ostream& os)
os << tab << volRegion::V() << endl; os << tab << volRegion::V() << endl;
} }
Log << endl;
} }
@ -572,12 +592,14 @@ bool Foam::functionObjects::momentum::write()
void Foam::functionObjects::momentum::updateMesh(const mapPolyMesh& mpm) void Foam::functionObjects::momentum::updateMesh(const mapPolyMesh& mpm)
{ {
volRegion::updateMesh(mpm); volRegion::updateMesh(mpm);
purgeFields(); // Mesh motion makes calculated fields dubious
} }
void Foam::functionObjects::momentum::movePoints(const polyMesh& pm) void Foam::functionObjects::momentum::movePoints(const polyMesh& pm)
{ {
volRegion::movePoints(pm); volRegion::movePoints(pm);
purgeFields(); // Mesh motion makes calculated fields dubious
} }

View File

@ -134,6 +134,9 @@ class momentum
{ {
// Private Member Functions // Private Member Functions
//- Remove calculated fields from the registry
void purgeFields();
//- Calculate the fields and integral values //- Calculate the fields and integral values
void calc(); void calc();

View File

@ -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) 2016-2017 OpenFOAM Foundation \\ / A nd | Copyright (C) 2016-2017 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -82,14 +82,6 @@ specieReactionRates
} }
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class ChemistryModelType>
Foam::functionObjects::specieReactionRates<ChemistryModelType>::
~specieReactionRates()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class ChemistryModelType> template<class ChemistryModelType>
@ -117,8 +109,11 @@ bool Foam::functionObjects::specieReactionRates<ChemistryModelType>::write()
const label nSpecie = chemistryModel_.nSpecie(); const label nSpecie = chemistryModel_.nSpecie();
const label nReaction = chemistryModel_.nReaction(); const label nReaction = chemistryModel_.nReaction();
// Region volume volRegion::update(); // Ensure cached values are valid
const scalar V = this->V();
const scalar volTotal = this->volRegion::V();
const bool useAll = (volRegion::vrtAll == this->volRegion::regionType());
for (label ri=0; ri<nReaction; ri++) for (label ri=0; ri<nReaction; ri++)
{ {
@ -134,7 +129,7 @@ bool Foam::functionObjects::specieReactionRates<ChemistryModelType>::write()
scalar sumVRRi = 0; scalar sumVRRi = 0;
if (isNull(cellIDs())) if (useAll)
{ {
sumVRRi = fvc::domainIntegrate(RR).value(); 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; file() << nl;

View File

@ -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) 2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -101,13 +101,13 @@ public:
//- Destructor //- Destructor
virtual ~specieReactionRates(); virtual ~specieReactionRates() = default;
// Member Functions // Member Functions
//- Read the specieReactionRates data //- Read the specieReactionRates data
virtual bool read(const dictionary&); virtual bool read(const dictionary& dict);
//- Do nothing //- Do nothing
virtual bool execute(); virtual bool execute();

View File

@ -75,6 +75,8 @@ functions
} }
#}; #};
} }
#include "momentum"
} }
// ************************************************************************* // // ************************************************************************* //

View File

@ -0,0 +1,33 @@
// -*- 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
}
}
// ************************************************************************* //