Skip to content

Commit

Permalink
!!! remove requires.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
rveltz committed Aug 16, 2024
1 parent 4948c6a commit 0e6ca9a
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 223 deletions.
3 changes: 3 additions & 0 deletions News.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file (hopefully).
## Near future
- Setfield.jl has been changed to Accessors.jl

## [0.3.7]
- remove `Requires.jl` and use extensions. This requires julia>=1.9

## [0.3.5]
- add field `save_solution` to `BifurcationProblem`. Allows to save problem state along with the current solution. Useful for periodic orbits for example where we can save the phase condition and the mesh when the latter is adapted.

Expand Down
18 changes: 13 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BifurcationKit"
uuid = "0f109fa4-8a5d-4b75-95aa-f515264e7665"
authors = ["Romain Veltz <[email protected]>"]
version = "0.3.6"
version = "0.3.7"

[deps]
ArnoldiMethod = "ec485272-7323-5ecc-a04f-4719b315124d"
Expand All @@ -22,7 +22,6 @@ Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Expand All @@ -42,14 +41,23 @@ LinearMaps = "2.7, ^3.0"
OrdinaryDiffEq = "^6.33"
Parameters = "0.12.3"
PreallocationTools = "0.4.1"
RecipesBase = "1.0, 1.1"
RecipesBase = "1.0, 1.1, 1.3"
RecursiveArrayTools = "^2.3, ^2.4, ^2.8, ^2.9, ^3"
Reexport = "1.2.2"
Requires = "1.1.2"
SciMLBase = "^1, ^2"
Setfield = "0.5.0, 0.7.0, 0.8.0, ^1.1"
StructArrays = "0.4, ^0.6"
julia = "1.5"
julia = "1.9"

[weakdeps]
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"

[extensions]
JLD2Ext = "JLD2"
PlotsExt = "Plots"
GLMakieExt = "GLMakie"

[extras]
AbstractDifferentiation = "c29ec348-61ec-40c8-8164-b8c60e9d9f3d"
Expand Down
6 changes: 3 additions & 3 deletions examples/COModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ br = @time continuation(prob, PALC(), opts_br;
normC = norminf,
bothside = true)

BK.plot(br, plotfold=false, )#markersize=4, legend=:topright, ylims=(0,0.16))
BK.plot(br)#markersize=4, legend=:topright, ylims=(0,0.16))
####################################################################################################
# periodic orbits
function plotSolution(x, p; k...)
Expand All @@ -42,7 +42,7 @@ end
function plotSolution(ax, x, p; ax1 = nothing, k...)
@info "plotsol Makie"
xtt = BK.get_periodic_orbit(p.prob, x, p.p)
lines!(ax1, br; putspecialptlegend = false)
lines!(ax1, br)
lines!(ax, xtt.t, xtt[1,:]; k...)
lines!(ax, xtt.t, xtt[2,:]; k...)
lines!(ax, xtt.t, xtt[3,:]; k...)
Expand Down Expand Up @@ -99,7 +99,7 @@ brpo = @time continuation(br, 5, opts_po_cont,
args_po...
)

BK.plot(br, brpo, putspecialptlegend = false, branchlabel = ["eq","max"])
BK.plot(br, brpo, branchlabel = ["eq","max"])
xlims!((1.037, 1.055))
scatter!(br)
plot!(brpo.param, brpo.min, label = "min", xlims = (1.037, 1.055))
Expand Down
29 changes: 29 additions & 0 deletions ext/GLMakieExt/GLMakieExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module GLMakieExt
using GLMakie, BifurcationKit
import BifurcationKit: plot,
plot!,
hasbranch,
plot_branch_cont,
plot_periodic_potrap,
plot_periodic_shooting!,
plot_periodic_shooting,
plot_eigenvals,
compute_eigenelements,
get_lens_symbol,
_getfirstusertype,
ContResult,
DCResult,
AbstractBranchResult,
get_plot_vars,
get_axis_labels,
_hasstability,
filter_bifurcations,
get_color,
colorbif,
get_plot_backend,
BK_Makie

# TODO block precompilation
get_plot_backend() = BK_Makie()
include("plot.jl")
end
99 changes: 47 additions & 52 deletions src/plotting/RecipesMakie.jl → ext/GLMakieExt/plot.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# https://github.com/SciML/SciMLBase.jl/issues/427
# https://github.com/VarLad/SciMLMakie.jl
using GLMakie: Point2f0

