ParticlePostProcessing.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2019-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "ParticlePostProcessing.H"
30 #include "Pstream.H"
31 #include "ListOps.H"
32 #include "ListListOps.H"
33 
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 
36 template<class CloudType>
38 {
39  this->writeCommented(os, "Time");
40  os << ' ' << "currentProc";
41 
42  if (!header_.empty())
43  {
44  os << ' ' << header_;
45  }
46 
47  os << endl;
48 }
49 
50 
51 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
52 
53 template<class CloudType>
55 (
56  const dictionary& dict,
57  CloudType& owner,
58  const word& modelName
59 )
60 :
61  CloudFunctionObject<CloudType>(dict, owner, modelName, typeName),
62  functionObjects::writeFile
63  (
64  owner,
65  this->localPath(),
66  typeName
67  ),
68  collector_(this->coeffDict(), owner.mesh()),
69  maxStoredParcels_(this->coeffDict().getScalar("maxStoredParcels")),
70  header_(),
71  fields_(),
72  times_(),
73  data_()
74 {
75  writeFile::read(this->coeffDict());
76 
77  this->coeffDict().readIfPresent("fields", fields_);
78 
79  if (maxStoredParcels_ <= 0)
80  {
82  << "maxStoredParcels = " << maxStoredParcels_
83  << ", cannot be equal to or less than zero"
84  << exit(FatalIOError);
85  }
86 
87  const label sz = collector_.size();
88  times_.resize(sz);
89  data_.resize(sz);
90 }
91 
92 
93 template<class CloudType>
95 (
97 )
98 :
100  writeFile(ppp),
101  collector_(ppp.collector_),
102  maxStoredParcels_(ppp.maxStoredParcels_),
103  header_(ppp.header_),
104  fields_(ppp.fields_),
105  times_(ppp.times_),
106  data_(ppp.data_)
107 {}
108 
109 
110 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111 
112 template<class CloudType>
114 (
115  const parcelType& p,
116  const polyPatch& pp,
117  const typename parcelType::trackingData& td
118 )
119 {
120  if (!collector_.isPatch())
121  {
122  return true;
123  }
124 
125  const label patchi = pp.index();
126  const label localPatchi = collector_.IDs().find(patchi);
127 
128  if (header_.empty())
129  {
130  OStringStream data;
131  p.writeProperties(data, fields_, " ", true);
132  header_ = data.str();
133  }
134 
135  if (localPatchi != -1 && data_[localPatchi].size() < maxStoredParcels_)
136  {
137  times_[localPatchi].append(this->owner().time().value());
138 
139  OStringStream data;
140  data<< Pstream::myProcNo();
141  p.writeProperties(data, fields_, " ", false);
142 
143  data_[localPatchi].append(data.str());
144  }
146  return true;
147 }
148 
149 
150 template<class CloudType>
152 (
153  const parcelType& p,
154  const typename parcelType::trackingData& td
155 )
156 {
157  if (collector_.isPatch())
158  {
159  return true;
160  }
161 
162  const labelList& IDs = collector_.IDs();
163  const List<boundBox>& BBs = collector_.BBs();
164  const faceZoneMesh& fzm = this->owner().mesh().faceZones();
165 
166  if (header_.empty())
167  {
168  OStringStream data;
169  p.writeProperties(data, fields_, " ", true);
170  header_ = data.str();
171  }
172 
173  forAll(IDs, i)
174  {
175  if (!BBs[i].contains(p.position()))
176  {
177  // Quick reject if the particle is not in the face zone bound box
178  continue;
179  }
180 
181  const label zonei = IDs[i];
182  const label localFacei = fzm[zonei].find(p.face());
183 
184  if (localFacei != -1 && data_[localFacei].size() < maxStoredParcels_)
185  {
186  times_[i].append(this->owner().time().value());
187 
188  OStringStream data;
189  data<< Pstream::myProcNo();
190  p.writeProperties(data, fields_, " ", false);
191 
192  data_[i].append(data.str());
193  }
194  }
195 
196  return true;
197 }
198 
199 
200 template<class CloudType>
202 {
203  const wordList& names = collector_.names();
204 
205  forAll(names, i)
206  {
207  List<scalarList> procTimes(Pstream::nProcs());
208  procTimes[Pstream::myProcNo()] = times_[i];
209  Pstream::gatherList(procTimes);
210 
211  List<List<string>> procData(Pstream::nProcs());
212  procData[Pstream::myProcNo()] = data_[i];
213  Pstream::gatherList(procData);
214 
216  (
217  header_,
218  [](string& x, const string& y)
219  {
220  if (y.size() > x.size())
221  {
222  x = y;
223  }
224  }
225  );
226 
227  if (Pstream::master())
228  {
229  List<string> globalData;
230  globalData = ListListOps::combine<List<string>>
231  (
232  procData,
233  accessOp<List<string>>()
234  );
235 
236  scalarList globalTimes;
237  globalTimes = ListListOps::combine<scalarList>
238  (
239  procTimes,
240  accessOp<scalarList>()
241  );
242 
243  if (this->writeToFile())
244  {
245  autoPtr<OFstream> osPtr = this->newFileAtTime
246  (
247  names[i],
248  this->owner().time().value()
249  );
250  OFstream& os = osPtr.ref();
251 
252  writeFileHeader(os);
253 
254  const labelList indices(sortedOrder(globalTimes));
255  forAll(globalTimes, j)
256  {
257  const label datai = indices[j];
258 
259  os << globalTimes[datai] << tab
260  << globalData[datai].c_str()
261  << nl;
262  }
263  }
264  }
265 
266  times_[i].clearStorage();
267  data_[i].clearStorage();
268  }
269 }
270 
271 
272 // ************************************************************************* //
List< scalar > scalarList
List of scalar.
Definition: scalarList.H:32
dictionary dict
ZoneMesh< faceZone, polyMesh > faceZoneMesh
A ZoneMesh with the type faceZone.
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List of names generated by calling name() for each list item and filtered for matches.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
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:521
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
wallPoints::trackData td(isBlockedFace, regionToBlockSize)
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void gatherList(const UList< commsStruct > &comms, UList< T > &values, const int tag, const label comm)
Gather data, but keep individual values separate. Uses the specified communication schedule...
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:49
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Can be negative if the process i...
Definition: UPstream.H:1086
ParticlePostProcessing(const dictionary &dict, CloudType &owner, const word &modelName)
Construct from dictionary.
Various functions to operate on Lists.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
scalar y
static label nProcs(const label communicator=worldComm)
Number of ranks in parallel run (for given communicator). It is 1 for serial run. ...
Definition: UPstream.H:1077
virtual bool postPatch(const parcelType &p, const polyPatch &pp, const typename parcelType::trackingData &td)
Post-patch hook.
dynamicFvMesh & mesh
A class for handling words, derived from Foam::string.
Definition: word.H:63
static void combineReduce(T &value, const CombineOp &cop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce inplace (cf. MPI Allreduce) applying cop to inplace combine value from different processors...
label size() const noexcept
Return number of collectors (zones or patches)
const dictionary & coeffDict() const
Return const access to the coefficients dictionary.
Definition: subModelBase.C:122
Writes out various standard Lagrangian data elements of particles hitting on a given list of patches ...
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry if present, and assign to T val. FatalIOError if it is found and the number of tokens i...
OBJstream os(runTime.globalPath()/outputName)
const faceZoneMesh & faceZones() const noexcept
Return face zone mesh.
Definition: polyMesh.H:671
virtual void write()
Write post-processing info.
virtual bool postFace(const parcelType &p, const typename parcelType::trackingData &td)
Post-face hook.
List< word > wordList
List of word.
Definition: fileName.H:59
const fvMesh & mesh() const
Return reference to the mesh.
Definition: DSMCCloudI.H:37
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:637
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1094
List< label > labelList
A List of labels.
Definition: List.H:62
volScalarField & p
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:69
Templated base class for dsmc cloud.
Definition: DSMCCloud.H:67
Templated cloud function object base class.
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...