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-------------------------------------------------------------------------------
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\*---------------------------------------------------------------------------*/
28
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 = -GREAT*pTraits<Type>::one;
96
97 if (Pstream::master())
98 {
99 value = sample01<Type>();
100 }
101
102 Pstream::scatter(value);
103
104 return value;
105}
106
107
108template<class Type>
110{
111 Type value = -GREAT*pTraits<Type>::one;
112
113 if (Pstream::master())
114 {
115 value = GaussNormal<Type>();
116 }
117
118 Pstream::scatter(value);
119
120 return value;
121}
122
123
124template<class Type>
125Type Foam::Random::globalPosition(const Type& start, const Type& end)
126{
127 Type value = -GREAT*pTraits<Type>::one;
128
129 if (Pstream::master())
130 {
131 value = position<Type>(start, end);
132 }
133
134 Pstream::scatter(value);
135
136 return value;
137}
138
139
140template<class Type>
142{
143 value = -GREAT*pTraits<Type>::one;
144
145 if (Pstream::master())
146 {
147 value = sample01<Type>();
148 }
149
150 Pstream::scatter(value);
151}
152
153
154// ************************************************************************* //
static void scatter(const List< commsStruct > &comms, T &value, const int tag, const label comm)
Broadcast data: Distribute without modification.
void globalRandomise01(Type &value)
Randomise value in the range 0-1.
Type GaussNormal()
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()
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:94
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
splitCell * master() const
Definition: splitCell.H:113
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408
uint8_t direction
Definition: direction.H:56