binned.C 6.18 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7 8
-------------------------------------------------------------------------------
    Copyright (C) 2015-2016 OpenCFD Ltd.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "binned.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
    namespace distributionModels
    {
        defineTypeNameAndDebug(binned, 0);
        addToRunTimeSelectionTable(distributionModel, binned, dictionary);
    }
}


const char* Foam::distributionModels::binned::header =
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    "(bin probability)";


// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::distributionModels::binned::initialise()
{
    const label nSample(xy_.size());

    // Convert values to integral values
    for (label bini = 1; bini < nSample; ++bini)
    {
        xy_[bini][1] += xy_[bini - 1][1];
    }

59 60 61 62 63 64 65
    // Normalise
    scalar sumProb = xy_.last()[1];
    forAll(xy_, bini)
    {
        xy_[bini][1] /= sumProb;
    }

66 67 68 69 70 71 72 73 74 75 76 77 78
    // Calculate the mean value
    label bini = 0;
    forAll(xy_, i)
    {
        if (xy_[i][1] > 0.5)
        {
            bini = i;
            break;
        }
    }

    meanValue_ = xy_[bini][1];
}
79 80 81 82 83 84 85


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::distributionModels::binned::binned
(
    const dictionary& dict,
86
    Random& rndGen
87 88 89
)
:
    distributionModel(typeName, dict, rndGen),
90 91
    xy_(distributionModelDict_.lookup("distribution")),
    meanValue_(0)
92
{
93
    if (maxValue() < minValue())
94 95 96
    {
        FatalErrorInFunction
            << "Maximum value is smaller than the minimum value:"
97 98
            << "    maxValue = " << maxValue()
            << ", minValue = " << minValue()
99 100 101
            << exit(FatalError);
    }

102
    initialise();
103 104 105 106 107
}


Foam::distributionModels::binned::binned
(
108
    const UList<scalar>& sampleData,
109
    const scalar binWidth,
110
    Random& rndGen
111 112 113
)
:
    distributionModel(typeName, dictionary::null, rndGen),
114 115
    xy_(),
    meanValue_(0)
116
{
117 118 119 120 121 122 123
    scalar minValue = GREAT;
    scalar maxValue = -GREAT;
    forAll(sampleData, i)
    {
        minValue = min(minValue, sampleData[i]);
        maxValue = max(maxValue, sampleData[i]);
    }
124

125 126 127
    const label bin0 = floor(minValue/binWidth);
    const label bin1 = ceil(maxValue/binWidth);
    const label nBin = bin1 - bin0;
128 129 130 131 132

    if (nBin == 0)
    {
        WarningInFunction
            << "Data cannot be binned - zero bins generated" << nl
133 134
            << "   Bin width   : " << binWidth << nl
            << "   Sample data : " << sampleData
135 136 137 138 139
            << endl;

        return;
    }

140 141 142
    // Populate bin boundaries and initialise occurrences
    xy_.setSize(nBin);
    forAll(xy_, bini)
143
    {
144 145
        xy_[bini][0] = (bin0 + bini)*binWidth;
        xy_[bini][1] = 0;
146 147
    }

148 149
    // Bin the data
    forAll(sampleData, i)
150
    {
151 152 153
        // Choose the nearest bin
        label bini = floor(sampleData[i]/binWidth) - bin0;
        label binii = min(bini + 1, nBin - 1);
154

155 156 157 158
        scalar d1 = mag(sampleData[i] - xy_[bini][0]);
        scalar d2 = mag(xy_[binii][0] - sampleData[i]);

        if (d1 < d2)
159
        {
160 161 162 163 164
            xy_[bini][1]++;
        }
        else
        {
            xy_[binii][1]++;
165 166 167
        }
    }

168
    initialise();
169 170 171 172 173 174
}


Foam::distributionModels::binned::binned(const binned& p)
:
    distributionModel(p),
175 176
    xy_(p.xy_),
    meanValue_(p.meanValue_)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
{}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::distributionModels::binned::~binned()
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

Foam::scalar Foam::distributionModels::binned::sample() const
{
    scalar y = rndGen_.sample01<scalar>();

192
    for (label i = 0; i < xy_.size() - 1; ++i)
193
    {
194
        if (xy_[i][1] > y)
195
        {
196
            return xy_[i][0];
197 198 199
        }
    }

200
    return xy_.last()[0];
201 202 203 204 205
}


Foam::scalar Foam::distributionModels::binned::minValue() const
{
206
    return xy_.first()[0];
207 208 209 210 211
}


Foam::scalar Foam::distributionModels::binned::maxValue() const
{
212
    return xy_.last()[0];
213 214 215 216 217 218 219 220 221 222 223 224
}


Foam::scalar Foam::distributionModels::binned::meanValue() const
{
    return meanValue_;
}


void Foam::distributionModels::binned::readData(Istream& is)
{
//    distributionModel::readData(is);
225
    is  >> xy_;
226 227 228 229 230 231
}


void Foam::distributionModels::binned::writeData(Ostream& os) const
{
//    distributionModel::writeData(os);
232
    os  << xy_ ;
233 234 235 236 237 238 239 240 241 242
}


Foam::dictionary Foam::distributionModels::binned::writeDict
(
    const word& dictName
) const
{
//    dictionary dict = distributionModel::writeDict(dictName);
    dictionary dict(dictName);
243
    dict.add("distribution", xy_);
244 245 246 247 248 249 250 251

    return dict;
}


void Foam::distributionModels::binned::readDict(const dictionary& dict)
{
//    distributionModel::readDict(dict);
252
    dict.readEntry("distribution", xy_);
253 254 255
}


256 257 258 259 260
Foam::Ostream& Foam::operator<<
(
    Ostream& os,
    const distributionModels::binned& b
)
261
{
262
    os.check(FUNCTION_NAME);
263 264 265 266 267 268

    b.writeData(os);
    return os;
}


269
Foam::Istream& Foam::operator>>(Istream& is, distributionModels::binned& b)
270
{
271
    is.check(FUNCTION_NAME);
272 273 274 275 276 277 278

    b.readData(is);
    return is;
}


// ************************************************************************* //