Fix Radau methods to support KLU with sparse jacobian prototypes #2893
+115
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2892
Summary
This PR fixes an issue where Radau methods (RadauIIA3, RadauIIA5, RadauIIA9, and AdaptiveRadau) would fail when using
KLUFactorizationwith a sparsejac_prototype.Root Cause
The issue was caused by a limitation in LinearSolve's KLU interface: KLU doesn't properly support complex sparse matrices. When initializing a LinearSolve cache with a complex sparse matrix and KLU, the factorization is never created (remains
Nothing), leading to aFieldError: type Nothing has no field colptrwhen trying to solve.Solution
This PR implements a two-part fix:
Proper initialization of complex W matrices: Use
f.jac_prototype .* (1 + 0im)(when available) to create complex W matrices with the correct sparsity pattern from the user-provided prototype.Automatic fallback to UMFPACK: When KLU is specified as the linear solver, automatically use
UMFPACKFactorizationfor the complex W matrices (W2/W3), while still using KLU for the real W1 matrix. UMFPACK fully supports complex sparse matrices.Changes
lib/OrdinaryDiffEqFIRK/src/firk_caches.jl: Updated cache initialization for all four Radau methods to use jac_prototype and fallback to UMFPACK for complex matriceslib/OrdinaryDiffEqFIRK/src/OrdinaryDiffEqFIRK.jl: Added imports forKLUFactorizationandUMFPACKFactorizationTesting
Verified that all Radau methods now work correctly with:
KLUFactorizationwith sparsejac_prototype✓LUFactorization✓SparspakFactorization✓The fix preserves the sparsity pattern correctly and allows users to benefit from KLU's performance for real matrices while seamlessly handling the complex matrix requirements of Radau methods.
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]