Cloud.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Cloud.H"
27 #include "processorPolyPatch.H"
28 #include "globalMeshData.H"
30 #include "mapPolyMesh.H"
31 #include "Time.H"
32 #include "OFstream.H"
33 #include "wallPolyPatch.H"
34 #include "cyclicAMIPolyPatch.H"
35 
36 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
37 
38 template<class ParticleType>
40 {
41  const polyBoundaryMesh& pbm = polyMesh_.boundaryMesh();
42  bool ok = true;
43  forAll(pbm, patchi)
44  {
45  if (isA<cyclicAMIPolyPatch>(pbm[patchi]))
46  {
47  const cyclicAMIPolyPatch& cami =
48  refCast<const cyclicAMIPolyPatch>(pbm[patchi]);
49 
50  ok = ok && cami.singlePatchProc() != -1;
51  }
52  }
53 
54  if (!ok)
55  {
57  << "Particle tracking across AMI patches is only currently "
58  << "supported for cases where the AMI patches reside on a "
59  << "single processor" << abort(FatalError);
60  }
61 }
62 
63 
64 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
65 
66 template<class ParticleType>
68 (
69  const polyMesh& pMesh,
70  const word& cloudName,
71  const IDLList<ParticleType>& particles
72 )
73 :
74  cloud(pMesh, cloudName),
76  polyMesh_(pMesh),
77  globalPositionsPtr_()
78 {
79  checkPatches();
80 
81  // Ask for the tetBasePtIs to trigger all processors to build
82  // them, otherwise, if some processors have no particles then
83  // there is a comms mismatch.
84  polyMesh_.tetBasePtIs();
85 
86  if (particles.size())
87  {
89  }
90 }
91 
92 
93 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
94 
95 template<class ParticleType>
97 {
98  this->append(pPtr);
99 }
100 
101 
102 template<class ParticleType>
104 {
105  delete(this->remove(&p));
106 }
107 
108 
109 template<class ParticleType>
111 {
112  forAllIter(typename Cloud<ParticleType>, *this, pIter)
113  {
114  ParticleType& p = pIter();
115 
116  if (p.cell() == -1)
117  {
119  << "deleting lost particle at position " << p.position()
120  << endl;
121 
122  deleteParticle(p);
123  }
124  }
125 }
126 
127 
128 template<class ParticleType>
130 {
131  // Reset particle count and particles only
132  // - not changing the cloud object registry or reference to the polyMesh
133  ParticleType::particleCount_ = 0;
135 }
136 
137 
138 template<class ParticleType>
139 template<class TrackCloudType>
141 (
142  TrackCloudType& cloud,
143  typename ParticleType::trackingData& td,
144  const scalar trackTime
145 )
146 {
147  const polyBoundaryMesh& pbm = pMesh().boundaryMesh();
148  const globalMeshData& pData = polyMesh_.globalData();
149 
150  // Which patches are processor patches
151  const labelList& procPatches = pData.processorPatches();
152 
153  // Indexing of equivalent patch on neighbour processor into the
154  // procPatches list on the neighbour
155  const labelList& procPatchNeighbours = pData.processorPatchNeighbours();
156 
157  // Which processors this processor is connected to
158  const labelList& neighbourProcs = pData[Pstream::myProcNo()];
159 
160  // Indexing from the processor number into the neighbourProcs list
161  labelList neighbourProcIndices(Pstream::nProcs(), -1);
162 
163  forAll(neighbourProcs, i)
164  {
165  neighbourProcIndices[neighbourProcs[i]] = i;
166  }
167 
168  // Initialise the stepFraction moved for the particles
169  forAllIter(typename Cloud<ParticleType>, *this, pIter)
170  {
171  pIter().stepFraction() = 0;
172  }
173 
174  // List of lists of particles to be transferred for all of the
175  // neighbour processors
176  List<IDLList<ParticleType>> particleTransferLists
177  (
178  neighbourProcs.size()
179  );
180 
181  // List of destination processorPatches indices for all of the
182  // neighbour processors
183  List<DynamicList<label>> patchIndexTransferLists
184  (
185  neighbourProcs.size()
186  );
187 
188  // Allocate transfer buffers
189  PstreamBuffers pBufs(Pstream::commsTypes::nonBlocking);
190 
191  // Clear the global positions as there are about to change
192  globalPositionsPtr_.clear();
193 
194  // While there are particles to transfer
195  while (true)
196  {
197  particleTransferLists = IDLList<ParticleType>();
198  forAll(patchIndexTransferLists, i)
199  {
200  patchIndexTransferLists[i].clear();
201  }
202 
203  // Loop over all particles
204  forAllIter(typename Cloud<ParticleType>, *this, pIter)
205  {
206  ParticleType& p = pIter();
207 
208  // Move the particle
209  bool keepParticle = p.move(cloud, td, trackTime);
210 
211  // If the particle is to be kept
212  // (i.e. it hasn't passed through an inlet or outlet)
213  if (keepParticle)
214  {
215  if (td.switchProcessor)
216  {
217  #ifdef FULLDEBUG
218  if
219  (
220  !Pstream::parRun()
221  || !p.onBoundaryFace()
222  || procPatchNeighbours[p.patch()] < 0
223  )
224  {
226  << "Switch processor flag is true when no parallel "
227  << "transfer is possible. This is a bug."
228  << exit(FatalError);
229  }
230  #endif
231 
232  const label patchi = p.patch();
233 
234  const label n = neighbourProcIndices
235  [
236  refCast<const processorPolyPatch>
237  (
238  pbm[patchi]
239  ).neighbProcNo()
240  ];
241 
242  p.prepareForParallelTransfer();
243 
244  particleTransferLists[n].append(this->remove(&p));
245 
246  patchIndexTransferLists[n].append
247  (
248  procPatchNeighbours[patchi]
249  );
250  }
251  }
252  else
253  {
254  deleteParticle(p);
255  }
256  }
257 
258  if (!Pstream::parRun())
259  {
260  break;
261  }
262 
263 
264  // Clear transfer buffers
265  pBufs.clear();
266 
267  // Stream into send buffers
268  forAll(particleTransferLists, i)
269  {
270  if (particleTransferLists[i].size())
271  {
272  UOPstream particleStream
273  (
274  neighbourProcs[i],
275  pBufs
276  );
277 
278  particleStream
279  << patchIndexTransferLists[i]
280  << particleTransferLists[i];
281  }
282  }
283 
284 
285  // Start sending. Sets number of bytes transferred
286  labelList allNTrans(Pstream::nProcs());
287  pBufs.finishedSends(allNTrans);
288 
289 
290  bool transferred = false;
291 
292  forAll(allNTrans, i)
293  {
294  if (allNTrans[i])
295  {
296  transferred = true;
297  break;
298  }
299  }
300  reduce(transferred, orOp<bool>());
301 
302  if (!transferred)
303  {
304  break;
305  }
306 
307  // Retrieve from receive buffers
308  forAll(neighbourProcs, i)
309  {
310  label neighbProci = neighbourProcs[i];
311 
312  label nRec = allNTrans[neighbProci];
313 
314  if (nRec)
315  {
316  UIPstream particleStream(neighbProci, pBufs);
317 
318  labelList receivePatchIndex(particleStream);
319 
320  IDLList<ParticleType> newParticles
321  (
322  particleStream,
323  typename ParticleType::iNew(polyMesh_)
324  );
325 
326  label pI = 0;
327 
328  forAllIter(typename Cloud<ParticleType>, newParticles, newpIter)
329  {
330  ParticleType& newp = newpIter();
331 
332  label patchi = procPatches[receivePatchIndex[pI++]];
333 
334  newp.correctAfterParallelTransfer(patchi, td);
335 
336  addParticle(newParticles.remove(&newp));
337  }
338  }
339  }
340  }
341 }
342 
343 
344 template<class ParticleType>
346 {
347  if (!globalPositionsPtr_.valid())
348  {
350  << "Global positions are not available. "
351  << "Cloud::storeGlobalPositions has not been called."
352  << exit(FatalError);
353  }
354 
355  // Ask for the tetBasePtIs to trigger all processors to build
356  // them, otherwise, if some processors have no particles then
357  // there is a comms mismatch.
358  polyMesh_.tetBasePtIs();
359 
360  const vectorField& positions = globalPositionsPtr_();
361 
362  label i = 0;
363  forAllIter(typename Cloud<ParticleType>, *this, iter)
364  {
365  iter().autoMap(positions[i], mapper);
366  ++ i;
367  }
368 }
369 
370 
371 template<class ParticleType>
373 {
374  OFstream pObj
375  (
376  this->db().time().path()/this->name() + "_positions.obj"
377  );
378 
379  forAllConstIter(typename Cloud<ParticleType>, *this, pIter)
380  {
381  const ParticleType& p = pIter();
382  pObj<< "v " << p.position().x() << " " << p.position().y() << " "
383  << p.position().z() << nl;
384  }
385 
386  pObj.flush();
387 }
388 
389 
390 template<class ParticleType>
392 {
393  // Store the global positions for later use by autoMap. It would be
394  // preferable not to need this. If the mapPolyMesh object passed to autoMap
395  // had a copy of the old mesh then the global positions could be recovered
396  // within autoMap, and this pre-processing would not be necessary.
397 
398  globalPositionsPtr_.reset(new vectorField(this->size()));
399 
400  vectorField& positions = globalPositionsPtr_();
401 
402  label i = 0;
403  forAllConstIter(typename Cloud<ParticleType>, *this, iter)
404  {
405  positions[i] = iter().position();
406  ++ i;
407  }
408 }
409 
410 
411 // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //
412 
413 #include "CloudIO.C"
414 
415 // ************************************************************************* //
Template class for intrusive linked lists.
Definition: ILList.H:50
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:424
void cloudReset(const Cloud< ParticleType > &c)
Reset the particles.
Definition: Cloud.C:129
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
const labelList & processorPatches() const
Return list of processor patch labels.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void deleteParticle(ParticleType &)
Remove particle from cloud and delete.
Definition: Cloud.C:103
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Various mesh related information for a parallel run. Upon construction, constructs all info using par...
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:453
Output to file stream.
Definition: OFstream.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
Cloud(const polyMesh &mesh, const word &cloudName, const IDLList< ParticleType > &particles)
Construct from mesh and a list of particles.
Definition: Cloud.C:68
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
volVectorField vectorField(fieldObject, mesh)
void autoMap(const mapPolyMesh &)
Remap the cells of particles corresponding to the.
Definition: Cloud.C:345
Combination-Reduction operation for a parallel run. The information from all nodes is collected on th...
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:53
void addParticle(ParticleType *pPtr)
Transfer particle to cloud.
Definition: Cloud.C:96
void storeGlobalPositions() const
Call this before a topology change. Stores the particles global.
Definition: Cloud.C:391
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:124
A class for handling words, derived from string.
Definition: word.H:59
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:184
virtual void flush()
Flush stream.
Definition: OSstream.C:254
T * remove(T *p)
Remove and return element.
Definition: UILList.H:139
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
errorManip< error > abort(error &err)
Definition: errorManip.H:131
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Base cloud calls templated on particle type.
Definition: Cloud.H:52
Foam::polyBoundaryMesh.
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:54
static const char nl
Definition: Ostream.H:265
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
const labelList & processorPatchNeighbours() const
Return processorPatchIndices of the neighbours.
Buffers for inter-processor communications streams (UOPstream, UIPstream).
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void deleteLostParticles()
Remove lost particles from cloud and delete.
Definition: Cloud.C:110
void move(TrackCloudType &cloud, typename ParticleType::trackingData &td, const scalar trackTime)
Move the particles.
Definition: Cloud.C:141
label patchi
#define WarningInFunction
Report a warning using Foam::Warning.
const dimensionedScalar c
Speed of light in a vacuum.
label n
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
volScalarField & p
void writePositions() const
Write positions to <cloudName>_positions.obj file.
Definition: Cloud.C:372