-
Notifications
You must be signed in to change notification settings - Fork 1
/
greta.py
213 lines (184 loc) · 6.22 KB
/
greta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import argparse
import numpy
from amuse.io import read_set_from_file, write_set_to_file
from amuse.units import units
from amuse.datamodel import Particles
from star_formation_class import assign_sink_group, form_stars_from_group
def new_argument_parser():
"""
Parse command line arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
'-i',
dest='sinkfilename',
default='',
help='AMUSE sink particle set',
)
parser.add_argument(
'-o',
dest='starfilename',
default='stars.amuse',
help='Output name for AMUSE star particle set [stars.amuse]',
)
parser.add_argument(
'-d',
dest='grouping_distance',
type=float,
default=0,
help='Grouping distance parameter in pc [0]',
)
parser.add_argument(
'-v',
dest='grouping_speed',
type=float,
default=0,
help='Grouping speed parameter in km/s [0]',
)
parser.add_argument(
'-t',
dest='grouping_age',
type=float,
default=0,
help='Grouping age parameter in Myr [0]'
)
parser.add_argument(
'--lower-limit',
dest='mass_lower',
type=float,
default=0.5,
help='Lower star mass limit in MSun [0.5]'
)
parser.add_argument(
'--upper-limit',
dest='mass_upper',
type=float,
default=100,
help='Upper star mass limit in MSun [100]'
)
parser.add_argument(
'-r',
dest='randomseed',
default=None,
help='Random seed [None]'
)
return parser.parse_args()
def preface():
msg = (
"""
██████╗ ██████╗ ███████╗████████╗ █████╗
██╔════╝ ██╔══██╗██╔════╝╚══██╔══╝██╔══██╗
██║ ███╗██████╔╝█████╗ ██║ ███████║
██║ ██║██╔══██╗██╔══╝ ██║ ██╔══██║
╚██████╔╝██║ ██║███████╗ ██║ ██║ ██║
╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝
GRoupEd sTar formAtion © Kong You Liow
References:
- Liow K. Y., Rieder S., Dobbs C. L., Jaffa S. E., 2022,
MNRAS, 510, 2657
- Rieder S., Dobbs C. L., Bending T., Liow K. Y., Wurster J., 2022,
MNRAS, 509, 6155
"""
)
print(msg)
def main():
preface()
o = new_argument_parser()
sinkfilename = o.sinkfilename
starfilename = o.starfilename
grouping_distance = o.grouping_distance | units.pc
grouping_speed = o.grouping_speed | units.kms
grouping_age = o.grouping_age | units.Myr
mass_lower = o.mass_lower | units.MSun
mass_upper = o.mass_upper | units.MSun
randomseed = o.randomseed
if randomseed is None:
randomseed = numpy.random.randint(2**32-1)
else:
randomseed = int(randomseed)
numpy.random.seed(randomseed)
sinks = read_set_from_file(sinkfilename, 'amuse')
if not hasattr(sinks, 'position'):
msg = (
"ERROR: Sink particles have no positions! Exiting program."
)
print(msg)
exit()
if not hasattr(sinks, 'velocity'):
msg = (
"WARNING: Sink particles have no velocities! Assuming they are "
+ "stationary."
)
print(msg)
sinks.velocity = 0 | units.kms
if not hasattr(sinks, 'radius'):
msg = (
"WARNING: Sink particles have no attribute 'radius', setting to "
+ "0.1 pc. This does not matter because this program is not "
+ "supposed to be used to generate initial conditions."
)
print(msg)
sinks.radius = 0.1 | units.pc
if not hasattr(sinks, 'birth_time'):
msg = (
"WARNING: Sink particles have no attribute 'birth_time', setting "
+ "to 0 Myr, i.e. assuming that all sink particles have zero age."
)
print(msg)
sinks.birth_time = 0 | units.Myr
print("Assigning groups to the sink particles...")
sinks.initialised = False
sinks = sinks.sorted_by_attribute('mass').reversed()
Nsinks = len(sinks)
Msinks = sinks.total_mass()
print(f"Number of sinks: {Nsinks}")
print(f"Total sink mass: {Msinks.in_(units.MSun)}")
for i, sink in enumerate(sinks):
if i % int(Nsinks/5) == 0:
print(f"Progress: {i}/{Nsinks}")
sink = assign_sink_group(
sink,
sinks,
group_radius=grouping_distance,
group_speed=grouping_speed,
group_age=grouping_age,
)
print(f"Number of groups: {len(set(sinks.in_group))}")
# Sanity check: each sink particle must be in a group.
ungrouped_sinks = sinks.select_array(
lambda x: x <= 0, ['in_group']
)
if not ungrouped_sinks.is_empty():
print(
"ERROR: There exist ungrouped sinks! Check group assignment."
)
exit()
print("Forming stars from sink groups...")
Ngroup = sinks.in_group.max()
stars = Particles()
for i in range(Ngroup):
if i % int(Ngroup/5) == 0:
print(f"Progress: {i}/{Ngroup}")
i += 1
new_stars = form_stars_from_group(
group_index=i,
sink_particles=sinks,
lower_mass_limit=mass_lower,
upper_mass_limit=mass_upper,
randomseed=numpy.random.randint(2**32-1),
shrink_sinks=False
)
if new_stars is not None:
stars.add_particles(new_stars)
Nstars = len(stars)
Mstars = stars.total_mass().in_(units.MSun)
fraction = Mstars/Msinks
print(f"Number of stars: {Nstars}")
print(f"Total star mass: {Mstars}")
print(f"Most massive star: {stars.mass.max().in_(units.MSun)}")
print(f"Average star mass: {(Mstars/Nstars).in_(units.MSun)}")
print(f"Mstars/Msinks fraction: {fraction:.4f}")
write_set_to_file(stars, starfilename, 'amuse', overwrite_file=True)
print(f"Star file written as {starfilename}")
if __name__ == '__main__':
main()