Working, but still slow.
This commit is contained in:
parent
5e3d043af4
commit
58eea0f264
@ -16,6 +16,13 @@ def calculate_distances(positions):
|
|||||||
dists[j][i] = Decimal(d)
|
dists[j][i] = Decimal(d)
|
||||||
return dists
|
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):
|
def print_progress_bar(iteration, total, start_time, length=50):
|
||||||
"""Prints a progress bar to the console."""
|
"""Prints a progress bar to the console."""
|
||||||
percent = (iteration / total) * 100
|
percent = (iteration / total) * 100
|
||||||
|
@ -108,36 +108,19 @@ class Simulator:
|
|||||||
|
|
||||||
def run(self, steps):
|
def run(self, steps):
|
||||||
time_start = time()
|
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):
|
for i in range(steps):
|
||||||
self.calculate_forces()
|
self.calculate_forces()
|
||||||
self.move_bodies()
|
self.move_bodies()
|
||||||
self.current_step += 1
|
self.current_step += 1
|
||||||
if (self.current_step % self.steps_per_save == 0):
|
if (self.current_step % self.steps_per_save == 0):
|
||||||
for b in self.bodies:
|
print_progress_bar(i, steps, time_start)
|
||||||
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)
|
|
||||||
#self._checkpoint()
|
#self._checkpoint()
|
||||||
|
|
||||||
def calculate_forces(self):
|
def calculate_forces(self):
|
||||||
positions = [
|
positions = [
|
||||||
body.X for body in self.bodies
|
body.X for body in self.bodies
|
||||||
]
|
]
|
||||||
dists = calculate_distances(positions)
|
dists = calculate_distances_np(positions)
|
||||||
for i in range(len(self.bodies)):
|
for i in range(len(self.bodies)):
|
||||||
for j in range(i, len(self.bodies)):
|
for j in range(i, len(self.bodies)):
|
||||||
if i == j:
|
if i == j:
|
||||||
|
5
test.py
5
test.py
@ -26,7 +26,7 @@ person = Body(
|
|||||||
|
|
||||||
T = 2*pi_approx*norm_pos(r)/person.V[1]
|
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)
|
STEP_SIZE = Decimal(6e-4)
|
||||||
n_steps = int(time_to_run/STEP_SIZE)
|
n_steps = int(time_to_run/STEP_SIZE)
|
||||||
|
|
||||||
@ -34,8 +34,9 @@ def main():
|
|||||||
print("Before: ")
|
print("Before: ")
|
||||||
print(str(person))
|
print(str(person))
|
||||||
print(str(earth))
|
print(str(earth))
|
||||||
|
import cProfile
|
||||||
s = Simulator([earth,person], STEP_SIZE, 100, Path("hello_world"))
|
s = Simulator([earth,person], STEP_SIZE, 100, Path("hello_world"))
|
||||||
s.run(n_steps)
|
cProfile.run(s.run(n_steps))
|
||||||
print("\nAfter:")
|
print("\nAfter:")
|
||||||
print(str(person))
|
print(str(person))
|
||||||
print(str(earth))
|
print(str(earth))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user