diff --git a/src/functionObjects/field/histogram/histogramModels/equalBinWidth/equalBinWidth.C b/src/functionObjects/field/histogram/histogramModels/equalBinWidth/equalBinWidth.C index 01bec912e9..790e6213c5 100644 --- a/src/functionObjects/field/histogram/histogramModels/equalBinWidth/equalBinWidth.C +++ b/src/functionObjects/field/histogram/histogramModels/equalBinWidth/equalBinWidth.C @@ -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("min", GREAT), + dict.getOrDefault("max", -GREAT) + ); + + nBins_ = dict.get("nBins"); if (nBins_ < 1) { @@ -79,9 +84,6 @@ bool Foam::histogramModels::equalBinWidth::read(const dictionary& dict) << abort(FatalIOError); } - min_ = dict.getOrDefault("min", GREAT); - max_ = dict.getOrDefault("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]; diff --git a/src/functionObjects/field/histogram/histogramModels/equalBinWidth/equalBinWidth.H b/src/functionObjects/field/histogram/histogramModels/equalBinWidth/equalBinWidth.H index e61b76f30d..ecd80d90cf 100644 --- a/src/functionObjects/field/histogram/histogramModels/equalBinWidth/equalBinWidth.H +++ b/src/functionObjects/field/histogram/histogramModels/equalBinWidth/equalBinWidth.H @@ -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: diff --git a/src/functionObjects/field/histogram/histogramModels/unequalBinWidth/unequalBinWidth.C b/src/functionObjects/field/histogram/histogramModels/unequalBinWidth/unequalBinWidth.C index b4b94c4337..658c54bd13 100644 --- a/src/functionObjects/field/histogram/histogramModels/unequalBinWidth/unequalBinWidth.C +++ b/src/functionObjects/field/histogram/histogramModels/unequalBinWidth/unequalBinWidth.C @@ -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>>("ranges"); + ranges_ = dict.get>("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]++; diff --git a/src/functionObjects/field/histogram/histogramModels/unequalBinWidth/unequalBinWidth.H b/src/functionObjects/field/histogram/histogramModels/unequalBinWidth/unequalBinWidth.H index c9c9815d83..c4fa60ff68 100644 --- a/src/functionObjects/field/histogram/histogramModels/unequalBinWidth/unequalBinWidth.H +++ b/src/functionObjects/field/histogram/histogramModels/unequalBinWidth/unequalBinWidth.H @@ -52,7 +52,7 @@ Usage \table Property | Description | Type | Reqd | Deflt ranges | Min-max values of histogram data | List\\> | no | - + --> | List\ | 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> ranges_; + List ranges_; public: