Skip to content

Commit cb1fffd

Browse files
committed
fix example from the py2 era...
1 parent 0014feb commit cb1fffd

1 file changed

Lines changed: 124 additions & 91 deletions

File tree

Lines changed: 124 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
"""
2-
Evolve a population of N stars.
3-
initial mass function between Mmin and Mmax and with stellar evolution with
4-
metalicity z.
2+
Evolve a population of N stars.
3+
initial mass function between Mmin and Mmax and with stellar evolution with
4+
metalicity z.
55
"""
6-
import sys
6+
77
import numpy
8-
from amuse.lab import *
9-
from matplotlib import pyplot
8+
import matplotlib.pyplot as plt
9+
10+
from amuse.units import units
11+
from amuse.datamodel import Particles
12+
from amuse.community.seba import Seba
13+
from amuse.community.sse import Sse
14+
from amuse.community.mesa_r2208 import Mesa
15+
from amuse.io import write_set_to_file, read_set_from_file
1016

11-
#from amuse.community.seba.interface import SeBa
17+
# from amuse.community.seba.interface import SeBa
1218

13-
from prepare_figure import single_frame, figure_frame, set_tickmarks
14-
from distinct_colours import get_distinct
1519

16-
def _get_stellar_temperature_and_luminosity(stars, C, z=0.02, t_end=100|units.Myr, write=False):
20+
def _get_stellar_temperature_and_luminosity(
21+
stars, C, z=0.02, t_end=100 | units.Myr, write=False
22+
):
1723

18-
if C.find("SeBa")>=0 :
19-
stellar = SeBa()
20-
filename = "Stellar_SeBa.h5"
21-
if C.find("SSE")>=0 :
22-
stellar = SSE()
23-
filename = "Stellar_SSE.h5"
24-
if C.find("MESA")>=0 :
25-
stellar = MESA()
26-
filename = "Stellar_MESA.h5"
27-
if C.find("EVtwin")>=0 :
28-
stellar = SeBa()
29-
filename = "Stellar_EVtwin.h5"
24+
if C.find("SeBa") >= 0:
25+
stellar = Seba()
26+
filename = "Stellar_SeBa.amuse"
27+
if C.find("SSE") >= 0:
28+
stellar = Sse()
29+
filename = "Stellar_SSE.amuse"
30+
if C.find("MESA") >= 0:
31+
stellar = Mesa()
32+
filename = "Stellar_MESA.amuse"
33+
if C.find("EVtwin") >= 0:
34+
stellar = Seba()
35+
filename = "Stellar_EVtwin.amuse"
3036

