diff --git a/orbiter/orbits/calc.py b/orbiter/orbits/calc.py index 71c0e8a..6bc0afa 100644 --- a/orbiter/orbits/calc.py +++ b/orbiter/orbits/calc.py @@ -16,6 +16,13 @@ def calculate_distances(positions): dists[j][i] = Decimal(d) return dists +def calculate_distances_np(positions): + positions = np.array(positions) + diffs = positions[:, np.newaxis] - positions[np.newaxis, :] + dists = np.linalg.norm(diffs, axis=-1) + np.fill_diagonal(dists, 0) + return dists + def print_progress_bar(iteration, total, start_time, length=50): """Prints a progress bar to the console.""" percent = (iteration / total) * 100 diff --git a/orbiter/orbits/simulator.py b/orbiter/orbits/simulator.py index 4ae4362..655f973 100644 --- a/orbiter/orbits/simulator.py +++ b/orbiter/orbits/simulator.py @@ -108,36 +108,19 @@ class Simulator: def run(self, steps): time_start = time() - import matplotlib.pyplot as plt - plt.ion() - fig, ax = plt.subplots() - x_data, y_data =[],[] - line, = ax.plot(x_data, y_data, 'bo-') - ax.set_xlim(-8e6,8e6) - ax.set_ylim(-8e6,8e6) for i in range(steps): self.calculate_forces() self.move_bodies() self.current_step += 1 if (self.current_step % self.steps_per_save == 0): - for b in self.bodies: - x_data.append(int(real_pos(b.X[0]))) - y_data.append(int(real_pos(b.X[1]))) - line.set_xdata(x_data) - line.set_ydata(y_data) - plt.draw() - plt.pause(0.2) - x_data, y_data =[],[] - - - #print_progress_bar(i, steps, time_start) + print_progress_bar(i, steps, time_start) #self._checkpoint() def calculate_forces(self): positions = [ body.X for body in self.bodies ] - dists = calculate_distances(positions) + dists = calculate_distances_np(positions) for i in range(len(self.bodies)): for j in range(i, len(self.bodies)): if i == j: diff --git a/test.py b/test.py index cc8ed07..e76470a 100644 --- a/test.py +++ b/test.py @@ -26,7 +26,7 @@ person = Body( T = 2*pi_approx*norm_pos(r)/person.V[1] -time_to_run = 15 #norm_time(2000) +time_to_run = T*3 #norm_time(2000) STEP_SIZE = Decimal(6e-4) n_steps = int(time_to_run/STEP_SIZE) @@ -34,8 +34,9 @@ def main(): print("Before: ") print(str(person)) print(str(earth)) + import cProfile s = Simulator([earth,person], STEP_SIZE, 100, Path("hello_world")) - s.run(n_steps) + cProfile.run(s.run(n_steps)) print("\nAfter:") print(str(person)) print(str(earth))