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-2020 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  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  forAll(mesh_.cells()[celli_], cellFaceI)
280  {
281  const label newFaceI = mesh_.cells()[celli_][cellFaceI];
282  const class face& newFace = mesh_.faces()[newFaceI];
283  const label newOwner = mesh_.faceOwner()[newFaceI];
284 
285  // Exclude the current face
286  if (tetFacei_ == newFaceI)
287  {
288  continue;
289  }
290 
291  // Loop over the edges, looking for the shared one. Note that we have to
292  // match the direction of the edge as well as the end points in order to
293  // avoid false positives when dealing with coincident ACMI faces.
294  const label edgeComp = newOwner == celli_ ? -1 : +1;
295  label edgeI = 0;
296  for
297  (
298  ;
299  edgeI < newFace.size()
300  && edge::compare(sharedEdge, newFace.faceEdge(edgeI)) != edgeComp;
301  ++ edgeI
302  );
303 
304  // If the face does not contain the edge, then move on to the next face
305  if (edgeI >= newFace.size())
306  {
307  continue;
308  }
309 
310  // Make the edge index relative to the base point
311  const label newBaseI = max(0, mesh_.tetBasePtIs()[newFaceI]);
312  edgeI = (edgeI - newBaseI + newFace.size()) % newFace.size();
313 
314  // If the edge is next the base point (i.e., the index is 0 or n - 1),
315  // then we swap it for the adjacent edge. This new edge is opposite the
316  // base point, and defines the tet with the original edge in it.
317  edgeI = min(max(1, edgeI), newFace.size() - 2);
318 
319  // Set the new face and tet point
320  tetFacei_ = newFaceI;
321  tetPti_ = edgeI;
322 
323  // Exit the loop now that the tet point has been found
324  break;
325  }
326 
327  if (tetPti_ == -1)
328  {
330  << "The search for an edge-connected face and tet-point failed."
331  << exit(FatalError);
332  }
333 
334  // Pre-rotation puts the shared edge opposite the base of the tetrahedron
335  if (sharedEdge.otherVertex(triOldIs[1]) == -1)
336  {
337  rotate(false);
338  }
339  else if (sharedEdge.otherVertex(triOldIs[2]) == -1)
340  {
341  rotate(true);
342  }
343 
344  // Get the new topology
345  const triFace triNewIs = currentTetIndices().faceTriIs(mesh_);
346 
347  // Reflect to account for the change of triangle orientation on the new face
348  reflect();
349 
350  // Post rotation puts the shared edge back in the correct location
351  if (sharedEdge.otherVertex(triNewIs[1]) == -1)
352  {
353  rotate(true);
354  }
355  else if (sharedEdge.otherVertex(triNewIs[2]) == -1)
356  {
357  rotate(false);
358  }
359 }
360 
361 
362 void Foam::particle::changeCell()
363 {
364  // Set the cell to be the one on the other side of the face
365  const label ownerCellI = mesh_.faceOwner()[tetFacei_];
366  const bool isOwner = celli_ == ownerCellI;
367  celli_ = isOwner ? mesh_.faceNeighbour()[tetFacei_] : ownerCellI;
368  // Reflect to account for the change of triangle orientation in the new cell
369  reflect();
370 }
371 
372 
373 void Foam::particle::changeToMasterPatch()
374 {
375  label thisPatch = patch();
376 
377  forAll(mesh_.cells()[celli_], cellFaceI)
378  {
379  // Skip the current face and any internal faces
380  const label otherFaceI = mesh_.cells()[celli_][cellFaceI];
381  if (facei_ == otherFaceI || mesh_.isInternalFace(otherFaceI))
382  {
383  continue;
384  }
385 
386  // Compare the two faces. If they are the same, chose the one with the
387  // lower patch index. In the case of an ACMI-wall pair, this will be
388  // the ACMI, which is what we want.
389  const class face& thisFace = mesh_.faces()[facei_];
390  const class face& otherFace = mesh_.faces()[otherFaceI];
391  if (face::compare(thisFace, otherFace) != 0)
392  {
393  const label otherPatch =
394  mesh_.boundaryMesh().whichPatch(otherFaceI);
395  if (thisPatch > otherPatch)
396  {
397  facei_ = otherFaceI;
398  thisPatch = otherPatch;
399  }
400  }
401  }
402 
403  tetFacei_ = facei_;
404 }
405 
406 
407 void Foam::particle::locate
408 (
409  const vector& position,
410  const vector* direction,
411  const label celli,
412  const bool boundaryFail,
413  const string& boundaryMsg
414 )
415 {
416  if (debug)
417  {
418  Info << "Particle " << origId() << nl << FUNCTION_NAME << nl << endl;
419  }
420 
421  celli_ = celli;
422 
423  // Find the cell, if it has not been given
424  if (celli < 0)
425  {
426  celli_ = mesh_.cellTree().findInside(position);
427  }
428  if (celli < 0)
429  {
431  << "Cell not found for particle position " << position << "."
432  << exit(FatalError);
433  }
434 
435  const vector displacement = position - mesh_.cellCentres()[celli_];
436 
437  // Loop all cell tets to find the one containing the position. Track
438  // through each tet from the cell centre. If a tet contains the position
439  // then the track will end with a single trackToTri.
440  const class cell& c = mesh_.cells()[celli_];
441  scalar minF = VGREAT;
442  label minTetFacei = -1, minTetPti = -1;
443  forAll(c, cellTetFacei)
444  {
445  const class face& f = mesh_.faces()[c[cellTetFacei]];
446  for (label tetPti = 1; tetPti < f.size() - 1; ++tetPti)
447  {
448  coordinates_ = barycentric(1, 0, 0, 0);
449  tetFacei_ = c[cellTetFacei];
450  tetPti_ = tetPti;
451  facei_ = -1;
452 
453  label tetTriI = -1;
454  const scalar f = trackToTri(displacement, 0, tetTriI);
455 
456  if (tetTriI == -1)
457  {
458  return;
459  }
460 
461  if (f < minF)
462  {
463  minF = f;
464  minTetFacei = tetFacei_;
465  minTetPti = tetPti_;
466  }
467  }
468  }
469 
470  // The particle must be (hopefully only slightly) outside the cell. Track
471  // into the tet which got the furthest.
472  coordinates_ = barycentric(1, 0, 0, 0);
473  tetFacei_ = minTetFacei;
474  tetPti_ = minTetPti;
475  facei_ = -1;
476  track(displacement, 0);
477  if (!onFace())
478  {
479  return;
480  }
481 
482  // If we are here then we hit a boundary
483  if (boundaryFail)
484  {
485  FatalErrorInFunction << boundaryMsg << exit(FatalError);
486  }
487  else
488  {
489  static label nWarnings = 0;
490  static const label maxNWarnings = 100;
491  if ((nWarnings < maxNWarnings) && boundaryFail)
492  {
493  WarningInFunction << boundaryMsg.c_str() << endl;
494  ++ nWarnings;
495  }
496  if (nWarnings == maxNWarnings)
497  {
499  << "Suppressing any further warnings about particles being "
500  << "located outside of the mesh." << endl;
501  ++ nWarnings;
502  }
503  }
504 
505 }
506 
507 
508 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
509 
511 (
512  const polyMesh& mesh,
513  const barycentric& coordinates,
514  const label celli,
515  const label tetFacei,
516  const label tetPti
517 )
518 :
519  mesh_(mesh),
520  coordinates_(coordinates),
521  celli_(celli),
522  tetFacei_(tetFacei),
523  tetPti_(tetPti),
524  facei_(-1),
525  stepFraction_(1.0),
526  behind_(0.0),
527  nBehind_(0),
528  origProc_(Pstream::myProcNo()),
529  origId_(getNewParticleID())
530 {}
531 
532 
534 (
535  const polyMesh& mesh,
536  const vector& position,
537  const label celli
538 )
539 :
540  mesh_(mesh),
541  coordinates_(-VGREAT, -VGREAT, -VGREAT, -VGREAT),
542  celli_(celli),
543  tetFacei_(-1),
544  tetPti_(-1),
545  facei_(-1),
546  stepFraction_(1.0),
547  behind_(0.0),
548  nBehind_(0),
549  origProc_(Pstream::myProcNo()),
550  origId_(getNewParticleID())
551 {
552  locate
553  (
554  position,
555  nullptr,
556  celli,
557  false,
558  "Particle initialised with a location outside of the mesh"
559  );
560 }
561 
562 
564 (
565  const polyMesh& mesh,
566  const vector& position,
567  const label celli,
568  const label tetFacei,
569  const label tetPti,
570  bool doLocate
571 )
572 :
573  mesh_(mesh),
574  coordinates_(-VGREAT, -VGREAT, -VGREAT, -VGREAT),
575  celli_(celli),
576  tetFacei_(tetFacei),
577  tetPti_(tetPti),
578  facei_(-1),
579  stepFraction_(1.0),
580  behind_(0.0),
581  nBehind_(0),
582  origProc_(Pstream::myProcNo()),
583  origId_(getNewParticleID())
584 {
585  if (doLocate)
586  {
587  locate
588  (
589  position,
590  nullptr,
591  celli,
592  false,
593  "Particle initialised with a location outside of the mesh"
594  );
595  }
596 }
597 
598 
600 :
601  mesh_(p.mesh_),
602  coordinates_(p.coordinates_),
603  celli_(p.celli_),
604  tetFacei_(p.tetFacei_),
605  tetPti_(p.tetPti_),
606  facei_(p.facei_),
607  stepFraction_(p.stepFraction_),
608  behind_(p.behind_),
609  nBehind_(p.nBehind_),
610  origProc_(p.origProc_),
611  origId_(p.origId_)
612 {}
613 
614 
616 :
617  mesh_(mesh),
618  coordinates_(p.coordinates_),
619  celli_(p.celli_),
620  tetFacei_(p.tetFacei_),
621  tetPti_(p.tetPti_),
622  facei_(p.facei_),
623  stepFraction_(p.stepFraction_),
624  behind_(p.behind_),
625  nBehind_(p.nBehind_),
626  origProc_(p.origProc_),
627  origId_(p.origId_)
628 {}
629 
630 
631 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
632 
633 Foam::scalar Foam::particle::track
634 (
635  const vector& displacement,
636  const scalar fraction
637 )
638 {
639  scalar f = trackToFace(displacement, fraction);
640 
641  while (onInternalFace())
642  {
643  changeCell();
644 
645  f *= trackToFace(f*displacement, f*fraction);
646  }
647 
648  return f;
649 }
650 
651 
652 Foam::scalar Foam::particle::trackToFace
653 (
654  const vector& displacement,
655  const scalar fraction
656 )
657 {
658  scalar f = 1;
659 
660  label tetTriI = onFace() ? 0 : -1;
661 
662  facei_ = -1;
663 
664  // Loop the tets in the current cell
665  while (nBehind_ < maxNBehind_)
666  {
667  f *= trackToTri(f*displacement, f*fraction, tetTriI);
668 
669  if (tetTriI == -1)
670  {
671  // The track has completed within the current tet
672  return 0;
673  }
674  else if (tetTriI == 0)
675  {
676  // The track has hit a face, so set the current face and return
677  facei_ = tetFacei_;
678  return f;
679  }
680  else
681  {
682  // Move to the next tet and continue the track
683  changeTet(tetTriI);
684  }
685  }
686 
687  // Warn if stuck, and incorrectly advance the step fraction to completion
688  static label stuckID = -1, stuckProc = -1;
689  if (origId_ != stuckID && origProc_ != stuckProc)
690  {
692  << "Particle #" << origId_ << " got stuck at " << position()
693  << endl;
694  }
695 
696  stuckID = origId_;
697  stuckProc = origProc_;
698 
699  stepFraction_ += f*fraction;
700 
701  behind_ = 0;
702  nBehind_ = 0;
703 
704  return 0;
705 }
706 
707 
709 (
710  const vector& displacement,
711  const scalar fraction,
712  label& tetTriI
713 )
714 {
715  const vector x0 = position();
716  const vector x1 = displacement;
717  const barycentric y0 = coordinates_;
718 
719  if (debug)
720  {
721  Pout<< "Particle " << origId() << endl << "Tracking from " << x0
722  << " along " << x1 << " to " << x0 + x1 << endl;
723  }
724 
725  // Get the tet geometry
726  vector centre;
727  scalar detA;
729  stationaryTetReverseTransform(centre, detA, T);
730 
731  if (debug)
732  {
733  vector o, b, v1, v2;
734  stationaryTetGeometry(o, b, v1, v2);
735  Pout<< "Tet points o=" << o << ", b=" << b
736  << ", v1=" << v1 << ", v2=" << v2 << 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 = std::isnormal(detA) && 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  Info << "Step Fraction : " << stepFraction_ << endl;
815  Info << "fraction*muH*detA : " << fraction*muH*detA << endl;
816  Info << "static muH : " << muH << endl;
817  Info << "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;
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  Info<< "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  Info<< (i ? " " : "Hit equation ") << i << " = "
899  << hitEqn[i] << endl;
900  }
901  Info<< " DetA equation = " << detA << endl;
902  }
903 
904  // Calculate the hit fraction
905  label iH = -1;
906  scalar muH = std::isnormal(detA[0]) && 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  Info<< "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  Info<< "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  }
1028 
1029 
1030  return iH != -1 ? 1 - muH*detA[0] : 0;
1031 }
1032 
1033 
1034 Foam::scalar Foam::particle::trackToTri
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  }
1064 }
1065 
1066 
1068 {}
1069 
1070 
1072 {}
1073 
1074 
1076 {
1077  // Convert the face index to be local to the processor patch
1078  facei_ = mesh_.boundaryMesh()[patch()].whichFace(facei_);
1079 }
1080 
1081 
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 
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;
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
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 
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 * * * * * * * * * * * * * * //
1251 
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 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::particle::prepareForParallelTransfer
void prepareForParallelTransfer()
Convert global addressing to the processor patch local equivalents.
Definition: particle.C:1075
Foam::reverse
void reverse(UList< T > &list, const label n)
Definition: UListI.H:396
Foam::Tensor< scalar >
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::coupledPolyPatch::separation
virtual const vectorField & separation() const
If the planes are separated the separation vector.
Definition: coupledPolyPatch.H:284
cubicEqn.H
Foam::particle::correctAfterInteractionListReferral
void correctAfterInteractionListReferral(const label celli)
Correct the topology after referral. The particle may still be.
Definition: particle.C:1157
Foam::barycentricTensor
BarycentricTensor< scalar > barycentricTensor
A scalar version of the templated BarycentricTensor.
Definition: barycentricTensor.H:48
s
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))
Definition: gmvOutputSpray.H:25
Foam::meshTools::otherFace
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:555
Foam::constant::physicoChemical::mu
const dimensionedScalar mu
Atomic mass unit.
Definition: createFieldRefs.H:4
Foam::particle::origId
label origId() const
Return the particle ID on the originating processor.
Definition: particleI.H:221
Foam::particle::autoMap
void autoMap(const vector &position, const mapPolyMesh &mapper)
Map after a topology change.
Definition: particle.C:1221
Foam::particle::trackToMovingTri
scalar trackToMovingTri(const vector &displacement, const scalar fraction, label &tetTriI)
As particle::trackToTri, but for moving meshes.
Definition: particle.C:841
Foam::coupledPolyPatch::forwardT
virtual const tensorField & forwardT() const
Return face transformation tensor.
Definition: coupledPolyPatch.H:296
indexedOctree.H
A
static const Foam::dimensionedScalar A("", Foam::dimPressure, 611.21)
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::coupledPolyPatch
The coupledPolyPatch is an abstract base class for patches that couple regions of the computational d...
Definition: coupledPolyPatch.H:53
Foam::particle::relocate
void relocate(const point &position, const label celli=-1)
Set the addressing based on the provided position.
Definition: particle.C:1237
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::transform
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:519
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:913
Foam::particle::particleCount_
static label particleCount_
Cumulative particle counter - used to provide unique ID.
Definition: particle.H:349
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::particle::writeLagrangianPositions
static bool writeLagrangianPositions
Definition: particle.H:360
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::face::compare
static int compare(const face &a, const face &b)
Compare faces.
Definition: face.C:300
Foam::cmptMin
void cmptMin(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:302
Foam::coupledPolyPatch::parallel
virtual bool parallel() const
Are the cyclic planes parallel.
Definition: coupledPolyPatch.H:290
Foam::debug::infoSwitch
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
Definition: debug.C:231
Foam::particle::track
scalar track(const vector &displacement, const scalar fraction)
Track along the displacement for a given fraction of the overall.
Definition: particle.C:634
Foam::operator!=
bool operator!=(const eddy &a, const eddy &b)
Definition: eddy.H:235
Foam::coupledPolyPatch::separated
virtual bool separated() const
Are the planes separated.
Definition: coupledPolyPatch.H:278
Foam::particle::trackToStationaryTri
scalar trackToStationaryTri(const vector &displacement, const scalar fraction, label &tetTriI)
As particle::trackToTri, but for stationary meshes.
Definition: particle.C:709
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::operator==
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::cubicEqn::value
scalar value(const scalar x) const
Evaluate the cubic equation at x.
Definition: cubicEqnI.H:105
coordinates
PtrList< coordinateSystem > coordinates(solidRegions.size())
Foam::polyMesh::faceOwner
virtual const labelList & faceOwner() const
Return face owner.
Definition: polyMesh.C:1107
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::y0
dimensionedScalar y0(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:281
Foam::Barycentric< scalar >
Foam::roots::real
Definition: Roots.H:56
Foam::particle::deviationFromMeshCentre
vector deviationFromMeshCentre() const
Get the displacement from the mesh centre. Used to correct the.
Definition: particle.C:1052
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::VectorSpace::replace
void replace(const direction, const Cmpt &)
Definition: VectorSpaceI.H:145
Foam::particle::origProc
label origProc() const
Return the originating processor ID.
Definition: particleI.H:209
Foam::FatalError
error FatalError
Foam::particle::correctAfterParallelTransfer
void correctAfterParallelTransfer(const label patchi, trackingData &td)
Convert processor patch addressing to the global equivalents.
Definition: particle.C:1083
Foam::particle::procTetPt
label procTetPt(const polyMesh &procMesh, const label procCell, const label procTetFace) const
Return the tet point appropriate for decomposition or reconstruction.
Definition: particle.C:1195
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::cmptSum
Cmpt cmptSum(const SphericalTensor< Cmpt > &st)
Return the sum of components of a SphericalTensor.
Definition: SphericalTensorI.H:164
Foam::polyPatch::faceCells
const labelUList & faceCells() const
Return face-cell addressing.
Definition: polyPatch.C:363
treeDataCell.H
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:51
Foam::polyPatch::start
label start() const
Return start label of this patch in the polyMesh face list.
Definition: polyPatch.H:313
Foam::particle::trackToFace
scalar trackToFace(const vector &displacement, const scalar fraction)
As particle::track, but also stops on internal faces.
Definition: particle.C:653
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::particle::writeLagrangianCoordinates
static bool writeLagrangianCoordinates
Definition: particle.H:356
Foam::mapPolyMesh::reverseCellMap
const labelList & reverseCellMap() const
Reverse cell map.
Definition: mapPolyMesh.H:532
Foam::polyMesh::faces
virtual const faceList & faces() const
Return raw faces.
Definition: polyMesh.C:1094
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::edge::compare
static int compare(const edge &a, const edge &b)
Compare edges.
Definition: edgeI.H:33
Foam::particle::trackToTri
scalar trackToTri(const vector &displacement, const scalar fraction, label &tetTriI)
As particle::trackToFace, but also stops on tet triangles. On.
Definition: particle.C:1035
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:464
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::Pair
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:54
f
labelList f(nPoints)
Foam::foamVersion::patch
const std::string patch
OpenFOAM patch number as a std::string.
Foam::particle::transformProperties
virtual void transformProperties(const tensor &T)
Transform the physical properties of the particle.
Definition: particle.C:1067
Foam::Vector< scalar >
Foam::vectorTensorTransform
Vector-tensor class used to perform translations and rotations in 3D space.
Definition: vectorTensorTransform.H:63
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::FixedList
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:104
Foam::particle
Base particle class.
Definition: particle.H:76
registerInfoSwitch
registerInfoSwitch("writeLagrangianPositions", bool, Foam::particle::writeLagrangianPositions)
Foam::particle::prepareForInteractionListReferral
void prepareForInteractionListReferral(const vectorTensorTransform &transform)
Break the topology and store the particle position so that the.
Definition: particle.C:1132
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::BarycentricTensor
Templated 4x3 tensor derived from VectorSpace. Has 12 components. Can represent a barycentric transfo...
Definition: BarycentricTensor.H:57
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::meshTools::constrainToMeshCentre
void constrainToMeshCentre(const polyMesh &mesh, point &pt)
Set the constrained components of position to mesh centre.
Definition: meshTools.C:629
particle.H
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:161
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::VectorSpace< Vector< Cmpt >, Cmpt, 3 >::zero
static const Vector< Cmpt > zero
Definition: VectorSpace.H:115
Foam::cubicEqn
Container to encapsulate various operations for cubic equation of the forms with real coefficients:
Definition: cubicEqn.H:112
registerSwitch.H
Foam::barycentric
Barycentric< scalar > barycentric
A scalar version of the templated Barycentric.
Definition: barycentric.H:47
transform.H
3D tensor transformation operations.
Foam::Roots
Templated storage for the roots of polynomial equations, plus flags to indicate the nature of the roo...
Definition: Roots.H:70
Foam::particle::trackingData
Definition: particle.H:95
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
triFace
face triFace(3)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303
Foam::particle::particle
particle(const polyMesh &mesh, const barycentric &coordinates, const label celli, const label tetFacei, const label tetPti)
Construct from components.
Definition: particle.C:511
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177