function GLMakie.convert_arguments(::PointBased, contres::AbstractBranchResult, vars = nothing, applytoY = identity, applytoX = identity)
ind1, ind2 = get_plot_vars(contres, vars)
return ([Point2f0(i, j) for (i, j) in zip(map(applytoX, getproperty(contres.branch, ind1)), map(applytoY, getproperty(contres.branch, ind2)))],)
Expand Down Expand Up @@ -35,72 +35,72 @@ function plot!(ax1, contres::AbstractBranchResult;
end
ax1.xlabel = xlab
ax1.ylabel = ylab

# display bifurcation points
bifpt = filter(x -> (x.type != :none) && (x.type != :endpoint) && (plotfold || x.type != :fold) && (x.idx <= length(contres)-1), contres.specialpoint)
if length(bifpt) >= 1 && plotspecialpoints #&& (ind1 == :param)
if filterspecialpoints == true
bifpt = filterBifurcations(bifpt)
end
scatter!(ax1,
[applytoX(getproperty(contres[pt.idx], ind1)) for pt in bifpt],
[applytoY(getproperty(contres[pt.idx], ind2)) for pt in bifpt];
marker = map(x -> (x.status == :guess) && (plotcirclesbif==false) ? :rect : :circle, bifpt),
markersize = 10,
color = map(x -> colorbif[x.type], bifpt),
)
[applytoX(getproperty(contres[pt.idx], ind1)) for pt in bifpt],
[applytoY(getproperty(contres[pt.idx], ind2)) for pt in bifpt];
marker = map(x -> (x.status == :guess) && (plotcirclesbif==false) ? :rect : :circle, bifpt),
markersize = 10,
color = map(x -> colorbif[x.type], bifpt),
)
end

# add legend for bifurcation points
if putspecialptlegend && length(bifpt) >= 1
bps = unique(x -> x.type, [pt for pt in bifpt if (pt.type != :none && (plotfold || pt.type != :fold))])
(length(bps) == 0) && return
for pt in bps
scatter!(ax1,
[applytoX(getproperty(contres[pt.idx], ind1))],
[applytoY(getproperty(contres[pt.idx], ind2))];
color = get_color(pt.type),
markersize = 10,
label = "$(pt.type)")
[applytoX(getproperty(contres[pt.idx], ind1))],
[applytoY(getproperty(contres[pt.idx], ind2))];
color = get_color(pt.type),
markersize = 10,
label = "$(pt.type)")
end
# GLMakie.axislegend(ax1, merge = true, unique = true)
GLMakie.axislegend(ax1, merge = true, unique = true)
end
ax1
end

function plot_branch_cont(contres::ContResult,
state,
iter,
plotuserfunction;
plotfold = false,
plotstability = true,
plotspecialpoints = true,
putspecialptlegend = true,
filterspecialpoints = false,
linewidthunstable = 1.0,
linewidthstable = 3.0linewidthunstable,
plotcirclesbif = true,
applytoY = identity,
applytoX = identity)
state,
iter,
plotuserfunction;
plotfold = false,
plotstability = true,
plotspecialpoints = true,
putspecialptlegend = true,
filterspecialpoints = false,
linewidthunstable = 1.0,
linewidthstable = 3.0linewidthunstable,
plotcirclesbif = true,
applytoY = identity,
applytoX = identity)
sol = getsolution(state)
if length(contres) == 0; return ; end

# names for axis labels
ind1, ind2 = get_plot_vars(contres, nothing)
xlab, ylab = get_axis_labels(ind1, ind2, contres)

# stability linewidth
linewidth = linewidthunstable
if _hasstability(contres) && plotstability
linewidth = map(x -> isodd(x) ? linewidthstable : linewidthunstable, contres.stable)
end

fig = Figure(size = (1200, 700))
ax1 = fig[1:2, 1] = Axis(fig, xlabel = String(xlab), ylabel = String(ylab), tellheight = true)

ax2 = fig[1, 2] = Axis(fig, xlabel = "step [$(state.step)]", ylabel = String(xlab))
lines!(ax2, contres.step, contres.param, linewidth = linewidth)

