diff --git a/docs/src/basics/solve.md b/docs/src/basics/solve.md index f9b22dcc..7482ede1 100644 --- a/docs/src/basics/solve.md +++ b/docs/src/basics/solve.md @@ -8,6 +8,25 @@ solve(::IntegralProblem, ::SciMLBase.AbstractIntegralAlgorithm) solve(::SampledIntegralProblem, ::SciMLBase.AbstractIntegralAlgorithm) ``` +## Default tolerances + +`QuadGKJL`, `HCubatureJL`, `CubatureJLh`, `CubatureJLp`, and +`HAdaptiveIntegrationJL` default to `abstol = 0` and `reltol = 1e-8`. With +`abstol = 0` the solvers control the integral residual through the relative +tolerance alone, so a small integral is not treated as converged just because its +residual is below a fixed absolute floor. + +For integrands dominated by cancellation — large positive and negative +contributions that nearly cancel, such as the zero integral of `sin(2πx)` on +`[0, 1]` — a zero `abstol` can drive the solver to refine until it exhausts +`maxiters` without satisfying the relative bound. In that case pass a positive, +problem-scaled `abstol`, for example proportional to the magnitude of the +integrand times the domain volume: + +```julia +sol = solve(prob, QuadGKJL(); abstol = 1e-8) +``` + ## Related SciML interface utilities `init`, `solve!`, `isinplace`, `remake`, and `ReturnCode` are SciMLBase interface diff --git a/src/common.jl b/src/common.jl index 2c289e8f..d43b0044 100644 --- a/src/common.jl +++ b/src/common.jl @@ -194,7 +194,15 @@ The arguments to `solve` are common across all of the quadrature methods. These common arguments are: - `maxiters`: Maximum number of algorithm iterations or backend evaluations. - - `abstol`: Absolute tolerance for the integral residual/error estimate. + - `abstol`: Absolute tolerance for the integral residual/error estimate. The default + is `0` for `QuadGKJL()`, `HCubatureJL()`, `CubatureJLh()`, `CubatureJLp()`, and + `HAdaptiveIntegrationJL()`, so small integrals are not swamped by a nonzero floor + and the residual is controlled by `reltol` alone. For integrands dominated by + cancellation (large positive and negative contributions that nearly cancel), a + zero `abstol` can force the solver to refine until an unachievable relative + bound is met and the effort budget is exhausted; pass a positive, problem-scaled + `abstol` (for example proportional to the magnitude of the integrand and the + domain volume) in that case. - `reltol`: Relative tolerance for the integral residual/error estimate. - `verbose`: Verbosity control via [`IntegralVerbosity`](@ref). Defaults to `IntegralVerbosity()`. diff --git a/test/interface_tests.jl b/test/interface_tests.jl index d56d9111..e0707e39 100644 --- a/test/interface_tests.jl +++ b/test/interface_tests.jl @@ -1,5 +1,5 @@ using Integrals -using Cuba, Cubature, Arblib, FastGaussQuadrature, FastTanhSinhQuadrature, MCIntegration +using Cuba, Cubature, Arblib, FastGaussQuadrature, FastTanhSinhQuadrature, MCIntegration, HAdaptiveIntegration using Test max_dim_test = 2 @@ -552,7 +552,7 @@ end # exact value of the integral over (0, 2en) exact = (-151 + 101 * sqrt(101)) / 7.5e11 - for alg in (QuadGKJL(), HCubatureJL(), CubatureJLh()) + for alg in (QuadGKJL(), HCubatureJL(), CubatureJLh(), HAdaptiveIntegrationJL()) scaled = solve(IntegralProblem((x, p) -> g(x), (0.0, 2en)), alg).u @test scaled ≈ exact rtol = 1.0e-6 # scaling the integrand has to scale the integral @@ -562,3 +562,20 @@ end end end end + +# CubatureJLp is not in the loop above: its p-adaptive rules return 0.0 on the +# narrow-support spike there at any budget, while on smooth integrands the +# default abstol = 0 keeps this small analytic integral resolved. +@testset "smooth small integral with default abstol" begin + f = (x, p) -> 1.0e-9 * exp(x) + exact = 1.0e-9 * (exp(1.0) - 1.0) + for alg in (CubatureJLp(), HAdaptiveIntegrationJL()) + scaled = solve(IntegralProblem(f, (0.0, 1.0)), alg).u + @test scaled ≈ exact rtol = 1.0e-6 + # scaling the integrand has to scale the integral + for n in (100.0, 1.0e6) + full = solve(IntegralProblem((x, p) -> n * f(x, p), (0.0, 1.0)), alg).u + @test n * scaled ≈ full rtol = 1.0e-6 + end + end +end