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

View File

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

View File

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

View File

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