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 -------------------------------------------------------------------------------
11 License
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 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
40  defineTypeNameAndDebug(randomise, 0);
41  addToRunTimeSelectionTable(functionObject, randomise, dictionary);
42 }
43 }
44 
45 
46 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47 
48 template<class Type>
49 bool 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 // ************************************************************************* //
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::functionObjects::volRegion::update
bool update()
Definition: volRegion.C:225
Foam::functionObjects::volRegion
Volume (cell) region selection class.
Definition: volRegion.H:111
Foam::functionObjects::randomise::randomise
randomise(const randomise &)=delete
Foam::read
bool read(const char *buf, int32_t &val)
Definition: int32.H:125
Foam::functionObjects::fieldExpression::read
virtual bool read(const dictionary &dict)
Definition: fieldExpression.C:86
Foam::functionObjects::regionFunctionObject::store
bool store(word &fieldName, const tmp< ObjectType > &tfield, bool cacheable=false)
Definition: regionFunctionObjectTemplates.C:100
Foam::functionObjects::volRegion::read
virtual bool read(const dictionary &dict)
Definition: volRegion.C:164
Foam::functionObjects::randomise::calc
virtual bool calc()
Definition: randomise.C:93
Foam::functionObjects::randomise::read
virtual bool read(const dictionary &dict)
Definition: randomise.C:128
field
rDeltaTY field()
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::functionObjects::fieldExpression::fieldName_
word fieldName_
Definition: fieldExpression.H:125
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:119
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Definition: atmBoundaryLayer.C:26
Foam::functionObjects::volRegion::useAllCells
bool useAllCells() const noexcept
Definition: volRegionI.H:24
Random.H
Foam::functionObject::scopedName
word scopedName(const word &name) const
Definition: functionObject.C:44
Foam::functionObjects::fieldExpression
Intermediate class for handling field expression function objects (e.g. blendingFactor etc....
Definition: fieldExpression.H:116
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::functionObjects::fvMeshFunctionObject::mesh_
const fvMesh & mesh_
Definition: fvMeshFunctionObject.H:69
Foam::functionObjects::volRegion::cellIDs
const labelList & cellIDs() const
Definition: volRegion.C:196
Foam::tmp::New
static tmp< T > New(Args &&... args)
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52
Foam::functionObjects::fieldExpression::resultName_
word resultName_
Definition: fieldExpression.H:128
randomise.H