cachedRandomTemplates.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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "cachedRandom.H"
27 #include "Pstream.H"
28 
29 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
30 
31 template<class Type>
33 {
34  Type value;
35  for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
36  {
37  value.component(cmpt) = scalar01();
38  }
39 
40  return value;
41 }
42 
43 
44 template<class Type>
46 {
47  Type value;
48  for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
49  {
50  value.component(cmpt) = GaussNormal<scalar>();
51  }
52 
53  return value;
54 }
55 
56 
57 template<class Type>
58 Type Foam::cachedRandom::position(const Type& start, const Type& end)
59 {
60  Type value(start);
61  for (direction cmpt=0; cmpt<pTraits<Type>::nComponents; cmpt++)
62  {
63  value.component(cmpt) +=
64  scalar01()*(end.component(cmpt) - start.component(cmpt));
65  }
66 
67  return value;
68 }
69 
70 
71 template<class Type>
73 {
74  value = sample01<Type>();
75 }
76 
77 
78 template<class Type>
80 {
81  Type value = -GREAT*pTraits<Type>::one;
82 
83  if (Pstream::master())
84  {
85  value = sample01<Type>();
86  }
87 
88  Pstream::scatter(value);
89 
90  return value;
91 }
92 
93 
94 template<class Type>
96 {
97  Type value = -GREAT*pTraits<Type>::one;
98 
99  if (Pstream::master())
100  {
101  value = GaussNormal<Type>();
102  }
103 
104  Pstream::scatter(value);
105 
106  return value;
107 }
108 
109 
110 template<class Type>
111 Type Foam::cachedRandom::globalPosition(const Type& start, const Type& end)
112 {
113  Type value = -GREAT*pTraits<Type>::one;
114 
115  if (Pstream::master())
116  {
117  value = position<Type>(start, end);
118  }
119 
120  Pstream::scatter(value);
121 
122  return value;
123 }
124 
125 
126 template<class Type>
128 {
129  value = -GREAT*pTraits<Type>::one;
130 
131  if (Pstream::master())
132  {
133  value = sample01<Type>();
134  }
135 
136  Pstream::scatter(value);
137 }
138 
139 
140 // ************************************************************************* //
Type globalSample01()
Return a sample whose components lie in the range 0-1.
unsigned char direction
Definition: direction.H:43
void randomise01(Type &value)
Randomise value in the range 0-1.
Type globalPosition(const Type &start, const Type &end)
Return a sample between start and end.
void globalRandomise01(Type &value)
Randomise value in the range 0-1.
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Traits class for primitives.
Definition: pTraits.H:50
Type sample01()
Return a sample whose components lie in the range 0-1.
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:398
Type position(const Type &start, const Type &end)
Return a sample between start and end.
Type globalGaussNormal()
Return a sample whose components are normally distributed.
Type GaussNormal()
Return a sample whose components are normally distributed.