STYLE: use MinMax for handling histogram ranges

This commit is contained in:
Mark Olesen
2022-11-09 11:17:23 +01:00
committed by Andrew Heather
parent 941cd7fef4
commit 0624fb0181
4 changed files with 40 additions and 51 deletions

View File

@ -53,8 +53,7 @@ Foam::histogramModels::equalBinWidth::equalBinWidth
:
histogramModel(name, mesh, dict),
nBins_(0),
min_(GREAT),
max_(-GREAT)
range_()
{
read(dict);
}
@ -69,7 +68,13 @@ bool Foam::histogramModels::equalBinWidth::read(const dictionary& dict)
return false;
}
nBins_ = dict.getScalar("nBins");
range_.reset
(
dict.getOrDefault<scalar>("min", GREAT),
dict.getOrDefault<scalar>("max", -GREAT)
);
nBins_ = dict.get<scalar>("nBins");
if (nBins_ < 1)
{
@ -79,9 +84,6 @@ bool Foam::histogramModels::equalBinWidth::read(const dictionary& dict)
<< abort(FatalIOError);
}
min_ = dict.getOrDefault<scalar>("min", GREAT);
max_ = dict.getOrDefault<scalar>("max", -GREAT);
return true;
}
@ -93,44 +95,44 @@ bool Foam::histogramModels::equalBinWidth::write(const bool log)
// Determine min and max from the operand field
// if the user did not provide any min or max
scalar histMax = max_;
scalar histMin = min_;
if (max_ == -GREAT)
scalarMinMax histRange(range_);
if (histRange.max() == -GREAT)
{
histMax = max(field).value();
histRange.max() = max(field).value();
if (min_ == GREAT)
if (histRange.min() == GREAT)
{
histMin = min(field).value();
histRange.min() = min(field).value();
}
if (log)
{
Info<< " Determined histogram bounds from field"
<< " min/max(" << fieldName() << ") = "
<< histMin << ' ' << histMax << endl;
<< histRange << endl;
}
}
else if (min_ == GREAT)
else if (histRange.min() == GREAT)
{
histMin = 0;
histRange.min() = Zero;
}
if (histMax < histMin)
if (!histRange.good())
{
FatalErrorInFunction
<< "Histogram minimum = " << histMin
<< ", cannot be larger than histogram maximum = " << histMax
<< "Invalid histogram range: " << histRange
<< exit(FatalError);
}
// Calculate the mid-points of bins for the graph axis
pointField binMidPoints(nBins_, Zero);
const scalar delta = (histMax - histMin)/nBins_;
const scalar delta = histRange.span()/nBins_;
{
scalar x = histMin + 0.5*delta;
scalar x = histRange.min() + 0.5*delta;
for (point& p : binMidPoints)
{
p.x() = x;
@ -146,7 +148,7 @@ bool Foam::histogramModels::equalBinWidth::write(const bool log)
forAll(field, celli)
{
const label bini = (field[celli] - histMin)/delta;
const label bini = (field[celli] - histRange.min())/delta;
if (bini >= 0 && bini < nBins_)
{
dataNormalised[bini] += V[celli];

View File

@ -69,6 +69,7 @@ SourceFiles
#define Foam_histogramModels_equalBinWidth_H
#include "histogramModel.H"
#include "MinMax.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -90,11 +91,8 @@ class equalBinWidth
//- Number of bins
label nBins_;
//- Minimum value of histogram data
scalar min_;
//- Maximum value of histogram data
scalar max_;
//- Min/max for histogram data
scalarMinMax range_;
public:

View File

@ -52,7 +52,7 @@ Foam::histogramModels::unequalBinWidth::unequalBinWidth
:
histogramModel(name, mesh, dict),
nBins_(-1),
ranges_(Zero)
ranges_()
{
read(dict);
}
@ -67,32 +67,26 @@ bool Foam::histogramModels::unequalBinWidth::read(const dictionary& dict)
return false;
}
ranges_ = dict.get<List<Pair<scalar>>>("ranges");
ranges_ = dict.get<List<scalarMinMax>>("ranges");
nBins_ = ranges_.size();
forAll(ranges_, bini)
{
const auto& range = ranges_[bini];
const scalar min = range.first();
const scalar max = range.second();
if (max < min)
if (!range.good())
{
FatalIOErrorInFunction(dict)
<< "For bin-" << bini
<< ", min is larger than max."
<< " min = " << min
<< " max = " << max
<< "Histogram bin-" << bini
<< " has invalid range: " << range
<< abort(FatalIOError);
}
}
nBins_ = ranges_.size();
if (nBins_ < 1)
{
FatalIOErrorInFunction(dict)
<< "Number of histogram bins = " << nBins_
<< " cannot be negative or zero."
<< "Invalid number of histogram bins: " << nBins_
<< abort(FatalIOError);
}
@ -110,13 +104,7 @@ bool Foam::histogramModels::unequalBinWidth::write(const bool log)
forAll(ranges_, bini)
{
point& p = midBin[bini];
const auto& range = ranges_[bini];
const scalar min = range.first();
const scalar max = range.second();
const scalar delta = max - min;
p.x() = min + 0.5*delta;
midBin[bini].x() = ranges_[bini].centre();
}
@ -130,10 +118,10 @@ bool Foam::histogramModels::unequalBinWidth::write(const bool log)
forAll(ranges_, bini)
{
const auto& range = ranges_[bini];
const scalar min = range.first();
const scalar max = range.second();
if (field[celli] >= min && field[celli] < max)
// Like range.contains(field[celli]) but exclusive on max()
if (field[celli] >= range.min() && field[celli] < range.max())
{
dataNormalised[bini] += V[celli];
dataCount[bini]++;

View File

@ -52,7 +52,7 @@ Usage
\table
Property | Description | Type | Reqd | Deflt
ranges | Min-max values of histogram data <!--
--> | List\<Pair\<scalar\>\> | no | -
--> | List\<scalarMinMax\> | no | -
\endtable
Note
@ -70,6 +70,7 @@ SourceFiles
#define Foam_histogramModels_unequalBinWidth_H
#include "histogramModel.H"
#include "MinMax.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -92,7 +93,7 @@ class unequalBinWidth
label nBins_;
//- Lower and upper ranges of operand bins
List<Pair<scalar>> ranges_;
List<scalarMinMax> ranges_;
public: