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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -27,6 +27,8 @@ License
\*---------------------------------------------------------------------------*/
#include "randomise.H"
#include "volFields.H"
#include "Random.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -43,17 +45,72 @@ namespace functionObjects
// * * * * * * * * * * * * * 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 processed = false;
// Ensure volRegion is properly up-to-date.
// Purge old fields if we need to etc.
(void)volRegion::update();
processed = processed || calcRandomised<scalar>();
processed = processed || calcRandomised<vector>();
processed = processed || calcRandomised<sphericalTensor>();
processed = processed || calcRandomised<symmTensor>();
processed = processed || calcRandomised<tensor>();
return processed;
return
(
calcTemplate<scalar>()
|| calcTemplate<vector>()
|| calcTemplate<sphericalTensor>()
|| calcTemplate<symmTensor>()
|| calcTemplate<tensor>()
);
}
@ -66,7 +123,8 @@ Foam::functionObjects::randomise::randomise
const dictionary& dict
)
:
fieldExpression(name, runTime, dict)
fieldExpression(name, runTime, dict),
volRegion(fieldExpression::mesh_, dict)
{
read(dict);
}
@ -77,6 +135,7 @@ Foam::functionObjects::randomise::randomise
bool Foam::functionObjects::randomise::read(const dictionary& dict)
{
fieldExpression::read(dict);
volRegion::read(dict);
dict.readEntry("magPerturbation", magPerturbation_);

View File

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

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 |
-------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -116,7 +116,7 @@ bool Foam::functionObjects::specieReactionRates<ChemistryModelType>::write()
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++)
{