def reset_system(integrator_num): body_list = [] body_list.append(body(5, [0, 0], [-1 / 5, 0], [0, 0])) body_list.append(body(1, [0, 1], [1, 0], [0, 0])) system = nbody_system(body_list) xs = [] ys = [] Edat = [] n = len(body_list) # lists for saving position data for i in range(n): xs.append([]) ys.append([]) for iteration in range(500): integrator = [system.euler, system.verlet, system.ruth3][integrator_num] integrator(0.005) for i in range(n): xs[i].append(body_list[i].pos[0]) ys[i].append(body_list[i].pos[1]) Edat.append(system.total_energy()) for i in range(n): plt.plot(xs[i], ys[i]) # plot last points last_xs = [x[-1] for x in xs] last_ys = [y[-1] for y in ys] plt.scatter(last_xs, last_ys) return xs, ys, Edat
import matplotlib as mpl import matplotlib.pyplot as plt from gravitational_system import nbody_system from body import body body_list = [] body_list.append(body(5, [0, 0], [-1 / 5, 0], [0, 0])) body_list.append(body(1, [0, 1], [1, 0], [0, 0])) system = nbody_system(body_list) xs = [] ys = [] E_data = [] n = len(body_list) # lists for saving position data for i in range(n): xs.append([]) ys.append([]) for iteration in range(500): # Choose numerical method; verlet, euler, ruth3 system.ruth3(0.005) for i in range(n): xs[i].append(body_list[i].pos[0]) ys[i].append(body_list[i].pos[1]) E_data.append(system.total_energy())
# list containing all celestial bodies body_list = [] np.set_printoptions(15) # load initial conditions from file input_file = csv.DictReader(open("initial.csv", encoding="utf8")) for b in input_file: pos = [float(x) for x in [b["x"], b["y"], b["z"]]] vel = [float(x) for x in [b["vx"], b["vy"], b["vz"]]] GM = float(b["GM"]) name = b["name"] body_list.append(body(GM, pos, vel, [0, 0, 0], name)) system = nbody_system(body_list, 1.4881852e-34) BodyID = 3 years = 10 stepperday = 4 stepsize = 1 / stepperday # days plot_interval = 1 # log once every x days offset = 5 # leap days etc focus_pos = [] earth_data = []