Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
65 changes: 65 additions & 0 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
AMUSE examples module
"""

import inspect
import importlib

try:
from IPython.core.getipython import get_ipython
from IPython.terminal.interactiveshell import InteractiveShell
except ImportError:

def get_ipython():
"N/A, so return None"
return None


def get(example_name, prefix="amuse.examples"):
"""
Retrieves an AMUSE example.
"""
if prefix is not None:
if prefix[-1] != ".":
prefix += "."
return inspect.getsource(importlib.import_module(f"{prefix}{example_name}"))


def to_cell(example_name, prefix="amuse.examples"):
"""
Creates a Jupyter cell from an AMUSE example.
Works also for other modules, set prefix to None in that case.
"""

contents = get(example_name, prefix=prefix)
shell = get_ipython()
shell.set_next_input(contents, replace=False)


def show(example_name, prefix="amuse.examples"):
"""
Prints the source code of an AMUSE example.
"""
print(get(example_name, prefix=prefix))


def run(example_name):
"""
Runs an AMUSE example.
"""
prefix = "amuse.examples."
try:
example = importlib.import_module(f"{prefix}{example_name}")
except ImportError as exc:
raise ImportError("Could not find example") from exc
example.main()


def shell_is_interactive():
"""
Returns True if the shell is interactive and False otherwise.
"""
try:
return isinstance(get_ipython(), InteractiveShell)
except ImportError:
return False
76 changes: 76 additions & 0 deletions examples/anim_gravity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Example for animating a gravity simulation.
"""

import argparse
import numpy as np

# #BOOKLISTSTART# #
import matplotlib.pyplot as plt
from matplotlib import animation
from amuse.io import read_set_from_file
from amuse.units import units


def animate(x, y):

def update(i):
data = np.stack([x[i], y[i]]).T
scat.set_offsets(data)

return (scat,)

number_of_snaps = len(x)
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim((-1.2, 1.2))
ax.set_ylim((-1.2, 1.2))
ax.set_xlabel("X [au]")
ax.set_ylabel("Y [au]")

colormap = ["#FFFF00", "wheat", "deepskyblue"]
size = [40, 20, 20]
edgecolor = ["orange", "wheat", "deepskyblue"]

ax.set_facecolor("black")
scat = ax.scatter(x[0], y[0], c=colormap, s=size, edgecolor=edgecolor)
anim = animation.FuncAnimation(fig, update, frames=number_of_snaps, interval=30)
anim.save("anim_gravity.mp4", dpi=150, writer=animation.FFMpegWriter(fps=25))


# #BOOKLISTSTOP# #


def anim_gravity(filename="sun_venus_earth.amuse", **kwargs):
particles = read_set_from_file(filename)

x = []
y = []
for si in particles.history:
x.append(si.x.value_in(units.au))
y.append(si.y.value_in(units.au))

animate(x, y)


def new_argument_parser():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"-i",
"--filename",
type=str,
default="sun_venus_earth.amuse",
help="input filename",
)
return parser


def main(**kwargs):
anim_gravity(**kwargs)


if __name__ == "__main__":
arguments = new_argument_parser().parse_args()
main(**arguments.__dict__)
93 changes: 0 additions & 93 deletions examples/applications/asterisk_example.py

This file was deleted.

107 changes: 0 additions & 107 deletions examples/applications/asterisk_movie_example.py

This file was deleted.

Loading
Loading