mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: histogram: 1) if max is not provided use field max. 2) output count. Fixes #467.
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
|
||||
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
|
||||
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -45,23 +45,31 @@ void Foam::functionObjects::histogram::writeGraph
|
||||
(
|
||||
const coordSet& coords,
|
||||
const word& fieldName,
|
||||
const scalarField& values
|
||||
const scalarField& normalizedValues,
|
||||
const scalarField& absoluteValues
|
||||
) const
|
||||
{
|
||||
const wordList fieldNames(1, fieldName);
|
||||
|
||||
fileName outputPath = baseTimeDir();
|
||||
mkDir(outputPath);
|
||||
OFstream graphFile
|
||||
(
|
||||
outputPath/formatterPtr_().getFileName(coords, fieldNames)
|
||||
outputPath
|
||||
/formatterPtr_().getFileName
|
||||
(
|
||||
coords,
|
||||
wordList(1, fieldName)
|
||||
)
|
||||
);
|
||||
|
||||
Log << " Writing histogram of " << fieldName
|
||||
<< " to " << graphFile.name() << endl;
|
||||
|
||||
List<const scalarField*> yPtrs(1);
|
||||
yPtrs[0] = &values;
|
||||
wordList fieldNames(2);
|
||||
fieldNames[0] = fieldName;
|
||||
fieldNames[1] = fieldName + "Count";
|
||||
List<const scalarField*> yPtrs(2);
|
||||
yPtrs[0] = &normalizedValues;
|
||||
yPtrs[1] = &absoluteValues;
|
||||
formatterPtr_().write(coords, fieldNames, yPtrs, graphFile);
|
||||
}
|
||||
|
||||
@ -76,7 +84,9 @@ Foam::functionObjects::histogram::histogram
|
||||
)
|
||||
:
|
||||
fvMeshFunctionObject(name, runTime, dict),
|
||||
writeFile(obr_, name)
|
||||
writeFile(obr_, name),
|
||||
max_(-GREAT),
|
||||
min_(GREAT)
|
||||
{
|
||||
read(dict);
|
||||
}
|
||||
@ -96,8 +106,9 @@ bool Foam::functionObjects::histogram::read(const dictionary& dict)
|
||||
writeFile::read(dict);
|
||||
|
||||
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_;
|
||||
|
||||
word format(dict.lookup("setFormat"));
|
||||
@ -149,38 +160,63 @@ bool Foam::functionObjects::histogram::write()
|
||||
: 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
|
||||
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)
|
||||
{
|
||||
xBin[i] = point(x, 0, 0);
|
||||
x += delta;
|
||||
}
|
||||
|
||||
scalarField data(nBins_, 0);
|
||||
scalarField dataNormalized(nBins_, 0);
|
||||
labelField dataCount(nBins_, 0);
|
||||
const scalarField& V = mesh_.V();
|
||||
|
||||
forAll(field, celli)
|
||||
{
|
||||
const label bini = (field[celli] - min_)/delta;
|
||||
const label bini = (field[celli] - histMin)/delta;
|
||||
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())
|
||||
{
|
||||
const scalar sumData = sum(data);
|
||||
const scalar sumData = sum(dataNormalized);
|
||||
|
||||
if (sumData > SMALL)
|
||||
{
|
||||
data /= sumData;
|
||||
dataNormalized /= sumData;
|
||||
|
||||
const coordSet coords
|
||||
(
|
||||
@ -190,7 +226,15 @@ bool Foam::functionObjects::histogram::write()
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -53,7 +53,7 @@ Usage
|
||||
type | type name: histogram | yes |
|
||||
field | Field to analyse | 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
|
||||
setFormat | Output format | yes |
|
||||
\endtable
|
||||
@ -115,7 +115,8 @@ class histogram
|
||||
(
|
||||
const coordSet& coords,
|
||||
const word& valueName,
|
||||
const scalarField& values
|
||||
const scalarField& normalizedValues,
|
||||
const scalarField& absoluteValues
|
||||
) const;
|
||||
|
||||
//- Disallow default bitwise copy construct
|
||||
|
||||
Reference in New Issue
Block a user