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:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObjects::volRegion::update
bool update()
Update the cached values as required.
Definition: volRegion.C:232
Foam::functionObjects::volRegion
Volume (cell) region selection class.
Definition: volRegion.H:115
Foam::functionObjects::randomise::randomise
randomise(const randomise &)=delete
No copy construct.
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::functionObjects::fieldExpression::read
virtual bool read(const dictionary &dict)
Read the fieldExpression data.
Definition: fieldExpression.C:93
Foam::functionObjects::regionFunctionObject::store
bool store(word &fieldName, const tmp< ObjectType > &tfield, bool cacheable=false)
Store the field in the (sub) objectRegistry under the given name.
Definition: regionFunctionObjectTemplates.C:107
Foam::functionObjects::volRegion::read
virtual bool read(const dictionary &dict)
Read from dictionary.
Definition: volRegion.C:171
Foam::functionObjects::randomise::calc
virtual bool calc()
Calculate the randomised field and return true if successful.
Definition: randomise.C:100
Foam::functionObjects::randomise::read
virtual bool read(const dictionary &dict)
Read the randomise data.
Definition: randomise.C:135
field
rDeltaTY field()
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::functionObjects::fieldExpression::fieldName_
word fieldName_
Name of field to process.
Definition: fieldExpression.H:129
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::volRegion::useAllCells
bool useAllCells() const noexcept
Use all cells, not the cellIDs.
Definition: volRegionI.H:31
Random.H
Foam::functionObject::scopedName
word scopedName(const word &name) const
Return a scoped (prefixed) name.
Definition: functionObject.C:51
Foam::functionObjects::fieldExpression
Intermediate class for handling field expression function objects (e.g. blendingFactor etc....
Definition: fieldExpression.H:120
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_
Reference to the fvMesh.
Definition: fvMeshFunctionObject.H:73
Foam::functionObjects::volRegion::cellIDs
const labelList & cellIDs() const
Return the local list of cell IDs.
Definition: volRegion.C:203
Foam::tmp::New
static tmp< T > New(Args &&... args)
Construct tmp of T with forwarding arguments.
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::fieldExpression::resultName_
word resultName_
Name of result field.
Definition: fieldExpression.H:132
randomise.H