Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/PALC/continuation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,19 @@ function continuation!(
palc_prediction!(cache, alg, p, solvers, trace)

# Perform correction step
success, hit_bnd = palc_correction!(
success, terminate_continuation = palc_correction!(
cache, alg, p, solvers, dsmin, dsmax, term_callback, analysis_callback, trace
)

if iter >= max_cont_steps
done = true
elseif !success
cache.ret = :Maxiters
elseif !success # If correction step failed, end the process
done = true
elseif success && hit_bnd
# cache ret should be set within correction to determine failure condition (currently this is limited to min stepsize)
elseif success && terminate_continuation # termination is due to callback or hitting bound, consider successful
done = true
# cache ret should be set within correction to determine termination condition
end
end
return nothing
Expand Down
17 changes: 15 additions & 2 deletions src/PALC/correction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ function palc_correction!(
scale_and_clamp_ds!(cache, 0.5, dsmin, dsmax)
end
end
else
else # solve is not successful
if abs(cache.ds) == dsmin
done = true
success = false
cache.ret = :MinimumStepSize # update ret with 'done' condition. This won't be overwritten since success=false (see continuation.jl)
else
# Reduce step-size and reattempt
scale_and_clamp_ds!(cache, 0.5, dsmin, dsmax)
Expand All @@ -137,7 +138,11 @@ function palc_correction!(
success && call!(analysis_callback, cache)

# Handle termination flag
terminate_continuation = !isnan(hit_bnd) || cb_trig
terminate_continuation = !isnan(hit_bnd) || cb_trig # hit bound or triggered callback

if terminate_continuation
set_successful_retcode!(cache, hit_bnd, cb_trig)
end

return success, terminate_continuation
end
Expand Down Expand Up @@ -393,3 +398,11 @@ function palc_correction_jacobian!(J, uλ, p)

return nothing
end

function set_successful_retcode!(cache, hit_bnd, cb_trig)
if isnan(hit_bnd) && cb_trig
cache.ret = :CallbackTermination
elseif !cb_trig
cache.ret = :HitBound
end
end
9 changes: 8 additions & 1 deletion src/PALC/palc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ mutable struct PALCCache{MT<:Union{Matrix{Float64},SparseMatrixCSC{Float64,Int}}
u_0::Vector{Float64}
u_1::Vector{Float64}
u_t::Vector{Float64}

# Return code
ret::Symbol # could also do a custom type for pretty viewing like sciml but symbols should work just fine
end

function PALCCache(
Expand Down Expand Up @@ -118,7 +121,7 @@ function PALCCache(
u_0 = similar(u0)
u_1 = similar(u0)
u_t = similar(u0)

ret = :None # init return code (should always be overwritten)
return PALCCache{Matrix{Float64}}(
ds0,
br,
Expand All @@ -139,6 +142,7 @@ function PALCCache(
u_0,
u_1,
u_t,
ret
)
end
function PALCCache(
Expand Down Expand Up @@ -183,6 +187,8 @@ function PALCCache(
u_1 = similar(u0)
u_t = similar(u0)

ret = :None # init return code

return PALCCache{SparseMatrixCSC{Float64,Int}}(
ds0,
br,
Expand All @@ -203,6 +209,7 @@ function PALCCache(
u_0,
u_1,
u_t,
ret
)
end

Expand Down
82 changes: 82 additions & 0 deletions test/test_retcodes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using SimpleContinuation
using Test

const SC = SimpleContinuation

function scalar_test_fun!(F, x, λ)
F[1] = (λ + x[1] - x[1]^3 / 3)
end

function scalar_fun_ujac!(J, F, x, λ)
scalar_test_fun!(F, x, λ)
J[1,1] = 1 - x[1]^2
end

function scalar_fun_jac!(J, F, x, λ)
scalar_test_fun!(F, x, λ)
J[1,1] = 1 - x[1]^2
J[1,2] = 1
end

u0 = [-2.]
λ0 = -1.

f_min_time = (F, u, s) -> scalar_test_fun!(F, u, s)
Jz_min_time = (J, F, u, s) -> scalar_fun_ujac!(J, F, u, s)
J_min_time = (J, F, u, s) -> scalar_fun_jac!(J, F, u, s)

# Should term due to callback
cache = continuation(
ContinuationProblem(
ContinuationFunction{Val{true}}(f_min_time, Jz_min_time, J_min_time),
u0,
λ0,
(λ0, 1.0),
),
PALC(; predicter=Bordered());
both_sides=false,
ds0=0.01,
dsmin=1e-1,
dsmax=0.01,
max_cont_steps=10e3,
#trace=ContinuationSteps(),
term_callback=FoldBifurcationTerminationCallback(),
)
@test cache.ret == :CallbackTermination

# Should term due to hitting boundary
cache = continuation(
ContinuationProblem(
ContinuationFunction{Val{true}}(f_min_time, Jz_min_time, J_min_time),
u0,
λ0,
(λ0, 1.0),
),
PALC(; predicter=Bordered());
both_sides=false,
ds0=0.01,
dsmin=1e-1,
dsmax=0.01,
max_cont_steps=10e3,
#trace=ContinuationSteps(),
)
@test cache.ret == :HitBound

# Should term due to maxiters
cache = continuation(
ContinuationProblem(
ContinuationFunction{Val{true}}(f_min_time, Jz_min_time, J_min_time),
u0,
λ0,
(λ0, 1.0),
),
PALC(; predicter=Bordered());
both_sides=false,
ds0=0.01,
dsmin=1e-1,
dsmax=0.01,
max_cont_steps=10,
#trace=ContinuationSteps(),
)
@test cache.ret == :Maxiters

Loading