Problem 2100

Summary: Infinite loop in G4JTPolynomialSolver::FindRoots given degenerate coefficients
Product: Geant4 Reporter: mstoeckl
Component: geometry/solidsAssignee: Evgueni.Tcherniaev
Status: RESOLVED FIXED    
Severity: trivial    
Priority: P4    
Version: 10.4   
Hardware: All   
OS: All   
Attachments: gdb backtrace
Modified G4Torus.cc

Description mstoeckl 2018-11-10 18:51:07 CET
Created attachment 517 [details]
gdb backtrace

Calling G4Torus::DistanceToIn(p, v) with a large (norm ~ kInfinity) initial `p` vector may invoke G4JTPolynomialSolver::FindRoots to solve an equation where one of the coefficients is (floating-point) infinity. This causes an infinite loop when trying to "Chop the interval (0,x) until ff <= 0", as `ff` becomes NaN and the loop termination comparison never succeeds.

I use Geant4.10.2, on Linux and an i5-5200U, built in the Release configuration, with gcc, and the '-ggdb3' flag.

This happens very rarely -- I have an application (https://github.com/mstoeckl/gview) which produces unusual geometry queries (e.g., along rays close to perfectly tangent to solids, and starting very far away from the target), and have encountered infinite loops only twice out of ~10^5 such queries. A slight change to my own code avoids the bug entirely.
Comment 1 Evgueni.Tcherniaev 2018-11-12 17:47:15 CET
Thank you for reporting the problem. Normally the DistanceToIn(p,v) method in a solid with curved surfaces should have a check on how far is a point p from the solid. If the point is too far, then it should be moved closer to the surface.

It looks that G4Torus::DistanceToIn(p,v) does not have such a check. We will investigate and improve the code if it is the case.
Comment 2 Evgueni.Tcherniaev 2018-11-12 23:45:18 CET
Created attachment 518 [details]
Modified G4Torus.cc

Could you try modified G4Torus.cc where missing check has been added. I believe that it should fix the problem.
Comment 3 Evgueni.Tcherniaev 2018-11-19 08:58:01 CET
A check "if point is too far" has been added into G4Torus::DistanceToIn(p,v).