Skip to content

Commit

Permalink
change method definition of record_from_solution which gives access t…
Browse files Browse the repository at this point in the history
…o the iterator and the state
  • Loading branch information
rveltz committed Sep 7, 2024
1 parent 9032bb2 commit f1503bc
Show file tree
Hide file tree
Showing 35 changed files with 107 additions and 94 deletions.
4 changes: 4 additions & 0 deletions News.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ BifurcationKit.jl, Changelog

All notable changes to this project will be documented in this file (hopefully). No performance improvements will be notified but mainly the addition of new methods, the modifications of internal structs, etc.

## [0.4.2]
- change bordered linear solvers' interface
- `record_from_solution` has been changed to the following definition

## [0.4.0]
- Setfield.jl is not anymore the main component of BifurcationKit to support parameter axis. It has been changed in favour of Accessors.jl

Expand Down
13 changes: 7 additions & 6 deletions examples/COModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ par_com = (q1 = 2.5, q2 = 2.0, q3 = 10., q4 = 0.0675, q5 = 1., q6 = 0.1, k = 0.4

z0 = [0.001137, 0.891483, 0.062345]

prob = BifurcationProblem(COm!, z0, par_com, (@optic _.q2); record_from_solution = (x, p) -> (x = x[1], y = x[2], s = x[3]))
prob = BifurcationProblem(COm!, z0, par_com, (@optic _.q2); record_from_solution = (x, p; k...) -> (x = x[1], y = x[2], s = x[3]))

opts_br = ContinuationPar(dsmax = 0.015, dsmin=1e-4, ds=1e-4, p_min = 0.5, p_max = 2.0, n_inversion = 6, detect_bifurcation = 3, nev = 3)
br = @time continuation(prob, PALC(), opts_br;
plot = true, verbosity = 0,
normC = norminf,
bothside = true)

BK.plot(br)#markersize=4, legend=:topright, ylims=(0,0.16))
plot(br)#markersize=4, legend=:topright, ylims=(0,0.16))
####################################################################################################
# periodic orbits
function plotSolution(x, p; k...)
Expand All @@ -49,7 +49,7 @@ function plotSolution(ax, x, p; ax1 = nothing, k...)
scatter!(ax, xtt.t, xtt[1,:]; markersize = 1.5, k...)
end

args_po = ( record_from_solution = (x, p) -> begin
args_po = ( record_from_solution = (x, p; k...) -> begin
xtt = BK.get_periodic_orbit(p.prob, x, @set par_com.q2 = p.p)
return (max = maximum(xtt[1,:]),
min = minimum(xtt[1,:]),
Expand Down Expand Up @@ -95,6 +95,7 @@ brpo = @time continuation(br, 5, opts_po_cont,
# alg = PALC(),
# alg = MoorePenrose(tangent=PALC(tangent = Bordered()), method = BK.direct),
δp = 0.0005,
linear_algo = COPBLS(),
callback_newton = callbackCO,
args_po...
)
Expand All @@ -114,8 +115,8 @@ sn_codim2 = continuation(br, 3, (@optic _.k), ContinuationPar(opts_br, p_max = 3
bdlinsolver = MatrixBLS()
)

BK.plot(sn_codim2)#, real.(sn_codim2.BT), ylims = (-1,1), xlims=(0,2))
BK.plot(sn_codim2, vars=(:q2, :x), branchlabel = "Fold", plotstability = false);plot!(br,xlims=(0.8,1.8))
plot(sn_codim2)#, real.(sn_codim2.BT), ylims = (-1,1), xlims=(0,2))
plot(sn_codim2, vars=(:q2, :x), branchlabel = "Fold", plotstability = false);plot!(br,xlims=(0.8,1.8))

hp_codim2 = continuation((@set br.alg.tangent = Bordered()), 2, (@optic _.k), ContinuationPar(opts_br, p_min = 0., p_max = 2.8, detect_bifurcation = 0, ds = -0.0001, dsmax = 0.08, dsmin = 1e-4, n_inversion = 6, detect_event = 2, detect_loop = true, max_steps = 50, detect_fold=false) ; plot = true,
verbosity = 0,
Expand All @@ -127,6 +128,6 @@ hp_codim2 = continuation((@set br.alg.tangent = Bordered()), 2, (@optic _.k), Co
bothside = true,
bdlinsolver = MatrixBLS())

BK.plot(sn_codim2, vars=(:q2, :x), branchlabel = "Fold", plotcirclesbif = true)
plot(sn_codim2, vars=(:q2, :x), branchlabel = "Fold", plotcirclesbif = true)
plot!(hp_codim2, vars=(:q2, :x), branchlabel = "Hopf",plotcirclesbif = true)
plot!(br,xlims=(0.6,1.5))
2 changes: 1 addition & 1 deletion examples/SH2d-fronts-cuda.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ par = (l = -0.1, ν = 1.3, L = L)
prob = BK.BifurcationProblem(F_shfft, AF(sol0), par, (@optic _.l) ;
J = J_shfft,
plot_solution = (x, p;kwargs...) -> plotsol!(x; color=:viridis, kwargs...),
record_from_solution = (x, p) -> norm(x))
record_from_solution = (x, p; k...) -> norm(x))

opt_new = NewtonPar(verbose = true, tol = 1e-6, linsolver = L, eigsolver = Leig)
sol_hexa = @time newton(prob, opt_new, normN = norminf);
Expand Down
12 changes: 8 additions & 4 deletions examples/SH2d-fronts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ par = (l = -0.1, ν = 1.3, L1 = L1);

optnew = NewtonPar(verbose = true, tol = 1e-8, max_iterations = 20)

prob = BifurcationProblem(F_sh, vec(sol0), par, (@optic _.l); J = dF_sh, plot_solution = (x, p; kwargs...) -> (plotsol!((x); label="", kwargs...)),record_from_solution = (x, p) -> (n2 = norm(x), n8 = norm(x, 8)), d2F=d2F_sh, d3F=d3F_sh)
prob = BifurcationProblem(F_sh, vec(sol0), par, (@optic _.l); J = dF_sh, plot_solution = (x, p; kwargs...) -> (plotsol!((x); label="", kwargs...)),record_from_solution = (x, p; k...) -> (n2 = norm(x), n8 = norm(x, 8)), d2F=d2F_sh, d3F=d3F_sh)
# optnew = NewtonPar(verbose = true, tol = 1e-8, max_iterations = 20, eigsolver = EigArpack(0.5, :LM))
sol_hexa = @time newton(prob, optnew)
println("--> norm(sol) = ", norm(sol_hexa.u, Inf64))
Expand Down Expand Up @@ -97,7 +97,7 @@ br = @time continuation(
###################################################################################################
# codim2 Fold continuation
optfold = @set optcont.detect_bifurcation = 0
@set! optfold.newton_options.verbose = true
@reset optfold.newton_options.verbose = true
optfold = setproperties(optfold; p_min = -2., p_max= 2., dsmax = 0.1)

# dispatch plot to fold solution
Expand Down Expand Up @@ -127,7 +127,7 @@ function dF_sh2(du, u, p)
end

prob2 = @set prob.VF.J = (u, p) -> (du -> dF_sh2(du, u, p))
@set! prob2.u0 = vec(sol0)
@reset prob2.u0 = vec(sol0)

sol_hexa = @time newton(prob2, @set optnew.linsolver = ls)
println("--> norm(sol) = ", norm(sol_hexa.u, Inf64))
Expand Down Expand Up @@ -168,7 +168,11 @@ plot!(br)
deflationOp = DeflationOperator(2, 1.0, [sol_hexa.u])
optcontdf = @set optcont.newton_options.verbose = false

algdc = BK.DefCont(deflation_operator = deflationOp, perturb_solution = (x,p,id) -> (x .+ 0.1 .* rand(length(x))), max_iter_defop = 50, max_branches = 40, seek_every_step = 5,)
algdc = BK.DefCont(deflation_operator = deflationOp,
perturb_solution = (x,p,id) -> (x .+ 0.1 .* rand(length(x))),
max_iter_defop = 50,
max_branches = 40,
seek_every_step = 5,)

brdf = continuation(prob, algdc, setproperties(optcontdf; detect_bifurcation = 0, plot_every_step = 1);
plot = true, verbosity = 2,
Expand Down
10 changes: 5 additions & 5 deletions examples/SH3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ prob = BK.BifurcationProblem(F_sh, AF(vec(sol0)), par, (@optic _.l),
J = (x, p) -> (dx -> dF_sh(x, p, dx)),
# J = (x, p) -> J_sh(x, p),
plot_solution = (ax, x, p; ax1=nothing) -> contour3dMakie!(ax, x),
record_from_solution = (x, p) -> (n2 = norm(x), n8 = norm(x, 8)),
record_from_solution = (x, p; k...) -> (n2 = norm(x), n8 = norm(x, 8)),
issymmetric = true)

optnew = NewtonPar(verbose = true, tol = 1e-8, max_iterations = 20, linsolver = @set ls.verbose = 0)
@set! optnew.eigsolver = eigSH3d
# @set! optnew.linsolver = DefaultLS()
@reset optnew.eigsolver = eigSH3d
# @reset optnew.linsolver = DefaultLS()
sol_hexa = @time newton(prob, optnew)
println("--> norm(sol) = ", norm(sol_hexa.u, Inf64))

Expand Down Expand Up @@ -173,11 +173,11 @@ br = @time continuation(
# end
)

plot(br)
BK.plot(br)
####################################################################################################
get_normal_form(br, 3; nev = 25)

br1 = @time continuation(br, 3, setproperties(optcont; save_sol_every_step = 10, detect_bifurcation = 3, p_max = 0.1, plot_every_step = 5, dsmax = 0.01);
br1 = @time continuation(br, 3, setproperties(optcont; save_sol_every_step = 10, detect_bifurcation = 0, p_max = 0.1, plot_every_step = 5, dsmax = 0.01);
plot = true, verbosity = 3,
δp = 0.005,
verbosedeflation = true,
Expand Down
2 changes: 1 addition & 1 deletion examples/SHpde_snaking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ parSH = (λ = -0.1, ν = 2., L1 = L1)
sol0 = 1.1cos.(X) .* exp.(-0X.^2/(2*5^2))

prob = BifurcationProblem(R_SH, sol0, parSH, (@optic _.λ); J = Jac_sp,
record_from_solution = (x, p) -> (n2 = norm(x), nw = normweighted(x), s = sum(x), s2 = x[end ÷ 2], s4 = x[end ÷ 4], s5 = x[end ÷ 5]),
record_from_solution = (x, p; k...) -> (n2 = norm(x), nw = normweighted(x), s = sum(x), s2 = x[end ÷ 2], s4 = x[end ÷ 4], s5 = x[end ÷ 5]),
plot_solution = (x, p;kwargs...)->(plot!(X, x; ylabel="solution", label="", kwargs...))
)
####################################################################################################
Expand Down
14 changes: 7 additions & 7 deletions examples/TMModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ end

par_tm == 1.5, τ = 0.013, J = 3.07, E0 = -2.0, τD = 0.200, U0 = 0.3, τF = 1.5, τS = 0.007)
z0 = [0.238616, 0.982747, 0.367876 ]
prob = BifurcationProblem(TMvf!, z0, par_tm, (@lens _.E0); record_from_solution = (x, p) -> (E = x[1], x = x[2], u = x[3]),)
prob = BifurcationProblem(TMvf!, z0, par_tm, (@optic _.E0); record_from_solution = (x, p; k...) -> (E = x[1], x = x[2], u = x[3]),)

opts_br = ContinuationPar(p_min = -10.0, p_max = -0., dsmax = 0.1, n_inversion = 8, nev = 3)
opts_br = ContinuationPar(p_min = -10.0, p_max = 1., dsmax = 0.1, n_inversion = 8, nev = 3)
br = continuation(prob, PALC(tangent = Bordered()), opts_br; plot = false, normC = norminf, bothside = true)

BK.plot(br, plotfold=false)
plot(br, plotfold=false)
####################################################################################################
br_fold = BK.continuation(br, 1, (@optic _.α), ContinuationPar(br.contparams, p_min = 0.2, p_max = 5.), bothside = true)
br_fold = BK.continuation(br, 2, (@optic _.α), ContinuationPar(br.contparams, p_min = 0.2, p_max = 5.), bothside = true)
plot(br_fold)
####################################################################################################
# continuation parameters
Expand All @@ -40,7 +40,7 @@ function plotSolution(x, p; k...)
plot!(br; subplot = 1, putspecialptlegend = false)
end

args_po = ( record_from_solution = (x, p) -> begin
args_po = ( record_from_solution = (x, p; k...) -> begin
xtt = BK.get_periodic_orbit(p.prob, x, p.p)
return (max = maximum(xtt[1,:]),
min = minimum(xtt[1,:]),
Expand Down Expand Up @@ -87,7 +87,7 @@ prob_ode = ODEProblem(TMvf!, copy(z0), (0., 1000.), par_tm; abstol = 1e-11, relt
opts_po_cont = ContinuationPar(opts_br, ds= -0.0001, dsmin = 1e-4, max_steps = 120, newton_options = NewtonPar(tol = 1e-6, max_iterations = 7), tol_stability = 1e-8, detect_bifurcation = 2, plot_every_step = 10, save_sol_every_step=1)

br_posh = @time continuation(
br, 4,
br, 5,
# arguments for continuation
opts_po_cont,
# this is where we tell that we want Standard Shooting
Expand All @@ -105,7 +105,7 @@ plot(br_posh, br, markersize=3)
opts_po_cont = ContinuationPar(opts_br, dsmax = 0.02, ds= 0.0001, max_steps = 50, newton_options = NewtonPar(tol = 1e-9, max_iterations=15), tol_stability = 1e-6, detect_bifurcation = 2, plot_every_step = 5)

br_popsh = @time continuation(
br, 4,
br, 5,
# arguments for continuation
opts_po_cont,
# this is where we tell that we want Poincaré Shooting
Expand Down
2 changes: 1 addition & 1 deletion examples/brusselator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ prob = BifurcationProblem(Fbru!, sol0, par_bru, (@optic _.l);
J = Jbru_sp,
# plot_solution = (x, p; kwargs...) -> plotsol(x; label="", kwargs... ), # for Plots.jl
# plot_solution = (ax, x, p) -> plotsol(ax, x), # For Makie.jl
record_from_solution = (x, p) -> x[div(n,2)])
record_from_solution = (x, p; k...) -> x[div(n,2)])
####################################################################################################
eigls = EigArpack(1.1, :LM)
opts_br_eq = ContinuationPar(dsmin = 0.03, dsmax = 0.05, ds = 0.03, p_max = 1.9, detect_bifurcation = 3, nev = 21, plot_every_step = 50, newton_options = NewtonPar(eigsolver = eigls, tol = 1e-9), max_steps = 1060, n_inversion = 6, tol_bisection_eigenvalue = 1e-20, max_bisection_steps = 30)
Expand Down
4 changes: 2 additions & 2 deletions examples/brusselatorShooting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ sol0 = vcat(par_bru.α * ones(n), par_bru.β/par_bru.α * ones(n))
probBif = BifurcationProblem(Fbru!, sol0, par_bru, (@optic _.l);
J = Jbru_sp,
plot_solution = (x, p; ax1 = 0, kwargs...) -> (plotsol(x; label="", kwargs... )),
record_from_solution = (x, p) -> x[div(n,2)]
record_from_solution = (x, p; k...) -> x[div(n,2)]
)
####################################################################################################
eigls = EigArpack(1.1, :LM)
Expand Down Expand Up @@ -177,7 +177,7 @@ br_po = @time continuation(deepcopy(probSh), outpo.u, PALC(),
# (Base.display(contResult.eig[end].eigenvals) ;true),
callback_newton = BK.cbMaxNorm(1.),
plot_solution = (x, p; kwargs...) -> BK.plot_periodic_shooting!(x[1:end-1], length(1:dM:M); kwargs...),
record_from_solution = (u, p) -> u[end], normC = norminf)
record_from_solution = (u, p; k...) -> u[end], normC = norminf)

####################################################################################################
# automatic branch switching with Shooting
Expand Down
16 changes: 9 additions & 7 deletions examples/cGL2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ poTrap = PeriodicOrbitTrapProblem(re_make(prob, params = (@set par_cgl.r = r_hop

ls0 = GMRESIterativeSolvers(N = 2n, reltol = 1e-9)#, Pl = lu(I + par_cgl.Δ))
poTrapMF = setproperties(poTrap; linsolver = ls0)
@set! poTrapMF.prob_vf.VF.J = (x, p) -> (dx -> dFcgl(x, p, dx))
@reset poTrapMF.prob_vf.VF.J = (x, p) -> (dx -> dFcgl(x, p, dx))

poTrap(orbitguess_f, @set par_cgl.r = r_hopf - 0.1) |> plot
poTrapMF(orbitguess_f, @set par_cgl.r = r_hopf - 0.1) |> plot
Expand Down Expand Up @@ -205,7 +205,7 @@ ls = GMRESIterativeSolvers(verbose = false, reltol = 1e-3, N = size(Jpo,1), rest
ls(Jpo, rand(ls.N))

opt_po = @set opt_newton.verbose = true
@set! opt_po.linsolver = ls
@reset opt_po.linsolver = ls

outpo_f = @time newton(poTrapMF, orbitguess_f, opt_po; normN = norminf)
BK.converged(outpo_f) && printstyled(color=:red, "--> T = ", outpo_f.u[end], ", amplitude = ", BK.amplitude(outpo_f.u, Nx*Ny, M; ratio = 2),"\n")
Expand All @@ -219,7 +219,7 @@ br_po = @time continuation(poTrapMF, outpo_f.u, PALC(), opts_po_cont;
verbosity = 3,
plot = true,
# plot_solution = (x, p;kwargs...) -> BK.plot_periodic_potrap(x, M, Nx, Ny; ratio = 2, kwargs...),
record_from_solution = (u, p) -> BK.getamplitude(poTrapMF, u, par_cgl; ratio = 2),
record_from_solution = (u, p; k...) -> BK.getamplitude(poTrapMF, u, par_cgl; ratio = 2),
normC = norminf)

branches = Any[br_pok2]
Expand All @@ -232,13 +232,15 @@ br_po = continuation(
br, 1,
# arguments for continuation
opts_po_cont, poTrapMF;
ampfactor = 3.,
verbosity = 3, plot = true,
# ampfactor = 3.,
verbosity = 3,
plot = true,
# callback_newton = (x, f, J, res, iteration, itl, options; kwargs...) -> (println("--> amplitude = ", BK.amplitude(x, n, M; ratio = 2));true),
finalise_solution = (z, tau, step, contResult; k...) ->
(BK.haseigenvalues(contResult) && Base.display(contResult.eig[end].eigenvals) ;true),
plot_solution = (x, p; kwargs...) -> BK.plot_periodic_potrap(x, M, Nx, Ny; ratio = 2, kwargs...),
record_from_solution = (u, p) -> BK.amplitude(u, Nx*Ny, M; ratio = 2), normC = norminf)
record_from_solution = (u, p; k...) -> BK.amplitude(u, Nx*Ny, M; ratio = 2),
normC = norminf)
####################################################################################################
# Experimental, full Inplace
@views function NL!(f, u, p, t = 0.)
Expand Down Expand Up @@ -302,7 +304,7 @@ out_ = similar(sol0f)
@time Fcgl!(out_, sol0f, par_cgl)
@time dFcgl!(out_, sol0f, par_cgl, sol0f)

probInp = BifurcationProblem(Fcgl!, vec(sol0), (@set par_cgl.r = r_hopf - 0.01), (@lens _.r); J = dFcgl!, inplace = true)
probInp = BifurcationProblem(Fcgl!, vec(sol0), (@set par_cgl.r = r_hopf - 0.01), (@optic _.r); J = dFcgl!, inplace = true)

ls = GMRESIterativeSolvers(verbose = false, reltol = 1e-3, N = size(Jpo,1), restart = 40, maxiter = 50, Pl = Precilu, log=true)
ls(Jpo, rand(ls.N))
Expand Down
2 changes: 1 addition & 1 deletion examples/carrier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ X = LinRange(-1,1,N)
dx = X[2] - X[1]
par_car == 0.7, X = X, dx = dx)
sol = -(1 .- par_car.X.^2)
recordFromSolution(x, p) = (x[2]-x[1]) * sum(x->x^2, x)
recordFromSolution(x, p; k...) = (x[2]-x[1]) * sum(x->x^2, x)

prob = BK.BifurcationProblem(F_carr, zeros(N), par_car, (@optic _.ϵ);
J = Jac_carr,
Expand Down
4 changes: 3 additions & 1 deletion examples/chan-af.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ end
sol0 = Fun( x -> x * (1-x), Interval(0.0, 1.0))
const Δ = Derivative(sol0.space, 2);
par_af == 3., β = 0.01)
prob = BifurcationProblem(F_chan, sol0, par_af, (@optic _.α); J = Jac_chan, plot_solution = (x, p; kwargs...) -> plot!(x; label = "l = $(length(x))", kwargs...))
prob = BifurcationProblem(F_chan, sol0, par_af, (@optic _.α);
J = Jac_chan,
plot_solution = (x, p; kwargs...) -> plot!(x; label = "l = $(length(x))", kwargs...))

optnew = NewtonPar(tol = 1e-12, verbose = true)
sol = @time BK.newton(prob, optnew, normN = norminf)
Expand Down
Loading

2 comments on commit f1503bc

@rveltz
Copy link
Member Author

@rveltz rveltz commented on f1503bc Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/114741

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.2 -m "<description of version>" f1503bca74b00d13895db2a1749ea1ea2331f1a1
git push origin v0.4.2

Please sign in to comment.