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-2022 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 :
60  fvMeshFunctionObject(name, runTime, dict),
61  writeFile(runTime, name),
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 
82  const word setFormat(dict.get<word>("setFormat"));
84  (
85  setFormat,
86  dict.subOrEmptyDict("formatOptions").optionalSubDict(setFormat)
87  );
88 
89  Info<< type() << " " << name() << " output:" << nl
90  << " Processing cloud : " << cloudName_ << nl
91  << endl;
92 
93  return true;
94  }
95 
96  return false;
97 }
98 
99 
101 {
102  return true;
103 }
104 
105 
107 {
108  Log << type() << " " << name() << " output:" << endl;
109 
110  if (!mesh_.foundObject<cloud>(cloudName_))
111  {
113  << "Unable to find cloud " << cloudName_
114  << " in the mesh database. Available clouds include:"
115  << flatOutput(mesh_.sortedNames<cloud>()) << endl;
116 
117  return false;
118  }
119 
120  const cloud& c = mesh_.lookupObject<cloud>(cloudName_);
121 
122  objectRegistry cloudObr
123  (
124  IOobject
125  (
126  scopedName("CloudRegistry"),
127  mesh_.time().timeName(),
129  mesh_.time(),
132  )
133  );
134 
135  c.writeObjects(cloudObr);
136 
137  List<DynamicList<label>> tagAddr;
138  if
139  (
140  tagFieldName_ != "none"
141  && cloudObr.foundObject<IOField<scalar>>(tagFieldName_)
142  )
143  {
144  // Tag field present - generate distribution per tag
145  const IOField<label>& tag =
146  cloudObr.lookupObject<IOField<label>>(tagFieldName_);
147  const labelHashSet tagMap(tag);
148  const label tagMax = tagMap.size();
149 
150  List<DynamicList<label>> tagAddr(tagMax);
151  forAll(tag, i)
152  {
153  label newTag = tagMap[tag[i]];
154  tagAddr[newTag].append(i);
155  }
156  }
157 
158 
159  forAll(nameVsBinWidth_, i)
160  {
161  const bool ok
162  (
163  processField<scalar>(cloudObr, i, tagAddr)
164  || processField<vector>(cloudObr, i, tagAddr)
165  || processField<tensor>(cloudObr, i, tagAddr)
166  || processField<sphericalTensor>(cloudObr, i, tagAddr)
167  || processField<symmTensor>(cloudObr, i, tagAddr)
168  || processField<tensor>(cloudObr, i, tagAddr)
169  );
170 
171  if (log && !ok)
172  {
174  << "Unable to find field " << nameVsBinWidth_[i].first()
175  << " in the " << cloudName_ << " cloud database" << endl;
176  }
177  }
178 
179  return true;
180 }
181 
182 
184 (
185  const word& fieldName,
186  const scalarField& field,
187  const scalar binWidth,
188  const label tag
189 )
190 {
191  if (field.empty())
192  {
193  return;
194  }
195 
196  word fldName(fieldName);
197  if (tag != -1)
198  {
199  fldName += '_' + Foam::name(tag);
200  }
201 
202  distributionModels::general distribution
203  (
204  field,
205  binWidth,
206  rndGen_
207  );
208 
209  Field<scalar> distX(distribution.x());
210  Field<scalar> distY(distribution.y());
211 
212  pointField xBin(distX.size(), Zero);
213  xBin.replace(vector::X, distX);
214 
215  const coordSet coords(fldName, "x", std::move(xBin), std::move(distX));
216 
217  writerPtr_->open(coords, baseTimeDir() / fldName);
218  fileName outFile = writerPtr_->write(fldName, distY);
219  writerPtr_->close(true);
220 
221  Log << " Wrote distribution of " << fieldName
222  << " to " << time_.relativePath(outFile) << endl;
223 }
224 
225 
226 // ************************************************************************* //
virtual bool execute()
Execute, currently does nothing.
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
rDeltaTY field()
dimensionedScalar log(const dimensionedScalar &ds)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
void append(const T &val)
Append an element at the end of the list.
Definition: List.H:491
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
void generateDistribution(const word &fieldName, const scalarField &field, const scalar binWidth, const label tag=-1)
Generate the distribution.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
Ignore writing from objectRegistry::writeObject()
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:587
particleDistribution(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
virtual bool read(const dictionary &)
Read the particleDistribution data.
const word & name() const noexcept
Return the name of this functionObject.
virtual bool write()
Write the particleDistribution.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:413
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:752
static autoPtr< coordSetWriter > New(const word &writeFormat)
Return a reference to the selected writer.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:38
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
A class for handling words, derived from Foam::string.
Definition: word.H:63
A cloud is a registry collection of lagrangian particles.
Definition: cloud.H:53
virtual const word & type() const =0
Runtime type information.
word tagFieldName_
Tag field name - used to filter the particles into groups.
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:241
List< Tuple2< word, scalar > > nameVsBinWidth_
List of field name vs. bin width.
#define WarningInFunction
Report a warning using Foam::Warning.
const dimensionedScalar c
Speed of light in a vacuum.
Nothing to be read.
#define Log
Definition: PDRblock.C:28
messageStream Info
Information stream (stdout output on master, null elsewhere)
virtual bool read(const dictionary &dict)
Read optional controls.
autoPtr< coordSetWriter > writerPtr_
Writer.
word setFormat(propsDict.getOrDefault< word >("setFormat", "vtk"))
Registry of regIOobjects.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:166
A primitive field of type <T> with automated input and output.
Namespace for OpenFOAM.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:157
static const word prefix
The prefix to local: lagrangian.
Definition: cloud.H:93