Skip to content

Commit 9d930ad

Browse files
committed
Add new examples
1 parent ff0216e commit 9d930ad

75 files changed

Lines changed: 19836 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import numpy
2+
from amuse.lab import *
3+
from matplotlib import pyplot
4+
5+
R_young_stars = [ 0.1012221753955787, 0.2968660204589863,
6+
0.5017430508551541, 0.7046418772801384, 0.8984687805821738,
7+
1.1087212228990806, 1.296848150063784]
8+
S_young_stars = [ 69.68021137071266, 21.28302762211248 ,
9+
8.871846723535821 , 4.634361707743547 , 0.842276996426784 ,
10+
0.35283784451473543, 0.32284672967371486]
11+
12+
R_all_stars = [ 0.09966839700574376, 0.29542608638995443,
13+
0.4910636790322119, 0.6989696157755654, 0.9093883458890231,
14+
1.1027718150669223, 1.3041555749011087]
15+
16+
S_all_stars = [246.4266804633498 , 108.52660553059054,
17+
59.62962877617595 , 22.170446486030563
18+
, 6.83698178212555 , 2.8123977037860786
19+
, 0.28189901519958876]
20+
21+
22+
23+
R_dense_gas = [ 0.10160842129883879, 0.30957902551862887,
24+
0.5005420834356191, 0.6940641257773106,
25+
0.8993648870413651, 1.107381682315753,
26+
1.2954901330586175]
27+
28+
29+
S_dense_gas = [587.057496278814, 193.757708949030, 81.05652212929158,
30+
25.83103135675521, 13.72720495792530,
31+
4.161094960653975, 3.939217817754274]
32+
33+
R_all_gas = [ 0.09890162549942522, 0.2971536318319011,
34+
0.5047269929821525, 0.6955052866807527,
35+
0.8934247174201266, 1.0960741121502846,
36+
1.296349286674131]
37+
S_all_gas = [ 1011.82389615108 , 382.3950057709804, 262.3523868909657
38+
, 154.2500699106347,
39+
107.5704842567941,
40+
88.9640154049995 ,
41+
68.72257895441606]
42+
43+
pyplot.rcParams.update({'font.size': 30})
44+
fig, ax = pyplot.subplots(figsize=[16,10])
45+
ax.minorticks_on() # switch on the minor ticks
46+
ax.tick_params('both', length=15, width=2, which='major')
47+
ax.tick_params('both', length=6, width=1, which='minor')
48+
from distinct_colours import get_distinct
49+
colors = get_distinct(10)
50+
51+
pyplot.scatter(R_young_stars, S_young_stars, s=100, marker='s', c=colors[6], lw=0)
52+
pyplot.scatter(R_all_stars, S_all_stars, s=100, marker='s', c=colors[0], lw=0)
53+
#pyplot.scatter(R_dense_gas, S_dense_gas, s=100, marker='s', c=colors[7], lw=0)
54+
pyplot.scatter(R_all_gas, S_all_gas, s=100, marker='s', c=colors[3], lw=0)
55+
56+
def plot_radial_distribution(gas, nbin, c, lw):
57+
X = []
58+
Ymean = []
59+
Ystd = []
60+
for gi in range(len(gas)-nbin):
61+
X.append(gas[gi: gi+nbin].r.value_in(units.parsec).mean())
62+
S = (gas[gi+nbin].r**2-gas[gi].r**2)
63+
rho = gas[gi: gi+nbin].mass.sum()/S
64+
#if hasattr(gas, "rho"):
65+
#rho = gas[gi: gi+nbin].rho.max()*S.sqrt()
66+
Ymean.append(rho.value_in(units.MSun/units.parsec**2))
67+
pyplot.plot(X, Ymean, c=colors[c], lw=lw)
68+
69+
def plot_radial_density_distribution(gas, stars):
70+
71+
com = stars.center_of_mass()
72+
73+
gas.r = ((gas.x-com[0])**2 + (gas.y-com[1])**2).sqrt()
74+
gas = gas.sorted_by_attributes("r")
75+
76+
stars.r = ((stars.x-com[0])**2 + (stars.y-com[1])**2).sqrt()
77+
stars = stars.sorted_by_attributes("r")
78+
79+
max_age = stars.birth_age.max()
80+
young_stars = stars[max_age-stars.birth_age<1.5|units.Myr]
81+
82+
mN2H = 2*29.02134
83+
cutoff_density = 1e5 * mN2H * constants.atomic_unit_of_mass/(1|units.cm**3)
84+
dense_gas = gas[gas.density>cutoff_density]
85+
86+
plot_radial_distribution(gas, 100, c=3, lw=4)
87+
88+
# plot_radial_distribution(dense_gas, 100, c=7, lw=2)
89+
plot_radial_distribution(stars, 60, c=0, lw=4)
90+
plot_radial_distribution(young_stars, 10, c=6, lw=4)
91+
pyplot.xlim(0, 1.1)
92+
pyplot.ylim(1, 1100)
93+
pyplot.semilogy()
94+
pyplot.xlabel("R [pc]")
95+
pyplot.ylabel("$\Sigma$ [M$_\odot$ pc$^{-2}]$")
96+
# pyplot.show()
97+
pyplot.savefig("2017arXiv170307029H_Fig3")
98+
99+
def main(filename=None):
100+
101+
disk = Particles(0)
102+
stars = Particles(0)
103+
bodies = read_set_from_file(filename, "amuse")
104+
for bi in bodies.history:
105+
#print(bi)
106+
if len(bi)>0:
107+
if hasattr(bi, "name") and "gas" in str(bi.name):
108+
disk.add_particles(bi.copy())
109+
elif "Star" in str(bi.name):
110+
stars.add_particles(bi.copy())
111+
112+
print("Stellar masses:", stars.mass.min().in_(units.MSun), stars.mass.mean().in_(units.MSun), stars.mass.max().in_(units.MSun), stars.mass.median().in_(units.MSun))
113+
114+
#plot_age_gasdensity(disk, stars)
115+
plot_radial_density_distribution(disk, stars)
116+
117+
def new_option_parser():
118+
from amuse.units.optparse import OptionParser
119+
result = OptionParser()
120+
result.add_option("-f",
121+
dest="filename", default ="GMC_R2pcN20k_SE_T45Myr.amuse",
122+
help="output filename [%default]")
123+
return result
124+
125+
if __name__ in ('__main__', '__plot__'):
126+
o, arguments = new_option_parser().parse_args()
127+
main(**o.__dict__)
128+
129+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
AMUSE examples module
3+
"""
4+
5+
import inspect
6+
import importlib
7+
8+
try:
9+
from IPython.core.getipython import get_ipython
10+
from IPython.terminal.interactiveshell import InteractiveShell
11+
except ImportError:
12+
13+
def get_ipython():
14+
"N/A, so return None"
15+
return None
16+
17+
18+
def get(example_name, prefix="amuse.examples"):
19+
"""
20+
Retrieves an AMUSE example.
21+
"""
22+
if prefix is not None:
23+
if prefix[-1] != ".":
24+
prefix += "."
25+
return inspect.getsource(importlib.import_module(f"{prefix}{example_name}"))
26+
27+
28+
def to_cell(example_name, prefix="amuse.examples"):
29+
"""
30+
Creates a Jupyter cell from an AMUSE example.
31+
Works also for other modules, set prefix to None in that case.
32+
"""
33+
34+
contents = get(example_name, prefix=prefix)
35+
shell = get_ipython()
36+
shell.set_next_input(contents, replace=False)
37+
38+
39+
def show(example_name, prefix="amuse.examples"):
40+
"""
41+
Prints the source code of an AMUSE example.
42+
"""
43+
print(get(example_name, prefix=prefix))
44+
45+
46+
def run(example_name):
47+
"""
48+
Runs an AMUSE example.
49+
"""
50+
prefix = "amuse.examples."
51+
try:
52+
example = importlib.import_module(f"{prefix}{example_name}")
53+
except ImportError as exc:
54+
raise ImportError("Could not find example") from exc
55+
example.main()
56+
57+
58+
def shell_is_interactive():
59+
"""
60+
Returns True if the shell is interactive and False otherwise.
61+
"""
62+
try:
63+
return isinstance(get_ipython(), InteractiveShell)
64+
except ImportError:
65+
return False
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Example for animating a gravity simulation.
3+
"""
4+
5+
import argparse
6+
import numpy as np
7+
8+
# #BOOKLISTSTART# #
9+
import matplotlib.pyplot as plt
10+
from matplotlib import animation
11+
from amuse.io import read_set_from_file
12+
from amuse.units import units
13+
14+
15+
def animate(x, y):
16+
17+
def update(i):
18+
data = np.stack([x[i], y[i]]).T
19+
scat.set_offsets(data)
20+
21+
return (scat,)
22+
23+
number_of_snaps = len(x)
24+
fig = plt.figure(figsize=(8, 8))
25+
ax = fig.add_subplot(1, 1, 1)
26+
ax.set_xlim((-1.2, 1.2))
27+
ax.set_ylim((-1.2, 1.2))
28+
ax.set_xlabel("X [au]")
29+
ax.set_ylabel("Y [au]")
30+
31+
colormap = ["#FFFF00", "wheat", "deepskyblue"]
32+
size = [40, 20, 20]
33+
edgecolor = ["orange", "wheat", "deepskyblue"]
34+
35+
ax.set_facecolor("black")
36+
scat = ax.scatter(x[0], y[0], c=colormap, s=size, edgecolor=edgecolor)
37+
anim = animation.FuncAnimation(fig, update, frames=number_of_snaps, interval=30)
38+
anim.save("anim_gravity.mp4", dpi=150, writer=animation.FFMpegWriter(fps=25))
39+
40+
41+
# #BOOKLISTSTOP# #
42+
43+
44+
def anim_gravity(filename="sun_venus_earth.amuse", **kwargs):
45+
particles = read_set_from_file(filename)
46+
47+
x = []
48+
y = []
49+
for si in particles.history:
50+
x.append(si.x.value_in(units.au))
51+
y.append(si.y.value_in(units.au))
52+
53+
animate(x, y)
54+
55+
56+
def new_argument_parser():
57+
parser = argparse.ArgumentParser(
58+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
59+
)
60+
parser.add_argument(
61+
"-i",
62+
"--filename",
63+
type=str,
64+
default="sun_venus_earth.amuse",
65+
help="input filename",
66+
)
67+
return parser
68+
69+
70+
def main(**kwargs):
71+
anim_gravity(**kwargs)
72+
73+
74+
if __name__ == "__main__":
75+
arguments = new_argument_parser().parse_args()
76+
main(**arguments.__dict__)

0 commit comments

Comments
 (0)