particleDistribution.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-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "particleDistribution.H"
30 #include "general.H"
31 #include "fvMesh.H"
32 #include "cloud.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
40  defineTypeNameAndDebug(particleDistribution, 0);
42  (
43  functionObject,
44  particleDistribution,
45  dictionary
46  );
47 }
48 }
49 
50 
51 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
52 
54 (
55  const word& name,
56  const Time& runTime,
57  const dictionary& dict
58 )
59 :
62  cloudName_("unknown-cloudName"),
63  tagFieldName_("none"),
64  rndGen_(),
65  nameVsBinWidth_(),
66  writerPtr_(nullptr)
67 {
68  read(dict);
69 }
70 
71 
72 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
73 
75 {
77  {
78  dict.readEntry("cloud", cloudName_);
79  dict.readIfPresent("tagField", tagFieldName_);
80  dict.readEntry("nameVsBinWidth", nameVsBinWidth_);
81  const word format(dict.get<word>("setFormat"));
83 
84  Info<< type() << " " << name() << " output:" << nl
85  << " Processing cloud : " << cloudName_ << nl
86  << endl;
87 
88  return true;
89  }
90 
91  return false;
92 }
93 
94 
96 {
97  return true;
98 }
99 
100 
102 {
103  Log << type() << " " << name() << " output:" << endl;
104 
105  if (!mesh_.foundObject<cloud>(cloudName_))
106  {
107  wordList cloudNames(mesh_.names<cloud>());
108 
110  << "Unable to find cloud " << cloudName_
111  << " in the mesh database. Available clouds include:"
112  << cloudNames << endl;
113 
114  return false;
115  }
116 
117  const cloud& c = mesh_.lookupObject<cloud>(cloudName_);
118 
119  objectRegistry cloudObr
120  (
121  IOobject
122  (
123  scopedName("CloudRegistry"),
124  mesh_.time().timeName(),
126  mesh_.time(),
129  )
130  );
131 
132  c.writeObjects(cloudObr);
133 
134  List<DynamicList<label>> tagAddr;
135  if
136  (
137  tagFieldName_ != "none"
138  && cloudObr.foundObject<IOField<scalar>>(tagFieldName_)
139  )
140  {
141  // Tag field present - generate distribution per tag
142  const IOField<label>& tag =
143  cloudObr.lookupObject<IOField<label>>(tagFieldName_);
144  const labelHashSet tagMap(tag);
145  const label tagMax = tagMap.size();
146 
147  List<DynamicList<label>> tagAddr(tagMax);
148  forAll(tag, i)
149  {
150  label newTag = tagMap[tag[i]];
151  tagAddr[newTag].append(i);
152  }
153  }
154 
155 
156  bool ok = false;
157  forAll(nameVsBinWidth_, i)
158  {
159  ok = false;
160  ok = ok || processField<scalar>(cloudObr, i, tagAddr);
161  ok = ok || processField<vector>(cloudObr, i, tagAddr);
162  ok = ok || processField<tensor>(cloudObr, i, tagAddr);
163  ok = ok || processField<sphericalTensor>(cloudObr, i, tagAddr);
164  ok = ok || processField<symmTensor>(cloudObr, i, tagAddr);
165  ok = ok || processField<tensor>(cloudObr, i, tagAddr);
166 
167  if (log && !ok)
168  {
170  << "Unable to find field " << nameVsBinWidth_[i].first()
171  << " in the " << cloudName_ << " cloud database" << endl;
172  }
173  }
174 
175  return true;
176 }
177 
178 
180 (
181  const word& fieldName,
182  const scalarField& field,
183  const scalar binWidth,
184  const label tag
185 )
186 {
187  if (field.empty())
188  {
189  return;
190  }
191 
192  word fName(fieldName);
193  if (tag != -1)
194  {
195  fName = fName + '_' + Foam::name(tag);
196  }
197 
199  (
200  field,
201  binWidth,
202  rndGen_
203  );
204 
205  const Field<scalar> distX(distribution.x());
206  const Field<scalar> distY(distribution.y());
207 
208  pointField xBin(distX.size(), Zero);
209  xBin.replace(0, distX);
210  const coordSet coords
211  (
212  fName,
213  "x",
214  xBin,
215  distX
216  );
217 
218  const wordList fieldNames(1, fName);
219 
220  fileName outputPath(baseTimeDir());
221  mkDir(outputPath);
222  OFstream graphFile(outputPath/writerPtr_->getFileName(coords, fieldNames));
223 
224  Log << " Writing distribution of " << fieldName
225  << " to " << graphFile.name() << endl;
226 
227  List<const scalarField*> yPtrs(1);
228  yPtrs[0] = &distY;
229  writerPtr_->write(coords, fieldNames, yPtrs, graphFile);
230 }
231 
232 
233 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::cloud::prefix
static const word prefix
The prefix to local: lagrangian.
Definition: cloud.H:87
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::distribution
Accumulating histogram of values. Specified bin resolution automatic generation of bins.
Definition: distribution.H:61
Log
#define Log
Definition: PDRblock.C:35
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::functionObjects::particleDistribution::read
virtual bool read(const dictionary &)
Read the particleDistribution data.
Definition: particleDistribution.C:74
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::functionObjects::particleDistribution::cloudName_
word cloudName_
Cloud name.
Definition: particleDistribution.H:186
Foam::functionObjects::particleDistribution::write
virtual bool write()
Write the particleDistribution.
Definition: particleDistribution.C:101
cloud.H
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:175
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::HashSet< label, Hash< label > >
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
Foam::distributionModels::general
Particle-size distribution model wherein random samples are drawn from a given arbitrary probability ...
Definition: general.H:173
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
format
word format(conversionProperties.get< word >("format"))
Foam::functionObjects::writeFile::read
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:213
Foam::functionObjects::particleDistribution::writerPtr_
autoPtr< writer< scalar > > writerPtr_
Writer.
Definition: particleDistribution.H:198
Foam::Field< scalar >
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::Field::replace
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:551
field
rDeltaTY field()
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::functionObjects::particleDistribution::particleDistribution
particleDistribution(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: particleDistribution.C:54
fieldNames
const wordRes fieldNames(propsDict.getOrDefault< wordRes >("fields", wordRes()))
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:173
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::coordSet
Holds list of sampling positions.
Definition: coordSet.H:53
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::cloud
A cloud is a registry collection of lagrangian particles.
Definition: cloud.H:57
particleDistribution.H
Foam::functionObject::name
const word & name() const noexcept
Return the name of this functionObject.
Definition: functionObject.C:143
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::functionObjects::particleDistribution::execute
virtual bool execute()
Execute, currently does nothing.
Definition: particleDistribution.C:95
Foam::List< word >
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::functionObjects::particleDistribution::tagFieldName_
word tagFieldName_
Tag field name - used to filter the particles into groups.
Definition: particleDistribution.H:189
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::log
Computes the natural logarithm of an input volScalarField.
Definition: log.H:227
Foam::functionObjects::writeFile
Base class for writing single files from the function objects.
Definition: writeFile.H:119
Foam::IOobject::NO_READ
Definition: IOobject.H:188
Foam::functionObjects::particleDistribution::generateDistribution
void generateDistribution(const word &fieldName, const scalarField &field, const scalar binWidth, const label tag=-1)
Generate the distribution.
Definition: particleDistribution.C:180
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
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::functionObjects::particleDistribution::nameVsBinWidth_
List< Tuple2< word, scalar > > nameVsBinWidth_
List of field name vs. bin width.
Definition: particleDistribution.H:195
Foam::writer::New
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: writer.C:38