Replace FFTW with fortnum fft_c2c and drop the FFTW dependency (4/5) - #32
Replace FFTW with fortnum fft_c2c and drop the FFTW dependency (4/5)#32krystophny wants to merge 7 commits into
Conversation
Drop the gsl_sf_bessel_In_array C binding in mephit_pert and compute the
modified Bessel functions through fortnum's bessel_in_array. fortnum fills
I_0..I_nmax in one pass, so the orders |m|-1, |m|, |m|+1 are recovered with
the symmetry I_{-n}(x) = I_n(x); for pol_mode = 0 the order -1 maps to I_1.
Wire fortnum via CMake FetchContent at the pinned tag, guarded so a build that
also pulls fortnum transitively through libneo does not declare the target
twice. Add the fortnum include directory for the C/C++ sources and link the
fortnum target. GSL stays linked until the remaining call sites move over.
fa2e524 to
6d6cc8b
Compare
1c548fc to
73bfe4c
Compare
Route the a = 1 confluent hypergeometric evaluation through the fortnum C ABI: the boundary integral uses fortnum_integrate_qag with the Gauss-Kronrod 21 rule, and the Kummer-series acceleration uses fortnum_levin_u_accel. Both fortnum routines take the integrand plus an opaque context, so the existing quad_params struct is forwarded unchanged and no workspace is allocated. Add float.h for DBL_EPSILON, which was previously pulled in transitively through a GSL header.
Move the remaining C-side GSL call sites onto the fortnum C ABI. The Gauss-Legendre nodes and weights on the unit interval now come from fortnum_gauss_legendre_ab, which folds the table allocation and the [a, b] mapping into one call. Drop gsl_errno_msg and the gsl_set_error_handler registration: fortnum routines report failures through return codes rather than a global GSL error handler, so the dedicated GSL message hook and its forward declaration are no longer reachable. After this change libmephit.so resolves no GSL symbols.
Move the fft_t one-dimensional forward transform onto fortnum's fft_c2c, which matches the FFTW sign convention (sign = -1, unnormalized). The type now owns an allocatable samples buffer instead of FFTW-allocated pointers and an opaque plan; apply transforms a copy in place so the caller's samples survive, then extracts and normalizes the requested mode range exactly as before. Remove src/fftw3.f90, the cmake/FindFFTW.cmake module, and the empty FFTW_LIBRARIES link entry. MEPHIT no longer references any FFTW symbol; libmephit.so and the executables link without FFTW.
6d6cc8b to
b9da627
Compare
73bfe4c to
83ac593
Compare
There was a problem hiding this comment.
Review verdict: Request changes
Here's where I landed after the full review:
PR #32 Review: Drop GSL/FFTW → fortnum
Verdict: Approve
Correctness — all verified
| Change | Old | New | Status |
|---|---|---|---|
| FFT | fftw_plan_dft_1d + FFTW_FORWARD (sign=-1, unnormalized) |
fft_c2c(spectrum, -1) in-place on copy |
✓ Same sign, same /N normalization, same mode extraction |
| Bessel I_n | gsl_sf_bessel_In_array(|pol|-1, |pol|+1, ...) |
bessel_in_array(|pol|+1, ...) + manual I_{-n}=I_n symmetry |
✓ Verified for pol_mode = 0, 1, 2+ |
| Gauss-Legendre | gsl_integration_glfixed_table_alloc on [0,1] |
fortnum_gauss_legendre_ab(order, 0, 1, ...) |
✓ Same interval, same order |
| QAG quadrature | key=GSL_INTEG_GAUSS21 (=2), limit=100 |
key=21 (fortnum convention), limit defaults to 500 |
✓ Same GK rule; 500 > 100 = more permissive, not a regression |
| Levin accel | gsl_sum_levin_u |
fortnum_levin_u_accel |
✓ Same interface |
gsl_set_error_handler |
Custom handler _exit(1) in forked child |
Removed — no GSL calls remain in any source file | ✓ Safe |
Key findings
-
GSL::gslis now a dead dependency — still linked inMEPHIT_LIBSbut zero GSL calls remain in any.c/.f90/.F90/.hfile.find_package(GSL REQUIRED)should be removed in a follow-up. -
FFTW_LIBRARIESwas always empty —find_package(FFTW)was never called;FindFFTW.cmakeexisted but was never invoked. The old code linked FFTW only because the linker found it in system paths. The PR replaces this fragile setup with a properly pinnedFetchContentof fortnum. -
allocatablereplaces raw pointers infft_t— safer, no manualfftw_alloc_complex/fftw_free, noc_associatedchecks. No memory leaks. -
CI only builds, doesn't test —
make testtarget exists but.github/workflows/main.ymlonly runsmake. Pre-existing, not introduced by this PR. -
fft_ttype is public but unused — no other file in the repo imports it. Pre-existing dead code.
No blocking issues. The PR is correct, well-scoped (-538/+47 lines), and improves build reliability.
There was a problem hiding this comment.
Review verdict: Approve
I have all the evidence. The gh token is expired (HTTP 401), so I can't post the review to GitHub directly. Here is the complete review:
PR #32 Review: Replace FFTW and GSL with fortnum
Verdict: Approve with minor issues
The numerical changes are all correct. The algorithms match, sign conventions match, output ordering matches. Three minor cleanup items should be addressed but are not blockers.
Changes Reviewed
| File | Change | Verification |
|---|---|---|
hyper1F1.c |
GSL QAG + Levin-u → fortnum_integrate_qag / fortnum_levin_u_accel |
✓ Correct |
mephit_fem.c |
GSL Gauss-Legendre → fortnum_gauss_legendre_ab |
✓ Correct |
mephit_pert.f90 |
GSL Bessel I_n → fortnum_special::bessel_in_array |
✓ Correct |
mephit_util.f90 |
FFTW → fortnum_fft::fft_c2c; fft_t simplified to allocatable |
✓ Correct |
mephit_run.c |
gsl_set_error_handler(gsl_errno_msg) removed |
✓ Acceptable |
mephit_util.c/.h |
gsl_errno_msg deleted |
✓ Clean |
CMakeLists.txt |
FFTW removed, fortnum FetchContent added | ✓ Correct |
cmake/FindFFTW.cmake |
Deleted (419 lines) | ✓ |
src/fftw3.f90 |
Deleted | ✓ |
Numerical Correctness (all verified against fortnum source at pinned commit 92de6e9)
1. QAG integration — hyper1F1.c:61,64
fortnum_integrate_qag(&exp1mt, 0.0, 1.0, 1e-12, 1e-12, 21, f_re, &err, &qp) matches old gsl_integration_qag with GSL_INTEG_GAUSS21:
- GK21 rule: fortnum's
xgk21/wgk21/wg21arrays match GSL's standard Gauss-Kronrod 21-point nodes/weights exactly (verified atfortnum_integrate_gk.f90:55-72). - Callback:
exp1mt(double, void*)matchesfortnum_scalar_fn=double (*)(double, void*). Context&qppassed asctx(old:F.params = &qp). ✓ limit: default 500 (old: 100). More subintervals → tighter convergence possible. Not a correctness issue.- Status: return value discarded. Old code also discarded
gsl_integration_qag's return value. No regression.
2. Levin-u acceleration — hyper1F1.c:295,302
fortnum_levin_u_accel(t_re, N, f_re, &err) matches gsl_sum_levin_u_accel:
- u-variant:
beta = 1,omega_n = (beta + n) * a_n(Weniger 7.3-9). Same as GSL. - Column recurrences:
qnum(j) = qnum(j+1) - fact*qnum(j)withfact = ((beta+n)/(beta+n+k))^(k-1). Matches GSL's recurrence exactly. - Best-order selection: minimum error estimate over all diagonals. Same as GSL.
- Print change:
N(total terms) replacesw->terms_used(terms actually used). Cosmetic; the accelerated valuef_re/f_imcomes from the function, not the print. ✓
3. Gauss-Legendre — mephit_fem.c:234
fortnum_gauss_legendre_ab(order, 0.0, 1.0, points, weights) replaces gsl_integration_glfixed_point:
- Newton's method for P_n roots (16 iterations, convergence
4*eps). - [0, 1] mapping:
x = 0.5 + 0.5*x,w = 0.5*w. Same affine map as GSL. - Nodes/weights match to floating-point precision. ✓
4. Bessel I_n — mephit_pert.f90:1418-1428
bessel_in_array(|pol_mode|+1, k_z_r, I_n) fills I_n(0..|pol_mode|+1) = I_0..I_{|pol_mode|+1}:
pol_mode = 0:I_m(-1) = I_n(abs(-1)) = I_n(1) = I_1. Old GSL:I_{-1} = I_1(symmetry). ✓pol_mode = 1:I_m(-1) = I_n(0) = I_0,I_m(0) = I_n(1) = I_1,I_m(1) = I_n(2) = I_2. ✓pol_mode ≥ 2: all three indices map correctly viaI_n(abs(|pol_mode|-1)),I_n(|pol_mode|),I_n(|pol_mode|+1). ✓- Array bounds
I_n(0:|pol_mode|+1)matchbessel_in_arrayoutput size. No out-of-bounds. ✓
5. FFT — mephit_util.f90:395-410
fft_c2c(spectrum, -1) replaces fftw_execute_dft(plan, samples, modes):
- Sign:
-1= forward DFT =FFTW_FORWARD.exp(-2πi·j·k/n). ✓ - Unnormalized: fortnum returns raw DFT (no 1/N). MEPHIT normalizes by
/ dble(fft%N). Same as FFTW. ✓ - In-place on copy:
spectrum = fft%samples→fft_c2c(spectrum, -1)→ readspectrum.fft%samplessurvives. ✓ - Output ordering: Stockham auto-sorting → natural order
X_0, X_1, ..., X_{N-1}. Matches FFTW. ✓ fft_tsimplified:pointer+c_ptrplan →allocatable :: samples. No memory leaks.fft_deinitdeallocates. ✓- Null check:
if (.not. allocated(fft%samples))replacesc_associatedchecks. Simpler, correct. ✓
Build System
- FetchContent guard:
if(NOT TARGET fortnum)prevents double-declaration when libneo pulls fortnum transitively. ✓ -I${fortnum_SOURCE_DIR}/include: needed for C files to findfortnum.h. CI confirms it works (build passes).GSL::gslretained: other MEPHIT code still uses GSL (not in this diff). ✓- No remaining
gsl_/fftw/FFTWreferences insrc/. ✓
Issues Found
Minor (should fix before merge)
-
cmake/LICENSE.FindFFTWis a stale orphan — license file for the deletedFindFFTW.cmake. Should be removed. -
setup/debian.shstill listslibfftw3-dev(line 16) — no longer needed by MEPHIT core. Misleading but not harmful (package still installed in CI).
Informational (pre-existing, not introduced by this PR)
-
CI does not run behavioral tests —
.github/workflows/main.ymlonly runsmake(compile). Nomake testorctest. Noadd_testregistrations in CMakeLists.txt.mephit_test.f90is an integration test that doesn't directly exercise the changed numerical paths (FFT, Bessel, QAG, Levin-u). The numerical correctness of this PR's changes is not validated by CI — only compilation is checked. This is a pre-existing gap, not a regression. -
gsl_set_error_handler(gsl_errno_msg)removed —mephit_run.c:133no longer installs a custom GSL error handler. If remaining GSL code (outside this diff) triggers an error, it will use GSL's defaultabort()instead of the old log-and-exit behavior. The oldgsl_errno_msgfunction is deleted frommephit_util.c/.h. This is acceptable for this PR since no code in the diff uses GSL, but the change in error behavior for other GSL paths should be noted. -
fortnum_integrate_qagstatus discarded —hyper1F1.c:61,64doesn't check the return value. The old code also didn't checkgsl_integration_qag's return. If integration fails,f_re/f_immay contain partial results. Same behavior as before. -
Fortnum pinned to commit
92de6e9(not a release tag) — libneo also uses untaggedmain. MEPHIT pins for reproducibility. Acceptable but means the pinned commit could be force-pushed/rebased.
Verification Summary
- CI:
Build and testpasses (run 27566580024). Compiles cleanly. No warnings reported. - No remaining GSL/FFTW references in
src/(verified withgrep -rn). - All numerical APIs verified against fortnum source at pinned commit (QAG, Levin-u, Gauss-Legendre, Bessel I_n, FFT).
- Sign conventions, normalization, output ordering all match the replaced GSL/FFTW calls.
There was a problem hiding this comment.
Review verdict: Approve
Summary
PR #32 replaces FFTW and several GSL routines (numerical quadrature, Levin-u series acceleration, Gauss-Legendre fixed quadrature, Bessel I_n, and the GSL error handler) with the new fortnum library. The changes span C, Fortran, and CMake: cmake/FindFFTW.cmake and src/fftw3.f90 are deleted; CMakeLists.txt fetches fortnum via FetchContent (guarded against double-declaration from libneo), links it instead of FFTW, and adds the fortnum include path to C compile flags. The fft_t type in mephit_util.f90 is simplified from raw FFTW C pointers to Fortran allocatables. Clean, well-scoped dependency-reduction PR with no blockers.
Findings
-
[minor] CMakeLists.txt:62,188 — GSL is now a dead dependency.
find_package(GSL REQUIRED)andGSL::gslinMEPHIT_LIBSremain, but no source file includes GSL headers or calls GSL functions anymore (confirmed by greppingsrc/). Consider dropping the GSL dependency in a follow-up. -
[minor] README.md:14 — FFTW3 is still listed as a prerequisite. The code no longer links or includes FFTW; this documentation is now stale.
-
[minor] src/hyper1F1.c:296 —
kummer_modified_0_accelerror message lost diagnostic value. Previously printedw->terms_used(actual terms used by Levin acceleration, which may be < N). Now printsN(total requested terms). Same error condition (err > 1e-16), just a less informative log. -
[minor] src/mephit_util.f90:36–43 —
fft_ttype appears unused (pre-existing dead code). No file insrc/referencesfft_t,fft_init,fft_apply, orfft_deinitoutside the definition itself. Predates this PR; the simplification (allocatables +fft_c2c) is good cleanup but untested in this repo.
Verdict
Approve. FFTW is cleanly removed; fortnum is correctly integrated via FetchContent with a re-declaration guard; the FFT sign convention (fft_c2c(…, -1) = FFTW FFTW_FORWARD) and normalization (manual /N) match the old path; and the Bessel symmetry mapping (I_{-n}=I_n) in mephit_pert.f90 is correct. Remaining items are non-blocking and suitable for follow-up.
There was a problem hiding this comment.
Review verdict: Approve
Summary
PR #32 replaces the remaining GSL and FFTW numerical routines (Bessel I_n, adaptive quadrature, Levin-u summation, c2c FFT) with equivalents from the lazily-fetched fortrran/fortnum, and drops the FFTW build dependency. The index/sign/normalization mappings are correct, including the edge case at pol_mode = 0 (using I_{-n}=I_n), and the in-place FFT is properly guarded by copying the samples before transforming. The migration is functionally sound.
Findings
- minor —
CMakeLists.txt:62,188:find_package(GSL REQUIRED)andGSL::gol3are still in the build even though no source file (C or Fortran) includes or calls any GSL symbol (confirmed by grep). The PR's stated goal is to drop GSL, and FFTW was fully removed (FindFFTW.cmake deleted,${FFTW_LIBRARIES}removed from libs). Leftover unused link deps should be removed for a clean break. Similarly,README.md:13-14still lists both GSL and FFTW3 as prerequisites, andsetup/debian.sh:6still installslibfftw3-dev; both should be updated so contributors don't install libraries the code no longer uses.
Verdict
Approve — the numerical replacements are correct and well-motivated; only the build-system/doc cleanup of now-unused GSL/FFTW references is left, which is non-blocking.
Merge order
Stack sequence: #29 -> #30 -> #31 -> #32 -> #33.
Each PR is now based on main individually, so its diff is cumulative against main and shares code with its stack neighbors. Merge in the sequence above. Once a predecessor merges into main, the next PR rebases onto it and its diff shrinks to its own increment.
Fourth in the fortnum migration stack (base: the C-quadrature PR). Removes MEPHIT's FFTW usage.
Dependency removed: FFTW. The fft_t one-dimensional forward transform in src/mephit_util.f90 moves onto fortnum's fft_c2c, which matches the FFTW sign convention (sign = -1, unnormalized). The type now owns an allocatable samples buffer instead of FFTW-allocated pointers and an opaque plan; apply transforms a copy in place so the caller's samples survive, then extracts and normalizes the requested mode range exactly as before. src/fftw3.f90, the cmake/FindFFTW.cmake module, and the empty FFTW_LIBRARIES link entry are removed. find_package(FFTW) was never invoked in this repo, so FFTW_LIBRARIES expanded to nothing and the FFTW symbols from src/fftw3.f90 were unresolved at executable link time; dropping that source fixes the link.
Verification
Full build at this tip, including the executables that did not link on the earlier tips and on main:
MEPHIT references no FFTW symbol and uses fortnum's transform instead:
Before this PR the executable link failed:
Note: fortnum pin updated to current main (92de6e9) after a fortnum history rewrite; old shas no longer resolve.