Got MBH working! It can find better-than-basic-spiral trajectories

This commit is contained in:
Connor
2021-09-02 17:21:40 -06:00
parent 58ad3c5293
commit 83886188db
8 changed files with 318 additions and 381 deletions

View File

@@ -1,6 +1,6 @@
@testset "Find Closest" begin
using JuMP
using NLsolve, PlotlyJS
# Initial Setup
sc = Sc("test")
@@ -9,10 +9,11 @@
i = rand(0.01:0.01:π/6)
T = 2π*(a^3/μs["Earth"])
prop_time = 2T
n = 30
n = 20
# A simple orbit raising
start = oe_to_xyz([ a, e, i, 0., 0., 0. ], μs["Earth"])
# T_craft = hcat(repeat([0.6], n), repeat([0.], n), repeat([0.], n))
Tx, Ty, Tz = conv_T(repeat([0.6], n), repeat([0.], n), repeat([0.], n),
start,
sc.mass,
@@ -23,35 +24,26 @@
new_T = 2π*(xyz_to_oe(final, μs["Earth"])[1]^3/μs["Earth"])
# This should be close enough to 0.6
Tx, Ty, Tz = conv_T(repeat([0.6], n), repeat([0.], n), repeat([0.], n),
Tx, Ty, Tz = conv_T(repeat([0.59], n), repeat([0.01], n), repeat([0.], n),
start,
sc.mass,
sc,
prop_time,
μs["Earth"])
result, solution = nlp_solve(start,
final,
sc,
μs["Earth"],
0.0,
prop_time,
Tx,
Ty,
Tz)
# solver_options=("max_cpu_time" => 30.))
result = nlp_solve(start, final, sc, μs["Earth"], 0.0, prop_time, hcat(Tx, Ty, Tz))
# Test and plot
@test JuMP.termination_status(result) == MOI.OPTIMAL
@test converged(result)
path1 = prop(zeros((100,3)), start, sc, μs["Earth"], T)[1]
path2, mass, calc_final = prop(treat_inputs(JuMP.value.(solution)), start, sc, μs["Earth"], prop_time)
path2, mass, calc_final = prop(tanh.(result.zero), start, sc, μs["Earth"], prop_time)
path3 = prop(zeros((100,3)), calc_final, sc, μs["Earth"], new_T)[1]
path4 = prop(zeros((100,3)), final, sc, μs["Earth"], new_T)[1]
savefig(plot_orbits([path1, path2, path3, path4],
labels=["initial", "transit", "after transit", "final"],
colors=["#FFFFFF","#FF4444","#44FF44","#4444FF"]),
"../plots/find_closest_test.html")
# if termination_status(result) == :OPTIMAL
# @test norm(calc_final - final) < 1e-4
# end
if converged(result)
@test norm(calc_final - final) < 1e-4
end
end

View File

@@ -8,22 +8,63 @@
e = rand(0.01:0.01:0.5)
i = rand(0.01:0.01:π/6)
T = 2π*(a^3/μs["Earth"])
prop_time = 2T
n = 25
prop_time = 0.75T
n = 10
# A simple orbit raising
# start = oe_to_xyz([ a, e, i, 0., 0., 0. ], μs["Earth"])
# ΔVs = repeat([0.6, 0., 0.]', outer=(n,1))
# final = prop(ΔVs, start, sc, μs["Earth"], prop_time)[1][end,:]
# new_T = 2π*√(xyz_to_oe(final, μs["Earth"])[1]^3/μs["Earth"])
start = oe_to_xyz([ a, e, i, 0., 0., 0. ], μs["Earth"])
# T_craft = hcat(repeat([0.6], n), repeat([0.], n), repeat([0.], n))
Tx, Ty, Tz = conv_T(repeat([0.8], n), repeat([0.], n), repeat([0.], n),
start,
sc.mass,
sc,
prop_time,
μs["Earth"])
nominal_path, normal_mass, final = prop(hcat(Tx, Ty, Tz), start, sc, μs["Earth"], prop_time)
new_T = 2π*(xyz_to_oe(final, μs["Earth"])[1]^3/μs["Earth"])
# This should be close enough to 0.6
# best, archive = mbh(start, final, sc, μs["Earth"], 0.0, prop_time, n)
# Find the best solution
best, archive = mbh(start,
final,
sc,
μs["Earth"],
0.0,
prop_time,
n,
num_iters=5,
patience_level=50,
verbose=true)
# Test and plot
@test_skip converged(best)
#for path in archive
# @test_skip converged(path)
#end
@test converged(best)
transit, best_masses, calc_final = prop(tanh.(best.zero), start, sc, μs["Earth"], prop_time)
initial_path = prop(zeros((100,3)), start, sc, μs["Earth"], T)[1]
after_transit = prop(zeros((100,3)), calc_final, sc, μs["Earth"], new_T)[1]
final_path = prop(zeros((100,3)), final, sc, μs["Earth"], new_T)[1]
savefig(plot_orbits([initial_path, nominal_path, final_path],
labels=["initial", "nominal transit", "final"],
colors=["#FF4444","#44FF44","#4444FF"]),
"../plots/mbh_nominal.html")
savefig(plot_orbits([initial_path, transit, after_transit, final_path],
labels=["initial", "transit", "after transit", "final"],
colors=["#FFFFFF", "#FF4444","#44FF44","#4444FF"]),
"../plots/mbh_best.html")
i = 0
best_mass = best_masses[end]
nominal_mass = normal_mass[end]
masses = []
for candidate in archive
@test converged(candidate)
path2, cand_ms, calc_final = prop(tanh.(candidate.zero), start, sc, μs["Earth"], prop_time)
push!(masses, cand_ms[end])
@test norm(calc_final - final) < 1e-4
end
@test best_mass == maximum(masses)
# This won't always work since the test is reduced in fidelity,
# but hopefully will usually work:
@test (sc.mass - best_mass) < 1.1 * (sc.mass - nominal_mass)
@show best_mass
@show nominal_mass
end