Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rveltz committed Jan 12, 2025
1 parent bce89e3 commit 819116b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
14 changes: 13 additions & 1 deletion src/periodicorbit/PeriodicOrbitCollocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,18 @@ function analytical_jacobian_sparse(coll::PeriodicOrbitOCollProblem,
block_to_sparse(jacBlock)
end

function jacobian_poocoll_block(coll::PeriodicOrbitOCollProblem,
u::AbstractVector,
pars;
kwargs...)
n, m, Ntst = size(coll)
blocks = n * ones(Int64, 1 + m * Ntst + 1); blocks[end] = 1
n_blocks = length(blocks)
J = BlockArray(zeros(length(u), length(u)), blocks, blocks)
jacobian_poocoll_block!(J, coll, u, pars; kwargs...)
return J
end

@views function jacobian_poocoll_block!(J,
coll::PeriodicOrbitOCollProblem,
u::AbstractVector{𝒯},
Expand Down Expand Up @@ -933,7 +945,7 @@ function _newton_pocoll(probPO::PeriodicOrbitOCollProblem,
_J = analytical_jacobian_sparse(probPO, orbitguess, par)
jac = (x, p) -> analytical_jacobian!(_J, probPO, x, p)
else
jac = (x, p) -> ForwardDiff.jacobian(z -> probPO(z, p), x)
jac = (x, p) -> ForwardDiff.jacobian(z -> residual(probPO, z, p), x)
end

if options.linsolver isa COPLS
Expand Down
2 changes: 1 addition & 1 deletion src/periodicorbit/codim2/codim2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function _continuation(gh::Bautin, br::AbstractResult{Tkind, Tprob},
probsh_fold = BifurcationProblem((x, p) -> residual(pbwrap, x, p), orbitguess, getparams(pbwrap), getlens(pbwrap);
J = (x, p) -> jacobian(pbwrap, x, p),
Jᵗ = Jᵗ,
d2F = (x, p, dx1, dx2) -> d2PO(z -> probPO(z, p), x, dx1, dx2),
d2F = (x, p, dx1, dx2) -> d2PO(z -> residual(probPO, z, p), x, dx1, dx2),
record_from_solution = _recordsol,
plot_solution = _plotsol,
)
Expand Down
2 changes: 1 addition & 1 deletion test/cop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ prob_col = PeriodicOrbitOCollProblem(Ntst, m;
= rand(N*( 1 + m * Ntst)))
_ci = generate_solution(prob_col, t->cos(t) .* ones(N), 2pi);
#####################################################
Jcofd = ForwardDiff.jacobian(z->prob_col(z, par_sl), _ci);
Jcofd = ForwardDiff.jacobian(z->BK.residual(prob_col,z, par_sl), _ci);
Jco = BK.analytical_jacobian(prob_col, _ci, par_sl);

_rhs = rand(size(Jco, 1))
Expand Down
18 changes: 9 additions & 9 deletions test/stuartLandauCollocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ _ci = BK.generate_solution(prob_col, _orbit, 1.)
BK.get_periodic_orbit(prob_col, _ci, par_sl)
BK.getmaximum(prob_col, _ci, par_sl)
@test BK.(sin, Val(2))(0.) == 0
prob_col(_ci, par_sl) #|> scatter
BK.residual(prob_col, _ci, par_sl) #|> scatter
BK.get_time_slices(prob_col, _ci)

# interpolate solution
Expand Down Expand Up @@ -156,13 +156,13 @@ prob_col.ϕ[2] = 1 #phase condition

_orbit(t) = [cos(t), sin(t)] * sqrt(par_sl.r/par_sl.c3)
_ci = BK.generate_solution(prob_col, _orbit, 2pi)
prob_col(_ci, par_sl)
@test prob_col(_ci, par_sl)[1:end-1] |> norminf < 1e-7
BK.residual(prob_col, _ci, par_sl)
@test BK.residual(prob_col, _ci, par_sl)[1:end-1] |> norminf < 1e-7

prob_coll_ip = @set prob_col.prob_vf = probsl_ip

@time prob_col(_ci, par_sl);
@time prob_coll_ip(_ci, par_sl);
@time BK.residual(prob_col, _ci, par_sl);
@time BK.residual(prob_coll_ip, _ci, par_sl);

# test precision of generated solution
_sol = BK.get_periodic_orbit(prob_col, _ci, nothing)
Expand Down Expand Up @@ -222,8 +222,8 @@ nullvf(x,p) = zero(x)
prob0 = BifurcationProblem(nullvf, zeros(N), par_hopf, (@optic _.r))
prob_col = BK.PeriodicOrbitOCollProblem(Ntst, m; prob_vf = prob0, N = N, ϕ = rand(N*( 1 + m * Ntst)), xπ = rand(N*( 1 + m * Ntst)))
_ci = BK.generate_solution(prob_col, t->cos(t) .* ones(N), 2pi);
prob_col(_ci, par_sl);
Jcofd = ForwardDiff.jacobian(z->prob_col(z, par_sl), _ci);
BK.residual(prob_col,_ci, par_sl);
Jcofd = ForwardDiff.jacobian(z -> BK.residual(prob_col, z, par_sl), _ci);
D = @time BK.analytical_jacobian(prob_col, _ci, par_sl); #0.000121 seconds (341 allocations: 156.516 KiB)
@test norminf(Jcofd - D) < 1e-15

Expand All @@ -236,7 +236,7 @@ idvf(x,p) = _al*x
prob_ana = BifurcationProblem(idvf, zeros(N), par_hopf, (@optic _.r) ; J = (x,p) -> _al)
prob_col = BK.PeriodicOrbitOCollProblem(Ntst, m; prob_vf = prob_ana, N = N, ϕ = rand(N*( 1 + m * Ntst)), xπ = rand(N*( 1 + m * Ntst)))
_ci = BK.generate_solution(prob_col, t->cos(t) .* ones(N), 2pi);
Jcofd = ForwardDiff.jacobian(z -> prob_col(z, par_sl), _ci);
Jcofd = ForwardDiff.jacobian(z -> BK.residual(prob_col, z, par_sl), _ci);
Jco = BK.analytical_jacobian(prob_col, _ci, par_sl); # 0.004388 seconds (573 allocations: 60.124 MiB)
@test norminf(Jcofd - Jco) < 1e-15

Expand All @@ -247,7 +247,7 @@ Ntst = 3
m = 2
prob_col = BK.PeriodicOrbitOCollProblem(Ntst, m; prob_vf = probsl, N = N, ϕ = rand(N*( 1 + m * Ntst)), xπ = rand(N*( 1 + m * Ntst)))
_ci = BK.generate_solution(prob_col, t->cos(t) .* ones(N), 2pi);
Jcofd = ForwardDiff.jacobian(z->prob_col(z, par_sl), _ci);
Jcofd = ForwardDiff.jacobian(z->BK.residual(prob_col, z, par_sl), _ci);
Jco = @time BK.analytical_jacobian(prob_col, _ci, par_sl);
Jco_bk = @time BK.jacobian_poocoll_block(prob_col, _ci, par_sl);
@test norminf(Jcofd - Jco) < 1e-14
Expand Down
4 changes: 2 additions & 2 deletions test/testHopfMA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ prob = BifurcationKit.BifurcationProblem(Fbru!, sol0, par_bru, (@optic _.l);

poTrap = PeriodicOrbitTrapProblem(prob, real.(vec_hopf), hopfpt.u, M, 2n )

jac_PO_fd = BK.finite_differences(x -> poTrap(x, (@set par_bru.l = l_hopf + 0.01)), orbitguess_f)
jac_PO_fd = BK.finite_differences(x -> BK.residual(poTrap, x, (@set par_bru.l = l_hopf + 0.01)), orbitguess_f)
jac_PO_sp = poTrap(Val(:JacFullSparse), orbitguess_f, (@set par_bru.l = l_hopf + 0.01))

# test of the Jacobian for PeriodicOrbit via Finite differences VS the FD associated jacobian
Expand All @@ -218,7 +218,7 @@ BK.get_time_diff(poTrap, orbitguess_f)
# BK.Jc(poTrap, orbitguess_f, par_bru, orbitguess_f)

# newton to find Periodic orbit
_prob = BK.BifurcationProblem((x, p) -> poTrap(x, p), copy(orbitguess_f), (@set par_bru.l = l_hopf + 0.01); J = (x, p) -> poTrap(Val(:JacFullSparse),x,p))
_prob = BK.BifurcationProblem((x, p) -> BK.residual(poTrap, x, p), copy(orbitguess_f), (@set par_bru.l = l_hopf + 0.01); J = (x, p) -> poTrap(Val(:JacFullSparse),x,p))
opt_po = NewtonPar(tol = 1e-8, max_iterations = 150)
outpo_f = @time BK.solve(_prob, Newton(), opt_po)
@test BK.converged(outpo_f)
Expand Down
26 changes: 13 additions & 13 deletions test/test_potrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ pbi = PeriodicOrbitTrapProblem(
@test BK.isinplace(pb) == false
# BK.POTrapFunctional(pb, res, orbitguess_f)
# BK.POTrapFunctional(pbi, res, orbitguess_f)
res = pb(orbitguess_f, par)
resg = pbg(orbitguess_f, par)
resi = pbi(orbitguess_f, par)
res = BK.residual(pb, orbitguess_f, par)
resg = BK.residual(pbg, orbitguess_f, par)
resi = BK.residual(pbi, orbitguess_f, par)
@test res == resi
@test res == resg

Expand All @@ -43,7 +43,7 @@ resi = pbi(orbitguess_f, par, orbitguess_f)
@test res == resi
@test res == resg

BK.potrap_functional!(pbi, resi, orbitguess_f, par)
BK.residual!(pbi, resi, orbitguess_f, par)
BK.potrap_functional_jac!(pbi, resi, orbitguess_f, par, orbitguess_f)
@test res == resi

Expand Down Expand Up @@ -147,7 +147,7 @@ function _dfunctional(poPb, u0, p, du)
end


res = pb(orbitguess_f, par)
res = BK.residual(pb, orbitguess_f, par)
_res = _functional(pb, orbitguess_f, par)
@test res _res

Expand All @@ -158,7 +158,7 @@ _res = _dfunctional(pb, orbitguess_f, par, _du)

# with mass matrix
pbmass = @set pb.massmatrix = spdiagm( 0 => rand(pb.N))
res = pbmass(orbitguess_f, par)
res = BK.residual(pbmass, orbitguess_f, par)
_res = _functional(pbmass, orbitguess_f, par)
@test res _res

Expand All @@ -177,7 +177,7 @@ pbsp = PeriodicOrbitTrapProblem(
10)
orbitguess_f = rand(2n*10+1)
dorbit = rand(2n*10+1)
Jfd = sparse( ForwardDiff.jacobian(x -> pbsp(x, par), orbitguess_f) )
Jfd = sparse( ForwardDiff.jacobian(x -> BK.residual(pbsp,x, par), orbitguess_f) )
Jan = pbsp(Val(:JacFullSparse), orbitguess_f, par)
@test norm(Jfd - Jan, Inf) < 1e-6

Expand All @@ -199,7 +199,7 @@ Jan = pbsp(Val(:JacCyclicSparse), orbitguess_f, par)

# we update the section
BK.updatesection!(pbsp, rand(2n*10+1), par)
Jfd2 = sparse( ForwardDiff.jacobian(x -> pbsp(x, par), orbitguess_f) )
Jfd2 = sparse( ForwardDiff.jacobian(x -> BK.residual(pbsp, x, par), orbitguess_f) )
Jan2 = pbsp(Val(:JacFullSparse), orbitguess_f, par)
@test Jan2 != Jan
@test norm(Jfd2 - Jan2, Inf) < 1e-6
Expand All @@ -211,7 +211,7 @@ Jan = pbsp(Val(:JacCyclicSparse), orbitguess_f, par)
##########################
#### idem with mass matrix
pbsp_mass = @set pbsp.massmatrix = massmatrix = spdiagm( 0 => rand(pbsp.N))
Jfd = sparse( ForwardDiff.jacobian(x -> pbsp_mass(x, par), orbitguess_f) )
Jfd = sparse( ForwardDiff.jacobian(x -> BK.residual(pbsp_mass, x, par), orbitguess_f) )
Jan = pbsp_mass(Val(:JacFullSparse), orbitguess_f, par)
@test norm(Jfd - Jan, Inf) < 1e-6

Expand All @@ -229,11 +229,11 @@ pbsp_mass(Val(:JacFullSparseInplace), Jan2, orbitguess_f, par, _indx)
@test norm(Jan2 - Jan, Inf) < 1e-6

Jan = pbsp_mass(Val(:JacCyclicSparse), orbitguess_f, par)
@test norm(Jfd[1:size(Jan,1),1:size(Jan,1)] - Jan, Inf) < 1e-6
@test norm(Jfd[1:size(Jan,1), 1:size(Jan,1)] - Jan, Inf) < 1e-6

# we update the section
BK.updatesection!(pbsp_mass, rand(2n*10+1), par)
Jfd2 = sparse( ForwardDiff.jacobian(x -> pbsp_mass(x, par), orbitguess_f) )
Jfd2 = sparse( ForwardDiff.jacobian(x -> BK.residual(pbsp_mass, x, par), orbitguess_f) )
Jan2 = pbsp_mass(Val(:JacFullSparse), orbitguess_f, par)
@test Jan2 != Jan
@test norm(Jfd2 - Jan2, Inf) < 1e-6
Expand Down Expand Up @@ -315,5 +315,5 @@ BK.get_periodic_orbit(pbspti, orbitguess_f, par)

@test pbspti. pbsp.
@test pbspti.ϕ pbsp.ϕ
pbspti(orbitguess_f, par)
@test pbsp(orbitguess_f, par) pbspti(orbitguess_f, par)
BK.residual(pbspti, orbitguess_f, par)
@test BK.residual(pbsp, orbitguess_f, par) BK.residual(pbspti, orbitguess_f, par)

0 comments on commit 819116b

Please sign in to comment.