1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
28 Test the Distribution class
30 Plot normal distribution test in gnuplot using:
33 normalDistribution(mean, sigma, x) = \
34 sqrt(1.0/(2.0*pi*sigma**2))*exp(-(x - mean)**2.0/(2.0*sigma**2))
36 plot normalDistribution(8.5, 2.5, x), "Distribution_scalar_test_x" w p
39 \*---------------------------------------------------------------------------*/
42 #include "labelVector.H"
44 #include "Distribution.H"
46 #include "dimensionedTypes.H"
48 #include "PstreamReduceOps.H"
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 int main(int argc, char *argv[])
56 # include "setRootCase.H"
62 label randomDistributionTestSize = 50000000;
64 Distribution<scalar> dS(scalar(5e-2));
66 Info<< nl << "Distribution<scalar>" << nl
68 << randomDistributionTestSize
69 << " times from GaussNormal distribution."
72 for (label i = 0; i < randomDistributionTestSize; i++)
74 dS.add(2.5*R.GaussNormal() + 8.5);
77 Info<< "Mean " << dS.mean() << nl
78 << "Median " << dS.median()
81 dS.write("Distribution_scalar_test_1");
83 Distribution<scalar> dS2(scalar(1e-2));
85 Info<< nl << "Distribution<scalar>" << nl
87 << randomDistributionTestSize
88 << " times from GaussNormal distribution."
91 for (label i = 0; i < randomDistributionTestSize; i++)
93 dS2.add(1.5*R.GaussNormal() -6.0);
96 Info<< "Mean " << dS2.mean() << nl
97 << "Median " << dS2.median()
100 dS2.write("Distribution_scalar_test_2");
102 Info<< nl << "Adding previous two Distribution<scalar>" << endl;
106 dS.write("Distribution_scalar_test_1+2");
109 if (Pstream::parRun())
111 // scalar in parallel
112 label randomDistributionTestSize = 100000000;
114 Distribution<scalar> dS(scalar(1e-1));
116 Pout<< "Distribution<scalar>" << nl
118 << randomDistributionTestSize
119 << " times from uniform distribution."
122 for (label i = 0; i < randomDistributionTestSize; i++)
124 dS.add(R.scalar01() + 10*Pstream::myProcNo());
127 Pout<< "Mean " << dS.mean() << nl
128 << "Median " << dS.median()
131 reduce(dS, sumOp< Distribution<scalar> >());
133 if (Pstream::master())
135 Info<< "Reducing parallel Distribution<scalar>" << nl
136 << "Mean " << dS.mean() << nl
137 << "Median " << dS.median()
140 dS.write("Distribution_scalar_test_parallel_reduced");
146 Distribution<vector> dV(vector(0.1, 0.05, 0.15));
148 label randomDistributionTestSize = 1000000;
150 Info<< nl << "Distribution<vector>" << nl
152 << randomDistributionTestSize
153 << " times from uniform and GaussNormal distribution."
156 for (label i = 0; i < randomDistributionTestSize; i++)
158 dV.add(R.vector01());
160 // Adding separate GaussNormal components with component
167 R.GaussNormal()*3.0 + 1.5,
168 R.GaussNormal()*0.25 + 4.0,
169 R.GaussNormal()*3.0 - 1.5
171 vector(1.0, 2.0, 5.0)
175 Info<< "Mean " << dV.mean() << nl
176 << "Median " << dV.median()
179 dV.write("Distribution_vector_test");
184 // Distribution<labelVector> dLV(labelVector::one*10);
186 // label randomDistributionTestSize = 2000000;
188 // Info<< nl << "Distribution<labelVector>" << nl
190 // << randomDistributionTestSize
191 // << " times from uniform distribution."
194 // for (label i = 0; i < randomDistributionTestSize; i++)
200 // R.integer(-1000, 1000),
201 // R.integer(-5000, 5000),
202 // R.integer(-2000, 7000)
207 // Info<< "Mean " << dLV.mean() << nl
208 // << "Median " << dLV.median()
211 // dLV.write("Distribution_labelVector_test");
216 Distribution<tensor> dT(tensor::one*1e-2);
218 label randomDistributionTestSize = 2000000;
220 Info<< nl << "Distribution<tensor>" << nl
222 << randomDistributionTestSize
223 << " times from uniform distribution."
226 for (label i = 0; i < randomDistributionTestSize; i++)
228 dT.add(R.tensor01());
231 Info<< "Mean " << dT.mean() << nl
232 << "Median " << dT.median()
235 dT.write("Distribution_tensor_test");
240 Distribution<symmTensor> dSyT(symmTensor::one*1e-2);
242 label randomDistributionTestSize = 2000000;
244 Info<< nl << "Distribution<symmTensor>" << nl
246 << randomDistributionTestSize
247 << " times from uniform distribution."
250 for (label i = 0; i < randomDistributionTestSize; i++)
252 dSyT.add(R.symmTensor01());
255 Info<< "Mean " << dSyT.mean() << nl
256 << "Median " << dSyT.median()
259 dSyT.write("Distribution_symmTensor_test");
264 Distribution<sphericalTensor> dSpT(sphericalTensor::one*1e-2);
266 label randomDistributionTestSize = 50000000;
268 Info<< nl << "Distribution<sphericalTensor>" << nl
270 << randomDistributionTestSize
271 << " times from uniform distribution."
274 for (label i = 0; i < randomDistributionTestSize; i++)
276 dSpT.add(R.sphericalTensor01());
279 Info<< "Mean " << dSpT.mean() << nl
280 << "Median " << dSpT.median()
283 dSpT.write("Distribution_sphericalTensor_test");
286 Info<< nl << "End" << nl << endl;
292 // ************************************************************************* //