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 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);
41 
43  (
44  functionObject,
45  particleDistribution,
46  dictionary
47  );
48 }
49 }
50 
51 
52 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
53 
55 (
56  const word& name,
57  const Time& runTime,
58  const dictionary& dict
59 )
60 :
63  cloudName_("unknown-cloudName"),
64  nameVsBinWidth_(),
65  tagFieldName_("none"),
66  rndGen_(),
67  writerPtr_(nullptr)
68 {
69  read(dict);
70 }
71 
72 
73 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
74 
76 {}
77 
78 
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 
82 {
84  {
85  dict.readEntry("cloud", cloudName_);
86  dict.readEntry("nameVsBinWidth", nameVsBinWidth_);
87  dict.readIfPresent("tagField", tagFieldName_);
88  const word format(dict.get<word>("setFormat"));
89  writerPtr_ = writer<scalar>::New(format);
90 
91  Info<< type() << " " << name() << " output:" << nl
92  << " Processing cloud : " << cloudName_ << nl
93  << endl;
94 
95  return true;
96  }
97 
98  return false;
99 }
100 
101 
103 {
104  return true;
105 }
106 
107 
109 {
110  Log << type() << " " << name() << " output:" << endl;
111 
112  if (!mesh_.foundObject<cloud>(cloudName_))
113  {
114  wordList cloudNames(mesh_.names<cloud>());
115 
117  << "Unable to find cloud " << cloudName_
118  << " in the mesh database. Available clouds include:"
119  << cloudNames << endl;
120 
121  return false;
122  }
123 
124  const cloud& c = mesh_.lookupObject<cloud>(cloudName_);
125 
126  objectRegistry cloudObr
127  (
128  IOobject
129  (
130  name() & "CloudRegistry",
131  mesh_.time().timeName(),
133  mesh_.time(),
136  )
137  );
138 
139  c.writeObjects(cloudObr);
140 
141  List<DynamicList<label>> tagAddr;
142  if
143  (
144  tagFieldName_ != "none"
145  && cloudObr.foundObject<IOField<scalar>>(tagFieldName_)
146  )
147  {
148  // Tag field present - generate distribution per tag
149  const IOField<label>& tag =
150  cloudObr.lookupObject<IOField<label>>(tagFieldName_);
151  const labelHashSet tagMap(tag);
152  const label tagMax = tagMap.size();
153 
154  List<DynamicList<label>> tagAddr(tagMax);
155  forAll(tag, i)
156  {
157  label newTag = tagMap[tag[i]];
158  tagAddr[newTag].append(i);
159  }
160  }
161 
162 
163  bool ok = false;
164  forAll(nameVsBinWidth_, i)
165  {
166  ok = false;
167  ok = ok || processField<scalar>(cloudObr, i, tagAddr);
168  ok = ok || processField<vector>(cloudObr, i, tagAddr);
169  ok = ok || processField<tensor>(cloudObr, i, tagAddr);
170  ok = ok || processField<sphericalTensor>(cloudObr, i, tagAddr);
171  ok = ok || processField<symmTensor>(cloudObr, i, tagAddr);
172  ok = ok || processField<tensor>(cloudObr, i, tagAddr);
173 
174  if (log && !ok)
175  {
177  << "Unable to find field " << nameVsBinWidth_[i].first()
178  << " in the " << cloudName_ << " cloud database" << endl;
179  }
180  }
181 
182  return true;
183 }
184 
185 
187 (
188  const word& fieldName,
189  const scalarField& field,
190  const scalar binWidth,
191  const label tag
192 )
193 {
194  if (field.empty())
195  {
196  return;
197  }
198 
199  word fName(fieldName);
200  if (tag != -1)
201  {
202  fName = fName + '_' + Foam::name(tag);
203  }
204 
206  (
207  field,
208  binWidth,
209  rndGen_
210  );
211 
212  const Field<scalar> distX(distribution.x());
213  const Field<scalar> distY(distribution.y());
214 
215  pointField xBin(distX.size(), Zero);
216  xBin.replace(0, distX);
217  const coordSet coords
218  (
219  fName,
220  "x",
221  xBin,
222  distX
223  );
224 
225  const wordList fieldNames(1, fName);
226 
227  fileName outputPath(baseTimeDir());
228  mkDir(outputPath);
229  OFstream graphFile(outputPath/writerPtr_->getFileName(coords, fieldNames));
230 
231  Log << " Writing distribution of " << fieldName
232  << " to " << graphFile.name() << endl;
233 
234  List<const scalarField*> yPtrs(1);
235  yPtrs[0] = &distY;
236  writerPtr_->write(coords, fieldNames, yPtrs, graphFile);
237 }
238 
239 
240 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
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:104
Foam::distribution
Accumulating histogram of values. Specified bin resolution automatic generation of bins.
Definition: distribution.H:61
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:81
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
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.
Definition: zero.H:128
Foam::functionObjects::particleDistribution::write
virtual bool write()
Write the particleDistribution.
Definition: particleDistribution.C:108
cloud.H
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:182
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::functionObjects::particleDistribution::particleDistribution
particleDistribution(const particleDistribution &)=delete
No copy construct.
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::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, add, dictionary)
Foam::distributionModels::general
general distribution model
Definition: general.H:70
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
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< scalar >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::functionObjects::particleDistribution::~particleDistribution
virtual ~particleDistribution()
Destructor.
Definition: particleDistribution.C:75
Foam::Field::replace
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:574
field
rDeltaTY field()
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
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:121
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:166
Foam::log
dimensionedScalar log(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:262
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
cloudNames
const wordList cloudNames(cloudFields.sortedToc())
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:99
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
Foam::cloud
A cloud is a registry collection of lagrangian particles.
Definition: cloud.H:57
particleDistribution.H
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::functionObjects::particleDistribution::execute
virtual bool execute()
Execute, currently does nothing.
Definition: particleDistribution.C:102
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::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::functionObjects::writeFile
functionObject base class for writing single files
Definition: writeFile.H:59
Foam::IOobject::NO_READ
Definition: IOobject.H:123
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:187
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:294
Log
#define Log
Report write to Foam::Info if the local log switch is true.
Definition: messageStream.H:332
Foam::writer::New
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: writer.C:38