randomise.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2016 OpenFOAM Foundation
9 Copyright (C) 2020-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "randomise.H"
30#include "volFields.H"
31#include "Random.H"
33
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35
36namespace Foam
37{
38namespace functionObjects
39{
42}
43}
44
45
46// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47
48template<class Type>
49bool Foam::functionObjects::randomise::calcTemplate()
50{
51 typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
52
53 const auto* fieldPtr = cfindObject<VolFieldType>(fieldName_);
54
55 if (fieldPtr)
56 {
57 const auto& field = *fieldPtr;
58
59 resultName_ = scopedName(fieldName_ & "Random");
60
61 auto trfield = tmp<VolFieldType>::New(field);
62 auto& rfield = trfield.ref();
63
64 Random rng(1234567);
65
66 auto applyPerturbation = [&](Type& cellval)
67 {
68 Type rndPert;
69 rng.randomise01(rndPert);
70 rndPert = 2.0*rndPert - pTraits<Type>::one;
71 rndPert /= mag(rndPert);
72
73 cellval += magPerturbation_*rndPert;
74 };
75
76 if (this->volRegion::useAllCells())
77 {
78 for (Type& cellval : rfield)
79 {
80 applyPerturbation(cellval);
81 }
82 }
83 else
84 {
85 for (const label celli : cellIDs())
86 {
87 applyPerturbation(rfield[celli]);
88 }
89 }
90
91 return store(resultName_, trfield);
92 }
93
94 return false;
95}
96
97
98// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
99
101{
102 // Ensure volRegion is properly up-to-date.
103 // Purge old fields if we need to etc.
104 (void)volRegion::update();
105
106 return
107 (
108 calcTemplate<scalar>()
109 || calcTemplate<vector>()
110 || calcTemplate<sphericalTensor>()
111 || calcTemplate<symmTensor>()
112 || calcTemplate<tensor>()
113 );
114}
115
116
117// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
118
120(
121 const word& name,
122 const Time& runTime,
123 const dictionary& dict
124)
125:
128{
129 read(dict);
130}
131
132
133// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
134
136{
139
140 dict.readEntry("magPerturbation", magPerturbation_);
141
142 return true;
143}
144
145
146// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
virtual bool read()
Re-read model coefficients if they have changed.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Abstract base-class for Time/database function objects.
word scopedName(const word &name) const
Return a scoped (prefixed) name.
Intermediate class for handling field expression function objects (e.g. blendingFactor etc....
word resultName_
Name of result field.
word fieldName_
Name of field to process.
Adds a random component to an input field, with a specified perturbation magnitude.
Definition: randomise.H:176
virtual bool calc()
Calculate the randomised field and return true if successful.
Definition: randomise.C:100
virtual bool read(const dictionary &dict)
Read the randomise data.
Definition: randomise.C:135
bool store(word &fieldName, const tmp< ObjectType > &tfield, bool cacheable=false)
Store the field in the (sub) objectRegistry under the given name.
Volume (cell) region selection class.
Definition: volRegion.H:116
bool useAllCells() const noexcept
Use all cells, not the cellIDs.
Definition: volRegionI.H:31
bool update()
Update the cached values as required.
Definition: volRegion.C:250
const labelList & cellIDs() const
Return the local list of cell IDs.
Definition: volRegion.C:210
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
rDeltaTY field()
engineTime & runTime
Namespace for OpenFOAM.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
dictionary dict