dataCloud.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) 2018-2020 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
28#include "dataCloud.H"
29#include "Cloud.H"
30#include "dictionary.H"
31#include "fvMesh.H"
32#include "pointList.H"
33#include "OFstream.H"
35
36// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37
38namespace Foam
39{
40namespace functionObjects
41{
44}
45}
46
47// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48
49bool Foam::functionObjects::dataCloud::writeCloud
50(
51 const fileName& outputName,
52 const word& cloudName
53)
54{
55 const auto* objPtr = mesh_.findObject<cloud>(cloudName);
56 if (!objPtr)
57 {
58 return false;
59 }
60
61 objectRegistry obrTmp
62 (
63 IOobject
64 (
65 "tmp::dataCloud::" + cloudName,
67 mesh_,
70 false
71 )
72 );
73
74 objPtr->writeObjects(obrTmp);
75
76 const auto* pointsPtr = cloud::findIOPosition(obrTmp);
77
78 if (!pointsPtr)
79 {
80 // This should be impossible
81 return false;
82 }
83
84 applyFilter_ = calculateFilter(obrTmp, log);
85 reduce(applyFilter_, orOp<bool>());
86
87
88 // Number of parcels (locally)
89 label nParcels = (applyFilter_ ? parcelAddr_.count() : pointsPtr->size());
90
91 // Total number of parcels on all processes
92 const label nTotParcels = returnReduce(nParcels, sumOp<label>());
93
94 if (applyFilter_)
95 {
96 // Report filtered/unfiltered count
97 Log << "After filtering using " << nTotParcels << '/'
98 << (returnReduce(pointsPtr->size(), sumOp<label>()))
99 << " parcels" << nl;
100 }
101
102 if (!nTotParcels)
103 {
104 return false;
105 }
106
107 if (Pstream::master())
108 {
109 mkDir(outputName.path());
110 }
111
112 return
113 (
114 writeField<label>(outputName, obrTmp)
115 || writeField<scalar>(outputName, obrTmp)
116 || writeField<vector>(outputName, obrTmp)
117 );
118}
119
120
121// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
122
124(
125 const word& name,
126 const Time& runTime,
127 const dictionary& dict
128)
129:
131 printf_(),
132 precision_(IOstream::defaultPrecision()),
133 applyFilter_(false),
134 selectClouds_(),
135 fieldName_(),
136 directory_()
137{
138 read(dict);
139}
140
141
142// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
143
145{
147
148 const int padWidth = dict.getOrDefault<int>("width", 8);
149
150 // Appropriate printf format - Enforce min/max sanity limits
151 if (padWidth < 1 || padWidth > 31)
152 {
153 printf_.clear();
154 }
155 else
156 {
157 printf_ = "%0" + std::to_string(padWidth) + "d";
158 }
159
160 precision_ =
161 dict.getOrDefault("precision", IOstream::defaultPrecision());
162
163
164 selectClouds_.clear();
165 dict.readIfPresent("clouds", selectClouds_);
166
167 if (selectClouds_.empty())
168 {
169 selectClouds_.resize(1);
170 selectClouds_.first() =
171 dict.getOrDefault<word>("cloud", cloud::defaultName);
172 }
173
174 dict.readEntry("field", fieldName_);
175
176 // Actions to define selection
177 parcelSelect_ = dict.subOrEmptyDict("selection");
178
179 // Output directory
180
181 directory_.clear();
182 dict.readIfPresent("directory", directory_);
183
184 if (directory_.size())
185 {
186 // User-defined output directory
187 directory_.expand();
188 if (!directory_.isAbsolute())
189 {
190 directory_ = time_.globalPath()/directory_;
191 }
192 }
193 else
194 {
195 // Standard postProcessing/ naming
196 directory_ = time_.globalPath()/functionObject::outputPrefix/name();
197 }
198 directory_.clean(); // Remove unneeded ".."
199
200 return true;
201}
202
203
205{
206 return true;
207}
208
209
211{
212 const wordList cloudNames(mesh_.sortedNames<cloud>(selectClouds_));
213
214 if (cloudNames.empty())
215 {
216 return true; // skip - not available
217 }
218
219 const word timeDesc = "_" +
220 (
221 printf_.empty()
222 ? Foam::name(time_.timeIndex())
223 : word::printf(printf_, time_.timeIndex())
224 );
225
226 Log << name() << " output Time: " << time_.timeName() << nl;
227
228 // Each cloud separately
229 for (const word& cloudName : cloudNames)
230 {
231 // Legacy is not to be supported
232
233 const fileName outputName
234 (
235 directory_/cloudName + timeDesc + ".dat"
236 );
237
238 // writeCloud() includes mkDir (on master)
239
240 if (writeCloud(outputName, cloudName))
241 {
242 Log << " cloud : "
243 << time_.relativePath(outputName) << endl;
244 }
245 }
246
247 return true;
248}
249
250
251// ************************************************************************* //
#define Log
Definition: PDRblock.C:35
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
bitSet parcelAddr_
The filtered parcel addressing. Eg, for the current cloud.
bool calculateFilter(const objectRegistry &obrTmp, const bool log=true)
Calculate parcel selection filter.
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:82
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:342
virtual bool read()
Re-read model coefficients if they have changed.
const word & constant() const
Return constant name.
Definition: TimePathsI.H:96
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
unsigned int count(const bool on=true) const
Count number of bits set.
Definition: bitSetI.H:500
A cloud is a registry collection of lagrangian particles.
Definition: cloud.H:60
static const IOField< point > * findIOPosition(const objectRegistry &obr)
Locate the "position" IOField within object registry.
Definition: cloud.H:153
static word defaultName
The default cloud name: defaultCloud.
Definition: cloud.H:90
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A class for handling file names.
Definition: fileName.H:76
Abstract base-class for Time/database function objects.
static word outputPrefix
Directory prefix.
This functionObject writes a cloud position and in ASCII.
Definition: dataCloud.H:159
virtual bool read(const dictionary &dict)
Read the dataCloud specification.
Definition: dataCloud.C:144
virtual bool execute()
Execute, currently does nothing.
Definition: dataCloud.C:204
virtual bool write()
Write fields.
Definition: dataCloud.C:210
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
const fvMesh & mesh_
Reference to the fvMesh.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:290
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
splitCell * master() const
Definition: splitCell.H:113
A class for handling words, derived from Foam::string.
Definition: word.H:68
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
engineTime & runTime
word outputName("finiteArea-edges.obj")
Namespace for OpenFOAM.
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:515
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce (copy) and return value.
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
const word cloudName(propsDict.get< word >("cloud"))