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 -------------------------------------------------------------------------------
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 "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 
38 namespace Foam
39 {
40 namespace functionObjects
41 {
42  defineTypeNameAndDebug(dataCloud, 0);
43  addToRunTimeSelectionTable(functionObject, dataCloud, dictionary);
44 }
45 }
46 
47 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
49 bool 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,
66  mesh_.time().constant(),
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 
123 Foam::functionObjects::dataCloud::dataCloud
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 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Log
#define Log
Definition: PDRblock.C:35
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
cloudName
const word cloudName(propsDict.get< word >("cloud"))
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Cloud.H
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
dataCloud.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
Foam::bitSet::count
unsigned int count(const bool on=true) const
Count number of bits set.
Definition: bitSetI.H:499
OFstream.H
Foam::Detail::parcelSelection::calculateFilter
bool calculateFilter(const objectRegistry &obrTmp, const bool log=true)
Calculate parcel selection filter.
Definition: parcelSelectionDetail.C:159
outputName
word outputName("finiteArea-edges.obj")
Foam::functionObjects::dataCloud::execute
virtual bool execute()
Execute, currently does nothing.
Definition: dataCloud.C:204
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::functionObjects::dataCloud::write
virtual bool write()
Write fields.
Definition: dataCloud.C:210
pointList.H
Foam::functionObject::outputPrefix
static word outputPrefix
Directory prefix.
Definition: functionObject.H:376
Foam::functionObjects::dataCloud::read
virtual bool read(const dictionary &dict)
Read the dataCloud specification.
Definition: dataCloud.C:144
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:123
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:173
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::cloud::defaultName
static word defaultName
The default cloud name: defaultCloud.
Definition: cloud.H:90
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:342
Foam::cloud
A cloud is a registry collection of lagrangian particles.
Definition: cloud.H:57
Foam::objectRegistry::findObject
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:401
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::List< word >
Foam::functionObjects::fvMeshFunctionObject::mesh_
const fvMesh & mesh_
Reference to the fvMesh.
Definition: fvMeshFunctionObject.H:73
dictionary.H
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::cloud::findIOPosition
static const IOField< point > * findIOPosition(const objectRegistry &obr)
Locate the "position" IOField within object registry.
Definition: cloud.H:153
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::word::printf
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:280
Foam::Detail::parcelSelection::parcelAddr_
bitSet parcelAddr_
The filtered parcel addressing. Eg, for the current cloud.
Definition: parcelSelectionDetail.H:227
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:96
Foam::IOobject::NO_READ
Definition: IOobject.H:188
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
Foam::functionObject::log
bool log
Flag to write log into Info.
Definition: functionObject.H:355