histogram.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2016-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "histogram.H"
27 #include "volFields.H"
28 #include "setWriter.H"
29 #include "polyTopoChangeMap.H"
30 #include "polyMeshMap.H"
31 #include "polyDistributionMap.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
42 }
43 }
44 
45 
46 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
47 
49 (
50  const word& name,
51  const Time& runTime,
52  const dictionary& dict
53 )
54 :
55  fvMeshFunctionObject(name, runTime, dict),
56  zone_(mesh(), dict),
57  file_(obr_, name)
58 {
59  read(dict);
60 }
61 
62 
63 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
64 
66 {}
67 
68 
69 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
70 
72 {
73  dict.lookup("field") >> fieldName_;
74  dict.lookup("max") >> max_;
75  min_ = dict.lookupOrDefault<scalar>("min", 0);
76  dict.lookup("nBins") >> nBins_;
77 
78  formatterPtr_ = setWriter::New(dict.lookup("setFormat"), dict);
79 
80  return true;
81 }
82 
83 
85 {
86  return wordList(fieldName_);
87 }
88 
89 
91 {
92  return true;
93 }
94 
95 
97 {
98  Log << type() << " " << name() << " write:" << nl;
99 
100  zone_.regenerate();
101 
102  const volScalarField& field
103  (
104  obr_.lookupObject<volScalarField>(fieldName_)
105  );
106 
107  // Calculate the mid-points of bins for the graph axis
108  scalarField xBin(nBins_);
109  const scalar delta = (max_- min_)/nBins_;
110  scalar x = min_ + 0.5*delta;
111  forAll(xBin, i)
112  {
113  xBin[i] = x;
114  x += delta;
115  }
116 
117  scalarField volFrac(nBins_, 0);
118  const scalarField& V = mesh_.V();
119 
120  if (zone_.all())
121  {
122  forAll(field, celli)
123  {
124  const label bini = (field[celli] - min_)/delta;
125  if (bini >= 0 && bini < nBins_)
126  {
127  volFrac[bini] += V[celli];
128  }
129  }
130  }
131  else
132  {
133  const labelList& zoneCells = zone_.zone();
134  forAll(zoneCells, i)
135  {
136  const label celli = zoneCells[i];
137  const label bini = (field[celli] - min_)/delta;
138  if (bini >= 0 && bini < nBins_)
139  {
140  volFrac[bini] += V[celli];
141  }
142  }
143  }
144 
146 
147  if (Pstream::master())
148  {
149  const scalar sumVol = sum(volFrac);
150 
151  if (sumVol > small)
152  {
153  volFrac /= sumVol;
154 
155  formatterPtr_().write
156  (
157  file_.baseTimeDir(),
158  typeName,
159  coordSet(true, fieldName_, xBin),
160  "v/vTotal",
161  volFrac
162  );
163  }
164  }
165 
166  return true;
167 }
168 
169 
171 (
172  const polyMesh& mesh
173 )
174 {
175  if (&mesh == &this->mesh())
176  {
177  zone_.movePoints();
178  }
179 }
180 
181 
183 (
184  const polyTopoChangeMap& map
185 )
186 {
187  if (&map.mesh() == &mesh())
188  {
189  zone_.topoChange(map);
190  }
191 }
192 
193 
195 (
196  const polyMeshMap& map
197 )
198 {
199  if (&map.mesh() == &mesh())
200  {
201  zone_.mapMesh(map);
202  }
203 }
204 
205 
207 (
208  const polyDistributionMap& map
209 )
210 {
211  if (&map.mesh() == &mesh())
212  {
213  zone_.distribute(map);
214  }
215 }
216 
217 
218 // ************************************************************************* //
scalar delta
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Macros for easy insertion into run-time selection tables.
Generic GeometricField class.
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
Holds list of sampling positions.
Definition: coordSet.H:51
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Abstract base-class for Time/database functionObjects.
Specialisation of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Write the volume-weighted histogram of a volScalarField.
Definition: histogram.H:151
virtual wordList fields() const
Return the list of fields required.
Definition: histogram.C:84
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: histogram.C:183
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: histogram.C:207
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: histogram.C:195
virtual void movePoints(const polyMesh &)
Update for mesh motion.
Definition: histogram.C:171
virtual bool execute()
Execute, currently does nothing.
Definition: histogram.C:90
virtual bool write()
Calculate the histogram and write.
Definition: histogram.C:96
virtual bool read(const dictionary &)
Read the histogram data.
Definition: histogram.C:71
histogram(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: histogram.C:49
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
const polyMesh & mesh() const
Return polyMesh.
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
const polyMesh & mesh() const
Return polyMesh.
Definition: polyMeshMap.H:75
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:78
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
const polyMesh & mesh() const
Return polyMesh.
static autoPtr< setWriter > New(const word &writeType, const IOstream::streamFormat writeFormat=IOstream::ASCII, const IOstream::compressionType writeCompression=IOstream::UNCOMPRESSED)
Select given write options.
Definition: setWriter.C:286
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling words, derived from string.
Definition: word.H:63
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
#define Log
Report write to Foam::Info if the local log switch is true.
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
addToRunTimeSelectionTable(functionObject, fvModel, dictionary)
Namespace for OpenFOAM.
List< word > wordList
A List of words.
Definition: fileName.H:54
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:59
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
static const char nl
Definition: Ostream.H:297
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict