Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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
30#include "general.H"
31#include "fvMesh.H"
32#include "cloud.H"
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35
36namespace Foam
37{
38namespace functionObjects
39{
42 (
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)
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 (
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
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 (
127 (
128 scopedName("CloudRegistry"),
129 mesh_.time().timeName(),
131 mesh_.time(),
134 )
135 );
136
137 c.writeObjects(cloudObr);
138
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;
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// ************************************************************************* //
#define Log
Definition PDRblock.C:28
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
word setFormat(propsDict.getOrDefault< word >("setFormat", "vtk"))
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition Field.C:619
A primitive field of type <T> with automated input and output.
Definition IOField.H:53
@ NO_READ
Nothing to be read.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void append(const T &val)
Append an element at the end of the list.
Definition List.H:497
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A cloud is a registry collection of lagrangian particles.
Definition cloud.H:56
static const word prefix
The prefix to local: lagrangian.
Definition cloud.H:79
static autoPtr< coordSetWriter > New(const word &writeFormat)
Return a reference to the selected writer.
Holds list of sampling positions.
Definition coordSet.H:52
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Particle-size distribution model wherein random samples are drawn from a given arbitrary probability ...
Definition general.H:168
Accumulating histogram of values. Specified bin resolution automatic generation of bins.
A class for handling file names.
Definition fileName.H:75
Abstract base-class for Time/database function objects.
const word & name() const noexcept
Return the name of this functionObject.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
word scopedName(const word &name) const
Return a scoped (prefixed) name.
virtual const word & type() const =0
Runtime type information.
const fvMesh & mesh_
Reference to the fvMesh.
fvMeshFunctionObject(const fvMeshFunctionObject &)=delete
No copy construct.
Computes the natural logarithm of an input volScalarField.
Definition log.H:212
Generates a particle distribution for lagrangian data at a given time.
List< Tuple2< word, scalar > > nameVsBinWidth_
List of field name vs. bin width.
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
bool processField(const objectRegistry &obr, const label fieldi, const List< DynamicList< label > > &addr)
void generateDistribution(const word &fieldName, const scalarField &field, const scalar binWidth, const label tag=-1)
Generate the distribution.
autoPtr< coordSetWriter > writerPtr_
Writer.
Random rndGen_
Random number generator - used by distribution models.
virtual bool execute()
Execute the function-object operations.
virtual bool write()
Write the function-object results.
word tagFieldName_
Tag field name - used to filter the particles into groups.
particleDistribution(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
const Time & time_
Reference to the time database.
writeFile(const objectRegistry &obr, const fileName &prefix, const word &name="undefined", const bool writeToFile=true, const string &ext=".dat")
Construct from objectRegistry, prefix, fileName.
Definition writeFile.C:200
virtual bool read(const dictionary &dict)
Read.
Definition writeFile.C:240
fileName baseTimeDir() const
Return the base directory for the current time value.
Definition writeFile.C:66
Registry of regIOobjects.
bool foundObject(const word &name, const bool recursive=false) const
Contains the named Type?
const Type * cfindObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
const Type & lookupObject(const word &name, const bool recursive=false) const
Lookup and return const reference to the object of the given Type. Fatal if not found or the wrong ty...
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
rDeltaTY field()
engineTime & runTime
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
HashSet< label, Hash< label > > labelHashSet
A HashSet of labels, uses label hasher.
Definition HashSet.H:85
messageStream Info
Information stream (stdout output on master, null elsewhere).
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
vectorField pointField
pointField is a vectorField.
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299