RandomTemplates.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) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2018-2022 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 "Random.H"
30 #include "Pstream.H"
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 template<class Type>
36 {
37  Type value;
38  for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; ++cmpt)
39  {
40  value.component(cmpt) = scalar01();
41  }
42 
43  return value;
44 }
45 
46 
47 template<class Type>
49 {
50  Type value;
51  for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; ++cmpt)
52  {
53  value.component(cmpt) = GaussNormal<scalar>();
54  }
55 
56  return value;
57 }
58 
59 
60 template<class Type>
61 Type Foam::Random::position(const Type& start, const Type& end)
62 {
63  Type value(start);
64  for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; ++cmpt)
65  {
66  value.component(cmpt) +=
67  scalar01()*(end.component(cmpt) - start.component(cmpt));
68  }
69 
70  return value;
71 }
72 
73 
74 template<class Type>
75 void Foam::Random::randomise01(Type& value)
76 {
77  value = sample01<Type>();
78 }
79 
80 
81 template<class Type>
83 {
84  for (label posi = values.size()-1; posi > 0; --posi)
85  {
86  const label i = position<label>(0, posi);
87  Foam::Swap(values[i], values[posi]);
88  }
89 }
90 
91 
92 template<class Type>
94 {
95  Type value(Zero);
96 
97  if (Pstream::master())
98  {
99  value = sample01<Type>();
100  }
101 
103 
104  return value;
105 }
106 
107 
108 template<class Type>
110 {
111  Type value(Zero);
112 
113  if (Pstream::master())
114  {
115  value = GaussNormal<Type>();
116  }
117 
119 
120  return value;
121 }
122 
123 
124 template<class Type>
125 Type Foam::Random::globalPosition(const Type& start, const Type& end)
126 {
127  Type value(Zero);
128 
129  if (Pstream::master())
130  {
131  value = position<Type>(start, end);
132  }
133 
135 
136  return value;
137 }
138 
139 
140 template<class Type>
141 void Foam::Random::globalRandomise01(Type& value)
142 {
143  if (Pstream::master())
144  {
145  value = sample01<Type>();
146  }
147 
148  Pstream::broadcast(value);
149 }
150 
151 
152 // ************************************************************************* //
void randomise01(Type &value)
Randomise value in the range [0,1].
Type globalGaussNormal()
Return a sample whose components are normally distributed with zero mean and unity variance N(0...
uint8_t direction
Definition: direction.H:46
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all communicator ranks. Does nothing in non-paral...
void shuffle(UList< Type > &values)
Shuffle the values in the list.
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
Type sample01()
Return a sample whose components lie in the range [0,1].
Type globalPosition(const Type &start, const Type &end)
Return a sample on the interval [start,end].
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:201
void globalRandomise01(Type &value)
Randomise value in the range 0-1.
Type globalSample01()
Return a sample whose components lie in the range [0,1].
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
Definition: DynamicList.H:692
Type GaussNormal()
Return a sample whose components are normally distributed with zero mean and unity variance N(0...
Type position(const Type &start, const Type &end)
Return a sample on the interval [start,end].
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127