Skip to content

Instantly share code, notes, and snippets.

View hannorein's full-sized avatar

Hanno Rein hannorein

View GitHub Profile
with open('somefile.txt', 'a') as f:
N = len(a)
for i in xrange(N):
f.write("%e %e %e\n" %(a[i],b[i],c[i]))
import struct
with open('somefile.txt', 'a') as f:
N = len(a)
for i in xrange(N):
f.write(struct.pack("f", a[i]))
f.write(struct.pack("f", b[i]))
f.write(struct.pack("f", c[i]))
@hannorein
hannorein / gist:7f633cd769c390aa0dc4
Created April 11, 2015 22:39
REBOUND installation using pip
# Create a directory to work in
mkdir rebound
cd rebound
# Create a virtual environment to work in
# (not required, but keeps your python installation clean)
virtualenv venv
source venv/bin/activate
# Install rebound
import json
import sys
if len(sys.argv)!=2:
print "Usage: ipynb2py.py FILENAME"
exit(1)
with open(sys.argv[1]) as data_file:
ipynb = json.load(data_file)
code = ""
/**
* A.S. This is my planetesimal disk with special integrator for close encounters.
* Particle id's: 0 = star, 1 = massive body, 2 = planetesimal, 3 = CLOSE ENCOUNTER
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#!/usr/bin/python
from selenium import webdriver
import unittest, time, re
from random import choice
correctanswers = []
def tryitnow ():
# Copy and paste the module url below, including the key:
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
m = np.array([1.0, 3.0404326480226416e-06, 0.0009547919152112404, 0.0002858856727222417])
r = np.array([[0.0, 0.0, 0.0], [-0.5774742940580093, 0.7974401660237153, -2.611328235294271e-05], [-5.2320819021823235, 1.4130269395522546, 0.11120655650204452], [-3.5888816588976513, -9.343800743805208, 0.30528182406879745]])
v = np.array([[0.0, 0.0, 0.0], [-0.8263301482607002, -0.5903562644526552, 2.5048741249552767e-05], [-0.11971493094874505, -0.40317587415621225, 0.00435283234131933], [0.28498977476287424, -0.11757835716481167, -0.009305024058343627]])
plt.xlim([-6,6])
plt.ylim([-6,6])
plt.plot(r[:,0:1],"o",label=["sun"]);
t = 0.
tmax = 2.*np.pi # corresponds to 1 year
steps = 1000
dt = tmax/steps
N = len(m)
track = np.zeros((steps*N,3))
for k in range(steps): # integrate for one year
for i in range(N): #update positions for all particles
r[i][0] += dt/2.*v[i][0]
r[i][1] += dt/2.*v[i][1]
%matplotlib inline
import rebound
sim = rebound.Simulation()
sim.add(["Sun","Earth","Jupiter","Saturn"])
rebound.OrbitPlot(sim);