histogram.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016 OpenFOAM Foundation
9  Copyright (C) 2016-2017 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "histogram.H"
30 #include "volFields.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace functionObjects
38 {
39  defineTypeNameAndDebug(histogram, 0);
40  addToRunTimeSelectionTable(functionObject, histogram, dictionary);
41 }
42 }
43 
44 
45 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
46 
47 void Foam::functionObjects::histogram::writeGraph
48 (
49  const coordSet& coords,
50  const word& fieldName,
51  const scalarField& normalizedValues,
52  const scalarField& absoluteValues
53 ) const
54 {
55  fileName outputPath = baseTimeDir();
56  mkDir(outputPath);
57  OFstream graphFile
58  (
59  outputPath
60  /formatterPtr_().getFileName
61  (
62  coords,
63  wordList(1, fieldName)
64  )
65  );
66 
67  Log << " Writing histogram of " << fieldName
68  << " to " << graphFile.name() << endl;
69 
70  wordList fieldNames(2);
71  fieldNames[0] = fieldName;
72  fieldNames[1] = fieldName + "Count";
73  List<const scalarField*> yPtrs(2);
74  yPtrs[0] = &normalizedValues;
75  yPtrs[1] = &absoluteValues;
76  formatterPtr_().write(coords, fieldNames, yPtrs, graphFile);
77 }
78 
79 
80 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
81 
82 Foam::functionObjects::histogram::histogram
83 (
84  const word& name,
85  const Time& runTime,
86  const dictionary& dict
87 )
88 :
90  writeFile(obr_, name),
91  max_(-GREAT),
92  min_(GREAT)
93 {
94  read(dict);
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
99 
101 {}
102 
103 
104 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
105 
107 {
110 
111  dict.readEntry("field", fieldName_);
112 
113  max_ = dict.lookupOrDefault<scalar>("max", -GREAT);
114  min_ = dict.lookupOrDefault<scalar>("min", GREAT);
115  dict.readEntry("nBins", nBins_);
116 
117  const word format(dict.get<word>("setFormat"));
118  formatterPtr_ = writer<scalar>::New(format);
119 
120  return true;
121 }
122 
123 
125 {
126  return true;
127 }
128 
129 
131 {
132  Log << type() << " " << name() << " write:" << nl;
133 
134  autoPtr<volScalarField> fieldPtr;
135  if (obr_.foundObject<volScalarField>(fieldName_))
136  {
137  Log << " Looking up field " << fieldName_ << endl;
138  }
139  else
140  {
141  Log << " Reading field " << fieldName_ << endl;
142  fieldPtr.reset
143  (
144  new volScalarField
145  (
146  IOobject
147  (
148  fieldName_,
149  mesh_.time().timeName(),
150  mesh_,
153  ),
154  mesh_
155  )
156  );
157  }
158 
159  const volScalarField& field =
160  (
161  fieldPtr.valid()
162  ? fieldPtr()
163  : obr_.lookupObject<volScalarField>(fieldName_)
164  );
165 
166 
167  scalar histMax = max_;
168  scalar histMin = min_;
169 
170  if (max_ == -GREAT)
171  {
172  // Determine current min and max
173  histMax = max(field).value();
174 
175  if (min_ == GREAT)
176  {
177  histMin = min(field).value();
178  }
179  Log << " Determined histogram bounds from field"
180  << " min/max(" << fieldName_ << ") = "
181  << histMin << ' ' << histMax << endl;
182  }
183  else if (min_ == GREAT)
184  {
185  histMin = 0;
186  }
187 
188  // Calculate the mid-points of bins for the graph axis
189  pointField xBin(nBins_);
190  const scalar delta = (histMax- histMin)/nBins_;
191 
192  scalar x = histMin + 0.5*delta;
193  forAll(xBin, i)
194  {
195  xBin[i] = point(x, 0, 0);
196  x += delta;
197  }
198 
199  scalarField dataNormalized(nBins_, Zero);
200  labelField dataCount(nBins_, Zero);
201  const scalarField& V = mesh_.V();
202 
203  forAll(field, celli)
204  {
205  const label bini = (field[celli] - histMin)/delta;
206  if (bini >= 0 && bini < nBins_)
207  {
208  dataNormalized[bini] += V[celli];
209  dataCount[bini]++;
210  }
211  }
212 
213  Pstream::listCombineGather(dataNormalized, plusEqOp<scalar>());
215 
216  if (Pstream::master())
217  {
218  const scalar sumData = sum(dataNormalized);
219 
220  if (sumData > SMALL)
221  {
222  dataNormalized /= sumData;
223 
224  const coordSet coords
225  (
226  fieldName_,
227  "x",
228  xBin,
229  mag(xBin)
230  );
231 
232 
233  // Convert count field from labelField to scalarField
234  scalarField count(dataCount.size());
235  forAll(count, i)
236  {
237  count[i] = 1.0*dataCount[i];
238  }
239 
240  writeGraph(coords, fieldName_, dataNormalized, count);
241  }
242  }
243 
244  return true;
245 }
246 
247 
248 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::autoPtr::reset
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:158
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::functionObjects::histogram::write
virtual bool write()
Calculate the histogram and write.
Definition: histogram.C:130
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
histogram.H
Foam::autoPtr::valid
bool valid() const noexcept
True if the managed pointer is non-null.
Definition: autoPtrI.H:107
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
Foam::functionObjects::histogram::execute
virtual bool execute()
Execute, currently does nothing.
Definition: histogram.C:124
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, add, dictionary)
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:59
format
word format(conversionProperties.get< word >("format"))
Foam::functionObjects::writeFile::read
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:212
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Field< vector >
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
delta
scalar delta
Definition: LISASMDCalcMethod2.H:8
field
rDeltaTY field()
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::functionObjects::histogram::~histogram
virtual ~histogram()
Definition: histogram.C:100
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:166
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::coordSet
Holds list of sampling positions.
Definition: coordSet.H:53
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::histogram::read
virtual bool read(const dictionary &)
Read the histogram data.
Definition: histogram.C:106
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::Pstream::listCombineGather
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
Definition: combineGatherScatter.C:290
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:74
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::functionObjects::writeFile
functionObject base class for writing single files
Definition: writeFile.H:59
Foam::plusEqOp
Definition: ops.H:72
Foam::point
vector point
Point is a vector.
Definition: point.H:43
Foam::functionObjects::writeFile::baseTimeDir
fileName baseTimeDir() const
Return the base directory for the current time value.
Definition: writeFile.C:77
Foam::GeometricField< scalar, fvPatchField, volMesh >
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
Log
#define Log
Report write to Foam::Info if the local log switch is true.
Definition: messageStream.H:332
Foam::IOobject::MUST_READ
Definition: IOobject.H:120
Foam::writer::New
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: writer.C:38