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 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 "Swap.H"
31 #include "Pstream.H"
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
35 template<class Type>
37 {
38  Type value;
39  for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; ++cmpt)
40  {
41  value.component(cmpt) = scalar01();
42  }
43 
44  return value;
45 }
46 
47 
48 template<class Type>
50 {
51  Type value;
52  for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; ++cmpt)
53  {
54  value.component(cmpt) = GaussNormal<scalar>();
55  }
56 
57  return value;
58 }
59 
60 
61 template<class Type>
62 Type Foam::Random::position(const Type& start, const Type& end)
63 {
64  Type value(start);
65  for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; ++cmpt)
66  {
67  value.component(cmpt) +=
68  scalar01()*(end.component(cmpt) - start.component(cmpt));
69  }
70 
71  return value;
72 }
73 
74 
75 template<class Type>
76 void Foam::Random::randomise01(Type& value)
77 {
78  value = sample01<Type>();
79 }
80 
81 
82 template<class Type>
84 {
85  for (label posi = values.size()-1; posi > 0; --posi)
86  {
87  const label i = position<label>(0, posi);
88  Foam::Swap(values[i], values[posi]);
89  }
90 }
91 
92 
93 template<class Type>
95 {
96  Type value = -GREAT*pTraits<Type>::one;
97 
98  if (Pstream::master())
99  {
100  value = sample01<Type>();
101  }
102 
103  Pstream::scatter(value);
104 
105  return value;
106 }
107 
108 
109 template<class Type>
111 {
112  Type value = -GREAT*pTraits<Type>::one;
113 
114  if (Pstream::master())
115  {
116  value = GaussNormal<Type>();
117  }
118 
119  Pstream::scatter(value);
120 
121  return value;
122 }
123 
124 
125 template<class Type>
126 Type Foam::Random::globalPosition(const Type& start, const Type& end)
127 {
128  Type value = -GREAT*pTraits<Type>::one;
129 
130  if (Pstream::master())
131  {
132  value = position<Type>(start, end);
133  }
134 
135  Pstream::scatter(value);
136 
137  return value;
138 }
139 
140 
141 template<class Type>
143 {
144  value = -GREAT*pTraits<Type>::one;
145 
146  if (Pstream::master())
147  {
148  value = sample01<Type>();
149  }
150 
151  Pstream::scatter(value);
152 }
153 
154 
155 // ************************************************************************* //
Foam::Random::globalSample01
Type globalSample01()
Return a sample whose components lie in the range [0,1].
Definition: RandomTemplates.C:94
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
Foam::Random::GaussNormal
Type GaussNormal()
Definition: RandomTemplates.C:49
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::Random::sample01
Type sample01()
Return a sample whose components lie in the range [0,1].
Definition: RandomTemplates.C:36
Foam::Random::shuffle
void shuffle(UList< Type > &values)
Shuffle the values in the list.
Definition: RandomTemplates.C:83
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:150
Foam::Random::randomise01
void randomise01(Type &value)
Randomise value in the range [0,1].
Definition: RandomTemplates.C:76
Swap.H
Swap arguments as per std::swap, but in Foam namespace.
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Pstream.H
Random.H
Foam::Random::globalRandomise01
void globalRandomise01(Type &value)
Randomise value in the range 0-1.
Definition: RandomTemplates.C:142
Foam::Random::globalGaussNormal
Type globalGaussNormal()
Definition: RandomTemplates.C:110
Foam::Random::position
Type position(const Type &start, const Type &end)
Return a sample on the interval [start,end].
Definition: RandomTemplates.C:62
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::UList< Type >
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::Random::globalPosition
Type globalPosition(const Type &start, const Type &end)
Return a sample on the interval [start,end].
Definition: RandomTemplates.C:126