Skip to content

PartialACA silently loses accuracy or throws BoundsError on kernels with exactly vanishing sub-blocks (double-layer kernel on flat faces) #94

Open

Description

@caperezar

Summary

PartialACA can silently lose accuracy, or throw a BoundsError, on kernels that vanish identically on parts of an admissible block. A common case is the double-layer kernel ∂G(x,y)/∂ν(y) on polyhedral or partly flat surfaces: it is exactly zero whenever x and y lie on the same flat face. The reproducer below uses only HMatrices.jl, with points on the six faces of a cube and the Laplace double-layer kernel.

Reproducer

using HMatrices, LinearAlgebra, StaticArrays, Random

const Pt = SVector{3,Float64}

# Points (and unit normals) on a uniform grid of each face of the cube [-1,1]^3.
function cube_points(n)
    X, N = Pt[], Pt[]
    g = range(-1, 1; length = n + 2)[2:end-1]
    for axis in 1:3, s in (-1.0, 1.0), a in g, b in g
        p, q = zeros(3), zeros(3)
        p[axis] = q[axis] = s
        p[mod1(axis + 1, 3)] = a
        p[mod1(axis + 2, 3)] = b
        push!(X, Pt(p)); push!(N, Pt(q))
    end
    return X, N
end

# Laplace double-layer kernel ∂G(x,y)/∂ν(y); it vanishes exactly when x and y
# lie on the same face (x - y ⟂ ν(y)).
struct DoubleLayer <: AbstractMatrix{Float64}
    X::Vector{Pt}
    N::Vector{Pt}
end
Base.size(K::DoubleLayer) = (length(K.X), length(K.X))
function Base.getindex(K::DoubleLayer, i::Int, j::Int)
    d = K.X[i] - K.X[j]
    r = norm(d)
    return r == 0 ? 0.0 : dot(d, K.N[j]) / (4π * r^3)
end

function check(n, nmax, rtol)
    Random.seed!(1)
    X, N = cube_points(n)
    K = DoubleLayer(X, N)
    tree = ClusterTree(X, HMatrices.CardinalitySplitter(; nmax))
    x = randn(length(X))
    y = Matrix(K) * x
    try
        H = assemble_hmatrix(K, tree, tree; comp = PartialACA(; rtol), threads = false)
        println("n=$n, nmax=$nmax, rtol=$rtol: relative error ", norm(H * x - y) / norm(y))
    catch e
        println("n=$n, nmax=$nmax, rtol=$rtol: ", sprint(showerror, e; context = :limit => true)[1:min(end, 80)])
    end
end

check(30, 32, 1e-6)   # silent loss of accuracy
check(30, 32, 1e-10)  # same points, tighter rtol: BoundsError
check(20, 16, 1e-8)   # BoundsError: index -1 into a BitVector

Output with HMatrices v0.2.13 on Julia v1.12.5 (macOS, aarch64):

n=30, nmax=32, rtol=1.0e-6: relative error 0.025752978839732852
n=30, nmax=32, rtol=1.0e-10: BoundsError: attempt to access 40-element BitVector at index [-1]
n=20, nmax=16, rtol=1.0e-8: BoundsError: attempt to access 21-element BitVector at index [-1]

The first case also prints aca possibly failed on ... for some blocks. Tightening rtol does not recover the accuracy. Smooth geometries (e.g. a sphere) do not show the problem, and the same blocks compress correctly with a dense TSVD. The BoundsError stack trace points to _aca_partial in src/compressor.jl:

BoundsError: attempt to access 21-element BitVector at index [-1]
 [1] throw_boundserror
 [2] checkbounds
 [3] setindex!            # I[i] = false with i == -1
 [4] _aca_partial(K, irange, jrange, atol, rmax, rtol, istart, buffer_)
 [5] PartialACA
 [6] _assemble_sparse_block!
 ...

What seems to happen

In _aca_partial (src/compressor.jl):

  1. Zero rows consume candidates without increasing the rank. When the residual row b is zero, the zero-pivot branch removes that row from I and moves to findfirst(I), without adding a cross. On blocks with many exactly zero rows, the candidate rows can run out while r < rmax and while the stopping test has not been met.
  2. The pivot search can return -1. After a successful cross, the next row is i = _aca_partial_pivot(a, I). This returns -1 when no index is both unused and selectable, for instance when all rows of I were consumed as in (1). The next iteration then executes I[i] = false with i == -1, which gives the BoundsError.
  3. Early stopping on block-structured residuals. Because the next row is chosen from the current column, rows that are zero in that column are never visited, even if their residual is large in other columns. The stopping test norm(a)*norm(b) ≤ rtol*‖R‖ then accepts an approximation that misses whole sub-blocks. This is the classical failure of partial-pivoting ACA on matrices with zero sub-blocks, and it explains the silent loss of accuracy.

Possible fix

What fixed it for us was a small change to the same algorithm:

  • treat a pivot of -1 (or an exhausted I) as "no candidate left" and stop, instead of indexing;
  • before accepting the stopping criterion, check the residual on a few randomly sampled unvisited rows and columns, and continue from a failing sample if any is not small (as in ACA+-type safeguards).

With these changes, the compressed double-layer operators match the dense ones to better than 1e-7 (relative) at rtol = 1e-9 on flat-faced meshes, with negligible extra cost. Happy to share the implementation or open a PR if that would help.

Context

We hit this while compressing the Helmholtz double-layer and adjoint double-layer operators of a Galerkin BEM on polyhedral surfaces (cubes, L-shaped prisms, and rounded cubes with flat faces). There, ACA quietly introduced an error of about 1% in the double-layer operators.

Activity

  1. maltezfaria commented on Nov 2, 2026

    @maltezfaria
    Member

    Thanks for the detailed issue! There is a recent branch, compressor, which possibly already contains a fix. I will merge that branch this week and see if it fixes the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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