Skip to content

Instantly share code, notes, and snippets.

@dronir
Created March 4, 2013 17:56
Show Gist options
  • Select an option

  • Save dronir/5084111 to your computer and use it in GitHub Desktop.

Select an option

Save dronir/5084111 to your computer and use it in GitHub Desktop.
Pluto simulation v0.1
module Integrator
export PlutoSim
const G = 1.48811382e-34 # G in units of AU and day
function potential{T<:Real}(mass::Vector{T}, r::Matrix{T})
N = size(r,1)
out = zeros(N,3)
d = zeros(1,3)
a = zeros(1,3)
for i = 1:N
for j = i+1:N
d[1] = r[j,1]-r[i,1]
d[2] = r[j,2]-r[i,2]
d[3] = r[j,3]-r[i,3]
F = G/norm(d)^3
a[1] = d[1] * F
a[2] = d[2] * F
a[3] = d[3] * F
out[i,1] += mass[j] * a[1]
out[i,2] += mass[j] * a[2]
out[i,3] += mass[j] * a[3]
out[j,1] -= mass[i] * a[1]
out[j,2] -= mass[i] * a[2]
out[j,3] -= mass[i] * a[3]
end
end
out
end
# Velocity Verlet
function Verlet{T<:Real}(mass::Vector{T}, r0::Matrix{T}, v0::Matrix{T}, h::Real)
a0 = potential(mass, r0)
r_new = r0 + v0*h + 0.5*a0*h^2
v_new = v0 + 0.5*(a0 + potential(mass, r_new)) * h
return (r_new, v_new)
end
function PlutoSim(N::Integer, h::Real)
r = zeros(6,3)
v = zeros(6,3)
mass = zeros(6)
# Set masses
mass[1] = 1.988435e30 # Sun
mass[2] = 1.8988e27 # Jupiter
mass[3] = 5.685e26 # Saturn
mass[4] = 8.6625e25 # Uranus
mass[5] = 1.0278e26 # Neptune
mass[6] = 1.314e22 # Pluto
# Sun
r[1,:] = [-9.001119860001391E-04, -2.518569331216286E-03, -5.035993149103694E-05]
v[1,:] = [ 6.249336869691155E-06, -7.202874592454979E-07, -1.368159714329672E-07]
# Jupiter
r[2,:] = [ 9.637969147947649E-01, 4.988694611226984E+00, -4.236695676623525E-02]
v[2,:] = [-7.500214649417347E-03, 1.791097950812253E-03, 1.603945618163088E-04]
# Saturn
r[3,:] = [-7.901119375041307E+00, -5.800240922607197E+00, 4.152972681975652E-01]
v[3,:] = [ 2.998916003562968E-03, -4.511136746229775E-03, -4.061079792754435E-05]
# Uranus
r[4,:] = [ 1.985459827926987E+01, 2.801201477916861E+00, -2.468196041542967E-01]
v[4,:] = [-5.782111510118402E-04, 3.711193104939768E-03, 2.130487704096845E-05]
# Neptune
r[5,:] = [ 2.664809431198752E+01, -1.375183029235338E+01, -3.309310983483438E-01]
v[5,:] = [ 1.419153776960717E-03, 2.808496228357725E-03, -9.056396274034148E-05]
# Pluto
r[6,:] = [5.303572815981638E+00, -3.190544944470667E+01, 1.879962140526394E+00]
v[6,:] = [3.156551097775996E-03, -1.132909607403660E-04, -9.009397396467777E-04]
NSteps = N
result = zeros(12,fld(NSteps, 100))
println(size(result))
println("Begin.")
tic()
sample = 0
t = 0.0
for i = 1:NSteps
if i % 100 == 0
sample += 1
result[ 1,sample] = r[1,1]
result[ 2,sample] = r[1,2]
result[ 3,sample] = r[2,1]
result[ 4,sample] = r[2,2]
result[ 5,sample] = r[3,1]
result[ 6,sample] = r[3,2]
result[ 7,sample] = r[4,1]
result[ 8,sample] = r[4,2]
result[ 9,sample] = r[5,1]
result[10,sample] = r[5,2]
result[11,sample] = r[6,1]
result[12,sample] = r[6,2]
end
r,v = Verlet(mass, r, v, h)
end
toc()
writecsv("output.txt", result)
run(`python plotorbits.py`)
end
end # module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment