Loading...
Searching...
No Matches
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"
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{
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 }
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:
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.
Generic GeometricField class.
Random number generator.
Definition Random.H:56
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
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....
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
word resultName_
Name of result field.
word fieldName_
Name of field to process.
fieldExpression(const word &name, const Time &runTime, const dictionary &dict, const word &fieldName=word::null, const word &resultName=word::null)
Construct from name, Time and dictionary.
const fvMesh & mesh_
Reference to the fvMesh.
Adds a random component to an input field, with a specified perturbation magnitude.
Definition randomise.H:143
virtual bool calc()
Calculate the randomised field and return true if successful.
Definition randomise.C:93
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
Definition randomise.C:128
randomise(const randomise &)=delete
No copy construct.
const ObjectType * cfindObject(const word &fieldName) const
Return const pointer to the object (eg, a field) in the (sub) objectRegistry.
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:112
bool useAllCells() const noexcept
Use all cells, not the cellIDs.
Definition volRegionI.H:24
volRegion(const fvMesh &mesh, const dictionary &dict)
Construct from fvMesh and dictionary.
Definition volRegion.C:143
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition volRegion.C:171
bool update()
Update the cached values as required.
Definition volRegion.C:243
const labelList & cellIDs() const
Return the local list of cell IDs.
Definition volRegion.C:203
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
static tmp< T > New(Args &&... args)
Construct tmp with forwarding arguments.
Definition tmp.H:215
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
rDeltaTY field()
engineTime & runTime
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
dictionary dict