From 29f4e77299508d3484990e0e245e2717059b75be Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Tue, 20 Jun 2017 14:46:12 +0100
Subject: [PATCH] ENH: wallBoundedStreamline: make sure to track across neg
 face; more testing

---
 .../wallBoundedParticleTemplates.C            | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/src/functionObjects/field/wallBoundedStreamLine/wallBoundedParticleTemplates.C b/src/functionObjects/field/wallBoundedStreamLine/wallBoundedParticleTemplates.C
index 731c4ad0b39..b2febe5203e 100644
--- a/src/functionObjects/field/wallBoundedStreamLine/wallBoundedParticleTemplates.C
+++ b/src/functionObjects/field/wallBoundedStreamLine/wallBoundedParticleTemplates.C
@@ -232,6 +232,39 @@ Foam::scalar Foam::wallBoundedParticle::trackToEdge
         {
             // Negative tet volume. Track back by setting the end point
             projectedEndPosition = position() - (endPosition-position());
+
+            // Make sure to use a large enough vector to cross the negative
+            // face. Bit overkill.
+            const vector d(endPosition-position());
+            const scalar magD(mag(d));
+            if (magD > ROOTVSMALL)
+            {
+                // Get overall mesh bounding box
+                treeBoundBox meshBb(mesh_.bounds());
+                // Extend to make 3D
+                meshBb.inflate(ROOTSMALL);
+
+                // Create vector guaranteed to cross mesh bounds
+                projectedEndPosition = position()-meshBb.mag()*d/magD;
+
+                // Clip to mesh bounds
+                point intPt;
+                direction intPtBits;
+                bool ok = meshBb.intersects
+                (
+                    projectedEndPosition,
+                    position()-projectedEndPosition,
+                    projectedEndPosition,
+                    position(),
+                    intPt,
+                    intPtBits
+                );
+                if (ok)
+                {
+                    // Should always be the case
+                    projectedEndPosition = intPt;
+                }
+            }
         }
 
         // Remove normal component
-- 
GitLab