ENH: histogram: 1) if max is not provided use field max. 2) output count. Fixes #467.

This commit is contained in:
mattijs
2017-05-17 12:52:13 +01:00
parent 1889ea83e3
commit 77865d22dd
2 changed files with 67 additions and 22 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -45,23 +45,31 @@ void Foam::functionObjects::histogram::writeGraph
( (
const coordSet& coords, const coordSet& coords,
const word& fieldName, const word& fieldName,
const scalarField& values const scalarField& normalizedValues,
const scalarField& absoluteValues
) const ) const
{ {
const wordList fieldNames(1, fieldName);
fileName outputPath = baseTimeDir(); fileName outputPath = baseTimeDir();
mkDir(outputPath); mkDir(outputPath);
OFstream graphFile OFstream graphFile
( (
outputPath/formatterPtr_().getFileName(coords, fieldNames) outputPath
/formatterPtr_().getFileName
(
coords,
wordList(1, fieldName)
)
); );
Log << " Writing histogram of " << fieldName Log << " Writing histogram of " << fieldName
<< " to " << graphFile.name() << endl; << " to " << graphFile.name() << endl;
List<const scalarField*> yPtrs(1); wordList fieldNames(2);
yPtrs[0] = &values; fieldNames[0] = fieldName;
fieldNames[1] = fieldName + "Count";
List<const scalarField*> yPtrs(2);
yPtrs[0] = &normalizedValues;
yPtrs[1] = &absoluteValues;
formatterPtr_().write(coords, fieldNames, yPtrs, graphFile); formatterPtr_().write(coords, fieldNames, yPtrs, graphFile);
} }
@ -76,7 +84,9 @@ Foam::functionObjects::histogram::histogram
) )
: :
fvMeshFunctionObject(name, runTime, dict), fvMeshFunctionObject(name, runTime, dict),
writeFile(obr_, name) writeFile(obr_, name),
max_(-GREAT),
min_(GREAT)
{ {
read(dict); read(dict);
} }
@ -96,8 +106,9 @@ bool Foam::functionObjects::histogram::read(const dictionary& dict)
writeFile::read(dict); writeFile::read(dict);
dict.lookup("field") >> fieldName_; dict.lookup("field") >> fieldName_;
dict.lookup("max") >> max_;
min_ = dict.lookupOrDefault<scalar>("min", 0); max_ = dict.lookupOrDefault<scalar>("max", -GREAT);
min_ = dict.lookupOrDefault<scalar>("min", GREAT);
dict.lookup("nBins") >> nBins_; dict.lookup("nBins") >> nBins_;
word format(dict.lookup("setFormat")); word format(dict.lookup("setFormat"));
@ -149,38 +160,63 @@ bool Foam::functionObjects::histogram::write()
: obr_.lookupObject<volScalarField>(fieldName_) : obr_.lookupObject<volScalarField>(fieldName_)
); );
scalar histMax = max_;
scalar histMin = min_;
if (max_ == -GREAT)
{
// Determine current min and max
histMax = max(field).value();
if (min_ == GREAT)
{
histMin = min(field).value();
}
Log << " Determined histogram bounds from field"
<< " min/max(" << fieldName_ << ") = "
<< histMin << ' ' << histMax << endl;
}
else if (min_ == GREAT)
{
histMin = 0;
}
// Calculate the mid-points of bins for the graph axis // Calculate the mid-points of bins for the graph axis
pointField xBin(nBins_); pointField xBin(nBins_);
const scalar delta = (max_- min_)/nBins_; const scalar delta = (histMax- histMin)/nBins_;
scalar x = min_ + 0.5*delta; scalar x = histMin + 0.5*delta;
forAll(xBin, i) forAll(xBin, i)
{ {
xBin[i] = point(x, 0, 0); xBin[i] = point(x, 0, 0);
x += delta; x += delta;
} }
scalarField data(nBins_, 0); scalarField dataNormalized(nBins_, 0);
labelField dataCount(nBins_, 0);
const scalarField& V = mesh_.V(); const scalarField& V = mesh_.V();
forAll(field, celli) forAll(field, celli)
{ {
const label bini = (field[celli] - min_)/delta; const label bini = (field[celli] - histMin)/delta;
if (bini >= 0 && bini < nBins_) if (bini >= 0 && bini < nBins_)
{ {
data[bini] += V[celli]; dataNormalized[bini] += V[celli];
dataCount[bini]++;
} }
} }
Pstream::listCombineGather(data, plusEqOp<scalar>()); Pstream::listCombineGather(dataNormalized, plusEqOp<scalar>());
Pstream::listCombineGather(dataCount, plusEqOp<label>());
if (Pstream::master()) if (Pstream::master())
{ {
const scalar sumData = sum(data); const scalar sumData = sum(dataNormalized);
if (sumData > SMALL) if (sumData > SMALL)
{ {
data /= sumData; dataNormalized /= sumData;
const coordSet coords const coordSet coords
( (
@ -190,7 +226,15 @@ bool Foam::functionObjects::histogram::write()
mag(xBin) mag(xBin)
); );
writeGraph(coords, fieldName_, data);
// Convert count field from labelField to scalarField
scalarField count(dataCount.size());
forAll(count, i)
{
count[i] = 1.0*dataCount[i];
}
writeGraph(coords, fieldName_, dataNormalized, count);
} }
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -53,7 +53,7 @@ Usage
type | type name: histogram | yes | type | type name: histogram | yes |
field | Field to analyse | yes | field | Field to analyse | yes |
nBins | Number of bins for the histogram | yes| nBins | Number of bins for the histogram | yes|
max | Maximum value sampled | yes | max | Maximum value sampled | no | field max
min | minimum value sampled | no | 0 min | minimum value sampled | no | 0
setFormat | Output format | yes | setFormat | Output format | yes |
\endtable \endtable
@ -115,7 +115,8 @@ class histogram
( (
const coordSet& coords, const coordSet& coords,
const word& valueName, const word& valueName,
const scalarField& values const scalarField& normalizedValues,
const scalarField& absoluteValues
) const; ) const;
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct