Skip to content

Commit 004c531

Browse files
authored
Add p as a parameter to F (#16)
1 parent ea85816 commit 004c531

File tree

7 files changed

+97
-83
lines changed

7 files changed

+97
-83
lines changed

examples/bratu.jl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using CairoMakie
1111

1212
# F(u) = 0
1313

14-
function bratu!(res, y, Δx, λ)
14+
function bratu!(res, y, (Δx, λ))
1515
N = length(y)
1616
for i in 1:N
1717
y_l = i == 1 ? zero(eltype(y)) : y[i - 1]
@@ -23,9 +23,9 @@ function bratu!(res, y, Δx, λ)
2323
return nothing
2424
end
2525

26-
function bratu(y, dx, λ)
26+
function bratu(y, p)
2727
res = similar(y)
28-
bratu!(res, y, dx, λ)
28+
bratu!(res, y, p)
2929
return res
3030
end
3131

@@ -57,8 +57,8 @@ fig
5757

5858
# ## Solving using inplace variant and CG
5959
uₖ, _ = newton_krylov!(
60-
(res, u) -> bratu!(res, u, dx, λ),
61-
copy(u₀), similar(u₀);
60+
bratu!,
61+
copy(u₀), (dx, λ), similar(u₀);
6262
Solver = CgSolver,
6363
)
6464

@@ -83,25 +83,25 @@ end
8383
# ## Solving using the out of place variant
8484

8585
_, stats = newton_krylov(
86-
(u) -> bratu(u, dx, λ),
87-
copy(u₀);
86+
bratu,
87+
copy(u₀), (dx, λ);
8888
Solver = CgSolver
8989
)
9090
stats
9191

9292
# ## Solving with a fixed forcing
9393
_, stats = newton_krylov!(
94-
(res, u) -> bratu!(res, u, dx, λ),
95-
copy(u₀), similar(u₀);
94+
bratu!,
95+
copy(u₀), (dx, λ), similar(u₀);
9696
Solver = CgSolver,
9797
forcing = NewtonKrylov.Fixed(0.1)
9898
)
9999
stats
100100

101101
# ## Solving with no forcing
102102
_, stats = newton_krylov!(
103-
(res, u) -> bratu!(res, u, dx, λ),
104-
copy(u₀), similar(u₀);
103+
bratu!,
104+
copy(u₀), (dx, λ), similar(u₀);
105105
Solver = CgSolver,
106106
forcing = nothing
107107
)
@@ -110,17 +110,17 @@ stats
110110
# ## Solve using GMRES -- doesn't converge
111111
# ```julia
112112
# _, stats = newton_krylov!(
113-
# (res, u) -> bratu!(res, u, dx, λ),
114-
# copy(u₀), similar(u₀);
113+
# bratu!,
114+
# copy(u₀), (dx, λ), similar(u₀);
115115
# Solver = GmresSolver,
116116
# )
117117
# stats
118118
# ```
119119

120120
# ## Solve using GMRES + ILU Preconditoner
121121
_, stats = newton_krylov!(
122-
(res, u) -> bratu!(res, u, dx, λ),
123-
copy(u₀), similar(u₀);
122+
bratu!,
123+
copy(u₀), (dx, λ), similar(u₀);
124124
Solver = GmresSolver,
125125
N = (J) -> ilu(collect(J)), # Assembles the full Jacobian
126126
krylov_kwargs = (; ldiv = true)
@@ -129,8 +129,8 @@ stats
129129

130130
# ## Solve using FGMRES + ILU Preconditoner
131131
_, stats = newton_krylov!(
132-
(res, u) -> bratu!(res, u, dx, λ),
133-
copy(u₀), similar(u₀);
132+
bratu!,
133+
copy(u₀), (dx, λ), similar(u₀);
134134
Solver = FgmresSolver,
135135
N = (J) -> ilu(collect(J)), # Assembles the full Jacobian
136136
krylov_kwargs = (; ldiv = true)
@@ -149,8 +149,8 @@ function LinearAlgebra.mul!(y, P::GmresPreconditioner, x)
149149
end
150150

151151
_, stats = newton_krylov!(
152-
(res, u) -> bratu!(res, u, dx, λ),
153-
copy(u₀), similar(u₀);
152+
bratu!,
153+
copy(u₀), (dx, λ), similar(u₀);
154154
Solver = FgmresSolver,
155155
N = (J) -> GmresPreconditioner(J, 5),
156156
)
@@ -159,17 +159,17 @@ stats
159159
# ## Explodes..
160160
# ```julia
161161
# newton_krylov!(
162-
# (res, u) -> bratu!(res, u, dx, λ),
163-
# copy(u₀), similar(u₀);
162+
# bratu!,
163+
# copy(u₀), (dx, λ), similar(u₀);
164164
# Solver = CglsSolver, # CgneSolver
165165
# krylov_kwargs = (; verbose=1)
166166
# )
167167
# ```
168168
#
169169
# ```julia
170170
# newton_krylov!(
171-
# (res, u) -> bratu!(res, u, dx, λ),
172-
# copy(u₀), similar(u₀);
171+
# bratu!,
172+
# copy(u₀), (dx, λ), similar(u₀);
173173
# verbose = 1,
174174
# Solver = BicgstabSolver, # L=2
175175
# η_max = nothing

examples/bratu_ka.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using KernelAbstractions
1010
# ## 1D Bratu equations
1111
# $y′′ + λ * exp(y) = 0$
1212

13-
@kernel function bratu_kernel!(res, y, Δx, λ)
13+
@kernel function bratu_kernel!(res, y, (Δx, λ))
1414
i = @index(Global, Linear)
1515
N = length(res)
1616
y_l = i == 1 ? zero(eltype(y)) : y[i - 1]
@@ -20,16 +20,16 @@ using KernelAbstractions
2020
res[i] = y′′ + λ * exp(y[i]) # = 0
2121
end
2222

23-
function bratu!(res, y, Δx, λ)
23+
function bratu!(res, y, p)
2424
device = KernelAbstractions.get_backend(res)
2525
kernel = bratu_kernel!(device)
26-
kernel(res, y, Δx, λ, ndrange = length(res))
26+
kernel(res, y, p, ndrange = length(res))
2727
return nothing
2828
end
2929

30-
function bratu(y, dx, λ)
30+
function bratu(y, p)
3131
res = similar(y)
32-
bratu!(res, y, dx, λ)
32+
bratu!(res, y, p)
3333
return res
3434
end
3535

@@ -61,8 +61,8 @@ fig
6161

6262
# ## Solving using inplace variant and CG
6363
uₖ, _ = newton_krylov!(
64-
(res, u) -> bratu!(res, u, dx, λ),
65-
copy(u₀), similar(u₀);
64+
bratu!,
65+
copy(u₀), (dx, λ), similar(u₀);
6666
Solver = CgSolver,
6767
)
6868

examples/bvp.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function Phi(t, tdag, vp, v)
77
return phi
88
end
99

10-
function Fbvp!(res, U, force, tv, tvdag, h, n)
10+
function Fbvp!(res, U, (force, tv, tvdag, h, n))
1111
@assert 2n == length(U)
1212
res[1] = U[2]
1313
res[2n] = U[2n - 1]
@@ -50,10 +50,9 @@ function BVP_solve(n = 801, T = Float64)
5050
force = zeros(n)
5151

5252
BVP_U0!(U0, n, tv)
53-
F!(res, u) = Fbvp!(res, u, force, tv, tvdag, h, n)
5453

5554
bvpout, stats = newton_krylov!(
56-
F!, U0, res,
55+
Fbvp!, U0, (force, tv, tvdag, h, n), res,
5756
Solver = FgmresSolver,
5857
N = (J) -> GmresPreconditioner(J, 30),
5958
)

examples/implicit_timedependent.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ function implicit_spring(G! = G_Euler!)
5656
if t == t₀
5757
continue
5858
end
59-
F!(res, y) = G!(res, (y, t) -> f(y, t, γ), y, yₙ, t, Δt)
60-
y, _ = newton_krylov!(F!, copy(yₙ))
59+
F!(res, y, (yₙ, t, Δt)) = G!(res, (y, t) -> f(y, t, γ), y, yₙ, t, Δt)
60+
y, _ = newton_krylov!(F!, copy(yₙ), (yₙ, t, Δt))
6161
push!(hist, y)
6262
yₙ .= y
6363
end

examples/simple.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
using NewtonKrylov, LinearAlgebra
44
using CairoMakie
55

6-
function F!(res, x)
6+
function F!(res, x, p)
77
res[1] = x[1]^2 + x[2]^2 - 2
88
return res[2] = exp(x[1] - 1) + x[2]^2 - 2
99
end
1010

11-
function F(x)
11+
function F(x, p)
1212
res = similar(x)
13-
F!(res, x)
13+
F!(res, x, p)
1414
return res
1515
end
1616

@@ -20,28 +20,28 @@ ys = LinRange(-15, 10, 1000)
2020

2121
levels = [0.1, 0.25, 0.5:2:10..., 10.0:10:200..., 200:100:4000...]
2222

23-
fig, ax = contour(xs, ys, (x, y) -> norm(F([x, y])); levels)
23+
fig, ax = contour(xs, ys, (x, y) -> norm(F([x, y], nothing)); levels)
2424

2525
trace_1 = let x₀ = [2.0, 0.5]
2626
xs = Vector{Tuple{Float64, Float64}}(undef, 0)
2727
hist(x, res, n_res) = (push!(xs, (x[1], x[2])); nothing)
28-
x, stats = newton_krylov!(F!, x₀, callback = hist)
28+
x, stats = newton_krylov!(F!, x₀, nothing, callback = hist)
2929
xs
3030
end
3131
lines!(ax, trace_1)
3232

3333
trace_2 = let x₀ = [2.5, 3.0]
3434
xs = Vector{Tuple{Float64, Float64}}(undef, 0)
3535
hist(x, res, n_res) = (push!(xs, (x[1], x[2])); nothing)
36-
x, stats = newton_krylov!(F!, x₀, callback = hist)
36+
x, stats = newton_krylov!(F!, x₀, nothing, callback = hist)
3737
xs
3838
end
3939
lines!(ax, trace_2)
4040

4141
trace_3 = let x₀ = [3.0, 4.0]
4242
xs = Vector{Tuple{Float64, Float64}}(undef, 0)
4343
hist(x, res, n_res) = (push!(xs, (x[1], x[2])); nothing)
44-
x, stats = newton_krylov!(F!, x₀, callback = hist, forcing = NewtonKrylov.EisenstatWalker(η_max = 0.68949), verbose = 1)
44+
x, stats = newton_krylov!(F!, x₀, nothing, callback = hist, forcing = NewtonKrylov.EisenstatWalker(η_max = 0.68949), verbose = 1)
4545
@show stats.solved
4646
xs
4747
end

0 commit comments

Comments
 (0)