-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstellar_wind_class.py
529 lines (493 loc) · 19.1 KB
/
stellar_wind_class.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Class for implementing a stellar wind
"""
import sys
import numpy
from numpy import pi
from amuse.units import units, nbody_system, constants
from amuse.datamodel.particles import Particle
from amuse.community.seba.interface import SeBa
# from amuse.community.fi.interface import Fi
from amuse.community.phantom.interface import Phantom
# from amuse.ic.gasplummer import new_plummer_gas_model
from amuse.io import write_set_to_file, read_set_from_file
from amuse.ext.masc import new_star_cluster
from amuse.ext import stellar_wind
from amuse.ext.molecular_cloud import molecular_cloud
from amuse.support.console import set_preferred_units
from plotting_class import plot_hydro_and_stars, temperature_to_u, u_to_temperature
from cooling_class import SimplifiedThermalModelEvolver # , Cooling
from star_forming_region_class import new_kroupa_mass_distribution
import default_settings
COOLING = False
def determine_short_timestep(sph, gas, h_min=0.1 | units.parsec):
gamma = sph.parameters.gamma
C_cour = sph.parameters.C_cour
eni = gas.u.max() # eni is the energy of the injected particle
spsound = numpy.sqrt(gamma*(gamma-1.)*eni)
return C_cour*h_min/spsound
def main():
numpy.random.seed(42)
evo_headstart = 0.0 | units.Myr
dt_base = 0.001 | units.Myr
dt = dt_base
time = 0 | units.Myr
time_end = 8 | units.Myr
Tmin = 22 | units.K
gas_density = 5e3 | units.amu * units.cm**-3
increase_vol = 2
Ngas = increase_vol**3 * 10000
Mgas = increase_vol**3 * 1000 | units.MSun # Mgas = Ngas | units.MSun
volume = Mgas / gas_density # 4/3 * pi * r**3
radius = (volume / (pi * 4/3))**(1/3)
radius = increase_vol * radius # 15 | units.parsec
gasconverter = nbody_system.nbody_to_si(Mgas, radius)
# gasconverter = nbody_system.nbody_to_si(1 | units.pc, 1 | units.MSun)
# gasconverter = nbody_system.nbody_to_si(1e10 | units.cm, 1e10 | units.g)
# NOTE: make stars first - so that it remains the same random
# initialisation even when we change the number of gas particles
if len(sys.argv) > 1:
gas = read_set_from_file(sys.argv[1], "amuse")
stars = read_set_from_file(sys.argv[2], "amuse")
stars.position = stars.position * 3
else:
# stars = new_star_cluster(
# stellar_mass=1000 | units.MSun, effective_radius=3 | units.parsec
# )
# stars.velocity = stars.velocity * 2.0
from amuse.datamodel import Particles
Nstars = 100
stars = Particles(Nstars)
stars.position = [0, 0, 0] | units.pc
stars.velocity = [0, 0, 0] | units.kms
stars.mass = new_kroupa_mass_distribution(Nstars, mass_min=1 | units.MSun).reshape(Nstars)
# 25 | units.MSun
gas = molecular_cloud(targetN=Ngas, convert_nbody=gasconverter).result
# gas.velocity = gas.velocity * 0.5
gas.u = temperature_to_u(100 | units.K)
# gas.x = gas.x
# gas.y = gas.y
# gas.z = gas.z
# gas.h_smooth = (gas.mass / gas_density / (4/3) / pi)**(1/3)
# print(gas.h_smooth.mean())
# gas = read_set_from_file("gas_initial.hdf5", "amuse")
# gas.density = gas_density
# print(gas.h_smooth.mean())
# exit()
u_now = gas.u
#print(gasconverter.to_nbody(gas[0].u))
#print(constants.kB.value_in(units.erg * units.K**-1))
#print((constants.kB * 6.02215076e23).value_in(units.erg * units.K**-1))
#print(gasconverter.to_nbody(temperature_to_u(10 | units.K)))
#tempiso = 2.d0/3.d0*ui/(Rg/gmwvar/uergg)
# print(nbody_system.length**2 / nbody_system.time**2)
# print(gasconverter.to_si(1 | nbody_system.length**2 / nbody_system.time**2).value_in(units.kms**2))
# print(gasconverter.to_nbody(temperature_to_u(Tmin)))
# Rg = (constants.kB * 6.02214076e23).value_in(units.erg * units.K**-1)
# gmwvar = 1.2727272727
# uergg = nbody_system.length**2 * nbody_system.time**-2
# uergg = 6.6720409999999996E-8
# print(Rg)
# print(
# 2.0/3.0*gasconverter.to_nbody(temperature_to_u(Tmin))/(Rg/gmwvar/uergg)
# )
# #tempiso, ui, Rg, gmwvar, uergg, udist, utime 1.7552962911187030E-018 2.5778500859241771E-003 83140000.000000000 1.2727272727272725 6.6720409999999996E-008 1.0000000000000000 3871.4231866737564
# u = 3./2. * Tmin.value_in(units.K) * (Rg/gmwvar/uergg)
# print(u)
# print(
# 2.0/3.0*u/(Rg/gmwvar/uergg)
# )
# print(u, Rg, gmwvar, uergg)
# print(temperature_to_u(10 | units.K).value_in(units.kms**2))
u = temperature_to_u(20 | units.K)
#print(gasconverter.to_nbody(u))
#print(u_to_temperature(u).value_in(units.K))
# exit()
# gas.u = u | units.kms**2
# exit()
# print(gasconverter.to_nbody(gas.u.mean()))
# print(gasconverter.to_si(gas.u.mean()).value_in(units.kms**2))
# exit()
gas.du_dt = (u_now - u_now) / dt # zero, but in the correct units
# stars = read_set_from_file("stars.amuse", "amuse")
# write_set_to_file(stars, 'stars.amuse', 'amuse', append_to_file=False)
# stars.velocity *= 3
# stars.vx += 0 | units.kms
# stars.vy += 0 | units.kms
M = stars.total_mass() + Mgas
R = stars.position.lengths().mean()
converter = nbody_system.nbody_to_si(M, R)
# exit()
# gas = new_plummer_gas_model(Ngas, gasconverter)
# gas = molecular_cloud(targetN=Ngas, convert_nbody=gasconverter).result
# gas.u = temperature_to_u(Tmin)
# gas = read_set_from_file("gas.amuse", "amuse")
# print(stars.mass == stars.mass.max())
print(len(stars.mass))
print(len(stars.mass == stars.mass.max()))
print(stars[0])
print(stars[stars.mass == stars.mass.max()])
mms = stars[stars.mass == stars.mass.max()]
print("Most massive star: %s" % mms.mass)
print("Gas particle mass: %s" % gas[0].mass)
evo = SeBa()
# sph = Fi(converter, mode="openmp")
phantomconverter = nbody_system.nbody_to_si(
default_settings.gas_rscale,
default_settings.gas_mscale,
)
sph = Phantom(phantomconverter, redirection="none")
sph.parameters.ieos = 2
sph.parameters.icooling = 1
sph.parameters.alpha = 0.1
sph.parameters.gamma = 5/3
sph.parameters.rho_crit = 1e17 | units.amu * units.cm**-3
sph.parameters.h_soft_sinkgas = 0.1 | units.parsec
sph.parameters.h_soft_sinksink = 0.1 | units.parsec
sph.parameters.h_acc = 0.1 | units.parsec
# print(sph.parameters)
stars_in_evo = evo.particles.add_particles(stars)
channel_stars_evo_from_code = stars_in_evo.new_channel_to(
stars,
attributes=[
"age", "radius", "mass", "luminosity", "temperature",
"stellar_type",
],
)
channel_stars_evo_from_code.copy()
# try:
# sph.parameters.timestep = dt
# except:
# print("SPH code doesn't support setting the timestep")
sph.parameters.stopping_condition_maximum_density = \
5e-16 | units.g * units.cm**-3
# sph.parameters.beta = 1.
# sph.parameters.C_cour = sph.parameters.C_cour / 4
# sph.parameters.C_force = sph.parameters.C_force / 4
print(sph.parameters)
stars_in_sph = stars.copy() # sph.sink_particles.add_particles(stars)
# stars_in_sph = sph.sink_particles.add_particles(stars)
channel_stars_grav_to_code = stars.new_channel_to(
# sph.sink_particles,
# sph.dm_particles,
stars_in_sph,
attributes=["mass"]
)
channel_stars_grav_from_code = stars_in_sph.new_channel_to(
stars,
attributes=["x", "y", "z", "vx", "vy", "vz"],
)
# We don't want to accrete gas onto the stars/sinks
stars_in_sph.radius = 0 | units.RSun
# stars_in_sph = sph.dm_particles.add_particles(stars)
# try:
# sph.parameters.isothermal_flag = True
# sph.parameters.integrate_entropy_flag = False
# sph.parameters.gamma = 1
# except:
# print("SPH code doesn't support setting isothermal flag")
gas_in_code = sph.gas_particles.add_particles(gas)
# print(gasconverter.to_nbody(gas_in_code[0].u).value_in(nbody_system.specific_energy))
# ui = temperature_to_u(10 | units.K)
# Rg = constants.kB * 6.02214179e+23
# gmwvar = (1.4/1.1) | units.g
# uergg = 1.# | nbody_system.specific_energy
# print("gmwvar = %s"%gasconverter.to_si(gmwvar))
# print("Rg = %s"% gasconverter.to_si(Rg))
# print("ui = %s"% gasconverter.to_si(ui))
# #print("uergg = %s"% gasconverter.to_nbody(uergg))
# print("uergg = %s" % gasconverter.to_si(1 | nbody_system.specific_energy).in_(units.cm**2 * units.s**-2))
# print("****** %s" % ((2.0/3.0)*ui/(Rg/gmwvar/uergg)) + "*****")
# print(gasconverter.to_nbody(Rg))
# print((ui).in_(units.cm**2*units.s**-2))
# #exit()
# sph.evolve_model(1 | units.day)
# write_set_to_file(sph.gas_particles, "gas_initial.hdf5", "amuse")
# exit()
channel_gas_to_code = gas.new_channel_to(
gas_in_code,
attributes=[
"x", "y", "z", "vx", "vy", "vz", "u",
]
)
# mass is never updated, and if sph is in isothermal mode u is not reliable
channel_gas_from_code = gas_in_code.new_channel_to(
gas,
attributes=[
"x", "y", "z", "vx", "vy", "vz", "density", "pressure", "rho",
"u", "h_smooth",
],
)
channel_gas_from_code.copy() # Initialise values for density etc
sph_particle_mass = gas[0].mass # 0.1 | units.MSun
r_max = 0.1 | units.parsec
wind = stellar_wind.new_stellar_wind(
sph_particle_mass,
mode="heating",
r_max=r_max,
derive_from_evolution=True,
tag_gas_source=True,
# target_gas=gas,
# timestep=dt,
)
stars_in_wind = wind.particles.add_particles(stars)
channel_stars_wind_to_code = stars.new_channel_to(
stars_in_wind,
attributes=[
"x", "y", "z", "vx", "vy", "vz", "age", "radius", "mass",
"luminosity", "temperature", "stellar_type",
],
)
channel_stars_wind_to_code.copy()
# reference_mu = 2.2 | units.amu
gasvolume = (4./3.) * numpy.pi * (
gas.position - gas.center_of_mass()
).lengths().mean()**3
rho0 = gas.total_mass() / gasvolume
print(rho0.value_in(units.g * units.cm**-3))
# exit()
# cooling_flag = "thermal_model"
# cooling = Cooling(
cooling = SimplifiedThermalModelEvolver(
# gas_in_code,
gas,
Tmin=Tmin,
# T0=30 | units.K,
# n0=rho0/reference_mu
)
cooling.model_time = sph.model_time
# cooling_to_code = cooling.particles.new_channel_to(gas
start_mass = (
stars.mass.sum()
+ (gas.mass.sum() if not gas.is_empty() else 0 | units.MSun)
)
step = 0
plotnr = 0
com = stars_in_sph.center_of_mass()
plot_hydro_and_stars(
time,
sph,
stars=stars,
sinks=None,
L=20,
# N=100,
filename="phantom-coolthermalwindtestplot-%04i.png" % step,
title="time = %06.2f %s" % (time.value_in(units.Myr), units.Myr),
gasproperties=["density", "temperature"],
# colorbar=True,
starscale=1,
offset_x=com[0].value_in(units.parsec),
offset_y=com[1].value_in(units.parsec),
thickness=5 | units.parsec,
)
dt = dt_base
sph.parameters.time_step = dt
delta_t = phantomconverter.to_si(2**(-16) | nbody_system.time)
print("delta_t: %s" % delta_t.in_(units.day))
# small_step = True
small_step = False
plot_every = 100
subplot_factor = 10
subplot_enabled = False
subplot = 0
while time < time_end:
time += dt
print("Gas mean u: %s" % (gas.u.mean().in_(units.erg/units.MSun)))
print("Evolving to t=%s (%s)" % (time, gasconverter.to_nbody(time)))
step += 1
evo.evolve_model(evo_headstart+time)
print(evo.particles.stellar_type.max())
channel_stars_evo_from_code.copy()
channel_stars_grav_to_code.copy()
if COOLING:
channel_gas_from_code.copy()
cooling.evolve_for(dt/2)
channel_gas_to_code.copy()
print(
"min/max temp in gas: %s %s" % (
u_to_temperature(gas_in_code.u.min()).in_(units.K),
u_to_temperature(gas_in_code.u.max()).in_(units.K),
)
)
if small_step:
# Take small steps until a full timestep is done.
# Each substep is 2* as long as the last until dt is reached
print("Doing small steps")
# print(u_to_temperature(sph.gas_particles[0].u))
# print(sph.gas_particles[0].u)
old_dt = dt_base
substeps = 2**8
dt = old_dt / substeps
dt_done = 0 * old_dt
sph.parameters.time_step = dt
print("adjusted dt to %s, base dt is %s" % (
dt.in_(units.Myr),
dt_base.in_(units.Myr),
)
)
sph.evolve_model(sph.model_time + dt)
dt_done += dt
while dt_done < old_dt:
sph.parameters.time_step = dt
print("adjusted dt to %s, base dt is %s" % (
dt.in_(units.Myr),
dt_base.in_(units.Myr),
)
)
sph.evolve_model(sph.model_time + dt)
dt_done += dt
dt = min(2*dt, old_dt-dt_done)
dt = max(dt, old_dt/substeps)
dt = dt_base
sph.parameters.time_step = dt
print(
"adjusted dt to %s" % sph.parameters.time_step.in_(units.Myr)
)
small_step = False
print("Finished small steps")
# print(u_to_temperature(sph.gas_particles[0].u))
# print(sph.gas_particles[0].u)
# exit()
else:
sph.evolve_model(time)
channel_gas_from_code.copy()
channel_stars_grav_from_code.copy()
u_previous = u_now
u_now = gas.u
gas.du_dt = (u_now - u_previous) / dt
channel_stars_wind_to_code.copy()
wind.evolve_model(time)
# channel_stars_wind_from_code.copy()
if COOLING:
channel_gas_from_code.copy()
cooling.evolve_for(dt/2)
channel_gas_to_code.copy()
if wind.has_new_wind_particles():
subplot_enabled = True
wind_p = wind.create_wind_particles()
# nearest = gas.find_closest_particle_to(wind_p.x, wind_p.y, wind_p.z)
# wind_p.h_smooth = nearest.h_smooth
wind_p.h_smooth = 100 | units.au
print("u: %s / T: %s" % (wind_p.u.mean(), u_to_temperature(wind_p.u.mean())))
# max_e = (1e44 | units.erg) / wind_p[0].mass
# max_e = 10 * gas.u.mean()
# max_e = (1.e48 | units.erg) / wind_p[0].mass
# wind_p[wind_p.u > max_e].u = max_e
# wind_p[wind_p.u > max_e].h_smooth = 0.1 | units.parsec
# print(wind_p.position)
print(
"time: %s, wind energy: %s"
% (time, (wind_p.u * wind_p.mass).sum())
)
print(
"wind temperature: %s"
% (u_to_temperature(wind_p.u))
)
print(
"gas particles: %i (total mass %s)"
% (len(wind_p), wind_p.total_mass())
)
# for windje in wind_p:
# # print(windje)
# source = stars[stars.key == windje.source][0]
# windje.position += source.position
# windje.velocity += source.velocity
# # print(source)
# # print(windje)
# # exit()
gas.add_particles(wind_p)
gas_in_code.add_particles(wind_p)
# for wp in wind_p:
# print(wp)
print("Wind particles added")
if True: # wind_p.u.max() > gas_in_code.u.max():
print("Setting dt to very short")
small_step = True # dt = 0.1 | units.yr
h_min = gas.h_smooth.min()
# delta_t = determine_short_timestep(sph, wind_p, h_min=h_min)
# print("delta_t is set to %s" % delta_t.in_(units.yr))
# else:
# small_step = True
print(
"time: %s sph: %s dM: %s" % (
time.in_(units.Myr),
sph.model_time.in_(units.Myr),
(
stars.total_mass()
+ (
gas.total_mass()
if not gas.is_empty()
else (0 | units.MSun)
)
- start_mass
)
)
)
# com = sph.sink_particles.center_of_mass()
# com = sph.dm_particles.center_of_mass()
com = stars.center_of_mass()
print("STEP: %i step%%plot_every: %i" % (step, step % plot_every))
if step % plot_every == 0:
plotnr = plotnr + 1
plot_hydro_and_stars(
time,
sph,
# stars=sph.sink_particles,
# stars=sph.dm_particles,
stars=stars,
sinks=None,
L=20,
# N=100,
# image_size_scale=10,
filename="phantom-coolthermalwindtestplot-%04i.png" % plotnr, # int(step/plot_every),
title="time = %06.2f %s" % (time.value_in(units.Myr), units.Myr),
gasproperties=["density", "temperature"],
# colorbar=True,
starscale=1,
offset_x=com[0].value_in(units.parsec),
offset_y=com[1].value_in(units.parsec),
thickness=5 | units.parsec,
)
# write_set_to_file(gas, "gas.amuse", "amuse", append_to_file=False)
# write_set_to_file(stars, "stars.amuse", "amuse", append_to_file=False)
elif (
subplot_enabled
and ((step % (plot_every/subplot_factor)) == 0)
):
plotnr = plotnr + 1
subplot += 1
plot_hydro_and_stars(
time,
sph,
# stars=sph.sink_particles,
# stars=sph.dm_particles,
stars=stars,
sinks=None,
L=20,
# N=100,
# image_size_scale=10,
filename="phantom-coolthermalwindtestplot-%04i.png" % plotnr, # int(step/plot_every),
title="time = %06.2f %s" % (time.value_in(units.Myr), units.Myr),
gasproperties=["density", "temperature"],
# colorbar=True,
starscale=1,
offset_x=com[0].value_in(units.parsec),
offset_y=com[1].value_in(units.parsec),
thickness=5 | units.parsec,
)
if subplot % subplot_factor == 0:
subplot_enabled = False
print(
"Average temperature of gas: %s" % (
u_to_temperature(gas.u).mean().in_(units.K)
)
)
return
if __name__ == "__main__":
# set_preferred_units(
# units.MSun, units.parsec, units.kms, units.erg, units.Myr,
# units.erg * units.MSun**-1,
# )
main()