0

I have a python script, which under some parameter space, prints out an error:

IntegrationWarning: The occurrence of round-off error is detected, which prevents the requested tolerance from being achieved.  The error may be underestimated.
function_integral = quad(some_function, x_a, x_b, args = (some parameters))[0]

In this code, x_b >= x_a, always, and, some_function(x) >= 0, always. Consequently, function_integral >= 0, always. Or it ought to.

The code continues to run, but, soon after crashes because of an error I deliberately added in (to catch possible bugs), which in this case, required function_integral >= 0. I crashed this code here with the crude approach of:

if function_integral < 0.:
     print("some stuff about the parameters")
     1/0

which, with ipython, I then use "debug" to go in and explore the issue.

This particular print-out of the "IntegrationWarning" error seems to occur when x_b = x_a + gamma, where gamma is really small compared to x_b and x_a. There are cases in the code when x_b = x_a, during which, the above integral is not necessary, since during these cases there's reasoning that function_integral ought to be zero. So, the integral function is not used here.

After the roundoff error, the code seems to return an integral value near function_integral = -1e-11, so, basically zero, but, the fact that it is negative, causes issues (and the round-off error itself causes concern for the accuracy of the code if it were to continue running).

I could by pass this issue in theory, by setting function_integral = 0 when x_b is basically equal to x_a, which might be sufficient for my purposes, but, I would like to explore the issue a bit more and see what's causing it to occur in the first place.

For some reason, when I add in a built-in crash, within the function which computes the integral (among a few other things) and outputs/returns its' value, it does not catch the error here. Only later in the code, when function_integral is used to define another parameter, which must be >= 0, has a crash (which I also built-in).

So, the question is, how do I crash the code (to debug it) when Python is printing out one of the built-in errors, like "IntegrationWarning"?

Edit: I ended up going with a quick & dirty approach of not calling quad() if x_b = x_a + gamma, where gamma is small compared to x_b and x_a. Not fully satisfied with this solution, but, in this case it'll probably be sufficient. Would be very curious to hear alternate approaches of debugging this case.

Canada709
  • 41
  • 8

1 Answers1

0
import sys

try:
    if function_integral < 0.:
        print("some stuff about the parameters")
except IntegrationWarning:
    sys.exit(1)

This should exit the code when there is 'Integration error'

Thibaultofc
  • 29
  • 10
  • Would you mind posting your entire code so I can see what's wrong about it ? – Thibaultofc Nov 22 '20 at 09:25
  • The particular function referred to above isn't that long, but, the entire code is a few thousand lines long, and I wouldn't really feel comfortable sharing it all. I can add other information, as needed, of course. – Canada709 Nov 24 '20 at 23:28