Skip to content

Commit

Permalink
Option to terminate solve by tolerance
Browse files Browse the repository at this point in the history
  • Loading branch information
bclyons12 committed Jun 14, 2023
1 parent ad03349 commit 66e9007
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/solve.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
function solve(shot::Shot, its::Integer;
function solve(shot::Shot, its::Integer; tol::Real=0.0, debug::Bool=false,
P=nothing, dP_dψ=nothing, F_dF_dψ=nothing, Jt_R=nothing, Jt=nothing,
Pbnd=shot.Pbnd, Fbnd=shot.Fbnd, debug=false)
Pbnd=shot.Pbnd, Fbnd=shot.Fbnd)
refill = Shot(shot; P, dP_dψ, F_dF_dψ, Jt_R, Jt, Pbnd, Fbnd)
return solve!(refill, its; debug)
return solve!(refill, its; tol, debug)
end

function solve!(refill::Shot, its::Integer; debug=false)
function solve!(refill::Shot, its::Integer; tol::Real=0.0, debug::Bool=false)
Fis, dFis, Fos, Ps = fft_prealloc_threaded(refill.M)
A = preallocate_Astar(refill)
L = 2 * refill.N * (2 * refill.M + 1)
B = zeros(L)
C = zeros(L)
Ψold = 0.0
warn_concentric = false
if debug
_, _, Ψold = find_axis(refill)
end
Expand All @@ -35,18 +36,32 @@ function solve!(refill::Shot, its::Integer; debug=false)
(debug && i == 1) && println(" Trying full refit for first iteration")
try
refill = refit(refill, Ψaxis, Raxis, Zaxis)
warn_concentric = false
catch err
isa(err, InterruptException) && rethrow(err)
println(" Warning: Fell back to concentric surfaces due to ", typeof(err))
warn_concentric = true
if debug
println(" Warning: Fit for iteration $i fell back to concentric surfaces due to ", typeof(err))
end
refill = refit_concentric(refill, Raxis, Zaxis)
end
end

error = abs((Ψaxis-Ψold)/Ψaxis)
if debug
println(" Status: Ψaxis = $Ψaxis, Error: ", abs((Ψaxis-Ψold)/Ψaxis))
println(" Status: Ψaxis = $Ψaxis, Error: $error")
Ψold = Ψaxis
end
if error <= tol && i > 1
debug && println("DONE: Successful convergence")
break
end

if i == its && debug
println("DONE: maximum iterations")
end
end
warn_concentric && println("WARNING: Final iteration used concentric surfaces and is likely inaccurate")
return refill
end

Expand Down

0 comments on commit 66e9007

Please sign in to comment.