diff --git a/src/functionObjects/field/histogram/histogram.C b/src/functionObjects/field/histogram/histogram.C index 064fb9dded..39c2649fab 100644 --- a/src/functionObjects/field/histogram/histogram.C +++ b/src/functionObjects/field/histogram/histogram.C @@ -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 yPtrs(1); - yPtrs[0] = &values; + wordList fieldNames(2); + fieldNames[0] = fieldName; + fieldNames[1] = fieldName + "Count"; + List 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("min", 0); + + max_ = dict.lookupOrDefault("max", -GREAT); + min_ = dict.lookupOrDefault("min", GREAT); dict.lookup("nBins") >> nBins_; word format(dict.lookup("setFormat")); @@ -149,38 +160,63 @@ bool Foam::functionObjects::histogram::write() : obr_.lookupObject(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()); + Pstream::listCombineGather(dataNormalized, plusEqOp()); + Pstream::listCombineGather(dataCount, plusEqOp