3137
stellar.parameters.metallicity = z
3238
stellar.particles.add_particles(stars)
@@ -35,69 +41,80 @@ def _get_stellar_temperature_and_luminosity(stars, C, z=0.02, t_end=100|units.My
3541
try:
3642
si.evolve_for(t_end)
3743
except:
38-
print "Failed to evolve star: m=", si.mass.in_(units.MSun)
39-
#stellar.evolve_model(t_end)
44+
print("Failed to evolve star: m=", si.mass.in_(units.MSun))
45+
# stellar.evolve_model(t_end)
4046
if write:
41-
#write_set_to_file(stellar.particles.savepoint(t_end), filename, 'h5')
42-
write_set_to_file(stellar.particles, filename, 'hdf5')
47+
# write_set_to_file(stellar.particles.savepoint(t_end), filename, 'amuse')
48+
write_set_to_file(stellar.particles, filename)
4349
stellar.stop()
4450

45-
def get_stellar_temperature_and_luminosity(stars, C, z=0.02, t_end=100|units.Myr, write=False):
4651

47-
if C.find("SeBa")>=0 :
48-
filename = "Stellar_SeBa.h5"
49-
stellar = SeBa()
52+
def get_stellar_temperature_and_luminosity(
53+
stars, C, z=0.02, t_end=100 | units.Myr, write=False
54+
):
55+
56+
if C.find("SeBa") >= 0:
57+
filename = "Stellar_SeBa.amuse"
58+
stellar = Seba()
5059
stellar.parameters.metallicity = z
5160
stellar.particles.add_particles(stars)
5261
channel_to_framework = stellar.particles.new_channel_to(stars)
5362
stellar.evolve_model(t_end)
5463
channel_to_framework.copy_attributes(["radius", "temperature", "luminosity"])
5564
stellar.stop()
5665
else:
57-
if C.find("MESA")>=0 :
58-
filename = "Stellar_MESA.h5"
59-
if C.find("EVtwin")>=0 :
60-
filename = "Stellar_EVtwin.h5"
66+
if C.find("MESA") >= 0:
67+
filename = "Stellar_MESA.amuse"
68+
if C.find("EVtwin") >= 0:
69+
filename = "Stellar_EVtwin.amuse"
6170

6271
for si in stars:
63-
stellar = MESA()
72+
stellar = Mesa()
6473
stellar.parameters.metallicity = z
6574
stellar.particles.add_particle(si)
66-
#stellar.commit_particles()
75+
# stellar.commit_particles()
6776
channel_to_framework = stellar.particles.new_channel_to(stars)
6877
try:
6978
stellar.evolve_model(t_end)
70-
channel_to_framework.copy_attributes(["radius", "temperature", "luminosity"])
71-
print "Successvolly evolved star: m=", si.mass.in_(units.MSun)
79+
channel_to_framework.copy_attributes(
80+
["radius", "temperature", "luminosity"]
81+
)
82+
print("Successvolly evolved star: m=", si.mass.in_(units.MSun))
7283
except:
73-
print "Failed to evolve star: m=", si.mass.in_(units.MSun)
74-
#stellar.evolve_model(t_end)
84+
print("Failed to evolve star: m=", si.mass.in_(units.MSun))
85+
# stellar.evolve_model(t_end)
7586
stellar.stop()
7687
if write:
77-
write_set_to_file(stars, filename, 'hdf5')
78-
79-
def plot_HRD(filename, color):
80-
stars = read_set_from_file(filename, 'hdf5')
81-
T = stars.temperature.value_in(units.K)
82-
L = stars.luminosity.value_in(units.LSun)
83-
R = stars.radius.value_in(units.RSun)
84-
85-
R = 80*numpy.sqrt(R)
86-
pyplot.scatter(T, L, c=color, lw=0, s=R)
87-
88-
def main(N, t_end, z, C="SeBa", plot=False):
88+
write_set_to_file(stars, filename)
89+
90+
91+
def plot_hrd(filename, ax):
92+
stars = read_set_from_file(filename)
93+
T = stars.temperature.value_in(units.K)
94+
L = stars.luminosity.value_in(units.LSun)
95+
R = stars.radius.value_in(units.RSun)
96+
97+
R = 80 * numpy.sqrt(R)
98+
ax.scatter(T, L, lw=0, s=R)
99+
100+
101+
def stellar_isochrone(N, t_end, z, C="SeBa", plot=False):
89102
if "SeBa" not in C:
90103
x_label = "T [K]"
91104
y_label = "L [L$_\odot$]"
92-
figure = single_frame(x_label, y_label, logx=True, logy=True, xsize=14, ysize=10)
93-
color = get_distinct(4)
94-
pyplot.xlim(1.e+5, 1.e+3)
95-
pyplot.ylim(1.e-4, 1.e+4)
96-
filename = "Stellar_"+"SeBa"+".h5"
97-
plot_HRD(filename, color[0])
98-
filename = "Stellar_"+C+".h5"
99-
plot_HRD(filename, color[1])
100-
pyplot.savefig("HRD_N3000at4500Myr")
105+
figure = plt.figure()
106+
ax = figure.add_subplot(1, 1, 1)
107+
ax.set_xlabel(x_label)
108+
ax.set_ylabel(y_label)
109+
ax.set_xscale("log")
110+
ax.set_yscale("log")
111+
ax.set_xlim(1.0e5, 1.0e3)
112+
ax.set_ylim(1.0e-4, 1.0e4)
113+
filename = "Stellar_" + "SeBa" + ".amuse"
114+
plot_hrd(filename, ax)
115+
filename = "Stellar_" + C + ".amuse"
116+
plot_hrd(filename, ax)
117+
plt.savefig("HRD_N3000at4500Myr")
101118
elif not plot:
102119
numpy.random.seed(1)
103120
masses = new_salpeter_mass_distribution(N)
@@ -106,33 +123,49 @@ def main(N, t_end, z, C="SeBa", plot=False):
106123
else:
107124
x_label = "T [K]"
108125
y_label = "L [L$_\odot$]"
109-
figure = single_frame(x_label, y_label, logx=True, logy=True, xsize=14, ysize=10)
110-
color = get_distinct(4)
111-
pyplot.xlim(1.e+5, 1.e+3)
112-
pyplot.ylim(1.e-4, 1.e+4)
113-
filename = "Stellar_"+C+".h5"
114-
plot_HRD(filename, color[0])
115-
# pyplot.show()
116-
pyplot.savefig("HRD_N3000at4500Myr")
117-
118-
def new_option_parser():
119-
from amuse.units.optparse import OptionParser
120-
result = OptionParser()
121-
result.add_option("-C", dest="C", default = "SeBa",
122-
help="stellar evolution code [SeBa]")
123-
result.add_option("-N", dest="N", type="int",default = 3000,
124-
help="number of stars [10]")
125-
result.add_option("-t", dest="t_end", unit=units.Myr,
126-
type="float", default = 4500.0|units.Myr,
127-
help="end time of the simulation [100] Myr")
128-
result.add_option("-z", dest="z", type="float", default = 0.02,
129-
help="metalicity [0.02]")
130-
result.add_option("-p", dest="plot", action="store_true", default = False,
131-
help="plot")
132-
return result
133-
134-
if __name__ in ('__main__', '__plot__'):
135-
o, arguments = new_option_parser().parse_args()
136-
main(**o.__dict__)
137-
138-
126+
figure = plt.figure()
127+
ax = figure.add_subplot(1, 1, 1)
128+
ax.set_xlabel(x_label)
129+
ax.set_ylabel(y_label)
130+
ax.set_xscale("log")
131+
ax.set_yscale("log")
132+
ax.set_xlim(1.0e5, 1.0e3)
133+
ax.set_ylim(1.0e-4, 1.0e4)
134+
filename = "Stellar_" + C + ".amuse"
135+
plot_hrd(filename, ax)
136+
plt.savefig("HRD_N3000at4500Myr.pdf")
137+
138+
139+
def new_argument_parser():
140+
parser = argparse.ArgumentParser(
141+
formatter_class=argparse.ArgumentDefaultsHelpFormatter
142+
)
143+
parser.add_argument(
144+
"-C", default="SeBa", help="stellar evolution code"
145+
)
146+
parser.add_argument(
147+
"-N", type=int, default=3000, help="number of stars"
148+
)
149+
parser.add_argument(
150+
"-t",
151+
"--time_end",
152+
type=units.Myr,
153+
default=4500.0 | units.Myr,
154+
help="end time of the simulation",
155+
)
156+
parser.add_argument(
157+
"-z", type=float, default=0.02, help="metalicity"
158+
)
159+
parser.add_argument(
160+
"-p", "--plot", action="store_true", default=False, help="plot"
161+
)
162+
return parser
163+
164+
165+
def main()
166+
args = new_argument_parser().parse_args()
167+
stellar_isochrone(**args.__dict__)
168+
169+
170+
if __name__ == "__main__":
171+
main()

0 commit comments

Comments
 (0)