diff --git a/applications/test/Distribution/DistributionTest.C b/applications/test/Distribution/DistributionTest.C index cdbbc4e469..687d5746ac 100644 --- a/applications/test/Distribution/DistributionTest.C +++ b/applications/test/Distribution/DistributionTest.C @@ -28,12 +28,24 @@ Application Description Test the Distribution class + Plot normal distribution test in gnuplot using: + + @verbatim + normalDistribution(mean, sigma, x) = \ + sqrt(1.0/(2.0*pi*sigma**2))*exp(-(x - mean)**2.0/(2.0*sigma**2)) + + plot normalDistribution(8.5, 2.5, x) + @endverbatim + \*---------------------------------------------------------------------------*/ #include "vector.H" #include "labelVector.H" #include "tensor.H" #include "Distribution.H" +#include "Random.H" +#include "dimensionedTypes.H" + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -41,36 +53,173 @@ using namespace Foam; int main(int argc, char *argv[]) { - Distribution dS(scalar(3.0)); - Distribution dV(vector(0.1, 0.24, 0.18)); - Distribution dLV(labelVector(2,3,4)); + Random R(918273); - dS.add(1.0); - dS.add(2.0); - dS.add(12.0); - dS.add(1.3); + { + // scalar + label randomDistributionTestSize = 50000000; - Info<< dS << nl << dS.raw() << endl; + Distribution dS(scalar(5e-2)); - vector vA(1.2, 1.3, 1.1); - vector vB(1.3, 1.5, 1.6); - vector vC(0.5, 5.3, 1.1); + Info<< nl << "Distribution" << nl + << "Sampling " + << randomDistributionTestSize + << " times from GaussNormal distribution." + << endl; - dV.add(vA); - dV.add(vB); - dV.add(vC); + for (label i = 0; i < randomDistributionTestSize; i++) + { + dS.add(2.5*R.GaussNormal() + 8.5); + } - Info<< dV << nl << dV.raw() << endl; + Info<< "Mean " << dS.mean() << nl + << "Median " << dS.median() + << endl; - labelVector lVA(6, 8, 11); - labelVector lVB(-12, -3, 6); - labelVector lVC(-4, -2, 5); + dS.write("Distribution_scalar_test", dS.normalised()); + } - dLV.add(lVA); - dLV.add(lVB); - dLV.add(lVC); + { + // vector + Distribution dV(vector(0.1, 0.05, 0.15)); - Info<< dLV << nl << dLV.raw() << endl; + label randomDistributionTestSize = 1000000; + + Info<< nl << "Distribution" << nl + << "Sampling " + << randomDistributionTestSize + << " times from uniform and GaussNormal distribution." + << endl; + + for (label i = 0; i < randomDistributionTestSize; i++) + { + dV.add(R.vector01()); + + // Adding separate GaussNormal components with component + // weights + + dV.add + ( + vector + ( + R.GaussNormal()*3.0 + 1.5, + R.GaussNormal()*0.25 + 4.0, + R.GaussNormal()*3.0 - 1.5 + ), + vector(1.0, 2.0, 5.0) + ); + } + + Info<< "Mean " << dV.mean() << nl + << "Median " << dV.median() + << endl; + + dV.write("Distribution_vector_test", dV.normalised()); + } + + { + // labelVector + Distribution dLV(labelVector::one*10); + + label randomDistributionTestSize = 2000000; + + Info<< nl << "Distribution" << nl + << "Sampling " + << randomDistributionTestSize + << " times from uniform distribution." + << endl; + + for (label i = 0; i < randomDistributionTestSize; i++) + { + dLV.add + ( + labelVector + ( + R.integer(-1000, 1000), + R.integer(-5000, 5000), + R.integer(-2000, 7000) + ) + ); + } + + Info<< "Mean " << dLV.mean() << nl + << "Median " << dLV.median() + << endl; + + dLV.write("Distribution_labelVector_test", dLV.normalised()); + } + + { + // tensor + Distribution dT(tensor::one*1e-2); + + label randomDistributionTestSize = 2000000; + + Info<< nl << "Distribution" << nl + << "Sampling " + << randomDistributionTestSize + << " times from uniform distribution." + << endl; + + for (label i = 0; i < randomDistributionTestSize; i++) + { + dT.add(R.tensor01()); + } + + Info<< "Mean " << dT.mean() << nl + << "Median " << dT.median() + << endl; + + dT.write("Distribution_tensor_test", dT.normalised()); + } + + { + // symmTensor + Distribution dSyT(symmTensor::one*1e-2); + + label randomDistributionTestSize = 2000000; + + Info<< nl << "Distribution" << nl + << "Sampling " + << randomDistributionTestSize + << " times from uniform distribution." + << endl; + + for (label i = 0; i < randomDistributionTestSize; i++) + { + dSyT.add(R.symmTensor01()); + } + + Info<< "Mean " << dSyT.mean() << nl + << "Median " << dSyT.median() + << endl; + + dSyT.write("Distribution_symmTensor_test", dSyT.normalised()); + } + + { + // sphericalTensor + Distribution dSpT(sphericalTensor::one*1e-2); + + label randomDistributionTestSize = 50000000; + + Info<< nl << "Distribution" << nl + << "Sampling " + << randomDistributionTestSize + << " times from uniform distribution." + << endl; + + for (label i = 0; i < randomDistributionTestSize; i++) + { + dSpT.add(R.sphericalTensor01()); + } + + Info<< "Mean " << dSpT.mean() << nl + << "Median " << dSpT.median() + << endl; + + dSpT.write("Distribution_sphericalTensor_test", dSpT.normalised()); + } Info<< nl << "End" << nl << endl; diff --git a/src/OpenFOAM/containers/HashTables/Map/Distribution/Distribution.C b/src/OpenFOAM/containers/HashTables/Map/Distribution/Distribution.C index 3a0defe22c..cee8014f09 100644 --- a/src/OpenFOAM/containers/HashTables/Map/Distribution/Distribution.C +++ b/src/OpenFOAM/containers/HashTables/Map/Distribution/Distribution.C @@ -25,16 +25,14 @@ License \*---------------------------------------------------------------------------*/ #include "Distribution.H" - -// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // - +#include "OFstream.H" // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template Foam::Distribution::Distribution() : - List< Map