From 5eb48c443a95369d4fe877b3cb2c3cbf603bf219 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 11 May 2021 19:51:07 +0200 Subject: [PATCH] ENH: add set/zone support to randomise function object (#2086) --- .../functionObjects/volRegion/volRegion.C | 10 +-- .../functionObjects/volRegion/volRegion.H | 20 +++-- .../functionObjects/volRegion/volRegionI.H | 19 ++--- .../volFieldValue/volFieldValueTemplates.C | 4 +- src/functionObjects/field/momentum/momentum.C | 32 +++----- .../field/randomise/randomise.C | 79 ++++++++++++++++--- .../field/randomise/randomise.H | 42 +++++----- .../field/randomise/randomiseTemplates.C | 66 ---------------- .../specieReactionRates/specieReactionRates.C | 4 +- 9 files changed, 134 insertions(+), 142 deletions(-) delete mode 100644 src/functionObjects/field/randomise/randomiseTemplates.C diff --git a/src/finiteVolume/functionObjects/volRegion/volRegion.C b/src/finiteVolume/functionObjects/volRegion/volRegion.C index 8cb833a98c..a8f1c4c8a8 100644 --- a/src/finiteVolume/functionObjects/volRegion/volRegion.C +++ b/src/finiteVolume/functionObjects/volRegion/volRegion.C @@ -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; diff --git a/src/finiteVolume/functionObjects/volRegion/volRegion.H b/src/finiteVolume/functionObjects/volRegion/volRegion.H index b0d0199e8e..38626fe6cd 100644 --- a/src/finiteVolume/functionObjects/volRegion/volRegion.H +++ b/src/finiteVolume/functionObjects/volRegion/volRegion.H @@ -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 diff --git a/src/finiteVolume/functionObjects/volRegion/volRegionI.H b/src/finiteVolume/functionObjects/volRegion/volRegionI.H index bf63780c71..3b3b68745b 100644 --- a/src/finiteVolume/functionObjects/volRegion/volRegionI.H +++ b/src/finiteVolume/functionObjects/volRegion/volRegionI.H @@ -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); - } - #endif + return (regionType_ == vrtAll); +} + +inline Foam::functionObjects::volRegion::regionTypes +Foam::functionObjects::volRegion::regionType() const noexcept +{ return regionType_; } diff --git a/src/functionObjects/field/fieldValues/volFieldValue/volFieldValueTemplates.C b/src/functionObjects/field/fieldValues/volFieldValue/volFieldValueTemplates.C index 974b1c3017..baf60816de 100644 --- a/src/functionObjects/field/fieldValues/volFieldValue/volFieldValueTemplates.C +++ b/src/functionObjects/field/fieldValues/volFieldValue/volFieldValueTemplates.C @@ -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& field ) const { - if (volRegion::vrtAll == this->volRegion::regionType()) + if (this->volRegion::useAllCells()) { return field; } diff --git a/src/functionObjects/field/momentum/momentum.C b/src/functionObjects/field/momentum/momentum.C index e6d0b76efa..618690f2a5 100644 --- a/src/functionObjects/field/momentum/momentum.C +++ b/src/functionObjects/field/momentum/momentum.C @@ -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_) + if (volRegion::useAllCells()) { - case vrtCellSet: - case vrtCellZone: + for (label celli=0; celli < mesh_.nCells(); ++celli) { - for (const label celli : cellIDs()) - { - sumMomentum_ += momentum[celli]; - sumAngularMom_ += angularMom[celli]; - } - break; + sumMomentum_ += momentum[celli]; + sumAngularMom_ += angularMom[celli]; } - case vrtAll: + } + else + { + for (const label celli : cellIDs()) { - for (label celli=0; celli < mesh_.nCells(); ++celli) - { - sumMomentum_ += momentum[celli]; - sumAngularMom_ += angularMom[celli]; - } - break; + 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_; diff --git a/src/functionObjects/field/randomise/randomise.C b/src/functionObjects/field/randomise/randomise.C index 219d1a7a48..acdc1aea87 100644 --- a/src/functionObjects/field/randomise/randomise.C +++ b/src/functionObjects/field/randomise/randomise.C @@ -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 +bool Foam::functionObjects::randomise::calcTemplate() +{ + typedef GeometricField VolFieldType; + + const auto* fieldPtr = cfindObject(fieldName_); + + if (fieldPtr) + { + const auto& field = *fieldPtr; + + resultName_ = fieldName_ & "Random"; + + auto trfield = tmp::New(field); + auto& rfield = trfield.ref(); + + Random rng(1234567); + + auto applyPerturbation = [&](Type& cellval) + { + Type rndPert; + rng.randomise01(rndPert); + rndPert = 2.0*rndPert - pTraits::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(); - processed = processed || calcRandomised(); - processed = processed || calcRandomised(); - processed = processed || calcRandomised(); - processed = processed || calcRandomised(); - - return processed; + return + ( + calcTemplate() + || calcTemplate() + || calcTemplate() + || calcTemplate() + || calcTemplate() + ); } @@ -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_); diff --git a/src/functionObjects/field/randomise/randomise.H b/src/functionObjects/field/randomise/randomise.H index 5ee9330d2b..74317e3ae1 100644 --- a/src/functionObjects/field/randomise/randomise.H +++ b/src/functionObjects/field/randomise/randomise.H @@ -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 | - + 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 - 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 // ************************************************************************* // diff --git a/src/functionObjects/field/randomise/randomiseTemplates.C b/src/functionObjects/field/randomise/randomiseTemplates.C deleted file mode 100644 index 0d3d1de1f2..0000000000 --- a/src/functionObjects/field/randomise/randomiseTemplates.C +++ /dev/null @@ -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 . - -\*---------------------------------------------------------------------------*/ - -#include "volFields.H" -#include "Random.H" - -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -template -bool Foam::functionObjects::randomise::calcRandomised() -{ - typedef GeometricField VolFieldType; - - if (foundObject(fieldName_, false)) - { - const VolFieldType& field = lookupObject(fieldName_); - - resultName_ = fieldName_ & "Random"; - - auto trfield = tmp::New(field); - auto& rfield = trfield.ref(); - - Random rand(1234567); - - for (Type& cellval : rfield) - { - Type rndPert; - rand.randomise01(rndPert); - rndPert = 2.0*rndPert - pTraits::one; - rndPert /= mag(rndPert); - - cellval += magPerturbation_*rndPert; - } - - return store(resultName_, trfield); - } - - return false; -} - - -// ************************************************************************* // diff --git a/src/thermophysicalModels/chemistryModel/functionObjects/specieReactionRates/specieReactionRates.C b/src/thermophysicalModels/chemistryModel/functionObjects/specieReactionRates/specieReactionRates.C index 272b358760..bf3c3ddd0c 100644 --- a/src/thermophysicalModels/chemistryModel/functionObjects/specieReactionRates/specieReactionRates.C +++ b/src/thermophysicalModels/chemistryModel/functionObjects/specieReactionRates/specieReactionRates.C @@ -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::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