Skip to content

Commit

Permalink
add tests for slicing functionality in ContResult and Branch objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibault Wildi committed Nov 19, 2024
1 parent addda5a commit cf14395
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ using Base.Threads; println("--> There are ", Threads.nthreads(), " threads")
include("test_linear.jl")
end

@testset "Results" begin
include("test_results.jl")
end

@testset "Newton" begin
include("test_newton.jl")
end
Expand Down
58 changes: 58 additions & 0 deletions test/test_results.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Test, BifurcationKit
const BK = BifurcationKit

# Simple Test problem (Pitchfork bifurcation) to generate a ContResult and a Branch object
function f(u, p)
return p.r .* u - u .^ 3
end
p = (r=-1.0,)
u0 = [0.0]
prob = BK.BifurcationProblem(f, u0, p, (@optic _.r))

@testset "ContResult" begin
opt = BK.ContinuationPar(p_min=-1.0, p_max=1.0)
contres = BK.continuation(prob, PALC(), opt)
@assert typeof(contres) <: BK.ContResult
bp = contres.specialpoint[1] # pitchfork bifurcation

# Test slicing of ContResult object
@test contres[1:bp.step+1].specialpoint[1].step == bp.step
@test contres[bp.step+1:end].specialpoint[1].step == bp.step

# Slicing and indexing should match
@test contres[bp.step:bp.step][1].param == contres[bp.step].param

# Recursive slicing should work
@test contres[bp.step:end][1:1][1].param == contres[bp.step:end][1].param

# Slicing should still work when not evey sol/eig is saved
opt = BK.ContinuationPar(opt; detect_bifurcation=1, save_sol_every_step=2, save_eig_every_step=3)
contres = BK.continuation(prob, PALC(), opt)
@assert length(contres) != length(contres.sol) != length(contres.eig)
@test length(contres[1:end]) == length(contres)
@test length(contres[1:end].sol) == length(contres.sol)
@test length(contres[1:end].eig) == length(contres.eig)
end

@testset "Branch" begin
# Test slicing of Branch object
opt = BK.ContinuationPar(p_min=-1.0, p_max=1.0)
contres = BK.continuation(prob, PALC(), opt)
branch = BK.continuation(contres, 1)
@assert typeof(branch) <: BK.Branch
bp = branch.specialpoint[1] # pitchfork bifurcation

# Test slicing of Branch object
@test branch[1:bp.step+1].specialpoint[1].step == bp.step
@test branch[bp.step+1:end].specialpoint[1].step == bp.step

# Slicing and indexing should match
@test branch[bp.step:bp.step][1].param == branch[bp.step].param

# Recursive slicing should work
@test branch[bp.step:end][1:1][1].param == branch[bp.step:end][1].param
end




0 comments on commit cf14395

Please sign in to comment.