ENH: add set/zone support to randomise function object (#2086)

This commit is contained in:
Mark Olesen
2021-05-11 19:51:07 +02:00
parent 3595e6f93c
commit 5eb48c443a
9 changed files with 134 additions and 142 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2016-2020 OpenCFD Ltd. Copyright (C) 2016-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -108,8 +108,8 @@ void Foam::functionObjects::volRegion::calculateCache()
{ {
FatalErrorInFunction FatalErrorInFunction
<< regionTypeNames_[regionType_] << regionTypeNames_[regionType_]
<< "(" << regionName_ << "):" << nl << '(' << regionName_ << "):" << nl
<< " Region has no cells" << " Region has no cells" << nl
<< exit(FatalError); << exit(FatalError);
} }
@ -127,7 +127,7 @@ void Foam::functionObjects::volRegion::writeFileHeader
{ {
wf.writeCommented(file, "Region"); wf.writeCommented(file, "Region");
file<< setw(1) << ':' << setw(1) << ' ' file<< setw(1) << ':' << setw(1) << ' '
<< regionTypeNames_[regionType_] << " " << regionName_ << endl; << regionTypeNames_[regionType_] << ' ' << regionName_ << endl;
wf.writeHeaderValue(file, "Cells", nCells_); wf.writeHeaderValue(file, "Cells", nCells_);
wf.writeHeaderValue(file, "Volume", V_); wf.writeHeaderValue(file, "Volume", V_);
} }
@ -184,7 +184,7 @@ bool Foam::functionObjects::volRegion::read(const dictionary& dict)
default: default:
{ {
FatalIOErrorInFunction(dict) FatalIOErrorInFunction(dict)
<< "Unknown region type. Valid region types are:" << "Unknown region type. Valid region types: "
<< flatOutput(regionTypeNames_.names()) << nl << flatOutput(regionTypeNames_.names()) << nl
<< exit(FatalIOError); << exit(FatalIOError);
break; break;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd. Copyright (C) 2016-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -64,7 +64,7 @@ Usage
\table \table
Property | Description | Required | Default Property | Description | Required | Default
regionType | Selection type: all/cellSet/cellZone | no | all regionType | Selection type: all/cellSet/cellZone | no | all
name | Name of cellSet/cellZone if required | no | name | Name of cellSet/cellZone if required | conditional |
\endtable \endtable
See also See also
@ -86,7 +86,7 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declarations // Forward Declarations
class fvMesh; class fvMesh;
class polyMesh; class polyMesh;
class mapPolyMesh; class mapPolyMesh;
@ -130,7 +130,7 @@ public:
//- Region type enumeration //- Region type enumeration
enum regionTypes enum regionTypes
{ {
vrtAll, //!< All cells vrtAll = 0, //!< All cells
vrtCellSet, //!< A cellSet vrtCellSet, //!< A cellSet
vrtCellZone //!< A cellZone vrtCellZone //!< A cellZone
}; };
@ -155,6 +155,9 @@ protected:
// Protected Member Functions // Protected Member Functions
//- Use all cells, not the cellIDs
inline bool useAllCells() const noexcept;
//- Output file header information //- Output file header information
void writeFileHeader(const writeFile& wf, Ostream& file) const; void writeFileHeader(const writeFile& wf, Ostream& file) const;
@ -177,13 +180,14 @@ public:
// Member Functions // Member Functions
//- Return the region type //- The region type
inline const regionTypes& regionType() const; inline regionTypes regionType() const noexcept;
//- Return the local list of cell IDs //- Return the local list of cell IDs.
// Empty or invalid for 'all'
const labelList& cellIDs() const; const labelList& cellIDs() const;
//- Return the number of cells selected in the region //- Return the total number of cells selected in the region
inline label nCells() const; inline label nCells() const;
//- Return total volume of the selected region //- Return total volume of the selected region

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,18 +28,15 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline const Foam::functionObjects::volRegion::regionTypes& inline bool Foam::functionObjects::volRegion::useAllCells() const noexcept
Foam::functionObjects::volRegion::regionType() const
{ {
#ifdef FULLDEBUG return (regionType_ == vrtAll);
if (requireUpdate_) }
{
FatalErrorInFunction
<< "Retrieving cached values that are not up-to-date" << nl
<< exit(FatalError);
}
#endif
inline Foam::functionObjects::volRegion::regionTypes
Foam::functionObjects::volRegion::regionType() const noexcept
{
return regionType_; return regionType_;
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2015-2020 OpenCFD Ltd. Copyright (C) 2015-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -336,7 +336,7 @@ Foam::functionObjects::fieldValues::volFieldValue::filterField
const Field<Type>& field const Field<Type>& field
) const ) const
{ {
if (volRegion::vrtAll == this->volRegion::regionType()) if (this->volRegion::useAllCells())
{ {
return field; return field;
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2020 OpenCFD Ltd. Copyright (C) 2018-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -200,26 +200,20 @@ void Foam::functionObjects::momentum::calc()
sumMomentum_ = Zero; sumMomentum_ = Zero;
sumAngularMom_ = Zero; sumAngularMom_ = Zero;
switch (regionType_) if (volRegion::useAllCells())
{
case vrtCellSet:
case vrtCellZone:
{
for (const label celli : cellIDs())
{
sumMomentum_ += momentum[celli];
sumAngularMom_ += angularMom[celli];
}
break;
}
case vrtAll:
{ {
for (label celli=0; celli < mesh_.nCells(); ++celli) for (label celli=0; celli < mesh_.nCells(); ++celli)
{ {
sumMomentum_ += momentum[celli]; sumMomentum_ += momentum[celli];
sumAngularMom_ += angularMom[celli]; sumAngularMom_ += angularMom[celli];
} }
break; }
else
{
for (const label celli : cellIDs())
{
sumMomentum_ += momentum[celli];
sumAngularMom_ += angularMom[celli];
} }
} }
@ -248,7 +242,7 @@ void Foam::functionObjects::momentum::writeFileHeader(Ostream& os)
writeHeader(os, "Momentum"); writeHeader(os, "Momentum");
} }
if (regionType_ != vrtAll) if (!volRegion::useAllCells())
{ {
writeHeader writeHeader
( (
@ -315,7 +309,7 @@ void Foam::functionObjects::momentum::writeValues(Ostream& os)
Info<< " Sum of Momentum"; Info<< " Sum of Momentum";
if (regionType_ != vrtAll) if (!volRegion::useAllCells())
{ {
Info<< ' ' << regionTypeNames_[regionType_] Info<< ' ' << regionTypeNames_[regionType_]
<< ' ' << regionName_; << ' ' << regionName_;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -27,6 +27,8 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "randomise.H" #include "randomise.H"
#include "volFields.H"
#include "Random.H"
#include "addToRunTimeSelectionTable.H" #include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -43,17 +45,72 @@ namespace functionObjects
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class Type>
bool Foam::functionObjects::randomise::calcTemplate()
{
typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
const auto* fieldPtr = cfindObject<VolFieldType>(fieldName_);
if (fieldPtr)
{
const auto& field = *fieldPtr;
resultName_ = fieldName_ & "Random";
auto trfield = tmp<VolFieldType>::New(field);
auto& rfield = trfield.ref();
Random rng(1234567);
auto applyPerturbation = [&](Type& cellval)
{
Type rndPert;
rng.randomise01(rndPert);
rndPert = 2.0*rndPert - pTraits<Type>::one;
rndPert /= mag(rndPert);
cellval += magPerturbation_*rndPert;
};
if (this->volRegion::useAllCells())
{
for (Type& cellval : rfield)
{
applyPerturbation(cellval);
}
}
else
{
for (const label celli : cellIDs())
{
applyPerturbation(rfield[celli]);
}
}
return store(resultName_, trfield);
}
return false;
}
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
bool Foam::functionObjects::randomise::calc() bool Foam::functionObjects::randomise::calc()
{ {
bool processed = false; // Ensure volRegion is properly up-to-date.
// Purge old fields if we need to etc.
(void)volRegion::update();
processed = processed || calcRandomised<scalar>(); return
processed = processed || calcRandomised<vector>(); (
processed = processed || calcRandomised<sphericalTensor>(); calcTemplate<scalar>()
processed = processed || calcRandomised<symmTensor>(); || calcTemplate<vector>()
processed = processed || calcRandomised<tensor>(); || calcTemplate<sphericalTensor>()
|| calcTemplate<symmTensor>()
return processed; || calcTemplate<tensor>()
);
} }
@ -66,7 +123,8 @@ Foam::functionObjects::randomise::randomise
const dictionary& dict const dictionary& dict
) )
: :
fieldExpression(name, runTime, dict) fieldExpression(name, runTime, dict),
volRegion(fieldExpression::mesh_, dict)
{ {
read(dict); read(dict);
} }
@ -77,6 +135,7 @@ Foam::functionObjects::randomise::randomise
bool Foam::functionObjects::randomise::read(const dictionary& dict) bool Foam::functionObjects::randomise::read(const dictionary& dict)
{ {
fieldExpression::read(dict); fieldExpression::read(dict);
volRegion::read(dict);
dict.readEntry("magPerturbation", magPerturbation_); dict.readEntry("magPerturbation", magPerturbation_);

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -69,8 +69,10 @@ Usage
Property | Description | Type | Req'd | Dflt Property | Description | Type | Req'd | Dflt
type | Type name: randomise | word | yes | - type | Type name: randomise | word | yes | -
libs | Library name: fieldFunctionObjects | word | yes | - libs | Library name: fieldFunctionObjects | word | yes | -
regionType | Selection type: all/cellSet/cellZone | word | no | all
magPerturbation | The magnitude of the perturbation | scalar | yes | - magPerturbation | The magnitude of the perturbation | scalar | yes | -
field | Name of the operand field | word | yes | - field | Name of the operand field | word | yes | -
name | Name of cellSet/cellZone if required | word | conditional | -
\endtable \endtable
The inherited entries are elaborated in: The inherited entries are elaborated in:
@ -90,7 +92,6 @@ See also
SourceFiles SourceFiles
randomise.C randomise.C
randomiseTemplates.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -98,6 +99,7 @@ SourceFiles
#define functionObjects_randomise_H #define functionObjects_randomise_H
#include "fieldExpression.H" #include "fieldExpression.H"
#include "volRegion.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -112,7 +114,8 @@ namespace functionObjects
class randomise class randomise
: :
public fieldExpression public fieldExpression,
public volRegion
{ {
// Private Data // Private Data
@ -122,16 +125,29 @@ class randomise
// Private Member Functions // Private Member Functions
//- Calculate the randomised field and register the result //- Calculate randomised field and register result
// Defined as file-local template implementation
template<class Type> template<class Type>
bool calcRandomised(); bool calcTemplate();
protected:
// Protected Member Functions
//- Calculate the randomised field and return true if successful //- Calculate the randomised field and return true if successful
virtual bool calc(); virtual bool calc();
//- No copy construct
randomise(const randomise&) = delete;
//- No copy assignment
void operator=(const randomise&) = delete;
public: public:
//- Runtime type information //- Runtime type information
TypeName("randomise"); TypeName("randomise");
@ -146,12 +162,6 @@ public:
const dictionary& dict const dictionary& dict
); );
//- No copy construct
randomise(const randomise&) = delete;
//- No copy assignment
void operator=(const randomise&) = delete;
//- Destructor //- Destructor
virtual ~randomise() = default; virtual ~randomise() = default;
@ -160,7 +170,7 @@ public:
// Member Functions // Member Functions
//- Read the randomise data //- Read the randomise data
virtual bool read(const dictionary&); virtual bool read(const dictionary& dict);
}; };
@ -171,12 +181,6 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "randomiseTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif #endif
// ************************************************************************* // // ************************************************************************* //

View File

@ -1,66 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation
-------------------------------------------------------------------------------
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 "volFields.H"
#include "Random.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
bool Foam::functionObjects::randomise::calcRandomised()
{
typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
if (foundObject<VolFieldType>(fieldName_, false))
{
const VolFieldType& field = lookupObject<VolFieldType>(fieldName_);
resultName_ = fieldName_ & "Random";
auto trfield = tmp<VolFieldType>::New(field);
auto& rfield = trfield.ref();
Random rand(1234567);
for (Type& cellval : rfield)
{
Type rndPert;
rand.randomise01(rndPert);
rndPert = 2.0*rndPert - pTraits<Type>::one;
rndPert /= mag(rndPert);
cellval += magPerturbation_*rndPert;
}
return store(resultName_, trfield);
}
return false;
}
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenFOAM Foundation Copyright (C) 2016-2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -116,7 +116,7 @@ bool Foam::functionObjects::specieReactionRates<ChemistryModelType>::write()
const scalar volTotal = this->volRegion::V(); const scalar volTotal = this->volRegion::V();
const bool useAll = (volRegion::vrtAll == this->volRegion::regionType()); const bool useAll = this->volRegion::useAllCells();
for (label ri=0; ri<nReaction; ri++) for (label ri=0; ri<nReaction; ri++)
{ {