Skip to content

Thick/thin-target bremsstrahlung integrals are slow #60

Description

@elastufka

Describe the performance issue

Making the functions emission.integrate_part and emission.split_and_integrate more pythonic only improve readability and not performance.

To Reproduce

from sunxspex import emission

p,q, eebrk, eelow, eehigh=4.0,6.0,150.0,20.0,3200.0
photon_energies=np.linspace(4,5000,8000)
z=1.2
maxfcn=2048
rerr=1e-4

%%timeit
thick_new,_=emission.split_and_integrate(model='thick-target', photon_energies=photon_energies, maxfcn=maxfcn, rerr=rerr,eelow=eelow, eebrk=eebrk, eehigh=eehigh,p=p, q=q, z=z, efd=False)

%%timeit
thick_original,_=emission.split_and_integrate0(model='thick-target', photon_energies=photon_energies, maxfcn=maxfcn, rerr=rerr,eelow=eelow, eebrk=eebrk, eehigh=eehigh,p=p, q=q, z=z, efd=False)

Proposed fix

I'll order these roughly by the amount of time (increasing) it would take [me] to try each approach. For reference, the integral is here and the equation for the bremsstrahlung cross-section is here.

  • Do nothing. The only people who care about speed are those planning on automatically fitting thousands of spectra

    • of course, it would be nice to have at least some improvement to tempt more users away from OPSEX
    • also, this results in a slower fit
  • Talk to an expert, or the people who wrote the IDL versions. Please don't all volunteer at once ;)

  • Use multiprocessing to simultaneously calculate all orders of the integral from npoints=4 to npoints=2*12, then use matrix subtraction to calculate the error and return the appropriate solution

    • waste of resources if integration satisfies relative error condition after only a few iterations
  • Figure out how to get Gauss-Kronrod quadrature to work (implemented by quadpy.c1.adaptive)

    • problem is that the intervals of the definite integral get split in half, and then there is a dimension mismatch between the electron_energy and photon_energy input to bremsstrahlung_cross_section
      • side note: Docstring is super confusing. There's no reason at all (in the code) for the photon energies to 'correspond to' the electron energies, and writing it that way makes me think one can be used to calculate the other
    • splitting photon energy bins in half to match the dimensions sometimes results in NaN when electron_energy is lower than photon_energy
      • also I have no idea if that's violating any physical laws
    • without the brem_cross term adaptive quadrature works great
      • the original integral is also much faster without it, but it's kind of very important unfortunately
  • Enable GPU support so that those who care about speed can have it if they have the hardware

    • PyTorch implementation might not be too difficult
    • again, kind of a waste of resources, and would introduce another dependency that is quite a large package
  • Implement with Cython

    • Haulin could write the C part, and there is the C library cubature which is also designed for solving Gauss-Legendre quadrature. But I think only the intgrand would need to be in C (and therefore the collisional energy, electron density, and bremsstrahlung cross-section functions)
    • cons: pointers
  • Transform the limits of the integral to [-1,1] so that pre-computed Gauss-Legendre points and weights can be used to do the integration

    • This might not take a lot of time for someone who enjoys math
    • However, this requires dealing with the bremsstrahlung cross-section, a complicated polynomial dependent on the electron energy, photon energy, and cross-terms thereof. So such an approach might not be possible
    • maybe someone who remembers how to use Mathematica can check
  • Learn more math. Probably numerical integration didn't peak with Gauss-Legendre and there are newer, better methods better suited to this problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions