Skip to content

Drop GSL dependency - #26

Closed
krystophny wants to merge 1 commit into
mainfrom
drop-gsl
Closed

Drop GSL dependency#26
krystophny wants to merge 1 commit into
mainfrom
drop-gsl

Conversation

@krystophny

Copy link
Copy Markdown
Member

Removes the GSL/FGSL dependency. Replacements come from the libneo math kit
(itpplasma/libneo#290) plus a short in-tree Levin-u summation. All replacement
code is clean-room, implemented from published algorithms.

Resolves #24.

Changes

  • src/mephit_pert.f90: the bind(C) interface to gsl_sf_bessel_In_array
    is replaced by neo_bessel_i::bessel_in_array. For pol_mode = 0 the
    I_{m-1} value now comes from the symmetry I_{-n} = I_n instead of an
    out-of-range GSL call.
  • src/math_c_api.f90, src/mephit_math.h (new): bind(C) shims
    mephit_integrate_gk (libneo Gauss-Kronrod 21, QUADPACK QAG semantics) and
    mephit_gauss_legendre (libneo Golub-Welsch nodes/weights). No module
    state; the C callback and its context pointer pass through a wrapper local
    to each call, so the shims stay thread-safe.
  • src/hyper1F1.c: gsl_integration_qag calls go through
    mephit_integrate_gk; gsl_sum_levin_u is replaced by an in-tree
    levin_u_sum, a 45-line Levin u-transformation after Weniger, Comput.
    Phys. Rep. 10 (1989) 189, eqs. (7.2-8) and (7.3-5).
  • src/mephit_fem.c: gsl_integration_glfixed is replaced by
    mephit_gauss_legendre.
  • src/mephit_util.c, src/mephit_util.h, src/mephit_run.c:
    gsl_errno_msg and the GSL error handler installation are removed;
    quadrature errors surface as QUADPACK return codes.
  • CMakeLists.txt: find_package(GSL REQUIRED) and GSL::gsl removed,
    LIBNEO::math linked, src/math_c_api.f90 added. FFTW is now located
    directly; the libneo math-kit branch no longer leaks FFTW variables into
    the parent scope.
  • cmake/SetupCODE.cmake: fgsl-1.6.0 pickup removed.

Depends on itpplasma/libneo#290 for the LIBNEO::math target; draft until
that merges into libneo main.

Verification

Built on macOS (Homebrew gfortran 15, Accelerate LAPACK) against the libneo
math-kit branch.

Before, GSL was a hard requirement and libmephit linked it:

$ git grep -n "GSL" main -- CMakeLists.txt
main:CMakeLists.txt:62:find_package(GSL REQUIRED)
main:CMakeLists.txt:178:  GSL::gsl

After, the full build succeeds without GSL and no binary links it:

$ cmake --build build -j8 2>&1 | tail -4
[183/186] Linking CXX shared library lib/libmephit.dylib
[184/186] Linking C executable bin/mephit_run.x
[185/186] Linking Fortran executable bin/mephit_test.x
[186/186] Linking Fortran executable bin/mephit_post.x
$ otool -L build/lib/libmephit.dylib | grep -i gsl; echo "exit: $?"
exit: 1

The repo has no ctest suite covering the touched paths, so a standalone
harness compares old GSL-backed code against the new implementations. The
old hyper1F1.c (from main, linked against Homebrew GSL 2.8) is compiled
with prefixed symbols next to the new libmephit; GSL is the oracle for
Gauss-Legendre nodes/weights and the Bessel array, matching the libneo
oracle-test pattern (GSL linked only in the test harness, never in shipped
targets). Representative arguments: 25 complex (b, z) pairs for the
hypergeometric routines, quadrature orders 1 to 12, Bessel orders 1 to 16
at six arguments between 1e-3 and 20.

$ ./check_c | tail -2
max rel diff: quad=1.119e-14 accel=1.278e-15 glfixed=4.371e-15
PASS
$ ./check_bessel
bessel In array max rel diff =  8.350E-16
PASS

All differences are at least two orders below the 1e-12 target. The cases
where hypergeometric1f1_quad diverges (integrand singular for the given
b) produce identical signed infinities before and after.

Not verified here: a full MEPHIT run (needs FreeFem++ and run data, not
available on this machine) and the Python tests under test/ (need a
completed run).

Replace GSL calls with libneo math kit routines and in-tree code:
- mephit_pert.f90: gsl_sf_bessel_In_array -> neo_bessel_i::bessel_in_array
- hyper1F1.c: gsl_integration_qag -> mephit_integrate_gk (libneo
  Gauss-Kronrod 21), gsl_sum_levin_u -> in-tree levin_u_sum
  (Weniger 1989)
- mephit_fem.c: gsl_integration_glfixed -> mephit_gauss_legendre
  (libneo Golub-Welsch)
- mephit_util.c, mephit_run.c: drop gsl_errno_msg and the GSL error
  handler; quadrature errors surface as QUADPACK return codes
- math_c_api.f90, mephit_math.h: bind(C) shims exporting the libneo
  quadrature to the C sources
- CMakeLists.txt: drop find_package(GSL), link LIBNEO::math, locate
  FFTW directly instead of relying on libneo's parent-scope leakage
- SetupCODE.cmake: drop fgsl-1.6.0 pickup

Requires libneo with the math kit (itpplasma/libneo#290).
@krystophny

Copy link
Copy Markdown
Member Author

Superseded by #28, which migrates MEPHIT onto fortnum (the single numerical core) instead of the libneo math-kit. #28 has a verified-green full build and numerical-equivalence checks for the Bessel, 1F1 quadrature, Levin acceleration, and Gauss-Legendre paths. Leaving this open for a human to close.

@krystophny

Copy link
Copy Markdown
Member Author

Superseded by #28, which drops GSL via fortnum directly (Fortran primals + iso_c ABI) instead of libneo math-kit. #28 builds green (91/91), no gsl symbols in libmephit.so. Closing in favor of #28.

@krystophny krystophny closed this Jun 14, 2026
@krystophny
krystophny deleted the drop-gsl branch August 20, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracker: migrate avoidable numerics to fortnum (drop GSL/FFTW copies)

1 participant