particle.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, 2020 OpenFOAM Foundation
9  Copyright (C) 2018-2021 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 "particle.H"
30 #include "transform.H"
31 #include "treeDataCell.H"
32 #include "cubicEqn.H"
33 #include "registerSwitch.H"
34 #include "indexedOctree.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40  defineTypeNameAndDebug(particle, 0);
41 }
42 
43 const Foam::label Foam::particle::maxNBehind_ = 10;
44 
45 Foam::label Foam::particle::particleCount_ = 0;
46 
48 
50 (
51  Foam::debug::infoSwitch("writeLagrangianPositions", 1)
52 );
53 
55 (
56  "writeLagrangianPositions",
57  bool,
59 );
60 
61 
62 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
63 
64 void Foam::particle::stationaryTetReverseTransform
65 (
66  vector& centre,
67  scalar& detA,
69 ) const
70 {
71  barycentricTensor A = stationaryTetTransform();
72 
73  const vector ab = A.b() - A.a();
74  const vector ac = A.c() - A.a();
75  const vector ad = A.d() - A.a();
76  const vector bc = A.c() - A.b();
77  const vector bd = A.d() - A.b();
78 
79  centre = A.a();
80 
81  detA = ab & (ac ^ ad);
82 
84  (
85  bd ^ bc,
86  ac ^ ad,
87  ad ^ ab,
88  ab ^ ac
89  );
90 }
91 
92 
93 void Foam::particle::movingTetReverseTransform
94 (
95  const scalar fraction,
96  Pair<vector>& centre,
97  FixedList<scalar, 4>& detA,
98  FixedList<barycentricTensor, 3>& T
99 ) const
100 {
101  Pair<barycentricTensor> A = movingTetTransform(fraction);
102 
103  const Pair<vector> ab(A[0].b() - A[0].a(), A[1].b() - A[1].a());
104  const Pair<vector> ac(A[0].c() - A[0].a(), A[1].c() - A[1].a());
105  const Pair<vector> ad(A[0].d() - A[0].a(), A[1].d() - A[1].a());
106  const Pair<vector> bc(A[0].c() - A[0].b(), A[1].c() - A[1].b());
107  const Pair<vector> bd(A[0].d() - A[0].b(), A[1].d() - A[1].b());
108 
109  centre[0] = A[0].a();
110  centre[1] = A[1].a();
111 
112  detA[0] = ab[0] & (ac[0] ^ ad[0]);
113  detA[1] =
114  (ab[1] & (ac[0] ^ ad[0]))
115  + (ab[0] & (ac[1] ^ ad[0]))
116  + (ab[0] & (ac[0] ^ ad[1]));
117  detA[2] =
118  (ab[0] & (ac[1] ^ ad[1]))
119  + (ab[1] & (ac[0] ^ ad[1]))
120  + (ab[1] & (ac[1] ^ ad[0]));
121  detA[3] = ab[1] & (ac[1] ^ ad[1]);
122 
123  T[0] = barycentricTensor
124  (
125  bd[0] ^ bc[0],
126  ac[0] ^ ad[0],
127  ad[0] ^ ab[0],
128  ab[0] ^ ac[0]
129  );
130  T[1] = barycentricTensor
131  (
132  (bd[0] ^ bc[1]) + (bd[1] ^ bc[0]),
133  (ac[0] ^ ad[1]) + (ac[1] ^ ad[0]),
134  (ad[0] ^ ab[1]) + (ad[1] ^ ab[0]),
135  (ab[0] ^ ac[1]) + (ab[1] ^ ac[0])
136  );
137  T[2] = barycentricTensor
138  (
139  bd[1] ^ bc[1],
140  ac[1] ^ ad[1],
141  ad[1] ^ ab[1],
142  ab[1] ^ ac[1]
143  );
144 }
145 
146 
147 void Foam::particle::reflect()
148 {
149  std::swap(coordinates_.c(), coordinates_.d());
150 }
151 
152 
153 void Foam::particle::rotate(const bool reverse)
154 {
155  if (!reverse)
156  {
157  scalar temp = coordinates_.b();
158  coordinates_.b() = coordinates_.c();
159  coordinates_.c() = coordinates_.d();
160  coordinates_.d() = temp;
161  }
162  else
163  {
164  scalar temp = coordinates_.d();
165  coordinates_.d() = coordinates_.c();
166  coordinates_.c() = coordinates_.b();
167  coordinates_.b() = temp;
168  }
169 }
170 
171 
172 void Foam::particle::changeTet(const label tetTriI)
173 {
174  const bool isOwner = mesh_.faceOwner()[tetFacei_] == celli_;
175 
176  const label firstTetPtI = 1;
177  const label lastTetPtI = mesh_.faces()[tetFacei_].size() - 2;
178 
179  if (tetTriI == 1)
180  {
181  changeFace(tetTriI);
182  }
183  else if (tetTriI == 2)
184  {
185  if (isOwner)
186  {
187  if (tetPti_ == lastTetPtI)
188  {
189  changeFace(tetTriI);
190  }
191  else
192  {
193  reflect();
194  tetPti_ += 1;
195  }
196  }
197  else
198  {
199  if (tetPti_ == firstTetPtI)
200  {
201  changeFace(tetTriI);
202  }
203  else
204  {
205  reflect();
206  tetPti_ -= 1;
207  }
208  }
209  }
210  else if (tetTriI == 3)
211  {
212  if (isOwner)
213  {
214  if (tetPti_ == firstTetPtI)
215  {
216  changeFace(tetTriI);
217  }
218  else
219  {
220  reflect();
221  tetPti_ -= 1;
222  }
223  }
224  else
225  {
226  if (tetPti_ == lastTetPtI)
227  {
228  changeFace(tetTriI);
229  }
230  else
231  {
232  reflect();
233  tetPti_ += 1;
234  }
235  }
236  }
237  else
238  {
240  << "Changing tet without changing cell should only happen when the "
241  << "track is on triangle 1, 2 or 3."
242  << exit(FatalError);
243  }
244 }
245 
246 
247 void Foam::particle::changeFace(const label tetTriI)
248 {
249  // Get the old topology
250  const triFace triOldIs(currentTetIndices().faceTriIs(mesh_));
251 
252  // Get the shared edge and the pre-rotation
253  edge sharedEdge;
254  if (tetTriI == 1)
255  {
256  sharedEdge = edge(triOldIs[1], triOldIs[2]);
257  }
258  else if (tetTriI == 2)
259  {
260  sharedEdge = edge(triOldIs[2], triOldIs[0]);
261  }
262  else if (tetTriI == 3)
263  {
264  sharedEdge = edge(triOldIs[0], triOldIs[1]);
265  }
266  else
267  {
269  << "Changing face without changing cell should only happen when the"
270  << " track is on triangle 1, 2 or 3."
271  << exit(FatalError);
272 
273  sharedEdge = edge(-1, -1);
274  }
275 
276  // Find the face in the same cell that shares the edge, and the
277  // corresponding tetrahedra point
278  tetPti_ = -1;
279  for (const label newFaceI : mesh_.cells()[celli_])
280  {
281  const Foam::face& newFace = mesh_.faces()[newFaceI];
282  const label newOwner = mesh_.faceOwner()[newFaceI];
283 
284  // Exclude the current face
285  if (tetFacei_ == newFaceI)
286  {
287  continue;
288  }
289 
290  // Loop over the edges, looking for the shared one. Note that we have to
291  // match the direction of the edge as well as the end points in order to
292  // avoid false positives when dealing with coincident ACMI faces.
293  const label edgeComp = newOwner == celli_ ? -1 : +1;
294  label edgeI = 0;
295  for
296  (
297  ;
298  edgeI < newFace.size()
299  && edge::compare(sharedEdge, newFace.edge(edgeI)) != edgeComp;
300  ++ edgeI
301  );
302 
303  // If the face does not contain the edge, then move on to the next face
304  if (edgeI >= newFace.size())
305  {
306  continue;
307  }
308 
309  // Make the edge index relative to the base point
310  const label newBaseI = max(0, mesh_.tetBasePtIs()[newFaceI]);
311  edgeI = (edgeI - newBaseI + newFace.size()) % newFace.size();
312 
313  // If the edge is next the base point (i.e., the index is 0 or n - 1),
314  // then we swap it for the adjacent edge. This new edge is opposite the
315  // base point, and defines the tet with the original edge in it.
316  edgeI = min(max(1, edgeI), newFace.size() - 2);
317 
318  // Set the new face and tet point
319  tetFacei_ = newFaceI;
320  tetPti_ = edgeI;
321 
322  // Exit the loop now that the tet point has been found
323  break;
324  }
325 
326  if (tetPti_ == -1)
327  {
329  << "The search for an edge-connected face and tet-point failed."
330  << exit(FatalError);
331  }
332 
333  // Pre-rotation puts the shared edge opposite the base of the tetrahedron
334  if (sharedEdge.otherVertex(triOldIs[1]) == -1)
335  {
336  rotate(false);
337  }
338  else if (sharedEdge.otherVertex(triOldIs[2]) == -1)
339  {
340  rotate(true);
341  }
342 
343  // Get the new topology
344  const triFace triNewIs = currentTetIndices().faceTriIs(mesh_);
345 
346  // Reflect to account for the change of triangle orientation on the new face
347  reflect();
348 
349  // Post rotation puts the shared edge back in the correct location
350  if (sharedEdge.otherVertex(triNewIs[1]) == -1)
351  {
352  rotate(true);
353  }
354  else if (sharedEdge.otherVertex(triNewIs[2]) == -1)
355  {
356  rotate(false);
357  }
358 }
359 
360 
361 void Foam::particle::changeCell()
362 {
363  // Set the cell to be the one on the other side of the face
364  const label ownerCellI = mesh_.faceOwner()[tetFacei_];
365  const bool isOwner = celli_ == ownerCellI;
366  celli_ = isOwner ? mesh_.faceNeighbour()[tetFacei_] : ownerCellI;
367  // Reflect to account for the change of triangle orientation in the new cell
368  reflect();
369 }
370 
371 
372 void Foam::particle::changeToMasterPatch()
373 {
374  label thisPatch = patch();
375 
376  for (const label otherFaceI : mesh_.cells()[celli_])
377  {
378  // Skip the current face and any internal faces
379  if (facei_ == otherFaceI || mesh_.isInternalFace(otherFaceI))
380  {
381  continue;
382  }
383 
384  // Compare the two faces. If they are the same, chose the one with the
385  // lower patch index. In the case of an ACMI-wall pair, this will be
386  // the ACMI, which is what we want.
387  const Foam::face& thisFace = mesh_.faces()[facei_];
388  const Foam::face& otherFace = mesh_.faces()[otherFaceI];
389  if (face::compare(thisFace, otherFace) != 0)
390  {
391  const label otherPatch =
392  mesh_.boundaryMesh().whichPatch(otherFaceI);
393  if (thisPatch > otherPatch)
394  {
395  facei_ = otherFaceI;
396  thisPatch = otherPatch;
397  }
398  }
399  }
400 
401  tetFacei_ = facei_;
402 }
403 
404 
405 void Foam::particle::locate
406 (
407  const vector& position,
408  const vector* direction,
409  const label celli,
410  const bool boundaryFail,
411  const string& boundaryMsg
412 )
413 {
414  if (debug)
415  {
416  Pout<< "Particle " << origId() << nl << FUNCTION_NAME << nl << endl;
417  }
418 
419  celli_ = celli;
420 
421  // Find the cell, if it has not been given
422  if (celli_ < 0)
423  {
424  celli_ = mesh_.cellTree().findInside(position);
425  if (celli_ < 0)
426  {
428  << "Cell not found for particle position " << position << "."
429  << exit(FatalError);
430  }
431  }
432 
433  // Perturbing displacement to avoid zero in case position is the
434  // cellCentre
435  const vector displacement =
436  position
437  - mesh_.cellCentres()[celli_]
438  + vector(VSMALL, VSMALL, VSMALL);
439 
440  // Loop all cell tets to find the one containing the position. Track
441  // through each tet from the cell centre. If a tet contains the position
442  // then the track will end with a single trackToTri.
443  const Foam::cell& c = mesh_.cells()[celli_];
444  scalar minF = VGREAT;
445  label minTetFacei = -1, minTetPti = -1;
446  forAll(c, cellTetFacei)
447  {
448  const Foam::face& f = mesh_.faces()[c[cellTetFacei]];
449  for (label tetPti = 1; tetPti < f.size() - 1; ++tetPti)
450  {
451  coordinates_ = barycentric(1, 0, 0, 0);
452  tetFacei_ = c[cellTetFacei];
453  tetPti_ = tetPti;
454  facei_ = -1;
455 
456  label tetTriI = -1;
457  const scalar f = trackToTri(displacement, 0, tetTriI);
458 
459  if (tetTriI == -1)
460  {
461  return;
462  }
463 
464  if (f < minF)
465  {
466  minF = f;
467  minTetFacei = tetFacei_;
468  minTetPti = tetPti_;
469  }
470  }
471  }
472 
473  // The particle must be (hopefully only slightly) outside the cell. Track
474  // into the tet which got the furthest.
475  coordinates_ = barycentric(1, 0, 0, 0);
476  tetFacei_ = minTetFacei;
477  tetPti_ = minTetPti;
478  facei_ = -1;
479  track(displacement, 0);
480  if (!onFace())
481  {
482  return;
483  }
484 
485  // If we are here then we hit a boundary
486  if (boundaryFail)
487  {
488  FatalErrorInFunction << boundaryMsg << exit(FatalError);
489  }
490  else
491  {
492  static label nWarnings = 0;
493  static const label maxNWarnings = 100;
494  if ((nWarnings < maxNWarnings) && boundaryFail)
495  {
496  WarningInFunction << boundaryMsg.c_str() << endl;
497  ++ nWarnings;
498  }
499  if (nWarnings == maxNWarnings)
500  {
502  << "Suppressing any further warnings about particles being "
503  << "located outside of the mesh." << endl;
504  ++ nWarnings;
505  }
506  }
508 }
509 
510 
511 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
512 
514 (
515  const polyMesh& mesh,
516  const barycentric& coordinates,
517  const label celli,
518  const label tetFacei,
519  const label tetPti
520 )
521 :
522  mesh_(mesh),
523  coordinates_(coordinates),
524  celli_(celli),
525  tetFacei_(tetFacei),
526  tetPti_(tetPti),
527  facei_(-1),
528  stepFraction_(1.0),
529  behind_(0.0),
530  nBehind_(0),
531  origProc_(Pstream::myProcNo()),
532  origId_(getNewParticleID())
533 {}
534 
535 
537 (
538  const polyMesh& mesh,
539  const vector& position,
540  const label celli
541 )
542 :
543  mesh_(mesh),
544  coordinates_(-VGREAT, -VGREAT, -VGREAT, -VGREAT),
545  celli_(celli),
546  tetFacei_(-1),
547  tetPti_(-1),
548  facei_(-1),
549  stepFraction_(1.0),
550  behind_(0.0),
551  nBehind_(0),
552  origProc_(Pstream::myProcNo()),
553  origId_(getNewParticleID())
554 {
555  locate
556  (
557  position,
558  nullptr,
559  celli,
560  false,
561  "Particle initialised with a location outside of the mesh"
562  );
563 }
564 
565 
567 (
568  const polyMesh& mesh,
569  const vector& position,
570  const label celli,
571  const label tetFacei,
572  const label tetPti,
573  bool doLocate
574 )
575 :
576  mesh_(mesh),
577  coordinates_(-VGREAT, -VGREAT, -VGREAT, -VGREAT),
578  celli_(celli),
579  tetFacei_(tetFacei),
580  tetPti_(tetPti),
581  facei_(-1),
582  stepFraction_(1.0),
583  behind_(0.0),
584  nBehind_(0),
585  origProc_(Pstream::myProcNo()),
586  origId_(getNewParticleID())
587 {
588  if (doLocate)
589  {
590  locate
591  (
592  position,
593  nullptr,
594  celli,
595  false,
596  "Particle initialised with a location outside of the mesh"
597  );
598  }
599 }
600 
601 
602 Foam::particle::particle(const particle& p)
603 :
604  mesh_(p.mesh_),
605  coordinates_(p.coordinates_),
606  celli_(p.celli_),
607  tetFacei_(p.tetFacei_),
608  tetPti_(p.tetPti_),
609  facei_(p.facei_),
610  stepFraction_(p.stepFraction_),
611  behind_(p.behind_),
612  nBehind_(p.nBehind_),
613  origProc_(p.origProc_),
614  origId_(p.origId_)
615 {}
616 
617 
619 :
620  mesh_(mesh),
621  coordinates_(p.coordinates_),
622  celli_(p.celli_),
623  tetFacei_(p.tetFacei_),
624  tetPti_(p.tetPti_),
625  facei_(p.facei_),
626  stepFraction_(p.stepFraction_),
627  behind_(p.behind_),
628  nBehind_(p.nBehind_),
629  origProc_(p.origProc_),
630  origId_(p.origId_)
631 {}
632 
633 
634 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
635 
636 Foam::scalar Foam::particle::track
637 (
638  const vector& displacement,
639  const scalar fraction
640 )
641 {
642  scalar f = trackToFace(displacement, fraction);
643 
644  while (onInternalFace())
645  {
646  changeCell();
647 
648  f *= trackToFace(f*displacement, f*fraction);
649  }
650 
651  return f;
652 }
653 
654 
655 Foam::scalar Foam::particle::trackToFace
656 (
657  const vector& displacement,
658  const scalar fraction
659 )
660 {
661  scalar f = 1;
662 
663  label tetTriI = onFace() ? 0 : -1;
664 
665  facei_ = -1;
666 
667  // Loop the tets in the current cell
668  while (nBehind_ < maxNBehind_)
669  {
670  f *= trackToTri(f*displacement, f*fraction, tetTriI);
671 
672  if (tetTriI == -1)
673  {
674  // The track has completed within the current tet
675  return 0;
676  }
677  else if (tetTriI == 0)
678  {
679  // The track has hit a face, so set the current face and return
680  facei_ = tetFacei_;
681  return f;
682  }
683  else
684  {
685  // Move to the next tet and continue the track
686  changeTet(tetTriI);
687  }
688  }
689 
690  // Warn if stuck, and incorrectly advance the step fraction to completion
691  static label stuckID = -1, stuckProc = -1;
692  if (origId_ != stuckID && origProc_ != stuckProc)
693  {
695  << "Particle #" << origId_ << " got stuck at " << position()
696  << endl;
697  }
698 
699  stuckID = origId_;
700  stuckProc = origProc_;
701 
702  stepFraction_ += f*fraction;
703 
704  behind_ = 0;
705  nBehind_ = 0;
706 
707  return 0;
708 }
709 
710 
712 (
713  const vector& displacement,
714  const scalar fraction,
715  label& tetTriI
716 )
717 {
718  const vector x0 = position();
719  const vector x1 = displacement;
720  const barycentric y0 = coordinates_;
721 
722  if (debug)
723  {
724  Pout<< "Particle " << origId() << endl << "Tracking from " << x0
725  << " along " << x1 << " to " << x0 + x1 << endl;
726  }
727 
728  // Get the tet geometry
729  vector centre;
730  scalar detA;
732  stationaryTetReverseTransform(centre, detA, T);
733 
734  if (debug)
735  {
736  Pout<< "Tet points: " << currentTetIndices().tet(mesh_) << endl
737  << "Tet determinant = " << detA << endl
738  << "Start local coordinates = " << y0 << endl;
739  }
740 
741  // Calculate the local tracking displacement
742  barycentric Tx1(x1 & T);
743 
744  if (debug)
745  {
746  Pout<< "Local displacement = " << Tx1 << "/" << detA << endl;
747  }
748 
749  // Calculate the hit fraction
750  label iH = -1;
751  scalar muH = detA <= 0 ? VGREAT : 1/detA;
752  for (label i = 0; i < 4; ++ i)
753  {
754  if (Tx1[i] < - detA*SMALL)
755  {
756  scalar mu = - y0[i]/Tx1[i];
757 
758  if (debug)
759  {
760  Pout<< "Hit on tet face " << i << " at local coordinate "
761  << y0 + mu*Tx1 << ", " << mu*detA*100 << "% of the "
762  << "way along the track" << endl;
763  }
764 
765  if (0 <= mu && mu < muH)
766  {
767  iH = i;
768  muH = mu;
769  }
770  }
771  }
772 
773  // Set the new coordinates
774  barycentric yH = y0 + muH*Tx1;
775 
776  // Clamp to zero any negative coordinates generated by round-off error
777  for (label i = 0; i < 4; ++ i)
778  {
779  yH.replace(i, i == iH ? 0 : max(0, yH[i]));
780  }
781 
782  // Re-normalise if within the tet
783  if (iH == -1)
784  {
785  yH /= cmptSum(yH);
786  }
787 
788  // Set the new position and hit index
789  coordinates_ = yH;
790  tetTriI = iH;
791 
792  if (debug)
793  {
794  if (iH != -1)
795  {
796  Pout<< "Track hit tet face " << iH << " first" << endl;
797  }
798  else
799  {
800  Pout<< "Track hit no tet faces" << endl;
801  }
802  Pout<< "End local coordinates = " << yH << endl
803  << "End global coordinates = " << position() << endl
804  << "Tracking displacement = " << position() - x0 << endl
805  << muH*detA*100 << "% of the step from " << stepFraction_ << " to "
806  << stepFraction_ + fraction << " completed" << endl << endl;
807  }
808 
809  // Set the proportion of the track that has been completed
810  stepFraction_ += fraction*muH*detA;
811 
812  if (debug)
813  {
814  Pout << "Step Fraction : " << stepFraction_ << endl;
815  Pout << "fraction*muH*detA : " << fraction*muH*detA << endl;
816  Pout << "static muH : " << muH << endl;
817  Pout << "origId() : " << origId() << endl;
818  }
819 
820  // Accumulate displacement behind
821  if (detA <= 0 || nBehind_ > 0)
822  {
823  behind_ += muH*detA*mag(displacement);
824 
825  if (behind_ > 0)
826  {
827  behind_ = 0;
828  nBehind_ = 0;
829  }
830  else
831  {
832  ++ nBehind_;
833  }
834  }
835 
836  return iH != -1 ? 1 - muH*detA : 0;
837 }
838 
839 
841 (
842  const vector& displacement,
843  const scalar fraction,
844  label& tetTriI
845 )
846 {
847  const vector x0 = position();
848  const vector x1 = displacement;
849  const barycentric y0 = coordinates_;
850 
851  if (debug)
852  {
853  Pout<< "Particle " << origId() << endl << "Tracking from " << x0
854  << " along " << x1 << " to " << x0 + x1 << endl;
855  }
856 
857  // Get the tet geometry
858  Pair<vector> centre;
859  FixedList<scalar, 4> detA;
860  FixedList<barycentricTensor, 3> T;
861  movingTetReverseTransform(fraction, centre, detA, T);
862 
863  if (debug)
864  {
865  Pair<vector> o, b, v1, v2;
866  movingTetGeometry(fraction, o, b, v1, v2);
867  Pout<< "Tet points o=" << o[0] << ", b=" << b[0]
868  << ", v1=" << v1[0] << ", v2=" << v2[0] << endl
869  << "Tet determinant = " << detA[0] << endl
870  << "Start local coordinates = " << y0[0] << endl;
871  }
872 
873 
874  // Get the relative global position
875  const vector x0Rel = x0 - centre[0];
876  const vector x1Rel = x1 - centre[1];
877 
878  // Form the determinant and hit equations
879  cubicEqn detAEqn(sqr(detA[0])*detA[3], detA[0]*detA[2], detA[1], 1);
880  const barycentric yC(1, 0, 0, 0);
881  const barycentric hitEqnA =
882  ((x1Rel & T[2]) + detA[3]*yC)*sqr(detA[0]);
883  const barycentric hitEqnB =
884  ((x1Rel & T[1]) + (x0Rel & T[2]) + detA[2]*yC)*detA[0];
885  const barycentric hitEqnC =
886  ((x1Rel & T[0]) + (x0Rel & T[1]) + detA[1]*yC);
887  const barycentric hitEqnD = y0;
888  FixedList<cubicEqn, 4> hitEqn;
889  forAll(hitEqn, i)
890  {
891  hitEqn[i] = cubicEqn(hitEqnA[i], hitEqnB[i], hitEqnC[i], hitEqnD[i]);
892  }
893 
894  if (debug)
895  {
896  for (label i = 0; i < 4; ++ i)
897  {
898  Pout<< (i ? " " : "Hit equation ") << i << " = "
899  << hitEqn[i] << endl;
900  }
901  Pout<< " DetA equation = " << detA << endl;
902  }
903 
904  // Calculate the hit fraction
905  label iH = -1;
906  scalar muH = detA[0] <= 0 ? VGREAT : 1/detA[0];
907  for (label i = 0; i < 4; ++i)
908  {
909  const Roots<3> mu = hitEqn[i].roots();
910 
911  for (label j = 0; j < 3; ++j)
912  {
913  if
914  (
915  mu.type(j) == roots::real
916  && hitEqn[i].derivative(mu[j]) < - detA[0]*SMALL
917  )
918  {
919  if (debug)
920  {
921  const barycentric yH
922  (
923  hitEqn[0].value(mu[j]),
924  hitEqn[1].value(mu[j]),
925  hitEqn[2].value(mu[j]),
926  hitEqn[3].value(mu[j])
927  );
928  const scalar detAH = detAEqn.value(mu[j]);
929 
930  Pout<< "Hit on tet face " << i << " at local coordinate "
931  << (std::isnormal(detAH) ? name(yH/detAH) : "???")
932  << ", " << mu[j]*detA[0]*100 << "% of the "
933  << "way along the track" << endl;
934 
935  Pout<< "derivative : " << hitEqn[i].derivative(mu[j]) << nl
936  << " coord " << j << " mu[j]: " << mu[j] << nl
937  << " hitEq " << i << endl;
938  }
939 
940  if (0 <= mu[j] && mu[j] < muH)
941  {
942  iH = i;
943  muH = mu[j];
944  }
945  }
946  }
947  }
948 
949  // Set the new coordinates
950  barycentric yH
951  (
952  hitEqn[0].value(muH),
953  hitEqn[1].value(muH),
954  hitEqn[2].value(muH),
955  hitEqn[3].value(muH)
956  );
957  // !!! <-- This fails if the tet collapses onto the particle, as detA tends
958  // to zero at the hit. In this instance, we can differentiate the hit and
959  // detA polynomials to find a limiting location, but this will not be on a
960  // triangle. We will then need to do a second track through the degenerate
961  // tet to find the final hit position. This second track is over zero
962  // distance and therefore can be of the static mesh type. This has not yet
963  // been implemented.
964  const scalar detAH = detAEqn.value(muH);
965  if (!std::isnormal(detAH))
966  {
968  << "A moving tet collapsed onto a particle. This is not supported. "
969  << "The mesh is too poor, or the motion too severe, for particle "
970  << "tracking to function." << exit(FatalError);
971  }
972  yH /= detAH;
973 
974  // Clamp to zero any negative coordinates generated by round-off error
975  for (label i = 0; i < 4; ++ i)
976  {
977  yH.replace(i, i == iH ? 0 : max(0, yH[i]));
978  }
979 
980  // Re-normalise if within the tet
981  if (iH == -1)
982  {
983  yH /= cmptSum(yH);
984  }
985 
986  // Set the new position and hit index
987  coordinates_ = yH;
988  tetTriI = iH;
989 
990  scalar advance = muH*detA[0];
991 
992  // Set the proportion of the track that has been completed
993  stepFraction_ += fraction*advance;
994 
995  // Accumulate displacement behind
996  if (detA[0] <= 0 || nBehind_ > 0)
997  {
998  behind_ += muH*detA[0]*mag(displacement);
999 
1000  if (behind_ > 0)
1001  {
1002  behind_ = 0;
1003  nBehind_ = 0;
1004  }
1005  else
1006  {
1007  ++ nBehind_;
1008  }
1009  }
1010 
1011  if (debug)
1012  {
1013  if (iH != -1)
1014  {
1015  Pout<< "Track hit tet face " << iH << " first" << endl;
1016  }
1017  else
1018  {
1019  Pout<< "Track hit no tet faces" << endl;
1020  }
1021 // Pout<< "End local coordinates = " << yH << endl
1022 // << "End global coordinates = " << position() << endl
1023 // << "Tracking displacement = " << position() - x0 << endl
1024 // << muH*detA[0]*100 << "% of the step from " << stepFraction_
1025 // << " to " << stepFraction_ + fraction << " completed" << endl
1026 // << endl;
1027  }
1029 
1030  return iH != -1 ? 1 - muH*detA[0] : 0;
1031 }
1032 
1033 
1034 Foam::scalar Foam::particle::trackToTri
1035 (
1036  const vector& displacement,
1037  const scalar fraction,
1038  label& tetTriI
1039 )
1040 {
1041  if ((mesh_.moving() && (stepFraction_ != 1 || fraction != 0)))
1042  {
1043  return trackToMovingTri(displacement, fraction, tetTriI);
1044  }
1045  else
1046  {
1047  return trackToStationaryTri(displacement, fraction, tetTriI);
1048  }
1049 }
1050 
1051 
1053 {
1054  if (cmptMin(mesh_.geometricD()) == -1)
1055  {
1056  vector pos = position(), posC = pos;
1057  meshTools::constrainToMeshCentre(mesh_, posC);
1058  return pos - posC;
1059  }
1060  else
1061  {
1062  return vector::zero;
1063  }
1065 
1066 
1068 {}
1069 
1070 
1072 {}
1073 
1074 
1077  // Convert the face index to be local to the processor patch
1078  facei_ = mesh_.boundaryMesh()[patch()].whichFace(facei_);
1079 }
1080 
1081 
1083 (
1084  const label patchi,
1085  trackingData& td
1086 )
1087 {
1088  const coupledPolyPatch& ppp =
1089  refCast<const coupledPolyPatch>(mesh_.boundaryMesh()[patchi]);
1090 
1091  if (!ppp.parallel())
1092  {
1093  const tensor& T =
1094  (
1095  ppp.forwardT().size() == 1
1096  ? ppp.forwardT()[0]
1097  : ppp.forwardT()[facei_]
1098  );
1099  transformProperties(T);
1100  }
1101  else if (ppp.separated())
1102  {
1103  const vector& s =
1104  (
1105  (ppp.separation().size() == 1)
1106  ? ppp.separation()[0]
1107  : ppp.separation()[facei_]
1108  );
1109  transformProperties(-s);
1110  }
1111 
1112  // Set the topology
1113  celli_ = ppp.faceCells()[facei_];
1114  facei_ += ppp.start();
1115  tetFacei_ = facei_;
1116  // Faces either side of a coupled patch are numbered in opposite directions
1117  // as their normals both point away from their connected cells. The tet
1118  // point therefore counts in the opposite direction from the base point.
1119  tetPti_ = mesh_.faces()[tetFacei_].size() - 1 - tetPti_;
1120 
1121  // Reflect to account for the change of triangle orientation in the new cell
1122  reflect();
1123 
1124  // Note that the position does not need transforming explicitly. The face-
1125  // triangle on the receive patch is the transformation of the one on the
1126  // send patch, so whilst the barycentric coordinates remain the same, the
1127  // change of triangle implicitly transforms the position.
1128 }
1129 
1130 
1132 (
1134 )
1135 {
1136  // Get the transformed position
1137  const vector pos = transform.invTransformPosition(position());
1138 
1139  // Break the topology
1140  celli_ = -1;
1141  tetFacei_ = -1;
1142  tetPti_ = -1;
1143  facei_ = -1;
1144 
1145  // Store the position in the barycentric data
1146  coordinates_ = barycentric(1 - cmptSum(pos), pos.x(), pos.y(), pos.z());
1147 
1148  // Transform the properties
1149  transformProperties(- transform.t());
1150  if (transform.hasR())
1151  {
1152  transformProperties(transform.R().T());
1153  }
1154 }
1155 
1156 
1158 {
1159  // Get the position from the barycentric data
1160  const vector pos(coordinates_.b(), coordinates_.c(), coordinates_.d());
1161 
1162  // Create some arbitrary topology for the supplied cell
1163  celli_ = celli;
1164  tetFacei_ = mesh_.cells()[celli_][0];
1165  tetPti_ = 1;
1166  facei_ = -1;
1167 
1168  // Get the reverse transform and directly set the coordinates from the
1169  // position. This isn't likely to be correct; the particle is probably not
1170  // in this tet. It will, however, generate the correct vector when the
1171  // position method is called. A referred particle should never be tracked,
1172  // so this approximate topology is good enough. By using the nearby cell we
1173  // minimise the error associated with the incorrect topology.
1174  coordinates_ = barycentric(1, 0, 0, 0);
1175  if (mesh_.moving() && stepFraction_ != 1)
1176  {
1177  Pair<vector> centre;
1178  FixedList<scalar, 4> detA;
1179  FixedList<barycentricTensor, 3> T;
1180  movingTetReverseTransform(0, centre, detA, T);
1181  coordinates_ += (pos - centre[0]) & T[0]/detA[0];
1182  }
1183  else
1184  {
1185  vector centre;
1186  scalar detA;
1188  stationaryTetReverseTransform(centre, detA, T);
1189  coordinates_ += (pos - centre) & T/detA;
1190  }
1191 }
1192 
1193 
1194 Foam::label Foam::particle::procTetPt
1195 (
1196  const polyMesh& procMesh,
1197  const label procCell,
1198  const label procTetFace
1199 ) const
1200 {
1201  // The tet point on the procMesh differs from the current tet point if the
1202  // mesh and procMesh faces are of differing orientation. The change is the
1203  // same as in particle::correctAfterParallelTransfer.
1204 
1205  if
1206  (
1207  (mesh_.faceOwner()[tetFacei_] == celli_)
1208  == (procMesh.faceOwner()[procTetFace] == procCell)
1209  )
1210  {
1211  return tetPti_;
1212  }
1213  else
1214  {
1215  return procMesh.faces()[procTetFace].size() - 1 - tetPti_;
1216  }
1217 }
1218 
1219 
1221 (
1222  const vector& position,
1223  const mapPolyMesh& mapper
1224 )
1225 {
1226  locate
1227  (
1228  position,
1229  nullptr,
1230  mapper.reverseCellMap()[celli_],
1231  true,
1232  "Particle mapped to a location outside of the mesh"
1233  );
1234 }
1235 
1236 
1237 void Foam::particle::relocate(const point& position, const label celli)
1238 {
1239  locate
1240  (
1241  position,
1242  nullptr,
1243  celli,
1244  true,
1245  "Particle mapped to a location outside of the mesh"
1246  );
1247 }
1248 
1249 
1250 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
1252 bool Foam::operator==(const particle& pA, const particle& pB)
1253 {
1254  return (pA.origProc() == pB.origProc() && pA.origId() == pB.origId());
1255 }
1256 
1257 
1258 bool Foam::operator!=(const particle& pA, const particle& pB)
1259 {
1260  return !(pA == pB);
1261 }
1262 
1263 
1264 // ************************************************************************* //
void correctAfterParallelTransfer(const label patchi, trackingData &td)
Convert processor patch addressing to the global equivalents.
Definition: particle.C:1076
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
uint8_t direction
Definition: direction.H:46
vector deviationFromMeshCentre() const
Get the displacement from the mesh centre. Used to correct the.
Definition: particle.C:1045
virtual bool separated() const
Are the planes separated.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:68
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
Cmpt cmptSum(const SphericalTensor< Cmpt > &st)
Return the sum of components of a SphericalTensor.
label start() const noexcept
Return start label of this patch in the polyMesh face list.
Definition: polyPatch.H:441
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
label procTetPt(const polyMesh &procMesh, const label procCell, const label procTetFace) const
Return the tet point appropriate for decomposition or reconstruction.
Definition: particle.C:1188
Vector-tensor class used to perform translations and rotations in 3D space.
label origId() const noexcept
Return the particle ID on the originating processor.
Definition: particleI.H:194
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
dimensionedSymmTensor sqr(const dimensionedVector &dv)
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
scalar trackToFace(const vector &displacement, const scalar fraction)
As particle::track, but also stops on internal faces.
Definition: particle.C:649
scalar trackToTri(const vector &displacement, const scalar fraction, label &tetTriI)
As particle::trackToFace, but also stops on tet triangles. On.
Definition: particle.C:1028
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static bool writeLagrangianCoordinates
Write particle coordinates file (v1712 and later) Default is true.
Definition: particle.H:466
The coupledPolyPatch is an abstract base class for patches that couple regions of the computational d...
dimensionedScalar y0(const dimensionedScalar &ds)
Barycentric< scalar > barycentric
A scalar version of the templated Barycentric.
Definition: barycentric.H:41
label otherFace(const primitiveMesh &mesh, const label celli, const label facei, const label edgeI)
Return face on cell using edgeI but not facei. Throws error.
Definition: meshTools.C:548
virtual void transformProperties(const tensor &T)
Transform the physical properties of the particle.
Definition: particle.C:1060
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
Definition: debug.C:228
virtual const vectorField & separation() const
If the planes are separated the separation vector.
void prepareForInteractionListReferral(const vectorTensorTransform &transform)
Break the topology and store the particle position so that the.
Definition: particle.C:1125
virtual const tensorField & forwardT() const
Return face transformation tensor.
Base particle class.
Definition: particle.H:69
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
virtual bool parallel() const
Are the cyclic planes parallel.
dimensionedScalar pos(const dimensionedScalar &ds)
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const labelUList & faceCells() const
Return face-cell addressing.
Definition: polyPatch.C:365
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
particle(const polyMesh &mesh, const barycentric &coordinates, const label celli, const label tetFacei, const label tetPti)
Construct from components.
Definition: particle.C:507
3D tensor transformation operations.
Inter-processor communications stream.
Definition: Pstream.H:57
face triFace(3)
void correctAfterInteractionListReferral(const label celli)
Correct the topology after referral. The particle may still be.
Definition: particle.C:1150
void prepareForParallelTransfer()
Convert global addressing to the processor patch local equivalents.
Definition: particle.C:1068
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:520
Vector< scalar > vector
Definition: vector.H:57
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
void autoMap(const vector &position, const mapPolyMesh &mapper)
Map after a topology change.
Definition: particle.C:1214
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
Foam::edge edge(const label edgei) const
Return i-th face edge (forward walk order).
Definition: faceI.H:133
int debug
Static debugging option.
#define FUNCTION_NAME
defineTypeNameAndDebug(combustionModel, 0)
labelList f(nPoints)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
BarycentricTensor< scalar > barycentricTensor
A scalar version of the templated BarycentricTensor.
const volScalarField & T
const dimensionedScalar mu
Atomic mass unit.
label origProc() const noexcept
Return the originating processor ID.
Definition: particleI.H:182
scalar trackToMovingTri(const vector &displacement, const scalar fraction, label &tetTriI)
As particle::trackToTri, but for moving meshes.
Definition: particle.C:834
PtrList< coordinateSystem > coordinates(solidRegions.size())
#define WarningInFunction
Report a warning using Foam::Warning.
static bool writeLagrangianPositions
Write particle positions file (v1706 format and earlier) Default is true (disable in etc/controlDict)...
Definition: particle.H:472
A cell is defined as a list of faces with extra functionality.
Definition: cell.H:53
const dimensionedScalar c
Speed of light in a vacuum.
const std::string patch
OpenFOAM patch number as a std::string.
void relocate(const point &position, const label celli=-1)
Set the addressing based on the provided position.
Definition: particle.C:1230
registerInfoSwitch("writeLagrangianPositions", bool, Foam::particle::writeLagrangianPositions)
static label particleCount_
Cumulative particle counter - used to provide unique ID.
Definition: particle.H:455
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:297
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
static int compare(const face &a, const face &b)
Compare faces.
Definition: face.C:273
void replace(const direction d, const dimensioned< cmptType > &dc)
Return a component with a dimensioned<cmptType>
scalar trackToStationaryTri(const vector &displacement, const scalar fraction, label &tetTriI)
As particle::trackToTri, but for stationary meshes.
Definition: particle.C:705
volScalarField & p
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
void cmptMin(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:521
static const Foam::dimensionedScalar A("", Foam::dimPressure, 611.21)
scalar track(const vector &displacement, const scalar fraction)
Track along the displacement for a given fraction of the overall.
Definition: particle.C:630
Tensor of scalars, i.e. Tensor<scalar>.
void constrainToMeshCentre(const polyMesh &mesh, point &pt)
Set the constrained components of position to mesh centre.
Definition: meshTools.C:622
vector position() const
Return current particle position.
Definition: particleI.H:283
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Namespace for OpenFOAM.
static int compare(const edge &a, const edge &b)
Compare edges.
Definition: edgeI.H:26