Skip to content

Segfault in interpolation #63

@antoine-levitt

Description

@antoine-levitt

I get a segfault when getting results out using VelocityVerlet, in a getindex. I'm guessing overagressive inbounds somewhere? I'm too lazy to do an MWE but the below should reproduce the bug. Changing the integrator to something else fixes it. I'm filing against NBS since that's what I'm using, but I can file to DifferentialEquations.jl if that is more appropriate.

using SPICE
using Downloads: download
using NBodySimulator, StaticArrays

# units: kilometers, seconds, kilograms

const LSK = "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/lsk/naif0012.tls"
const SPK = "https://naif.jpl.nasa.gov/pub/naif/generic_kernels/spk/planets/de440.bsp"
# Download kernels
# time kernel
download(LSK, "naif0012.tls")
download(SPK, "de440.bsp")
download("https://naif.jpl.nasa.gov/pub/naif/generic_kernels/pck/Gravity.tpc", "Gravity.tpc")

furnsh("naif0012.tls")
# ephemeris kernel
furnsh("de440.bsp")
# masses kernel
furnsh("Gravity.tpc")

# Convert the calendar date to ephemeris seconds past J2000
start_date = str2et("Jan 1, 2020")

years_to_observe = 1
dates = collect(range(start_date, start_date + 60*60*24*365*years_to_observe, length=365*years_to_observe))

ref_pos = spkpos.("earth", dates, "J2000", "none", "sun")
ref_pos = [p[1] for p in ref_pos]
ref_pos = reduce(hcat, ref_pos)'

const grav_constant = 6.6743e-11 / 1e9 # m^3 kg^-1 s^-2 -> km^3

planets = ["earth_barycenter", "sun"]
# planets = ["earth_barycenter", "Mercury_barycenter", "Venus_barycenter", "Mars_barycenter", "Jupiter_barycenter", "Saturn_barycenter", "Uranus_barycenter", "Neptune_barycenter", "Sun"]
# planets = ("earth_barycenter", "Mercury_barycenter", "Venus_barycenter", "Mars_barycenter", "Jupiter_barycenter", "Saturn_barycenter", "Uranus_barycenter", "Sun")
planets_to_observe = [1]

masses = bodvrd.(planets, "GM") ./ grav_constant
posvel = [spkezr(p, start_date, "J2000", "none", "sun") for p in planets]

bodies = [MassBody(SVector(posvel[i][1][1:3]...), SVector(posvel[i][1][4:6]...), masses[i][1]) for i = 1:length(planets)]
system = GravitationalSystem(bodies, grav_constant)
tspan = (dates[1], dates[end])
simulation = NBodySimulation(system, tspan)
result = run_simulation(simulation, VelocityVerlet(), dt=60*60*24).solution
pos = [result(t)[:, planets_to_observe] .- result(t)[:, end] for t in dates] # observe wrt sun
pos = cat(pos...; dims=3) # α x p x t

function get_ref_pos(t)
    hcat([spkpos(p, t, "J2000", "none", "sun")[1] for p in planets[planets_to_observe]]...)
end
ref_pos = cat(get_ref_pos.(dates)...; dims=3)

println(maximum(abs, pos-ref_pos))

# using PyPlot
# plot3D(pos[1, 1, :], pos[2, 1, :], pos[3, 1, :])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions