Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
30#include "Pstream.H"
31#include "ListOps.H"
32#include "ListListOps.H"
33
34// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35
36template<class CloudType>
37void Foam::ParticlePostProcessing<CloudType>::writeFileHeader(Ostream& os) const
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
53template<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(),
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"
85 }
86
87 const label sz = collector_.size();
88 times_.resize(sz);
89 data_.resize(sz);
90}
91
92
93template<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
112template<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());
145
146 return true;
147}
148
149
150template<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 }
195
196 return true;
197}
198
199
200template<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;
231 (
232 procData,
234 );
235
236 scalarList globalTimes;
238 (
239 procTimes,
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// ************************************************************************* //
scalar y
Various functions to operate on Lists.
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())
Templated cloud function object base class.
CloudFunctionObject(CloudType &owner)
Construct null from owner.
virtual fileName localPath() const
Output directory.
const CloudType & owner() const
Return const access to the owner cloud.
const fvMesh & mesh() const
Return reference to the mesh.
Definition DSMCCloudI.H:37
void append(const T &val)
Append an element at the end of the list.
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
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Foam::string str() const
Get the string. As Foam::string instead of std::string (may change in future).
Writes out various standard Lagrangian data elements of particles hitting on a given list of patches ...
ParticlePostProcessing(const dictionary &dict, CloudType &owner, const word &modelName)
Construct from dictionary.
virtual void write()
Write post-processing info.
virtual bool postFace(const parcelType &p, const typename parcelType::trackingData &td)
Post-face hook.
virtual bool postPatch(const parcelType &p, const polyPatch &pp, const typename parcelType::trackingData &td)
Post-patch hook.
static void combineReduce(T &value, CombineOp cop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Reduce inplace (cf. MPI Allreduce) applying cop to inplace combine value from different processors.
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Negative if the process is not a...
Definition UPstream.H:1706
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
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:1697
@ gatherList
gatherList [manual algorithm]
Definition UPstream.H:194
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
T & ref()
Return reference to the managed object without nullptr checking.
Definition autoPtr.H:231
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
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...
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 writeToFile() const
Flag to allow writing to file.
Definition writeFile.C:286
virtual autoPtr< OFstream > newFileAtTime(const word &name, scalar timeValue) const
Return autoPtr to a new file for a given time.
Definition writeFile.C:110
const faceZoneMesh & faceZones() const noexcept
Return face zone mesh.
Definition polyMesh.H:671
A patch is a list of labels that address the faces in the global face list.
Definition polyPatch.H:73
const dictionary & coeffDict() const
Return const access to the coefficients dictionary.
const dictionary & dict() const
Return const access to the cloud dictionary.
const word & modelName() const
Return const access to the name of the sub-model.
A class for handling words, derived from Foam::string.
Definition word.H:66
volScalarField & p
dynamicFvMesh & mesh
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
OBJstream os(runTime.globalPath()/outputName)
auto & names
wallPoints::trackData td(isBlockedFace, regionToBlockSize)
AccessType combine(const UList< T > &lists, AccessOp aop=accessOp< T >())
Combines sub-lists into a single list.
Definition ListListOps.C:62
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
List< word > wordList
List of word.
Definition fileName.H:60
DSMCCloud< dsmcParcel > CloudType
List< label > labelList
A List of labels.
Definition List.H:62
ZoneMesh< faceZone, polyMesh > faceZoneMesh
A ZoneMesh with faceZone content on a polyMesh.
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
List< scalar > scalarList
List of scalar.
Definition scalarList.H:32
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
constexpr char tab
The tab '\t' character(0x09).
Definition Ostream.H:49
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299
Object access operator or list access operator (default is pass-through).
Definition UList.H:1120