OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

distribution.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 2008-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*----------------------------------------------------------------------------*/
00026 
00027 #include "distribution.H"
00028 
00029 namespace Foam
00030 {
00031 
00032 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00033 
00034 distribution::distribution()
00035 :
00036     Map<label>(),
00037     binWidth_(1)
00038 {}
00039 
00040 
00041 distribution::distribution(const scalar binWidth)
00042 :
00043     Map<label>(),
00044     binWidth_(binWidth)
00045 {}
00046 
00047 
00048 distribution::distribution(const distribution& d)
00049 :
00050     Map<label>(static_cast< Map<label> >(d)),
00051     binWidth_(d.binWidth())
00052 {}
00053 
00054 
00055 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00056 
00057 distribution::~distribution()
00058 {}
00059 
00060 
00061 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00062 
00063 label distribution::totalEntries() const
00064 {
00065     label sumOfEntries = 0;
00066 
00067     forAllConstIter(Map<label>, *this, iter)
00068     {
00069         sumOfEntries += iter();
00070 
00071         if (sumOfEntries < 0)
00072         {
00073             WarningIn("label distribution::totalEntries()")
00074                 << "Accumulated distribution values total has become negative: "
00075                 << "sumOfEntries = " << sumOfEntries
00076                 << ". This is most likely to be because too many samples "
00077                 << "have been added to the bins and the label has 'rolled "
00078                 << "round'. Try distribution::approxTotalEntries which "
00079                 << "returns a scalar." << endl;
00080 
00081             sumOfEntries = -1;
00082 
00083             break;
00084         }
00085     }
00086 
00087     return sumOfEntries;
00088 }
00089 
00090 
00091 scalar distribution::approxTotalEntries() const
00092 {
00093     scalar sumOfEntries = 0;
00094 
00095     forAllConstIter(Map<label>, *this, iter)
00096     {
00097         sumOfEntries += scalar(iter());
00098     }
00099 
00100     return sumOfEntries;
00101 }
00102 
00103 
00104 scalar distribution::mean() const
00105 {
00106     scalar runningSum = 0;
00107 
00108     scalar totEnt = approxTotalEntries();
00109 
00110     List<label> keys = toc();
00111 
00112     forAll(keys,k)
00113     {
00114         label key = keys[k];
00115 
00116         runningSum +=
00117             (0.5 + scalar(key))
00118            *binWidth_
00119            *scalar((*this)[key])
00120            /totEnt;
00121     }
00122 
00123     return runningSum;
00124 }
00125 
00126 
00127 scalar distribution::median()
00128 {
00129     // From:
00130     // http://mathworld.wolfram.com/StatisticalMedian.html
00131     // The statistical median is the value of the distribution variable
00132     // where the cumulative distribution = 0.5.
00133 
00134     scalar median = 0.0;
00135 
00136     scalar runningSum = 0.0;
00137 
00138     List<Pair<scalar> > normDist(normalised());
00139 
00140     if (normDist.size())
00141     {
00142         if (normDist.size() == 1)
00143         {
00144             median = normDist[0].first();
00145         }
00146         else if
00147         (
00148             normDist.size() > 1
00149          && normDist[0].second()*binWidth_ > 0.5
00150         )
00151         {
00152             scalar xk = normDist[1].first();
00153             scalar xkm1 = normDist[0].first();
00154             scalar Sk =
00155                 (normDist[0].second() + normDist[1].second())*binWidth_;
00156             scalar Skm1 = normDist[0].second()*binWidth_;
00157 
00158             median = (0.5 - Skm1)*(xk - xkm1)/(Sk - Skm1) + xkm1;
00159         }
00160         else
00161         {
00162             label lastNonZeroIndex = 0;
00163 
00164             forAll(normDist,nD)
00165             {
00166                 if (runningSum + (normDist[nD].second()*binWidth_) > 0.5)
00167                 {
00168                     scalar xk = normDist[nD].first();
00169                     scalar xkm1 = normDist[lastNonZeroIndex].first();
00170                     scalar Sk = runningSum + (normDist[nD].second()*binWidth_);
00171                     scalar Skm1 = runningSum;
00172 
00173                     median = (0.5 - Skm1)*(xk - xkm1)/(Sk - Skm1) + xkm1;
00174 
00175                     break;
00176                 }
00177                 else if (normDist[nD].second() > 0.0)
00178                 {
00179                     runningSum += normDist[nD].second()*binWidth_;
00180 
00181                     lastNonZeroIndex = nD;
00182                 }
00183             }
00184         }
00185     }
00186 
00187     return median;
00188 }
00189 
00190 
00191 void distribution::add(const scalar valueToAdd)
00192 {
00193     iterator iter(this->begin());
00194 
00195     label n = label(valueToAdd/binWidth_) - label(neg(valueToAdd/binWidth_));
00196 
00197     iter = find(n);
00198 
00199     if (iter == this->end())
00200     {
00201         this->insert(n,1);
00202     }
00203     else
00204     {
00205         (*this)[n]++;
00206     }
00207 
00208     if ((*this)[n] < 0)
00209     {
00210         FatalErrorIn("distribution::add(const scalar valueToAdd)")
00211             << "Accumulated distribution value has become negative: "
00212             << "bin = " << (0.5 + scalar(n)) * binWidth_
00213             << ", value = " << (*this)[n]
00214             << ". This is most likely to be because too many samples "
00215             << "have been added to a bin and the label has 'rolled round'"
00216             << abort(FatalError);
00217     }
00218 }
00219 
00220 
00221 void distribution::add(const label valueToAdd)
00222 {
00223     add(scalar(valueToAdd));
00224 }
00225 
00226 
00227 void distribution::insertMissingKeys()
00228 {
00229     iterator iter(this->begin());
00230 
00231     List<label> keys = toc();
00232 
00233     sort(keys);
00234 
00235     if (keys.size())
00236     {
00237         for (label k = keys[0]; k < keys[keys.size()-1]; k++)
00238         {
00239             iter = find(k);
00240 
00241             if (iter == this->end())
00242             {
00243                 this->insert(k,0);
00244             }
00245         }
00246     }
00247 }
00248 
00249 
00250 List< Pair<scalar> > distribution::normalised()
00251 {
00252     scalar totEnt = approxTotalEntries();
00253 
00254     insertMissingKeys();
00255 
00256     List<label> keys = toc();
00257 
00258     sort(keys);
00259 
00260     List<Pair<scalar> > normDist(size());
00261 
00262     forAll(keys,k)
00263     {
00264         label key = keys[k];
00265 
00266         normDist[k].first() = (0.5 + scalar(key))*binWidth_;
00267 
00268         normDist[k].second() = scalar((*this)[key])/totEnt/binWidth_;
00269     }
00270 
00271     return normDist;
00272 }
00273 
00274 
00275 List< Pair<scalar> > distribution::normalisedMinusMean()
00276 {
00277     return normalisedShifted(mean());
00278 }
00279 
00280 
00281 List< Pair<scalar> > distribution::normalisedShifted(const scalar shiftValue)
00282 {
00283     List<Pair<scalar> > oldDist(normalised());
00284 
00285     List<Pair<scalar> > newDist(oldDist.size());
00286 
00287     forAll(oldDist,u)
00288     {
00289         oldDist[u].first() -= shiftValue;
00290     }
00291 
00292     scalar lowestOldBin = oldDist[0].first()/binWidth_ - 0.5;
00293 
00294     label lowestNewKey = label
00295     (
00296         lowestOldBin + 0.5*sign(lowestOldBin)
00297     );
00298 
00299     scalar interpolationStartDirection =
00300         sign(scalar(lowestNewKey) - lowestOldBin);
00301 
00302     label newKey = lowestNewKey;
00303 
00304 //     Info << shiftValue
00305 //         << nl << lowestOldBin
00306 //         << nl << lowestNewKey
00307 //         << nl << interpolationStartDirection
00308 //         << endl;
00309 
00310 //     scalar checkNormalisation = 0;
00311 
00312 //     forAll (oldDist, oD)
00313 //     {
00314 //         checkNormalisation += oldDist[oD].second()*binWidth_;
00315 //     }
00316 
00317 //     Info << "Initial normalisation = " << checkNormalisation << endl;
00318 
00319     forAll(oldDist,u)
00320     {
00321         newDist[u].first() = (0.5 + scalar(newKey)) * binWidth_;
00322 
00323         if (interpolationStartDirection < 0)
00324         {
00325             if (u == 0)
00326             {
00327                 newDist[u].second() =
00328                     (0.5 + scalar(newKey))*oldDist[u].second()
00329                   - oldDist[u].second()
00330                         *(oldDist[u].first() - binWidth_)/ binWidth_;
00331             }
00332             else
00333             {
00334                 newDist[u].second() =
00335                     (0.5 + scalar(newKey))
00336                    *(oldDist[u].second() - oldDist[u-1].second())
00337                   +
00338                     (
00339                         oldDist[u-1].second()*oldDist[u].first()
00340                       - oldDist[u].second()*oldDist[u-1].first()
00341                     )
00342                     /binWidth_;
00343             }
00344         }
00345         else
00346         {
00347             if (u == oldDist.size() - 1)
00348             {
00349                 newDist[u].second() =
00350                     (0.5 + scalar(newKey))*-oldDist[u].second()
00351                   + oldDist[u].second()*(oldDist[u].first() + binWidth_)
00352                    /binWidth_;
00353             }
00354             else
00355             {
00356                 newDist[u].second() =
00357                     (0.5 + scalar(newKey))
00358                    *(oldDist[u+1].second() - oldDist[u].second())
00359                   +
00360                     (
00361                         oldDist[u].second()*oldDist[u+1].first()
00362                       - oldDist[u+1].second()*oldDist[u].first()
00363                     )
00364                    /binWidth_;
00365             }
00366         }
00367 
00368         newKey++;
00369     }
00370 
00371 //     checkNormalisation = 0;
00372 
00373 //     forAll (newDist, nD)
00374 //     {
00375 //         checkNormalisation += newDist[nD].second()*binWidth_;
00376 //     }
00377 
00378 //     Info << "Shifted normalisation = " << checkNormalisation << endl;
00379 
00380     return newDist;
00381 }
00382 
00383 
00384 List<Pair<scalar> > distribution::raw()
00385 {
00386     insertMissingKeys();
00387 
00388     List<label> keys = toc();
00389 
00390     sort(keys);
00391 
00392     List<Pair<scalar> > rawDist(size());
00393 
00394     forAll(keys,k)
00395     {
00396         label key = keys[k];
00397 
00398         rawDist[k].first() = (0.5 + scalar(key))*binWidth_;
00399 
00400         rawDist[k].second() = scalar((*this)[key]);
00401     }
00402 
00403     return rawDist;
00404 }
00405 
00406 
00407 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00408 
00409 void distribution::operator=(const distribution& rhs)
00410 {
00411     // Check for assignment to self
00412     if (this == &rhs)
00413     {
00414         FatalErrorIn("distribution::operator=(const distribution&)")
00415             << "Attempted assignment to self"
00416             << abort(FatalError);
00417     }
00418 
00419     Map<label>::operator=(rhs);
00420 
00421     binWidth_ = rhs.binWidth();
00422 }
00423 
00424 
00425 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00426 
00427 Ostream& operator<<(Ostream& os, const distribution& d)
00428 {
00429     os  << d.binWidth_
00430         << static_cast<const Map<label>&>(d);
00431 
00432     // Check state of Ostream
00433     os.check
00434     (
00435         "Ostream& operator<<(Ostream&, "
00436         "const distribution&)"
00437     );
00438 
00439     return os;
00440 }
00441 
00442 
00443 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00444 
00445 } // End namespace Foam
00446 
00447 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd