Skip to content

Commit 16178e3

Browse files
committed
fix better
1 parent cb1fffd commit 16178e3

1 file changed

Lines changed: 37 additions & 59 deletions

File tree

src/amuse/examples/stellar_isochrone.py

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
metalicity z.
55
"""
66

7+
import argparse
78
import numpy
89
import matplotlib.pyplot as plt
910

@@ -12,60 +13,33 @@
1213
from amuse.community.seba import Seba
1314
from amuse.community.sse import Sse
1415
from amuse.community.mesa_r2208 import Mesa
16+
from amuse.community.evtwin import Evtwin
17+
from amuse.ic.salpeter import new_salpeter_mass_distribution
1518
from amuse.io import write_set_to_file, read_set_from_file
1619

1720
# from amuse.community.seba.interface import SeBa
1821

1922

20-
def _get_stellar_temperature_and_luminosity(
21-
stars, C, z=0.02, t_end=100 | units.Myr, write=False
22-
):
23-
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"
36-
37-
stellar.parameters.metallicity = z
38-
stellar.particles.add_particles(stars)
39-
40-
for si in stellar.particles:
41-
try:
42-
si.evolve_for(t_end)
43-
except:
44-
print("Failed to evolve star: m=", si.mass.in_(units.MSun))
45-
# stellar.evolve_model(t_end)
46-
if write:
47-
# write_set_to_file(stellar.particles.savepoint(t_end), filename, 'amuse')
48-
write_set_to_file(stellar.particles, filename)
49-
stellar.stop()
50-
51-
5223
def get_stellar_temperature_and_luminosity(
53-
stars, C, z=0.02, t_end=100 | units.Myr, write=False
24+
stars, stellar_code, z=0.02, time_end=100 | units.Myr, write=False, overwrite=False
5425
):
55-
56-
if C.find("SeBa") >= 0:
57-
filename = "Stellar_SeBa.amuse"
58-
stellar = Seba()
26+
if stellar_code.lower() in ["seba", "sse"]:
27+
if stellar_code.lower() == "sse":
28+
filename = "Stellar_SSE.amuse"
29+
stellar = Sse()
30+
else:
31+
filename = "Stellar_SeBa.amuse"
32+
stellar = Seba()
5933
stellar.parameters.metallicity = z
6034
stellar.particles.add_particles(stars)
6135
channel_to_framework = stellar.particles.new_channel_to(stars)
62-
stellar.evolve_model(t_end)
36+
stellar.evolve_model(time_end)
6337
channel_to_framework.copy_attributes(["radius", "temperature", "luminosity"])
6438
stellar.stop()
6539
else:
66-
if C.find("MESA") >= 0:
40+
if stellar_code.lower() == "mesa":
6741
filename = "Stellar_MESA.amuse"
68-
if C.find("EVtwin") >= 0:
42+
if stellar_code.lower() == "evtwin":
6943
filename = "Stellar_EVtwin.amuse"
7044

7145
for si in stars:
@@ -75,17 +49,17 @@ def get_stellar_temperature_and_luminosity(
7549
# stellar.commit_particles()
7650
channel_to_framework = stellar.particles.new_channel_to(stars)
7751
try:
78-
stellar.evolve_model(t_end)
52+
stellar.evolve_model(time_end)
7953
channel_to_framework.copy_attributes(
8054
["radius", "temperature", "luminosity"]
8155
)
8256
print("Successvolly evolved star: m=", si.mass.in_(units.MSun))
8357
except:
8458
print("Failed to evolve star: m=", si.mass.in_(units.MSun))
85-
# stellar.evolve_model(t_end)
59+
# stellar.evolve_model(time_end)
8660
stellar.stop()
8761
if write:
88-
write_set_to_file(stars, filename)
62+
write_set_to_file(stars, filename, overwrite_file=overwrite)
8963

9064

9165
def plot_hrd(filename, ax):
@@ -98,10 +72,10 @@ def plot_hrd(filename, ax):
9872
ax.scatter(T, L, lw=0, s=R)
9973

10074

101-
def stellar_isochrone(N, t_end, z, C="SeBa", plot=False):
102-
if "SeBa" not in C:
75+
def stellar_isochrone(N, time_end, z, stellar_code="SeBa", plot=False, overwrite=False):
76+
if plot and "SeBa" not in stellar_code:
10377
x_label = "T [K]"
104-
y_label = "L [L$_\odot$]"
78+
y_label = r"L [L$_\odot$]"
10579
figure = plt.figure()
10680
ax = figure.add_subplot(1, 1, 1)
10781
ax.set_xlabel(x_label)
@@ -112,17 +86,24 @@ def stellar_isochrone(N, t_end, z, C="SeBa", plot=False):
11286
ax.set_ylim(1.0e-4, 1.0e4)
11387
filename = "Stellar_" + "SeBa" + ".amuse"
11488
plot_hrd(filename, ax)
115-
filename = "Stellar_" + C + ".amuse"
89+
filename = f"Stellar_{stellar_code}.amuse"
11690
plot_hrd(filename, ax)
11791
plt.savefig("HRD_N3000at4500Myr")
11892
elif not plot:
11993
numpy.random.seed(1)
12094
masses = new_salpeter_mass_distribution(N)
12195
stars = Particles(mass=masses)
122-
get_stellar_temperature_and_luminosity(stars, C=C, z=z, t_end=t_end, write=True)
96+
get_stellar_temperature_and_luminosity(
97+
stars,
98+
stellar_code=stellar_code,
99+
z=z,
100+
time_end=time_end,
101+
write=True,
102+
overwrite=overwrite,
103+
)
123104
else:
124105
x_label = "T [K]"
125-
y_label = "L [L$_\odot$]"
106+
y_label = r"L [L$_\odot$]"
126107
figure = plt.figure()
127108
ax = figure.add_subplot(1, 1, 1)
128109
ax.set_xlabel(x_label)
@@ -131,7 +112,7 @@ def stellar_isochrone(N, t_end, z, C="SeBa", plot=False):
131112
ax.set_yscale("log")
132113
ax.set_xlim(1.0e5, 1.0e3)
133114
ax.set_ylim(1.0e-4, 1.0e4)
134-
filename = "Stellar_" + C + ".amuse"
115+
filename = f"Stellar_{stellar_code}.amuse"
135116
plot_hrd(filename, ax)
136117
plt.savefig("HRD_N3000at4500Myr.pdf")
137118

@@ -141,28 +122,25 @@ def new_argument_parser():
141122
formatter_class=argparse.ArgumentDefaultsHelpFormatter
142123
)
143124
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"
125+
"-C", "--stellar_code", default="SeBa", help="stellar evolution code to use"
148126
)
127+
parser.add_argument("-N", type=int, default=3000, help="number of stars")
149128
parser.add_argument(
150129
"-t",
151130
"--time_end",
152131
type=units.Myr,
153132
default=4500.0 | units.Myr,
154133
help="end time of the simulation",
155134
)
135+
parser.add_argument("-z", type=float, default=0.02, help="metalicity")
136+
parser.add_argument("-p", "--plot", action="store_true", default=False, help="plot")
156137
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"
138+
"--overwrite", action="store_true", default=False, help="overwrite files"
161139
)
162140
return parser
163141

164142

165-
def main()
143+
def main():
166144
args = new_argument_parser().parse_args()
167145
stellar_isochrone(**args.__dict__)
168146

0 commit comments

Comments
 (0)