Skip to content

Commit 7000381

Browse files
add B as kwargs
1 parent 9ea6190 commit 7000381

File tree

5 files changed

+12
-57
lines changed

5 files changed

+12
-57
lines changed

src/aggregation.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
function smoothed_aggregation(A::TA, _B = nothing,
1+
function smoothed_aggregation(A::TA,
22
::Type{Val{bs}}=Val{1};
3+
B = nothing,
34
symmetry = HermitianSymmetry(),
45
strength = SymmetricStrength(),
56
aggregate = StandardAggregation(),
@@ -12,9 +13,8 @@ function smoothed_aggregation(A::TA, _B = nothing,
1213
diagonal_dominance = false,
1314
keep = false,
1415
coarse_solver = Pinv, kwargs...) where {T,V,bs,TA<:SparseMatrixCSC{T,V}}
15-
1616
n = size(A, 1)
17-
B = isnothing(_B) ? ones(T,n) : copy(_B)
17+
B = isnothing(B) ? ones(T,n) : copy(B)
1818
@assert size(A, 1) == size(B, 1)
1919

2020
#=max_levels, max_coarse, strength =

src/multilevel.jl

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -287,47 +287,7 @@ end
287287
abstract type AMGAlg end
288288

289289
struct RugeStubenAMG <: AMGAlg end
290-
291-
"""
292-
SmoothedAggregationAMG(B=nothing)
293-
294-
Smoothed Aggregation AMG (Algebraic Multigrid) algorithm configuration.
295-
296-
# Arguments
297-
- `B::Union{AbstractArray, Nothing}`: The **near null space** in SA-AMG represents the low energy that cannot be attenuated by relaxtion, and thus needs to be perserved across the coarse grid.
298-
299-
- `B` can be:
300-
- a `Vector` (e.g., for scalar PDEs),
301-
- a `Matrix` (e.g., for vector PDEs or systems with multiple equations),
302-
- or `nothing`.
303-
304-
# Notes
305-
If `B` is set to `nothing`, it will be internally defaulted to `B = ones(T, n)`, where `T = eltype(A)` and `n = size(A, 1)`.
306-
307-
# Examples
308-
309-
**Poisson equation (scalar PDE):**
310-
```julia
311-
n = size(A, 1)
312-
B = ones(Float64, n)
313-
amg = SmoothedAggregationAMG(B)
314-
```
315-
316-
**Linear elasticity equation in 2d (vector PDE):**
317-
```julia
318-
n = size(A, 1) # Ndof = 2 * number of nodes
319-
B = zeros(Float64, n, 3)
320-
B[1:2:end, :] = [1.0, 0.0, -y]
321-
B[2:2:end, :] = [0.0, 1.0, x]
322-
amg = SmoothedAggregationAMG(B)
323-
```
324-
"""
325-
struct SmoothedAggregationAMG <: AMGAlg
326-
B::Union{<:AbstractArray,Nothing} # `B` can be `Vector`, `Matrix`, or `nothing`
327-
function SmoothedAggregationAMG(B::Union{AbstractArray,Nothing}=nothing)
328-
new(B)
329-
end
330-
end
290+
struct SmoothedAggregationAMG <: AMGAlg end
331291

332292
function solve(A::AbstractMatrix, b::Vector, s::AMGAlg, args...; kwargs...)
333293
solt = init(s, A, b, args...; kwargs...)
@@ -337,7 +297,7 @@ function init(::RugeStubenAMG, A, b, args...; kwargs...)
337297
AMGSolver(ruge_stuben(A; kwargs...), b)
338298
end
339299
function init(sa::SmoothedAggregationAMG, A, b; kwargs...)
340-
AMGSolver(smoothed_aggregation(A,sa.B; kwargs...), b)
300+
AMGSolver(smoothed_aggregation(A; kwargs...), b)
341301
end
342302
function solve!(solt::AMGSolver, args...; kwargs...)
343303
_solve(solt.ml, solt.b, args...; kwargs...)

src/precs.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ function SmoothedAggregationPreconBuilder(; blocksize = 1, kwargs...)
1414
end
1515

1616
function (b::SmoothedAggregationPreconBuilder)(A::AbstractSparseMatrixCSC, p)
17-
B = get(b.kwargs, :B, nothing) # extract nns from kwargs, default to `nothing`
18-
return (aspreconditioner(smoothed_aggregation(SparseMatrixCSC(A), B, Val{b.blocksize}; b.kwargs...)),I)
17+
return (aspreconditioner(smoothed_aggregation(SparseMatrixCSC(A), Val{b.blocksize}; b.kwargs...)), I)
1918
end
2019

2120

test/nns_test.jl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
#2. pass `B` as vector
1212
B = ones(T,n)
13-
x_vec = solve(A, b, SmoothedAggregationAMG(B), maxiter = 1, abstol = 1e-6)
13+
x_vec = solve(A, b, SmoothedAggregationAMG(), maxiter = 1, abstol = 1e-6;B=B)
1414
@test x_vec x_nothing
1515

1616
#3. pass `B` as matrix
1717
B = ones(T,n,1)
18-
x_mat = solve(A, b, SmoothedAggregationAMG(B), maxiter = 1, abstol = 1e-6)
18+
x_mat = solve(A, b, SmoothedAggregationAMG(), maxiter = 1, abstol = 1e-6;B=B)
1919
@test x_mat x_nothing
2020
end
2121

@@ -194,11 +194,8 @@ end
194194
@load "lin_elastic_2d.jld2" A b B
195195
A = SparseMatrixCSC(A.m, A.n, A.colptr, A.rowval, A.nzval)
196196

197-
x_nns, residuals_nns = solve(A, b, SmoothedAggregationAMG(B); log=true, reltol=1e-10)
198-
x_wonns, residuals_wonns = solve(A, b, SmoothedAggregationAMG(); log=true, reltol=1e-10)
199-
200-
ml = smoothed_aggregation(A, B)
201-
@show ml
197+
x_nns, residuals_nns = solve(A, b, SmoothedAggregationAMG(), log=true, reltol=1e-10;B=B)
198+
x_wonns, residuals_wonns = solve(A, b, SmoothedAggregationAMG(), log=true, reltol=1e-10)
202199

203200
println("No NNS: final residual at iteration ", length(residuals_wonns), ": ", residuals_wonns[end])
204201
println("With NNS: final residual at iteration ", length(residuals_nns), ": ", residuals_nns[end])
@@ -233,8 +230,8 @@ end
233230
@test u[end-1] (P * L^3) / (3 * E * I) # vertical disp. at the end of the beam
234231

235232

236-
x_nns, residuals_nns = solve(A, b, SmoothedAggregationAMG(B); log=true, reltol=1e-10)
237-
x_wonns, residuals_wonns = solve(A, b, SmoothedAggregationAMG(); log=true, reltol=1e-10)
233+
x_nns, residuals_nns = solve(A, b, SmoothedAggregationAMG(), log=true, reltol=1e-10,B=B)
234+
x_wonns, residuals_wonns = solve(A, b, SmoothedAggregationAMG(), log=true, reltol=1e-10)
238235

239236
println("No NNS: final residual at iteration ", length(residuals_wonns), ": ", residuals_wonns[end])
240237
println("With NNS: final residual at iteration ", length(residuals_nns), ": ", residuals_nns[end])

test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ for sz in [ (10,10), (20,20), (50,50)]
348348
strategy = KrylovJL_CG(precs = RugeStubenPreconBuilder())
349349
@test solve(prob, strategy, atol=1.0e-14) u0 rtol = 1.0e-8
350350

351-
352351
strategy = KrylovJL_CG(precs = SmoothedAggregationPreconBuilder())
353352
@test solve(prob, strategy, atol=1.0e-14) u0 rtol = 1.0e-8
354353

0 commit comments

Comments
 (0)