Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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\*---------------------------------------------------------------------------*/
29#include "Random.H"
30#include "Pstream.H"
31
32// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33
34template<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
47template<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
60template<class Type>
61Type 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
74template<class Type>
76{
77 value = sample01<Type>();
78}
79
80
81template<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
92template<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
108template<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
124template<class Type>
125Type 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
140template<class Type>
141void 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// ************************************************************************* //
Type position(const Type &start, const Type &end)
Return a sample on the interval [start,end].
void globalRandomise01(Type &value)
Randomise value in the range 0-1.
Type GaussNormal()
Return a sample whose components are normally distributed with zero mean and unity variance N(0,...
Type sample01()
Return a sample whose components lie in the range [0,1].
Type globalSample01()
Return a sample whose components lie in the range [0,1].
void shuffle(UList< Type > &values)
Shuffle the values in the list.
Type globalPosition(const Type &start, const Type &end)
Return a sample on the interval [start,end].
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,...
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
@ broadcast
broadcast [MPI]
Definition UPstream.H:189
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Exchange contents of lists - see DynamicList::swap().
uint8_t direction
Definition direction.H:49
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127