-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Add algorithm for N-body simulation #4245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ec738b0
5ee6c48
1367811
7fe7295
579adfc
fd96cad
e7809db
d14de5a
8b5aa24
9809124
bb25fdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,6 +139,11 @@ def plot( | |
y_start: float = -1, | ||
y_end: float = 1, | ||
) -> None: | ||
""" | ||
Utility function to plot how the given body-system evolves over time. | ||
No doctest provided since this function does not have a return value. | ||
""" | ||
|
||
INTERVAL = 20 # Frame rate of the animation | ||
DELTA_TIME = INTERVAL / 1000 # Time between time steps in seconds | ||
|
||
|
@@ -156,7 +161,7 @@ def plot( | |
) | ||
|
||
# Function called once at the start of the animation | ||
def init() -> list[patches.Circle]: # type: ignore | ||
def init() -> list[patches.Circle]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
axes = plt.gca() | ||
axes.set_aspect("equal") | ||
|
||
|
@@ -165,7 +170,7 @@ def init() -> list[patches.Circle]: # type: ignore | |
return patches | ||
|
||
# Function called at each step of the animation | ||
def animate(i: int) -> list[patches.Circle]: # type: ignore | ||
def update(frame: int) -> list[patches.Circle]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
# Update the positions of the bodies | ||
body_system.update_system(DELTA_TIME) | ||
|
||
|
@@ -175,14 +180,17 @@ def animate(i: int) -> list[patches.Circle]: # type: ignore | |
return patches | ||
|
||
anim = animation.FuncAnimation( # noqa: F841 | ||
fig, animate, init_func=init, interval=INTERVAL, blit=True | ||
fig, update, init_func=init, interval=INTERVAL, blit=True | ||
) | ||
|
||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
# Example 1: figure-8 solution to the 3-body-problem | ||
# This example can be seen as a test of the implementation: given the right | ||
# initial conditions, the bodies should move in a figure-8. | ||
# (initial conditions taken from http://www.artcompsci.org/vol_1/v1_web/node56.html) | ||
position_x = 0.9700436 | ||
position_y = -0.24308753 | ||
velocity_x = 0.466203685 | ||
|
@@ -197,6 +205,10 @@ def animate(i: int) -> list[patches.Circle]: # type: ignore | |
plot("Figure-8 solution to the 3-body-problem", body_system1, -2, 2, -2, 2) | ||
|
||
# Example 2: Moon's orbit around the earth | ||
# This example can be seen as a test of the implementation: given the right | ||
# initial conditions, the moon should orbit around the earth as it actually does. | ||
# (mass, velocity and distance taken from https://en.wikipedia.org/wiki/Earth | ||
# and https://en.wikipedia.org/wiki/Moon) | ||
moon_mass = 7.3476e22 | ||
earth_mass = 5.972e24 | ||
velocity_dif = 1022 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
other/n_body_simulation.py
, please provide doctest for the functioninit