Skip to content

Commit b519b1c

Browse files
committed
AbstractVector -> AbstractArray and minor fixes
1 parent 7ed4c68 commit b519b1c

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/multilevel.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ struct V <: Cycle
116116
end
117117

118118
"""
119-
solve(ml::MultiLevel, b::AbstractVector, cycle, kwargs...)
119+
solve(ml::MultiLevel, b::AbstractArray, cycle, kwargs...)
120120
121121
Execute multigrid cycling.
122122
@@ -134,14 +134,14 @@ Keyword Arguments
134134
* log::Bool - return vector of residuals along with solution
135135
136136
"""
137-
function solve(ml::MultiLevel, b::AbstractVector, args...; kwargs...)
137+
function solve(ml::MultiLevel, b::AbstractArray, args...; kwargs...)
138138
n = length(ml) == 1 ? size(ml.final_A, 1) : size(ml.levels[1].A, 1)
139139
V = promote_type(eltype(ml.workspace), eltype(b))
140-
bs = blocksize(ml.workspace)
141-
x = bs === 1 ? zeros(V, n) : zeros(V, n, bs)
140+
ndims(b) == blocksize(ml.workspace) && throw("Dimension of MultiLevel workspace and dimension of vector b must be equal.")
141+
x = zeros(V, size(b))
142142
return solve!(x, ml, b, args...; kwargs...)
143143
end
144-
function solve!(x, ml::MultiLevel, b::AbstractVector{T},
144+
function solve!(x, ml::MultiLevel, b::AbstractArray{T},
145145
cycle::Cycle = V();
146146
maxiter::Int = 100,
147147
tol::Float64 = 1e-5,
@@ -169,7 +169,7 @@ function solve!(x, ml::MultiLevel, b::AbstractVector{T},
169169
end
170170
if calculate_residual
171171
mul!(res, A, x)
172-
res .= b .- res
172+
reshape(res, size(b)) .= b .- reshape(res, size(b))
173173
normres = norm(res)
174174
log && push!(residuals, normres)
175175
end
@@ -186,7 +186,7 @@ function __solve!(x, ml, v::V, b, lvl)
186186

187187
res = ml.workspace.res_vecs[lvl]
188188
mul!(res, A, x)
189-
res .= b .- res
189+
reshape(res, size(b)) .= b .- reshape(res, size(b))
190190

191191
coarse_b = ml.workspace.coarse_bs[lvl]
192192
mul!(coarse_b, ml.levels[lvl].R, res)

src/smoother.jl

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct GaussSeidel{S} <: Smoother
1010
sweep::S
1111
iter::Int
1212
end
13-
GaussSeidel(iter = 1) = GaussSeidel(SymmetricSweep(), iter)
13+
GaussSeidel(; iter = 1) = GaussSeidel(SymmetricSweep(), iter)
1414
GaussSeidel(f::ForwardSweep) = GaussSeidel(f, 1)
1515
GaussSeidel(b::BackwardSweep) = GaussSeidel(b, 1)
1616
GaussSeidel(s::SymmetricSweep) = GaussSeidel(s, 1)
@@ -47,35 +47,38 @@ end
4747
struct Jacobi{T,TX} <: Smoother
4848
ω::T
4949
temp::TX
50+
iter::Int
5051
end
51-
Jacobi(ω, x::TX) where {T, TX<:AbstractVector{T}} = Jacobi{T,TX}(ω, similar(x))
52-
Jacobi(x::TX, ω = 0.5) where {T, TX<:AbstractVector{T}} = Jacobi{T,TX}(ω, similar(x))
52+
Jacobi(ω, x::TX; iter=1) where {T, TX<:AbstractArray{T}} = Jacobi{T,TX}(ω, similar(x), iter)
53+
Jacobi(x::TX, ω=0.5; iter=1) where {T, TX<:AbstractArray{T}} = Jacobi{T,TX}(ω, similar(x), iter)
5354

5455
function (jacobi::Jacobi)(A, x, b)
5556

5657
ω = jacobi.ω
5758
one = Base.one(eltype(A))
5859
temp = jacobi.temp
5960

60-
@inbounds for col = 1:size(x, 2)
61-
for i = 1:size(A, 1)
62-
temp[i, col] = x[i, col]
63-
end
61+
for i in 1:jacobi.iter
62+
@inbounds for col = 1:size(x, 2)
63+
for i = 1:size(A, 1)
64+
temp[i, col] = x[i, col]
65+
end
6466

65-
for i = 1:size(A, 1)
66-
rsum = zero(eltype(A))
67-
diag = zero(eltype(A))
67+
for i = 1:size(A, 1)
68+
rsum = zero(eltype(A))
69+
diag = zero(eltype(A))
6870

69-
for j in nzrange(A, i)
70-
row = A.rowval[j]
71-
val = A.nzval[j]
71+
for j in nzrange(A, i)
72+
row = A.rowval[j]
73+
val = A.nzval[j]
7274

73-
diag = ifelse(row == i, val, diag)
74-
rsum += ifelse(row == i, 0, val * temp[row, col])
75-
end
75+
diag = ifelse(row == i, val, diag)
76+
rsum += ifelse(row == i, 0, val * temp[row, col])
77+
end
7678

77-
xcand = (one - ω) * temp[i, col] + ω * ((b[i, col] - rsum) / diag)
78-
x[i, col] = ifelse(diag == 0, x[i, col], xcand)
79+
xcand = (one - ω) * temp[i, col] + ω * ((b[i, col] - rsum) / diag)
80+
x[i, col] = ifelse(diag == 0, x[i, col], xcand)
81+
end
7982
end
8083
end
8184
end
@@ -87,8 +90,8 @@ struct ParallelJacobi{T,TX} <: Smoother
8790
ω::T
8891
temp::TX
8992
end
90-
ParallelJacobi(ω, x::TX) where {T, TX<:AbstractVector{T}} = ParallelJacobi{T,TX}(ω, similar(x))
91-
ParallelJacobi(x::TX, ω = 0.5) where {T, TX<:AbstractVector{T}} = ParallelJacobi{T,TX}(ω, similar(x))
93+
ParallelJacobi(ω, x::TX) where {T, TX<:AbstractArray{T}} = ParallelJacobi{T,TX}(ω, similar(x))
94+
ParallelJacobi(x::TX, ω = 0.5) where {T, TX<:AbstractArray{T}} = ParallelJacobi{T,TX}(ω, similar(x))
9295
9396
struct ParallelTemp{TX, TI}
9497
temp::TX

0 commit comments

Comments
 (0)