-
Notifications
You must be signed in to change notification settings - Fork 0
/
multigrid_raw.jl
170 lines (150 loc) · 4.62 KB
/
multigrid_raw.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using LinearAlgebra
using SparseArrays
using Plots
function A₁(n::Int, σ::Float64)
∂² = Tridiagonal(ones(n-1), -2 * ones(n), ones(n-1))
∂x² = (n^2) * kron(sparse(∂²), I(n))
∂y² = (n^2) * kron(I(n), sparse(∂²))
Δ = ∂x² + ∂y²
return σ * I(n^2) - Δ
end
function A₂(n::Int, ϵ::Float64)
∂² = Tridiagonal(ones(n-1), -2 * ones(n), ones(n-1))
∂x² = (n^2) * kron(sparse(∂²), I(n))
∂y² = (n^2) * kron(I(n), sparse(∂²))
return - ∂x² - ϵ * ∂y²
end
A₁(σ::Float64) = n -> A₁(n, σ)
A₂(ϵ::Float64) = n -> A₂(n, ϵ)
function boundaries(v)
n = Int(sqrt(size(v,1)))
gridᵥ = reshape(v, (n, n))'
gridᵥ[1,:] .= 0
gridᵥ[end,:] .= 0
gridᵥ[:,1] .= 0
gridᵥ[:,end] .= 0
return gridᵥ |> transpose |> vec
end
function Jacobi(A, b, u₀ = zeros(size(A, 1)), ϵ = 1e-7, maxiter = 10, bounds = false)
u = u₀; n = Int(sqrt(size(A, 1))); iter = 0
M = Diagonal(A)
N = UnitLowerTriangular(A) + UnitUpperTriangular(A) - 2*I(n^2)
while iter <= maxiter
iter += 1
if bounds
u = boundaries(u)
end
u = inv(M) * (N*u + b)
(norm(b - A*u, 2) > ϵ) || break
end
return u
end
function JOR(A, b, ω, u₀ = zeros(size(A, 1)), ϵ = 1e-7, maxiter = 10, bounds = false)
u = u₀; iter = 0
M = Diagonal(A) / ω
while iter <= maxiter
iter += 1
if bounds
u = boundaries(u)
end
r = b - A*u
z = inv(M) * r
u += z
(norm(r, 2) > ϵ) || break
end
return u
end
function SOR(A, b, ω, u₀ = zeros(size(A, 1)), ϵ = 1e-7, maxiter = 10, bounds = false)
u = u₀; n = Int(sqrt(size(A, 1))); iter = 0
D = Diagonal(A)
L = UnitLowerTriangular(A) - I(n^2)
U = UnitUpperTriangular(A) - I(n^2)
while iter <= maxiter
iter += 1
if bounds
u = boundaries(u)
end
u = inv(D + ω * L) * (ω * b - (ω * U + (ω-1) * D) * u)
(norm(b - A*u, 2) > ϵ) || break
end
return u
end
# Restriction
injection(grid) = grid[2:2:end, 2:2:end]
function halfweight(grid)
g = Float64.(grid)
for i=2:2:size(grid,1)-1, j=2:2:size(grid,2)-1
g[i,j] = g[i,j] / 2 - (g[i-1,j] + g[i+1,j] +
g[i,j-1] + g[i,j+1]) / 8
end
return injection(g)
end
function ϵ_halfweight(ϵ, grid)
g = Float64.(grid)
for i=2:2:size(grid,1)-1, j=2:2:size(grid,2)-1
g[i,j] = ϵ * g[i,j] / 4 - (
g[i-1,j] + g[i+1,j]
+ (1 - ϵ/2) * g[i,j-1] + (1 - ϵ/2) * g[i,j+1]) / 8
end
return injection(g)
end
ϵ_halfweight(ϵ) = grid -> ϵ_halfweight(ϵ, grid)
function fullweight(grid)
g = Float64.(grid)
for i=2:2:size(grid,1)-1, j=2:2:size(grid,2)-1
g[i,j] = g[i,j] / 4 - (g[i-1,j] + g[i+1,j] +
g[i,j-1] + g[i,j+1]) / 8 -
(g[i-1,j-1] + g[i+1,j+1] +
g[i-1,j+1] + g[i+1,j-1]) / 16
end
return injection(g)
end
# Prolongation
function enlarge(grid)
n = size(grid,1) * 2
n == 2 && return repeat(grid, n, n)
g = zeros((n,n))
for i=2:n-1, j=2:n-1
g[i, j] = grid[i÷2, j÷2]
end
return g
end
function linearize(grid)
n = size(grid,1) * 2
n == 2 && return repeat(grid, n, n)
g = zeros((n,n))
for i=2:n-1, j=2:n-1
g[i, j] = (grid[Int(floor((i+1)/2)), Int(floor((j+1)/2))]
+ grid[Int(ceil((i+1)/2)), Int(floor((j+1)/2))]
+ grid[Int(floor((i+1)/2)), Int(ceil((j+1)/2))]
+ grid[Int(ceil((i+1)/2)), Int(ceil((j+1)/2))]) / 4
end
return g
end
function multigrid(A, b, u, l, ω, ϵ=1e-7, steps=1,
restrict=injection, prolong=enlarge, iter=10)
n = Int(sqrt(size(b, 1)))
Aₙ = A(n)
if l == 0
# We can also use a direct solver instead
# u = Array(Aₙ) \ b
u = JOR(Aₙ, b, ω, u, ϵ, iter) # Resolution
else
u = JOR(Aₙ, b, ω, u, ϵ, iter) # Pre-smoothing
# Defect restriction
r = reshape(b - Aₙ*u, (n, n)) |> transpose |>
restrict |> transpose |> vec
# Coarse-level Correction
δᵣ = zeros(size(r))
for i=1:steps
δᵣ = multigrid(A, r, δᵣ, l-1, ω, ϵ, steps,
restrict, prolong, iter*3)
end
# Defect Prolongation δᵣ → δ
δ = reshape(δᵣ, (n÷2, n÷2)) |> transpose |>
prolong |> transpose |> vec
u += δ # Correction
u = JOR(Aₙ, b, ω, u, ϵ, iter) # Post-smoothing
end
return u
end ;