tschoonj / xraylib

A library for X-ray matter interaction cross sections for X-ray fluorescence applications
https://github.com/tschoonj/xraylib/wiki
Other
120 stars 54 forks source link

Non-zero return despite error for PM3_auger_cascade_kissel and PM4_auger_cascade_kissel #224

Closed Nin17 closed 6 months ago

Nin17 commented 8 months ago

In the error-handling section of the documentation, it states that If you are not interested in the error message, feel free to simply pass NULL, and rely on the return value of the function, since 0.0 (or NULL in some cases) is returned when an error occurred: ... This is perfectly fine, and is fact recommended if speed execution speed is paramount.. However, the functions PM3_auger_cascade_kissel and PM4_auger_cascade_kissel do not return 0.0 when an error has occurred in CS_Photo_Partial as they are missing the lines:

    if (rv == 0.0)
        return 0.0;

that are present in every other function in the file.

Python code demonstrating the problem (tested with xraylib from conda-forge versions 4.1.0 - 4.1.3):

import xraylib as xrl
import ctypes as ct
from ctypes.util import find_library

Z, E = 92, 895.

try:
    print(xrl.PM3_auger_cascade_kissel(Z, E, *[1.0]*6))
except ValueError as error:
    print(error)

try:
    print(xrl.PM4_auger_cascade_kissel(Z, E, *[1.0]*7))
except ValueError as error:
    print(error)

libxrl = ct.CDLL(find_library('xrl'))
PM3_auger_cascade_kissel = libxrl.PM3_auger_cascade_kissel
PM3_auger_cascade_kissel.restype = ct.c_double
PM3_auger_cascade_kissel.argtypes = [ct.c_int, *[ct.c_double]*7, ct.c_void_p]

PM4_auger_cascade_kissel = libxrl.PM4_auger_cascade_kissel
PM4_auger_cascade_kissel.restype = ct.c_double
PM4_auger_cascade_kissel.argtypes = [ct.c_int, *[ct.c_double]*8, ct.c_void_p]

print(PM3_auger_cascade_kissel(Z, E, *[1.0]*6, None))
print(PM4_auger_cascade_kissel(Z, E, *[1.0]*7, None))

Output:

Spline extrapolation is not allowed
Spline extrapolation is not allowed
1.0716170059114
1.22222728236858
tschoonj commented 8 months ago

Thanks for opening this issue. This indeed feels like a bug. Would you be willing to try and fix this yourself?

tschoonj commented 6 months ago

I finally had some time to resurrect the CI, and merged this ticket in.

The code itself looks fine, and it is certainly a bug, but I don't think that in real situations it would have led to incorrect values being generated, given that when CS_Photo_Partial errors out (which is going to be due to the excitation energy being too low to excite the M sub-shell), then the other P* parameters passed to the function will already be 0.

Thanks again for your contribution! I plan on making an xraylib release later this week