if compute_eigenelements(iter)
eigvals = contres.eig[end].eigenvals
ax_ev = fig[3, 1:2] = Axis(fig, xlabel = "", ylabel = "")
Expand All @@ -113,23 +113,23 @@ function plot_branch_cont(contres::ContResult,
end
lines!(ax_ev, [0, 0], [maxIm, minIm], color = :blue, linewidth = linewidthunstable)
end

# plot arrow to indicate the order of computation
if length(contres) > 1
x = contres.branch[end].param
y = getproperty(contres.branch,1)[end]
u = contres.branch[end].param - contres.branch[end-1].param
v = getproperty(contres.branch,1)[end]-getproperty(contres.branch,1)[end-1]
v = getproperty(contres.branch,1)[end] - getproperty(contres.branch,1)[end-1]
GLMakie.arrows!(ax1, [x], [y], [u], [v], color = :green, arrowsize = 20,)
end

plot!(ax1, contres; plotfold, plotstability, plotspecialpoints, putspecialptlegend, filterspecialpoints, linewidthunstable, linewidthstable, plotcirclesbif, applytoY, applytoX)

if isnothing(plotuserfunction) == false
ax_perso = fig[2, 2] = Axis(fig, tellheight = true)
plotuserfunction(ax_perso, sol.u, sol.p; ax1 = ax1)
end

display(fig)
fig
end
Expand All @@ -142,30 +142,25 @@ function plot(contres::AbstractBranchResult; kP...)

fig = Figure()
ax1 = fig[1, 1] = Axis(fig, xlabel = String(xlab), ylabel = String(ylab), tellheight = true)

plot!(ax1, contres; kP...)

plot!(ax1, contres; kP...)
display(fig)
fig, ax1
end

plot(brdc::DCResult; kP...) = plot(brdc.branches...; kP...)

function plot(brs::AbstractBranchResult...;
branchlabel = fill("", length(brs)),
kP...)
branchlabel = ["$i" for i=1:length(brs)],
kP...)
if length(brs) == 0; return ;end
fig = Figure()
ax1 = fig[1, 1] = Axis(fig)

for (id, contres) in pairs(brs)

ind1, ind2 = get_plot_vars(contres, nothing)
xlab, ylab = get_axis_labels(ind1, ind2, contres)

plot!(ax1, contres; branchlabel = branchlabel[id], kP...)

end
# GLMakie.axislegend(ax1, merge = true, unique = true)

GLMakie.axislegend(ax1, merge = true, unique = true)
display(fig)
fig, ax1
end
Expand Down Expand Up @@ -218,7 +213,7 @@ end
function plot(bd::BifDiagNode; code = (), level = (-Inf, Inf), k...)
if ~hasbranch(bd); return; end

fig = Figure(size = (1200, 700))
fig = Figure()
ax = fig[1, 1] = Axis(fig)

_plot_bifdiag_makie!(ax, bd; code, level, k...)
Expand All @@ -244,4 +239,4 @@ function _plot_bifdiag_makie!(ax, bd::Vector{BifDiagNode}; code = (), level = (-
_plot_bifdiag_makie!(ax, b; code, level, k... )
end
end
####################################################################################################
####################################################################################################
5 changes: 5 additions & 0 deletions ext/JLD2Ext/JLD2Ext.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module JLD2Ext
using JLD2, BifurcationKit
import BifurcationKit: save_to_file, AbstractContinuationIterable, ContResult
include("save.jl")
end
50 changes: 50 additions & 0 deletions ext/JLD2Ext/save.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Save solution / data in JLD2 file
- `filename` is for example "example.jld2"
- `sol` is the solution
- `p` is the parameter
- `i` is the index of the solution to be saved
"""
function save_to_file(iter::AbstractContinuationIterable, sol, p, i::Int64, br::ContResult)
if iter.contparams.save_to_file == false; return nothing; end
filename = iter.filename
# this allows to save two branches forward/backward in case
# bothside = true is passed to continuation
fd = iter.contparams.ds >=0 ? "fw" : "bw"

# create a group in the JLD format
jldopen(filename*".jld2", "a+") do file
if haskey(file, "sol-$fd-$i")
delete!(file, "sol-$fd-$i")
end
mygroup = JLD2.Group(file, "sol-$fd-$i")
mygroup["sol"] = sol
mygroup["param"] = p
end

jldopen(filename*"-branch.jld2", "a+") do file
if haskey(file, "branch"*fd)
delete!(file, "branch"*fd)
end
file["branch"*fd] = br
end
end

# final save of branch, in case bothsided = true is used
function save_to_file(iter::AbstractContinuationIterable, br::ContResult)
if iter.contparams.save_to_file == false; return nothing; end
filename = iter.filename

jldopen(filename*"-branch.jld2", "a+") do file
if haskey(file, "branchfw")
delete!(file, "branchfw")
end
if haskey(file, "branchbw")
delete!(file, "branchbw")
end
if haskey(file, "branch")
delete!(file, "branch")
end
file["branch"] = br
end
end
22 changes: 22 additions & 0 deletions ext/PlotsExt/PlotsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module PlotsExt
using Plots, BifurcationKit
import BifurcationKit: plot_branch_cont,
plot_periodic_potrap,
plot_periodic_shooting!,
plot_periodic_shooting,
plot_eigenvals,
compute_eigenelements,
get_lens_symbol,
_getfirstusertype,
ContResult,
DCResult,
AbstractBranchResult,
get_plot_vars,
get_axis_labels,
_hasstability,
filter_bifurcations,
get_color,
AbstractResult

include("plot.jl")
end
Loading

0 comments on commit 0e6ca9a

Please sign in to comment.