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-2023 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  const cloud* cloudPtr = mesh_.cfindObject<cloud>(cloudName_);
111 
112  if (!cloudPtr)
113  {
115  << "Unable to find cloud " << cloudName_
116  << " in the mesh database. Available clouds include:"
117  << flatOutput(mesh_.sortedNames<cloud>()) << endl;
118 
119  return false;
120  }
121 
122  const cloud& c = *cloudPtr;
123 
124  objectRegistry cloudObr
125  (
126  IOobject
127  (
128  scopedName("CloudRegistry"),
129  mesh_.time().timeName(),
131  mesh_.time(),
134  )
135  );
136 
137  c.writeObjects(cloudObr);
138 
139  List<DynamicList<label>> tagAddr;
140  if
141  (
142  tagFieldName_ != "none"
143  && cloudObr.foundObject<IOField<scalar>>(tagFieldName_)
144  )
145  {
146  // Tag field present - generate distribution per tag
147  const IOField<label>& tag =
148  cloudObr.lookupObject<IOField<label>>(tagFieldName_);
149  const labelHashSet tagMap(tag);
150  const label tagMax = tagMap.size();
151 
152  List<DynamicList<label>> tagAddr(tagMax);
153  forAll(tag, i)
154  {
155  label newTag = tagMap[tag[i]];
156  tagAddr[newTag].append(i);
157  }
158  }
159 
160 
161  forAll(nameVsBinWidth_, i)
162  {
163  const bool ok
164  (
165  processField<scalar>(cloudObr, i, tagAddr)
166  || processField<vector>(cloudObr, i, tagAddr)
167  || processField<tensor>(cloudObr, i, tagAddr)
168  || processField<sphericalTensor>(cloudObr, i, tagAddr)
169  || processField<symmTensor>(cloudObr, i, tagAddr)
170  || processField<tensor>(cloudObr, i, tagAddr)
171  );
172 
173  if (log && !ok)
174  {
176  << "Unable to find field " << nameVsBinWidth_[i].first()
177  << " in the " << cloudName_ << " cloud database" << endl;
178  }
179  }
180 
181  return true;
182 }
183 
184 
186 (
187  const word& fieldName,
188  const scalarField& field,
189  const scalar binWidth,
190  const label tag
191 )
192 {
193  if (field.empty())
194  {
195  return;
196  }
197 
198  word fldName(fieldName);
199  if (tag != -1)
200  {
201  fldName += '_' + Foam::name(tag);
202  }
203 
204  distributionModels::general distribution
205  (
206  field,
207  binWidth,
208  rndGen_
209  );
210 
211  Field<scalar> distX(distribution.x());
212  Field<scalar> distY(distribution.y());
213 
214  pointField xBin(distX.size(), Zero);
215  xBin.replace(vector::X, distX);
216 
217  const coordSet coords(fldName, "x", std::move(xBin), std::move(distX));
218 
219  writerPtr_->open(coords, baseTimeDir() / fldName);
220  fileName outFile = writerPtr_->write(fldName, distY);
221  writerPtr_->close(true);
222 
223  Log << " Wrote distribution of " << fieldName
224  << " to " << time_.relativePath(outFile) << endl;
225 }
226 
227 
228 // ************************************************************************* //
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:129
void append(const T &val)
Append an element at the end of the list.
Definition: List.H:517
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:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Ignore writing from objectRegistry::writeObject()
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:620
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:421
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
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 expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
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:172
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:127
static const word prefix
The prefix to local: lagrangian.
Definition: cloud.H:79