From 844685d29d3fc8389fb1b477a178c1909c752ec5 Mon Sep 17 00:00:00 2001 From: graham Date: Mon, 23 Nov 2009 12:22:13 +0000 Subject: [PATCH] Finished working version of templated Distribution class. Changed the underlying map to be a Map to allow the addition of a value to the Distribution to be weighted (by timestep or by cell size for example) component by-component - default weight is one. Adding normalisation, write, mean snd median functions. Test application DistributionTest draws numbers from the Random class and creates distributions of them, writing to disk, for the main types: scalar, vector, symmTensior, sphericalTensor and tensor. Creating a labelVector distibution, but need to check what is happening to the mean and median values and what happens when an odd number is used as a binWidth, as truncation to label will be occurring. --- .../test/Distribution/DistributionTest.C | 193 ++++++++++-- .../Map/Distribution/Distribution.C | 277 +++++++++++++++--- .../Map/Distribution/Distribution.H | 53 ++-- 3 files changed, 435 insertions(+), 88 deletions(-) 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